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 ***************************************************************************/
31 //////////////////////////////////////////////////////////////////////////
33 unsigned Planet::planetScore(int tick = 0) const
35 int ticks = m_Score.size();
42 return m_Score[ticks];
47 //////////////////////////////////////////////////////////////////////////
49 void Planet::setPlanetScore(unsigned number, int tick = 0)
52 int ticks = m_Score.size();
55 earlier = m_Score[ticks - 1];
57 for (int i = ticks; i <= tick; ++i)
59 m_Score.push_back(earlier);
61 m_Score[tick] = number;
64 //////////////////////////////////////////////////////////////////////////
66 void Planet::addPlanetScore(unsigned number, int tick = 0)
69 int ticks = m_Score.size();
72 earlier = m_Score[ticks - 1];
74 for (int i = ticks; i <= tick; ++i)
76 m_Score.push_back(earlier);
78 m_Score[tick] += number;
80 //////////////////////////////////////////////////////////////////////////
82 int Planet::roids(std::string type, int tick = 0) const
84 // const... I would like [] as for const types: int ticks = m_Roids[type].size();
86 vector<int>const* roids = 0;
87 for (RoidList::const_iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
98 int ticks = roids->size();
104 return roids->at(ticks);
105 return roids->at(tick);
108 //////////////////////////////////////////////////////////////////////////
110 void Planet::setRoids(std::string type, int number, int tick = 0)
112 int ticks = m_Roids[type].size();
114 if (m_Roids[type].size() > 0)
115 roids = m_Roids[type][m_Roids[type].size() - 1];
117 for (int i = ticks; i <= tick; ++i )
118 m_Roids[type].push_back(roids);
119 m_Roids[type][tick] = number;
122 //////////////////////////////////////////////////////////////////////////
124 void Planet::takeRoids(std::string type, int number, int tick = 0)
126 int ticks = m_Roids[type].size();
128 if (m_Roids[type].size() > 0)
129 roids = m_Roids[type][m_Roids[type].size() - 1];
131 for (int i = ticks; i <= tick; ++i )
132 m_Roids[type].push_back(roids);
133 m_Roids[type][tick] -= number;
135 if (type != "uninit")
136 addPlanetScore(-3000*number, tick);
141 //////////////////////////////////////////////////////////////////////////
143 void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile)
145 if (hostile.size() == 0)
150 for (vector<Fleet*>::iterator i = friendly.begin(); i != friendly.end(); ++i)
151 (*i)->resetResources();
153 for (vector<Fleet*>::iterator i = hostile.begin(); i != hostile.end(); ++i)
154 (*i)->resetResources();
156 for(int tick = 1; skipped < 20; ++tick)
158 //See who's in the battle at the current tick
159 vector<Fleet*> friends = calculateSide(friendly, 6, tick);
160 vector<Fleet*> hostiles = calculateSide(hostile, 3, tick);
162 // No idea to calculate anything if noone is there.. ;)
163 if (hostiles.size() == 0)
172 allFriends.addToThis(friends, tick - 1);
174 /*for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
175 allFriends.setRoids(i->first, roids(i->first, tick));
177 allFriends.setPlanetScore(m_iScore);*/
180 allHostiles.addToThis(hostiles, tick - 1);
182 map<string, map<string, int> > stealfriendly;
183 map<string, map<string, int> > stealhostile;
186 for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
188 setRoids(roid->first, roids(roid->first, tick-1), tick);
191 setPlanetScore(planetScore(tick - 1), tick);
194 calcOneTick(&allFriends, &allHostiles, stealfriendly, stealhostile, tick );
196 //allFriends.printFleet();
198 allFriends.distributeLossesGains(friends, tick);
200 allHostiles.distributeLossesGains(hostiles, tick);
201 allHostiles.distributeCappedRoids(hostiles, tick);
203 calculateScoreLoss(friends, tick);
205 /* for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
206 setRoids(i->first, roids(i->first, 1), tick);*/
209 for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
211 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
212 cout << i->first << " roids : " << (*j) << endl;
216 //////////////////////////////////////////////////////////////////////////
218 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 = 1)
220 if (planetScore(tick - 1) > 0)
221 setCapping(float(planetScore(tick - 1)) / hostile->score() / 10, tick);
225 map<string, int> pods;
228 map<int, string> unitsinit; // order units after their ininitiative
229 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
230 unitsinit[i->second.initiative()] = i->first;
232 for (map<int, string>::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i)
234 Fleet* hostiletemp = new Fleet(*hostile);
235 Planet* friendlytemp = new Planet(*friendly);
237 string unittype = i->second;
239 //cerr << "Initiative: " << s_Units[unittype].initiative() << " with unit: " << unittype << endl;
241 if (s_Units[unittype].type() == "EMP")
243 hostiletemp->takeEMP(unittype, friendly->freeFleet(unittype, 1));
244 friendlytemp->takeEMP(unittype, hostile->freeFleet(unittype, 1));
246 else if (s_Units[unittype].type() == "Steal")
248 hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), stealfriendly[unittype]);
249 friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), stealhostile[unittype]);
253 map<string, int> temp;
254 hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), temp);
255 friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), temp);
258 if (s_Units[unittype].type() == "Pod")
260 cerr << "Capping is: " << capping(tick) << endl;
262 if (capping(tick) > 0)
264 for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
266 int caproids = capping(tick) * roids(roid->first, tick - 1);
267 int freepods = hostiletemp->freeFleet(unittype, 1);
269 cerr << "Possible to steal " << caproids << " " << roid->first << " roids\n";
270 cerr << freepods << " free pods available\n";
274 if (freepods < caproids)
277 cerr << caproids << " roids stolen\n";
279 takeRoids(roid->first, caproids, tick);
281 //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.
282 hostiletemp->killFleet(unittype, caproids, 1);
283 pods[unittype] += caproids;
284 //int totroids = caproids + hostiletemp->resource(roid->first, 0);
285 hostiletemp->addResource(roid->first, caproids, 1);
287 cerr << caproids << " stolen " << roid->first << " roids\n";
292 //set the the objects so they point at the modified objects
293 *friendly = *friendlytemp;
295 *hostile = *hostiletemp;
298 hostile->addFleet(pods, 1);
302 //////////////////////////////////////////////////////////////////////////
304 void Planet::calculateScoreLoss(std::vector<Fleet*> friendly, int tick = 1)
306 for (vector<Fleet*>::iterator i = friendly.begin(); i != friendly.end(); ++i)
308 if ((*i)->name().find("Home") != string::npos)
310 int scorechange = (*i)->score(tick) - (*i)->score(tick - 1);
311 addPlanetScore(scorechange, tick);
316 //////////////////////////////////////////////////////////////////////////
318 float Planet::capping(int tick = 0)
320 int ticks = m_Capping.size();
327 return m_Capping[tick];
330 //////////////////////////////////////////////////////////////////////////
332 void Planet::setCapping(float capping, int tick = 0)
334 int ticks = m_Capping.size();
336 for (int i = ticks; i <= tick; ++i)
338 m_Capping.push_back(0);
341 if (capping <= 0.15 && capping >= 0)
342 m_Capping[tick] = capping;
344 m_Capping[tick] = 0.15;