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
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.
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.
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
31 #include "Renamable.h"
34 #include "prodslotlist.h"
43 #define DEFAULT_CITY_NAME "Noname"
44 #define DEFAULT_CITY_INCOME 20
46 //! A City on the game map.
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.
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.
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.
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.
66 class City : public Ownable, public Location, public Renamable,
71 //! The xml tag of this object in a saved-game file.
72 static std::string d_tag;
74 //! Default constructor.
76 * Make a new city object.
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.
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);
92 //! Alternative copy constructor.
94 * Make a city by copying another one, but it has a different position.
96 City(const City&, Vector<int> pos);
98 //! Loading constructor.
100 * Make a new city object by reading it from a saved-game file.
102 * @param helper The opened saved-game file to load the City from.
104 City(XML_Helper* helper, guint32 width);
111 //! Return the defense level of the city.
112 int getDefenseLevel() const {return calculateDefenseLevel();}
114 //! Return the income of the city per turn.
115 guint32 getGold() const {return d_gold;}
117 //! Returns whether or not the city has been destroyed.
118 bool isBurnt() const {return d_burnt;}
120 //! Returns whether or not the city is a capital city.
121 bool isCapital() const {return d_capital;}
123 //! Returns the original owner of this capital city.
124 Player *getCapitalOwner() const {return d_capital_owner;}
126 //! Get the position where the city will send produced armies.
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
132 Vector<int> getVectoring() const {return d_vector;}
134 bool isUnnamed() const {return getName() == getDefaultName() ? true : false;};
139 //! Set the gold the city produces each turn.
140 void setGold(guint32 gold){d_gold = gold;}
142 //! Set whether or not the city is destroyed.
143 void setBurnt(bool burnt){d_burnt = burnt;}
145 //! Sets whether the city is a capital.
146 void setCapital(bool capital) {d_capital = capital;}
148 //! Sets whether the city is a capital.
149 void setCapitalOwner(Player *p) {d_capital_owner = p;}
151 //! Set the point where the city will send the produced armies.
153 * @note This method does not check to see if the destination point
154 * can receive yet another vectored unit.
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.
160 void setVectoring(Vector<int> pos);
163 // Methods that operate on class data and modify the class.
165 //! Changes the owner of the city and prepares it for takeover.
167 * @param newowner The pointer to the Player in Playerlist who is the
168 * new owner of this City.
170 void conquer(Player* newowner);
172 //! Sets the production to random starting values
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.
179 void setRandomArmytypes(bool produce_allies, int likely);
181 //! Produces one instance of the strongest Army the city can produce.
182 void produceStrongestProductionBase();
184 //! Produces one instance of the weakest Army the city can produce.
185 void produceWeakestProductionBase();
187 //! Produces a scout in the city.
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.
197 //! Do everything neccessary for a new turn.
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.
205 //! Changes the vectoring destination, but for en-route units too.
207 * This method acts like City::setVectoring but also changes the
208 * destination of the units that this city has already vectored.
210 * @note This method does not check to see if the destination point
211 * can receive yet another vectored unit.
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.
217 * @return This method always returns true.
219 bool changeVectorDestination(Vector<int> dest);
221 //! Callback that makes the army show up.
222 const Army *armyArrives(Vector<int> &pos);
225 // Methods that operate on class data and do not modify the class.
227 //! Save the city to an opened saved-game file.
228 bool save(XML_Helper* helper) const;
230 //! Returns true if the city isn't accepting too many vectored armies.
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.
236 * @return True if the city can have another city vectoring to it.
239 bool canAcceptMoreVectoring() const;
241 //! Returns true if the city can accept vectoring from a set of cities.
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).
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.
252 * @return True if the city can have this many more cities vectoring
253 * to it. Otherwise false.
255 bool canAcceptMoreVectoring(guint32 number_of_cities) const;
257 //! Return how many armies are in the city.
258 guint32 countDefenders() const;
260 //! Return the stacks that are inside the city walls.
261 std::vector<Stack *> getDefenders() const;
265 //! Get the default name of any city.
266 static std::string getDefaultName() {return _(DEFAULT_CITY_NAME);};
270 //! Callback for loading city objects from a saved-game file.
271 bool load(std::string tag, XML_Helper *helper);
273 //! Produces the currently active Army production base.
274 Army * produceArmy(Vector<int> &pos);
276 //! Makes an Army production base be a little different than expected.
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.
285 void randomlyImproveOrDegradeArmy(ArmyProdBase *army);
287 //! Sort the Army production bases that this city produces by strength.
289 * @note Pnly use this prior to the start of game.
291 void sortProduction();
293 //! Calculate defense level.
295 * The defense level is a function of how many production slots
298 guint32 calculateDefenseLevel() const;
302 //! The City gives the Player this much gold per turn.
305 //! The defense level of the city.
307 * This value is not taken into consideration for battles.
308 * It is just for show.
312 //! Whether or not the city is destroyed or not.
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.
321 //! Whether or not the city is vectoring units.
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.
329 * If this value is True, then the newly produced Army units are
330 * vectored to a destination determined by City::d_vector.
334 //! Where to send newly produced Army units to.
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
342 Vector<int> d_vector;
344 //! Whether or not this is a capital city.
346 * Capital cities do not have a purpose other than bragging rights
347 * among players during gameplay. Every Player starts with exactly
350 * Conquering a capital city does not change the original owner of
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.
358 //! The original owner of this capital city.
359 Player *d_capital_owner;