1 // Copyright (C) 2000, 2001, 2002, 2003 Michael Bartl
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Ulf Lorenz
3 // Copyright (C) 2004 John Farrell
4 // Copyright (C) 2005, 2006 Andrea Paternesi
5 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Library General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "LocationList.h"
26 #include <sigc++/trackable.h>
32 //! A list of City objects on the game map.
34 * The citylist keeps track of the city objects located on the game map. It
35 * is implemented as a singleton because many classes use it for looking
38 class Citylist : public LocationList<City*>, public sigc::trackable
42 //! The xml tag of this object in a saved-game file.
43 static std::string d_tag;
45 // Methods that operate on class data and modify the class.
48 * This function loops through all cities belonging to player p and
49 * processes them. This method adds the income of each city to
50 * the player's treasury and produces a new armies from the City
51 * object's active Army production base if they are due to be created.
53 * @param p The player whose cities are processed.
55 //! Process all City objects for the next turn.
56 void nextTurn(Player* p);
58 //! Changes ownership of all cities owned by old owner, to a new owner.
59 void changeOwnership(Player *old_owner, Player *new_owner);
62 * Scan through all of the City objects who are vectoring to the given
63 * city, and stop the vectoring.
65 * @param city The city to search for vectoring to.
67 //! Stops vectoring from any city to the specified city.
68 void stopVectoringTo(City *city);
71 // Methods that operate on class data and do not modify the class.
74 * Scan through all of the city objects for a city that is vectoring
77 * @param target The city to check if any other cities are vectoring
80 * @return True if another city is vectoring to the given city.
83 //! Return whether or not any cities are vectoring to the given city.
84 bool isVectoringTarget(City *target) const;
86 //! Return the amount of money that the given player will pay next turn.
87 guint32 calculateUpcomingUpkeep(Player *p) const;
89 //! Return the number of cities vectoring to the given city.
90 guint32 countCitiesVectoringTo(const City *dest) const;
92 //! Save the list of City objects to an opened saved-game file.
93 bool save(XML_Helper* helper) const;
95 //! Count the number of functional cities that a given player owns.
96 int countCities(Player* p) const;
98 //! Count the total number of inhabitable cities.
99 int countCities() const;
102 * Scans the list of city objects for the first one that is owned by
105 * @param player The owner of the City to search for.
107 * @return A pointer to a City object owned by the given player, or
108 * NULL if the given player doesn't own any city objects.
110 //! Returns the first City object owned by the given player.
111 City* getFirstCity(Player* player) const;
113 //! Have each of the cities owned by the given player, pay gold pieces.
115 * This method increases the player's treasury.
117 void collectTaxes(Player* p) const;
119 //! Returns the closest city that is owned by an enemy player.
121 * Scans through all of the City objects in the list for the nearest
122 * one that is at war with the active player. Nearness is determined
123 * by counting the tiles between POS and the enemy city.
125 * @note This method will not return a razed city.
127 * @param pos The position on the game map to search for the nearest
128 * enemy-owned City object from.
130 * @return A pointer to the nearest enemy-owned City object, or NULL
131 * if there aren't any City objects owned by any enemies.
133 City* getNearestEnemyCity(const Vector<int>& pos) const;
135 City* getClosestEnemyCity(const Stack *stack) const;
137 //! Returns the closest city that isn't owned by the active player.
139 * Scans through all of the City objects in the list for the nearest
140 * one that owned by a Player who isn't the active player.
142 * @note This method will not return a razed city.
144 * @param pos The position on the game map to search for the nearest
145 * City object that isn't owned by the active player.
147 * @return A pointer to the nearest foreign-owned City object, or NULL
148 * if there aren't any City objects owned by any other Player.
150 City* getNearestForeignCity(const Vector<int>& pos) const;
152 City* getClosestForeignCity(const Stack *stack) const;
154 //! Return the closest city owned by the active player.
156 * Scans through all of the City objects in the list for the nearest
157 * one that owned by the active player from the given position.
159 * @note This method will not return a razed city.
161 * @param pos The position on the game map to search for the nearest
162 * City object that is owned by the active player.
164 * @return A pointer to the nearest City object that is owned by the
165 * active player, or returns NULL if the active player doesn't
166 * own any City objects.
168 City* getNearestFriendlyCity(const Vector<int>& pos) const;
170 City* getClosestFriendlyCity(const Stack *stack) const;
172 //! Find the closest city owned by the active player and isn't too far.
174 * Scans through all of the City objects in the list for the nearest
175 * one that owned by the active player and isn't farther away than the
176 * prescribed number of tiles.
178 * @note This method will not return a razed city.
180 * @param pos The position on the game map to search for the nearest
181 * City object that is owned by the active player.
182 * @param dist The number of tiles away that is deemed "too far".
184 * @return A pointer to the nearest City object that is owned by the
185 * active player, and is within the perscribed number of tiles.
186 * Returns NULL if the active player doesn't own any City
187 * objects within of the prescribed number of tiles.
189 City* getNearestFriendlyCity(const Vector<int>& pos, int dist) const;
191 //! Find the closest city to the given position.
193 * Scans through all of the City objects in the list for the nearest
194 * one that isn't razed.
196 * @param pos The position on the game map to search for the nearest
199 * @return A pointer to the nearest City object. Returns NULL if there
200 * are not any City objects in the list.
202 City* getNearestCity(const Vector<int>& pos) const;
204 City* getClosestCity(const Stack *stack) const;
206 //! Find the closest city that isn't too far away.
208 * Scans through all of the City objects in the list for the nearest
209 * one that isn't razed and isn't farther away than the presecribed
212 * @param pos The position on the game map to search for the nearest
214 * @param dist The number of tiles away that is deemed "too far".
216 * @return A pointer to the nearest city that is not razed and is
217 * within the prescribed number of tiles. Returns NULL if
218 * no city could be found.
220 City* getNearestCity(const Vector<int>& pos, int dist) const;
222 //! Find the nearest city that is not obscured by fog.
224 * Scan through all cities, searching for the closest one that is
225 * not covered by fog-of-war on a hidden map.
227 * @note This method will not return a city that has been razed.
229 * @param pos The position to find the nearest city from.
231 * @return A pointer to the nearest city that is not obscured by fog.
233 City* getNearestVisibleCity(const Vector<int>& pos) const;
235 //! Find the nearest city that is unobscured and is not too far away.
237 * Scan through all the cities, searching for the closest one that
238 * is not covered by fog-of-war on a hidden map, but is not farther
239 * away than a given distance.
241 * @note This method will not return a city that has been razed.
243 * @param pos The position to find the nearest city from.
244 * @param dist The number of tiles away that is deemed "too far".
246 * @return A pointer to the nearest city that is not obscured by fog
247 * and is within the prescribed number of tiles. Returns NULL
248 * if no city could be found.
250 City* getNearestVisibleCity(const Vector<int>& pos, int dist) const;
252 //! Return the closest city owned by the given player.
254 * Scans through all of the City objects in the list for the nearest
255 * one that owned by the given player to the given position.
257 * @note This method will not return a razed city.
259 * @param pos The position on the game map to search for the nearest
260 * City object that is owned by the given player.
261 * @param player The player who owns the City object this method is
264 * @return A pointer to the nearest City object that is owned by the
265 * given player, or returns NULL if the given player doesn't
266 * own any City objects.
268 City* getNearestCity(const Vector<int>& pos, Player *player) const;
270 City* getClosestCity(const Stack *stack, Player *p) const;
272 //! Return the closest city owned by the neutral player.
274 * Scans through all of the City objects in the list for the nearest
275 * one that owned by the neutral player to the given position.
277 * @note This method will not return a razed city.
279 * @param pos The position on the game map to search for the nearest
280 * City object that is owned by the neutral player.
281 * @param player The player who owns the City object this method is
284 * @return A pointer to the nearest City object that is owned by the
285 * neutral player, or returns NULL if the neutral player
286 * doesn't own any City objects.
288 City* getNearestNeutralCity(const Vector<int>& pos) const;
290 //! Find the nearest unobscured city that is owned by the active player.
292 * Scan through all cities, searching for the closest one that is
293 * not covered by fog-of-war on a hidden map and is owned by the
296 * @note This method will not return a city that has been razed.
298 * @param pos The position to find the nearest city from.
300 * @return A pointer to the nearest city that is not obscured by fog,
301 * and is owned by the active player.
303 City* getNearestVisibleFriendlyCity(const Vector<int>& pos) const;
305 //! Get the nearest unfogged city of active player's that isn't too far.
307 * Scan through all cities, searching for the closest one that is
308 * not covered by fog-of-war on a hidden map and is owned by the
309 * active player, and is within the prescribed number of tiles.
311 * @note This method will not return a city that has been razed.
313 * @param pos The position to find the nearest city from.
314 * @param dist The number of tiles away that is deemed "too far".
316 * @return A pointer to the nearest city that is not obscured by fog,
317 * and is owned by the active player and is within the
318 * prescribed number of tiles. Returns NULL if no city
319 * fitting those parameters could be found.
321 City* getNearestVisibleFriendlyCity(const Vector<int>& pos, int dist) const;
323 //! Return the list of cities vectoring to the given city.
325 * Scan through all of the city objects for cities that are vectoring
326 * to the given city. Add pointers to these cities to a list.
328 * @param target The city to check if any other cities are vectoring
331 * @return The list of pointers to city objects that are vectoring to
334 std::list<City*> getCitiesVectoringTo(City *target) const;
336 //! Return the list of cities vectoring to the given spot on the map.
337 std::list<City*> getCitiesVectoringTo(Vector<int> target) const;
339 //! Get the nearest city that is farther than a given number tiles.
341 * Scans through all of the City objects in the list for the nearest
342 * one that isn't razed and is farther away then the given number of
345 * @param pos The position on the game map to search for the nearest
347 * @param dist The distance that is deemed "too close".
349 * @return A pointer to the nearest City object that is farther away
350 * than the given distance. Returns NULL if there are no
351 * cities that are farther away than the given distance.
353 City* getNearestCityPast(const Vector<int>& pos, int dist) const;
356 //! Get the nearest city to POS that can be vectored to.
357 City* getNearestFriendlyVectorableCity(const Vector<int>& pos) const;
359 //! Get our nearest cities in order of distance from our capital.
360 std::list<City*> getNearestFriendlyCities(Player *player, Vector<int> pos = Vector<int>(-1,-1)) const;
362 //! Get the capital city of the given player.
363 City *getCapitalCity(Player *player) const;
367 //! Returns the singleton instance. Creates a new one if neccessary.
368 static Citylist* getInstance();
370 //! Loads the singleton instance from an opened saved-game file.
371 static Citylist* getInstance(XML_Helper* helper);
373 //! Deletes the singleton instance.
374 static void deleteInstance();
379 //! Default constructor.
382 //! Loading constructor.
384 * Make a new Citylist object by reading it from an opened saved-game
387 * @param helper The opened saved-game file to load the list of City
390 Citylist(XML_Helper* helper);
397 //! A callback for loading City objects into the list of cities.
398 bool load(std::string tag, XML_Helper* helper);
400 //! A static pointer for the singleton instance.
401 static Citylist* s_instance;