initial commit, lordsawar source, slightly modified
[lordsawar] / src / stacktile.h
1 // Copyright (C) 2009 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 #ifndef STACKTILE_H
18 #define STACKTILE_H
19 #include <list>
20 #include <gtkmm.h>
21 #include "vector.h"
22 #include "Tile.h"
23 class Stack;
24 class Player;
25 /**
26  * This class is about managing a set stacks that share a tile on the map.
27  * 
28  * A stacktile object is a temporary object that holds a number of non-empty 
29  * stack objects belonging to the same player where their total number of 
30  * armies doesn't exceed 8.  The actual value is related to the 
31  * MAX_ARMIES_ON_A_SINGLE_TILE constant.
32  *
33  * This means that there can be up to 8 army units belong to the green player,
34  * and another 8 army units belonging to the red player.  Stacks of differing 
35  * sides only share a tile when fighting.
36  *
37  * The stacktile object is not saved to disk, instead it is reconstituted 
38  * based on the loading of stacklists.
39  *
40  * Like map backpack objects that exist on every tile, whether they have any 
41  * items in them or not, a stacktile object exists on every tile whether any 
42  * stacks are present or not.  the parent object in both cases is the maptile 
43  * object.
44  *
45  * A stack doesn't know what stacktile it's in but it knows Where it is on the 
46  * map.  game map has a quick lookup of position to stacktile.
47  */
48 //! Allow a bunch of stacks to share a single tile.
49
50 struct StackTileRecord
51 {
52   guint32 stack_id;
53   guint32 player_id;
54 };
55
56 class StackTile: public std::list<StackTileRecord>
57 {
58 public:
59     //! Constructor.
60     StackTile(Vector<int> pos);
61
62     //! Destructor.
63     ~StackTile();
64
65     // Methods that operate on the class data and modify the class.
66
67     //! Check to see if the given stack can be added to this tile.
68     bool canAdd(const Stack *stack);
69
70     //! Check to see if a stack with the given size and owner can be added here.
71     bool canAdd(guint32 size, Player *owner);
72
73     //! Remove the given stack from this stacktile.
74     bool leaving(Stack *stack);
75
76     //! Add the given stack to this stacktile.
77     void arriving(Stack *stack);
78
79     //! Add the given stack to this stacktile.
80     void add(Stack *stack);
81
82     //! Set all stacks on this tile to be defending.
83     void setDefending(Player *owner, bool defending);
84
85     //! Set all stacks on this tile to be parked.
86     void setParked(Player *owner, bool parked);
87
88     //! Merge all stacks on this tile belonging to the given player.
89     Stack *group(Player *owner);
90
91     //! Merge all stacks on this tile belonging to the given player into S.
92     void group(Player *owner, Stack *s);
93
94     //! Split all army units belonging to the given player into stacks.
95     void ungroup(Player *owner);
96
97
98     // Methods that operate on the class data and do not modify the class.
99
100     //! Return the first stack on this tile belonging to the given player.
101     Stack *getFriendlyStack(Player *owner) const;
102
103     //! Return all of the stacks on this tile belonging to the given player.
104     std::list<Stack *> getFriendlyStacks(Player *owner) const;
105
106     //! Return all stacks on this tile.
107     std::list<Stack *> getStacks() const;
108
109     //! Return the first stack on this tile not belonging to the given player.
110     Stack *getEnemyStack(Player *notowner) const;
111
112     //! Return all of the stacks on this tile not belonging to the given player.
113     std::list<Stack *> getEnemyStacks(Player *owner) const;
114
115     //! Return the first stack on this tile.
116     Stack *getStack() const;
117
118     //! Get the next friendly stack on this tile that isn't the given stack.
119     Stack *getOtherStack(Stack *stack) const;
120
121     //! Return true if this tile contains the given stack id.
122     bool contains(guint32 stack_id) const;
123
124     Vector<int> getTile() const {return tile;};
125
126     //! Return the number of army units on this tile owned by the given player.
127     guint32 countNumberOfArmies(Player *owner) const;
128 private:
129
130     Stack *groupStacks(Player *owner, Stack *s);
131     //! Return the position of the given stack in our list of records.
132     StackTile::const_iterator findStack(const Stack *s) const;
133     StackTile::iterator findStack(Stack *s);
134
135     // DATA
136
137     // Where on the game map this stack tile is.
138     Vector<int> tile;
139 };
140 #endif