initial commit, lordsawar source, slightly modified
[lordsawar] / src / citylist.h
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
6 //
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.
11 //
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.
16 //
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 
20 //  02110-1301, USA.
21
22 #ifndef CITYLIST_H
23 #define CITYLIST_H
24
25 #include "LocationList.h"
26 #include <sigc++/trackable.h>
27
28 class City;
29 class Player;
30 class XML_Helper;
31
32 //! A list of City objects on the game map.
33 /**
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 
36  * up cities.
37  */
38 class Citylist : public LocationList<City*>, public sigc::trackable
39 {
40     public:
41
42         //! The xml tag of this object in a saved-game file.
43         static std::string d_tag; 
44
45         // Methods that operate on class data and modify the class.
46
47         /**
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.
52          *
53          * @param p        The player whose cities are processed.
54          */
55         //! Process all City objects for the next turn.
56         void nextTurn(Player* p);
57
58         //! Changes ownership of all cities owned by old owner, to a new owner.
59         void changeOwnership(Player *old_owner, Player *new_owner);
60
61         /**
62          * Scan through all of the City objects who are vectoring to the given
63          * city, and stop the vectoring.
64          *
65          * @param city  The city to search for vectoring to.
66          */
67         //! Stops vectoring from any city to the specified city.
68         void stopVectoringTo(City *city);
69
70
71         // Methods that operate on class data and do not modify the class.
72
73         /**
74          * Scan through all of the city objects for a city that is vectoring
75          * to the given city.
76          *
77          * @param target  The city to check if any other cities are vectoring 
78          *                to.
79          *
80          * @return True if another city is vectoring to the given city.
81          *         Otherwise false.
82          */
83         //! Return whether or not any cities are vectoring to the given city.
84         bool isVectoringTarget(City *target) const;
85
86         //! Return the amount of money that the given player will pay next turn.
87         guint32 calculateUpcomingUpkeep(Player *p) const;
88
89         //! Return the number of cities vectoring to the given city.
90         guint32 countCitiesVectoringTo(const City *dest) const;
91
92         //! Save the list of City objects to an opened saved-game file.
93         bool save(XML_Helper* helper) const;
94        
95         //! Count the number of functional cities that a given player owns. 
96         int countCities(Player* p) const;
97
98         //! Count the total number of inhabitable cities.
99         int countCities() const;
100
101         /**
102          * Scans the list of city objects for the first one that is owned by
103          * the given player.
104          *
105          * @param player  The owner of the City to search for.
106          *
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.
109          */
110         //! Returns the first City object owned by the given player.
111         City* getFirstCity(Player* player) const;
112
113         //! Have each of the cities owned by the given player, pay gold pieces.
114         /**
115          * This method increases the player's treasury.
116          */
117         void collectTaxes(Player* p) const;
118
119         //! Returns the closest city that is owned by an enemy player.
120         /**
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.
124          *
125          * @note This method will not return a razed city.
126          *
127          * @param pos  The position on the game map to search for the nearest
128          *             enemy-owned City object from.
129          *
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.
132          */
133         City* getNearestEnemyCity(const Vector<int>& pos) const;
134
135         City* getClosestEnemyCity(const Stack *stack) const;
136
137         //! Returns the closest city that isn't owned by the active player.
138         /**
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.
141          *
142          * @note This method will not return a razed city.
143          *
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.
146          *
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.
149          */
150         City* getNearestForeignCity(const Vector<int>& pos) const;
151
152         City* getClosestForeignCity(const Stack *stack) const;
153
154         //! Return the closest city owned by the active player.
155         /**
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.
158          *
159          * @note This method will not return a razed city.
160          *
161          * @param pos  The position on the game map to search for the nearest
162          *             City object that is owned by the active player.
163          *
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.
167          */
168         City* getNearestFriendlyCity(const Vector<int>& pos) const;
169
170         City* getClosestFriendlyCity(const Stack *stack) const;
171
172         //! Find the closest city owned by the active player and isn't too far.
173         /**
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.
177          *
178          * @note This method will not return a razed city.
179          *
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".
183          *
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.
188          */
189         City* getNearestFriendlyCity(const Vector<int>& pos, int dist) const;
190
191         //! Find the closest city to the given position.
192         /**
193          * Scans through all of the City objects in the list for the nearest
194          * one that isn't razed.
195          *
196          * @param pos  The position on the game map to search for the nearest
197          *             City object from.
198          *
199          * @return A pointer to the nearest City object.  Returns NULL if there
200          *        are not any City objects in the list.
201          */
202         City* getNearestCity(const Vector<int>& pos) const;
203
204         City* getClosestCity(const Stack *stack) const;
205
206         //! Find the closest city that isn't too far away.
207         /**
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
210          * number of tiles.
211          *
212          * @param pos  The position on the game map to search for the nearest
213          *             City object from.
214          * @param dist The number of tiles away that is deemed "too far".
215          *
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.
219          */
220         City* getNearestCity(const Vector<int>& pos, int dist) const;
221
222         //! Find the nearest city that is not obscured by fog.
223         /**
224          * Scan through all cities, searching for the closest one that is
225          * not covered by fog-of-war on a hidden map.
226          *
227          * @note This method will not return a city that has been razed.
228          *
229          * @param pos  The position to find the nearest city from.
230          *
231          * @return A pointer to the nearest city that is not obscured by fog.
232          */
233         City* getNearestVisibleCity(const Vector<int>& pos) const;
234
235         //! Find the nearest city that is unobscured and is not too far away.
236         /**
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.
240          *
241          * @note This method will not return a city that has been razed.
242          *
243          * @param pos  The position to find the nearest city from.
244          * @param dist The number of tiles away that is deemed "too far".
245          *
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.
249          */
250         City* getNearestVisibleCity(const Vector<int>& pos, int dist) const;
251
252         //! Return the closest city owned by the given player.
253         /**
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.
256          *
257          * @note This method will not return a razed city.
258          *
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
262          *               searching for.
263          *
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.
267          */
268         City* getNearestCity(const Vector<int>& pos, Player *player) const;
269
270         City* getClosestCity(const Stack *stack, Player *p) const;
271
272         //! Return the closest city owned by the neutral player.
273         /**
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.
276          *
277          * @note This method will not return a razed city.
278          *
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
282          *               searching for.
283          *
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.
287          */
288         City* getNearestNeutralCity(const Vector<int>& pos) const;
289
290         //! Find the nearest unobscured city that is owned by the active player.
291         /**
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
294          * active player.
295          *
296          * @note This method will not return a city that has been razed.
297          *
298          * @param pos  The position to find the nearest city from.
299          *
300          * @return A pointer to the nearest city that is not obscured by fog,
301          *         and is owned by the active player.
302          */
303         City* getNearestVisibleFriendlyCity(const Vector<int>& pos) const;
304
305         //! Get the nearest unfogged city of active player's that isn't too far.
306         /**
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.
310          *
311          * @note This method will not return a city that has been razed.
312          *
313          * @param pos  The position to find the nearest city from.
314          * @param dist The number of tiles away that is deemed "too far".
315          *
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.
320          */
321         City* getNearestVisibleFriendlyCity(const Vector<int>& pos, int dist) const;
322
323         //! Return the list of cities vectoring to the given city.
324         /**
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.
327          *
328          * @param target  The city to check if any other cities are vectoring 
329          *                to.
330          *
331          * @return The list of pointers to city objects that are vectoring to
332          *         the given city.
333          */
334         std::list<City*> getCitiesVectoringTo(City *target) const;
335
336         //! Return the list of cities vectoring to the given spot on the map.
337         std::list<City*> getCitiesVectoringTo(Vector<int> target) const;
338
339         //! Get the nearest city that is farther than a given number tiles.
340         /**
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
343          * tiles.
344          *
345          * @param pos  The position on the game map to search for the nearest
346          *             City object from.
347          * @param dist The distance that is deemed "too close".
348          *
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.
352          */
353         City* getNearestCityPast(const Vector<int>& pos, int dist) const;
354
355
356         //! Get the nearest city to POS that can be vectored to.
357         City* getNearestFriendlyVectorableCity(const Vector<int>& pos) const;
358
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;
361
362         //! Get the capital city of the given player.
363         City *getCapitalCity(Player *player) const;
364
365         // Static Methods
366
367         //! Returns the singleton instance.  Creates a new one if neccessary.
368         static Citylist* getInstance();
369
370         //! Loads the singleton instance from an opened saved-game file.
371         static Citylist* getInstance(XML_Helper* helper);
372
373         //! Deletes the singleton instance.
374         static void deleteInstance();
375
376
377     protected:
378
379         //! Default constructor.
380         Citylist();
381
382         //! Loading constructor.
383         /**
384          * Make a new Citylist object by reading it from an opened saved-game 
385          * file.
386          *
387          * @param helper  The opened saved-game file to load the list of City
388          *                objects from.
389          */
390         Citylist(XML_Helper* helper);
391
392         //! Destructor.
393         ~Citylist();
394
395
396     private:
397         //! A callback for loading City objects into the list of cities.
398         bool load(std::string tag, XML_Helper* helper);
399
400         //! A static pointer for the singleton instance.
401         static Citylist* s_instance;
402 };
403
404 #endif // CITYLIST_H
405
406 // End of file