Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/release2012/src/modules/questsystem/QuestManager.cc @ 11479

Last change on this file since 11479 was 8952, checked in by dafrick, 13 years ago

Removing some unused variables and taking care of some other warnings (NULL as argument to non-pointer and depricated use of COUT).

  • Property svn:eol-style set to native
File size: 13.2 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/OrxAssert.h"
38#include "util/ScopedSingletonManager.h"
39#include "core/command/ConsoleCommand.h"
40#include "core/CoreIncludes.h"
41#include "core/GUIManager.h"
42#include "core/LuaState.h"
43
44#include "infos/PlayerInfo.h"
45
46#include "Quest.h"
47#include "QuestHint.h"
48#include "QuestItem.h"
49
50namespace orxonox
51{
52    ManageScopedSingleton(QuestManager, ScopeID::Root, false);
53
54    /**
55    @brief
56        Constructor. Registers the object.
57    @todo
58        Is inheriting from BaseObject proper?
59    */
60    QuestManager::QuestManager()
61    {
62        RegisterRootObject(QuestManager);
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        if(quest == NULL)
98        {
99            orxout(internal_error, context::quests) << "Quest pointer is NULL." << endl;
100            return false;
101        }
102
103        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
104        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); // Inserting the Quest.
105
106        if(result.second) // If inserting was a success.
107        {
108            quest->setRegistered();
109            orxout(verbose, context::quests) << "Quest with questId {" << quest->getId() << "} successfully inserted." << endl;
110            return true;
111        }
112        else
113        {
114           orxout(internal_warning, context::quests) << "Quest with the same id was already present." << endl;
115           return false;
116        }
117    }
118
119    /**
120    @brief
121        Unregisters a Quest in the QuestManager.
122    */
123    bool QuestManager::unregisterQuest(Quest* quest)
124    {
125        return this->questMap_.erase(quest->getId()) == 1;
126    }
127
128    /**
129    @brief
130        Registers a QuestHint with the QuestManager to make it globally accessible.
131        Uses it's id to make sure to be able to be identify and retrieve it later.
132    @param hint
133        The QuestHint to be registered.
134    @return
135        Returns true if successful, false if not.
136    */
137    bool QuestManager::registerHint(QuestHint* hint)
138    {
139        if(hint == NULL)
140        {
141            orxout(internal_error, context::quests) << "Quest pointer is NULL." << endl;
142            return false;
143        }
144
145        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
146        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); // Inserting the QuestHSint.
147
148        if(result.second) // If inserting was a success.
149        {
150            hint->setRegistered();
151            orxout(verbose, context::quests) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << endl;
152            return true;
153        }
154        else
155        {
156           orxout(internal_warning, context::quests) << "QuestHint with the same id was already present." << endl;
157           return false;
158        }
159    }
160
161    /**
162    @brief
163        Unregisters a QuestHint in the QuestManager.
164    */
165    bool QuestManager::unregisterHint(QuestHint* hint)
166    {
167        return this->hintMap_.erase(hint->getId()) == 1;
168    }
169
170    /**
171    @brief
172        Finds a Quest with the given id.
173    @param questId
174        The id of the Quest sought for.
175    @return
176        Returns a pointer to the Quest with the input id.
177        Returns NULL if there is no Quest with the given questId.
178    @throws
179        Throws an exception if the given questId is invalid.
180    */
181    Quest* QuestManager::findQuest(const std::string & questId)
182    {
183        if(questId == "") // Check for validity of the given id.
184            ThrowException(Argument, "Invalid questId.");
185
186        Quest* quest;
187        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
188        if (it != this->questMap_.end()) // If the Quest is registered.
189            quest = it->second;
190        else
191        {
192           quest = NULL;
193           orxout(internal_warning, context::quests) << "The quest with id {" << questId << "} is nowhere to be found." << endl;
194        }
195
196        return quest;
197    }
198
199    /**
200    @brief
201        Finds a QuestHint with the given id.
202    @param hintId
203        The id of the QuestHint sought for.
204    @return
205        Returns a pointer to the QuestHint with the input id.
206        Returns NULL if there is no QuestHint with the given hintId.
207    @throws
208        Throws an exception if the given hintId is invalid.
209    */
210    QuestHint* QuestManager::findHint(const std::string & hintId)
211    {
212        if(hintId == "") // Check for validity of the given id.
213            ThrowException(Argument, "Invalid hintId.");
214
215        QuestHint* hint;
216        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
217        if (it != this->hintMap_.end()) // If the QuestHint is registered.
218            hint = it->second;
219        else
220        {
221           hint = NULL;
222           orxout(internal_warning, context::quests) << "The hint with id {" << hintId << "} is nowhere to be found." << endl;
223        }
224
225        return hint;
226    }
227
228    /**
229    @brief
230        Get the number of Quests the input player has, that are root quests.
231    @param player
232        The player.
233    @return
234        Returns the number of Quests the input player has, that are root quests.
235    */
236    int QuestManager::getNumRootQuests(PlayerInfo* player)
237    {
238        int numQuests = 0;
239        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
240        {
241            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player))
242                numQuests++;
243        }
244        return numQuests;
245    }
246
247    /**
248    @brief
249        Get the index-th root quest of the input player.
250    @param player
251        The player.
252    @param index
253        The index of the root quest.
254    @return
255        Returns the index-th root quest of the input player.
256    */
257    Quest* QuestManager::getRootQuest(PlayerInfo* player, int index)
258    {
259        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
260        {
261            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player) && index-- == 0)
262                return it->second;
263        }
264        return NULL;
265    }
266
267    /**
268    @brief
269        Get the number of sub-quest of an input Quest for the input player.
270    @param quest
271        The quest to get the sub-quests of.
272    @param player
273        The player.
274    @return
275        Returns the number of sub-quest of an input Quest for the input player.
276    */
277    int QuestManager::getNumSubQuests(Quest* quest, PlayerInfo* player)
278    {
279        if(quest == NULL)
280            return this->getNumRootQuests(player);
281
282        std::list<Quest*> quests = quest->getSubQuestList();
283        int numQuests = 0;
284        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
285        {
286            if(!(*it)->isInactive(player))
287                numQuests++;
288        }
289        return numQuests;
290    }
291
292    /**
293    @brief
294        Get the index-th sub-quest of the input Quest for the input player.
295    @param quest
296        The Quest to get the sub-quest of.
297    @param player
298        The player.
299    @param index
300        The index of the sub-quest.
301    */
302    Quest* QuestManager::getSubQuest(Quest* quest, PlayerInfo* player, int index)
303    {
304        if(quest == NULL)
305            return this->getRootQuest(player, index);
306
307        std::list<Quest*> quests = quest->getSubQuestList();
308        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
309        {
310            if(!(*it)->isInactive(player) && index-- == 0)
311                return *it;
312        }
313        return NULL;
314    }
315
316    /**
317    @brief
318        Get the number of QuestHints of the input Quest for the input player.
319    @param quest
320        The quest to get the hints of.
321    @param player
322        The player.
323    @return
324        Returns the number of QuestHints of the input Quest for the input player.
325    */
326    int QuestManager::getNumHints(Quest* quest, PlayerInfo* player)
327    {
328        std::list<QuestHint*> hints = quest->getHintsList();
329        int numHints = 0;
330        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
331        {
332            if((*it)->isActive(player))
333                numHints++;
334        }
335        return numHints;
336    }
337
338    /**
339    @brief
340        Get the index-th QuestHint of the input Quest for the input player.
341    @param quest
342        The Quest to get the QuestHint of.
343    @param player
344        The player.
345    @param index
346        The index of the QuestHint.
347    @return
348        Returns a pointer to the index-th QuestHint of the input Quest for the input player.
349    */
350    QuestHint* QuestManager::getHints(Quest* quest, PlayerInfo* player, int index)
351    {
352        std::list<QuestHint*> hints = quest->getHintsList();
353        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
354        {
355            if((*it)->isActive(player) && index-- == 0)
356                return *it;
357        }
358        return NULL;
359    }
360
361    /**
362    @brief
363        Get the parent-quest of the input Quest.
364    @param quest
365        The Quest to get the parent-quest of.
366    @return
367        Returns a pointer to the parent-quest of the input Quest.
368    */
369    Quest* QuestManager::getParentQuest(Quest* quest)
370    {
371        OrxAssert(quest, "The input Quest is NULL.");
372        return quest->getParentQuest();
373    }
374
375    /**
376    @brief
377        Get the QuestDescription of the input Quest.
378    @param item
379        The Quest to get the QuestDescription of.
380    @return
381        Return a pointer ot the QuestDescription of the input Quest.
382    */
383    QuestDescription* QuestManager::getDescription(Quest* item)
384    {
385        OrxAssert(item, "The input Quest is NULL.");
386        return item->getDescription();
387    }
388
389    /**
390    @brief
391        Get the QuestDescription of the input QuestHint.
392    @param item
393        The QuestHint to get the QuestDescription of.
394    @return
395        Returns a pointer to the QuestDescription of the input QuestHint.
396    */
397    QuestDescription* QuestManager::getDescription(QuestHint* item)
398    {
399        OrxAssert(item, "The input QuestHint is NULL.");
400        return item->getDescription();
401    }
402
403    /**
404    @brief
405        Get the id of the input Quest.
406    @param item
407        The Quest to get the id of.
408    @return
409        Returns the id of the input Quest.
410    */
411    const std::string QuestManager::getId(Quest* item) const
412    {
413        OrxAssert(item, "The input Quest is NULL.");
414        return item->getId();
415    }
416
417    /**
418    @brief
419        Get the id of the input QuestHint.
420    @param item
421        The QuestHint to get the id of.
422    @return
423        Returns the id of the input QuestHint.
424    */
425    const std::string QuestManager::getId(QuestHint* item) const
426    {
427        OrxAssert(item, "The input QuestHint is NULL.");
428        return item->getId();
429    }
430
431    /**
432    @brief
433        Retrieve the player for a certain GUI.
434    @param guiName
435        The name of the GUI the player is retrieved for.
436    @return
437        Returns the player.
438    @todo
439        This very well might be outdated. So: Check if still needed, and update if necessary.
440    */
441    PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName)
442    {
443        PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName);
444        if(player == NULL)
445        {
446            orxout(internal_error, context::quests) << "GUIOverlay with name '" << guiName << "' has no player." << endl;
447            return NULL;
448        }
449
450        return player;
451    }
452
453}
Note: See TracBrowser for help on using the repository browser.