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;
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 = 0) const
171 unsigned tot_score = 0;
173 for (FleetList::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
175 if (i->second.size() >= tick)
177 tot_score += i->second[tick] * s_Units[i->first].totRes() / 10;
183 //////////////////////////////////////////////////////////////////////////
185 void Fleet::setFleet(string unittype, int number, int tick = 0)
188 int ticks = m_Fleet[unittype].size();
191 earlier = m_Fleet[unittype][ticks - 1];
193 for (int i = ticks; i <= tick; ++i)
195 m_Fleet[unittype].push_back(earlier);
197 m_Fleet[unittype][tick] = number;
200 //////////////////////////////////////////////////////////////////////////
202 int Fleet::fleet(string unittype, int tick = 0)
204 int ticks = m_Fleet[unittype].size();
211 return m_Fleet[unittype][ticks];
213 return m_Fleet[unittype][tick];
216 //////////////////////////////////////////////////////////////////////////
218 void Fleet::addToThis(std::vector<Fleet*> fleets, int tick = 0)
220 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
222 if (m_Fleet[i->first].size() == 0)
223 m_Fleet[i->first].push_back(0);
225 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
227 int num = (*j)->fleet(i->first, tick);
228 m_Fleet[i->first][0] += num;
230 cerr << (*j)->name() << " adding " << num << " units of type " << i->first << endl;
235 //////////////////////////////////////////////////////////////////////////
237 void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0)
239 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
241 string unittype = i->first;
244 if (m_Fleet[unittype].size() < 1)
246 if (m_Fleet[unittype][0] == 0)
250 int totallost = fleet(unittype,1) - fleet(unittype, 0);
253 cerr << "Distributing type: " << unittype << " with a total loss of " << totallost << " units" << endl;
255 cerr << "Total number of units before: " << fleet(unittype, 0) << " and after : " << fleet(unittype, 1) << endl;
257 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
259 int fl1 = (*j)->fleet(unittype, tick - 1);
260 float part = float(fl1) / fleet(unittype, 0) ;
261 int lost = totallost * part;
262 cerr << (*j)->name() << " gaining " << lost << " " << unittype << " since it's " << part * 100 << "% of the whole fleet, and it had : " << fl1 << " units last tick.." << endl;
263 (*j)->setFleet(unittype, (*j)->fleet(unittype, tick - 1) + lost, tick);
268 //////////////////////////////////////////////////////////////////////////
270 std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int stays = 0, int tick = 0)
273 for (vector<Fleet*>::iterator i = fleets.begin(); i != fleets.end(); ++i)
275 if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < stays)
278 cerr << "Using fleet " << (*i)->name() << " for tick " << tick << endl;
280 else if ((*i)->name() == "Home Planet")
286 //////////////////////////////////////////////////////////////////////////
288 int Fleet::freeFleet(std:: string unittype, int tick = 0)
290 int bticks = m_BlockedFleet[unittype].size();
295 return fleet(unittype, tick);
298 int free = fleet(unittype,tick) - m_BlockedFleet[unittype][tick];
305 //////////////////////////////////////////////////////////////////////////
307 void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
310 float guns = s_Units[unittype].guns() * number;
312 cerr << number << " " << unittype << ": with " << guns << " guns\n";
317 cerr << unittype << ": with " << guns << " guns\n";
319 float gunsleft = guns;
320 for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
322 string ta = s_Units[unittype].target(count);
323 cerr << "Shooting at target class: " << ta << endl;
327 map<string, int*> targets;
329 for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
332 if (m_Fleet[j->first].size() == 0)
335 if (m_Fleet[j->first].size() == 1)
336 m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
338 //cerr << "Target is class: " << j->second.type() << endl;
340 if (m_Fleet[j->first][1] > 0 && ( ta == j->second.unitClass() || ta == "All"))
343 // cerr << "Looking at target: " << j->first << endl;
344 targets[j->first] = &m_Fleet[j->first][1];
349 if (targets.size() == 0)
353 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
354 total += (*j->second);
356 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
358 float maxguns = float((*j->second))/total * guns;
359 //cerr << "Now shooting at target: " << j->first << endl;
361 if (m_Armor[j->first] <= 0 || m_Armor[j->first] > s_Units[j->first].armor())
362 m_Armor[j->first] = s_Units[j->first].armor();
365 //cerr << "Targets agility: " << s_Units[j->first].agility() << endl;
366 //cerr << "Weaponspeed: " << s_Units[unittype].weaponSpeed() << endl;
370 if (*(j->second) <= 0)
373 int wpsp = s_Units[unittype].weaponSpeed();
374 int agil = s_Units[j->first].agility();
376 k -= float(100)/(25 + wpsp - agil);
377 //cout << "Used " << blaha << " guns to hit with one shot.\n";
378 //cout << "WPSP: " << wpsp << "\nAgil: " << agil << endl;
380 m_Armor[j->first] -= s_Units[unittype].power();
381 if (m_Armor[j->first] <= 0)
383 m_Armor[j->first] = s_Units[j->first].armor();
385 hitunits[j->first]++;
387 //There is a chance that we're hitting a blocked ship.
388 if (m_BlockedFleet[j->first].size() >= 1)
390 int test = rand() % m_BlockedFleet[j->first][0];
392 && m_BlockedFleet[j->first][0] > 0)
394 if (m_BlockedFleet[j->first].size() == 1)
395 m_BlockedFleet[j->first].push_back(m_BlockedFleet[j->first][0] - 1);
396 else if (m_BlockedFleet[j->first][1] > 0)
397 m_BlockedFleet[j->first][1]--;
404 cerr << hitunits[j->first] << " units of type: " << j->first << "killed\n";
408 gunsleft -= maxguns - k;
414 //////////////////////////////////////////////////////////////////////////
416 void Fleet::takeEMP(std::string unittype, int number)
418 int guns = s_Units[unittype].guns() * number;
422 cerr << unittype << ": with " << guns << " guns\n";
424 float gunsleft = guns;
425 for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
427 string ta = s_Units[unittype].target(count);
428 cerr << "Shooting at target class: " << ta << endl;
432 map<string, int*> targets;
434 for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
436 if (j->second.type() == "PDS")
439 if (m_Fleet[j->first].size() == 0)
442 if (m_Fleet[j->first].size() == 1)
443 m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
445 //cerr << "Target is class: " << j->second.type() << endl;
447 if (m_Fleet[j->first][1] > 0 && ( ta == j->second.unitClass() || ta == "All"))
450 // cerr << "Looking at target: " << j->first << endl;
451 targets[j->first] = &m_Fleet[j->first][1];
456 if (targets.size() == 0)
460 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
461 total += (*j->second);
463 for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
465 int maxguns = (*j->second)/total * guns;
466 cerr << "Now shooting at target: " << j->first << endl;
475 if (*(j->second) <= 0)
478 int eres = s_Units[j->first].EMP();
480 k -= float(100)/(100-eres);
482 blockFleet(j->first, 1);
485 cerr << hits << " units of type: " << j->first << " blocked\n";
489 gunsleft -= maxguns - k;
495 //////////////////////////////////////////////////////////////////////////
497 void Fleet::killFleet(std::string unittype, int number, int tick = 0)
500 int ticks = m_Fleet[unittype].size();
503 earlier = m_Fleet[unittype][ticks - 1];
505 for (int i = ticks; i <= tick; ++i)
507 m_Fleet[unittype].push_back(earlier);
509 m_Fleet[unittype][tick] -= number;
512 //////////////////////////////////////////////////////////////////////////
514 void Fleet::setResource(std::string type, int number, int tick = 0)
517 if (m_Resources[type].size() <= tick)
518 m_Resources[type].push_back(number);
519 m_Resources[type][tick] = number;
522 //////////////////////////////////////////////////////////////////////////
524 int Fleet::resource(std::string type, int tick = 0) const
526 vector<int>const* resource = 0;
527 for (ResourceList::const_iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
529 if (i->first == type)
531 resource = &i->second;
538 int ticks = resource->size();
546 return resource->at(ticks);
547 return resource->at(tick);
550 //////////////////////////////////////////////////////////////////////////
552 void Fleet::printFleet()
554 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
556 for (int tick = 0; tick < 5 ;++tick)
558 int num = fleet(i->first, tick);
562 cerr << num << " " << i->first << " during tick: " << tick << endl;
567 //////////////////////////////////////////////////////////////////////////
569 void Fleet::blockFleet(std::string unittype, int number, int tick = 0)
571 if (m_BlockedFleet[unittype].size() >= 1)
573 m_BlockedFleet[unittype][0] += number;
574 if (m_BlockedFleet[unittype].size() > 1)
575 m_BlockedFleet[unittype][1] += number;
577 m_BlockedFleet[unittype].push_back(m_BlockedFleet[unittype][0]);
581 m_BlockedFleet[unittype].push_back(number);
582 m_BlockedFleet[unittype].push_back(number);