]> ruin.nu Git - hbs.git/blob - bs/planet.cpp
fixed a bug with non shooting pds...
[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_iScore = 0;
27 }
28
29 Planet::~Planet(){
30 }
31
32 //////////////////////////////////////////////////////////////////////////
33 //
34 unsigned Planet::planetScore() const
35 {
36         return m_iScore;
37 }
38
39 //////////////////////////////////////////////////////////////////////////
40 //
41 void Planet::setPlanetScore(unsigned i)
42 {
43         m_iScore = i;
44 }
45
46 //////////////////////////////////////////////////////////////////////////
47 //
48 int Planet::roids(std::string type, int tick = 0) const
49 {
50         // const... I would like [] as for const types: int ticks = m_Roids[type].size();
51         
52         vector<int>const* roids = 0;
53         for (RoidList::const_iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
54         {
55                 if (i->first == type)
56                 {
57                         roids = &i->second;
58                         break;
59                 }
60         }
61         if (roids == 0)
62                 return 0;
63
64         int ticks = roids->size();
65
66         if( ticks == 0)
67                 return 0;
68         --ticks;
69         if (ticks < tick)
70                 return roids->at(ticks);
71         return roids->at(tick);
72 }
73
74 //////////////////////////////////////////////////////////////////////////
75 //
76 void Planet::setRoids(std::string type, int number, int tick = 0)
77 {
78         int ticks = m_Roids[type].size();
79         int roids = 0;
80         if (m_Roids[type].size() > 0)
81                 roids = m_Roids[type][m_Roids[type].size() - 1];
82
83         for (int i = ticks; i <= tick; ++i )
84                 m_Roids[type].push_back(roids);
85         m_Roids[type][tick] = number;
86 }
87
88 //////////////////////////////////////////////////////////////////////////
89 //
90 void Planet::takeRoids(std::string type, int number, int tick = 0)
91 {
92         int ticks = m_Roids[type].size();
93         int roids = 0;
94         if (m_Roids[type].size() > 0)
95                 roids = m_Roids[type][m_Roids[type].size() - 1];
96
97         for (int i = ticks; i <= tick; ++i )
98                 m_Roids[type].push_back(roids);
99         m_Roids[type][tick] -= number;
100 }
101
102
103
104 //////////////////////////////////////////////////////////////////////////
105 //
106 void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile)
107 {
108         if (hostile.size() == 0)
109                         return;
110
111         int skipped = 0;
112         for(int tick = 1; skipped < 20; ++tick)
113         {
114                 //See who's in the battle at the current tick
115                 vector<Fleet*> friends = calculateSide(friendly, 6, tick);
116                 vector<Fleet*> hostiles = calculateSide(hostile, 3, tick);
117
118                 // No idea to calculate anything if noone is there.. ;)
119                 if (hostiles.size() == 0)
120                 {
121                         skipped++;
122                         continue;
123                 }
124                 else
125                         skipped = 0;
126
127                 Planet allFriends;
128                 allFriends.addToThis(friends, tick - 1);
129
130                 /*for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
131                         allFriends.setRoids(i->first, roids(i->first, tick));
132
133                 allFriends.setPlanetScore(m_iScore);*/
134                 
135                 Fleet allHostiles;
136                 allHostiles.addToThis(hostiles, tick - 1);
137
138                 map<string, map<string, int> > stealfriendly;
139                 map<string, map<string, int> > stealhostile;
140                 
141                 calcOneTick(&allFriends, &allHostiles, stealfriendly, stealhostile, tick );
142
143                 //allFriends.printFleet();
144
145                 allFriends.distributeLossesGains(friends, tick);
146                 allHostiles.distributeLossesGains(hostiles, tick);
147
148         /*      for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
149                         setRoids(i->first, roids(i->first, 1), tick);*/
150         }
151
152         for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
153         {
154                 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
155                         cout << i->first << " roids : " << (*j) << endl;
156         }
157 }
158
159 //////////////////////////////////////////////////////////////////////////
160 //
161 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)
162 {
163         map<int, string> unitsinit; // order units after their ininitiative
164         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
165                 unitsinit[i->second.initiative()] = i->first;
166
167         for (map<int, string>::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i)
168         {
169                 Fleet* hostiletemp = new Fleet(*hostile);
170                 Planet* friendlytemp = new Planet(*friendly);
171                 
172                 string unittype = i->second;
173
174                 cerr << "Initiative: " << s_Units[unittype].initiative() << " with unit: " << unittype << endl;
175
176                 if (s_Units[unittype].type() == "EMP")  
177                 {
178                         hostiletemp->takeEMP(unittype, friendly->freeFleet(unittype, 1));
179                         friendlytemp->takeEMP(unittype, hostile->freeFleet(unittype, 1));
180                 }
181                 else if (s_Units[unittype].type() == "Steal")
182                 {
183                         hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), stealfriendly[unittype]);
184                         friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), stealhostile[unittype]);
185                 }
186                 else
187                 {
188                         map<string, int> temp;
189                         hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), temp);
190                         friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), temp);
191                 }
192
193                 if (s_Units[unittype].type() == "Pod")
194                 {
195                         if (m_iScore > 0)
196                         {
197                                 float capping = float(m_iScore) / hostile->score() /  10;
198                         
199                                 cerr << "Capping is: " << capping << endl;
200         
201                                 if (capping > 0.15)
202                                         capping = 0.15;
203                                 if (capping > 0)
204                                 {
205                                         for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
206                                         {
207                                                 int caproids = capping * roids(roid->first, tick - 1);
208                                                 int freepods = hostiletemp->freeFleet(unittype, 1);
209                 
210                                                 cerr << "Possible to steal " << caproids << " " << roid->first << " roids\n";
211                                                 cerr << freepods << " free pods available\n";
212                                                 
213                                                 if (freepods <= 0)
214                                                         break;
215                                                 if (freepods < caproids)
216                                                         caproids = freepods;
217                 
218                                                 cerr << caproids << " roids stolen\n";
219                                                         
220                                                 takeRoids(roid->first, caproids, tick);
221                 
222                                                 //FIXME: Going to move this to the distribute roids section instead..
223                                                 hostiletemp->killFleet(unittype, caproids, 1);
224                                                 int totroids = caproids + hostiletemp->resource(roid->first, 0);
225                                                 hostiletemp->setResource(roid->first, totroids, 1);
226                                                 
227                                                 cerr << totroids << " stolen " << roid->first << " roids\n";
228                                         }
229                                 }
230                         }
231                 }
232
233                 //set the the objects so they point at the modified objects
234                 *friendly = *friendlytemp;
235                 delete friendlytemp;
236                 *hostile = *hostiletemp;
237                 delete hostiletemp;
238         }
239 }
240