1 // Copyright (C) 2003, 2004, 2005, 2006 Ulf Lorenz
2 // Copyright (C) 2004 Andrea Paternesi
3 // Copyright (C) 2007, 2008, 2009, 2014 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 QUEST_H
22 #define QUEST_H
23 
24 #include <list>
25 #include <queue>
26 #include "callback-enums.h"
27 #include "OwnerId.h"
28 #include "vector.h"
29 
30 class XML_Helper;
31 class QuestsManager;
32 class Hero;
33 class Stack;
34 class City;
35 class Army;
36 
37 //! Base class for Quest objects. Hero units go on quests for Reward objects.
38 /**
39  * Quest objects are started by Hero objects by visiting a Temple object.  The
40  * Quest has a given Quest::Type.  If the Hero can successfully complete the
41  * terms of the Quest, a Reward is given.  If the Hero cannot complete the
42  * Quest, because of death or because the Quest is now simply impossible, the
43  * Quest expires.
44  *
45  * This class adds some basic functionality for quests, mainly code concerning
46  * hero association.
47  *
48  * This object and the classes that derive from it equate to the
49  * lordsawar.questlist.quest XML entity in the saved-game file.
50  *
51  */
52 class Quest: public OwnerId
53 {
54     public:
55 	//! The xml tag of this object in a saved-game file.
56 	static Glib::ustring d_tag;
57 
58 	//! The various kinds of Quest objects.
59         enum Type {
60 	  //! Find another Player's Hero and kill it.
61 	  KILLHERO = 1,
62 	  //! Seek and destroy a number of another Player's armies.
63 	  KILLARMIES = 2,
64 	  //! Conquer and sack another Player's city.
65 	  CITYSACK = 3,
66 	  //! Conquer and raze another Player's city.
67 	  CITYRAZE = 4,
68 	  //! Conquer and occupy another Player's city.
69 	  CITYOCCUPY = 5,
70 	  //! Find an army of the given kind and kill it.
71 	  KILLARMYTYPE = 6,
72 	  //! Sack and pillage a number of gold pieces from enemies.
73 	  PILLAGEGOLD = 7
74 	};
75 
76         //! Standard constructor.
77 	/**
78 	 * Make a new Quest object.  This object is not called directly -- it
79 	 * is only called by the derived classes.
80 	 *
81 	 * @param q_mgr The quest manager this quest is being associated with.
82 	 * @param hero  The Id of the Hero object that owns this quest.
83 	 * @param type  The type of Quest the Hero is going on.
84 	 */
85         Quest(QuestsManager& q_mgr, guint32 hero, Type type);
86 
87         //! Loading constructor.
88 	/**
89 	 * Make a new quest by loading it in from an opened saved-game file.
90 	 * @note This only reads the data that is common to all quests.
91 	 *
92 	 * @param q_mgr   The quest manager to associate the new Quest with.
93 	 * @param helper  The opened saved-game file to load the Quest from.
94 	 */
95         Quest(QuestsManager& q_mgr, XML_Helper* helper);
96 
97 	//! Destructor.
~Quest()98         virtual ~Quest() {};
99 
100 	// Get Methods
101 
102 	//! Return the description of the Quest.
103 	/**
104          *  This is the 'static' part of the quest description,
105          *  set once the quest has been initialized.  Another,
106          *  dynamic part consists of the quest's status info,
107          *  which is obtained by the Quest::getProgress method.
108          */
getDescription()109         Glib::ustring getDescription() const { return d_description; }
110 
111 	//! Returns if the Quest will be deleted at the end of the round.
isPendingDeletion()112         bool isPendingDeletion() const {return d_pending;}
113 
114 	//! Return the Id of the Hero object responsible for this Quest object.
getHeroId()115         guint32 getHeroId() const { return d_hero; }
116 
117         //! Returns the name of the Hero responsible for this Quest.
getHeroName()118         Glib::ustring getHeroName() const {return d_hero_name;}
119 
120         //! Return the type of the quest (one of values listed in Quest::Type).
getType()121         guint32 getType() const { return d_type; }
122 
123 	//! Return the targets for this Quest.
124 	/**
125 	 * This method provides a list of positions that the hero is seeking.
126 	 * This method is called by the questmap object to assist in showing
127 	 * the quest on a map.
128 	 * Quest::PILLAGEGOLD does not have any targets.
129 	 *
130 	 * @return A list of positions on the map that the Hero is seeking.
131 	 */
getTargets()132 	std::list< Vector<int> > getTargets() const {return d_targets;}
133 
134 
135 	// Set Methods
136 
137 	//! Set the Quest as not mattering anymore.
deactivate()138         void deactivate() {d_pending = true;}
139 
140 
141 	// Methods that operate on the class data but do not modify the class.
142 
143         //! Return a pointer to the Hero object responsible for the Quest.
getHero()144         Hero* getHero() const { return getHeroById(d_hero); }
145 
146 	//! Determine the name of the hero, even if it's dead.
147 	Glib::ustring getHeroNameForDeadHero() const;
148 
149 	//! Save the Quest to an opened saved-game file.
150         /**
151           * @note This function is called by the actual quests and only saves
152           * the common data. It does NOT open/close tags etc. This has to be
153           * done by the derived classes.
154 	  *
155 	  * @param helper  The opened saved-game file to save the common Quest
156 	  *                data to.
157           */
158         virtual bool save(XML_Helper* helper) const;
159 
160 
161 	// Methods that need to be implemented by derived classes.
162 
163 	//! Return the description of the progress the Hero has made.
164         virtual Glib::ustring getProgress() const = 0;
165 
166 	//! Return the completion text that is associated with this Quest.
167         /**
168 	 * @param msgs  A queue of strings that represents the completion
169 	 *              text to show.
170          */
171         virtual void getSuccessMsg(std::queue<Glib::ustring>& msgs) const = 0;
172 
173         /**
174          * \brief Provide the lines of the message describing
175                   the quest completion.
176          */
177 	//! Return the text that is shown when the Quest has expired.
178 	/**
179 	 * @param msgs  A queue of strings that represents the text to show
180 	 *              when the Quest has expired.
181 	 */
182         virtual void getExpiredMsg(std::queue<Glib::ustring>& msgs) const = 0;
183 
184 	//! Callback whenever an Army dies.
185 	/**
186 	 * This method notifies the Quest that an army has died, and if the u
187 	 * hero responsible for this quest killed it or not.
188 	 *
189 	 * @param army           An Army object that has just died in the game.
190 	 * @param heroIsCulprit  Whether or not the Hero object associated with
191 	 *                       this Quest object is responsible for killing
192 	 *                       the given Army object.
193 	 */
194 	virtual void armyDied(Army *army, bool heroIsCulprit)=0;
195 
196 	//! Callback whenever a city has been conquered.
197 	/**
198 	 * This method notifies the Quest that a City has fallen, and what the
199 	 * conquering action (pillage/sack/raze/occupy) was.  It also notifies
200 	 * whether or not the hero responsible for this quest was involved in
201 	 * the conquering, and how much gold was taken as a result.
202 	 *
203 	 * @param city           The City object that has been conquered.
204 	 * @param action         What action was taken by the Player.  See
205 	 *                       CityDefeatedAction for more information.
206 	 * @param heroIsCulprit  Whether or not the Hero object associated with
207 	 *                       this Quest object is responsible for
208 	 *                       conquering the given City object.
209 	 * @param gold           How many gold pieces were taken as a result
210 	 *                       of the action.
211 	 */
212 	virtual void cityAction(City *city, CityDefeatedAction action,
213 				bool heroIsCulprit, int gold)=0;
214 
215 
216 	// Static Methods
217 
218 	//! Determine the name of a hero, given the id.
219 	static Glib::ustring getHeroNameForDeadHero(guint32 id);
220 
221 	//! Convert a Quest::Type string to an enumerated value.
222 	static Quest::Type questTypeFromString(Glib::ustring str);
223 
224 	//! Convert a Quest::Type enumerated value to a string.
225 	static Glib::ustring questTypeToString(const Quest::Type type);
226 
227 	//! Return the Stack and Hero of a Quest.
228         /**
229          * @param hero      The id of the Hero on this quest.
230          * @param stack     This pointer is filled with a pointer to the stack
231 	 *                  that the Hero is in.  If passed as NULL, it is not
232 	 *                  calculated at all.
233 	 *
234          * @return A pointer to the Hero object or NULL if the Hero is dead.
235          */
236         static Hero* getHeroById(guint32 hero, Stack** stack = NULL);
237 
238     protected:
239 	// DATA
240 
241 	//! The QuestsManager object that this Quest object is associated with.
242         QuestsManager& d_q_mgr;
243 
244 	//! A description of the Quest (this text does not change).
245         /**
246          * This value is to be filled by the derived quest objects.
247          */
248         Glib::ustring d_description;
249 
250 	//! The Id of the Hero object responsible for this Quest.
251         guint32 d_hero;
252 
253         //! The type of the Quest (one of Quest::Type).
254         guint32 d_type;
255 
256         //! If set to false, this quest is deactivated and not to be processed.
257         bool d_pending;
258 
259 	//! The name of the hero who is on the Quest.
260 	/**
261 	 * The name of the Hero must be saved so that after the Hero dies, we
262 	 * can submit a history item that references the Hero's name.
263 	 */
264         Glib::ustring d_hero_name;
265 
266 	//! A list of targets to display on a questmap.
267 	/**
268 	 * The derived Quest classes fill in this value.
269 	 */
270 	std::list< Vector<int> > d_targets;
271 };
272 
273 #endif
274