Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7536 was 7456, checked in by dafrick, 14 years ago

Reviewing documentation fo Questsystem, moving documentation fully into doxygen.
Added some files to modules they belong to.

  • Property svn:eol-style set to native
File size: 12.0 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
[7456]49#include "ToluaBindQuestsystem.h"
50
[2662]51namespace orxonox
52{
[5755]53    // Register tolua_open function when loading the library
54    DeclareToluaInterface(Questsystem);
55
[5929]56    ManageScopedSingleton(QuestManager, ScopeID::Root, false);
[1996]57
[2261]58    /**
59    @brief
60        Constructor. Registers the object.
[2911]61    @todo
62        Is inheriting from BaseObject proper?
[2261]63    */
[2911]64    QuestManager::QuestManager()
[1996]65    {
[2911]66        RegisterRootObject(QuestManager);
[7456]67
68        COUT(3) << "QuestManager created." << std::endl;
[1996]69    }
[2092]70
[2261]71    /**
72    @brief
73        Destructor.
74    */
[1996]75    QuestManager::~QuestManager()
76    {
[7456]77        COUT(3) << "QuestManager destroyed." << std::endl;
[1996]78    }
79
80    /**
81    @brief
[5745]82        Retreive all Quests.
83    @return
84        Returns a map with all Quests indexed by their id's.
85    */
86    std::map<std::string, Quest*> & QuestManager::getQuests(void)
87    {
88        return this->questMap_;
89    }
90
91    /**
92    @brief
[2261]93        Registers a Quest with the QuestManager to make it globally accessable.
94        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]95    @param quest
[2261]96        The Quest that is to be registered.
[1996]97    @return
98        Returns true if successful, false if not.
99    */
[2911]100    bool QuestManager::registerQuest(Quest* quest)
[1996]101    {
[7456]102        //TODO: Replace with assert.
103        if(quest == NULL) // Doh! Just as if there were actual quests behind NULL-pointers.
[2068]104        {
105            COUT(2) << "Registration of Quest in QuestManager failed, because inserted Quest-pointer was NULL." << std::endl;
106            return false;
[2093]107        }
[2092]108
[2261]109        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
[7456]110        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); // Inserting the Quest.
[2092]111
[7456]112        if(result.second) // If inserting was a success.
[2068]113        {
[7163]114            quest->setRegistered();
115            COUT(4) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl;
[2068]116            return true;
[2093]117        }
118        else
119        {
120           COUT(2) << "Quest with the same id was already present." << std::endl;
121           return false;
122        }
[1996]123    }
[2092]124
[1996]125    /**
126    @brief
[7163]127        Unregisters a Quest in the QuestManager.
128    */
129    bool QuestManager::unregisterQuest(Quest* quest)
130    {
131        return this->questMap_.erase(quest->getId()) == 1;
132    }
133
134    /**
135    @brief
[1996]136        Registers a QuestHint with the QuestManager to make it globally accessable.
[2261]137        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]138    @param hint
139        The QuestHint to be registered.
140    @return
141        Returns true if successful, false if not.
142    */
[2911]143    bool QuestManager::registerHint(QuestHint* hint)
[1996]144    {
[7456]145        //TODO: Replace with assert.
146        if(hint == NULL) // Still not liking NULL-pointers.
[2068]147        {
148            COUT(2) << "Registration of QuestHint in QuestManager failed, because inserted QuestHint-pointer was NULL." << std::endl;
149            return false;
150        }
[2092]151
[2261]152        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
[7456]153        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); // Inserting the QuestHSint.
[2092]154
[7456]155        if(result.second) // If inserting was a success.
[2068]156        {
[7163]157            hint->setRegistered();
158            COUT(4) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl;
[2068]159            return true;
[2093]160        }
161        else
162        {
163           COUT(2) << "QuestHint with the same id was already present." << std::endl;
164           return false;
165        }
[1996]166    }
[2092]167
[1996]168    /**
169    @brief
[7163]170        Unregisters a QuestHint in the QuestManager.
171    */
172    bool QuestManager::unregisterHint(QuestHint* hint)
173    {
174        return this->hintMap_.erase(hint->getId()) == 1;
175    }
176
177    /**
178    @brief
[2261]179        Finds a Quest with the given id.
[1996]180    @param questId
[2261]181        The id of the Quest sought for.
[1996]182    @return
[2261]183        Returns a pointer to the Quest with the input id.
184        Returns NULL if there is no Quest with the given questId.
[2068]185    @throws
186        Throws an exception if the given questId is invalid.
[1996]187    */
[2911]188    Quest* QuestManager::findQuest(const std::string & questId)
[1996]189    {
[7456]190        if(questId.compare(BLANKSTRING) == 1) // Check vor validity of the given id.
[2068]191            ThrowException(Argument, "Invalid questId.");
[2092]192
[1996]193        Quest* quest;
[2911]194        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
[7456]195        if (it != this->questMap_.end()) // If the Quest is registered.
[2093]196            quest = it->second;
197        else
198        {
199           quest = NULL;
200           COUT(2) << "The quest with id {" << questId << "} is nowhere to be found." << std::endl;
201        }
[2092]202
[2093]203        return quest;
[1996]204    }
[2092]205
[1996]206    /**
207    @brief
[2261]208        Finds a QuestHint with the given id.
[1996]209    @param hintId
[2261]210        The id of the QuestHint sought for.
[1996]211    @return
[2261]212        Returns a pointer to the QuestHint with the input id.
213        Returns NULL if there is no QuestHint with the given hintId.
[2068]214    @throws
215        Throws an exception if the given hintId is invalid.
[1996]216    */
[2911]217    QuestHint* QuestManager::findHint(const std::string & hintId)
[1996]218    {
[7456]219        if(hintId.compare(BLANKSTRING) == 1) // Check vor validity of the given id.
[2068]220            ThrowException(Argument, "Invalid hintId.");
[2092]221
[1996]222        QuestHint* hint;
[2911]223        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
[7456]224        if (it != this->hintMap_.end()) // If the QuestHint is registered.
[2093]225            hint = it->second;
226        else
227        {
228           hint = NULL;
229           COUT(2) << "The hint with id {" << hintId << "} is nowhere to be found." << std::endl;
230        }
[2092]231
[2093]232        return hint;
[1996]233    }
234
[7456]235    /**
236    @brief
237        Get the number of Quests the input player has, that are root quests.
238    @param player
239        The player.
240    @return
241        Returns the number of Quests the input player has, that are root quests.
242    */
243    int QuestManager::getNumRootQuests(PlayerInfo* player)
[2963]244    {
[7163]245        int numQuests = 0;
246        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
247        {
248            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player))
249                numQuests++;
250        }
251        return numQuests;
252    }
[5693]253
[7456]254    /**
255    @brief
256        Get the index-th root quest of the input player.
257    @param player
258        The player.
259    @param index
260        The index of the root quest.
261    @return
262        Returns the index-th root quest of the input player.
263    */
264    Quest* QuestManager::getRootQuest(PlayerInfo* player, int index)
[7163]265    {
266        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
267        {
268            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player) && index-- == 0)
269                return it->second;
270        }
271        return NULL;
272    }
[5693]273
[7456]274    /**
275    @brief
276        Get the number of sub-quest of an input Quest for the input player.
277    @param quest
278        The quest to get the sub-quests of.
279    @param player
280        The player.
281    @return
282        Returns the number of sub-quest of an input Quest for the input player.
283    */
[7163]284    int QuestManager::getNumSubQuests(Quest* quest, PlayerInfo* player)
285    {
286        std::list<Quest*> quests = quest->getSubQuestList();
287        int numQuests = 0;
288        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
289        {
290            if(!(*it)->isInactive(player))
291                numQuests++;
292        }
293        return numQuests;
[2963]294    }
295
[7456]296    /**
297    @brief
298        Get the index-th sub-quest of the input Quest for the input player.
299    @param quest
300        The Quest to get the sub-quest of.
301    @param player
302        The player.
303    @param index
304        The index of the sub-quest.
305    */
[7163]306    Quest* QuestManager::getSubQuest(Quest* quest, PlayerInfo* player, int index)
307    {
308        std::list<Quest*> quests = quest->getSubQuestList();
309        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
310        {
311            if(!(*it)->isInactive(player) && index-- == 0)
312                return *it;
313        }
314        return NULL;
315    }
316
[7456]317    /**
318    @brief
319        Get the number of QuestHints of the input Quest for the input player.
320    @param quest
321        The quest to get the hints of.
322    @param player
323        The player.
324    @return Returns the number of QuestHints of the input Quest for the input player.
325    */
[7163]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
[7456]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    */
[7163]348    QuestHint* QuestManager::getHints(Quest* quest, PlayerInfo* player, int index)
349    {
350        std::list<QuestHint*> hints = quest->getHintsList();
351        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
352        {
353            if((*it)->isActive(player) && index-- == 0)
354                return *it;
355        }
356        return NULL;
357    }
358
[7456]359    /**
360    @brief
361        Get the QuestDescription of the input Quest.
362    @param item
363        The Quest to get the QuestDescription of.
364    @return
365        Return a pointer ot the QuestDescription of the input Quest.
366    */
[7163]367    QuestDescription* QuestManager::getDescription(Quest* item)
368    {
369        return item->getDescription();
370    }
371
[7456]372    /**
373    @brief
374        Get the QuestDescription of the input QuestHint.
375    @param item
376        The QuestHint to get the QuestDescription of.
377    @return
378        Returns a pointer to the QuestDescription of the input QuestHint.
379    */
[7163]380    QuestDescription* QuestManager::getDescription(QuestHint* item)
381    {
382        return item->getDescription();
383    }
384
[2993]385    /**
386    @brief
[5745]387        Retrieve the player for a certain GUI.
388    @param guiName
389        The name of the GUI the player is retrieved for.
[2993]390    @return
[5745]391        Returns the player.
392    @todo
393        This very well might be outdated. So: Check if still needed, and update if necessary.
[2993]394    */
[6536]395    PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName)
[2963]396    {
[5745]397        PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName);
398        if(player == NULL)
[2963]399        {
[5745]400            COUT(1) << "Error: GUIOverlay with name '" << guiName << "' has no player." << std::endl;
[2993]401            return NULL;
402        }
[5693]403
[5745]404        return player;
[2963]405    }
406
[1996]407}
Note: See TracBrowser for help on using the repository browser.