]> ruin.nu Git - hbs.git/blob - bs/planet.cpp
started to work on the battle report.. almost there =)
[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) 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)
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)
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) 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)
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)
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(-1000*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         m_Report.clear();
158
159         for(int tick = 1; skipped < 20; ++tick)
160         {
161                 //See who's in the battle at the current tick
162                 vector<Fleet*> friends = calculateSide(friendly, 6, tick);
163                 vector<Fleet*> hostiles = calculateSide(hostile, 3, tick);
164
165                 // No idea to calculate anything if noone is there.. ;)
166                 if (hostiles.size() == 0)
167                 {
168                         skipped++;
169                         continue;
170                 }
171                 else
172                         skipped = 0;
173
174                 Planet allFriends;
175                 allFriends.addToThis(friends, tick - 1);
176
177                 /*for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
178                         allFriends.setRoids(i->first, roids(i->first, tick));
179
180                 allFriends.setPlanetScore(m_iScore);*/
181                 
182                 Fleet allHostiles;
183                 allHostiles.addToThis(hostiles, tick - 1);
184
185                 map<string, map<string, int> > stealfriendly;
186                 map<string, map<string, int> > stealhostile;
187
188                 //Reset roids
189                 for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
190                 {
191                         setRoids(roid->first, roids(roid->first, tick-1), tick);
192                 }
193
194                 setPlanetScore(planetScore(tick - 1), tick);
195                 
196                 
197                 calcOneTick(&allFriends, &allHostiles, stealfriendly, stealhostile, tick );
198
199                 //allFriends.printFleet();
200
201                 allFriends.distributeLossesGains(friends, tick);
202                 allFriends.distributeStolenShips(stealfriendly, friends, tick);
203
204                 allHostiles.distributeLossesGains(hostiles, tick);
205                 allHostiles.distributeCappedRoids(hostiles, tick);
206                 allHostiles.distributeStolenShips(stealhostile, hostiles, tick);
207
208                 calculateScoreLoss(friends, tick);
209
210         /*      for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
211                         setRoids(i->first, roids(i->first, 1), tick);*/
212         }
213
214         for (vector<Fleet*>::iterator i = friendly.begin(); i != friendly.end(); ++i)
215                 (*i)->calculateSalvage();
216
217         for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
218         {
219                 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
220                         cout << i->first << " roids : " << (*j) << endl;
221         }
222 }
223
224 //////////////////////////////////////////////////////////////////////////
225 //
226 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)
227 {
228         if (planetScore(tick - 1) > 0)
229                 setCapping(float(planetScore(tick - 1)) / hostile->score() /  10, tick);
230         else
231                 setCapping(0, tick);
232                         
233         //FIXME: remove this line later.. map<string, int> pods;
234
235
236         //FIXME: Need to change this and allow multiple shiptypes with the same initiative. 
237         //FIXME: Looks like I don't have to.. 
238         map<int, string> unitsinit; // order units after their ininitiative
239         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
240                 unitsinit[i->second.initiative()] = i->first;
241
242         
243
244         for (map<int, string>::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i)
245         {
246                 Fleet* hostiletemp = new Fleet(*hostile);
247                 Planet* friendlytemp = new Planet(*friendly);
248                 
249                 string unittype = i->second;
250                 int init = i->first;
251
252                 int freefriendly = friendly->freeFleet(unittype, 1);
253                 int freehostile = hostile->freeFleet(unittype, 1);
254
255                 if ( freefriendly <= 0 && freehostile <= 0)
256                         continue;
257
258                 //cerr << "Initiative: " << s_Units[unittype].initiative() << " with unit: " << unittype << endl;
259         
260                 map<string, int> friendlyhits;
261                 map<string, int> hostilehits;
262
263                 if (s_Units[unittype].type() == "EMP")  
264                 {
265                         hostiletemp->takeEMP(unittype, freefriendly, friendlyhits);
266                         friendlytemp->takeEMP(unittype, freehostile, hostilehits);
267                 }
268                 else if (s_Units[unittype].type() == "Steal")
269                 {
270                         hostiletemp->takeShoot(unittype, freefriendly, stealfriendly[unittype]);
271                         friendlytemp->takeShoot(unittype, freehostile, stealhostile[unittype]);
272                         
273                         friendlyhits = stealfriendly[unittype];
274                         hostilehits = stealhostile[unittype];
275
276                         friendlytemp->calculateLostStealships(unittype, stealfriendly[unittype], 1);
277                         hostiletemp->calculateLostStealships(unittype, stealhostile[unittype], 1);
278                 }
279                 else
280                 {
281                         hostiletemp->takeShoot(unittype, freefriendly, friendlyhits);
282                         friendlytemp->takeShoot(unittype, freehostile, hostilehits);
283                 }
284
285
286                 if (s_Units[unittype].type() == "Pod")
287                 {
288                         //FIXME: Prolly need to recode the whole capping section for r7, due to multiple pods thingy
289                         cerr << "Capping is: " << capping(tick) << endl;
290         
291                         if (capping(tick) > 0)
292                         {
293                                 for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
294                                 {
295                                         int caproids = int(capping(tick) * roids(roid->first, tick - 1));
296                                         //int freepods = hostiletemp->freeFleet(unittype, 1);
297                 
298                                         cerr << "Possible to steal " << caproids << " " << roid->first << " roids\n";
299                                         cerr << hostiletemp->freePodGuns(1) << " free pod guns available\n";
300                                 
301                                         if (hostiletemp->freePodGuns(1) <= 0)
302                                                 break;
303                                         if (hostiletemp->freePodGuns(1) < caproids)
304                                                 caproids = hostiletemp->freePodGuns(1);
305                 
306                                         cerr << caproids << " roids stolen\n";
307
308                                         takeRoids(roid->first, caproids, tick);
309                 
310                                         /*FIXME: REMOVE THIS BLOCK LATER
311                                         //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.
312                                         hostiletemp->killFleet(unittype, caproids, 1);
313                                         pods[unittype] += caproids;
314                                         //int totroids = caproids + hostiletemp->resource(roid->first, 0);
315                                         */
316                                         hostiletemp->addResource(roid->first, caproids, 1);
317                                         hostiletemp->usePodGuns(1, caproids);
318                                         cerr << caproids << " stolen " << roid->first << " roids\n";
319                                 }
320                         }
321
322                         //FIXME: This is ugly and I want to change this, but as long as pods shoot last it works.. 
323                         *friendly = *friendlytemp;
324                         delete friendlytemp;
325                         *hostile = *hostiletemp;
326                         delete hostiletemp;
327
328                         return;
329                 }
330                 m_Report[tick][init]["Friendly"][unittype] = friendlyhits;
331                 m_Report[tick][init]["Friendly"][unittype]["000"] = freefriendly;
332                 m_Report[tick][init]["Hostile"][unittype] = hostilehits;
333                 m_Report[tick][init]["Hostile"][unittype]["000"] = freehostile;
334
335                 //set the the objects so they point at the modified objects
336                 *friendly = *friendlytemp;
337                 delete friendlytemp;
338                 *hostile = *hostiletemp;
339                 delete hostiletemp;
340         }
341         //hostile->addFleet(pods, 1);
342         
343 }
344
345 //////////////////////////////////////////////////////////////////////////
346 //
347 void Planet::calculateScoreLoss(std::vector<Fleet*> friendly, int tick)
348 {
349         for (vector<Fleet*>::iterator i = friendly.begin(); i != friendly.end(); ++i)
350         {
351                 if ((*i)->name().find("Home") != string::npos)
352                 {
353                         int scorechange = (*i)->score(tick) - (*i)->score(tick - 1);
354                         addPlanetScore(scorechange, tick);
355                 }
356         }
357 }
358
359 //////////////////////////////////////////////////////////////////////////
360 //
361 float Planet::capping(int tick)
362 {
363         int ticks = m_Capping.size();
364
365         --ticks;
366
367         if (ticks < tick)
368                 return 0;
369
370         return m_Capping[tick];
371 }
372
373 //////////////////////////////////////////////////////////////////////////
374 //
375 void Planet::setCapping(float capping, int tick)
376 {
377         int ticks = m_Capping.size();
378
379         for (int i = ticks; i <= tick; ++i)
380         {
381                 m_Capping.push_back(0);
382         }
383
384         if (capping <= 0.15 && capping >= 0)
385                 m_Capping[tick] = capping;
386         else if (capping >= 0.15)
387                 m_Capping[tick] = 0.15;
388         else
389                 m_Capping[tick] = 0;
390 }
391
392 //////////////////////////////////////////////////////////////////////////
393 //
394 ReportList Planet::report() const
395 {
396         return m_Report;
397 }