]> ruin.nu Git - hbs.git/blob - bs/planet.cpp
emp works now too..
[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, tick);
107                 
108                 Fleet allHostiles;
109                 allHostiles.addToThis(hostiles, tick);
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.printFleet();
117
118                 allFriends.distributeLossesGains(friends, tick);
119                 allHostiles.distributeLossesGains(hostiles, tick);
120         }
121 }
122
123 //////////////////////////////////////////////////////////////////////////
124 //
125 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 )
126 {
127         map<int, string> unitsinit; // order units after their ininitiative
128         for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
129                 unitsinit[i->second.initiative()] = i->first;
130
131         for (map<int, string>::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i)
132         {
133                 Fleet* hostiletemp = new Fleet(*hostile);
134                 Planet* friendlytemp = new Planet(*friendly);
135                 
136                 string unittype = i->second;
137
138                 //cerr << "Initiative: " << s_Units[unittype].initiative() << " with unit: " << unittype << endl;
139
140                 if (s_Units[unittype].type() == "EMP")  
141                 {
142                         hostiletemp->takeEMP(unittype, friendly->freeFleet(unittype, 1));
143                         friendlytemp->takeEMP(unittype, hostile->freeFleet(unittype, 1));
144                 }
145                 else if (s_Units[unittype].type() == "Steal")
146                 {
147                         hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), stealfriendly[unittype]);
148                         friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), stealhostile[unittype]);
149                 }
150                 else
151                 {
152                         map<string, int> temp;
153                         hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), temp);
154                         friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), temp);
155                 }
156
157                 if (s_Units[unittype].type() == "Pod")
158                 {
159                         float capping = friendly->m_iScore / hostile->score() /  10;
160       for (RoidList::iterator roids = m_Roids.begin(); roids != m_Roids.end(); ++roids)
161                         {
162                                 int caproids = capping * roids->second[0];
163                                 int freepods = hostiletemp->freeFleet(unittype, 1);
164                                 
165                                 if (freepods == 0)
166                                         break;
167                                 if (freepods < caproids)
168                                         caproids = caproids - freepods;
169                                         
170                                 roids->second.push_back(roids->second[0] - caproids);
171                                 hostiletemp->killFleet(unittype, caproids, 1);
172                         }
173                 }
174
175                 //set the the objects so they point at the modified objects
176                 *friendly = *friendlytemp;
177                 delete friendlytemp;
178                 *hostile = *hostiletemp;
179                 delete hostiletemp;
180         }
181 }
182