]> ruin.nu Git - hbs.git/blob - bs/fleet.cpp
3ce2b71344e50574694a6280d54c6c52cd2a899a
[hbs.git] / bs / fleet.cpp
1 /***************************************************************************
2                           fleet.cpp  -  description
3                              -------------------
4     begin                : Tue Jan 22 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 "fleet.h"
19
20 #include <iostream>
21 using namespace std;
22
23 //Static variables
24 map<string, vector<int> > Fleet::s_Races;
25 UnitList Fleet::s_Units;
26
27 Fleet::Fleet()
28 {
29         m_iETA = 0;
30         m_sRace = "Cathaar";
31 }
32
33 Fleet::~Fleet(){
34 }
35
36 //////////////////////////////////////////////////////////////////////////
37 //
38 void Fleet::setName(string sName)
39 {
40         m_sName = sName;
41 }
42
43 //////////////////////////////////////////////////////////////////////////
44 //
45 string Fleet::name() const
46 {
47         return m_sName;
48 }
49
50 //////////////////////////////////////////////////////////////////////////
51 //
52 /** This function first sets the race, then it iterates through the the 
53  * s_Races and checks if it finds the race it returns true, if it reaches
54  * the end without finding it it returns false.
55  */
56 bool Fleet::setRace(string sRace)
57 {
58         m_sRace = sRace;
59         for (map<string, vector<int> >::iterator i = s_Races.begin(); i != s_Races.end(); i++)
60         {
61                 if (m_sRace == (*i).first)
62                         return true;
63         }
64         return false;
65 }
66
67 //////////////////////////////////////////////////////////////////////////
68 //
69 string Fleet::race() const
70 {
71         return m_sRace;
72 }
73
74 //////////////////////////////////////////////////////////////////////////
75 //
76 /** This function iterates through m_Fleet and adds all numbers together to
77  * produce a total.
78  */
79 int Fleet::numberOfShips() const
80 {
81         int total = 0;
82
83         for (map<string, vector<int> >::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
84         {
85                 if (i->second.size() != 0)
86                         total += i->second[0];
87         }
88
89         return total;
90 }
91
92 //////////////////////////////////////////////////////////////////////////
93 //
94 void Fleet::setETA(int eta)
95 {
96         m_iETA = eta;
97 }
98
99 //////////////////////////////////////////////////////////////////////////
100 //
101 int  Fleet::ETA() const
102 {
103         return m_iETA;
104 }
105
106 //////////////////////////////////////////////////////////////////////////
107 //
108 void Fleet::setRaces(map<string, vector<int> >& races)
109 {
110         s_Races = races;
111 }
112
113 //////////////////////////////////////////////////////////////////////////
114 //
115 void Fleet::setUnits(UnitList& units)
116 {
117         s_Units = units;
118
119         /*
120
121         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); i++)
122         {
123                 cerr << s_Units[(*i).first].Name() << "\t\t"
124                         << s_Units[(*i).first].race() <<"\t"
125                         << s_Units[(*i).first].unitClass() << "\t"
126                         << s_Units[(*i).first].target(0) << "\t"
127                         << s_Units[(*i).first].target(1) << "\t"
128                         << s_Units[(*i).first].target(2) << "\t"
129                         << s_Units[(*i).first].initiative() << "\t"
130                         << s_Units[(*i).first].agility() << "\t"
131                         << s_Units[(*i).first].weaponSpeed() << "\t"
132                         << s_Units[(*i).first].guns() << "\t"
133                         << s_Units[(*i).first].power() << "\t"
134                         << s_Units[(*i).first].armor() << "\t"
135                         << s_Units[(*i).first].EMP() << "\t"
136                         << s_Units[(*i).first].totRes() << "\t"
137                         << s_Units[(*i).first].fuel() << "\t"
138                         << s_Units[(*i).first].ETA() << "\t"
139                         << s_Units[(*i).first].type() << endl;
140         }
141         */
142 }
143
144 //////////////////////////////////////////////////////////////////////////
145 //
146 const map<string, vector<int> >& Fleet::Races()
147 {
148         return s_Races;
149 }
150
151 //////////////////////////////////////////////////////////////////////////
152 //
153 const UnitList& Fleet::Units()
154 {
155         return s_Units;
156 }
157
158 //////////////////////////////////////////////////////////////////////////
159 //
160 vector<int> Fleet::RacesAllowed() const
161 {
162         return s_Races[m_sRace];
163 }
164
165 //////////////////////////////////////////////////////////////////////////
166 //
167 unsigned Fleet::score(int tick = 0) const
168 {
169         unsigned tot_score = 0;
170
171         for (FleetList::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
172         {
173                   if (i->second.size() >= tick)
174                                 break;
175                         tot_score += i->second[tick] * s_Units[i->first].totRes() / 10;
176         }
177
178         return tot_score;
179 }
180
181 //////////////////////////////////////////////////////////////////////////
182 //
183 void Fleet::setFleet(string unittype, int number, int tick = 0)
184 {
185         if (m_Fleet[unittype].size() <= tick)
186         {
187                 m_Fleet[unittype].push_back(number);
188                 return;
189         }
190         m_Fleet[unittype][tick] = number;
191 }
192
193 //////////////////////////////////////////////////////////////////////////
194 //
195 int      Fleet::fleet(string unittype, int tick = 0)
196 {
197         int ticks = m_Fleet[unittype].size();
198         if (ticks == 0)
199                 return 0;
200
201         --ticks;
202
203         if (ticks < tick)
204                 m_Fleet[unittype][ticks];
205
206         return m_Fleet[unittype][tick];
207 }
208
209 //////////////////////////////////////////////////////////////////////////
210 //FIXME
211 void Fleet::addToThis(std::vector<Fleet*> fleets, int tick = 0)
212 {
213         for (UnitList::iterator i = s_Units.begin();  i != s_Units.end(); ++i)
214         {
215                 if (m_Fleet[i->first].size() == 0)
216                                 m_Fleet[i->first].push_back(0);
217
218                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
219                 {
220                 // FIXTHIS!!    m_Fleet[i->first][0] += j->fleet(i->first, tick);
221                 }
222         }
223 }
224
225 //////////////////////////////////////////////////////////////////////////
226 //
227 void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0)
228 {
229 }
230
231 //////////////////////////////////////////////////////////////////////////
232 //
233 std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int stays = 0, int tick = 0)
234 {
235         vector<Fleet*> fl;
236         for (vector<Fleet*>::iterator i = fleets.begin(); i != fleets.end(); ++i)
237         {
238                 if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < stays)
239                         fl.push_back((*i));
240                 else if ((*i)->name() == "Home Planet")
241                         fl.push_back((*i));
242         }
243         return fl;
244 }
245
246 //////////////////////////////////////////////////////////////////////////
247 //
248 int Fleet::freeFleet(std:: string unittype, int tick = 0)
249 {
250         if (m_Fleet[unittype].size() == 0)
251                 return 0;
252         if (m_BlockedFleet[unittype].size() < tick)
253                 return m_Fleet[unittype][tick];
254
255         return m_Fleet[unittype][tick] - m_BlockedFleet[unittype][tick];
256 }
257
258
259 //////////////////////////////////////////////////////////////////////////
260 //
261 void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
262 {
263 }
264
265 //////////////////////////////////////////////////////////////////////////
266 //
267 void Fleet::takeEMP(std::string unittype, int number)
268 {
269 }
270
271 //////////////////////////////////////////////////////////////////////////
272 //
273 void Fleet::killFleet(std::string unittype, int number, int tick = 0)
274 {
275         if (m_Fleet[unittype].size() <= tick)
276         {
277                 m_Fleet[unittype].push_back(m_Fleet[unittype][m_Fleet[unittype].size()] - number);
278                 return;
279         }
280         m_Fleet[unittype][tick] -= number;
281 }
282
283 //////////////////////////////////////////////////////////////////////////
284 //
285 void Fleet::setResource(std::string type, int number, int tick = 0)
286 {
287
288         if (m_Resources[type].size() <= tick)
289                 m_Resources[type].push_back(number);
290         m_Resources[type][tick] = number;
291 }
292
293 //////////////////////////////////////////////////////////////////////////
294 //
295 int Fleet::resource(std::string type, int tick = 0) const
296 {
297         vector<int>const* resource = 0;
298         for (ResourceList::const_iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
299         {
300                 if (i->first == type)
301                 {
302                         resource = &i->second;
303                         break;
304                 }
305         }
306         if (resource == 0)
307                 return 0;
308
309         int ticks = resource->size();
310
311         if( ticks == 0)
312                 return 0;
313
314         --ticks;
315
316         if (ticks < tick)
317                 return resource->at(ticks);
318         return resource->at(tick);
319 }
320