Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7589 was 7552, checked in by dafrick, 14 years ago

Resolving some TODOs and doing some additional cleanup. Almost done now…

  • Property svn:eol-style set to native
File size: 11.4 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    {
[7552]102        assert(quest);
[2092]103
[2261]104        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
[7456]105        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); // Inserting the Quest.
[2092]106
[7456]107        if(result.second) // If inserting was a success.
[2068]108        {
[7163]109            quest->setRegistered();
110            COUT(4) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl;
[2068]111            return true;
[2093]112        }
113        else
114        {
115           COUT(2) << "Quest with the same id was already present." << std::endl;
116           return false;
117        }
[1996]118    }
[2092]119
[1996]120    /**
121    @brief
[7163]122        Unregisters a Quest in the QuestManager.
123    */
124    bool QuestManager::unregisterQuest(Quest* quest)
125    {
126        return this->questMap_.erase(quest->getId()) == 1;
127    }
128
129    /**
130    @brief
[1996]131        Registers a QuestHint with the QuestManager to make it globally accessable.
[2261]132        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]133    @param hint
134        The QuestHint to be registered.
135    @return
136        Returns true if successful, false if not.
137    */
[2911]138    bool QuestManager::registerHint(QuestHint* hint)
[1996]139    {
[7552]140        assert(hint);
[2092]141
[2261]142        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
[7456]143        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); // Inserting the QuestHSint.
[2092]144
[7456]145        if(result.second) // If inserting was a success.
[2068]146        {
[7163]147            hint->setRegistered();
148            COUT(4) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl;
[2068]149            return true;
[2093]150        }
151        else
152        {
153           COUT(2) << "QuestHint with the same id was already present." << std::endl;
154           return false;
155        }
[1996]156    }
[2092]157
[1996]158    /**
159    @brief
[7163]160        Unregisters a QuestHint in the QuestManager.
161    */
162    bool QuestManager::unregisterHint(QuestHint* hint)
163    {
164        return this->hintMap_.erase(hint->getId()) == 1;
165    }
166
167    /**
168    @brief
[2261]169        Finds a Quest with the given id.
[1996]170    @param questId
[2261]171        The id of the Quest sought for.
[1996]172    @return
[2261]173        Returns a pointer to the Quest with the input id.
174        Returns NULL if there is no Quest with the given questId.
[2068]175    @throws
176        Throws an exception if the given questId is invalid.
[1996]177    */
[2911]178    Quest* QuestManager::findQuest(const std::string & questId)
[1996]179    {
[7456]180        if(questId.compare(BLANKSTRING) == 1) // Check vor validity of the given id.
[2068]181            ThrowException(Argument, "Invalid questId.");
[2092]182
[1996]183        Quest* quest;
[2911]184        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
[7456]185        if (it != this->questMap_.end()) // If the Quest is registered.
[2093]186            quest = it->second;
187        else
188        {
189           quest = NULL;
190           COUT(2) << "The quest with id {" << questId << "} is nowhere to be found." << std::endl;
191        }
[2092]192
[2093]193        return quest;
[1996]194    }
[2092]195
[1996]196    /**
197    @brief
[2261]198        Finds a QuestHint with the given id.
[1996]199    @param hintId
[2261]200        The id of the QuestHint sought for.
[1996]201    @return
[2261]202        Returns a pointer to the QuestHint with the input id.
203        Returns NULL if there is no QuestHint with the given hintId.
[2068]204    @throws
205        Throws an exception if the given hintId is invalid.
[1996]206    */
[2911]207    QuestHint* QuestManager::findHint(const std::string & hintId)
[1996]208    {
[7456]209        if(hintId.compare(BLANKSTRING) == 1) // Check vor validity of the given id.
[2068]210            ThrowException(Argument, "Invalid hintId.");
[2092]211
[1996]212        QuestHint* hint;
[2911]213        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
[7456]214        if (it != this->hintMap_.end()) // If the QuestHint is registered.
[2093]215            hint = it->second;
216        else
217        {
218           hint = NULL;
219           COUT(2) << "The hint with id {" << hintId << "} is nowhere to be found." << std::endl;
220        }
[2092]221
[2093]222        return hint;
[1996]223    }
224
[7456]225    /**
226    @brief
227        Get the number of Quests the input player has, that are root quests.
228    @param player
229        The player.
230    @return
231        Returns the number of Quests the input player has, that are root quests.
232    */
233    int QuestManager::getNumRootQuests(PlayerInfo* player)
[2963]234    {
[7163]235        int numQuests = 0;
236        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
237        {
238            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player))
239                numQuests++;
240        }
241        return numQuests;
242    }
[5693]243
[7456]244    /**
245    @brief
246        Get the index-th root quest of the input player.
247    @param player
248        The player.
249    @param index
250        The index of the root quest.
251    @return
252        Returns the index-th root quest of the input player.
253    */
254    Quest* QuestManager::getRootQuest(PlayerInfo* player, int index)
[7163]255    {
256        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
257        {
258            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player) && index-- == 0)
259                return it->second;
260        }
261        return NULL;
262    }
[5693]263
[7456]264    /**
265    @brief
266        Get the number of sub-quest of an input Quest for the input player.
267    @param quest
268        The quest to get the sub-quests of.
269    @param player
270        The player.
271    @return
272        Returns the number of sub-quest of an input Quest for the input player.
273    */
[7163]274    int QuestManager::getNumSubQuests(Quest* quest, PlayerInfo* player)
275    {
276        std::list<Quest*> quests = quest->getSubQuestList();
277        int numQuests = 0;
278        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
279        {
280            if(!(*it)->isInactive(player))
281                numQuests++;
282        }
283        return numQuests;
[2963]284    }
285
[7456]286    /**
287    @brief
288        Get the index-th sub-quest of the input Quest for the input player.
289    @param quest
290        The Quest to get the sub-quest of.
291    @param player
292        The player.
293    @param index
294        The index of the sub-quest.
295    */
[7163]296    Quest* QuestManager::getSubQuest(Quest* quest, PlayerInfo* player, int index)
297    {
298        std::list<Quest*> quests = quest->getSubQuestList();
299        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
300        {
301            if(!(*it)->isInactive(player) && index-- == 0)
302                return *it;
303        }
304        return NULL;
305    }
306
[7456]307    /**
308    @brief
309        Get the number of QuestHints of the input Quest for the input player.
310    @param quest
311        The quest to get the hints of.
312    @param player
313        The player.
314    @return Returns the number of QuestHints of the input Quest for the input player.
315    */
[7163]316    int QuestManager::getNumHints(Quest* quest, PlayerInfo* player)
317    {
318        std::list<QuestHint*> hints = quest->getHintsList();
319        int numHints = 0;
320        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
321        {
322            if((*it)->isActive(player))
323                numHints++;
324        }
325        return numHints;
326    }
327
[7456]328    /**
329    @brief
330        Get the index-th QuestHint of the input Quest for the input player.
331    @param quest
332        The Quest to get the QuestHint of.
333    @param player
334        The player.
335    @param index
336        The index of the QuestHint.
337    */
[7163]338    QuestHint* QuestManager::getHints(Quest* quest, PlayerInfo* player, int index)
339    {
340        std::list<QuestHint*> hints = quest->getHintsList();
341        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
342        {
343            if((*it)->isActive(player) && index-- == 0)
344                return *it;
345        }
346        return NULL;
347    }
348
[7456]349    /**
350    @brief
351        Get the QuestDescription of the input Quest.
352    @param item
353        The Quest to get the QuestDescription of.
354    @return
355        Return a pointer ot the QuestDescription of the input Quest.
356    */
[7163]357    QuestDescription* QuestManager::getDescription(Quest* item)
358    {
359        return item->getDescription();
360    }
361
[7456]362    /**
363    @brief
364        Get the QuestDescription of the input QuestHint.
365    @param item
366        The QuestHint to get the QuestDescription of.
367    @return
368        Returns a pointer to the QuestDescription of the input QuestHint.
369    */
[7163]370    QuestDescription* QuestManager::getDescription(QuestHint* item)
371    {
372        return item->getDescription();
373    }
374
[2993]375    /**
376    @brief
[5745]377        Retrieve the player for a certain GUI.
378    @param guiName
379        The name of the GUI the player is retrieved for.
[2993]380    @return
[5745]381        Returns the player.
382    @todo
383        This very well might be outdated. So: Check if still needed, and update if necessary.
[2993]384    */
[6536]385    PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName)
[2963]386    {
[5745]387        PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName);
388        if(player == NULL)
[2963]389        {
[5745]390            COUT(1) << "Error: GUIOverlay with name '" << guiName << "' has no player." << std::endl;
[2993]391            return NULL;
392        }
[5693]393
[5745]394        return player;
[2963]395    }
396
[1996]397}
Note: See TracBrowser for help on using the repository browser.