]> ruin.nu Git - hbs.git/blob - bs/planet.cpp
3e1f39ae65eab751585cde681e1cddc6789c67bd
[hbs.git] / bs / planet.cpp
1 /***************************************************************************
2                           planet.cpp  -  description
3                              -------------------
4     begin                : Wed Apr 3 2002
5     copyright            : (C) 2002 by Michael Andreen
6     email                : whale@linux.nu
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
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.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include "planet.h"
19
20 #include <iostream>
21 using namespace std;
22
23 Planet::Planet()
24 {
25         m_sRace = "Planet";
26 }
27
28 Planet::~Planet(){
29 }
30
31 //////////////////////////////////////////////////////////////////////////
32 //
33 unsigned Planet::planetScore(int tick = 0) const
34 {
35         int ticks = m_Score.size();
36         if (ticks == 0)
37                 return 0;
38
39         --ticks;
40
41         if (ticks < tick)
42                 return m_Score[ticks];
43
44         return m_Score[tick];
45 }
46
47 //////////////////////////////////////////////////////////////////////////
48 //
49 void Planet::setPlanetScore(unsigned number, int tick = 0)
50 {
51         int earlier = 0;
52         int ticks = m_Score.size();
53
54         if (ticks != 0)
55                 earlier = m_Score[ticks - 1];
56
57         for (int i = ticks; i <= tick; ++i)
58         {
59                 m_Score.push_back(earlier);
60         }
61         m_Score[tick] = number;
62 }
63
64 //////////////////////////////////////////////////////////////////////////
65 //
66 void Planet::addPlanetScore(unsigned number, int tick = 0)
67 {
68         int earlier = 0;
69         int ticks = m_Score.size();
70
71         if (ticks != 0)
72                 earlier = m_Score[ticks - 1];
73
74         for (int i = ticks; i <= tick; ++i)
75         {
76                 m_Score.push_back(earlier);
77         }
78         m_Score[tick] += number;
79 }
80 //////////////////////////////////////////////////////////////////////////
81 //
82 int Planet::roids(std::string type, int tick = 0) const
83 {
84         // const... I would like [] as for const types: int ticks = m_Roids[type].size();
85         
86         vector<int>const* roids = 0;
87         for (RoidList::const_iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
88         {
89                 if (i->first == type)
90                 {
91                         roids = &i->second;
92                         break;
93                 }
94         }
95         if (roids == 0)
96                 return 0;
97
98         int ticks = roids->size();
99
100         if( ticks == 0)
101                 return 0;
102         --ticks;
103         if (ticks < tick)
104                 return roids->at(ticks);
105         return roids->at(tick);
106 }
107
108 //////////////////////////////////////////////////////////////////////////
109 //
110 void Planet::setRoids(std::string type, int number, int tick = 0)
111 {
112         int ticks = m_Roids[type].size();
113         int roids = 0;
114         if (m_Roids[type].size() > 0)
115                 roids = m_Roids[type][m_Roids[type].size() - 1];
116
117         for (int i = ticks; i <= tick; ++i )
118                 m_Roids[type].push_back(roids);
119         m_Roids[type][tick] = number;
120 }
121
122 //////////////////////////////////////////////////////////////////////////
123 //
124 void Planet::takeRoids(std::string type, int number, int tick = 0)
125 {
126         int ticks = m_Roids[type].size();
127         int roids = 0;
128         if (m_Roids[type].size() > 0)
129                 roids = m_Roids[type][m_Roids[type].size() - 1];
130
131         for (int i = ticks; i <= tick; ++i )
132                 m_Roids[type].push_back(roids);
133         m_Roids[type][tick] -= number;
134 }
135
136
137
138 //////////////////////////////////////////////////////////////////////////
139 //
140 void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile)
141 {
142         if (hostile.size() == 0)
143                         return;
144
145         int skipped = 0;
146         for(int tick = 1; skipped < 20; ++tick)
147         {
148                 //See who's in the battle at the current tick
149                 vector<Fleet*> friends = calculateSide(friendly, 6, tick);
150                 vector<Fleet*> hostiles = calculateSide(hostile, 3, tick);
151
152                 // No idea to calculate anything if noone is there.. ;)
153                 if (hostiles.size() == 0)
154                 {
155                         skipped++;
156                         continue;
157                 }
158                 else
159                         skipped = 0;
160
161                 Planet allFriends;
162                 allFriends.addToThis(friends, tick - 1);
163
164                 /*for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
165                         allFriends.setRoids(i->first, roids(i->first, tick));
166
167                 allFriends.setPlanetScore(m_iScore);*/
168                 
169                 Fleet allHostiles;
170                 allHostiles.addToThis(hostiles, tick - 1);
171
172                 map<string, map<string, int> > stealfriendly;
173                 map<string, map<string, int> > stealhostile;
174
175                 //Reset roids
176                 for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
177                         setRoids(roid->first, roids(roid->first, tick-1), tick);
178
179                 setPlanetScore(planetScore(tick - 1), tick);
180                 
181                 
182                 calcOneTick(&allFriends, &allHostiles, stealfriendly, stealhostile, tick );
183
184                 //allFriends.printFleet();
185
186                 allFriends.distributeLossesGains(friends, tick);
187                 allHostiles.distributeLossesGains(hostiles, tick);
188                 allHostiles.distributeCappedRoids(hostiles, tick);
189
190                 calculateScoreLoss(friends, tick);
191
192         /*      for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
193                         setRoids(i->first, roids(i->first, 1), tick);*/
194         }
195
196         for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
197         {
198                 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
199                         cout << i->first << " roids : " << (*j) << endl;
200         }
201 }
202
203 //////////////////////////////////////////////////////////////////////////
204 //
205 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)
206 {
207         map<int, string> unitsinit; // order units after their ininitiative
208         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
209                 unitsinit[i->second.initiative()] = i->first;
210
211         for (map<int, string>::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i)
212         {
213                 Fleet* hostiletemp = new Fleet(*hostile);
214                 Planet* friendlytemp = new Planet(*friendly);
215                 
216                 string unittype = i->second;
217
218                 //cerr << "Initiative: " << s_Units[unittype].initiative() << " with unit: " << unittype << endl;
219
220                 if (s_Units[unittype].type() == "EMP")  
221                 {
222                         hostiletemp->takeEMP(unittype, friendly->freeFleet(unittype, 1));
223                         friendlytemp->takeEMP(unittype, hostile->freeFleet(unittype, 1));
224                 }
225                 else if (s_Units[unittype].type() == "Steal")
226                 {
227                         hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), stealfriendly[unittype]);
228                         friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), stealhostile[unittype]);
229                 }
230                 else
231                 {
232                         map<string, int> temp;
233                         hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), temp);
234                         friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), temp);
235                 }
236
237                 if (s_Units[unittype].type() == "Pod")
238                 {
239                         if (planetScore(tick - 1) > 0)
240                         {
241                                 setCapping(float(planetScore(tick - 1)) / hostile->score() /  10, tick);
242                         
243                                 cerr << "Capping is: " << capping(tick) << endl;
244         
245                                 if (capping(tick) > 0)
246                                 {
247                                         for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
248                                         {
249                                                 int caproids = capping(tick) * roids(roid->first, tick - 1);
250                                                 int freepods = hostiletemp->freeFleet(unittype, 1);
251                 
252                                                 cerr << "Possible to steal " << caproids << " " << roid->first << " roids\n";
253                                                 cerr << freepods << " free pods available\n";
254                                                 
255                                                 if (freepods <= 0)
256                                                         break;
257                                                 if (freepods < caproids)
258                                                         caproids = freepods;
259                 
260                                                 cerr << caproids << " roids stolen\n";
261
262                                                 takeRoids(roid->first, caproids, tick);
263                 
264                                                 //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.
265                                                 hostiletemp->killFleet(unittype, caproids, 1);
266                                                 //int totroids = caproids + hostiletemp->resource(roid->first, 0);
267                                                 hostiletemp->addResource(roid->first, caproids, 1);
268                                                 
269                                                 cerr << caproids << " stolen " << roid->first << " roids\n";
270                                         }
271                                 }
272                         }
273                 }
274
275                 //set the the objects so they point at the modified objects
276                 *friendly = *friendlytemp;
277                 delete friendlytemp;
278                 *hostile = *hostiletemp;
279                 delete hostiletemp;
280         }
281 }
282
283 //////////////////////////////////////////////////////////////////////////
284 //
285 void Planet::calculateScoreLoss(std::vector<Fleet*> friendly, int tick = 1)
286 {
287         for (vector<Fleet*>::iterator i = friendly.begin(); i != friendly.end(); ++i)
288         {
289                 if ((*i)->name().find("Home") != string::npos)
290                 {
291                         int scorechange = (*i)->score(tick) - (*i)->score(tick - 1);
292                         addPlanetScore(scorechange, tick);
293                 }
294         }
295 }
296
297 //////////////////////////////////////////////////////////////////////////
298 //
299 float Planet::capping(int tick = 0)
300 {
301         int ticks = m_Capping.size();
302
303         --ticks;
304
305         if (ticks < tick)
306                 return 0;
307
308         return m_Capping[tick];
309 }
310
311 //////////////////////////////////////////////////////////////////////////
312 //
313 void Planet::setCapping(float capping, int tick = 0)
314 {
315         int ticks = m_Capping.size();
316
317         for (int i = ticks; i <= tick; ++i)
318         {
319                 m_Capping.push_back(0);
320         }
321
322         if (capping <= 0.15 && capping >= 0)
323                 m_Capping[tick] = capping;
324         else
325                 m_Capping[tick] = 0.15;
326 }