]> ruin.nu Git - hbs.git/blob - bs/planet.cpp
41d92ac51eb2ed6004bab3b01c8009a46b8fdefb
[hbs.git] / bs / planet.cpp
1 /***************************************************************************
2                           planet.cpp  -  description
3                              -------------------
4     begin                : Wed Apr 3 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 "planet.h"
19
20 #include <iostream>
21 using namespace std;
22
23 Planet::Planet()
24 {
25         m_sRace = "Planet";
26         m_iScore = 0;
27 }
28
29 Planet::~Planet(){
30 }
31
32 //////////////////////////////////////////////////////////////////////////
33 //
34 unsigned Planet::planetScore() const
35 {
36         return m_iScore;
37 }
38
39 //////////////////////////////////////////////////////////////////////////
40 //
41 void Planet::setPlanetScore(unsigned i)
42 {
43         m_iScore = i;
44 }
45
46 //////////////////////////////////////////////////////////////////////////
47 //
48 int Planet::roids(std::string type, int tick = 0) const
49 {
50         // const... I would like [] as for const types: int ticks = m_Roids[type].size();
51         
52         vector<int>const* roids = 0;
53         for (RoidList::const_iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
54         {
55                 if (i->first == type)
56                 {
57                         roids = &i->second;
58                         break;
59                 }
60         }
61         if (roids == 0)
62                 return 0;
63
64         int ticks = roids->size();
65
66         if( ticks == 0)
67                 return 0;
68         if (ticks < tick)
69                 return roids->at(ticks);
70         return roids->at(tick);
71 }
72
73 //////////////////////////////////////////////////////////////////////////
74 //
75 void Planet::setRoids(std::string type, int number)
76 {
77         if (m_Roids[type].size() == 0)
78                 m_Roids[type].push_back(number);
79         m_Roids[type][0] = number;
80 }
81
82 //////////////////////////////////////////////////////////////////////////
83 //
84 void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile)
85 {
86         if (hostile.size() == 0)
87                         return;
88
89         int skipped = 0;
90         for(int tick = 1; skipped < 20; ++tick)
91         {
92                 //See who's in the battle at the current tick
93                 vector<Fleet*> friends = calculateSide(friendly, 6, tick);
94                 vector<Fleet*> hostiles = calculateSide(hostile, 3, tick);
95
96                 // No idea to calculate anything if noone is there.. ;)
97                 if (hostiles.size() == 0)
98                 {
99                         skipped++;
100                         continue;
101                 }
102                 else
103                         skipped = 0;
104
105                 Planet allFriends;
106                 allFriends.addToThis(friends);
107                 
108                 Fleet allHostiles;
109                 allHostiles.addToThis(hostiles);
110
111                 map<string, map<string, int> > stealfriendly;
112                 map<string, map<string, int> > stealhostile;
113                 
114                 calcOneTick(&allFriends, &allHostiles, stealfriendly, stealhostile );
115
116                 allFriends.distributeLossesGains(friends, tick);
117                 allHostiles.distributeLossesGains(hostiles, tick);
118         }
119 }
120
121 //////////////////////////////////////////////////////////////////////////
122 //
123 void Planet::calcOneTick(Planet* friendly, Fleet* hostile, std::map<std::string, std::map<std::string, int> >& stealfriendly, std::map<std::string, std::map<std::string, int> >&  stealhostile )
124 {
125         map<int, string> unitsinit; // order units after their ininitiative
126         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
127                 unitsinit[i->second.initiative()] = i->first;
128
129         for (map<int, string>::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i)
130         {
131                 Fleet* hostiletemp = new Fleet(*hostile);
132                 Planet* friendlytemp = new Planet(*friendly);
133                 
134                 string unittype = i->second;
135
136                 cerr << "Initiative: " << s_Units[unittype].initiative() << " with unit: " << unittype << endl;
137
138                 if (s_Units[unittype].type() == "EMP")  
139                 {
140                         hostiletemp->takeEMP(unittype, friendly->freeFleet(unittype, 1));
141                         friendlytemp->takeEMP(unittype, hostile->freeFleet(unittype, 1));
142                 }
143                 else if (s_Units[unittype].type() == "Steal")
144                 {
145                         hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), stealfriendly[unittype]);
146                         friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), stealhostile[unittype]);
147                 }
148                 else
149                 {
150                         map<string, int> temp;
151                         hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), temp);
152                         friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), temp);
153                 }
154
155                 if (s_Units[unittype].type() == "Pod")
156                 {
157                         float capping = friendly->m_iScore / hostile->score() /  10;
158       for (RoidList::iterator roids = m_Roids.begin(); roids != m_Roids.end(); ++roids)
159                         {
160                                 int caproids = capping * roids->second[0];
161                                 int freepods = hostiletemp->freeFleet(unittype, 1);
162                                 
163                                 if (freepods == 0)
164                                         break;
165                                 if (freepods < caproids)
166                                         caproids = caproids - freepods;
167                                         
168                                 roids->second.push_back(roids->second[0] - caproids);
169                                 hostiletemp->killFleet(unittype, caproids, 1);
170                         }
171                 }
172
173                 //set the the objects so they point at the modified objects
174                 *friendly = *friendlytemp;
175                 delete friendlytemp;
176                 *hostile = *hostiletemp;
177                 delete hostiletemp;
178         }
179 }
180