Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/output/src/modules/questsystem/QuestManager.cc @ 8987

Last change on this file since 8987 was 8811, checked in by landauf, 13 years ago

Replaced COUT() with orxout() in all modules. phew.

  • Property svn:eol-style set to native
File size: 12.6 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file QuestManager.cc
31    @brief Implementation of the QuestManager class.
32*/
33
34#include "QuestManager.h"
35
36#include "util/Exception.h"
37#include "util/ScopedSingletonManager.h"
38#include "core/command/ConsoleCommand.h"
39#include "core/CoreIncludes.h"
40#include "core/GUIManager.h"
41#include "core/LuaState.h"
42
43#include "infos/PlayerInfo.h"
44
45#include "Quest.h"
46#include "QuestHint.h"
47#include "QuestItem.h"
48
49namespace orxonox
50{
51    ManageScopedSingleton(QuestManager, ScopeID::Root, false);
52
53    /**
54    @brief
55        Constructor. Registers the object.
56    @todo
57        Is inheriting from BaseObject proper?
58    */
59    QuestManager::QuestManager()
60    {
61        RegisterRootObject(QuestManager);
62
63        orxout(internal_info, context::quests) << "QuestManager created." << endl;
64    }
65
66    /**
67    @brief
68        Destructor.
69    */
70    QuestManager::~QuestManager()
71    {
72        orxout(internal_info, context::quests) << "QuestManager destroyed." << endl;
73    }
74
75    /**
76    @brief
77        Retreive all Quests.
78    @return
79        Returns a map with all Quests indexed by their id's.
80    */
81    std::map<std::string, Quest*> & QuestManager::getQuests(void)
82    {
83        return this->questMap_;
84    }
85
86    /**
87    @brief
88        Registers a Quest with the QuestManager to make it globally accessible.
89        Uses it's id to make sure to be able to be identify and retrieve it later.
90    @param quest
91        The Quest that is to be registered.
92    @return
93        Returns true if successful, false if not.
94    */
95    bool QuestManager::registerQuest(Quest* quest)
96    {
97        assert(quest);
98
99        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
100        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); // Inserting the Quest.
101
102        if(result.second) // If inserting was a success.
103        {
104            quest->setRegistered();
105            orxout(verbose, context::quests) << "Quest with questId {" << quest->getId() << "} successfully inserted." << endl;
106            return true;
107        }
108        else
109        {
110           orxout(internal_warning, context::quests) << "Quest with the same id was already present." << endl;
111           return false;
112        }
113    }
114
115    /**
116    @brief
117        Unregisters a Quest in the QuestManager.
118    */
119    bool QuestManager::unregisterQuest(Quest* quest)
120    {
121        return this->questMap_.erase(quest->getId()) == 1;
122    }
123
124    /**
125    @brief
126        Registers a QuestHint with the QuestManager to make it globally accessible.
127        Uses it's id to make sure to be able to be identify and retrieve it later.
128    @param hint
129        The QuestHint to be registered.
130    @return
131        Returns true if successful, false if not.
132    */
133    bool QuestManager::registerHint(QuestHint* hint)
134    {
135        assert(hint);
136
137        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
138        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); // Inserting the QuestHSint.
139
140        if(result.second) // If inserting was a success.
141        {
142            hint->setRegistered();
143            orxout(verbose, context::quests) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << endl;
144            return true;
145        }
146        else
147        {
148           orxout(internal_warning, context::quests) << "QuestHint with the same id was already present." << endl;
149           return false;
150        }
151    }
152
153    /**
154    @brief
155        Unregisters a QuestHint in the QuestManager.
156    */
157    bool QuestManager::unregisterHint(QuestHint* hint)
158    {
159        return this->hintMap_.erase(hint->getId()) == 1;
160    }
161
162    /**
163    @brief
164        Finds a Quest with the given id.
165    @param questId
166        The id of the Quest sought for.
167    @return
168        Returns a pointer to the Quest with the input id.
169        Returns NULL if there is no Quest with the given questId.
170    @throws
171        Throws an exception if the given questId is invalid.
172    */
173    Quest* QuestManager::findQuest(const std::string & questId)
174    {
175        if(questId == "") // Check for validity of the given id.
176            ThrowException(Argument, "Invalid questId.");
177
178        Quest* quest;
179        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
180        if (it != this->questMap_.end()) // If the Quest is registered.
181            quest = it->second;
182        else
183        {
184           quest = NULL;
185           orxout(internal_warning, context::quests) << "The quest with id {" << questId << "} is nowhere to be found." << endl;
186        }
187
188        return quest;
189    }
190
191    /**
192    @brief
193        Finds a QuestHint with the given id.
194    @param hintId
195        The id of the QuestHint sought for.
196    @return
197        Returns a pointer to the QuestHint with the input id.
198        Returns NULL if there is no QuestHint with the given hintId.
199    @throws
200        Throws an exception if the given hintId is invalid.
201    */
202    QuestHint* QuestManager::findHint(const std::string & hintId)
203    {
204        if(hintId == "") // Check for validity of the given id.
205            ThrowException(Argument, "Invalid hintId.");
206
207        QuestHint* hint;
208        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
209        if (it != this->hintMap_.end()) // If the QuestHint is registered.
210            hint = it->second;
211        else
212        {
213           hint = NULL;
214           orxout(internal_warning, context::quests) << "The hint with id {" << hintId << "} is nowhere to be found." << endl;
215        }
216
217        return hint;
218    }
219
220    /**
221    @brief
222        Get the number of Quests the input player has, that are root quests.
223    @param player
224        The player.
225    @return
226        Returns the number of Quests the input player has, that are root quests.
227    */
228    int QuestManager::getNumRootQuests(PlayerInfo* player)
229    {
230        int numQuests = 0;
231        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
232        {
233            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player))
234                numQuests++;
235        }
236        return numQuests;
237    }
238
239    /**
240    @brief
241        Get the index-th root quest of the input player.
242    @param player
243        The player.
244    @param index
245        The index of the root quest.
246    @return
247        Returns the index-th root quest of the input player.
248    */
249    Quest* QuestManager::getRootQuest(PlayerInfo* player, int index)
250    {
251        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
252        {
253            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player) && index-- == 0)
254                return it->second;
255        }
256        return NULL;
257    }
258
259    /**
260    @brief
261        Get the number of sub-quest of an input Quest for the input player.
262    @param quest
263        The quest to get the sub-quests of.
264    @param player
265        The player.
266    @return
267        Returns the number of sub-quest of an input Quest for the input player.
268    */
269    int QuestManager::getNumSubQuests(Quest* quest, PlayerInfo* player)
270    {
271        if(quest == NULL)
272            return this->getNumRootQuests(player);
273
274        std::list<Quest*> quests = quest->getSubQuestList();
275        int numQuests = 0;
276        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
277        {
278            if(!(*it)->isInactive(player))
279                numQuests++;
280        }
281        return numQuests;
282    }
283
284    /**
285    @brief
286        Get the index-th sub-quest of the input Quest for the input player.
287    @param quest
288        The Quest to get the sub-quest of.
289    @param player
290        The player.
291    @param index
292        The index of the sub-quest.
293    */
294    Quest* QuestManager::getSubQuest(Quest* quest, PlayerInfo* player, int index)
295    {
296        if(quest == NULL)
297            return this->getRootQuest(player, index);
298
299        std::list<Quest*> quests = quest->getSubQuestList();
300        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
301        {
302            if(!(*it)->isInactive(player) && index-- == 0)
303                return *it;
304        }
305        return NULL;
306    }
307
308    /**
309    @brief
310        Get the number of QuestHints of the input Quest for the input player.
311    @param quest
312        The quest to get the hints of.
313    @param player
314        The player.
315    @return
316        Returns the number of QuestHints of the input Quest for the input player.
317    */
318    int QuestManager::getNumHints(Quest* quest, PlayerInfo* player)
319    {
320        std::list<QuestHint*> hints = quest->getHintsList();
321        int numHints = 0;
322        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
323        {
324            if((*it)->isActive(player))
325                numHints++;
326        }
327        return numHints;
328    }
329
330    /**
331    @brief
332        Get the index-th QuestHint of the input Quest for the input player.
333    @param quest
334        The Quest to get the QuestHint of.
335    @param player
336        The player.
337    @param index
338        The index of the QuestHint.
339    @return
340        Returns a pointer to the index-th QuestHint of the input Quest for the input player.
341    */
342    QuestHint* QuestManager::getHints(Quest* quest, PlayerInfo* player, int index)
343    {
344        std::list<QuestHint*> hints = quest->getHintsList();
345        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
346        {
347            if((*it)->isActive(player) && index-- == 0)
348                return *it;
349        }
350        return NULL;
351    }
352
353    /**
354    @brief
355        Get the parent-quest of the input Quest.
356    @param quest
357        The Quest to get the parent-quest of.
358    @return
359        Returns a pointer to the parent-quest of the input Quest.
360    */
361    Quest* QuestManager::getParentQuest(Quest* quest)
362    {
363        return quest->getParentQuest();
364    }
365
366    /**
367    @brief
368        Get the QuestDescription of the input Quest.
369    @param item
370        The Quest to get the QuestDescription of.
371    @return
372        Return a pointer ot the QuestDescription of the input Quest.
373    */
374    QuestDescription* QuestManager::getDescription(Quest* item)
375    {
376        return item->getDescription();
377    }
378
379    /**
380    @brief
381        Get the QuestDescription of the input QuestHint.
382    @param item
383        The QuestHint to get the QuestDescription of.
384    @return
385        Returns a pointer to the QuestDescription of the input QuestHint.
386    */
387    QuestDescription* QuestManager::getDescription(QuestHint* item)
388    {
389        return item->getDescription();
390    }
391
392    /**
393    @brief
394        Get the id of the input Quest.
395    @param item
396        The Quest to get the id of.
397    @return
398        Returns the id of the input Quest.
399    */
400    const std::string QuestManager::getId(Quest* item) const
401    {
402        return item->getId();
403    }
404
405    /**
406    @brief
407        Get the id of the input QuestHint.
408    @param item
409        The QuestHint to get the id of.
410    @return
411        Returns the id of the input QuestHint.
412    */
413    const std::string QuestManager::getId(QuestHint* item) const
414    {
415        return item->getId();
416    }
417
418    /**
419    @brief
420        Retrieve the player for a certain GUI.
421    @param guiName
422        The name of the GUI the player is retrieved for.
423    @return
424        Returns the player.
425    @todo
426        This very well might be outdated. So: Check if still needed, and update if necessary.
427    */
428    PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName)
429    {
430        PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName);
431        if(player == NULL)
432        {
433            orxout(internal_error, context::quests) << "GUIOverlay with name '" << guiName << "' has no player." << endl;
434            return NULL;
435        }
436
437        return player;
438    }
439
440}
Note: See TracBrowser for help on using the repository browser.