1 /***************************************************************************
2 fleet.cpp - description
4 begin : Tue Jan 22 2002
5 copyright : (C) 2002 by Michael Andreen
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
25 map<string, vector<int> > Fleet::s_Races;
26 UnitList Fleet::s_Units;
38 //////////////////////////////////////////////////////////////////////////
40 void Fleet::setName(string sName)
45 //////////////////////////////////////////////////////////////////////////
47 string Fleet::name() const
52 //////////////////////////////////////////////////////////////////////////
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.
58 bool Fleet::setRace(string sRace)
61 for (map<string, vector<int> >::iterator i = s_Races.begin(); i != s_Races.end(); i++)
63 if (m_sRace == (*i).first)
69 //////////////////////////////////////////////////////////////////////////
71 string Fleet::race() const
76 //////////////////////////////////////////////////////////////////////////
78 /** This function iterates through m_Fleet and adds all numbers together to
81 int Fleet::numberOfShips() const
85 for (map<string, vector<int> >::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
87 if (i->second.size() != 0)
88 total += i->second[0];
94 //////////////////////////////////////////////////////////////////////////
96 void Fleet::setETA(int eta)
101 //////////////////////////////////////////////////////////////////////////
103 int Fleet::ETA() const
108 //////////////////////////////////////////////////////////////////////////
110 void Fleet::setRaces(map<string, vector<int> >& races)
115 //////////////////////////////////////////////////////////////////////////
117 void Fleet::setUnits(UnitList& units)
123 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); i++)
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;
145 //////////////////////////////////////////////////////////////////////////
147 const map<string, vector<int> >& Fleet::Races()
152 //////////////////////////////////////////////////////////////////////////
154 const UnitList& Fleet::Units()
159 //////////////////////////////////////////////////////////////////////////
161 vector<int> Fleet::RacesAllowed() const
163 return s_Races[m_sRace];
166 //////////////////////////////////////////////////////////////////////////
168 unsigned Fleet::score(int tick) const
170 unsigned tot_score = 0;
172 for (FleetList::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
174 int ticks = i->second.size();
179 tot_score += i->second[ticks] * s_Units[i->first].totRes() / 10;
181 tot_score += i->second[tick] * s_Units[i->first].totRes() / 10;
187 //////////////////////////////////////////////////////////////////////////
189 void Fleet::setFleet(string unittype, int number, int tick)
192 int ticks = m_Fleet[unittype].size();
195 earlier = m_Fleet[unittype][ticks - 1];
197 for (int i = ticks; i <= tick; ++i)
199 m_Fleet[unittype].push_back(earlier);
201 m_Fleet[unittype][tick] = number;
204 //////////////////////////////////////////////////////////////////////////
206 void Fleet::addFleet(std::string unittype, int number, int tick)
209 int ticks = m_Fleet[unittype].size();
212 earlier = m_Fleet[unittype][ticks - 1];
214 for (int i = ticks; i <= tick; ++i)
216 m_Fleet[unittype].push_back(earlier);
218 m_Fleet[unittype][tick] += number;
221 //////////////////////////////////////////////////////////////////////////
223 int Fleet::fleet(string unittype, int tick)
226 int ticks = m_Fleet[unittype].size();
231 return m_Fleet[unittype][0];
236 return m_Fleet[unittype][ticks];
238 return m_Fleet[unittype][tick];
241 //////////////////////////////////////////////////////////////////////////
243 int Fleet::blockedFleet(std::string unittype, int tick)
245 int ticks = m_BlockedFleet[unittype].size();
254 return m_BlockedFleet[unittype][tick];
257 //////////////////////////////////////////////////////////////////////////
259 void Fleet::setBlockedFleet(std::string unittype, int number, int tick)
261 int ticks = m_BlockedFleet[unittype].size();
263 for (int i = ticks; i <= tick; ++i)
265 m_BlockedFleet[unittype].push_back(0);
267 m_BlockedFleet[unittype][tick] = number;
269 cerr << "This fleet got " << m_BlockedFleet[unittype][tick] << " blocked units tick: " << tick << endl;
273 //////////////////////////////////////////////////////////////////////////
275 void Fleet::addToThis(std::vector<Fleet*> fleets, int tick)
277 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
279 if (m_Fleet[i->first].size() == 0)
280 m_Fleet[i->first].push_back(0);
282 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
284 int num = (*j)->fleet(i->first, tick);
285 m_Fleet[i->first][0] += num;
287 cerr << (*j)->name() << " adding " << num << " units of type " << i->first << endl;
292 //////////////////////////////////////////////////////////////////////////
294 void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick)
296 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
298 string unittype = i->first;
301 if (m_Fleet[unittype].size() < 1)
303 if (m_Fleet[unittype][0] == 0)
307 int totallost = fleet(unittype,1) - fleet(unittype, 0);
310 cerr << "Distributing type: " << unittype << " with a total loss of " << totallost << " units" << endl;
312 cerr << "Total number of units before: " << fleet(unittype, 0) << " and after : " << fleet(unittype, 1) << endl;
314 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
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);
321 cerr << (*j)->name() << " gaining " << lost << " " << unittype << " since it's " << part * 100 << "% of the whole fleet, and it had : " << fl1 << " units last tick.." << endl;
323 lost = part * blockedFleet(unittype, 0);
325 cerr << (*j)->name() << " got " << lost << " blocked " << unittype << ", the total number of blocked ships was: " << blockedFleet(unittype, 0) << endl;
327 (*j)->setBlockedFleet(unittype, lost, tick);
332 //////////////////////////////////////////////////////////////////////////
334 std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int stays, int tick)
337 for (vector<Fleet*>::iterator i = fleets.begin(); i != fleets.end(); ++i)
339 if ((*i)->stays() == 0)
341 else if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < (*i)->stays())
344 cerr << "Using fleet " << (*i)->name() << " for tick " << tick << endl;
346 else if ((*i)->stays() < 0)
352 //////////////////////////////////////////////////////////////////////////
354 int Fleet::freeFleet(std:: string unittype, int tick)
356 int bticks = m_BlockedFleet[unittype].size();
361 return fleet(unittype, tick);
364 int free = fleet(unittype,tick) - m_BlockedFleet[unittype][tick];
371 //////////////////////////////////////////////////////////////////////////
373 void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
376 float guns = s_Units[unittype].guns() * number;
382 cerr << number << " " << unittype << ": with " << guns << " guns\n";
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)
387 string ta = s_Units[unittype].target(count);
388 cerr << "Shooting at target class: " << ta << endl;
392 map<string, int*> targets;
394 for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
396 if (m_Fleet[j->first].size() == 0)
400 if (m_Fleet[j->first].size() == 1 )
401 m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
403 //cerr << "Target is class: " << j->second.type() << endl;
405 if (m_Fleet[j->first][1] > 0 && ( ta == j->second.unitClass() || ta == "All"))
408 // cerr << "Looking at target: " << j->first << endl;
409 targets[j->first] = &m_Fleet[j->first][1];
413 if (targets.size() == 0)
417 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
418 total += (*j->second);
420 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
422 float maxguns = float((*j->second))/total * guns;
423 //cerr << "Now shooting at target: " << j->first << endl;
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();
429 //cerr << "Targets agility: " << s_Units[j->first].agility() << endl;
430 //cerr << "Weaponspeed: " << s_Units[unittype].weaponSpeed() << endl;
434 if (*(j->second) <= 0)
437 int wpsp = s_Units[unittype].weaponSpeed();
438 int agil = s_Units[j->first].agility();
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;
444 m_Armor[j->first] -= s_Units[unittype].power();
445 if (m_Armor[j->first] <= 0)
447 m_Armor[j->first] = s_Units[j->first].armor();
449 hitunits[j->first]++;
451 //There is a chance that we're hitting a blocked ship.
453 if (m_BlockedFleet[j->first].size() >= 1)
455 int test = rand() % m_BlockedFleet[j->first][0];
457 && m_BlockedFleet[j->first][0] > 0)
459 if (m_BlockedFleet[j->first].size() == 1)
460 m_BlockedFleet[j->first].push_back(m_BlockedFleet[j->first][0] - 1);
461 else if (m_BlockedFleet[j->first][1] > 0)
462 m_BlockedFleet[j->first][1]--;
470 cerr << hitunits[j->first] << " units of type: " << j->first << "killed\n";
474 gunsleft -= maxguns - k;
480 //////////////////////////////////////////////////////////////////////////
482 void Fleet::takeEMP(std::string unittype, int number, std::map<std::string, int>& hitunits)
484 int guns = s_Units[unittype].guns() * number;
488 cerr << unittype << ": with " << guns << " guns\n";
490 float gunsleft = guns;
491 for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
493 string ta = s_Units[unittype].target(count);
494 cerr << "Shooting at target class: " << ta << endl;
498 map<string, int*> targets;
500 for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
502 if (j->second.type() == "PDS")
505 if (freeFleet(j->first, 1) <= 0)
508 if (m_Fleet[j->first].size() == 0)
511 if (m_Fleet[j->first].size() == 1)
512 m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
514 //cerr << "Target is class: " << j->second.type() << endl;
516 if (m_Fleet[j->first][1] > 0 && ( ta == j->second.unitClass() || ta == "All"))
519 // cerr << "Looking at target: " << j->first << endl;
520 targets[j->first] = &m_Fleet[j->first][1];
525 if (targets.size() == 0)
529 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
530 total += (*j->second);
532 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
534 float maxguns = float((*j->second))/total * guns;
535 cerr << "Now shooting at target: " << j->first << endl;
544 if (*(j->second) <= blockedFleet(j->first, 1))
547 int eres = s_Units[j->first].EMP();
549 k -= float(100)/(100-eres);
551 blockFleet(j->first, 1);
554 cerr << hits << " units of type: " << j->first << " blocked\n";
558 gunsleft -= maxguns - k;
564 //////////////////////////////////////////////////////////////////////////
566 void Fleet::killFleet(std::string unittype, int number, int tick)
569 int ticks = m_Fleet[unittype].size();
572 earlier = m_Fleet[unittype][ticks - 1];
574 for (int i = ticks; i <= tick; ++i)
576 m_Fleet[unittype].push_back(earlier);
578 m_Fleet[unittype][tick] -= number;
581 //////////////////////////////////////////////////////////////////////////
583 void Fleet::setResource(std::string type, int number, int tick)
586 int ticks = m_Resources[type].size();
587 for (int i = ticks; i <= tick; ++i)
588 m_Resources[type].push_back(number);
589 m_Resources[type][tick] = number;
592 //////////////////////////////////////////////////////////////////////////
594 void Fleet::addResource(std::string type, int number, int tick)
597 int ticks = m_Resources[type].size();
598 int latest = resource(type, tick - 1);
600 for (int i = ticks; i <= tick; ++i)
601 m_Resources[type].push_back(latest);
602 m_Resources[type][tick] += number;
605 //////////////////////////////////////////////////////////////////////////
607 int Fleet::resource(std::string type, int tick) const
612 vector<int>const* resource = 0;
613 for (ResourceList::const_iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
615 if (i->first == type)
617 resource = &i->second;
624 int ticks = resource->size();
632 return resource->at(ticks);
633 return resource->at(tick);
636 //////////////////////////////////////////////////////////////////////////
638 void Fleet::resetResources()
640 m_Resources.clear() ;
643 //////////////////////////////////////////////////////////////////////////
645 void Fleet::printFleet()
647 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
649 for (int tick = 0; tick < 5 ;++tick)
651 int num = fleet(i->first, tick);
655 cerr << num << " " << i->first << " during tick: " << tick << endl;
660 //////////////////////////////////////////////////////////////////////////
662 void Fleet::blockFleet(std::string unittype, int number, int tick)
664 if (m_BlockedFleet[unittype].size() >= 1)
666 m_BlockedFleet[unittype][0] += number;
667 if (m_BlockedFleet[unittype].size() > 1)
668 m_BlockedFleet[unittype][1] += number;
670 m_BlockedFleet[unittype].push_back(m_BlockedFleet[unittype][0]);
674 m_BlockedFleet[unittype].push_back(number);
675 m_BlockedFleet[unittype].push_back(number);
679 //////////////////////////////////////////////////////////////////////////
681 void Fleet::distributeCappedRoids(std::vector<Fleet*> fleets, int tick)
683 for (ResourceList::iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
685 string res = i->first;
688 cerr << "Distributing type: " << res << endl;
689 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
690 cout << (*j) << endl;
692 if (m_Resources[res].size() < 2)
694 if (m_Resources[res][1] == 0)
698 int totcapped = resource(res,1) - resource(res, 0);
701 cerr << "Distributing type: " << res << " with a total gain of " << totcapped << " roids" << endl;
703 cerr << "Total number of roids before: " << resource(res, 0) << " and after : " << resource(res, 1) << endl;
705 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
707 unsigned fl1 = (*j)->score(tick - 1);
708 float part = float(fl1) / score(0) ;
709 int lost = totcapped * part;
711 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 //(*j)->setResource(res, (*j)->resource(res,tick-1) + lost, tick);
714 (*j)->addResource(res,lost, tick);
719 //////////////////////////////////////////////////////////////////////////
721 void Fleet::addFleet(std::map<string, int> units, int tick)
723 for (map<string, int>::iterator i = units.begin(); i != units.end(); ++i)
724 addFleet(i->first, i->second, tick);
727 //////////////////////////////////////////////////////////////////////////
729 int Fleet::stays() const
734 //////////////////////////////////////////////////////////////////////////
736 void Fleet::setStays(int ticks)
741 //////////////////////////////////////////////////////////////////////////
743 void Fleet::calculateLostStealships(string unittype, std::map<std::string, int> stolen, int tick)
746 for (map<string, int>::iterator i = stolen.begin(); i != stolen.end(); ++i)
748 stealscore += stolen[i->first] * (s_Units[i->first].totRes() / 10.0);
751 int lost = stealscore / (s_Units[unittype].totRes() / 10.0);
753 cerr << "Lost " << lost << " " << unittype << " due to stealing ships worth: " << stealscore << endl;
754 killFleet(unittype, lost, tick);
757 //////////////////////////////////////////////////////////////////////////
759 void Fleet::distributeStolenShips(std::map<std::string, std::map<std::string, int> > stolen, std::vector<Fleet*> fleets, int tick)
761 for(map<string, map<string, int> >::iterator i = stolen.begin(); i != stolen.end(); ++i)
763 int totalstealers = 0;
764 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
765 totalstealers += (*j)->fleet(i->first, tick - 1);
767 for (map<string, int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
769 for (vector<Fleet*>::iterator k = fleets.begin(); k != fleets.end(); ++k)
771 int stolen = float ((*k)->fleet(i->first, tick - 1)) / totalstealers * j->second;
772 (*k)->addFleet(j->first, stolen, tick);
778 //////////////////////////////////////////////////////////////////////////
780 void Fleet::calculateSalvage()
782 for (FleetList::iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
785 map<string, int> res = s_Units[i->first].resources();
787 if (i->second.size() > 0)
788 cerr << endl << i->first << ": ";
791 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j, ++tick)
793 int lostunits = fleet(i->first, tick - 1) - fleet(i->first, tick);
797 cerr << "(" << tick << ":" << fleet(i->first, tick) << ") ";
798 for (map<string, int>::iterator k = res.begin(); k != res.end(); ++k)
799 addResource(k->first, lostunits * k->second * 0.25, tick);
804 //////////////////////////////////////////////////////////////////////////
806 void Fleet::resetTicks()
808 for (FleetList::iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
810 if ( i->second.size() < 2)
813 int temp = i->second[0];
817 i->second.push_back(temp);
822 //////////////////////////////////////////////////////////////////////////
824 int Fleet::freePodGuns(int tick)
827 for (FleetList::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
829 if (s_Units[i->first].type() == "Pod")
831 guns += freeFleet(i->first, tick) * s_Units[i->first].guns();
834 guns -= usedPodGuns(tick);
838 //////////////////////////////////////////////////////////////////////////
840 int Fleet::usedPodGuns(int tick) const
842 int ticks = m_UsedPodGuns.size();
848 return m_UsedPodGuns[0];
853 return m_UsedPodGuns[ticks];
855 return m_UsedPodGuns[tick];
859 //////////////////////////////////////////////////////////////////////////
861 void Fleet::usePodGuns(int tick, int guns)
863 int ticks = m_UsedPodGuns.size();
865 for (int i = ticks; i <= tick; ++i)
867 m_UsedPodGuns.push_back(0);
869 m_UsedPodGuns[tick] += guns;