initial commit, lordsawar source, slightly modified
[lordsawar] / src / vectoredunitlist.h
1 //  Copyright (C) 2007, 2008 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
16 //  02110-1301, USA.
17
18 #ifndef VECTOREDUNITLIST_H
19 #define VECTOREDUNITLIST_H
20
21 #include <gtkmm.h>
22 #include <list>
23 #include <string>
24 class VectoredUnit;
25 class Player;
26 class XML_Helper;
27
28 #include "vector.h"
29
30 #include <sigc++/trackable.h>
31
32 class City;
33 //! A list of VectoredUnit objects.
34 /** 
35  * This class loads and saves the VectoredUnit objects in the game.  It 
36  * facilitates looking up VectoredUnit objects in the list.
37  *
38  * This class is loaded from, and saved to the lordsawar.vectoredunitlist XML
39  * entity in the saved-game file.
40  *
41  * Implemented as a singleton.
42  */
43 class VectoredUnitlist : public std::list<VectoredUnit*>, public sigc::trackable
44 {
45     public:
46         //! The xml tag of this object in a saved-game file.
47         static std::string d_tag; 
48
49
50         // Methods that operate on the class data and modify the class.
51
52         //! Processes all VectoredUnit objects belonging to the given Player.
53         void nextTurn(Player* p);
54
55
56         //! Cull the list of VectoredUnit objects going to the given position.
57         /**
58          * Scan through the VectoredUnitlist for VectoredUnit objects that
59          * have the given destination position on the game map.  When found, 
60          * remove it from the list.
61          *
62          * When a planted standard is picked up by another Player's Hero this
63          * method is called.
64          *
65          * @param pos  Any VectoredUnit object in the list that is being
66          *             vectored to this tile is deleted from the list.
67          */
68         bool removeVectoredUnitsGoingTo(Vector<int> pos);
69
70         //! Cull the list of VectoredUnit objects going to the given city.
71         /**
72          * Scan through the VectoredUnitlist for VectoredUnit objects that
73          * have a destination position of the given city.  When found, remove
74          * it from the list.
75          *
76          * This method gets called when a destination city is conquered.
77          *
78          * @param city  Any VectoredUnit object in the list that is being
79          *              vectored to one of the tiles in this City are deleted
80          *              from the list.
81          */
82         bool removeVectoredUnitsGoingTo(City *city);
83
84         //! Cull the list of VectoredUnit objects being vectored from a place.
85         /**
86          * Scan through the VectoredUnitlist for VectoredUnit objects that
87          * have a source tile of the given position on the game map.  When 
88          * found, remove the VectoredUnit object from this list.
89          *
90          * When a source city gets conquered, VectoredUnit objects need to be
91          * deleted from this list.
92          *
93          * @param pos  Any VectoredUnit object in the list that is being
94          *             vectored from this tile is deleted from the list.
95          */
96         bool removeVectoredUnitsComingFrom(Vector<int> pos);
97
98         //! Cull the list of VectoredUnit objects coming from the given city.
99         /**
100          * Scan through the VectoredUnitlist for VectoredUnit objects that
101          * have a source position of a tile in the given city.  When found, 
102          * remove it from the list.
103          *
104          * This method gets called when a destination city is conquered.
105          *
106          * @param city  Any VectoredUnit object in the list that is being
107          *              vectored to one of the tiles in this City are deleted
108          *              from the list.
109          */
110         bool removeVectoredUnitsComingFrom(City *city);
111
112         //! Change the destination of vectored units as they are "in the air".
113         /**
114          * Scan through all of the VectoredUnit objects in the list for the
115          * ones that are being vectored to the given city.  When found,
116          * change the destination to be the given destination.
117          *
118          * @param city     A pointer to the city to change VectoredUnit objects
119          *                 from going to.
120          * @param new_dest A position on the game map to change where the
121          *                 VectoredUnit objects are going to.
122          */
123         bool changeDestination(City *city, Vector<int> new_dest);
124
125         //! Change the owner of all of the vectored units in the list.
126         /**
127          * Change the vectored units belonging to the old_player, to the
128          * new_player.
129          */
130         void changeOwnership(Player *old_owner, Player *new_owner);
131
132         //! Remove a vectored unit from the list.  Also deletes it.
133         iterator flErase(iterator object);
134
135
136         // Methods that operate on the class data but do not modify the class.
137
138         //! Save the list of VectoredUnit objects to a saved-game file.
139         bool save(XML_Helper* helper) const;
140
141         //! Return the list of VectoredUnit objects with the given destination.
142         /**
143          * Scan through the list of VectoredUnit objects for the ones that are
144          * being vectored to the given position.  Return all of the 
145          * VectoredUnit objects that match.
146          *
147          * This method is used for showing who's going where.
148          *
149          * @param pos  Any VectoredUnit object in the list that is being
150          *             vectored to this tile is returned.
151          *
152          * @param vectored  This list is filled with the VectoredUnit objects 
153          *                  being vectored to the given position.
154          */
155         void getVectoredUnitsGoingTo(Vector<int> pos, 
156                                      std::list<VectoredUnit*>& vectored) const;
157
158         //! Return the list of VectoredUnit objects going to the given city.
159         /**
160          * Scan through the list of VectoredUnit objects for the ones that are
161          * being vectored to the tiles on the game map assocaited with the
162          * given city.  Return all of the VectoredUnit objects that match.
163          *
164          * This method is used for showing who's going where.
165          *
166          * @param city  Any VectoredUnit object in the list that is being
167          *              vectored to this city is returned.
168          *
169          * @param vectored  This list is filled with the VectoredUnit objects 
170          *                  being vectored to the given city.
171          */
172         void getVectoredUnitsGoingTo(City *city, 
173                                      std::list<VectoredUnit*>& vectored) const;
174
175         //! Return the list of VectoredUnit objects with the given source.
176         /**
177          * Scan through the list of VectoredUnit objects for the ones that are
178          * being vectored from the given position.  Return all of the 
179          * VectoredUnit objects that match.
180          *
181          * This method is used for showing who's coming from where.
182          *
183          * @param pos  Any VectoredUnit object in the list that is being
184          *             vectored from this tile is returned.
185          *
186          * @param vectored  This list is filled with the VectoredUnit objects 
187          *                  being vectored from the given position.
188          */
189         void getVectoredUnitsComingFrom(Vector<int> pos, 
190                                         std::list<VectoredUnit*>& vectored) const;
191
192         //! Return the number of VectoredUnits being vectored to a given place.
193         /**
194          * Scan through all of the VectoredUnit objects in the list for ones
195          * that are being vectored to the given position on the game map.
196          * Count all of the matching VectoredUnit objects, and return the
197          * count.
198          *
199          * @return The number of VectoredUnit objects that are being vectored
200          *         to the given position on the game map.
201          */
202         guint32 getNumberOfVectoredUnitsGoingTo(Vector<int> pos) const;
203
204         // Static Methods
205
206         //! Gets the singleton instance or creates a new one.
207         static VectoredUnitlist* getInstance();
208
209         //! Loads the VectoredUnitlist from a saved-game file.
210         /**
211          * Load all VectoredUnit objects in the VectoredUnitlist from a 
212          * saved-game file.
213          *
214          * @param helper     The opened saved-game file to read from.
215          *
216          * @return The loaded VectoredUnitlist.
217          */
218         static VectoredUnitlist* getInstance(XML_Helper* helper);
219
220         //! Explicitly deletes the singleton instance.
221         static void deleteInstance();
222         
223     protected:
224
225         //! Default constructor.
226         VectoredUnitlist();
227
228         //! Loading constructor.
229         VectoredUnitlist(XML_Helper* helper);
230
231         //! Destructor.
232         ~VectoredUnitlist();
233
234     private:
235
236         //! Callback for loading the VectoredUnitlist from a saved-game file.
237         bool load(std::string tag, XML_Helper* helper);
238
239         // DATA
240
241         //! A static pointer for the singleton instance.
242         static VectoredUnitlist* s_instance;
243 };
244
245 #endif