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;
37 //////////////////////////////////////////////////////////////////////////
39 void Fleet::setName(string sName)
44 //////////////////////////////////////////////////////////////////////////
46 string Fleet::name() const
51 //////////////////////////////////////////////////////////////////////////
53 /** This function first sets the race, then it iterates through the the
54 * s_Races and checks if it finds the race it returns true, if it reaches
55 * the end without finding it it returns false.
57 bool Fleet::setRace(string sRace)
60 for (map<string, vector<int> >::iterator i = s_Races.begin(); i != s_Races.end(); i++)
62 if (m_sRace == (*i).first)
68 //////////////////////////////////////////////////////////////////////////
70 string Fleet::race() const
75 //////////////////////////////////////////////////////////////////////////
77 /** This function iterates through m_Fleet and adds all numbers together to
80 int Fleet::numberOfShips() const
84 for (map<string, vector<int> >::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
86 if (i->second.size() != 0)
87 total += i->second[0];
93 //////////////////////////////////////////////////////////////////////////
95 void Fleet::setETA(int eta)
100 //////////////////////////////////////////////////////////////////////////
102 int Fleet::ETA() const
107 //////////////////////////////////////////////////////////////////////////
109 void Fleet::setRaces(map<string, vector<int> >& races)
114 //////////////////////////////////////////////////////////////////////////
116 void Fleet::setUnits(UnitList& units)
122 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); i++)
124 cerr << s_Units[(*i).first].Name() << "\t\t"
125 << s_Units[(*i).first].race() <<"\t"
126 << s_Units[(*i).first].unitClass() << "\t"
127 << s_Units[(*i).first].target(0) << "\t"
128 << s_Units[(*i).first].target(1) << "\t"
129 << s_Units[(*i).first].target(2) << "\t"
130 << s_Units[(*i).first].initiative() << "\t"
131 << s_Units[(*i).first].agility() << "\t"
132 << s_Units[(*i).first].weaponSpeed() << "\t"
133 << s_Units[(*i).first].guns() << "\t"
134 << s_Units[(*i).first].power() << "\t"
135 << s_Units[(*i).first].armor() << "\t"
136 << s_Units[(*i).first].EMP() << "\t"
137 << s_Units[(*i).first].totRes() << "\t"
138 << s_Units[(*i).first].fuel() << "\t"
139 << s_Units[(*i).first].ETA() << "\t"
140 << s_Units[(*i).first].type() << endl;
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 = 0) 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 = 0)
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 int Fleet::fleet(string unittype, int tick = 0)
208 int ticks = m_Fleet[unittype].size();
215 return m_Fleet[unittype][ticks];
217 return m_Fleet[unittype][tick];
220 //////////////////////////////////////////////////////////////////////////
222 int Fleet::blockedFleet(std::string unittype, int tick = 0)
224 int ticks = m_BlockedFleet[unittype].size();
233 return m_BlockedFleet[unittype][tick];
236 //////////////////////////////////////////////////////////////////////////
238 void Fleet::setBlockedFleet(std::string unittype, int number, int tick = 0)
240 int ticks = m_BlockedFleet[unittype].size();
242 for (int i = ticks; i <= tick; ++i)
244 m_BlockedFleet[unittype].push_back(0);
246 m_BlockedFleet[unittype][tick] = number;
248 cerr << "This fleet got " << m_BlockedFleet[unittype][tick] << " blocked units tick: " << tick << endl;
252 //////////////////////////////////////////////////////////////////////////
254 void Fleet::addToThis(std::vector<Fleet*> fleets, int tick = 0)
256 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
258 if (m_Fleet[i->first].size() == 0)
259 m_Fleet[i->first].push_back(0);
261 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
263 int num = (*j)->fleet(i->first, tick);
264 m_Fleet[i->first][0] += num;
266 cerr << (*j)->name() << " adding " << num << " units of type " << i->first << endl;
271 //////////////////////////////////////////////////////////////////////////
273 void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0)
275 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
277 string unittype = i->first;
280 if (m_Fleet[unittype].size() < 1)
282 if (m_Fleet[unittype][0] == 0)
286 int totallost = fleet(unittype,1) - fleet(unittype, 0);
289 cerr << "Distributing type: " << unittype << " with a total loss of " << totallost << " units" << endl;
291 cerr << "Total number of units before: " << fleet(unittype, 0) << " and after : " << fleet(unittype, 1) << endl;
293 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
295 int fl1 = (*j)->fleet(unittype, tick - 1);
296 float part = float(fl1) / fleet(unittype, 0) ;
297 int lost = totallost * part;
298 (*j)->setFleet(unittype, (*j)->fleet(unittype, tick - 1) + lost, tick);
300 cerr << (*j)->name() << " gaining " << lost << " " << unittype << " since it's " << part * 100 << "% of the whole fleet, and it had : " << fl1 << " units last tick.." << endl;
302 lost = part * blockedFleet(unittype, 0);
304 cerr << (*j)->name() << " got " << lost << " blocked " << unittype << ", the total number of blocked ships was: " << blockedFleet(unittype, 0) << endl;
306 (*j)->setBlockedFleet(unittype, lost, tick);
311 //////////////////////////////////////////////////////////////////////////
313 std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int stays = 0, int tick = 0)
316 for (vector<Fleet*>::iterator i = fleets.begin(); i != fleets.end(); ++i)
318 if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < stays)
321 cerr << "Using fleet " << (*i)->name() << " for tick " << tick << endl;
323 else if ((*i)->name() == "Home Planet")
329 //////////////////////////////////////////////////////////////////////////
331 int Fleet::freeFleet(std:: string unittype, int tick = 0)
333 int bticks = m_BlockedFleet[unittype].size();
338 return fleet(unittype, tick);
341 int free = fleet(unittype,tick) - m_BlockedFleet[unittype][tick];
348 //////////////////////////////////////////////////////////////////////////
350 void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
353 float guns = s_Units[unittype].guns() * number;
359 cerr << number << " " << unittype << ": with " << guns << " guns\n";
361 float gunsleft = guns;
362 for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
364 string ta = s_Units[unittype].target(count);
365 cerr << "Shooting at target class: " << ta << endl;
369 map<string, int*> targets;
371 for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
374 if (m_Fleet[j->first].size() == 0)
377 if (m_Fleet[j->first].size() == 1)
378 m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
380 //cerr << "Target is class: " << j->second.type() << endl;
382 if (m_Fleet[j->first][1] > 0 && ( ta == j->second.unitClass() || ta == "All"))
385 // cerr << "Looking at target: " << j->first << endl;
386 targets[j->first] = &m_Fleet[j->first][1];
391 if (targets.size() == 0)
395 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
396 total += (*j->second);
398 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
400 float maxguns = float((*j->second))/total * guns;
401 //cerr << "Now shooting at target: " << j->first << endl;
403 if (m_Armor[j->first] <= 0 || m_Armor[j->first] > s_Units[j->first].armor())
404 m_Armor[j->first] = s_Units[j->first].armor();
407 //cerr << "Targets agility: " << s_Units[j->first].agility() << endl;
408 //cerr << "Weaponspeed: " << s_Units[unittype].weaponSpeed() << endl;
412 if (*(j->second) <= 0)
415 int wpsp = s_Units[unittype].weaponSpeed();
416 int agil = s_Units[j->first].agility();
418 k -= float(100)/(25 + wpsp - agil);
419 //cout << "Used " << blaha << " guns to hit with one shot.\n";
420 //cout << "WPSP: " << wpsp << "\nAgil: " << agil << endl;
422 m_Armor[j->first] -= s_Units[unittype].power();
423 if (m_Armor[j->first] <= 0)
425 m_Armor[j->first] = s_Units[j->first].armor();
427 hitunits[j->first]++;
429 //There is a chance that we're hitting a blocked ship.
430 if (m_BlockedFleet[j->first].size() >= 1)
432 int test = rand() % m_BlockedFleet[j->first][0];
434 && m_BlockedFleet[j->first][0] > 0)
436 if (m_BlockedFleet[j->first].size() == 1)
437 m_BlockedFleet[j->first].push_back(m_BlockedFleet[j->first][0] - 1);
438 else if (m_BlockedFleet[j->first][1] > 0)
439 m_BlockedFleet[j->first][1]--;
446 cerr << hitunits[j->first] << " units of type: " << j->first << "killed\n";
450 gunsleft -= maxguns - k;
456 //////////////////////////////////////////////////////////////////////////
458 void Fleet::takeEMP(std::string unittype, int number)
460 int guns = s_Units[unittype].guns() * number;
464 cerr << unittype << ": with " << guns << " guns\n";
466 float gunsleft = guns;
467 for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
469 string ta = s_Units[unittype].target(count);
470 cerr << "Shooting at target class: " << ta << endl;
474 map<string, int*> targets;
476 for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
478 if (j->second.type() == "PDS")
481 if (m_Fleet[j->first].size() == 0)
484 if (m_Fleet[j->first].size() == 1)
485 m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
487 //cerr << "Target is class: " << j->second.type() << endl;
489 if (m_Fleet[j->first][1] > 0 && ( ta == j->second.unitClass() || ta == "All"))
492 // cerr << "Looking at target: " << j->first << endl;
493 targets[j->first] = &m_Fleet[j->first][1];
498 if (targets.size() == 0)
502 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
503 total += (*j->second);
505 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
507 float maxguns = float((*j->second))/total * guns;
508 cerr << "Now shooting at target: " << j->first << endl;
517 if (*(j->second) <= 0)
520 int eres = s_Units[j->first].EMP();
522 k -= float(100)/(100-eres);
524 blockFleet(j->first, 1);
527 cerr << hits << " units of type: " << j->first << " blocked\n";
531 gunsleft -= maxguns - k;
537 //////////////////////////////////////////////////////////////////////////
539 void Fleet::killFleet(std::string unittype, int number, int tick = 0)
542 int ticks = m_Fleet[unittype].size();
545 earlier = m_Fleet[unittype][ticks - 1];
547 for (int i = ticks; i <= tick; ++i)
549 m_Fleet[unittype].push_back(earlier);
551 m_Fleet[unittype][tick] -= number;
554 //////////////////////////////////////////////////////////////////////////
556 void Fleet::setResource(std::string type, int number, int tick = 0)
559 int ticks = m_Resources[type].size();
560 for (int i = ticks; i <= tick; ++i)
561 m_Resources[type].push_back(number);
562 m_Resources[type][tick] = number;
565 //////////////////////////////////////////////////////////////////////////
567 void Fleet::addResource(std::string type, int number, int tick = 0)
570 int ticks = m_Resources[type].size();
574 latest = m_Resources[type][ticks - 1];
576 for (int i = ticks; i <= tick; ++i)
577 m_Resources[type].push_back(latest);
578 m_Resources[type][tick] += number;
581 //////////////////////////////////////////////////////////////////////////
583 int Fleet::resource(std::string type, int tick = 0) const
585 vector<int>const* resource = 0;
586 for (ResourceList::const_iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
588 if (i->first == type)
590 resource = &i->second;
597 int ticks = resource->size();
605 return resource->at(ticks);
606 return resource->at(tick);
609 //////////////////////////////////////////////////////////////////////////
611 void Fleet::printFleet()
613 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
615 for (int tick = 0; tick < 5 ;++tick)
617 int num = fleet(i->first, tick);
621 cerr << num << " " << i->first << " during tick: " << tick << endl;
626 //////////////////////////////////////////////////////////////////////////
628 void Fleet::blockFleet(std::string unittype, int number, int tick = 0)
630 if (m_BlockedFleet[unittype].size() >= 1)
632 m_BlockedFleet[unittype][0] += number;
633 if (m_BlockedFleet[unittype].size() > 1)
634 m_BlockedFleet[unittype][1] += number;
636 m_BlockedFleet[unittype].push_back(m_BlockedFleet[unittype][0]);
640 m_BlockedFleet[unittype].push_back(number);
641 m_BlockedFleet[unittype].push_back(number);
645 //////////////////////////////////////////////////////////////////////////
647 void Fleet::distributeCappedRoids(std::vector<Fleet*> fleets, int tick = 0)
649 for (ResourceList::iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
651 string res = i->first;
653 cerr << "Distributing type: " << res << endl;
654 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
655 cout << (*j) << endl;
657 if (m_Resources[res].size() < 2)
659 if (m_Resources[res][1] == 0)
663 int totcapped = resource(res,1) - resource(res, 0);
666 cerr << "Distributing type: " << res << " with a total gain of " << totcapped << " roids" << endl;
668 cerr << "Total number of roids before: " << resource(res, 0) << " and after : " << resource(res, 1) << endl;
670 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
672 unsigned fl1 = (*j)->score(tick - 1);
673 float part = float(fl1) / score(0) ;
674 int lost = totcapped * part;
676 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;
677 (*j)->setResource(res, (*j)->resource(res,tick-1) + lost, tick);