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;
146 //////////////////////////////////////////////////////////////////////////
148 const map<string, vector<int> >& Fleet::Races()
153 //////////////////////////////////////////////////////////////////////////
155 const UnitList& Fleet::Units()
160 //////////////////////////////////////////////////////////////////////////
162 vector<int> Fleet::RacesAllowed() const
164 return s_Races[m_sRace];
167 //////////////////////////////////////////////////////////////////////////
169 unsigned Fleet::score(int tick) const
171 unsigned tot_score = 0;
173 for (FleetList::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
175 int ticks = i->second.size();
180 tot_score += i->second[ticks] * s_Units[i->first].totRes() / 10;
182 tot_score += i->second[tick] * s_Units[i->first].totRes() / 10;
188 //////////////////////////////////////////////////////////////////////////
190 void Fleet::setFleet(string unittype, int number, int tick)
193 int ticks = m_Fleet[unittype].size();
196 earlier = m_Fleet[unittype][ticks - 1];
198 for (int i = ticks; i <= tick; ++i)
200 m_Fleet[unittype].push_back(earlier);
202 m_Fleet[unittype][tick] = number;
205 //////////////////////////////////////////////////////////////////////////
207 void Fleet::addFleet(std::string unittype, int number, int tick)
210 int ticks = m_Fleet[unittype].size();
213 earlier = m_Fleet[unittype][ticks - 1];
215 for (int i = ticks; i <= tick; ++i)
217 m_Fleet[unittype].push_back(earlier);
219 m_Fleet[unittype][tick] += number;
222 //////////////////////////////////////////////////////////////////////////
224 int Fleet::fleet(string unittype, int tick)
227 int ticks = m_Fleet[unittype].size();
232 return m_Fleet[unittype][0];
237 return m_Fleet[unittype][ticks];
239 return m_Fleet[unittype][tick];
242 //////////////////////////////////////////////////////////////////////////
244 int Fleet::blockedFleet(std::string unittype, int tick)
246 int ticks = m_BlockedFleet[unittype].size();
255 return m_BlockedFleet[unittype][tick];
258 //////////////////////////////////////////////////////////////////////////
260 void Fleet::setBlockedFleet(std::string unittype, int number, int tick)
262 int ticks = m_BlockedFleet[unittype].size();
264 for (int i = ticks; i <= tick; ++i)
266 m_BlockedFleet[unittype].push_back(0);
268 m_BlockedFleet[unittype][tick] = number;
270 cerr << "This fleet got " << m_BlockedFleet[unittype][tick] << " blocked units tick: " << tick << endl;
274 //////////////////////////////////////////////////////////////////////////
276 void Fleet::addToThis(std::vector<Fleet*> fleets, int tick)
278 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
280 if (m_Fleet[i->first].size() == 0)
281 m_Fleet[i->first].push_back(0);
283 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
285 int num = (*j)->fleet(i->first, tick);
286 m_Fleet[i->first][0] += num;
288 cerr << (*j)->name() << " adding " << num << " units of type " << i->first << endl;
293 //////////////////////////////////////////////////////////////////////////
295 void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick)
297 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
299 string unittype = i->first;
302 if (m_Fleet[unittype].size() < 1)
304 if (m_Fleet[unittype][0] == 0)
308 int totallost = fleet(unittype,1) - fleet(unittype, 0);
311 cerr << "Distributing type: " << unittype << " with a total loss of " << totallost << " units" << endl;
313 cerr << "Total number of units before: " << fleet(unittype, 0) << " and after : " << fleet(unittype, 1) << endl;
315 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
317 int fl1 = (*j)->fleet(unittype, tick - 1);
318 float part = float(fl1) / fleet(unittype, 0) ;
319 int lost = int(totallost * part);
320 (*j)->setFleet(unittype, (*j)->fleet(unittype, tick - 1) + lost, tick);
322 cerr << (*j)->name() << " gaining " << lost << " " << unittype << " since it's " << part * 100 << "% of the whole fleet, and it had : " << fl1 << " units last tick.." << endl;
324 lost = int(part * blockedFleet(unittype, 0));
326 cerr << (*j)->name() << " got " << lost << " blocked " << unittype << ", the total number of blocked ships was: " << blockedFleet(unittype, 0) << endl;
328 (*j)->setBlockedFleet(unittype, lost, tick);
333 //////////////////////////////////////////////////////////////////////////
335 std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int tick)
338 for (vector<Fleet*>::iterator i = fleets.begin(); i != fleets.end(); ++i)
340 if ((*i)->stays() == 0)
342 else if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < (*i)->stays())
345 cerr << "Using fleet " << (*i)->name() << " for tick " << tick << endl;
347 else if ((*i)->stays() < 0)
353 //////////////////////////////////////////////////////////////////////////
355 int Fleet::freeFleet(std:: string unittype, int tick)
357 int bticks = m_BlockedFleet[unittype].size();
362 return fleet(unittype, tick);
365 int free = fleet(unittype,tick) - m_BlockedFleet[unittype][tick];
372 //////////////////////////////////////////////////////////////////////////
374 void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
377 float guns = s_Units[unittype].guns() * number;
383 float gunsleft = guns;
384 for (int count = 0; count < 3; ++count)
386 string ta = s_Units[unittype].target(count);
391 map<string, int*> targets = findTargets(ta,total, 0);
393 if (targets.size() == 0)
396 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
398 float maxguns = float((*j->second))/total * guns;
400 if (m_Armor[j->first] <= 0 || m_Armor[j->first] > s_Units[j->first].armor())
401 m_Armor[j->first] = s_Units[j->first].armor();
407 if (*(j->second) <= 0)
410 int wpsp = s_Units[unittype].weaponSpeed();
411 int agil = s_Units[j->first].agility();
413 k -= float(100)/(25 + wpsp - agil);
415 m_Armor[j->first] -= s_Units[unittype].power();
416 if (m_Armor[j->first] <= 0)
418 m_Armor[j->first] = s_Units[j->first].armor();
420 hitunits[j->first]++;
427 gunsleft -= maxguns - k;
433 //////////////////////////////////////////////////////////////////////////
435 void Fleet::takeEMP(std::string unittype, int number, std::map<std::string, int>& hitunits)
437 int guns = s_Units[unittype].guns() * number;
441 float gunsleft = guns;
442 for (int count = 0; count < 3; ++count)
444 string ta = s_Units[unittype].target(count);
449 map<string, int*> targets = findTargets(ta, total, 1);
451 if (targets.size() == 0)
454 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
456 float maxguns = float((*j->second))/total * gunsleft;
463 if (*(j->second) <= blockedFleet(j->first, 1))
466 int eres = s_Units[j->first].EMP();
468 k -= float(100)/(100-eres);
470 hitunits[j->first]++;
471 blockFleet(j->first, 1);
476 gunsleft -= maxguns - k;
482 //////////////////////////////////////////////////////////////////////////
484 void Fleet::killFleet(std::string unittype, int number, int tick)
487 int ticks = m_Fleet[unittype].size();
490 earlier = m_Fleet[unittype][ticks - 1];
492 for (int i = ticks; i <= tick; ++i)
494 m_Fleet[unittype].push_back(earlier);
496 m_Fleet[unittype][tick] -= number;
499 //////////////////////////////////////////////////////////////////////////
501 void Fleet::setResource(std::string type, int number, int tick)
504 int ticks = m_Resources[type].size();
505 for (int i = ticks; i <= tick; ++i)
506 m_Resources[type].push_back(number);
507 m_Resources[type][tick] = number;
510 //////////////////////////////////////////////////////////////////////////
512 void Fleet::addResource(std::string type, int number, int tick)
515 int ticks = m_Resources[type].size();
516 int latest = resource(type, tick - 1);
518 for (int i = ticks; i <= tick; ++i)
519 m_Resources[type].push_back(latest);
520 m_Resources[type][tick] += number;
523 //////////////////////////////////////////////////////////////////////////
525 int Fleet::resource(std::string type, int tick) const
530 vector<int>const* resource = 0;
531 for (ResourceList::const_iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
533 if (i->first == type)
535 resource = &i->second;
542 int ticks = resource->size();
550 return resource->at(ticks);
551 return resource->at(tick);
554 //////////////////////////////////////////////////////////////////////////
556 void Fleet::resetResources()
558 m_Resources.clear() ;
561 //////////////////////////////////////////////////////////////////////////
563 void Fleet::printFleet()
565 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
567 for (int tick = 0; tick < 5 ;++tick)
569 int num = fleet(i->first, tick);
573 cerr << num << " " << i->first << " during tick: " << tick << endl;
578 //////////////////////////////////////////////////////////////////////////
580 void Fleet::blockFleet(std::string unittype, int number, int tick)
582 if (m_BlockedFleet[unittype].size() >= 1)
584 m_BlockedFleet[unittype][0] += number;
585 if (m_BlockedFleet[unittype].size() > 1)
586 m_BlockedFleet[unittype][1] += number;
588 m_BlockedFleet[unittype].push_back(m_BlockedFleet[unittype][0]);
592 m_BlockedFleet[unittype].push_back(number);
593 m_BlockedFleet[unittype].push_back(number);
597 //////////////////////////////////////////////////////////////////////////
599 void Fleet::distributeCappedRoids(std::vector<Fleet*> fleets, int tick)
601 for (ResourceList::iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
603 string res = i->first;
606 cerr << "Distributing type: " << res << endl;
607 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
608 cout << (*j) << endl;
610 if (m_Resources[res].size() < 2)
612 if (m_Resources[res][1] == 0)
616 int totcapped = resource(res,1) - resource(res, 0);
619 cerr << "Distributing type: " << res << " with a total gain of " << totcapped << " roids" << endl;
621 cerr << "Total number of roids before: " << resource(res, 0) << " and after : " << resource(res, 1) << endl;
623 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
625 unsigned fl1 = (*j)->score(tick - 1);
626 float part = float(fl1) / score(0) ;
627 int lost = int(totcapped * part);
629 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;
631 //(*j)->setResource(res, (*j)->resource(res,tick-1) + lost, tick);
632 (*j)->addResource(res,lost, tick);
637 //////////////////////////////////////////////////////////////////////////
639 void Fleet::addFleet(std::map<string, int> units, int tick)
641 for (map<string, int>::iterator i = units.begin(); i != units.end(); ++i)
642 addFleet(i->first, i->second, tick);
645 //////////////////////////////////////////////////////////////////////////
647 int Fleet::stays() const
652 //////////////////////////////////////////////////////////////////////////
654 void Fleet::setStays(int ticks)
659 //////////////////////////////////////////////////////////////////////////
661 void Fleet::calculateLostStealships(string unittype, std::map<std::string, int> stolen, int tick)
664 for (map<string, int>::iterator i = stolen.begin(); i != stolen.end(); ++i)
666 stealscore += int(stolen[i->first] * (s_Units[i->first].totRes() / 10.0));
669 int lost = int(stealscore / (s_Units[unittype].totRes() / 10.0));
671 cerr << "Lost " << lost << " " << unittype << " due to stealing ships worth: " << stealscore << endl;
672 killFleet(unittype, lost, tick);
675 //////////////////////////////////////////////////////////////////////////
677 void Fleet::distributeStolenShips(std::map<std::string, std::map<std::string, int> > stolen, std::vector<Fleet*> fleets, int tick)
679 for(map<string, map<string, int> >::iterator i = stolen.begin(); i != stolen.end(); ++i)
681 int totalstealers = 0;
682 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
683 totalstealers += (*j)->fleet(i->first, tick - 1);
685 for (map<string, int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
687 for (vector<Fleet*>::iterator k = fleets.begin(); k != fleets.end(); ++k)
689 int stolen = int(float ((*k)->fleet(i->first, tick - 1)) / totalstealers * j->second);
690 (*k)->addFleet(j->first, stolen, tick);
696 //////////////////////////////////////////////////////////////////////////
698 void Fleet::calculateSalvage()
700 for (FleetList::iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
703 map<string, int> res = s_Units[i->first].resources();
705 if (i->second.size() > 0)
706 cerr << endl << i->first << ": ";
709 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j, ++tick)
711 int lostunits = fleet(i->first, tick - 1) - fleet(i->first, tick);
715 cerr << "(" << tick << ":" << fleet(i->first, tick) << ") ";
716 for (map<string, int>::iterator k = res.begin(); k != res.end(); ++k)
717 addResource(k->first, int(lostunits * k->second * 0.25), tick);
722 //////////////////////////////////////////////////////////////////////////
724 void Fleet::resetTicks()
726 for (FleetList::iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
728 if ( i->second.size() < 2)
731 int temp = i->second[0];
735 i->second.push_back(temp);
740 //////////////////////////////////////////////////////////////////////////
742 int Fleet::freePodGuns(int tick)
745 for (FleetList::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
747 if (s_Units[i->first].type() == "Pod")
749 guns += freeFleet(i->first, tick) * s_Units[i->first].guns();
752 guns -= usedPodGuns(tick);
756 //////////////////////////////////////////////////////////////////////////
758 int Fleet::usedPodGuns(int tick) const
760 int ticks = m_UsedPodGuns.size();
766 return m_UsedPodGuns[0];
771 return m_UsedPodGuns[ticks];
773 return m_UsedPodGuns[tick];
777 //////////////////////////////////////////////////////////////////////////
779 void Fleet::usePodGuns(int tick, int guns)
781 int ticks = m_UsedPodGuns.size();
783 for (int i = ticks; i <= tick; ++i)
785 m_UsedPodGuns.push_back(0);
787 m_UsedPodGuns[tick] += guns;
790 //////////////////////////////////////////////////////////////////////////
792 std::map<std::string, int*> Fleet::findTargets(std::string targetClass,int& total, int attackerType)
794 map<string, int*> targets;
796 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
798 if (m_Fleet[i->first].size() == 0)
801 if (m_Fleet[i->first].size() == 1 )
802 m_Fleet[i->first].push_back(m_Fleet[i->first][0]);
804 if (m_Fleet[i->first][1] > 0 && ( targetClass == i->second.unitClass() || targetClass == "All"))
806 if (i->second.unitClass() == "PDS" && attackerType == 1)
808 targets[i->first] = &m_Fleet[i->first][1];
809 total += m_Fleet[i->first][1];