]> ruin.nu Git - hbs.git/blob - bs/fleet.cpp
f0fb8e1dee9d07d45c42c8d0784d65d6f8b5c907
[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         int earlier = 0;
187         int ticks = m_Fleet[unittype].size();
188
189         if (ticks != 0)
190                 earlier = m_Fleet[unittype][ticks - 1];
191
192         for (int i = ticks; i <= tick; ++i)
193         {
194                 m_Fleet[unittype].push_back(earlier);
195         }
196         m_Fleet[unittype][tick] = number;
197 }
198
199 //////////////////////////////////////////////////////////////////////////
200 //
201 int      Fleet::fleet(string unittype, int tick = 0)
202 {
203         int ticks = m_Fleet[unittype].size();
204         if (ticks == 0)
205                 return 0;
206
207         --ticks;
208
209         if (ticks < tick)
210                 return m_Fleet[unittype][ticks];
211
212         return m_Fleet[unittype][tick];
213 }
214
215 //////////////////////////////////////////////////////////////////////////
216 //FIXME
217 void Fleet::addToThis(std::vector<Fleet*> fleets, int tick = 0)
218 {
219         for (UnitList::iterator i = s_Units.begin();  i != s_Units.end(); ++i)
220         {
221                 if (m_Fleet[i->first].size() == 0)
222                                 m_Fleet[i->first].push_back(0);
223
224                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
225                 {
226                         m_Fleet[i->first][0] += (*j)->fleet(i->first, tick);
227                 }
228         }
229 }
230
231 //////////////////////////////////////////////////////////////////////////
232 //
233 void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0)
234 {
235         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
236         {
237                 string unittype = i->first;
238                 if (m_Fleet[unittype].size() < 1)
239                         break;
240     if (m_Fleet[unittype][0] == 0)
241                         break;
242
243                 int totallost = m_Fleet[unittype][1] - m_Fleet[unittype][0];
244                 
245                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
246                 {
247                         int lost =  totallost * ( (*j)->fleet(unittype, tick - 1) / m_Fleet[unittype][0] );
248                         (*j)->setFleet(unittype, (*j)->fleet(unittype, tick - 1) + lost, tick);
249                 }
250         }
251 }
252
253 //////////////////////////////////////////////////////////////////////////
254 //
255 std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int stays = 0, int tick = 0)
256 {
257         vector<Fleet*> fl;
258         for (vector<Fleet*>::iterator i = fleets.begin(); i != fleets.end(); ++i)
259         {
260                 if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < stays)
261                         fl.push_back((*i));
262                 else if ((*i)->name() == "Home Planet")
263                         fl.push_back((*i));
264         }
265         return fl;
266 }
267
268 //////////////////////////////////////////////////////////////////////////
269 //
270 int Fleet::freeFleet(std:: string unittype, int tick = 0)
271 {
272         int bticks = m_BlockedFleet[unittype].size();
273
274         --bticks;
275
276         if (bticks < tick)
277                 return fleet(unittype, tick);
278
279
280         int free = fleet(unittype,tick) - m_BlockedFleet[unittype][tick];
281         if (free < 0)
282                 return 0;
283         return free;
284 }
285
286
287 //////////////////////////////////////////////////////////////////////////
288 //
289 void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
290 {
291
292         cerr << unittype << ":  " << number << " in number\n";
293         int guns = s_Units[unittype].guns() * number;
294         if (guns == 0)
295                 return;
296
297         cerr << unittype << " with " << guns << " guns\n";
298
299         int gunsleft = guns;
300         for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
301         {
302                 string ta = s_Units[unittype].target(count);
303     cerr << "Shooting at target class: " << ta << endl;
304                 while (gunsleft > 0)
305                 {
306         
307                         map<string, int*> targets;
308
309                         for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
310                         {
311
312                                 if (m_Fleet[j->first].size() == 0)
313                                         break;
314
315                                 if (m_Fleet[j->first].size() == 1)
316                                         m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
317
318                                 //cerr << "Target is class: " << j->second.type() << endl;
319
320                                 if (m_Fleet[j->first][1] > 0  && ( ta == j->second.unitClass() || ta == "All"))
321                                 {
322
323                                         cerr << "Looking at target: " << j->first << endl;
324                                         targets[j->first] = &m_Fleet[j->first][1];
325                                 }
326
327                         }
328
329                         if (targets.size() == 0)
330                                 break;
331
332                         int total = 0;
333                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
334                                 total += (*j->second);
335
336                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
337                         {
338                                 int maxguns = (*j->second)/total * guns;
339                                 cerr << "Now shooting at target: " << j->first << endl;
340
341                                 if (m_Armor[j->first] <= 0 || m_Armor[j->first] > s_Units[j->first].armor())
342                                         m_Armor[j->first] = s_Units[j->first].armor();
343                                 float k = maxguns;
344
345                                 int blaha = 0;
346                                 cerr << "Targets agility: " << s_Units[j->first].agility() << endl;
347                                 cerr << "Weaponspeed: " << s_Units[unittype].weaponSpeed() << endl;
348                                 while (k > 0)   
349                                 {
350
351                                         if (*(j->second) <= 0)
352                                                 break;
353                                         k -= 100/(25+s_Units[unittype].weaponSpeed() - s_Units[j->first].agility());
354                                         m_Armor[j->first] -= s_Units[unittype].power();
355                                         if (m_Armor[j->first] <= 0)
356                                         {
357                                                 m_Armor[j->first] = s_Units[j->first].armor();
358                                                 (*j->second)--;
359                                                 hitunits[j->first]++;
360                                                 
361                                                 //There is a chance that we're hitting a blocked ship.
362                                                 if (m_BlockedFleet[j->first].size() >= 1)
363                                                 {
364                                                         int test = rand() % m_BlockedFleet[j->first][0];
365                                                         if (test == 1
366                                                                 && m_BlockedFleet[j->first][0] > 0)
367                                                         {
368                                                                 if (m_BlockedFleet[j->first].size() == 1)
369                                                                         m_BlockedFleet[j->first].push_back(m_BlockedFleet[j->first][0] - 1);
370                                                                 else if (m_BlockedFleet[j->first][1] > 0)
371                                                                         m_BlockedFleet[j->first][1]--;
372                                                         }
373                                                 }
374                                         }
375
376                                 }
377
378                                 cerr << hitunits[j->first] << " units of type: " << j->first << "killed\n";
379                                 if (k <= 0)
380                                         gunsleft -= maxguns;
381                                 else
382                                         gunsleft -= maxguns - k;
383                         }
384                 }
385         }
386 }
387
388 //////////////////////////////////////////////////////////////////////////
389 //
390 void Fleet::takeEMP(std::string unittype, int number)
391 {
392
393 }
394
395 //////////////////////////////////////////////////////////////////////////
396 //
397 void Fleet::killFleet(std::string unittype, int number, int tick = 0)
398 {
399         if (m_Fleet[unittype].size() <= tick)
400         {
401                 m_Fleet[unittype].push_back(m_Fleet[unittype][m_Fleet[unittype].size()] - number);
402                 return;
403         }
404         m_Fleet[unittype][tick] -= number;
405 }
406
407 //////////////////////////////////////////////////////////////////////////
408 //
409 void Fleet::setResource(std::string type, int number, int tick = 0)
410 {
411
412         if (m_Resources[type].size() <= tick)
413                 m_Resources[type].push_back(number);
414         m_Resources[type][tick] = number;
415 }
416
417 //////////////////////////////////////////////////////////////////////////
418 //
419 int Fleet::resource(std::string type, int tick = 0) const
420 {
421         vector<int>const* resource = 0;
422         for (ResourceList::const_iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
423         {
424                 if (i->first == type)
425                 {
426                         resource = &i->second;
427                         break;
428                 }
429         }
430         if (resource == 0)
431                 return 0;
432
433         int ticks = resource->size();
434
435         if( ticks == 0)
436                 return 0;
437
438         --ticks;
439
440         if (ticks < tick)
441                 return resource->at(ticks);
442         return resource->at(tick);
443 }
444