1 /***************************************************************************
2 planet.cpp - description
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 ***************************************************************************/
32 //////////////////////////////////////////////////////////////////////////
34 unsigned Planet::planetScore(int tick) const
36 int ticks = m_Score.size();
43 return m_Score[ticks];
48 //////////////////////////////////////////////////////////////////////////
50 void Planet::setPlanetScore(unsigned number, int tick)
53 int ticks = m_Score.size();
56 earlier = m_Score[ticks - 1];
58 for (int i = ticks; i <= tick; ++i)
60 m_Score.push_back(earlier);
62 m_Score[tick] = number;
65 //////////////////////////////////////////////////////////////////////////
67 void Planet::addPlanetScore(unsigned number, int tick)
70 int ticks = m_Score.size();
73 earlier = m_Score[ticks - 1];
75 for (int i = ticks; i <= tick; ++i)
77 m_Score.push_back(earlier);
79 m_Score[tick] += number;
81 //////////////////////////////////////////////////////////////////////////
83 int Planet::roids(std::string type, int tick) const
85 // const... I would like [] as for const types: int ticks = m_Roids[type].size();
87 vector<int>const* roids = 0;
88 for (RoidList::const_iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
99 int ticks = roids->size();
105 return roids->at(ticks);
106 return roids->at(tick);
109 //////////////////////////////////////////////////////////////////////////
111 void Planet::setRoids(std::string type, int number, int tick)
113 int ticks = m_Roids[type].size();
115 if (m_Roids[type].size() > 0)
116 roids = m_Roids[type][m_Roids[type].size() - 1];
118 for (int i = ticks; i <= tick; ++i )
119 m_Roids[type].push_back(roids);
120 m_Roids[type][tick] = number;
123 //////////////////////////////////////////////////////////////////////////
125 void Planet::takeRoids(std::string type, int number, int tick)
127 int ticks = m_Roids[type].size();
129 if (m_Roids[type].size() > 0)
130 roids = m_Roids[type][m_Roids[type].size() - 1];
132 for (int i = ticks; i <= tick; ++i )
133 m_Roids[type].push_back(roids);
134 m_Roids[type][tick] -= number;
136 if (type != "uninit")
137 addPlanetScore(-1000*number, tick);
142 //////////////////////////////////////////////////////////////////////////
144 void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile)
146 if (hostile.size() == 0)
151 for (vector<Fleet*>::iterator i = friendly.begin(); i != friendly.end(); ++i)
154 for (vector<Fleet*>::iterator i = hostile.begin(); i != hostile.end(); ++i)
157 for(int tick = 1; skipped < 20; ++tick)
159 //See who's in the battle at the current tick
160 vector<Fleet*> friends = calculateSide(friendly, 6, tick);
161 vector<Fleet*> hostiles = calculateSide(hostile, 3, tick);
163 // No idea to calculate anything if noone is there.. ;)
164 if (hostiles.size() == 0)
173 allFriends.addToThis(friends, tick - 1);
175 /*for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
176 allFriends.setRoids(i->first, roids(i->first, tick));
178 allFriends.setPlanetScore(m_iScore);*/
181 allHostiles.addToThis(hostiles, tick - 1);
183 map<string, map<string, int> > stealfriendly;
184 map<string, map<string, int> > stealhostile;
187 for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
189 setRoids(roid->first, roids(roid->first, tick-1), tick);
192 setPlanetScore(planetScore(tick - 1), tick);
195 calcOneTick(&allFriends, &allHostiles, stealfriendly, stealhostile, tick );
197 //allFriends.printFleet();
199 allFriends.distributeLossesGains(friends, tick);
200 allFriends.distributeStolenShips(stealfriendly, friends, tick);
202 allHostiles.distributeLossesGains(hostiles, tick);
203 allHostiles.distributeCappedRoids(hostiles, tick);
204 allHostiles.distributeStolenShips(stealhostile, hostiles, tick);
206 calculateScoreLoss(friends, tick);
208 /* for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
209 setRoids(i->first, roids(i->first, 1), tick);*/
212 for (vector<Fleet*>::iterator i = friendly.begin(); i != friendly.end(); ++i)
213 (*i)->calculateSalvage();
215 for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
217 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
218 cout << i->first << " roids : " << (*j) << endl;
222 //////////////////////////////////////////////////////////////////////////
224 void Planet::calcOneTick(Planet* friendly, Fleet* hostile, std::map<std::string, std::map<std::string, int> >& stealfriendly, std::map<std::string, std::map<std::string, int> >& stealhostile, int tick)
226 if (planetScore(tick - 1) > 0)
227 setCapping(float(planetScore(tick - 1)) / hostile->score() / 10, tick);
231 //FIXME: remove this line later.. map<string, int> pods;
234 //FIXME: Need to change this and allow multiple shiptypes with the same initiative.
235 //FIXME: Looks like I don't have to..
236 map<int, string> unitsinit; // order units after their ininitiative
237 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
238 unitsinit[i->second.initiative()] = i->first;
240 for (map<int, string>::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i)
242 Fleet* hostiletemp = new Fleet(*hostile);
243 Planet* friendlytemp = new Planet(*friendly);
245 string unittype = i->second;
247 //cerr << "Initiative: " << s_Units[unittype].initiative() << " with unit: " << unittype << endl;
249 if (s_Units[unittype].type() == "EMP")
251 hostiletemp->takeEMP(unittype, friendly->freeFleet(unittype, 1));
252 friendlytemp->takeEMP(unittype, hostile->freeFleet(unittype, 1));
254 else if (s_Units[unittype].type() == "Steal")
256 hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), stealfriendly[unittype]);
257 friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), stealhostile[unittype]);
259 friendlytemp->calculateLostStealships(unittype, stealfriendly[unittype], 1);
260 hostiletemp->calculateLostStealships(unittype, stealhostile[unittype], 1);
264 map<string, int> temp;
265 hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), temp);
266 friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), temp);
269 if (s_Units[unittype].type() == "Pod")
271 //FIXME: Prolly need to recode the whole capping section for r7, due to multiple pods thingy
272 cerr << "Capping is: " << capping(tick) << endl;
274 if (capping(tick) > 0)
276 for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
278 int caproids = capping(tick) * roids(roid->first, tick - 1);
279 //int freepods = hostiletemp->freeFleet(unittype, 1);
281 cerr << "Possible to steal " << caproids << " " << roid->first << " roids\n";
282 cerr << hostiletemp->freePodGuns(1) << " free pod guns available\n";
284 if (hostiletemp->freePodGuns(1) <= 0)
286 if (hostiletemp->freePodGuns(1) < caproids)
287 caproids = hostiletemp->freePodGuns(1);
289 cerr << caproids << " roids stolen\n";
291 takeRoids(roid->first, caproids, tick);
293 /*FIXME: REMOVE THIS BLOCK LATER
294 //FIXME: Going to move this to the distribute roids section instead.. Not really move, I'll keep this, but "regenerate" the pods in the distribute ships function.
295 hostiletemp->killFleet(unittype, caproids, 1);
296 pods[unittype] += caproids;
297 //int totroids = caproids + hostiletemp->resource(roid->first, 0);
299 hostiletemp->addResource(roid->first, caproids, 1);
300 hostiletemp->usePodGuns(1, caproids);
301 cerr << caproids << " stolen " << roid->first << " roids\n";
305 //FIXME: This is ugly and I want to change this, but as long as pods shoot last it works..
306 *friendly = *friendlytemp;
308 *hostile = *hostiletemp;
314 //set the the objects so they point at the modified objects
315 *friendly = *friendlytemp;
317 *hostile = *hostiletemp;
320 //hostile->addFleet(pods, 1);
324 //////////////////////////////////////////////////////////////////////////
326 void Planet::calculateScoreLoss(std::vector<Fleet*> friendly, int tick)
328 for (vector<Fleet*>::iterator i = friendly.begin(); i != friendly.end(); ++i)
330 if ((*i)->name().find("Home") != string::npos)
332 int scorechange = (*i)->score(tick) - (*i)->score(tick - 1);
333 addPlanetScore(scorechange, tick);
338 //////////////////////////////////////////////////////////////////////////
340 float Planet::capping(int tick)
342 int ticks = m_Capping.size();
349 return m_Capping[tick];
352 //////////////////////////////////////////////////////////////////////////
354 void Planet::setCapping(float capping, int tick)
356 int ticks = m_Capping.size();
358 for (int i = ticks; i <= tick; ++i)
360 m_Capping.push_back(0);
363 if (capping <= 0.15 && capping >= 0)
364 m_Capping[tick] = capping;
365 else if (capping >= 0.15)
366 m_Capping[tick] = 0.15;