1 // Copyright (C) 2009 Ben Asselstine
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.
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.
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
26 * This class is about managing a set stacks that share a tile on the map.
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.
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.
37 * The stacktile object is not saved to disk, instead it is reconstituted
38 * based on the loading of stacklists.
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
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.
48 //! Allow a bunch of stacks to share a single tile.
50 struct StackTileRecord
56 class StackTile: public std::list<StackTileRecord>
60 StackTile(Vector<int> pos);
65 // Methods that operate on the class data and modify the class.
67 //! Check to see if the given stack can be added to this tile.
68 bool canAdd(const Stack *stack);
70 //! Check to see if a stack with the given size and owner can be added here.
71 bool canAdd(guint32 size, Player *owner);
73 //! Remove the given stack from this stacktile.
74 bool leaving(Stack *stack);
76 //! Add the given stack to this stacktile.
77 void arriving(Stack *stack);
79 //! Add the given stack to this stacktile.
80 void add(Stack *stack);
82 //! Set all stacks on this tile to be defending.
83 void setDefending(Player *owner, bool defending);
85 //! Set all stacks on this tile to be parked.
86 void setParked(Player *owner, bool parked);
88 //! Merge all stacks on this tile belonging to the given player.
89 Stack *group(Player *owner);
91 //! Merge all stacks on this tile belonging to the given player into S.
92 void group(Player *owner, Stack *s);
94 //! Split all army units belonging to the given player into stacks.
95 void ungroup(Player *owner);
98 // Methods that operate on the class data and do not modify the class.
100 //! Return the first stack on this tile belonging to the given player.
101 Stack *getFriendlyStack(Player *owner) const;
103 //! Return all of the stacks on this tile belonging to the given player.
104 std::list<Stack *> getFriendlyStacks(Player *owner) const;
106 //! Return all stacks on this tile.
107 std::list<Stack *> getStacks() const;
109 //! Return the first stack on this tile not belonging to the given player.
110 Stack *getEnemyStack(Player *notowner) const;
112 //! Return all of the stacks on this tile not belonging to the given player.
113 std::list<Stack *> getEnemyStacks(Player *owner) const;
115 //! Return the first stack on this tile.
116 Stack *getStack() const;
118 //! Get the next friendly stack on this tile that isn't the given stack.
119 Stack *getOtherStack(Stack *stack) const;
121 //! Return true if this tile contains the given stack id.
122 bool contains(guint32 stack_id) const;
124 Vector<int> getTile() const {return tile;};
126 //! Return the number of army units on this tile owned by the given player.
127 guint32 countNumberOfArmies(Player *owner) const;
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);
137 // Where on the game map this stack tile is.