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