]> ruin.nu Git - hbs.git/blob - bs/fleet.h
added code so you lose ships if you steal..
[hbs.git] / bs / fleet.h
1 /***************************************************************************
2                           fleet.h  -  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 #ifndef FLEET_H
19 #define FLEET_H
20
21 #include <map>
22 #include <ext/hash_map>
23 #include <string>
24 #include <vector>
25
26 #include "unittype.h"
27  template<typename T> class MyComp { public: bool operator()(T,T) { return false; } };
28 typedef std::map<std::string, std::vector<int> > FleetList;
29 typedef std::map<std::string, UnitType> UnitList;
30 typedef std::map<std::string, std::vector<int> > RaceList;
31 typedef std::map<std::string, std::vector<int> > ResourceList;
32 typedef std::map<std::string, int> ArmorList;
33 //! An abstraction of a fleet and the engine for the battle simulation.
34 /**This class and it's derivates is  the engine for the whole battlesystem.
35   *     One of the few parts that I plan make fully portable.
36   *\todo LOTS (I think ;)
37   *@author Michael Andreen
38   */
39 class Fleet 
40 {
41 public: 
42         Fleet();
43         virtual ~Fleet();
44
45         /**Sets the name that represents this fleet. Might be different a name
46          * like foobar or some coordinates like 1:1:1. The name doesn't have to be
47          * unique, since it's up to the rest of the program to handle that part.
48          * \see Name
49          */
50         void   setName(std::string sName);
51
52         /**Returns the name of this fleet.
53          * \see setName
54          */
55         std::string name() const;
56
57         /**The race string decides what type of ships this fleet can have.
58          * The values must be feeded into this class.
59          * \param sRace This is just the name of the race. It's case-sensitive.
60          * \return If the race is available this function returns true, if not false
61          * is returned. The race is set in both cases though.
62          * \see Race
63          */
64         bool   setRace(std::string sRace);
65
66         /**Just returns what race this fleet belongs to.. 
67          * \return The race, represented as a string.
68          * \see setRace
69          */
70         std::string race() const;
71
72         /**Returns the ship races allowed for this fleet.. This is not a hard limit
73          * it's more a indication to the interface what should be shown or not, the
74          * class can still handle all the different shiptypes.
75          * \return a vector of ints that can be used to compare with Unittype::race()
76          * \see Unittype::race()
77          */
78         std::vector<int> RacesAllowed() const;
79
80         /**Returns the total number of ships in this fleet
81          */
82         int numberOfShips() const;
83
84         /**Sets the estimated time of arrival. The time as a single integer,
85          * in relation to the current time. For example if the current time is
86          * 10, and the arrival is at 12, then the eta is 2.
87          */
88         void setETA(int eta);
89
90         /**Return the estimated time of arrival. It's counted from the current time (tick).
91          */
92         int  ETA() const;
93
94
95         /**Returns the score. This value is the total resources spent on this fleet
96          * devided with 10.
97          * \param tick tells the function what tick you want the score from. 0 is 
98          * initial score before the fleet has landed.
99          */
100         unsigned score(int tick = 0) const;
101
102         /**This function is used to set the number of units for a specific tic.
103          * \param untitype the unittype's name, as a std string
104          * \param number the number of ships
105          * \param tick what tick to set for. Tick 0 (before battle) is standard, no
106          * other is hardly used other than inside the engine.
107          */
108         void setFleet(std::string unittype, int number, int tick = 0);
109
110         void addFleet(std::string unittype, int number, int tick = 0);
111
112         void addFleet(std::map<std::string, int> units, int tick = 0);
113
114         /**Returns the numbers of units fo a specific type at a specific tick.
115          * \param unittype The shipname, as a std string
116          * \param tick The tick you want to look at, tick 0 (before battle) is default
117          * \return returns the number of units that params specifies.
118          */
119         int      fleet(std::string unittype, int tick = 0);
120
121         /**Returns the number of free ships, that is: ships that aren't dead and not
122          * blocked.
123          * \param unittype The shipname, as a std string
124          * \param tick The tick you want to look at, tick 0 (before battle) is default
125          * \return returns the number of units that params specifies.
126          */
127         int freeFleet(std:: string unittype, int tick = 0);
128
129         /** This function is used to see how many ships of a specific type got
130          * blocked at a specific tick.
131          * \param unittype the name of the unittype
132          * \param tick what tick you want to look at
133          * \return the number of ships that got blocked at the specified tick.
134          */
135         int blockedFleet(std::string unittype, int tick = 0);
136         
137         void setBlockedFleet(std::string unittype, int number, int tick = 0);
138
139         /** This function takes the vector with fleets and adds all their units of the
140          * specific tick to the current fleet's first tick. Main usage is for battle
141          * calculations.
142          * \param fleets a vector with fleet pointers
143          * \param tick an int to specify what tick to use. tick 0 (before battle)
144          * is default
145          * \todo add another tick parameter to specify what tick to use in the 
146          * current fleet.
147          */
148         void addToThis(std::vector<Fleet*> fleets, int tick = 0);
149
150         /** This function takes a vector with fleets and distribute all ship losses
151          * between tick 0 and 1 in the current fleet and adds that to the specified
152          * tick in each fleet.
153          * \param fleets a vector with fleet pointers that holds the fleets that's
154          * going to share the losses.
155          * \param tick the tick to add the losses to for the fleets in the vector.
156          * \todo add another tick parameter to specify what tick to use in the 
157          * current fleet.
158          */
159         void distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0);
160
161         /** This functions takes the captured roids between tick 0 and 1 in the
162          * current fleet and divides them between the fleets given as param.
163          * \param fleets a vector with Fleet pointers, points to the fleets that
164          * should get parts of the roids.
165          * \param tick the tick to use in the fleets given as argument, when the
166          * roids are handed out.
167          * \todo add another tick parameter to specify what tick to use in the 
168          * current fleet.
169          */
170         void distributeCappedRoids(std::vector<Fleet*> fleets, int tick = 0);
171
172         /** Checks through the vector with fleets to see who's in time for the
173          * specified tick and staying long enough so they're not too early.
174          * \param fleets a vector with Fleet pointers to use for the calculations
175          * \param stays the number of ticks the fleets stays, will be obsolee when 
176          * I get the time.
177          * \param tick What the current tick is to use for the calculations
178          * \return returns a vector with the fleet that are in time for the battle 
179          * and are staying long enough.
180          * \todo add the stays part as a integrated part of each fleet.
181          */
182         std::vector<Fleet*> calculateSide(std::vector<Fleet*> fleets, int stays = 0, int tick = 0);
183
184         /** Kill some ships of the specified type in the current fleet.
185          * \param unittype the name of the unit to kill
186          * \param number how many ships to kill
187          * \param tick what tick they where killed.
188          */
189         void killFleet(std::string unittype, int number, int tick = 0);
190
191         /**Block some ships of the specified type in the current fleet.
192          * \param unittype the name of the unit to block
193          * \param number how many ships to block
194          * \param tick what tick they where blocked.
195          */
196         void blockFleet(std::string unittype, int number, int tick = 0);
197
198         /** Set the resources of the specified type for the tick.
199          *
200          * \param type The name of the resource (in pa: metal,
201          * crystal and eonium (+ uninit for roids).
202          * \param number just simply the number of the specified resource.
203          * \param tick what tick to set
204          * \sa resource
205          */
206         void setResource(std::string type, int number, int tick = 0);
207
208         /** Adds a number ot the specified resource type.
209          * \param type The name of the resource (in pa: metal,
210          * crystal and eonium (+ uninit for roids).
211          * \param number just simply the number of the specified resource.
212          * \param tick what tick to set
213          * \sa resource
214          */
215         void addResource(std::string type, int number, int tick = 0);
216
217         /** Looks up the how much of a specific resource type the current fleet has.
218          *
219          * Resources is a kinda vague word, in general it's used as roid capping
220          * for hostile fleet and as salvage for friendly.
221          * \param type The name of the resource (in pa: metal,
222          * crystal and eonium (+ uninit for roids).
223          * \param tick what tick to set
224          * \returns simply returns the number of the specific resource this fleet
225          * has.
226          */
227         int resource(std::string type, int tick = 0)const;
228         
229         void resetResources();
230
231         /** This function tells how many ticks the fleet stays at it's target.
232          * \returns the number of ticks as an int.
233          */
234         int stays() const;
235         void setStays(int ticks);
236
237         /** This is a little more advanced function. It makes a a number of units
238          * of a specific unittype shoot at the current fleet and calculates the 
239          * losses.
240          * 
241          * Atm all calculations are done for tick 0/1
242          * \param unittype The name of the unit that shots at the current fleet.
243          * \param number The number of the specific unittype that shoot.
244          * \param hitunits a map to count the number of killed units, just if the
245          * rest of the program needs it.
246          * \todo add a parameter to specify what tick to calc for and move out as
247          * much of the code as possible because it's kinda crowded atm and lots of
248          * very similar stuff in both the take* functions.
249          * \sa takeEMP
250          */
251         void takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits);
252
253         /**This is a little more advanced function. It makes a a number of units
254          * of a specific unittype shoot at the current fleet and calculates how
255          * many got blocked..
256          * 
257          * Atm all calculations are done for tick 0/1
258          * \param unittype The name of the unit that shots at the current fleet.
259          * \param number The number of the specific unittype that shoot.
260          * \todo add a parameter to specify what tick to calc for and move out as
261          * much of the code as possible because it's kinda crowded atm and lots of
262          * very similar stuff in both the take* functions.
263          * \sa takeShoot
264          */
265         void takeEMP(std::string unittype, int number);
266
267         void calculateLostStealships(std::string unittype, std::map<std::string, int> stolen, int tick = 1);
268
269         /** This function prints out all the ships in the fleet to stderr,
270          * mainly used for debugging.
271          */
272         void printFleet();
273
274         //static functions
275         //
276         /** This function is used to set the different races used.
277          * \param races a Simple RaceList which holds all the info
278          */
279         static void setRaces(RaceList& races);
280
281         /** This function is used to set all the units used in the game.
282          * \param units This is a simple UnitList holding all info needed.
283          */
284         static void setUnits(UnitList& units);
285
286         /** This function is used to get the list of races used.
287          * \returns it returns a simple RaceList just like the one that set it.
288          */
289         static const RaceList& Races();
290
291         /** This function is used to get the list of all the units in the game
292          * \returns a simple UnitList holding everything.
293          */
294         static const UnitList& Units();
295
296
297 protected:
298
299
300         std::string  m_sName;
301         std::string      m_sRace;
302         int          m_iETA;
303         FleetList    m_Fleet;
304         FleetList    m_BlockedFleet;
305         ResourceList m_Resources;
306         ArmorList        m_Armor;
307         int          m_iStays;
308         
309
310
311         static UnitList s_Units;
312         static RaceList s_Races;
313 };
314
315 #endif