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