]> ruin.nu Git - hbs.git/blob - bs/fleet.cpp
136ad22ac8a77860265672eae5db422a012eb978
[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 void Fleet::addFleet(std::string unittype, int number, int tick = 0)
207 {
208         int earlier = 0;
209         int ticks = m_Fleet[unittype].size();
210
211         if (ticks != 0)
212                 earlier = m_Fleet[unittype][ticks - 1];
213
214         for (int i = ticks; i <= tick; ++i)
215         {
216                 m_Fleet[unittype].push_back(earlier);
217         }
218         m_Fleet[unittype][tick] += number;
219 }
220
221 //////////////////////////////////////////////////////////////////////////
222 //
223 int      Fleet::fleet(string unittype, int tick = 0)
224 {
225         int ticks = m_Fleet[unittype].size();
226         if (ticks == 0)
227                 return 0;
228
229         --ticks;
230
231         if (ticks < tick)
232                 return m_Fleet[unittype][ticks];
233
234         return m_Fleet[unittype][tick];
235 }
236
237 //////////////////////////////////////////////////////////////////////////
238 //
239 int Fleet::blockedFleet(std::string unittype, int tick = 0)
240 {
241         int ticks = m_BlockedFleet[unittype].size();
242         if (ticks == 0)
243                 return 0;
244
245         --ticks;
246
247         if (ticks < tick)
248                 return 0;
249
250         return m_BlockedFleet[unittype][tick];
251 }
252
253 //////////////////////////////////////////////////////////////////////////
254 //
255 void Fleet::setBlockedFleet(std::string unittype, int number, int tick = 0)
256 {
257         int ticks = m_BlockedFleet[unittype].size();
258
259         for (int i = ticks; i <= tick; ++i)
260         {
261                 m_BlockedFleet[unittype].push_back(0);
262         }
263         m_BlockedFleet[unittype][tick] = number;
264
265         cerr << "This fleet got " << m_BlockedFleet[unittype][tick] << " blocked units tick: " << tick << endl;
266
267 }
268
269 //////////////////////////////////////////////////////////////////////////
270 //FIXME
271 void Fleet::addToThis(std::vector<Fleet*> fleets, int tick = 0)
272 {
273         for (UnitList::iterator i = s_Units.begin();  i != s_Units.end(); ++i)
274         {
275                 if (m_Fleet[i->first].size() == 0)
276                                 m_Fleet[i->first].push_back(0);
277
278                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
279                 {
280                         int num = (*j)->fleet(i->first, tick);
281                         m_Fleet[i->first][0] += num;
282                         if (num > 0)
283                                 cerr << (*j)->name() <<  " adding " << num << " units of type " << i->first << endl;
284                 }
285         }
286 }
287
288 //////////////////////////////////////////////////////////////////////////
289 //
290 void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0)
291 {
292         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
293         {
294                 string unittype = i->first;
295
296
297                 if (m_Fleet[unittype].size() < 1)
298                         continue;
299                 if (m_Fleet[unittype][0] == 0)
300                         continue;
301
302                  
303                 int totallost = fleet(unittype,1) - fleet(unittype, 0);
304
305
306                 cerr << "Distributing type: " << unittype << " with a total loss of " << totallost << " units" << endl;
307
308                 cerr << "Total number of units before: " << fleet(unittype, 0)  << " and after : " << fleet(unittype, 1) << endl;
309                 
310                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
311                 {
312                         int fl1 = (*j)->fleet(unittype, tick - 1);
313                         float part = float(fl1) / fleet(unittype, 0) ;
314                         int lost =  totallost * part;
315                         (*j)->setFleet(unittype, (*j)->fleet(unittype, tick - 1) + lost, tick);
316
317                         cerr << (*j)->name() << " gaining " << lost << " " << unittype  << " since it's " << part * 100 << "% of the whole fleet, and it had : " << fl1 << " units last tick.." <<  endl;
318
319                         lost = part * blockedFleet(unittype, 0);
320
321                         cerr << (*j)->name() << " got " << lost << " blocked " << unittype  << ", the total number of blocked ships was: " << blockedFleet(unittype, 0) << endl; 
322
323                         (*j)->setBlockedFleet(unittype, lost, tick);
324                 }
325         }
326 }
327
328 //////////////////////////////////////////////////////////////////////////
329 //
330 std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int stays = 0, int tick = 0)
331 {
332         vector<Fleet*> fl;
333         for (vector<Fleet*>::iterator i = fleets.begin(); i != fleets.end(); ++i)
334         {
335                 if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < stays)
336                 {
337                         fl.push_back((*i));
338                         cerr << "Using fleet " << (*i)->name() << " for tick " << tick << endl;
339                 }
340                 else if ((*i)->name() == "Home Planet")
341                         fl.push_back((*i));
342         }
343         return fl;
344 }
345
346 //////////////////////////////////////////////////////////////////////////
347 //
348 int Fleet::freeFleet(std:: string unittype, int tick = 0)
349 {
350         int bticks = m_BlockedFleet[unittype].size();
351
352         --bticks;
353
354         if (bticks < tick)
355                 return fleet(unittype, tick);
356
357
358         int free = fleet(unittype,tick) - m_BlockedFleet[unittype][tick];
359         if (free < 0)
360                 return 0;
361         return free;
362 }
363
364
365 //////////////////////////////////////////////////////////////////////////
366 //
367 void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
368 {
369
370         float guns = s_Units[unittype].guns() * number;
371
372         
373         if (guns == 0)
374                 return;
375
376         cerr << number << " " << unittype << ": with " << guns << " guns\n";
377
378         float gunsleft = guns;
379         for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
380         {
381                 string ta = s_Units[unittype].target(count);
382         cerr << "Shooting at target class: " << ta << endl;
383                 while (gunsleft > 0)
384                 {
385         
386                         map<string, int*> targets;
387
388                         for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
389                         {
390
391                                 if (m_Fleet[j->first].size() == 0)
392                                         continue;
393
394
395                                 if (m_Fleet[j->first].size() == 1 )
396                                         m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
397
398                                 //cerr << "Target is class: " << j->second.type() << endl;
399
400                                 if (m_Fleet[j->first][1] > 0  && ( ta == j->second.unitClass() || ta == "All"))
401                                 {
402
403                                 //      cerr << "Looking at target: " << j->first << endl;
404                                         targets[j->first] = &m_Fleet[j->first][1];
405                                 }
406
407                         }
408
409                         if (targets.size() == 0)
410                                 break;
411
412                         int total = 0;
413                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
414                                 total += (*j->second);
415
416                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
417                         {
418                                 float maxguns = float((*j->second))/total * guns;
419                                 //cerr << "Now shooting at target: " << j->first << endl;
420
421                                 if (m_Armor[j->first] <= 0 || m_Armor[j->first] > s_Units[j->first].armor())
422                                         m_Armor[j->first] = s_Units[j->first].armor();
423                                 double k = maxguns;
424
425                                 //cerr << "Targets agility: " << s_Units[j->first].agility() << endl;
426                                 //cerr << "Weaponspeed: " << s_Units[unittype].weaponSpeed() << endl;
427                                 while (k > 0)   
428                                 {
429
430                                         if (*(j->second) <= 0)
431                                                 break;
432
433                                         int wpsp = s_Units[unittype].weaponSpeed();
434                                         int agil = s_Units[j->first].agility();
435
436                                         k -= float(100)/(25 + wpsp - agil);
437                                         //cout << "Used " << blaha << " guns to hit with one shot.\n";
438                                         //cout << "WPSP: " << wpsp << "\nAgil: " << agil << endl;
439
440                                         m_Armor[j->first] -= s_Units[unittype].power();
441                                         if (m_Armor[j->first] <= 0)
442                                         {
443                                                 m_Armor[j->first] = s_Units[j->first].armor();
444                                                 (*j->second)--;
445                                                 hitunits[j->first]++;
446                                                 
447                                                 //There is a chance that we're hitting a blocked ship.
448                                                 if (m_BlockedFleet[j->first].size() >= 1)
449                                                 {
450                                                         int test = rand() % m_BlockedFleet[j->first][0];
451                                                         if (test == 1
452                                                                 && m_BlockedFleet[j->first][0] > 0)
453                                                         {
454                                                                 if (m_BlockedFleet[j->first].size() == 1)
455                                                                         m_BlockedFleet[j->first].push_back(m_BlockedFleet[j->first][0] - 1);
456                                                                 else if (m_BlockedFleet[j->first][1] > 0)
457                                                                         m_BlockedFleet[j->first][1]--;
458                                                         }
459                                                 }
460                                         }
461
462                                 }
463
464                                 cerr << hitunits[j->first] << " units of type: " << j->first << "killed\n";
465                                 if (k <= 0)
466                                         gunsleft -= maxguns;
467                                 else
468                                         gunsleft -= maxguns - k;
469                         }
470                 }
471         }
472 }
473
474 //////////////////////////////////////////////////////////////////////////
475 //
476 void Fleet::takeEMP(std::string unittype, int number)
477 {
478         int guns = s_Units[unittype].guns() * number;
479         if (guns == 0)
480                 return;
481
482         cerr << unittype << ": with " << guns << " guns\n";
483
484         float gunsleft = guns;
485         for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
486         {
487                 string ta = s_Units[unittype].target(count);
488         cerr << "Shooting at target class: " << ta << endl;
489                 while (gunsleft > 0)
490                 {
491         
492                         map<string, int*> targets;
493
494                         for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
495                         {
496                                 if (j->second.type() == "PDS")
497                                         continue;
498
499                                 if (freeFleet(j->first, 1) <= 0)
500                                         continue;
501
502                                 if (m_Fleet[j->first].size() == 0)
503                                         continue;
504
505                                 if (m_Fleet[j->first].size() == 1)
506                                         m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
507
508                                 //cerr << "Target is class: " << j->second.type() << endl;
509
510                                 if (m_Fleet[j->first][1] > 0  && ( ta == j->second.unitClass() || ta == "All"))
511                                 {
512
513                                 //      cerr << "Looking at target: " << j->first << endl;
514                                         targets[j->first] = &m_Fleet[j->first][1];
515                                 }
516
517                         }
518
519                         if (targets.size() == 0)
520                                 break;
521
522                         int total = 0;
523                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
524                                 total += (*j->second);
525
526                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
527                         {
528                                 float maxguns = float((*j->second))/total * guns;
529                                 cerr << "Now shooting at target: " << j->first << endl;
530
531                                 double k = maxguns;
532
533                                 int hits = 0;
534
535                                 while (k > 0)   
536                                 {
537
538                                         if (*(j->second) <= blockedFleet(j->first, 1))
539                                                 break;
540
541                                         int eres = s_Units[j->first].EMP();
542
543                                         k -= float(100)/(100-eres);
544                                         hits++;
545                                         blockFleet(j->first, 1);
546                                 }
547
548                                 cerr << hits << " units of type: " << j->first << " blocked\n";
549                                 if (k <= 0)
550                                         gunsleft -= maxguns;
551                                 else
552                                         gunsleft -= maxguns - k;
553                         }
554                 }
555         }
556 }
557
558 //////////////////////////////////////////////////////////////////////////
559 //
560 void Fleet::killFleet(std::string unittype, int number, int tick = 0)
561 {
562         int earlier = 0;
563         int ticks = m_Fleet[unittype].size();
564
565         if (ticks != 0)
566                 earlier = m_Fleet[unittype][ticks - 1];
567
568         for (int i = ticks; i <= tick; ++i)
569         {
570                 m_Fleet[unittype].push_back(earlier);
571         }
572         m_Fleet[unittype][tick] -= number;
573 }
574
575 //////////////////////////////////////////////////////////////////////////
576 //
577 void Fleet::setResource(std::string type, int number, int tick = 0)
578 {
579
580         int ticks = m_Resources[type].size();
581         for (int i = ticks; i <= tick; ++i)
582                 m_Resources[type].push_back(number);
583         m_Resources[type][tick] = number;
584 }
585
586 //////////////////////////////////////////////////////////////////////////
587 //
588 void Fleet::addResource(std::string type, int number, int tick = 0)
589 {
590
591         int ticks = m_Resources[type].size();
592         int latest = resource(type, tick - 1);
593
594         for (int i = ticks; i <= tick; ++i)
595                 m_Resources[type].push_back(latest);
596         m_Resources[type][tick] += number;
597 }
598
599 //////////////////////////////////////////////////////////////////////////
600 //
601 int Fleet::resource(std::string type, int tick = 0) const
602 {
603         if (tick < 0)
604                 return 0;
605
606         vector<int>const* resource = 0;
607         for (ResourceList::const_iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
608         {
609                 if (i->first == type)
610                 {
611                         resource = &i->second;
612                         break;
613                 }
614         }
615         if (resource == 0)
616                 return 0;
617
618         int ticks = resource->size();
619
620         if( ticks == 0)
621                 return 0;
622
623         --ticks;
624
625         if (ticks < tick)
626                 return resource->at(ticks);
627         return resource->at(tick);
628 }
629
630 //////////////////////////////////////////////////////////////////////////
631 //
632 void Fleet::resetResources()
633 {
634         m_Resources.clear() ;   
635 }
636
637 //////////////////////////////////////////////////////////////////////////
638 //
639 void Fleet::printFleet()
640 {
641         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
642         {
643                 for (int tick = 0; tick < 5 ;++tick)
644                 {
645                         int num = fleet(i->first, tick);
646
647                         if (num <= 0)
648                                 break;
649                         cerr << num << " " << i->first << " during tick: " << tick << endl;
650                 }
651         }
652 }
653
654 //////////////////////////////////////////////////////////////////////////
655 //
656 void Fleet::blockFleet(std::string unittype, int number, int tick = 0)
657 {
658         if (m_BlockedFleet[unittype].size() >= 1)
659         {
660                 m_BlockedFleet[unittype][0] += number;
661                 if (m_BlockedFleet[unittype].size() > 1)
662                         m_BlockedFleet[unittype][1] += number;
663                 else
664                         m_BlockedFleet[unittype].push_back(m_BlockedFleet[unittype][0]);
665         }
666         else
667         {
668                 m_BlockedFleet[unittype].push_back(number);
669                 m_BlockedFleet[unittype].push_back(number);
670         }
671 }
672
673 //////////////////////////////////////////////////////////////////////////
674 //
675 void Fleet::distributeCappedRoids(std::vector<Fleet*> fleets, int tick = 0)
676 {
677         for (ResourceList::iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
678         {
679                 string res = i->first;
680
681
682                 cerr << "Distributing type: " << res << endl;
683                 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
684                         cout << (*j) << endl;
685
686                 if (m_Resources[res].size() < 2)
687                         continue;
688                 if (m_Resources[res][1] == 0)
689                         continue;
690
691                  
692                 int totcapped = resource(res,1) - resource(res, 0);
693
694
695                 cerr << "Distributing type: " << res << " with a total gain of " << totcapped << " roids" << endl;
696
697                 cerr << "Total number of roids before: " << resource(res, 0)  << " and after : " << resource(res, 1) << endl;
698                 
699                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
700                 {
701                         unsigned fl1 = (*j)->score(tick - 1);
702                         float part = float(fl1) / score(0) ;
703                         int lost =  totcapped * part;
704
705                         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;
706
707                         //(*j)->setResource(res, (*j)->resource(res,tick-1) + lost, tick);
708                         (*j)->addResource(res,lost, tick);
709                 }
710         }
711 }
712
713 //////////////////////////////////////////////////////////////////////////
714 //
715
716 void Fleet::addFleet(std::map<string, int> units, int tick = 0)
717 {
718         for (map<string, int>::iterator i = units.begin(); i != units.end(); ++i)
719                 addFleet(i->first, i->second, tick);
720 }