]> ruin.nu Git - hbs.git/blob - bs/fleet.cpp
changed the capping functionallity..
[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_iStays = 3;
32         m_sRace = "Cathaar";
33 }
34
35 Fleet::~Fleet(){
36 }
37
38 //////////////////////////////////////////////////////////////////////////
39 //
40 void Fleet::setName(string sName)
41 {
42         m_sName = sName;
43 }
44
45 //////////////////////////////////////////////////////////////////////////
46 //
47 string Fleet::name() const
48 {
49         return m_sName;
50 }
51
52 //////////////////////////////////////////////////////////////////////////
53 //
54 /** This function first sets the race, then it iterates through the the 
55  * s_Races and checks if it finds the race it returns true, if it reaches
56  * the end without finding it it returns false.
57  */
58 bool Fleet::setRace(string sRace)
59 {
60         m_sRace = sRace;
61         for (map<string, vector<int> >::iterator i = s_Races.begin(); i != s_Races.end(); i++)
62         {
63                 if (m_sRace == (*i).first)
64                         return true;
65         }
66         return false;
67 }
68
69 //////////////////////////////////////////////////////////////////////////
70 //
71 string Fleet::race() const
72 {
73         return m_sRace;
74 }
75
76 //////////////////////////////////////////////////////////////////////////
77 //
78 /** This function iterates through m_Fleet and adds all numbers together to
79  * produce a total.
80  */
81 int Fleet::numberOfShips() const
82 {
83         int total = 0;
84
85         for (map<string, vector<int> >::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
86         {
87                 if (i->second.size() != 0)
88                         total += i->second[0];
89         }
90
91         return total;
92 }
93
94 //////////////////////////////////////////////////////////////////////////
95 //
96 void Fleet::setETA(int eta)
97 {
98         m_iETA = eta;
99 }
100
101 //////////////////////////////////////////////////////////////////////////
102 //
103 int  Fleet::ETA() const
104 {
105         return m_iETA;
106 }
107
108 //////////////////////////////////////////////////////////////////////////
109 //
110 void Fleet::setRaces(map<string, vector<int> >& races)
111 {
112         s_Races = races;
113 }
114
115 //////////////////////////////////////////////////////////////////////////
116 //
117 void Fleet::setUnits(UnitList& units)
118 {
119         s_Units = units;
120
121
122         
123            for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); i++)
124            {
125                    cerr << s_Units[(*i).first].Name() << "\t\t"
126 /*                 << s_Units[(*i).first].race() <<"\t"
127                    << s_Units[(*i).first].unitClass() << "\t"
128                    << s_Units[(*i).first].target(0) << "\t"
129                    << s_Units[(*i).first].target(1) << "\t"
130                    << s_Units[(*i).first].target(2) << "\t"
131                    << s_Units[(*i).first].initiative() << "\t"
132                    << s_Units[(*i).first].agility() << "\t"
133                    << s_Units[(*i).first].weaponSpeed() << "\t"
134                    << s_Units[(*i).first].guns() << "\t"
135                    << s_Units[(*i).first].power() << "\t"
136                    << s_Units[(*i).first].armor() << "\t"
137                    << s_Units[(*i).first].EMP() << "\t"
138                    << s_Units[(*i).first].totRes() << "\t"
139                    << s_Units[(*i).first].fuel() << "\t"
140                    << s_Units[(*i).first].ETA() << "\t"*/
141                    << s_Units[(*i).first].type() << endl;
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) 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)
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)
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)
224 {
225
226         int ticks = m_Fleet[unittype].size();
227         if (ticks == 0)
228                 return 0;
229
230         if (tick < 0)
231                 return m_Fleet[unittype][0];
232
233         --ticks;
234
235         if (ticks < tick)
236                 return m_Fleet[unittype][ticks];
237
238         return m_Fleet[unittype][tick];
239 }
240
241 //////////////////////////////////////////////////////////////////////////
242 //
243 int Fleet::blockedFleet(std::string unittype, int tick)
244 {
245         int ticks = m_BlockedFleet[unittype].size();
246         if (ticks == 0)
247                 return 0;
248
249         --ticks;
250
251         if (ticks < tick)
252                 return 0;
253
254         return m_BlockedFleet[unittype][tick];
255 }
256
257 //////////////////////////////////////////////////////////////////////////
258 //
259 void Fleet::setBlockedFleet(std::string unittype, int number, int tick)
260 {
261         int ticks = m_BlockedFleet[unittype].size();
262
263         for (int i = ticks; i <= tick; ++i)
264         {
265                 m_BlockedFleet[unittype].push_back(0);
266         }
267         m_BlockedFleet[unittype][tick] = number;
268
269         cerr << "This fleet got " << m_BlockedFleet[unittype][tick] << " blocked units tick: " << tick << endl;
270
271 }
272
273 //////////////////////////////////////////////////////////////////////////
274 //
275 void Fleet::addToThis(std::vector<Fleet*> fleets, int tick)
276 {
277         for (UnitList::iterator i = s_Units.begin();  i != s_Units.end(); ++i)
278         {
279                 if (m_Fleet[i->first].size() == 0)
280                         m_Fleet[i->first].push_back(0);
281
282                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
283                 {
284                         int num = (*j)->fleet(i->first, tick);
285                         m_Fleet[i->first][0] += num;
286                         if (num > 0)
287                                 cerr << (*j)->name() <<  " adding " << num << " units of type " << i->first << endl;
288                 }
289         }
290 }
291
292 //////////////////////////////////////////////////////////////////////////
293 //
294 void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick)
295 {
296         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
297         {
298                 string unittype = i->first;
299
300
301                 if (m_Fleet[unittype].size() < 1)
302                         continue;
303                 if (m_Fleet[unittype][0] == 0)
304                         continue;
305
306
307                 int totallost = fleet(unittype,1) - fleet(unittype, 0);
308
309
310                 cerr << "Distributing type: " << unittype << " with a total loss of " << totallost << " units" << endl;
311
312                 cerr << "Total number of units before: " << fleet(unittype, 0)  << " and after : " << fleet(unittype, 1) << endl;
313
314                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
315                 {
316                         int fl1 = (*j)->fleet(unittype, tick - 1);
317                         float part = float(fl1) / fleet(unittype, 0) ;
318                         int lost =  totallost * part;
319                         (*j)->setFleet(unittype, (*j)->fleet(unittype, tick - 1) + lost, tick);
320
321                         cerr << (*j)->name() << " gaining " << lost << " " << unittype  << " since it's " << part * 100 << "% of the whole fleet, and it had : " << fl1 << " units last tick.." <<  endl;
322
323                         lost = part * blockedFleet(unittype, 0);
324
325                         cerr << (*j)->name() << " got " << lost << " blocked " << unittype  << ", the total number of blocked ships was: " << blockedFleet(unittype, 0) << endl; 
326
327                         (*j)->setBlockedFleet(unittype, lost, tick);
328                 }
329         }
330 }
331
332 //////////////////////////////////////////////////////////////////////////
333 //
334 std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int stays, int tick)
335 {
336         vector<Fleet*> fl;
337         for (vector<Fleet*>::iterator i = fleets.begin(); i != fleets.end(); ++i)
338         {
339                 if ((*i)->stays() == 0)
340                         continue;
341                 else if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < (*i)->stays())
342                 {
343                         fl.push_back((*i));
344                         cerr << "Using fleet " << (*i)->name() << " for tick " << tick << endl;
345                 }
346                 else if ((*i)->stays() < 0)
347                         fl.push_back((*i));
348         }
349         return fl;
350 }
351
352 //////////////////////////////////////////////////////////////////////////
353 //
354 int Fleet::freeFleet(std:: string unittype, int tick)
355 {
356         int bticks = m_BlockedFleet[unittype].size();
357
358         --bticks;
359
360         if (bticks < tick)
361                 return fleet(unittype, tick);
362
363
364         int free = fleet(unittype,tick) - m_BlockedFleet[unittype][tick];
365         if (free < 0)
366                 return 0;
367         return free;
368 }
369
370
371 //////////////////////////////////////////////////////////////////////////
372 //
373 void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
374 {
375
376         float guns = s_Units[unittype].guns() * number;
377
378
379         if (guns == 0)
380                 return;
381
382         cerr << number << " " << unittype << ": with " << guns << " guns\n";
383
384         float gunsleft = guns;
385         for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
386         {
387                 string ta = s_Units[unittype].target(count);
388                 cerr << "Shooting at target class: " << ta << endl;
389                 while (gunsleft > 0)
390                 {
391
392                         map<string, int*> targets;
393
394                         for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
395                         {
396                                 if (m_Fleet[j->first].size() == 0)
397                                         continue;
398
399
400                                 if (m_Fleet[j->first].size() == 1 )
401                                         m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
402
403                                 //cerr << "Target is class: " << j->second.type() << endl;
404
405                                 if (m_Fleet[j->first][1] > 0  && ( ta == j->second.unitClass() || ta == "All"))
406                                 {
407
408                                         //      cerr << "Looking at target: " << j->first << endl;
409                                         targets[j->first] = &m_Fleet[j->first][1];
410                                 }
411                         }
412
413                         if (targets.size() == 0)
414                                 break;
415
416                         int total = 0;
417                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
418                                 total += (*j->second);
419
420                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
421                         {
422                                 float maxguns = float((*j->second))/total * guns;
423                                 //cerr << "Now shooting at target: " << j->first << endl;
424
425                                 if (m_Armor[j->first] <= 0 || m_Armor[j->first] > s_Units[j->first].armor())
426                                         m_Armor[j->first] = s_Units[j->first].armor();
427                                 double k = maxguns;
428
429                                 //cerr << "Targets agility: " << s_Units[j->first].agility() << endl;
430                                 //cerr << "Weaponspeed: " << s_Units[unittype].weaponSpeed() << endl;
431                                 while (k > 0)   
432                                 {
433
434                                         if (*(j->second) <= 0)
435                                                 break;
436
437                                         int wpsp = s_Units[unittype].weaponSpeed();
438                                         int agil = s_Units[j->first].agility();
439
440                                         k -= float(100)/(25 + wpsp - agil);
441                                         //cout << "Used " << blaha << " guns to hit with one shot.\n";
442                                         //cout << "WPSP: " << wpsp << "\nAgil: " << agil << endl;
443
444                                         m_Armor[j->first] -= s_Units[unittype].power();
445                                         if (m_Armor[j->first] <= 0)
446                                         {
447                                                 m_Armor[j->first] = s_Units[j->first].armor();
448                                                 (*j->second)--;
449                                                 hitunits[j->first]++;
450
451                                                 //There is a chance that we're hitting a blocked ship.
452                                                 if (m_BlockedFleet[j->first].size() >= 1)
453                                                 {
454                                                         int test = rand() % m_BlockedFleet[j->first][0];
455                                                         if (test == 1
456                                                                         && m_BlockedFleet[j->first][0] > 0)
457                                                         {
458                                                                 if (m_BlockedFleet[j->first].size() == 1)
459                                                                         m_BlockedFleet[j->first].push_back(m_BlockedFleet[j->first][0] - 1);
460                                                                 else if (m_BlockedFleet[j->first][1] > 0)
461                                                                         m_BlockedFleet[j->first][1]--;
462                                                         }
463                                                 }
464                                         }
465
466                                 }
467
468                                 cerr << hitunits[j->first] << " units of type: " << j->first << "killed\n";
469                                 if (k <= 0)
470                                         gunsleft -= maxguns;
471                                 else
472                                         gunsleft -= maxguns - k;
473                         }
474                 }
475         }
476 }
477
478 //////////////////////////////////////////////////////////////////////////
479 //
480 void Fleet::takeEMP(std::string unittype, int number)
481 {
482         int guns = s_Units[unittype].guns() * number;
483         if (guns == 0)
484                 return;
485
486         cerr << unittype << ": with " << guns << " guns\n";
487
488         float gunsleft = guns;
489         for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
490         {
491                 string ta = s_Units[unittype].target(count);
492                 cerr << "Shooting at target class: " << ta << endl;
493                 while (gunsleft > 0)
494                 {
495
496                         map<string, int*> targets;
497
498                         for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
499                         {
500                                 if (j->second.type() == "PDS")
501                                         continue;
502
503                                 if (freeFleet(j->first, 1) <= 0)
504                                         continue;
505
506                                 if (m_Fleet[j->first].size() == 0)
507                                         continue;
508
509                                 if (m_Fleet[j->first].size() == 1)
510                                         m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
511
512                                 //cerr << "Target is class: " << j->second.type() << endl;
513
514                                 if (m_Fleet[j->first][1] > 0  && ( ta == j->second.unitClass() || ta == "All"))
515                                 {
516
517                                         //      cerr << "Looking at target: " << j->first << endl;
518                                         targets[j->first] = &m_Fleet[j->first][1];
519                                 }
520
521                         }
522
523                         if (targets.size() == 0)
524                                 break;
525
526                         int total = 0;
527                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
528                                 total += (*j->second);
529
530                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
531                         {
532                                 float maxguns = float((*j->second))/total * guns;
533                                 cerr << "Now shooting at target: " << j->first << endl;
534
535                                 double k = maxguns;
536
537                                 int hits = 0;
538
539                                 while (k > 0)   
540                                 {
541
542                                         if (*(j->second) <= blockedFleet(j->first, 1))
543                                                 break;
544
545                                         int eres = s_Units[j->first].EMP();
546
547                                         k -= float(100)/(100-eres);
548                                         hits++;
549                                         blockFleet(j->first, 1);
550                                 }
551
552                                 cerr << hits << " units of type: " << j->first << " blocked\n";
553                                 if (k <= 0)
554                                         gunsleft -= maxguns;
555                                 else
556                                         gunsleft -= maxguns - k;
557                         }
558                 }
559         }
560 }
561
562 //////////////////////////////////////////////////////////////////////////
563 //
564 void Fleet::killFleet(std::string unittype, int number, int tick)
565 {
566         int earlier = 0;
567         int ticks = m_Fleet[unittype].size();
568
569         if (ticks != 0)
570                 earlier = m_Fleet[unittype][ticks - 1];
571
572         for (int i = ticks; i <= tick; ++i)
573         {
574                 m_Fleet[unittype].push_back(earlier);
575         }
576         m_Fleet[unittype][tick] -= number;
577 }
578
579 //////////////////////////////////////////////////////////////////////////
580 //
581 void Fleet::setResource(std::string type, int number, int tick)
582 {
583
584         int ticks = m_Resources[type].size();
585         for (int i = ticks; i <= tick; ++i)
586                 m_Resources[type].push_back(number);
587         m_Resources[type][tick] = number;
588 }
589
590 //////////////////////////////////////////////////////////////////////////
591 //
592 void Fleet::addResource(std::string type, int number, int tick)
593 {
594
595         int ticks = m_Resources[type].size();
596         int latest = resource(type, tick - 1);
597
598         for (int i = ticks; i <= tick; ++i)
599                 m_Resources[type].push_back(latest);
600         m_Resources[type][tick] += number;
601 }
602
603 //////////////////////////////////////////////////////////////////////////
604 //
605 int Fleet::resource(std::string type, int tick) const
606 {
607         if (tick < 0)
608                 return 0;
609
610         vector<int>const* resource = 0;
611         for (ResourceList::const_iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
612         {
613                 if (i->first == type)
614                 {
615                         resource = &i->second;
616                         break;
617                 }
618         }
619         if (resource == 0)
620                 return 0;
621
622         int ticks = resource->size();
623
624         if( ticks == 0)
625                 return 0;
626
627         --ticks;
628
629         if (ticks < tick)
630                 return resource->at(ticks);
631         return resource->at(tick);
632 }
633
634 //////////////////////////////////////////////////////////////////////////
635 //
636 void Fleet::resetResources()
637 {
638         m_Resources.clear() ;   
639 }
640
641 //////////////////////////////////////////////////////////////////////////
642 //
643 void Fleet::printFleet()
644 {
645         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
646         {
647                 for (int tick = 0; tick < 5 ;++tick)
648                 {
649                         int num = fleet(i->first, tick);
650
651                         if (num <= 0)
652                                 break;
653                         cerr << num << " " << i->first << " during tick: " << tick << endl;
654                 }
655         }
656 }
657
658 //////////////////////////////////////////////////////////////////////////
659 //
660 void Fleet::blockFleet(std::string unittype, int number, int tick)
661 {
662         if (m_BlockedFleet[unittype].size() >= 1)
663         {
664                 m_BlockedFleet[unittype][0] += number;
665                 if (m_BlockedFleet[unittype].size() > 1)
666                         m_BlockedFleet[unittype][1] += number;
667                 else
668                         m_BlockedFleet[unittype].push_back(m_BlockedFleet[unittype][0]);
669         }
670         else
671         {
672                 m_BlockedFleet[unittype].push_back(number);
673                 m_BlockedFleet[unittype].push_back(number);
674         }
675 }
676
677 //////////////////////////////////////////////////////////////////////////
678 //
679 void Fleet::distributeCappedRoids(std::vector<Fleet*> fleets, int tick)
680 {
681         for (ResourceList::iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
682         {
683                 string res = i->first;
684
685
686                 cerr << "Distributing type: " << res << endl;
687                 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
688                         cout << (*j) << endl;
689
690                 if (m_Resources[res].size() < 2)
691                         continue;
692                 if (m_Resources[res][1] == 0)
693                         continue;
694
695
696                 int totcapped = resource(res,1) - resource(res, 0);
697
698
699                 cerr << "Distributing type: " << res << " with a total gain of " << totcapped << " roids" << endl;
700
701                 cerr << "Total number of roids before: " << resource(res, 0)  << " and after : " << resource(res, 1) << endl;
702
703                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
704                 {
705                         unsigned fl1 = (*j)->score(tick - 1);
706                         float part = float(fl1) / score(0) ;
707                         int lost =  totcapped * part;
708
709                         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;
710
711                         //(*j)->setResource(res, (*j)->resource(res,tick-1) + lost, tick);
712                         (*j)->addResource(res,lost, tick);
713                 }
714         }
715 }
716
717 //////////////////////////////////////////////////////////////////////////
718 //
719 void Fleet::addFleet(std::map<string, int> units, int tick)
720 {
721         for (map<string, int>::iterator i = units.begin(); i != units.end(); ++i)
722                 addFleet(i->first, i->second, tick);
723 }
724
725 //////////////////////////////////////////////////////////////////////////
726 //
727 int Fleet::stays() const
728 {
729         return m_iStays;
730 }
731
732 //////////////////////////////////////////////////////////////////////////
733 //
734 void Fleet::setStays(int ticks)
735 {
736         m_iStays = ticks;
737 }
738
739 //////////////////////////////////////////////////////////////////////////
740 //
741 void Fleet::calculateLostStealships(string unittype, std::map<std::string, int> stolen, int tick)
742 {
743         int stealscore = 0;
744         for (map<string, int>::iterator i = stolen.begin(); i != stolen.end(); ++i)
745         {
746                 stealscore += stolen[i->first] * (s_Units[i->first].totRes() / 10.0);   
747         }
748
749         int lost = stealscore / (s_Units[unittype].totRes() / 10.0);
750
751         cerr << "Lost " << lost << " " << unittype << " due to stealing ships worth: " << stealscore << endl; 
752         killFleet(unittype, lost, tick);
753 }
754
755 //////////////////////////////////////////////////////////////////////////
756 //
757 void Fleet::distributeStolenShips(std::map<std::string, std::map<std::string, int> > stolen, std::vector<Fleet*> fleets, int tick)
758 {
759         for(map<string, map<string, int> >::iterator i = stolen.begin(); i != stolen.end(); ++i)
760         {
761                 int totalstealers = 0;
762                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
763                         totalstealers += (*j)->fleet(i->first, tick - 1);
764
765                 for (map<string, int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
766                 {
767                         for (vector<Fleet*>::iterator k = fleets.begin(); k != fleets.end(); ++k)
768                         {
769                                 int stolen = float ((*k)->fleet(i->first, tick - 1)) / totalstealers * j->second;
770                                 (*k)->addFleet(j->first, stolen, tick);
771                         }
772                 }
773         }
774 }
775
776 //////////////////////////////////////////////////////////////////////////
777 //
778 void Fleet::calculateSalvage()
779 {
780         for (FleetList::iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
781         {
782
783                 map<string, int> res = s_Units[i->first].resources();
784
785                 if (i->second.size() > 0)
786                         cerr << endl << i->first << ": ";
787
788                 int tick = 0;
789                 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j, ++tick)
790                 {
791                         int lostunits = fleet(i->first, tick - 1) - fleet(i->first, tick);
792
793                         if (lostunits <= 0)
794                                 continue;
795                         cerr << "(" << tick << ":" << fleet(i->first, tick) << ") ";
796                         for (map<string, int>::iterator k = res.begin(); k != res.end(); ++k)
797                                 addResource(k->first, lostunits * k->second * 0.25, tick);
798                 }
799         }
800 }
801
802 //////////////////////////////////////////////////////////////////////////
803 //
804 void Fleet::resetTicks()
805 {
806         for (FleetList::iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
807         {
808                 if ( i->second.size() < 2)
809                         continue;
810
811                 int temp = i->second[0];
812                 i->second.clear();
813
814                 if (temp > 0)
815                         i->second.push_back(temp);
816         }
817         resetResources();
818 }
819
820 //////////////////////////////////////////////////////////////////////////
821 //
822 int Fleet::freePodGuns(int tick) 
823 {
824         int guns = 0;
825         for (FleetList::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
826         {
827                 if (s_Units[i->first].type() == "Pod")
828                 {
829                         guns += freeFleet(i->first, tick) * s_Units[i->first].guns();
830                 }
831         }
832         guns -= usedPodGuns(tick);
833         return guns;
834 }
835
836 //////////////////////////////////////////////////////////////////////////
837 //
838 int Fleet::usedPodGuns(int tick) const
839 {
840         int ticks = m_UsedPodGuns.size();
841
842         if (ticks == 0)
843                 return 0;
844
845         if (tick < 0)
846                 return m_UsedPodGuns[0];
847
848         --ticks;
849
850         if (ticks < tick)
851                 return m_UsedPodGuns[ticks];
852
853         return m_UsedPodGuns[tick];
854
855 }
856
857 //////////////////////////////////////////////////////////////////////////
858 //
859 void Fleet::usePodGuns(int tick, int guns)
860 {
861         int ticks = m_UsedPodGuns.size();
862
863         for (int i = ticks; i <= tick; ++i)
864         {
865                 m_UsedPodGuns.push_back(0);
866         }
867         m_UsedPodGuns[tick] += guns;
868 }