1 // Copyright (C) 2003, 2005 Ulf Lorenz
2 // Copyright (C) 2004 Andrea Paternesi
3 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
4 // Copyright (C) 2007, 2008 Ole Laursen
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Library General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #ifndef QUEST_MANAGER_H
22 #define QUEST_MANAGER_H
27 #include <sigc++/trackable.h>
28 #include <sigc++/signal.h>
29 #include "callback-enums.h"
40 //! Manages Quest objects.
42 * This class creates new quests and assigns them to heroes. It also keeps
43 * track of pending quests and invalidates quests due to Hero death. It
44 * acts as a central place to catch army death events, and city conquered
45 * events, and it passes these events on to the Quest objects it manages.
47 * This object equates to the lordsawar.questlist XML entity in the saved-game
50 * This class is implemented as a singleton.
53 class QuestsManager : public sigc::trackable
57 //! The xml tag of this object in a saved-game file.
58 static std::string d_tag;
60 // Methods that operate on the class data and modify it.
62 //! Create a new quest for the given hero.
64 * Check and see which quests are possible and give the specified hero
67 * @param heroId The Id of the Hero object to be responsible for the
69 * @param razing_possible Whether or not razing cities is allowed in
70 * the scenario. If this is set to true, the
71 * quests that involve razing will be
72 * considered. Otherwise a razing quest will
73 * not be considered at all, and not returned.
75 * @return A pointer to the new Quest object.
77 Quest* createNewQuest(guint32 heroId, bool razing_possible);
79 //! Create new kill hero quest from remote action.
80 Quest* createNewKillHeroQuest(guint32 heroId, guint32 targetHeroId);
82 //! Create new enemy armies quest from remote action.
83 Quest* createNewEnemyArmiesQuest(guint32 heroId, guint32 num_armies,
84 guint32 victim_player_id);
86 //! Create new city sacking quest from remote action.
87 Quest* createNewCitySackQuest(guint32 heroId, guint32 cityId);
89 //! Create new city razing quest from remote action.
90 Quest* createNewCityRazeQuest(guint32 heroId, guint32 cityId);
92 //! Create new city occupation quest from remote action.
93 Quest* createNewCityOccupyQuest(guint32 heroId, guint32 cityId);
95 //! Create new kill enemy army type quest from remote action.
96 Quest* createNewEnemyArmytypeQuest(guint32 heroId, guint32 armyTypeId);
98 //! Create new pillage gold quest from remote action.
99 Quest* createNewPillageGoldQuest(guint32 heroId, guint32 amount);
101 //! Mark the Quest that the given Hero object is on to be completed.
103 * This method deactivates the quest and saves the completion
104 * notification message which will be presented to the player.
106 * @param heroId The id of the Hero object who has a Quest that we
107 * want to mark as complete.
109 void questCompleted(guint32 heroId);
111 //! Mark the Quest that the given Hero object is on to be expired.
113 * This method deactivates the quest and saves the expiry notification
114 * message which will be presented to the player.
116 * @param heroId The id of the Hero object who has a Quest that we
117 * want to mark as expired.
119 void questExpired(guint32 heroId);
121 //! Callback when an Army object is killed.
123 * Here we account for a dead army. maybe it's our hero,
124 * maybe it's a target hero, maybe we're somebody else's target hero
125 * or maybe we're some other army we're supposed to kill.
127 * @param army The army who was killed.
128 * @param culprits The list of Army object Ids that were involved in
129 * killing the given army.
131 void armyDied(Army *army, std::vector<guint32>& culprits);
133 //! Callback when a city is razed.
135 * Other classes call this to trigger this razing event. Derived
136 * classes of Quest catch this event via the Quest::cityAction
139 * @param city A pointer to the City object being razed.
140 * @param stack A pointer to the stack that conquered and is razing
143 void cityRazed(City *city, Stack *stack);
145 //! Callback when a city is sacked.
147 * Other classes call this to trigger this sacking event. Derived
148 * classes of Quest catch this event via the Quest::cityAction
151 * @param city A pointer to the City object being sacked.
152 * @param stack A pointer to the stack that conquered and is sacking
154 * @param gold The number of gold pieces that the sacking resulted in.
156 void citySacked(City *city, Stack *stack, int gold);
158 //! Callback when a city is pillaged.
160 * Other classes call this to trigger this pillaging event. Derived
161 * classes of Quest catch this event via the Quest::cityAction
164 * @param city A pointer to the City object being pillaged.
165 * @param stack A pointer to the stack that conquered and is pillaging
167 * @param gold The number of gold pieces that the pillaging resulted
170 void cityPillaged(City *city, Stack *stack, int gold);
172 //! Callback when a city is occupied.
174 * Other classes call this to trigger this occupying event. Derived
175 * classes of Quest catch this event via the Quest::cityAction
178 * @param city A pointer to the City object being occupied.
179 * @param stack A pointer to the stack that conquered and is occupying
182 void cityOccupied(City *city, Stack *stack);
184 //! Process the Quests at the start of every turn for the given player.
186 * @param player The player to process Quest objects for. The Hero
187 * object must be owned by this player to be processed.
189 void nextTurn(Player *player);
192 // Methods that operate on the class data and do not modify it.
194 //! Return a list of active Quest objects that belong to the player.
196 * @param player The player to get active Quest objects for.
198 std::vector<Quest*> getPlayerQuests(const Player *player) const;
200 Quest* getHeroQuest(guint32 hero_id) const;
202 //! Save the quests to an opened saved-game file.
204 * Saves the lordsawar.questlist XML entity to the saved-game file.
206 * @param helper The opened saved-game file to save the Quest objects
209 bool save(XML_Helper* helper) const;
214 //! Emitted when a Hero object completes a Quest.
216 * @param quest A pointer to the Quest object that was successfully
218 * @param reward A pointer to the reward that the Hero is receiving.
220 sigc::signal<void, Quest *, Reward *> quest_completed;
222 //! Emitted when a Hero object fails to complete a Quest.
224 * @param quest A pointer to the Ques tobject that was expired.
226 sigc::signal<void, Quest *> quest_expired;
231 //! Gets the singleton instance or creates a new one.
232 static QuestsManager* getInstance();
235 * Make a new QuestsManager object by loading all Quest objects from
236 * an opened saved-game file.
238 * @param helper The opened saved-game file to read from.
240 * @return A pointer to the new QuestsManager object.
242 //! Loads the questlist from a saved-game file.
243 static QuestsManager* getInstance(XML_Helper* helper);
245 //! Explicitly deletes the singleton instance.
246 static void deleteInstance();
250 //! Default constructor.
253 //! Loading constructor.
255 * Loads the lordsawar.questlist XML entity from the saved-game file.
257 * @param helper The opened saved-game file to load the questlist from.
259 QuestsManager(XML_Helper* helper);
266 //! Definition of a function pointer for the Quest::isFeasible methods.
267 typedef bool (*QFeasibilityType)(guint32);
269 //! Callback for loading Quest objects into the QuestsManager.
270 bool load(std::string tag, XML_Helper* helper);
272 //! Does some setup that has to be done on loading as well as creation.
275 //! Deactivates a given quest, i.e. marks it as 'to-delete'.
277 * @note A Hero can only have one quest, so giving the heroId is as
278 * good as specifying a particular quest.
280 void deactivateQuest(guint32 heroId);
282 //! This method performs cleanup of the marked quests
284 * Remove the quests marked as deactivated, and have heroes that belong
285 * to players of the given type.
287 void cleanup(Player::Type type = Player::HUMAN);
289 //! Callback when a city is conquered.
291 * This method calls the other simlarly named methods in the derived
294 * @param city A pointer to the city that was defeated.
295 * @param stack A pointer to the stack doing the conquering.
296 * @param action What action was taken: pillaging, sacking, razing, or
298 * @param gold The number of gold pieces achieved in the sacking or
301 void cityAction(City *c, Stack *s, CityDefeatedAction action, int gold);
305 //! A hash of all Quests in this QuestsManager. Lookup by HeroId.
306 std::map<guint32,Quest*> d_quests;
308 //! A list of quests that have been marked as 'to-delete'.
309 std::list<Quest*> d_inactive_quests;
311 //! A vector of isFeasible function pointers.
313 * This list of function pointers is used to see if it makes sense to
314 * give out a quest of a particular kind (Quest::Type).
316 std::vector<QFeasibilityType> d_questsFeasible;
318 //! A static pointer for the singleton instance.
319 static QuestsManager * s_instance;