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() const
38 //////////////////////////////////////////////////////////////////////////
40 void Planet::setPlanetScore(unsigned i)
45 //////////////////////////////////////////////////////////////////////////
47 int Planet::roids(std::string type, int tick = 0) const
49 // const... I would like [] as for const types: int ticks = m_Roids[type].size();
51 vector<int>const* roids = 0;
52 for (RoidList::const_iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
63 int ticks = roids->size();
68 return roids->at(ticks);
69 return roids->at(tick);
72 //////////////////////////////////////////////////////////////////////////
74 void Planet::setRoids(std::string type, int number)
76 if (m_Roids[type].size() == 0)
77 m_Roids[type].push_back(number);
78 m_Roids[type][0] = number;
81 //////////////////////////////////////////////////////////////////////////
83 void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile)
85 for(int tick = 1; ; ++tick)
87 //See who's in the battle at the current tick
88 vector<Fleet*> friends = calculateSide(friendly, 6, tick);
89 vector<Fleet*> hostiles = calculateSide(hostile, 3, tick);
91 // No idea to calculate anything if noone is there.. ;)
92 if (hostiles.size() == 0)
96 allFriends.addToThis(friends);
99 allHostiles.addToThis(hostiles);
101 map<string, map<string, int> > stealfriendly;
102 map<string, map<string, int> > stealhostile;
104 calcOneTick(&allFriends, &allHostiles, stealfriendly, stealhostile );
106 allFriends.distributeLossesGains(friends, tick);
107 allHostiles.distributeLossesGains(friends, tick);
111 //////////////////////////////////////////////////////////////////////////
113 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 )
115 map<int, string> unitsinit; // order units after their ininitiative
116 for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
117 unitsinit[i->second.ETA()] = i->first;
119 for (map<int, string>::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i)
121 Fleet* hostiletemp = new Fleet(*hostile);
122 Planet* friendlytemp = new Planet(*friendly);
124 string unittype = i->second;
126 if (s_Units[unittype].type() == "EMP")
128 hostiletemp->takeEMP(unittype, friendly->freeFleet(unittype, 1));
129 friendlytemp->takeEMP(unittype, hostile->freeFleet(unittype, 1));
131 else if (s_Units[unittype].type() == "Steal")
133 hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), stealfriendly[unittype]);
134 friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), stealhostile[unittype]);
138 map<string, int> temp;
139 hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), temp);
140 friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), temp);
143 if (s_Units[unittype].type() == "Pod")
145 float capping = friendly->m_iScore / hostile->score() / 10;
146 for (RoidList::iterator roids = m_Roids.begin(); roids != m_Roids.end(); ++roids)
148 int caproids = capping * roids->second[0];
149 int freepods = hostiletemp->freeFleet(unittype, 1);
153 if (freepods < caproids)
154 caproids = caproids - freepods;
156 roids->second.push_back(roids->second[0] - caproids);
157 hostiletemp->killFleet(unittype, caproids, 1);
161 //set the the objects so they point at the modified objects
163 friendly = friendlytemp;
165 hostile = hostiletemp;