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