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() const
39 //////////////////////////////////////////////////////////////////////////
41 void Planet::setPlanetScore(unsigned i)
46 //////////////////////////////////////////////////////////////////////////
48 int Planet::roids(std::string type, int tick = 0) const
50 // const... I would like [] as for const types: int ticks = m_Roids[type].size();
52 vector<int>const* roids = 0;
53 for (RoidList::const_iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
64 int ticks = roids->size();
70 return roids->at(ticks);
71 return roids->at(tick);
74 //////////////////////////////////////////////////////////////////////////
76 void Planet::setRoids(std::string type, int number, int tick = 0)
78 int ticks = m_Roids[type].size();
80 if (m_Roids[type].size() > 0)
81 roids = m_Roids[type][m_Roids[type].size() - 1];
83 for (int i = ticks; i <= tick; ++i )
84 m_Roids[type].push_back(roids);
85 m_Roids[type][tick] = number;
88 //////////////////////////////////////////////////////////////////////////
90 void Planet::takeRoids(std::string type, int number, int tick = 0)
92 int ticks = m_Roids[type].size();
94 if (m_Roids[type].size() > 0)
95 roids = m_Roids[type][m_Roids[type].size() - 1];
97 for (int i = ticks; i <= tick; ++i )
98 m_Roids[type].push_back(roids);
99 m_Roids[type][tick] -= number;
104 //////////////////////////////////////////////////////////////////////////
106 void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile)
108 if (hostile.size() == 0)
112 for(int tick = 1; skipped < 20; ++tick)
114 //See who's in the battle at the current tick
115 vector<Fleet*> friends = calculateSide(friendly, 6, tick);
116 vector<Fleet*> hostiles = calculateSide(hostile, 3, tick);
118 // No idea to calculate anything if noone is there.. ;)
119 if (hostiles.size() == 0)
128 allFriends.addToThis(friends, tick - 1);
130 /*for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
131 allFriends.setRoids(i->first, roids(i->first, tick));
133 allFriends.setPlanetScore(m_iScore);*/
136 allHostiles.addToThis(hostiles, tick - 1);
138 map<string, map<string, int> > stealfriendly;
139 map<string, map<string, int> > stealhostile;
141 calcOneTick(&allFriends, &allHostiles, stealfriendly, stealhostile, tick );
143 //allFriends.printFleet();
145 allFriends.distributeLossesGains(friends, tick);
146 allHostiles.distributeLossesGains(hostiles, tick);
147 allHostiles.distributeCappedRoids(hostiles, tick);
149 /* for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
150 setRoids(i->first, roids(i->first, 1), tick);*/
153 for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
155 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
156 cout << i->first << " roids : " << (*j) << endl;
160 //////////////////////////////////////////////////////////////////////////
162 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 = 0)
164 map<int, string> unitsinit; // order units after their ininitiative
165 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
166 unitsinit[i->second.initiative()] = i->first;
168 for (map<int, string>::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i)
170 Fleet* hostiletemp = new Fleet(*hostile);
171 Planet* friendlytemp = new Planet(*friendly);
173 string unittype = i->second;
175 //cerr << "Initiative: " << s_Units[unittype].initiative() << " with unit: " << unittype << endl;
177 if (s_Units[unittype].type() == "EMP")
179 hostiletemp->takeEMP(unittype, friendly->freeFleet(unittype, 1));
180 friendlytemp->takeEMP(unittype, hostile->freeFleet(unittype, 1));
182 else if (s_Units[unittype].type() == "Steal")
184 hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), stealfriendly[unittype]);
185 friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), stealhostile[unittype]);
189 map<string, int> temp;
190 hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), temp);
191 friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), temp);
194 if (s_Units[unittype].type() == "Pod")
198 float capping = float(m_iScore) / hostile->score() / 10;
200 cerr << "Capping is: " << capping << endl;
206 for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
208 int caproids = capping * roids(roid->first, tick - 1);
209 int freepods = hostiletemp->freeFleet(unittype, 1);
211 cerr << "Possible to steal " << caproids << " " << roid->first << " roids\n";
212 cerr << freepods << " free pods available\n";
216 if (freepods < caproids)
219 cerr << caproids << " roids stolen\n";
221 takeRoids(roid->first, caproids, tick);
223 //FIXME: Going to move this to the distribute roids section instead..
224 hostiletemp->killFleet(unittype, caproids, 1);
225 //int totroids = caproids + hostiletemp->resource(roid->first, 0);
226 hostiletemp->addResource(roid->first, caproids, 1);
228 cerr << caproids << " stolen " << roid->first << " roids\n";
234 //set the the objects so they point at the modified objects
235 *friendly = *friendlytemp;
237 *hostile = *hostiletemp;