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