~ubuntu-branches/ubuntu/maverick/lordsawar/maverick

« back to all changes in this revision

Viewing changes to src/stacklist.h

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2010-04-10 09:29:33 UTC
  • mfrom: (1.1.9 upstream) (5.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100410092933-23uq4dxig30kmtcw
Tags: 0.1.8-1
* New upstream release.
* Add misc:Depends for -data package.
* Bump Standards Version to 3.8.4. (No changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Ulf Lorenz
3
3
// Copyright (C) 2004, 2005 Andrea Paternesi
4
4
// Copyright (C) 2004 Andrea Paternesi
5
 
// Copyright (C) 2007, 2008, 2009 Ben Asselstine
 
5
// Copyright (C) 2007, 2008, 2009, 2010 Ben Asselstine
6
6
// Copyright (C) 2008 Ole Laursen
7
7
//
8
8
//  This program is free software; you can redistribute it and/or modify
24
24
#define STACKLIST_H
25
25
 
26
26
#include <list>
 
27
#include <map>
27
28
#include <vector>
28
29
#include <string>
29
30
#include <sigc++/trackable.h>
54
55
 
55
56
        //! Default constructor.
56
57
        Stacklist();
 
58
 
57
59
        //! Copy constructor.
58
60
        Stacklist(Stacklist *stacklist);
 
61
 
59
62
        //! Loading constructor.
60
63
        Stacklist(XML_Helper* helper);
 
64
 
61
65
        //! Destructor.
62
66
        ~Stacklist();
63
67
 
64
 
        /**
65
 
         * Scan through every player's Stacklist, for a stack that is located 
66
 
         * at the given position on the game map.
67
 
         *
68
 
         * @param x   The number of tiles down in the vertical axis from the
69
 
         *            topmost edge of the map.
70
 
         * @param y   The number of tiles right in the horizontal axis from
71
 
         *            the leftmost edge of the map.
72
 
         *
73
 
         * @return A pointer to a stack at the given position, or NULL if no
74
 
         *         Stack could be found.
75
 
         */
76
 
        //! Return the stack at position (x,y) or 0 if there is none.
77
 
        static Stack* getObjectAt(int x, int y);
78
 
        void getObjectAt(Vector<int> pos, Stack *& s1, Stack *& s2);
79
 
 
80
 
        /**
81
 
         * Scan through every player's Stacklist, for a stack that is located 
82
 
         * at the given position on the game map.
83
 
         *
84
 
         * @param point    The point on the map that we're looking to see
85
 
         *                 if there is a Stack on.
86
 
         *
87
 
         * @return A pointer to a stack at the given position, or NULL if no
88
 
         *         Stack could be found.
89
 
         */
90
 
        //! Return stack at position pos or 0 if there is none.
91
 
        static Stack* getObjectAt(Vector<int> point)
92
 
          { return getObjectAt(point.x, point.y);}
93
 
 
94
 
        /**
95
 
         * Scan through all stacks in the list, and then through each Army 
96
 
         * unit of every Stack for an Army unit with a particular Id.
97
 
         *
98
 
         * @param id     The Id of the Army unit that we're looking for.
99
 
         *
100
 
         * @return The position of the Army unit.  If no Army unit could be
101
 
         *         found with the given Id, the position of (-1,-1) is
102
 
         *         returned.
103
 
         */
104
 
        //! Return position of an Army.
105
 
        static Vector<int> getPosition(guint32 id);
 
68
        // Set Methods
 
69
 
 
70
        //!Set the stack the player is currently working with.
 
71
        /** 
 
72
         * Sets the currently selected stack. The purpose of this method is 
 
73
         * to designate a stack to be the one the player is currently touching.
 
74
         * It is important to use this method because several functions
 
75
         * expect that there is an active stack.
 
76
         *
 
77
         * @param activestack      The stack currently selected by the player.
 
78
         */
 
79
        void setActivestack(Stack* activestack);
 
80
 
 
81
 
 
82
        // Get Methods
 
83
 
 
84
        //! Returns the currently selected stack.
 
85
        Stack* getActivestack() const {return d_activestack;}
 
86
 
 
87
 
 
88
        // Methods that operate on class data and do not modify the class.
106
89
 
107
90
        /** 
108
91
         * This method finds stacks which occupy the same tile.
123
106
        //! Get the other stack on a tile that has more than one stack on it.
124
107
        static Stack* getAmbiguity(Stack* stack);
125
108
 
126
 
        //! Searches through the all players Stacklists and deletes the stack.
127
 
        static bool deleteStack(Stack* stack);
128
 
        static bool deleteStack(guint32 id);
129
 
 
130
 
        /** 
131
 
         * Scan each tile occupied by the given city and return a list of
132
 
         * stacks who are in the city.
133
 
         *
134
 
         * When a city is attacked, all stacks which occupy a city tile are
135
 
         * regarded as defenders.  The purpose of this function is to 
136
 
         * enumerate the defending stacks when a stack has attacked a city.
137
 
         *
138
 
         * @param city        The city to search for stacks in.
139
 
         *
140
 
         * @return A list of all stacks defending the city.
141
 
         */
142
 
        //! Return a list of stacks defending a city.
143
 
        static std::vector<Stack*> defendersInCity(City* city);
144
 
 
145
 
        //! Returns the total number of stacks owned by all players.
146
 
        static unsigned int getNoOfStacks();
147
 
 
148
109
        //! Returns the total number of armies in the list.
149
 
        unsigned int countArmies();
150
 
 
151
 
        //! Returns the total number of heroes in the list.
152
 
        unsigned int countHeroes();
153
 
 
154
 
        /** 
155
 
         * Sets the currently selected stack. The purpose of this method is 
156
 
         * to designate a stack to be the one the player is currently touching.
157
 
         * It is important to use this method because several functions
158
 
         * expect that there is an active stack.
159
 
         *
160
 
         * @param activestack      The stack currently selected by the player.
161
 
         */
162
 
        void setActivestack(Stack* activestack);
 
110
        unsigned int countArmies() const;
 
111
 
 
112
        //! Returns the total number of armies in the list that are awardable.
 
113
        unsigned int countAllies() const;
163
114
 
164
115
        /**
165
116
         * Scan through the list of stacks to find one that is not defending, 
169
120
         *         stacks can move.
170
121
         */
171
122
        //! Return the next moveable stack in the list.
172
 
        Stack* getNextMovable();
 
123
        Stack* getNextMovable() const;
 
124
 
 
125
        /**
 
126
         * @return True if any stacks in the list have enough moves for 
 
127
         * it's next step along it's Path.  Otherwise, false.
 
128
         */
 
129
        //! Returns whether or not any stacks can move.
 
130
        bool enoughMoves() const;
173
131
 
174
132
        //! Save the data to an opened saved-game file.
175
133
        bool save(XML_Helper* helper) const;
176
134
 
 
135
        //! Find a stack in this stacklist given this id.
 
136
        Stack *getStackById(guint32 id) const;
 
137
 
 
138
        //! Find the stack in this stacklist that contains an army with this id.
 
139
        Stack *getArmyStackById(guint32 army) const;
 
140
 
 
141
        //! Collect gold pieces from army units in the list that give money.
 
142
        /**
 
143
         * Heroes in a stacklist can provide gold pieces by carrying items.
 
144
         * These items are +3 gold per city, etc so the number of cities
 
145
         * must be passed into this method.
 
146
         *
 
147
         * This method increases the given player's treasury.
 
148
         */
 
149
        void collectTaxes(Player *p, guint32 num_cities) const;
 
150
 
 
151
        //! Return a list of the heroes in the stacklist.
 
152
        std::list<Hero*> getHeroes() const;
 
153
 
 
154
        //! Return a hero from this stacklist that is closest to the given pos.
 
155
        /**
 
156
         * The hero must be within a given distance, provided by the dist
 
157
         * parameter.  The distance is measured in tiles (not movement points).
 
158
         */
 
159
        Hero *getNearestHero(Vector<int> pos, int dist) const;
 
160
        
 
161
 
 
162
        // Methods that operate on class data and modify the class.
 
163
 
 
164
        //! remove all movement points from every army in every stack.
 
165
        void drainAllMovement();
 
166
 
177
167
        //! Callback method executed at the end of every turn.
178
168
        void nextTurn();
 
169
 
 
170
        //! Have everyone in this stacklist collect their upkeep from a player.
 
171
        /**
 
172
         * This method decreases the given player's treasury.
 
173
         */
179
174
        void payUpkeep(Player *p);
180
175
 
181
 
        /**
182
 
         * @return True if any stacks in the list have enough moves for 
183
 
         * it's next step along it's Path.  Otherwise, false.
184
 
         */
185
 
        //! Returns whether or not any stacks can move.
186
 
        bool enoughMoves() const;
187
 
 
188
 
        //! Returns the currently selected stack.
189
 
        Stack* getActivestack() const {return d_activestack;}
190
 
 
191
176
        //! Erase all stacks from the list, and their contents too.
192
177
        void flClear();
193
178
 
194
179
        //! Add a stack to the list.
195
180
        void add(Stack *stack);
 
181
 
196
182
        /** 
197
183
         * Erase a Stack from the list, and free the contents of the Stack.
198
184
         *
213
199
         */
214
200
        //! Erase a stack from the list.
215
201
        bool flRemove(Stack* stack);
 
202
 
 
203
        //! Erase a stack from the list, given the stack id.
216
204
        bool flRemove(guint32 id);
217
205
 
218
 
        /**
219
 
         * Scan through the Stacklist, for a stack that is located at the 
220
 
         * given position on the game map.
221
 
         *
222
 
         * @note This method works only on this Stacklist, rather than all
223
 
         *       of the players Stacklists as in Stacklist::getObjectAt.
224
 
         *
225
 
         * @param x   The number of tiles down in the vertical axis from the
226
 
         *            topmost edge of the map.
227
 
         * @param y   The number of tiles right in the horizontal axis from
228
 
         *            the leftmost edge of the map.
229
 
         *
230
 
         * @return A pointer to a stack at the given position, or NULL if no
231
 
         *         Stack could be found.
232
 
         */
233
 
        //! Return the stack at position (x,y) or 0 if there is none.
234
 
        Stack* getOwnObjectAt(int x, int y);
235
 
        Stack* getOwnObjectAt(Vector<int> point)
236
 
          { return getOwnObjectAt(point.x, point.y);}
237
 
 
238
 
        Stack *getStackById(guint32 id);
239
 
        Stack *getArmyStackById(guint32 army);
240
 
 
241
 
        void collectTaxes(Player *p, guint32 num_cities);
242
 
 
243
 
        std::list<Hero*> getTopHeroes(int num);
244
 
 
 
206
 
 
207
        // Signals
 
208
 
 
209
        //! The stack in the stacklist has been grouped or ungrouped.
 
210
        sigc::signal<void, Stack*, bool> sgrouped;
 
211
 
 
212
        //! The stack in the stacklist is arriving on a new tile.
 
213
        sigc::signal<void, Stack*, Vector<int> > snewpos;
 
214
 
 
215
        //! The stack in the stacklist is leaving an old tile.
 
216
        sigc::signal<void, Stack*, Vector<int> > soldpos;
 
217
 
 
218
 
 
219
        // Static Methods
 
220
 
 
221
        //! Searches through the all players Stacklists and deletes the stack.
 
222
        static bool deleteStack(Stack* stack);
 
223
 
 
224
        //! Searches for the stack id in all lists, and deletes it.
 
225
        static bool deleteStack(guint32 id);
 
226
 
 
227
        /** 
 
228
         * Scan each tile occupied by the given city and return a list of
 
229
         * stacks who are in the city.
 
230
         *
 
231
         * When a city is attacked, all stacks which occupy a city tile are
 
232
         * regarded as defenders.  The purpose of this function is to 
 
233
         * enumerate the defending stacks when a stack has attacked a city.
 
234
         *
 
235
         * @param city        The city to search for stacks in.
 
236
         *
 
237
         * @return A list of all stacks defending the city.
 
238
         */
 
239
        //! Return a list of stacks defending a city.
 
240
        static std::vector<Stack*> getDefendersInCity(const City* city);
 
241
 
 
242
        //! Returns the total number of stacks owned by all players.
 
243
        static unsigned int getNoOfStacks();
 
244
 
 
245
        //! Returns the total number of armies owned by all players.
 
246
        static unsigned int getNoOfArmies();
 
247
 
 
248
        /**
 
249
         * This method removes the given stack from it's owner's stacklist and
 
250
         * places it in the new owner's stacklist.
 
251
         */
 
252
        //! Change allegience of the given stack to a new owner.
 
253
        static void changeOwnership(Stack *stack, Player *new_owner);
 
254
 
 
255
        //! Can this stack reach a point where it is not blocked by friendlies?
 
256
        /**
 
257
         * The idea here is that the stack is blocked by a friendly stack on
 
258
         * it's path.  This method checks to see if the stack has enough 
 
259
         * movement points to proceed through the friendly stacks (that are
 
260
         * too big to join with), and land on a point in it's path beyond it.
 
261
         */
245
262
        static bool canJumpOverTooLargeStack(Stack *s);
246
263
 
247
 
        std::list<Hero*> getHeroes();
248
 
 
249
 
        Hero *getNearestHero(Vector<int> pos, int dist);
250
 
        
251
 
        sigc::signal<void, Stack*, bool> sgrouped;
252
 
 
253
 
        //! remove all movement points from every army in every stack.
254
 
        void drainAllMovement();
255
 
 
 
264
        //! Return position of an Army with the given id in this stacklist.
 
265
        /**
 
266
         * Scan through all stacks in the list, and then through each Army 
 
267
         * unit of every Stack for an Army unit with a particular Id.
 
268
         *
 
269
         * @param id     The Id of the Army unit that we're looking for.
 
270
         *
 
271
         * @return The position of the Army unit.  If no Army unit could be
 
272
         *         found with the given Id, the position of (-1,-1) is
 
273
         *         returned.
 
274
         */
 
275
        static Vector<int> getPosition(guint32 id);
 
276
 
 
277
 
 
278
        std::list<Vector<int> > getPositions() const;
 
279
 
 
280
        std::list<Stack*> getStacksWithItems() const;
 
281
 
 
282
        bool check();
256
283
    private:
257
 
        //! Callback function for loading.
 
284
 
 
285
        //! Callback for loading this object from an opened saved-game file.
258
286
        bool load(std::string tag, XML_Helper* helper);
259
287
 
260
 
        //! Callbacks for when things happen to stack in our list.
 
288
        //! Callback for when a stack initiates movement after being stopped.
261
289
        void on_stack_starts_moving (Stack *s);
 
290
 
 
291
        //! Callback for when a stack halts.
262
292
        void on_stack_stops_moving (Stack *s);
 
293
 
 
294
        //! Callback for when a stack has been killed, or disbanded.
263
295
        void on_stack_died (Stack *stack);
 
296
 
 
297
        //! Callback for when a stack has been grouped or ungrouped.
264
298
        void on_stack_grouped (Stack *stack, bool grouped);
265
299
 
266
 
        void getHeroes(std::vector<guint32>& dst);
 
300
        void getHeroes(std::vector<guint32>& dst) const;
 
301
 
 
302
        //! Notify the game map that a stack is leaving it's tile.
 
303
        bool deletePositionFromMap(Stack *stack);
 
304
 
 
305
        //! Notify the game map that a stack is arriving on a tile.
 
306
        bool addPositionToMap(Stack *s);
 
307
 
 
308
        // DATA
 
309
 
267
310
        //! A pointer to the currently selected Stack.
268
311
        Stack* d_activestack;
269
312
 
270
 
        //! methods for managing the dual hash maps.
271
 
        bool deletePositionFromMap(Stack *stack);
272
 
        bool addPositionToMap(Stack *s);
273
 
        //! a hash map of where the stacks are on the map.
274
 
        typedef std::map<Vector<int>, Stack*> PositionMap;
275
 
        PositionMap d_object1;
276
 
        PositionMap d_object2;
277
313
        typedef std::map<Stack *, std::list<sigc::connection> > ConnectionMap;
 
314
        // A set of signal connections for each stack in our list.
 
315
        /**
 
316
         * We can use this map to lookup the outstanding signals connections
 
317
         * that a given stack has.  This is so that we can destroy them
 
318
         * when the stack is deleted.
 
319
         */
278
320
        ConnectionMap d_connections;
 
321
        
 
322
        typedef std::map<guint32, Stack*> IdMap;
 
323
        //! A map to quickly lookup the stack by it's unique id.
 
324
        IdMap d_id;
279
325
 
280
326
};
281
327