]> ruin.nu Git - hbs.git/blob - bs/fleet.cpp
fixed a bug with non shooting pds...
[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                         int num = (*j)->fleet(i->first, tick);
228                         m_Fleet[i->first][0] += num;
229                         if (num > 0)
230                                 cerr << (*j)->name() <<  " adding " << num << " units of type " << i->first << endl;
231                 }
232         }
233 }
234
235 //////////////////////////////////////////////////////////////////////////
236 //
237 void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0)
238 {
239         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
240         {
241                 string unittype = i->first;
242
243
244                 if (m_Fleet[unittype].size() < 1)
245                         continue;
246                 if (m_Fleet[unittype][0] == 0)
247                         continue;
248
249                  
250                 int totallost = fleet(unittype,1) - fleet(unittype, 0);
251
252
253                 cerr << "Distributing type: " << unittype << " with a total loss of " << totallost << " units" << endl;
254
255                 cerr << "Total number of units before: " << fleet(unittype, 0)  << " and after : " << fleet(unittype, 1) << endl;
256                 
257                 for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
258                 {
259                         int fl1 = (*j)->fleet(unittype, tick - 1);
260                         float part = float(fl1) / fleet(unittype, 0) ;
261                         int lost =  totallost * part;
262                         cerr << (*j)->name() << " gaining " << lost << " " << unittype  << " since it's " << part * 100 << "% of the whole fleet, and it had : " << fl1 << " units last tick.." <<  endl;
263                         (*j)->setFleet(unittype, (*j)->fleet(unittype, tick - 1) + lost, tick);
264                 }
265         }
266 }
267
268 //////////////////////////////////////////////////////////////////////////
269 //
270 std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int stays = 0, int tick = 0)
271 {
272         vector<Fleet*> fl;
273         for (vector<Fleet*>::iterator i = fleets.begin(); i != fleets.end(); ++i)
274         {
275                 if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < stays)
276                 {
277                         fl.push_back((*i));
278                         cerr << "Using fleet " << (*i)->name() << " for tick " << tick << endl;
279                 }
280                 else if ((*i)->name() == "Home Planet")
281                         fl.push_back((*i));
282         }
283         return fl;
284 }
285
286 //////////////////////////////////////////////////////////////////////////
287 //
288 int Fleet::freeFleet(std:: string unittype, int tick = 0)
289 {
290         int bticks = m_BlockedFleet[unittype].size();
291
292         --bticks;
293
294         if (bticks < tick)
295                 return fleet(unittype, tick);
296
297
298         int free = fleet(unittype,tick) - m_BlockedFleet[unittype][tick];
299         if (free < 0)
300                 return 0;
301         return free;
302 }
303
304
305 //////////////////////////////////////////////////////////////////////////
306 //
307 void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
308 {
309
310         float guns = s_Units[unittype].guns() * number;
311
312         
313         if (guns == 0)
314                 return;
315
316         cerr << number << " " << unittype << ": with " << guns << " guns\n";
317
318         float gunsleft = guns;
319         for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
320         {
321                 string ta = s_Units[unittype].target(count);
322         cerr << "Shooting at target class: " << ta << endl;
323                 while (gunsleft > 0)
324                 {
325         
326                         map<string, int*> targets;
327
328                         for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
329                         {
330
331                                 if (m_Fleet[j->first].size() == 0)
332                                         continue;
333
334                                 if (m_Fleet[j->first].size() == 1)
335                                         m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
336
337                                 //cerr << "Target is class: " << j->second.type() << endl;
338
339                                 if (m_Fleet[j->first][1] > 0  && ( ta == j->second.unitClass() || ta == "All"))
340                                 {
341
342                                 //      cerr << "Looking at target: " << j->first << endl;
343                                         targets[j->first] = &m_Fleet[j->first][1];
344                                 }
345
346                         }
347
348                         if (targets.size() == 0)
349                                 break;
350
351                         int total = 0;
352                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
353                                 total += (*j->second);
354
355                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
356                         {
357                                 float maxguns = float((*j->second))/total * guns;
358                                 //cerr << "Now shooting at target: " << j->first << endl;
359
360                                 if (m_Armor[j->first] <= 0 || m_Armor[j->first] > s_Units[j->first].armor())
361                                         m_Armor[j->first] = s_Units[j->first].armor();
362                                 double k = maxguns;
363
364                                 //cerr << "Targets agility: " << s_Units[j->first].agility() << endl;
365                                 //cerr << "Weaponspeed: " << s_Units[unittype].weaponSpeed() << endl;
366                                 while (k > 0)   
367                                 {
368
369                                         if (*(j->second) <= 0)
370                                                 break;
371
372                                         int wpsp = s_Units[unittype].weaponSpeed();
373                                         int agil = s_Units[j->first].agility();
374
375                                         k -= float(100)/(25 + wpsp - agil);
376                                         //cout << "Used " << blaha << " guns to hit with one shot.\n";
377                                         //cout << "WPSP: " << wpsp << "\nAgil: " << agil << endl;
378
379                                         m_Armor[j->first] -= s_Units[unittype].power();
380                                         if (m_Armor[j->first] <= 0)
381                                         {
382                                                 m_Armor[j->first] = s_Units[j->first].armor();
383                                                 (*j->second)--;
384                                                 hitunits[j->first]++;
385                                                 
386                                                 //There is a chance that we're hitting a blocked ship.
387                                                 if (m_BlockedFleet[j->first].size() >= 1)
388                                                 {
389                                                         int test = rand() % m_BlockedFleet[j->first][0];
390                                                         if (test == 1
391                                                                 && m_BlockedFleet[j->first][0] > 0)
392                                                         {
393                                                                 if (m_BlockedFleet[j->first].size() == 1)
394                                                                         m_BlockedFleet[j->first].push_back(m_BlockedFleet[j->first][0] - 1);
395                                                                 else if (m_BlockedFleet[j->first][1] > 0)
396                                                                         m_BlockedFleet[j->first][1]--;
397                                                         }
398                                                 }
399                                         }
400
401                                 }
402
403                                 cerr << hitunits[j->first] << " units of type: " << j->first << "killed\n";
404                                 if (k <= 0)
405                                         gunsleft -= maxguns;
406                                 else
407                                         gunsleft -= maxguns - k;
408                         }
409                 }
410         }
411 }
412
413 //////////////////////////////////////////////////////////////////////////
414 //
415 void Fleet::takeEMP(std::string unittype, int number)
416 {
417         int guns = s_Units[unittype].guns() * number;
418         if (guns == 0)
419                 return;
420
421         cerr << unittype << ": with " << guns << " guns\n";
422
423         float gunsleft = guns;
424         for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
425         {
426                 string ta = s_Units[unittype].target(count);
427         cerr << "Shooting at target class: " << ta << endl;
428                 while (gunsleft > 0)
429                 {
430         
431                         map<string, int*> targets;
432
433                         for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
434                         {
435                                 if (j->second.type() == "PDS")
436                                         continue;
437
438                                 if (m_Fleet[j->first].size() == 0)
439                                         continue;
440
441                                 if (m_Fleet[j->first].size() == 1)
442                                         m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
443
444                                 //cerr << "Target is class: " << j->second.type() << endl;
445
446                                 if (m_Fleet[j->first][1] > 0  && ( ta == j->second.unitClass() || ta == "All"))
447                                 {
448
449                                 //      cerr << "Looking at target: " << j->first << endl;
450                                         targets[j->first] = &m_Fleet[j->first][1];
451                                 }
452
453                         }
454
455                         if (targets.size() == 0)
456                                 break;
457
458                         int total = 0;
459                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
460                                 total += (*j->second);
461
462                         for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
463                         {
464                                 int maxguns = (*j->second)/total * guns;
465                                 cerr << "Now shooting at target: " << j->first << endl;
466
467                                 double k = maxguns;
468
469                                 int hits = 0;
470
471                                 while (k > 0)   
472                                 {
473
474                                         if (*(j->second) <= 0)
475                                                 break;
476
477                                         int eres = s_Units[j->first].EMP();
478
479                                         k -= float(100)/(100-eres);
480                                         hits++;
481                                         blockFleet(j->first, 1);
482                                 }
483
484                                 cerr << hits << " units of type: " << j->first << " blocked\n";
485                                 if (k <= 0)
486                                         gunsleft -= maxguns;
487                                 else
488                                         gunsleft -= maxguns - k;
489                         }
490                 }
491         }
492 }
493
494 //////////////////////////////////////////////////////////////////////////
495 //
496 void Fleet::killFleet(std::string unittype, int number, int tick = 0)
497 {
498         int earlier = 0;
499         int ticks = m_Fleet[unittype].size();
500
501         if (ticks != 0)
502                 earlier = m_Fleet[unittype][ticks - 1];
503
504         for (int i = ticks; i <= tick; ++i)
505         {
506                 m_Fleet[unittype].push_back(earlier);
507         }
508         m_Fleet[unittype][tick] -= number;
509 }
510
511 //////////////////////////////////////////////////////////////////////////
512 //
513 void Fleet::setResource(std::string type, int number, int tick = 0)
514 {
515
516         if (m_Resources[type].size() <= tick)
517                 m_Resources[type].push_back(number);
518         m_Resources[type][tick] = number;
519 }
520
521 //////////////////////////////////////////////////////////////////////////
522 //
523 int Fleet::resource(std::string type, int tick = 0) const
524 {
525         vector<int>const* resource = 0;
526         for (ResourceList::const_iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
527         {
528                 if (i->first == type)
529                 {
530                         resource = &i->second;
531                         break;
532                 }
533         }
534         if (resource == 0)
535                 return 0;
536
537         int ticks = resource->size();
538
539         if( ticks == 0)
540                 return 0;
541
542         --ticks;
543
544         if (ticks < tick)
545                 return resource->at(ticks);
546         return resource->at(tick);
547 }
548
549 //////////////////////////////////////////////////////////////////////////
550 //
551 void Fleet::printFleet()
552 {
553         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
554         {
555                 for (int tick = 0; tick < 5 ;++tick)
556                 {
557                         int num = fleet(i->first, tick);
558
559                         if (num <= 0)
560                                 break;
561                         cerr << num << " " << i->first << " during tick: " << tick << endl;
562                 }
563         }
564 }
565
566 //////////////////////////////////////////////////////////////////////////
567 //
568 void Fleet::blockFleet(std::string unittype, int number, int tick = 0)
569 {
570         if (m_BlockedFleet[unittype].size() >= 1)
571         {
572                 m_BlockedFleet[unittype][0] += number;
573                 if (m_BlockedFleet[unittype].size() > 1)
574                         m_BlockedFleet[unittype][1] += number;
575                 else
576                         m_BlockedFleet[unittype].push_back(m_BlockedFleet[unittype][0]);
577         }
578         else
579         {
580                 m_BlockedFleet[unittype].push_back(number);
581                 m_BlockedFleet[unittype].push_back(number);
582         }
583 }