]> ruin.nu Git - hbs.git/blob - bs/fleet.cpp
Fixed a bug in the shipstealing code..
[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 //
148 const map<string, vector<int> >& Fleet::Races()
149 {
150         return s_Races;
151 }
152
153 //////////////////////////////////////////////////////////////////////////
154 //
155 const UnitList& Fleet::Units()
156 {
157         return s_Units;
158 }
159
160 //////////////////////////////////////////////////////////////////////////
161 //
162 vector<int> Fleet::RacesAllowed() const
163 {
164         return s_Races[m_sRace];
165 }
166
167 //////////////////////////////////////////////////////////////////////////
168 //
169 unsigned Fleet::score(int tick = 0) const
170 {
171         unsigned tot_score = 0;
172
173         for (FleetList::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
174         {
175                 int ticks =     i->second.size();
176                 if (ticks == 0)
177                         continue;
178                 --ticks;
179                 if ( ticks < tick)
180                         tot_score += i->second[ticks] * s_Units[i->first].totRes() / 10;
181                 else
182                         tot_score += i->second[tick] * s_Units[i->first].totRes() / 10;
183         }
184
185         return tot_score;
186 }
187
188 //////////////////////////////////////////////////////////////////////////
189 //
190 void Fleet::setFleet(string unittype, int number, int tick = 0)
191 {
192         int earlier = 0;
193         int ticks = m_Fleet[unittype].size();
194
195         if (ticks != 0)
196                 earlier = m_Fleet[unittype][ticks - 1];
197
198         for (int i = ticks; i <= tick; ++i)
199         {
200                 m_Fleet[unittype].push_back(earlier);
201         }
202         m_Fleet[unittype][tick] = number;
203 }
204
205 //////////////////////////////////////////////////////////////////////////
206 //
207 void Fleet::addFleet(std::string unittype, int number, int tick = 0)
208 {
209         int earlier = 0;
210         int ticks = m_Fleet[unittype].size();
211
212         if (ticks != 0)
213                 earlier = m_Fleet[unittype][ticks - 1];
214
215         for (int i = ticks; i <= tick; ++i)
216         {
217                 m_Fleet[unittype].push_back(earlier);
218         }
219         m_Fleet[unittype][tick] += number;
220 }
221
222 //////////////////////////////////////////////////////////////////////////
223 //
224 int      Fleet::fleet(string unittype, int tick = 0)
225 {
226
227         int ticks = m_Fleet[unittype].size();
228         if (ticks == 0)
229                 return 0;
230
231         if (tick < 0)
232                 return m_Fleet[unittype][0];
233
234         --ticks;
235
236         if (ticks < tick)
237                 return m_Fleet[unittype][ticks];
238
239         return m_Fleet[unittype][tick];
240 }
241
242 //////////////////////////////////////////////////////////////////////////
243 //
244 int Fleet::blockedFleet(std::string unittype, int tick = 0)
245 {
246         int ticks = m_BlockedFleet[unittype].size();
247         if (ticks == 0)
248                 return 0;
249
250         --ticks;
251
252         if (ticks < tick)
253                 return 0;
254
255         return m_BlockedFleet[unittype][tick];
256 }
257
258 //////////////////////////////////////////////////////////////////////////
259 //
260 void Fleet::setBlockedFleet(std::string unittype, int number, int tick = 0)
261 {
262         int ticks = m_BlockedFleet[unittype].size();
263
264         for (int i = ticks; i <= tick; ++i)
265         {
266                 m_BlockedFleet[unittype].push_back(0);
267         }
268         m_BlockedFleet[unittype][tick] = number;
269
270         cerr << "This fleet got " << m_BlockedFleet[unittype][tick] << " blocked units tick: " << tick << endl;
271
272 }
273
274 //////////////////////////////////////////////////////////////////////////
275 //FIXME
276 void Fleet::addToThis(std::vector<Fleet*> fleets, int tick = 0)
277 {
278         for (UnitList::iterator i = s_Units.begin();  i != s_Units.end(); ++i)
279         {
280                 if (m_Fleet[i->first].size() == 0)
281                                 m_Fleet[i->first].push_back(0);
282
283                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
284                 {
285                         int num = (*j)->fleet(i->first, tick);
286                         m_Fleet[i->first][0] += num;
287                         if (num > 0)
288                                 cerr << (*j)->name() <<  " adding " << num << " units of type " << i->first << endl;
289                 }
290         }
291 }
292
293 //////////////////////////////////////////////////////////////////////////
294 //
295 void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0)
296 {
297         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
298         {
299                 string unittype = i->first;
300
301
302                 if (m_Fleet[unittype].size() < 1)
303                         continue;
304                 if (m_Fleet[unittype][0] == 0)
305                         continue;
306
307                  
308                 int totallost = fleet(unittype,1) - fleet(unittype, 0);
309
310
311                 cerr << "Distributing type: " << unittype << " with a total loss of " << totallost << " units" << endl;
312
313                 cerr << "Total number of units before: " << fleet(unittype, 0)  << " and after : " << fleet(unittype, 1) << endl;
314                 
315                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
316                 {
317                         int fl1 = (*j)->fleet(unittype, tick - 1);
318                         float part = float(fl1) / fleet(unittype, 0) ;
319                         int lost =  totallost * part;
320                         (*j)->setFleet(unittype, (*j)->fleet(unittype, tick - 1) + lost, tick);
321
322                         cerr << (*j)->name() << " gaining " << lost << " " << unittype  << " since it's " << part * 100 << "% of the whole fleet, and it had : " << fl1 << " units last tick.." <<  endl;
323
324                         lost = part * blockedFleet(unittype, 0);
325
326                         cerr << (*j)->name() << " got " << lost << " blocked " << unittype  << ", the total number of blocked ships was: " << blockedFleet(unittype, 0) << endl; 
327
328                         (*j)->setBlockedFleet(unittype, lost, tick);
329                 }
330         }
331 }
332
333 //////////////////////////////////////////////////////////////////////////
334 //
335 std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int stays = 0, int tick = 0)
336 {
337         vector<Fleet*> fl;
338         for (vector<Fleet*>::iterator i = fleets.begin(); i != fleets.end(); ++i)
339         {
340                 if ((*i)->stays() == 0)
341                         continue;
342                 else if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < (*i)->stays())
343                 {
344                         fl.push_back((*i));
345                         cerr << "Using fleet " << (*i)->name() << " for tick " << tick << endl;
346                 }
347                 else if ((*i)->stays() < 0)
348                         fl.push_back((*i));
349         }
350         return fl;
351 }
352
353 //////////////////////////////////////////////////////////////////////////
354 //
355 int Fleet::freeFleet(std:: string unittype, int tick = 0)
356 {
357         int bticks = m_BlockedFleet[unittype].size();
358
359         --bticks;
360
361         if (bticks < tick)
362                 return fleet(unittype, tick);
363
364
365         int free = fleet(unittype,tick) - m_BlockedFleet[unittype][tick];
366         if (free < 0)
367                 return 0;
368         return free;
369 }
370
371
372 //////////////////////////////////////////////////////////////////////////
373 //
374 void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
375 {
376
377         float guns = s_Units[unittype].guns() * number;
378
379         
380         if (guns == 0)
381                 return;
382
383         cerr << number << " " << unittype << ": with " << guns << " guns\n";
384
385         float gunsleft = guns;
386         for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
387         {
388                 string ta = s_Units[unittype].target(count);
389         cerr << "Shooting at target class: " << ta << endl;
390                 while (gunsleft > 0)
391                 {
392         
393                         map<string, int*> targets;
394
395                         for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
396                         {
397
398                                 if (m_Fleet[j->first].size() == 0)
399                                         continue;
400
401
402                                 if (m_Fleet[j->first].size() == 1 )
403                                         m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
404
405                                 //cerr << "Target is class: " << j->second.type() << endl;
406
407                                 if (m_Fleet[j->first][1] > 0  && ( ta == j->second.unitClass() || ta == "All"))
408                                 {
409
410                                 //      cerr << "Looking at target: " << j->first << endl;
411                                         targets[j->first] = &m_Fleet[j->first][1];
412                                 }
413
414                         }
415
416                         if (targets.size() == 0)
417                                 break;
418
419                         int total = 0;
420                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
421                                 total += (*j->second);
422
423                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
424                         {
425                                 float maxguns = float((*j->second))/total * guns;
426                                 //cerr << "Now shooting at target: " << j->first << endl;
427
428                                 if (m_Armor[j->first] <= 0 || m_Armor[j->first] > s_Units[j->first].armor())
429                                         m_Armor[j->first] = s_Units[j->first].armor();
430                                 double k = maxguns;
431
432                                 //cerr << "Targets agility: " << s_Units[j->first].agility() << endl;
433                                 //cerr << "Weaponspeed: " << s_Units[unittype].weaponSpeed() << endl;
434                                 while (k > 0)   
435                                 {
436
437                                         if (*(j->second) <= 0)
438                                                 break;
439
440                                         int wpsp = s_Units[unittype].weaponSpeed();
441                                         int agil = s_Units[j->first].agility();
442
443                                         k -= float(100)/(25 + wpsp - agil);
444                                         //cout << "Used " << blaha << " guns to hit with one shot.\n";
445                                         //cout << "WPSP: " << wpsp << "\nAgil: " << agil << endl;
446
447                                         m_Armor[j->first] -= s_Units[unittype].power();
448                                         if (m_Armor[j->first] <= 0)
449                                         {
450                                                 m_Armor[j->first] = s_Units[j->first].armor();
451                                                 (*j->second)--;
452                                                 hitunits[j->first]++;
453                                                 
454                                                 //There is a chance that we're hitting a blocked ship.
455                                                 if (m_BlockedFleet[j->first].size() >= 1)
456                                                 {
457                                                         int test = rand() % m_BlockedFleet[j->first][0];
458                                                         if (test == 1
459                                                                 && m_BlockedFleet[j->first][0] > 0)
460                                                         {
461                                                                 if (m_BlockedFleet[j->first].size() == 1)
462                                                                         m_BlockedFleet[j->first].push_back(m_BlockedFleet[j->first][0] - 1);
463                                                                 else if (m_BlockedFleet[j->first][1] > 0)
464                                                                         m_BlockedFleet[j->first][1]--;
465                                                         }
466                                                 }
467                                         }
468
469                                 }
470
471                                 cerr << hitunits[j->first] << " units of type: " << j->first << "killed\n";
472                                 if (k <= 0)
473                                         gunsleft -= maxguns;
474                                 else
475                                         gunsleft -= maxguns - k;
476                         }
477                 }
478         }
479 }
480
481 //////////////////////////////////////////////////////////////////////////
482 //
483 void Fleet::takeEMP(std::string unittype, int number)
484 {
485         int guns = s_Units[unittype].guns() * number;
486         if (guns == 0)
487                 return;
488
489         cerr << unittype << ": with " << guns << " guns\n";
490
491         float gunsleft = guns;
492         for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
493         {
494                 string ta = s_Units[unittype].target(count);
495         cerr << "Shooting at target class: " << ta << endl;
496                 while (gunsleft > 0)
497                 {
498         
499                         map<string, int*> targets;
500
501                         for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
502                         {
503                                 if (j->second.type() == "PDS")
504                                         continue;
505
506                                 if (freeFleet(j->first, 1) <= 0)
507                                         continue;
508
509                                 if (m_Fleet[j->first].size() == 0)
510                                         continue;
511
512                                 if (m_Fleet[j->first].size() == 1)
513                                         m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
514
515                                 //cerr << "Target is class: " << j->second.type() << endl;
516
517                                 if (m_Fleet[j->first][1] > 0  && ( ta == j->second.unitClass() || ta == "All"))
518                                 {
519
520                                 //      cerr << "Looking at target: " << j->first << endl;
521                                         targets[j->first] = &m_Fleet[j->first][1];
522                                 }
523
524                         }
525
526                         if (targets.size() == 0)
527                                 break;
528
529                         int total = 0;
530                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
531                                 total += (*j->second);
532
533                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
534                         {
535                                 float maxguns = float((*j->second))/total * guns;
536                                 cerr << "Now shooting at target: " << j->first << endl;
537
538                                 double k = maxguns;
539
540                                 int hits = 0;
541
542                                 while (k > 0)   
543                                 {
544
545                                         if (*(j->second) <= blockedFleet(j->first, 1))
546                                                 break;
547
548                                         int eres = s_Units[j->first].EMP();
549
550                                         k -= float(100)/(100-eres);
551                                         hits++;
552                                         blockFleet(j->first, 1);
553                                 }
554
555                                 cerr << hits << " units of type: " << j->first << " blocked\n";
556                                 if (k <= 0)
557                                         gunsleft -= maxguns;
558                                 else
559                                         gunsleft -= maxguns - k;
560                         }
561                 }
562         }
563 }
564
565 //////////////////////////////////////////////////////////////////////////
566 //
567 void Fleet::killFleet(std::string unittype, int number, int tick = 0)
568 {
569         int earlier = 0;
570         int ticks = m_Fleet[unittype].size();
571
572         if (ticks != 0)
573                 earlier = m_Fleet[unittype][ticks - 1];
574
575         for (int i = ticks; i <= tick; ++i)
576         {
577                 m_Fleet[unittype].push_back(earlier);
578         }
579         m_Fleet[unittype][tick] -= number;
580 }
581
582 //////////////////////////////////////////////////////////////////////////
583 //
584 void Fleet::setResource(std::string type, int number, int tick = 0)
585 {
586
587         int ticks = m_Resources[type].size();
588         for (int i = ticks; i <= tick; ++i)
589                 m_Resources[type].push_back(number);
590         m_Resources[type][tick] = number;
591 }
592
593 //////////////////////////////////////////////////////////////////////////
594 //
595 void Fleet::addResource(std::string type, int number, int tick = 0)
596 {
597
598         int ticks = m_Resources[type].size();
599         int latest = resource(type, tick - 1);
600
601         for (int i = ticks; i <= tick; ++i)
602                 m_Resources[type].push_back(latest);
603         m_Resources[type][tick] += number;
604 }
605
606 //////////////////////////////////////////////////////////////////////////
607 //
608 int Fleet::resource(std::string type, int tick = 0) const
609 {
610         if (tick < 0)
611                 return 0;
612
613         vector<int>const* resource = 0;
614         for (ResourceList::const_iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
615         {
616                 if (i->first == type)
617                 {
618                         resource = &i->second;
619                         break;
620                 }
621         }
622         if (resource == 0)
623                 return 0;
624
625         int ticks = resource->size();
626
627         if( ticks == 0)
628                 return 0;
629
630         --ticks;
631
632         if (ticks < tick)
633                 return resource->at(ticks);
634         return resource->at(tick);
635 }
636
637 //////////////////////////////////////////////////////////////////////////
638 //
639 void Fleet::resetResources()
640 {
641         m_Resources.clear() ;   
642 }
643
644 //////////////////////////////////////////////////////////////////////////
645 //
646 void Fleet::printFleet()
647 {
648         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
649         {
650                 for (int tick = 0; tick < 5 ;++tick)
651                 {
652                         int num = fleet(i->first, tick);
653
654                         if (num <= 0)
655                                 break;
656                         cerr << num << " " << i->first << " during tick: " << tick << endl;
657                 }
658         }
659 }
660
661 //////////////////////////////////////////////////////////////////////////
662 //
663 void Fleet::blockFleet(std::string unittype, int number, int tick = 0)
664 {
665         if (m_BlockedFleet[unittype].size() >= 1)
666         {
667                 m_BlockedFleet[unittype][0] += number;
668                 if (m_BlockedFleet[unittype].size() > 1)
669                         m_BlockedFleet[unittype][1] += number;
670                 else
671                         m_BlockedFleet[unittype].push_back(m_BlockedFleet[unittype][0]);
672         }
673         else
674         {
675                 m_BlockedFleet[unittype].push_back(number);
676                 m_BlockedFleet[unittype].push_back(number);
677         }
678 }
679
680 //////////////////////////////////////////////////////////////////////////
681 //
682 void Fleet::distributeCappedRoids(std::vector<Fleet*> fleets, int tick = 0)
683 {
684         for (ResourceList::iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
685         {
686                 string res = i->first;
687
688
689                 cerr << "Distributing type: " << res << endl;
690                 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
691                         cout << (*j) << endl;
692
693                 if (m_Resources[res].size() < 2)
694                         continue;
695                 if (m_Resources[res][1] == 0)
696                         continue;
697
698                  
699                 int totcapped = resource(res,1) - resource(res, 0);
700
701
702                 cerr << "Distributing type: " << res << " with a total gain of " << totcapped << " roids" << endl;
703
704                 cerr << "Total number of roids before: " << resource(res, 0)  << " and after : " << resource(res, 1) << endl;
705                 
706                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
707                 {
708                         unsigned fl1 = (*j)->score(tick - 1);
709                         float part = float(fl1) / score(0) ;
710                         int lost =  totcapped * part;
711
712                         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;
713
714                         //(*j)->setResource(res, (*j)->resource(res,tick-1) + lost, tick);
715                         (*j)->addResource(res,lost, tick);
716                 }
717         }
718 }
719
720 //////////////////////////////////////////////////////////////////////////
721 //
722 void Fleet::addFleet(std::map<string, int> units, int tick = 0)
723 {
724         for (map<string, int>::iterator i = units.begin(); i != units.end(); ++i)
725                 addFleet(i->first, i->second, tick);
726 }
727
728 //////////////////////////////////////////////////////////////////////////
729 //
730 int Fleet::stays() const
731 {
732         return m_iStays;
733 }
734
735 //////////////////////////////////////////////////////////////////////////
736 //
737 void Fleet::setStays(int ticks)
738 {
739         m_iStays = ticks;
740 }
741
742 //////////////////////////////////////////////////////////////////////////
743 //
744 void Fleet::calculateLostStealships(string unittype, std::map<std::string, int> stolen, int tick = 1)
745 {
746         int stealscore = 0;
747         for (map<string, int>::iterator i = stolen.begin(); i != stolen.end(); ++i)
748         {
749                 stealscore += stolen[i->first] * (s_Units[i->first].totRes() / 10.0);   
750         }
751
752         int lost = stealscore / (s_Units[unittype].totRes() / 10.0);
753
754         cerr << "Lost " << lost << " " << unittype << " due to stealing ships worth: " << stealscore << endl; 
755         killFleet(unittype, lost, tick);
756 }
757
758 //////////////////////////////////////////////////////////////////////////
759 //
760 void Fleet::distributeStolenShips(std::map<std::string, std::map<std::string, int> > stolen, std::vector<Fleet*> fleets, int tick = 0)
761 {
762         for(map<string, map<string, int> >::iterator i = stolen.begin(); i != stolen.end(); ++i)
763         {
764                 int totalstealers = 0;
765                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
766                         totalstealers += (*j)->fleet(i->first, tick - 1);
767
768                 for (map<string, int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
769                 {
770                         for (vector<Fleet*>::iterator k = fleets.begin(); k != fleets.end(); ++k)
771                         {
772                                 int stolen = float ((*k)->fleet(i->first, tick - 1)) / totalstealers * j->second;
773                                 (*k)->addFleet(j->first, stolen, tick);
774                         }
775                 }
776         }
777 }
778
779 //////////////////////////////////////////////////////////////////////////
780 //
781 void Fleet::calculateSalvage()
782 {
783         for (FleetList::iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
784         {
785                 
786                 map<string, int> res = s_Units[i->first].resources();
787
788                 if (i->second.size() > 0)
789                         cerr << endl << i->first << ": ";
790
791                 int tick = 0;
792                 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j, ++tick)
793                 {
794                         int lostunits = fleet(i->first, tick - 1) - fleet(i->first, tick);
795
796                         if (lostunits <= 0)
797                                 continue;
798                         cerr << "(" << tick << ":" << fleet(i->first, tick) << ") ";
799                         for (map<string, int>::iterator k = res.begin(); k != res.end(); ++k)
800                                 addResource(k->first, lostunits * k->second * 0.25, tick);
801                 }
802         }
803 }
804
805 //////////////////////////////////////////////////////////////////////////
806 //
807 void Fleet::resetTicks()
808 {
809         for (FleetList::iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
810         {
811                 if ( i->second.size() < 2)
812                         continue;
813
814                 int temp = i->second[0];
815                 i->second.clear();
816
817                 if (temp > 0)
818                         i->second.push_back(temp);
819         }
820         resetResources();
821 }