]> ruin.nu Git - hbs.git/blob - bs/planet.cpp
0e4de71300db0651919b5b36549e5537dbf61dba
[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                 allHostiles.distributeCappedRoids(hostiles, tick);
148
149         /*      for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
150                         setRoids(i->first, roids(i->first, 1), tick);*/
151         }
152
153         for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
154         {
155                 for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
156                         cout << i->first << " roids : " << (*j) << endl;
157         }
158 }
159
160 //////////////////////////////////////////////////////////////////////////
161 //
162 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)
163 {
164         map<int, string> unitsinit; // order units after their ininitiative
165         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
166                 unitsinit[i->second.initiative()] = i->first;
167
168         for (map<int, string>::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i)
169         {
170                 Fleet* hostiletemp = new Fleet(*hostile);
171                 Planet* friendlytemp = new Planet(*friendly);
172                 
173                 string unittype = i->second;
174
175                 cerr << "Initiative: " << s_Units[unittype].initiative() << " with unit: " << unittype << endl;
176
177                 if (s_Units[unittype].type() == "EMP")  
178                 {
179                         hostiletemp->takeEMP(unittype, friendly->freeFleet(unittype, 1));
180                         friendlytemp->takeEMP(unittype, hostile->freeFleet(unittype, 1));
181                 }
182                 else if (s_Units[unittype].type() == "Steal")
183                 {
184                         hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), stealfriendly[unittype]);
185                         friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), stealhostile[unittype]);
186                 }
187                 else
188                 {
189                         map<string, int> temp;
190                         hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), temp);
191                         friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), temp);
192                 }
193
194                 if (s_Units[unittype].type() == "Pod")
195                 {
196                         if (m_iScore > 0)
197                         {
198                                 float capping = float(m_iScore) / hostile->score() /  10;
199                         
200                                 cerr << "Capping is: " << capping << endl;
201         
202                                 if (capping > 0.15)
203                                         capping = 0.15;
204                                 if (capping > 0)
205                                 {
206                                         for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
207                                         {
208                                                 int caproids = capping * roids(roid->first, tick - 1);
209                                                 int freepods = hostiletemp->freeFleet(unittype, 1);
210                 
211                                                 cerr << "Possible to steal " << caproids << " " << roid->first << " roids\n";
212                                                 cerr << freepods << " free pods available\n";
213                                                 
214                                                 if (freepods <= 0)
215                                                         break;
216                                                 if (freepods < caproids)
217                                                         caproids = freepods;
218                 
219                                                 cerr << caproids << " roids stolen\n";
220                                                         
221                                                 takeRoids(roid->first, caproids, tick);
222                 
223                                                 //FIXME: Going to move this to the distribute roids section instead..
224                                                 hostiletemp->killFleet(unittype, caproids, 1);
225                                                 //int totroids = caproids + hostiletemp->resource(roid->first, 0);
226                                                 hostiletemp->addResource(roid->first, caproids, 1);
227                                                 
228                                                 cerr << caproids << " stolen " << roid->first << " roids\n";
229                                         }
230                                 }
231                         }
232                 }
233
234                 //set the the objects so they point at the modified objects
235                 *friendly = *friendlytemp;
236                 delete friendlytemp;
237                 *hostile = *hostiletemp;
238                 delete hostiletemp;
239         }
240 }
241