]> ruin.nu Git - hbs.git/blob - bs/fleet.cpp
a15fba36788628798d041ce50c4745585d590737
[hbs.git] / bs / fleet.cpp
1 /***************************************************************************
2                           fleet.cpp  -  description
3                              -------------------
4     begin                : Tue Jan 22 2002
5     copyright            : (C) 2002 by Michael Andreen
6     email                : whale@linux.nu
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include "fleet.h"
19
20 #include <iostream>
21 #include <cstdlib>
22 using namespace std;
23
24 //Static variables
25 map<string, vector<int> > Fleet::s_Races;
26 UnitList Fleet::s_Units;
27
28 Fleet::Fleet()
29 {
30         m_iETA = 0;
31         m_sRace = "Cathaar";
32 }
33
34 Fleet::~Fleet(){
35 }
36
37 //////////////////////////////////////////////////////////////////////////
38 //
39 void Fleet::setName(string sName)
40 {
41         m_sName = sName;
42 }
43
44 //////////////////////////////////////////////////////////////////////////
45 //
46 string Fleet::name() const
47 {
48         return m_sName;
49 }
50
51 //////////////////////////////////////////////////////////////////////////
52 //
53 /** This function first sets the race, then it iterates through the the 
54  * s_Races and checks if it finds the race it returns true, if it reaches
55  * the end without finding it it returns false.
56  */
57 bool Fleet::setRace(string sRace)
58 {
59         m_sRace = sRace;
60         for (map<string, vector<int> >::iterator i = s_Races.begin(); i != s_Races.end(); i++)
61         {
62                 if (m_sRace == (*i).first)
63                         return true;
64         }
65         return false;
66 }
67
68 //////////////////////////////////////////////////////////////////////////
69 //
70 string Fleet::race() const
71 {
72         return m_sRace;
73 }
74
75 //////////////////////////////////////////////////////////////////////////
76 //
77 /** This function iterates through m_Fleet and adds all numbers together to
78  * produce a total.
79  */
80 int Fleet::numberOfShips() const
81 {
82         int total = 0;
83
84         for (map<string, vector<int> >::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
85         {
86                 if (i->second.size() != 0)
87                         total += i->second[0];
88         }
89
90         return total;
91 }
92
93 //////////////////////////////////////////////////////////////////////////
94 //
95 void Fleet::setETA(int eta)
96 {
97         m_iETA = eta;
98 }
99
100 //////////////////////////////////////////////////////////////////////////
101 //
102 int  Fleet::ETA() const
103 {
104         return m_iETA;
105 }
106
107 //////////////////////////////////////////////////////////////////////////
108 //
109 void Fleet::setRaces(map<string, vector<int> >& races)
110 {
111         s_Races = races;
112 }
113
114 //////////////////////////////////////////////////////////////////////////
115 //
116 void Fleet::setUnits(UnitList& units)
117 {
118         s_Units = units;
119
120         
121 /*
122         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); i++)
123         {
124                 cerr << s_Units[(*i).first].Name() << "\t\t"
125                         << s_Units[(*i).first].race() <<"\t"
126                         << s_Units[(*i).first].unitClass() << "\t"
127                         << s_Units[(*i).first].target(0) << "\t"
128                         << s_Units[(*i).first].target(1) << "\t"
129                         << s_Units[(*i).first].target(2) << "\t"
130                         << s_Units[(*i).first].initiative() << "\t"
131                         << s_Units[(*i).first].agility() << "\t"
132                         << s_Units[(*i).first].weaponSpeed() << "\t"
133                         << s_Units[(*i).first].guns() << "\t"
134                         << s_Units[(*i).first].power() << "\t"
135                         << s_Units[(*i).first].armor() << "\t"
136                         << s_Units[(*i).first].EMP() << "\t"
137                         << s_Units[(*i).first].totRes() << "\t"
138                         << s_Units[(*i).first].fuel() << "\t"
139                         << s_Units[(*i).first].ETA() << "\t"
140                         << s_Units[(*i).first].type() << endl;
141         }
142 */      
143 }
144
145 //////////////////////////////////////////////////////////////////////////
146 //
147 const map<string, vector<int> >& Fleet::Races()
148 {
149         return s_Races;
150 }
151
152 //////////////////////////////////////////////////////////////////////////
153 //
154 const UnitList& Fleet::Units()
155 {
156         return s_Units;
157 }
158
159 //////////////////////////////////////////////////////////////////////////
160 //
161 vector<int> Fleet::RacesAllowed() const
162 {
163         return s_Races[m_sRace];
164 }
165
166 //////////////////////////////////////////////////////////////////////////
167 //
168 unsigned Fleet::score(int tick = 0) const
169 {
170         unsigned tot_score = 0;
171
172         for (FleetList::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
173         {
174                 int ticks =     i->second.size();
175                 if (ticks == 0)
176                         continue;
177                 --ticks;
178                 if ( ticks < tick)
179                         tot_score += i->second[ticks] * s_Units[i->first].totRes() / 10;
180                 else
181                         tot_score += i->second[tick] * s_Units[i->first].totRes() / 10;
182         }
183
184         return tot_score;
185 }
186
187 //////////////////////////////////////////////////////////////////////////
188 //
189 void Fleet::setFleet(string unittype, int number, int tick = 0)
190 {
191         int earlier = 0;
192         int ticks = m_Fleet[unittype].size();
193
194         if (ticks != 0)
195                 earlier = m_Fleet[unittype][ticks - 1];
196
197         for (int i = ticks; i <= tick; ++i)
198         {
199                 m_Fleet[unittype].push_back(earlier);
200         }
201         m_Fleet[unittype][tick] = number;
202 }
203
204 //////////////////////////////////////////////////////////////////////////
205 //
206 int      Fleet::fleet(string unittype, int tick = 0)
207 {
208         int ticks = m_Fleet[unittype].size();
209         if (ticks == 0)
210                 return 0;
211
212         --ticks;
213
214         if (ticks < tick)
215                 return m_Fleet[unittype][ticks];
216
217         return m_Fleet[unittype][tick];
218 }
219
220 //////////////////////////////////////////////////////////////////////////
221 //
222 int Fleet::blockedFleet(std::string unittype, int tick = 0)
223 {
224         int ticks = m_BlockedFleet[unittype].size();
225         if (ticks == 0)
226                 return 0;
227
228         --ticks;
229
230         if (ticks < tick)
231                 return 0;
232
233         return m_BlockedFleet[unittype][tick];
234 }
235
236 //////////////////////////////////////////////////////////////////////////
237 //
238 void Fleet::setBlockedFleet(std::string unittype, int number, int tick = 0)
239 {
240         int ticks = m_BlockedFleet[unittype].size();
241
242         for (int i = ticks; i <= tick; ++i)
243         {
244                 m_BlockedFleet[unittype].push_back(0);
245         }
246         m_BlockedFleet[unittype][tick] = number;
247
248         cerr << "This fleet got " << m_BlockedFleet[unittype][tick] << " blocked units tick: " << tick << endl;
249
250 }
251
252 //////////////////////////////////////////////////////////////////////////
253 //FIXME
254 void Fleet::addToThis(std::vector<Fleet*> fleets, int tick = 0)
255 {
256         for (UnitList::iterator i = s_Units.begin();  i != s_Units.end(); ++i)
257         {
258                 if (m_Fleet[i->first].size() == 0)
259                                 m_Fleet[i->first].push_back(0);
260
261                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
262                 {
263                         int num = (*j)->fleet(i->first, tick);
264                         m_Fleet[i->first][0] += num;
265                         if (num > 0)
266                                 cerr << (*j)->name() <<  " adding " << num << " units of type " << i->first << endl;
267                 }
268         }
269 }
270
271 //////////////////////////////////////////////////////////////////////////
272 //
273 void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0)
274 {
275         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
276         {
277                 string unittype = i->first;
278
279
280                 if (m_Fleet[unittype].size() < 1)
281                         continue;
282                 if (m_Fleet[unittype][0] == 0)
283                         continue;
284
285                  
286                 int totallost = fleet(unittype,1) - fleet(unittype, 0);
287
288
289                 cerr << "Distributing type: " << unittype << " with a total loss of " << totallost << " units" << endl;
290
291                 cerr << "Total number of units before: " << fleet(unittype, 0)  << " and after : " << fleet(unittype, 1) << endl;
292                 
293                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
294                 {
295                         int fl1 = (*j)->fleet(unittype, tick - 1);
296                         float part = float(fl1) / fleet(unittype, 0) ;
297                         int lost =  totallost * part;
298                         (*j)->setFleet(unittype, (*j)->fleet(unittype, tick - 1) + lost, tick);
299
300                         cerr << (*j)->name() << " gaining " << lost << " " << unittype  << " since it's " << part * 100 << "% of the whole fleet, and it had : " << fl1 << " units last tick.." <<  endl;
301
302                         lost = part * blockedFleet(unittype, 0);
303
304                         cerr << (*j)->name() << " got " << lost << " blocked " << unittype  << ", the total number of blocked ships was: " << blockedFleet(unittype, 0) << endl; 
305
306                         (*j)->setBlockedFleet(unittype, lost, tick);
307                 }
308         }
309 }
310
311 //////////////////////////////////////////////////////////////////////////
312 //
313 std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int stays = 0, int tick = 0)
314 {
315         vector<Fleet*> fl;
316         for (vector<Fleet*>::iterator i = fleets.begin(); i != fleets.end(); ++i)
317         {
318                 if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < stays)
319                 {
320                         fl.push_back((*i));
321                         cerr << "Using fleet " << (*i)->name() << " for tick " << tick << endl;
322                 }
323                 else if ((*i)->name() == "Home Planet")
324                         fl.push_back((*i));
325         }
326         return fl;
327 }
328
329 //////////////////////////////////////////////////////////////////////////
330 //
331 int Fleet::freeFleet(std:: string unittype, int tick = 0)
332 {
333         int bticks = m_BlockedFleet[unittype].size();
334
335         --bticks;
336
337         if (bticks < tick)
338                 return fleet(unittype, tick);
339
340
341         int free = fleet(unittype,tick) - m_BlockedFleet[unittype][tick];
342         if (free < 0)
343                 return 0;
344         return free;
345 }
346
347
348 //////////////////////////////////////////////////////////////////////////
349 //
350 void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
351 {
352
353         float guns = s_Units[unittype].guns() * number;
354
355         
356         if (guns == 0)
357                 return;
358
359         cerr << number << " " << unittype << ": with " << guns << " guns\n";
360
361         float gunsleft = guns;
362         for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
363         {
364                 string ta = s_Units[unittype].target(count);
365         cerr << "Shooting at target class: " << ta << endl;
366                 while (gunsleft > 0)
367                 {
368         
369                         map<string, int*> targets;
370
371                         for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
372                         {
373
374                                 if (m_Fleet[j->first].size() == 0)
375                                         continue;
376
377
378                                 if (m_Fleet[j->first].size() == 1 )
379                                         m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
380
381                                 //cerr << "Target is class: " << j->second.type() << endl;
382
383                                 if (m_Fleet[j->first][1] > 0  && ( ta == j->second.unitClass() || ta == "All"))
384                                 {
385
386                                 //      cerr << "Looking at target: " << j->first << endl;
387                                         targets[j->first] = &m_Fleet[j->first][1];
388                                 }
389
390                         }
391
392                         if (targets.size() == 0)
393                                 break;
394
395                         int total = 0;
396                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
397                                 total += (*j->second);
398
399                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
400                         {
401                                 float maxguns = float((*j->second))/total * guns;
402                                 //cerr << "Now shooting at target: " << j->first << endl;
403
404                                 if (m_Armor[j->first] <= 0 || m_Armor[j->first] > s_Units[j->first].armor())
405                                         m_Armor[j->first] = s_Units[j->first].armor();
406                                 double k = maxguns;
407
408                                 //cerr << "Targets agility: " << s_Units[j->first].agility() << endl;
409                                 //cerr << "Weaponspeed: " << s_Units[unittype].weaponSpeed() << endl;
410                                 while (k > 0)   
411                                 {
412
413                                         if (*(j->second) <= 0)
414                                                 break;
415
416                                         int wpsp = s_Units[unittype].weaponSpeed();
417                                         int agil = s_Units[j->first].agility();
418
419                                         k -= float(100)/(25 + wpsp - agil);
420                                         //cout << "Used " << blaha << " guns to hit with one shot.\n";
421                                         //cout << "WPSP: " << wpsp << "\nAgil: " << agil << endl;
422
423                                         m_Armor[j->first] -= s_Units[unittype].power();
424                                         if (m_Armor[j->first] <= 0)
425                                         {
426                                                 m_Armor[j->first] = s_Units[j->first].armor();
427                                                 (*j->second)--;
428                                                 hitunits[j->first]++;
429                                                 
430                                                 //There is a chance that we're hitting a blocked ship.
431                                                 if (m_BlockedFleet[j->first].size() >= 1)
432                                                 {
433                                                         int test = rand() % m_BlockedFleet[j->first][0];
434                                                         if (test == 1
435                                                                 && m_BlockedFleet[j->first][0] > 0)
436                                                         {
437                                                                 if (m_BlockedFleet[j->first].size() == 1)
438                                                                         m_BlockedFleet[j->first].push_back(m_BlockedFleet[j->first][0] - 1);
439                                                                 else if (m_BlockedFleet[j->first][1] > 0)
440                                                                         m_BlockedFleet[j->first][1]--;
441                                                         }
442                                                 }
443                                         }
444
445                                 }
446
447                                 cerr << hitunits[j->first] << " units of type: " << j->first << "killed\n";
448                                 if (k <= 0)
449                                         gunsleft -= maxguns;
450                                 else
451                                         gunsleft -= maxguns - k;
452                         }
453                 }
454         }
455 }
456
457 //////////////////////////////////////////////////////////////////////////
458 //
459 void Fleet::takeEMP(std::string unittype, int number)
460 {
461         int guns = s_Units[unittype].guns() * number;
462         if (guns == 0)
463                 return;
464
465         cerr << unittype << ": with " << guns << " guns\n";
466
467         float gunsleft = guns;
468         for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
469         {
470                 string ta = s_Units[unittype].target(count);
471         cerr << "Shooting at target class: " << ta << endl;
472                 while (gunsleft > 0)
473                 {
474         
475                         map<string, int*> targets;
476
477                         for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
478                         {
479                                 if (j->second.type() == "PDS")
480                                         continue;
481
482                                 if (freeFleet(j->first, 1) <= 0)
483                                         continue;
484
485                                 if (m_Fleet[j->first].size() == 0)
486                                         continue;
487
488                                 if (m_Fleet[j->first].size() == 1)
489                                         m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
490
491                                 //cerr << "Target is class: " << j->second.type() << endl;
492
493                                 if (m_Fleet[j->first][1] > 0  && ( ta == j->second.unitClass() || ta == "All"))
494                                 {
495
496                                 //      cerr << "Looking at target: " << j->first << endl;
497                                         targets[j->first] = &m_Fleet[j->first][1];
498                                 }
499
500                         }
501
502                         if (targets.size() == 0)
503                                 break;
504
505                         int total = 0;
506                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
507                                 total += (*j->second);
508
509                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
510                         {
511                                 float maxguns = float((*j->second))/total * guns;
512                                 cerr << "Now shooting at target: " << j->first << endl;
513
514                                 double k = maxguns;
515
516                                 int hits = 0;
517
518                                 while (k > 0)   
519                                 {
520
521                                         if (*(j->second) <= blockedFleet(j->first, 1))
522                                                 break;
523
524                                         int eres = s_Units[j->first].EMP();
525
526                                         k -= float(100)/(100-eres);
527                                         hits++;
528                                         blockFleet(j->first, 1);
529                                 }
530
531                                 cerr << hits << " units of type: " << j->first << " blocked\n";
532                                 if (k <= 0)
533                                         gunsleft -= maxguns;
534                                 else
535                                         gunsleft -= maxguns - k;
536                         }
537                 }
538         }
539 }
540
541 //////////////////////////////////////////////////////////////////////////
542 //
543 void Fleet::killFleet(std::string unittype, int number, int tick = 0)
544 {
545         int earlier = 0;
546         int ticks = m_Fleet[unittype].size();
547
548         if (ticks != 0)
549                 earlier = m_Fleet[unittype][ticks - 1];
550
551         for (int i = ticks; i <= tick; ++i)
552         {
553                 m_Fleet[unittype].push_back(earlier);
554         }
555         m_Fleet[unittype][tick] -= number;
556 }
557
558 //////////////////////////////////////////////////////////////////////////
559 //
560 void Fleet::setResource(std::string type, int number, int tick = 0)
561 {
562
563         int ticks = m_Resources[type].size();
564         for (int i = ticks; i <= tick; ++i)
565                 m_Resources[type].push_back(number);
566         m_Resources[type][tick] = number;
567 }
568
569 //////////////////////////////////////////////////////////////////////////
570 //
571 void Fleet::addResource(std::string type, int number, int tick = 0)
572 {
573
574         int ticks = m_Resources[type].size();
575         int latest = 0;
576
577         if (ticks > 0)
578                 latest = m_Resources[type][ticks - 1];
579
580         for (int i = ticks; i <= tick; ++i)
581                 m_Resources[type].push_back(latest);
582         m_Resources[type][tick] += number;
583 }
584
585 //////////////////////////////////////////////////////////////////////////
586 //
587 int Fleet::resource(std::string type, int tick = 0) const
588 {
589         vector<int>const* resource = 0;
590         for (ResourceList::const_iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
591         {
592                 if (i->first == type)
593                 {
594                         resource = &i->second;
595                         break;
596                 }
597         }
598         if (resource == 0)
599                 return 0;
600
601         int ticks = resource->size();
602
603         if( ticks == 0)
604                 return 0;
605
606         --ticks;
607
608         if (ticks < tick)
609                 return resource->at(ticks);
610         return resource->at(tick);
611 }
612
613 //////////////////////////////////////////////////////////////////////////
614 //
615 void Fleet::printFleet()
616 {
617         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
618         {
619                 for (int tick = 0; tick < 5 ;++tick)
620                 {
621                         int num = fleet(i->first, tick);
622
623                         if (num <= 0)
624                                 break;
625                         cerr << num << " " << i->first << " during tick: " << tick << endl;
626                 }
627         }
628 }
629
630 //////////////////////////////////////////////////////////////////////////
631 //
632 void Fleet::blockFleet(std::string unittype, int number, int tick = 0)
633 {
634         if (m_BlockedFleet[unittype].size() >= 1)
635         {
636                 m_BlockedFleet[unittype][0] += number;
637                 if (m_BlockedFleet[unittype].size() > 1)
638                         m_BlockedFleet[unittype][1] += number;
639                 else
640                         m_BlockedFleet[unittype].push_back(m_BlockedFleet[unittype][0]);
641         }
642         else
643         {
644                 m_BlockedFleet[unittype].push_back(number);
645                 m_BlockedFleet[unittype].push_back(number);
646         }
647 }
648
649 //////////////////////////////////////////////////////////////////////////
650 //
651 void Fleet::distributeCappedRoids(std::vector<Fleet*> fleets, int tick = 0)
652 {
653         for (ResourceList::iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
654         {
655                 string res = i->first;
656
657                 cerr << "Distributing type: " << res << endl;
658                 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
659                         cout << (*j) << endl;
660
661                 if (m_Resources[res].size() < 2)
662                         continue;
663                 if (m_Resources[res][1] == 0)
664                         continue;
665
666                  
667                 int totcapped = resource(res,1) - resource(res, 0);
668
669
670                 cerr << "Distributing type: " << res << " with a total gain of " << totcapped << " roids" << endl;
671
672                 cerr << "Total number of roids before: " << resource(res, 0)  << " and after : " << resource(res, 1) << endl;
673                 
674                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
675                 {
676                         unsigned fl1 = (*j)->score(tick - 1);
677                         float part = float(fl1) / score(0) ;
678                         int lost =  totcapped * part;
679
680                         cerr << (*j)->name() << " gaining " << lost << " " << res  << " since it's " << part * 100 << "% of the whole score, and it had : " << fl1 << " score last tick.. compared to fleet total of: " << score(0) <<  endl;
681                         (*j)->setResource(res, (*j)->resource(res,tick-1) + lost, tick);
682                 }
683         }
684 }
685
686