initial commit, lordsawar source, slightly modified
[lordsawar] / src / city.h
1 // Copyright (C) 2000, 2001, 2003 Michael Bartl
2 // Copyright (C) 2002 Mark L. Amidon
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Ulf Lorenz
4 // Copyright (C) 2005, 2006 Andrea Paternesi
5 // Copyright (C) 2006, 2007, 2008, 2009 Ben Asselstine
6 // Copyright (C) 2008 Ole Laursen
7 //
8 //  This program is free software; you can redistribute it and/or modify
9 //  it under the terms of the GNU General Public License as published by
10 //  the Free Software Foundation; either version 3 of the License, or
11 //  (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU Library General Public License for more details.
17 //
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
21 //  02110-1301, USA.
22
23 #ifndef CITY_H
24 #define CITY_H
25
26 #include <list>
27 #include <string>
28 #include <vector>
29 #include "Location.h"
30 #include "Ownable.h"
31 #include "Renamable.h"
32 #include "prodslot.h"
33 #include "defs.h"
34 #include "prodslotlist.h"
35
36 class Player;
37 class Stack;
38 class Army;
39 class ArmyProdBase;
40 class ArmyProto;
41 class Hero;
42
43 #define DEFAULT_CITY_NAME "Noname"
44 #define DEFAULT_CITY_INCOME 20
45
46 //! A City on the game map.
47 /**
48  * Players vie for control of City objects on the game map.  The main goal
49  * of the game is to conquer these City objects.  Cities can be also be razed,
50  * making them uninhabitable and unconquerable.
51  *
52  * A city can produce armies, provide income, place produced armies on it's 
53  * tiles and buy production as well as have it's name changed.  Cities can
54  * also vector their produced units to another position on the map.
55  *
56  * A City has 4 production slots.  A production slot is a production 
57  * capability of a city.  Every one of the slots can be filled with an 
58  * Army production base.  Cities can be assigned a random set of Army
59  * production bases.  The name of a City can also be randomly set from a 
60  * list of potential City names.
61  *
62  * Some City objects are capital cities.  Every player has a single capital
63  * city.  Conquering another player's capital city doesn't give any bonus
64  * except for bragging rights.
65  */
66 class City : public Ownable, public Location, public Renamable, 
67     public ProdSlotlist
68 {
69     public:
70
71         //! The xml tag of this object in a saved-game file.
72         static std::string d_tag; 
73
74         //! Default constructor.
75         /** 
76           * Make a new city object.
77           *
78           * @param pos       The location of the city on the game map.
79           * @param width     The number of tiles this city spans.
80           * @param name      The name of the city.
81           * @param gold      The amount of gold the city produces each turn.
82           * @param numslots  The number of production slots for this city.
83           */
84         City(Vector<int> pos, guint32 width, 
85              std::string name = DEFAULT_CITY_NAME, 
86              guint32 gold = DEFAULT_CITY_INCOME, 
87              guint32 numslots = MAX_PRODUCTION_SLOTS_IN_A_CITY);
88
89         //! Copy constructor.
90         City(const City&);
91
92         //! Alternative copy constructor.
93         /**
94          * Make a city by copying another one, but it has a different position.
95          */
96         City(const City&, Vector<int> pos);
97
98         //! Loading constructor.
99         /**
100          * Make a new city object by reading it from a saved-game file.
101          *
102          * @param helper The opened saved-game file to load the City from.
103          */
104         City(XML_Helper* helper, guint32 width);
105
106         //! Destructor.
107         ~City();
108
109         //Get Methods
110         
111         //! Return the defense level of the city.
112         int getDefenseLevel() const {return calculateDefenseLevel();}
113
114         //! Return the income of the city per turn.
115         guint32 getGold() const {return d_gold;}
116
117         //! Returns whether or not the city has been destroyed.
118         bool isBurnt() const {return d_burnt;}
119
120         //! Returns whether or not the city is a capital city.
121         bool isCapital() const {return d_capital;}
122
123         //! Returns the original owner of this capital city.
124         Player *getCapitalOwner() const {return d_capital_owner;}
125
126         //! Get the position where the city will send produced armies.
127         /**
128          * @return The position on the map where the city will send it's newly
129          *         produced armies.  Returns (-1,-1) if the city is not
130          *         vectoring.
131          */
132         Vector<int> getVectoring() const {return d_vector;}
133
134         bool isUnnamed() const {return getName() == getDefaultName() ? true : false;};
135
136
137         //Set Methods
138
139         //! Set the gold the city produces each turn.
140         void setGold(guint32 gold){d_gold = gold;}
141
142         //! Set whether or not the city is destroyed.
143         void setBurnt(bool burnt){d_burnt = burnt;}
144
145         //! Sets whether the city is a capital.
146         void setCapital(bool capital) {d_capital = capital;}
147
148         //! Sets whether the city is a capital.
149         void setCapitalOwner(Player *p) {d_capital_owner = p;}
150
151         //! Set the point where the city will send the produced armies.
152         /**
153          * @note This method does not check to see if the destination point
154          * can receive yet another vectored unit.
155          *
156          * @param pos  The position on the map to send the produced units to.
157          *             The position must point to another city, or a planted
158          *             standard.  Set to (-1,-1) if not vectoring.
159          */
160         void setVectoring(Vector<int> pos);
161
162
163         // Methods that operate on class data and modify the class.
164
165         //! Changes the owner of the city and prepares it for takeover.
166         /**
167          * @param newowner  The pointer to the Player in Playerlist who is the
168          *                  new owner of this City.
169          */
170         void conquer(Player* newowner);
171         
172         //! Sets the production to random starting values
173         /**
174          * @param produce_allies   Whether or not awardable Army units can
175          *                         be production bases in this city.
176          * @param likely  A value between 0 and 3 that represents how likely
177          *                more production bases are.
178          */
179         void setRandomArmytypes(bool produce_allies, int likely);
180
181         //! Produces one instance of the strongest Army the city can produce.
182         void produceStrongestProductionBase();
183
184         //! Produces one instance of the weakest Army the city can produce.
185         void produceWeakestProductionBase();
186
187         //! Produces a scout in the city.
188         /**
189          * A Scout is defined as the first Army in the Armyset.
190          * It is used when neutral cities are "average".  
191          * It is also used as a fallback when a city must have one Army unit.
192          * @note The scout army doesn't have to be an Army production base in
193          * the city for this method to operate.
194          */
195         void produceScout();
196
197         //! Do everything neccessary for a new turn.
198         /**
199          * Checks to see if an Army unit should be produced.
200          * Checks to see if a newly produced Army unit should be sent off to 
201          * the city's vector destination.
202          */
203         void nextTurn();
204
205         //! Changes the vectoring destination, but for en-route units too.
206         /**
207          * This method acts like City::setVectoring but also changes the
208          * destination of the units that this city has already vectored.
209          *
210          * @note This method does not check to see if the destination point
211          * can receive yet another vectored unit.
212          *
213          * @param dest  The new destination position on the game map to vector 
214          *              newly produced Army units to.  The position must point
215          *              to a city or a planted standard.
216          *
217          * @return This method always returns true.
218          */
219         bool changeVectorDestination(Vector<int> dest);
220
221         //! Callback that makes the army show up.
222         const Army *armyArrives(Vector<int> &pos);
223
224         
225         // Methods that operate on class data and do not modify the class.
226
227         //! Save the city to an opened saved-game file.
228         bool save(XML_Helper* helper) const;
229         
230         //! Returns true if the city isn't accepting too many vectored armies.
231         /**
232          * Scans all of the cities vectoring to this city.  If vectoring
233          * to this city would accrue the count to 
234          * MAX_CITIES_VECTORED_TO_ONE_CITY, this method returns false.
235          *
236          * @return True if the city can have another city vectoring to it.
237          *         Otherwise false.
238          */
239         bool canAcceptMoreVectoring() const;
240
241         //! Returns true if the city can accept vectoring from a set of cities.
242         /**
243          * Instead of checking to see if one unit can be vectored here like in 
244          * City::canAcceptMoreVectoring, this method checks if the city can
245          * receive multiple units (from an equal number of cities, because a
246          * city can only produce and vector one army at a time).
247          *
248          * @param number_of_cities The number of cities to check to
249          *                         see if this city can receive on top of what
250          *                         it's already receiving.
251          *
252          * @return True if the city can have this many more cities vectoring 
253          *         to it.  Otherwise false.
254          */
255         bool canAcceptMoreVectoring(guint32 number_of_cities) const;
256
257         //! Return how many armies are in the city.
258         guint32 countDefenders() const;
259
260         //! Return the stacks that are inside the city walls.
261         std::vector<Stack *> getDefenders() const;
262
263         // Static Methods
264         
265         //! Get the default name of any city.
266         static std::string getDefaultName() {return _(DEFAULT_CITY_NAME);};
267
268     private:
269
270         //! Callback for loading city objects from a saved-game file.
271         bool load(std::string tag, XML_Helper *helper);
272
273         //! Produces the currently active Army production base.
274         Army * produceArmy(Vector<int> &pos);
275
276         //! Makes an Army production base be a little different than expected.
277         /**
278          * This method is responsible for making Army production bases be
279          * a little bit different than the Army prototypes they derive from.
280          * This method will take a prototype (e.g. scouts), and maybe give it
281          * a strength of 2 (rather than 1), or a time of 2 (rather than 1).
282          * It also works the other way.  Elephants can be altered to take 5 
283          * turns instead of 4 turns, or have their strength decreased to 7.
284          */
285         void randomlyImproveOrDegradeArmy(ArmyProdBase *army);
286
287         //! Sort the Army production bases that this city produces by strength.
288         /**
289          * @note Pnly use this prior to the start of game.
290          */
291         void sortProduction();
292
293         //! Calculate defense level.
294         /**
295          * The defense level is a function of how many production slots
296          * the city has.
297          */
298         guint32 calculateDefenseLevel() const;
299
300         // DATA
301
302         //! The City gives the Player this much gold per turn.
303         guint32 d_gold;
304
305         //! The defense level of the city.
306         /**
307          * This value is not taken into consideration for battles.
308          * It is just for show.
309          */
310         int d_defense_level;
311         
312         //! Whether or not the city is destroyed or not.
313         /**
314          * When City objects are razed they become uninhabitable and also 
315          * unconquerable.  They can still be used as a jumping off point
316          * into water, but they will not produce any more Army units or
317          * provide income to any players.
318          */
319         bool d_burnt;
320
321         //! Whether or not the city is vectoring units.
322         /**
323          * Vectoring involves sending units to a destination other than this
324          * city.  The destination can be any other city also owned by the
325          * owner of this city, or to the planted standard belonging to the
326          * owner of this city.  It always takes two turns to get to the
327          * vectoring destination.  See VectoredUnitlist for more information.
328          *
329          * If this value is True, then the newly produced Army units are
330          * vectored to a destination determined by City::d_vector.
331          */
332         bool d_vectoring;
333
334         //! Where to send newly produced Army units to.
335         /**
336          * A position on the game map to send the Army units that this City
337          * produces.  When vectoring is disabled, this value is (-1, -1).
338          * The position on the map must coincide with a City owned by the
339          * owner of this city, or the planted standard of the owner of this
340          * city.
341          */
342         Vector<int> d_vector;
343
344         //! Whether or not this is a capital city.
345         /**
346          * Capital cities do not have a purpose other than bragging rights
347          * among players during gameplay.  Every Player starts with exactly
348          * one capital city.
349          *
350          * Conquering a capital city does not change the original owner of
351          * the capital city.
352          *
353          * If this value is True, this city is a capital city of the
354          * City::d_capital_owner player.  When false, it is not a capital city.
355          */
356         bool d_capital;
357
358         //! The original owner of this capital city.
359         Player *d_capital_owner;
360 };
361
362 #endif // CITY_H