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, 2011, 2014 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 #pragma once
24 #ifndef CITY_H
25 #define CITY_H
26 
27 #include <list>
28 #include <vector>
29 #include "Location.h"
30 #include "Ownable.h"
31 #include "Renamable.h"
32 #include "prodslotlist.h"
33 
34 class Player;
35 class Stack;
36 class Army;
37 class ArmyProdBase;
38 
39 #define DEFAULT_CITY_NAME "Noname"
40 #define DEFAULT_CITY_INCOME 20
41 
42 //! A City on the game map.
43 /**
44  * Players vie for control of City objects on the game map.  The main goal
45  * of the game is to conquer these City objects.  Cities can be also be razed,
46  * making them uninhabitable and unconquerable.
47  *
48  * A city can produce armies, provide income, place produced armies on it's
49  * tiles and buy production as well as have it's name changed.  Cities can
50  * also vector their produced units to another position on the map.
51  *
52  * A City has 4 production slots.  A production slot is a production
53  * capability of a city.  Every one of the slots can be filled with an
54  * Army production base.  Cities can be assigned a random set of Army
55  * production bases.  The name of a City can also be randomly set from a
56  * list of potential City names.
57  *
58  * Some City objects are capital cities.  Every player has a single capital
59  * city.  Conquering another player's capital city doesn't give any bonus
60  * except for bragging rights.
61  */
62 class City : public Ownable, public Location, public Renamable,
63     public ProdSlotlist
64 {
65     public:
66 
67 	//! The xml tag of this object in a saved-game file.
68 	static Glib::ustring d_tag;
69 
70 	//! Default constructor.
71         /**
72           * Make a new city object.
73 	  *
74           * @param pos       The location of the city on the game map.
75 	  * @param width     The number of tiles this city spans.
76           * @param name      The name of the city.
77           * @param gold      The amount of gold the city produces each turn.
78 	  * @param numslots  The number of production slots for this city.
79           */
80         City(Vector<int> pos, guint32 width,
81 	     Glib::ustring name = DEFAULT_CITY_NAME,
82 	     guint32 gold = DEFAULT_CITY_INCOME,
83 	     guint32 numslots = MAX_PRODUCTION_SLOTS_IN_A_CITY);
84 
85 	//! Copy constructor.
86         City(const City&);
87 
88 	//! Alternative copy constructor.
89 	/**
90 	 * Make a city by copying another one, but it has a different position.
91 	 */
92         City(const City&, Vector<int> pos);
93 
94         //! Loading constructor.
95 	/**
96 	 * Make a new city object by reading it from a saved-game file.
97 	 *
98 	 * @param helper The opened saved-game file to load the City from.
99 	 */
100         City(XML_Helper* helper, guint32 width);
101 
102 	//! Destructor.
~City()103         ~City() {};
104 
105 	//Get Methods
106 
107         //! Return if the city can produce new production lines.
getBuildProduction()108         bool getBuildProduction() const {return d_build_production;}
109 
110         //! Return the defense level of the city.
111         int getDefenseLevel() const;
112 
113         //! Return the income of the city per turn.
getGold()114         guint32 getGold() const {return d_gold;}
115 
116         //! Returns whether or not the city has been destroyed.
isBurnt()117         bool isBurnt() const {return d_burnt;}
118 
119         //! Returns whether or not the city is a capital city.
isCapital()120         bool isCapital() const {return d_capital;}
121 
122         //! Returns the original owner of this capital city.
getCapitalOwner()123         Player *getCapitalOwner() const {return d_capital_owner;}
124 
125         //! Get the position where the city will send produced armies.
126 	/**
127 	 * @return The position on the map where the city will send it's newly
128 	 *         produced armies.  Returns (-1,-1) if the city is not
129 	 *         vectoring.
130 	 */
getVectoring()131         Vector<int> getVectoring() const {return d_vector;}
132 
isUnnamed()133 	bool isUnnamed() const {return getName() == getDefaultName() ? true : false;};
134 
135 
136 	//Set Methods
137 
138         //! Set whether or not the city can produce new production lines.
setBuildProduction(bool build)139         void setBuildProduction(bool build){d_build_production = build;}
140 
141         //! Set the gold the city produces each turn.
setGold(guint32 gold)142         void setGold(guint32 gold){d_gold = gold;}
143 
144         //! Set whether or not the city is destroyed.
setBurnt(bool burnt)145         void setBurnt(bool burnt){d_burnt = burnt;}
146 
147         //! Sets whether the city is a capital.
setCapital(bool capital)148         void setCapital(bool capital) {d_capital = capital;}
149 
150         //! Sets whether the city is a capital.
setCapitalOwner(Player * p)151         void setCapitalOwner(Player *p) {d_capital_owner = p;}
152 
153         //! Set the point where the city will send the produced armies.
154         /**
155 	 * @note This method does not check to see if the destination point
156 	 * can receive yet another vectored unit.
157 	 *
158 	 * @param pos  The position on the map to send the produced units to.
159 	 *             The position must point to another city, or a planted
160 	 *             standard.  Set to (-1,-1) if not vectoring.
161 	 */
162         void setVectoring(Vector<int> pos);
163 
164 
165 	// Methods that operate on class data and modify the class.
166 
167         //! Changes the owner of the city and prepares it for takeover.
168 	/**
169 	 * @param newowner  The pointer to the Player in Playerlist who is the
170 	 *                  new owner of this City.
171 	 */
172         void conquer(Player* newowner);
173 
174         //! Sets the production to random starting values
175 	/**
176 	 * @param produce_allies   Whether or not awardable Army units can
177 	 *                         be production bases in this city.
178 	 * @param likely  A value between 0 and 3 that represents how likely
179 	 *                more production bases are.
180 	 */
181 	void setRandomArmytypes(bool produce_allies, int likely);
182 
183         //! Produces one instance of the strongest Army the city can produce.
184         void produceStrongestProductionBase();
185 
186         //! Produces one instance of the weakest Army the city can produce.
187         void produceWeakestProductionBase();
188 
189         //! Produces a scout in the city.
190         void produceWeakestQuickestArmyInArmyset();
191 
192         //! Do everything neccessary for a new turn.
193 	/**
194 	 * Checks to see if an Army unit should be produced.
195 	 * Checks to see if a newly produced Army unit should be sent off to
196 	 * the city's vector destination.
197 	 */
198         void nextTurn();
199 
200 	//! Changes the vectoring destination, but for en-route units too.
201 	/**
202 	 * This method acts like City::setVectoring but also changes the
203 	 * destination of the units that this city has already vectored.
204 	 *
205 	 * @note This method does not check to see if the destination point
206 	 * can receive yet another vectored unit.
207 	 *
208 	 * @param dest  The new destination position on the game map to vector
209 	 *              newly produced Army units to.  The position must point
210 	 *              to a city or a planted standard.
211 	 *
212 	 * @return This method always returns true.
213 	 */
214 	bool changeVectorDestination(Vector<int> dest);
215 
216 	//! Callback that makes the army show up.
217 	const Army *armyArrives(Stack *& stack);
218 
219         //! Kill a percentage of the army units in the city.
220         std::list<Stack*> diseaseDefenders(double percent_to_kill);
221 
222         //! Change owner of city AND the stacks in it.
223         void persuadeDefenders(Player *new_owner);
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.
getDefaultName()266 	static Glib::ustring getDefaultName() {return _(DEFAULT_CITY_NAME);};
267 
268     private:
269 
270 	//! Callback for loading city objects from a saved-game file.
271 	bool load(Glib::ustring tag, XML_Helper *helper);
272 
273         //! Produces the currently active Army production base.
274         Army * produceArmy(Stack *& stack);
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         // DATA
294 
295 	//! The City gives the Player this much gold per turn.
296         guint32 d_gold;
297 
298 	//! The defense level of the city.
299 	/**
300 	 * This value is not taken into consideration for battles.
301 	 * It is just for show.
302 	 */
303         int d_defense_level;
304 
305 	//! Whether or not the city is destroyed or not.
306 	/**
307 	 * When City objects are razed they become uninhabitable and also
308 	 * unconquerable.  They can still be used as a jumping off point
309 	 * into water, but they will not produce any more Army units or
310 	 * provide income to any players.
311 	 */
312         bool d_burnt;
313 
314 	//! Whether or not the city is vectoring units.
315 	/**
316 	 * Vectoring involves sending units to a destination other than this
317 	 * city.  The destination can be any other city also owned by the
318 	 * owner of this city, or to the planted standard belonging to the
319 	 * owner of this city.  It always takes two turns to get to the
320 	 * vectoring destination.  See VectoredUnitlist for more information.
321 	 *
322 	 * If this value is True, then the newly produced Army units are
323 	 * vectored to a destination determined by City::d_vector.
324 	 */
325         bool d_vectoring;
326 
327 	//! Where to send newly produced Army units to.
328 	/**
329 	 * A position on the game map to send the Army units that this City
330 	 * produces.  When vectoring is disabled, this value is (-1, -1).
331 	 * The position on the map must coincide with a City owned by the
332 	 * owner of this city, or the planted standard of the owner of this
333 	 * city.
334 	 */
335         Vector<int> d_vector;
336 
337 	//! Whether or not this is a capital city.
338 	/**
339 	 * Capital cities do not have a purpose other than bragging rights
340 	 * among players during gameplay.  Every Player starts with exactly
341 	 * one capital city.
342 	 *
343 	 * Conquering a capital city does not change the original owner of
344 	 * the capital city.
345 	 *
346 	 * If this value is True, this city is a capital city of the
347 	 * City::d_capital_owner player.  When false, it is not a capital city.
348 	 */
349         bool d_capital;
350 
351 	//! The original owner of this capital city.
352 	Player *d_capital_owner;
353 
354         //! Whether or not we're allowed to build new production lines.
355         /**
356          * This refers to the "buy" button in the city window.
357          * If we're not allowed to build new production lines in the city,
358          * the buy button will be disabled.
359          *
360          */
361         bool d_build_production;
362 };
363 
364 bool armyCompareStrength (const ArmyProdBase *lhs, const ArmyProdBase *rhs);
365 #endif // CITY_H
366