Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/questsystem/QuestManager.cc @ 8887

Last change on this file since 8887 was 8858, checked in by landauf, 13 years ago

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

  • orxout levelname message
  • orxout_context levelname context message
  • shortcuts: log message, error message, warning message, status message, info message, debug message
  • Property svn:eol-style set to native
File size: 12.6 KB
RevLine 
[1996]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
[2261]29/**
[7456]30    @file QuestManager.cc
[2662]31    @brief Implementation of the QuestManager class.
[2261]32*/
33
[2105]34#include "QuestManager.h"
35
[3196]36#include "util/Exception.h"
[7284]37#include "util/ScopedSingletonManager.h"
[7456]38#include "core/command/ConsoleCommand.h"
[1996]39#include "core/CoreIncludes.h"
[5693]40#include "core/GUIManager.h"
[5755]41#include "core/LuaState.h"
[7456]42
[5748]43#include "infos/PlayerInfo.h"
[2105]44
[2095]45#include "Quest.h"
46#include "QuestHint.h"
[5748]47#include "QuestItem.h"
[1996]48
[2662]49namespace orxonox
50{
[5929]51    ManageScopedSingleton(QuestManager, ScopeID::Root, false);
[1996]52
[2261]53    /**
54    @brief
55        Constructor. Registers the object.
[2911]56    @todo
57        Is inheriting from BaseObject proper?
[2261]58    */
[2911]59    QuestManager::QuestManager()
[1996]60    {
[2911]61        RegisterRootObject(QuestManager);
[7456]62
[8858]63        orxout(internal_info, context::quests) << "QuestManager created." << endl;
[1996]64    }
[2092]65
[2261]66    /**
67    @brief
68        Destructor.
69    */
[1996]70    QuestManager::~QuestManager()
71    {
[8858]72        orxout(internal_info, context::quests) << "QuestManager destroyed." << endl;
[1996]73    }
74
75    /**
76    @brief
[5745]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
[8079]88        Registers a Quest with the QuestManager to make it globally accessible.
[2261]89        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]90    @param quest
[2261]91        The Quest that is to be registered.
[1996]92    @return
93        Returns true if successful, false if not.
94    */
[2911]95    bool QuestManager::registerQuest(Quest* quest)
[1996]96    {
[7552]97        assert(quest);
[2092]98
[2261]99        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
[7456]100        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); // Inserting the Quest.
[2092]101
[7456]102        if(result.second) // If inserting was a success.
[2068]103        {
[7163]104            quest->setRegistered();
[8858]105            orxout(verbose, context::quests) << "Quest with questId {" << quest->getId() << "} successfully inserted." << endl;
[2068]106            return true;
[2093]107        }
108        else
109        {
[8858]110           orxout(internal_warning, context::quests) << "Quest with the same id was already present." << endl;
[2093]111           return false;
112        }
[1996]113    }
[2092]114
[1996]115    /**
116    @brief
[7163]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
[8079]126        Registers a QuestHint with the QuestManager to make it globally accessible.
[2261]127        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]128    @param hint
129        The QuestHint to be registered.
130    @return
131        Returns true if successful, false if not.
132    */
[2911]133    bool QuestManager::registerHint(QuestHint* hint)
[1996]134    {
[7552]135        assert(hint);
[2092]136
[2261]137        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
[7456]138        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); // Inserting the QuestHSint.
[2092]139
[7456]140        if(result.second) // If inserting was a success.
[2068]141        {
[7163]142            hint->setRegistered();
[8858]143            orxout(verbose, context::quests) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << endl;
[2068]144            return true;
[2093]145        }
146        else
147        {
[8858]148           orxout(internal_warning, context::quests) << "QuestHint with the same id was already present." << endl;
[2093]149           return false;
150        }
[1996]151    }
[2092]152
[1996]153    /**
154    @brief
[7163]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
[2261]164        Finds a Quest with the given id.
[1996]165    @param questId
[2261]166        The id of the Quest sought for.
[1996]167    @return
[2261]168        Returns a pointer to the Quest with the input id.
169        Returns NULL if there is no Quest with the given questId.
[2068]170    @throws
171        Throws an exception if the given questId is invalid.
[1996]172    */
[2911]173    Quest* QuestManager::findQuest(const std::string & questId)
[1996]174    {
[8079]175        if(questId == "") // Check for validity of the given id.
[2068]176            ThrowException(Argument, "Invalid questId.");
[2092]177
[1996]178        Quest* quest;
[2911]179        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
[7456]180        if (it != this->questMap_.end()) // If the Quest is registered.
[2093]181            quest = it->second;
182        else
183        {
184           quest = NULL;
[8858]185           orxout(internal_warning, context::quests) << "The quest with id {" << questId << "} is nowhere to be found." << endl;
[2093]186        }
[2092]187
[2093]188        return quest;
[1996]189    }
[2092]190
[1996]191    /**
192    @brief
[2261]193        Finds a QuestHint with the given id.
[1996]194    @param hintId
[2261]195        The id of the QuestHint sought for.
[1996]196    @return
[2261]197        Returns a pointer to the QuestHint with the input id.
198        Returns NULL if there is no QuestHint with the given hintId.
[2068]199    @throws
200        Throws an exception if the given hintId is invalid.
[1996]201    */
[2911]202    QuestHint* QuestManager::findHint(const std::string & hintId)
[1996]203    {
[8079]204        if(hintId == "") // Check for validity of the given id.
[2068]205            ThrowException(Argument, "Invalid hintId.");
[2092]206
[1996]207        QuestHint* hint;
[2911]208        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
[7456]209        if (it != this->hintMap_.end()) // If the QuestHint is registered.
[2093]210            hint = it->second;
211        else
212        {
213           hint = NULL;
[8858]214           orxout(internal_warning, context::quests) << "The hint with id {" << hintId << "} is nowhere to be found." << endl;
[2093]215        }
[2092]216
[2093]217        return hint;
[1996]218    }
219
[7456]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)
[2963]229    {
[7163]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    }
[5693]238
[7456]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)
[7163]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    }
[5693]258
[7456]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    */
[7163]269    int QuestManager::getNumSubQuests(Quest* quest, PlayerInfo* player)
270    {
[8706]271        if(quest == NULL)
272            return this->getNumRootQuests(player);
273
[7163]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;
[2963]282    }
283
[7456]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    */
[7163]294    Quest* QuestManager::getSubQuest(Quest* quest, PlayerInfo* player, int index)
295    {
[8706]296        if(quest == NULL)
297            return this->getRootQuest(player, index);
298
[7163]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
[7456]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.
[8706]315    @return
316        Returns the number of QuestHints of the input Quest for the input player.
[7456]317    */
[7163]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
[7456]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.
[8706]339    @return
340        Returns a pointer to the index-th QuestHint of the input Quest for the input player.
[7456]341    */
[7163]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
[7456]353    /**
354    @brief
[8706]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
[7456]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    */
[7163]374    QuestDescription* QuestManager::getDescription(Quest* item)
375    {
376        return item->getDescription();
377    }
378
[7456]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    */
[7163]387    QuestDescription* QuestManager::getDescription(QuestHint* item)
388    {
389        return item->getDescription();
390    }
391
[2993]392    /**
393    @brief
[8706]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
[5745]420        Retrieve the player for a certain GUI.
421    @param guiName
422        The name of the GUI the player is retrieved for.
[2993]423    @return
[5745]424        Returns the player.
425    @todo
426        This very well might be outdated. So: Check if still needed, and update if necessary.
[2993]427    */
[6536]428    PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName)
[2963]429    {
[5745]430        PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName);
431        if(player == NULL)
[2963]432        {
[8858]433            orxout(internal_error, context::quests) << "GUIOverlay with name '" << guiName << "' has no player." << endl;
[2993]434            return NULL;
435        }
[5693]436
[5745]437        return player;
[2963]438    }
439
[1996]440}
Note: See TracBrowser for help on using the repository browser.