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();
69 return roids->at(ticks);
70 return roids->at(tick);
73 //////////////////////////////////////////////////////////////////////////
75 void Planet::setRoids(std::string type, int number)
77 if (m_Roids[type].size() == 0)
78 m_Roids[type].push_back(number);
79 m_Roids[type][0] = number;
82 //////////////////////////////////////////////////////////////////////////
84 void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile)
86 if (hostile.size() == 0)
90 for(int tick = 1; skipped < 20; ++tick)
92 //See who's in the battle at the current tick
93 vector<Fleet*> friends = calculateSide(friendly, 6, tick);
94 vector<Fleet*> hostiles = calculateSide(hostile, 3, tick);
96 // No idea to calculate anything if noone is there.. ;)
97 if (hostiles.size() == 0)
106 allFriends.addToThis(friends);
109 allHostiles.addToThis(hostiles);
111 map<string, map<string, int> > stealfriendly;
112 map<string, map<string, int> > stealhostile;
114 calcOneTick(&allFriends, &allHostiles, stealfriendly, stealhostile );
116 allFriends.distributeLossesGains(friends, tick);
117 allHostiles.distributeLossesGains(hostiles, tick);
121 //////////////////////////////////////////////////////////////////////////
123 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 )
125 map<int, string> unitsinit; // order units after their ininitiative
126 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
127 unitsinit[i->second.initiative()] = i->first;
129 for (map<int, string>::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i)
131 Fleet* hostiletemp = new Fleet(*hostile);
132 Planet* friendlytemp = new Planet(*friendly);
134 string unittype = i->second;
136 cerr << "Initiative: " << s_Units[unittype].initiative() << " with unit: " << unittype << endl;
138 if (s_Units[unittype].type() == "EMP")
140 hostiletemp->takeEMP(unittype, friendly->freeFleet(unittype, 1));
141 friendlytemp->takeEMP(unittype, hostile->freeFleet(unittype, 1));
143 else if (s_Units[unittype].type() == "Steal")
145 hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), stealfriendly[unittype]);
146 friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), stealhostile[unittype]);
150 map<string, int> temp;
151 hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), temp);
152 friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), temp);
155 if (s_Units[unittype].type() == "Pod")
157 float capping = friendly->m_iScore / hostile->score() / 10;
158 for (RoidList::iterator roids = m_Roids.begin(); roids != m_Roids.end(); ++roids)
160 int caproids = capping * roids->second[0];
161 int freepods = hostiletemp->freeFleet(unittype, 1);
165 if (freepods < caproids)
166 caproids = caproids - freepods;
168 roids->second.push_back(roids->second[0] - caproids);
169 hostiletemp->killFleet(unittype, caproids, 1);
173 //set the the objects so they point at the modified objects
174 *friendly = *friendlytemp;
176 *hostile = *hostiletemp;