1 // Copyright (C) 2001, 2003 Michael Bartl
2 // Copyright (C) 2002, 2003, 2004, 2005 Ulf Lorenz
3 // Copyright (C) 2007, 2008, 2009, 2014, 2020 Ben Asselstine
4 //
5 //  This program is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation; either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU Library General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 //  02110-1301, USA.
19 
20 #pragma once
21 #ifndef RUIN_H
22 #define RUIN_H
23 
24 #define DEFAULT_RUIN_NAME  "Ruin"
25 #include <sigc++/trackable.h>
26 #include "NamedLocation.h"
27 #include "stack.h"
28 
29 class Stack;
30 class Reward;
31 class Sage;
32 class Keeper;
33 
34 //! A ruin on the game map.
35 /**
36  * A ruin is a simple feature on the map which contains an id, a flag whether
37  * it has already been searched and optionally an occupant (called "keeper").
38  * If a ruin is searched, the player starts a fight with the keeper. If the
39  * player wins, the ruin becomes searched and the player gets a reward.
40  *
41  * Sometimes a ruin contains a sage which lets the player choose from a
42  * variety of rewards.
43  *
44  * Sometimes a ruin is hidden to all players except one player.
45  */
46 class Ruin : public NamedLocation, public sigc::trackable
47 {
48     public:
49 	//! The xml tag of this object in a saved-game file.
50 	static Glib::ustring d_tag;
51 
52 	//! The kind of ruin.
53         enum Type {
54 	  //! A normal ruin.
55 	  RUIN = 0,
56 	  //! A stronghold ruin is a little stronger.
57 	  STRONGHOLD = 1,
58           //! A Sage is a ruin without a keeper and provides choice of reward.
59           SAGE = 2
60 	};
61 
62 	//! Default constructor.
63         /**
64           * @param pos          The location of the ruin.
65 	  * @param width        The span of tiles that the ruin covers.
66           * @param name         The name of the ruin.
67           * @param occupant     The monster occupying the ruin.
68           * @param searched     Sets the searchedness flag of the ruin.
69 	  * @param hidden       Sets the hidden flag of the ruin.
70 	  * @param owner        Who can see this hidden ruin.
71 	  * @param sage         if this ruin contains a sage or not.
72           */
73         Ruin(Vector<int> pos, guint32 width,
74 	     Glib::ustring name = DEFAULT_RUIN_NAME, int type = Ruin::RUIN,
75 	     Keeper* occupant = 0, bool searched = false, bool hidden = false,
76 	     Player *owner = 0, bool sage = false);
77 
78         //! Copy constructor.
79         Ruin(const Ruin&);
80 
81 	//! Alternative copying constructor that changes the ruin position.
82         Ruin(const Ruin&, Vector<int> pos);
83 
84         //! Loading constructor.
85 	/**
86 	 * @param helper  The opened saved-game file to load the ruin from.
87 	 */
88         Ruin(XML_Helper* helper, guint32 tile_width);
89 
90 	//!Destructor.
91         ~Ruin();
92 
93 
94 	// Get Methods
95 
96         //! Returns the type of the ruin.
getType()97         int getType() const {return d_type;};
98 
99         //! Return whether or not the ruin has been searched already.
isSearched()100         bool isSearched() const {return d_searched;}
101 
102         //! Returns the keeper that guards the ruin from Hero units.
getOccupant()103         Keeper* getOccupant() const {return d_occupant;}
104 
105 	//! Returns whether or not this is a "hidden" ruin.
isHidden()106 	bool isHidden() const {return d_hidden;}
107 
108 	//! Returns whether or not this ruin has a sage.
hasSage()109 	bool hasSage() const {return d_type == SAGE ? true : false;}
110 
111 	//! Returns the player that owns this hidden ruin.
112         /**
113          * When the ruin has been searched, the owner is the player whose
114          * hero searched it.
115          */
getOwner()116 	Player *getOwner() const {return d_owner;}
117 
118 	//! Returns the reward for this ruin.
getReward()119 	Reward *getReward() const {return d_reward;}
120 
121 	//! Returns whether or not the ruin lacks a non-default name.
isUnnamed()122 	bool isUnnamed() const {return getName() == getDefaultName() ? true : false;};
123 
124 	// Set Methods
125 
126         //! Sets the type of the ruin.
setType(int type)127         void setType(int type) {d_type = type;};
128 
129         //! Change whether or not the ruin has been successfully searched.
setSearched(bool searched)130         void setSearched(bool searched) {d_searched = searched; }
131 
132         //! Set the keeper of the ruin.
133         void setOccupant(Keeper* occupant);
134 
135         //! Remove the keeper.
136         void clearOccupant();
137 
138         //! Change the "hidden" flag of the ruin.
setHidden(bool hidden)139         void setHidden (bool hidden) {d_hidden = hidden;}
140 
141 	//! Sets whether or not this ruin has a sage.
142 	void setSage(bool sage);
143 
144 	//! Sets the player that owns this hidden ruin.
setOwner(Player * owner)145 	void setOwner(Player *owner) {d_owner = owner;}
146 
147 	//! Sets the reward for this ruin.
148 	void setReward(Reward *r);
149 
150 
151 	// Methods that operate on class data and modify the class.
152 
153 	//! Put a random reward in this ruin.
154 	/**
155 	 * @note This method does not remove an existing reward before putting
156 	 *       a new one in it.
157 	 */
158 	void populateWithRandomReward();
159 
160 
161 	// Methods that operate on class data and modify the class.
162 
163         //! Steal the pointer to the reward and set the ruin's reward to nil.
164         Reward *takeReward();
165 
166         //! Callback for loading the ruin data.
167         bool load(Glib::ustring tag, XML_Helper* helper);
168 
169 	// Methods that operate on class data and do not modify the class.
170 
171         //! Saves the ruin data to an opened saved-game file.
172         bool save(XML_Helper* helper) const;
173 
174         //! Create a sage object (a list of rewards).
175         Sage* generateSage() const;
176 
177 	// Static Methods
178 
179 	//! Get the default name of any ruin.
getDefaultName()180 	static Glib::ustring getDefaultName() {return _(DEFAULT_RUIN_NAME);};
181 
182 	//! Convert a Ruin::Type enumerated value to a string.
183 	static Glib::ustring ruinTypeToString(const Ruin::Type type);
184 
185 	//! Convert a string containing a Ruin::Type to an enumerated value.
186 	static Ruin::Type ruinTypeFromString(const Glib::ustring str);
187 
188     private:
189 
190         // DATA
191 
192 	//! Whether or not the Ruin has already successfully been searched.
193         bool d_searched;
194 
195 	//! The type of the ruin.
196         guint32 d_type;
197 
198 	//! The keeper of the ruin.
199 	/**
200 	 * The Hero unit fights this named stack when it is searched.  The
201          * stack consists of a single Army unit that is cabable of defending
202          * ruins.
203 	 */
204         Keeper* d_occupant;
205 
206 	//! Whether or not the ruin is a hidden ruin.
207 	/**
208 	 * Hidden ruins are rewards.  Only a certain player can see the Ruin.
209 	 */
210 	bool d_hidden;
211 
212 	//! The player who can see the hidden ruin.
213 	/**
214 	 * Owning a ruin only makes sense if it is a hidden ruin.
215 	 * Only this player can see the hidden ruin, although other players
216 	 * may occupy the same tile.
217 	 */
218 	Player *d_owner;
219 
220 	//! The ruin contains a sage.
221 	/**
222 	 * A Sage offers the Hero the choice of many rewards.
223 	 */
224 	bool d_sage;
225 
226 	//! The reward to give if the Hero is successful in beating the keeper.
227 	Reward *d_reward;
228 };
229 
230 #endif // RUIN_H
231