Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/quest/QuestManager.cc @ 3177

Last change on this file since 3177 was 3158, checked in by rgrieder, 16 years ago

Removed the filename part of all @file foo.cc because Doxygen chooses the (always correct) filename automatically.
Also removed a few <string> includes that are not necessary.

  • Property svn:eol-style set to native
File size: 10.9 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/**
[3158]30    @file
[2662]31    @brief Implementation of the QuestManager class.
[2261]32*/
33
[2105]34#include "QuestManager.h"
35
[1996]36#include "core/CoreIncludes.h"
[2963]37#include "core/ConsoleCommand.h"
38#include "core/input/InputManager.h"
[2105]39
[2963]40#include "gui/GUIManager.h"
[2993]41#include "objects/infos/PlayerInfo.h"
[2095]42#include "Quest.h"
43#include "QuestHint.h"
[1996]44
[2662]45namespace orxonox
46{
[2911]47    //! Pointer to the current (and single) instance of this class.
[2963]48    /*static*/ QuestManager* QuestManager::singletonRef_s = NULL;
[1996]49
[2261]50    /**
51    @brief
52        Constructor. Registers the object.
[2911]53    @todo
54        Is inheriting from BaseObject proper?
[2261]55    */
[2911]56    QuestManager::QuestManager()
[1996]57    {
[2911]58        RegisterRootObject(QuestManager);
59
60        assert(singletonRef_s == 0);
61        singletonRef_s = this;
[1996]62    }
[2092]63
[2261]64    /**
65    @brief
66        Destructor.
67    */
[1996]68    QuestManager::~QuestManager()
69    {
[2092]70
[1996]71    }
72
73    /**
74    @brief
[2911]75        Returns a reference to the current (and single) instance of the QuestManager, and creates one if there isn't one to begin with.
76    @return
77        Returns a reference to the single instance of the Quest Manager.
78    */
79    /*static*/ QuestManager & QuestManager::getInstance()
80    {
81        assert(singletonRef_s);
82        return *singletonRef_s;
83    }
84
85    /**
86    @brief
[2261]87        Registers a Quest with the QuestManager to make it globally accessable.
88        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]89    @param quest
[2261]90        The Quest that is to be registered.
[1996]91    @return
92        Returns true if successful, false if not.
93    */
[2911]94    bool QuestManager::registerQuest(Quest* quest)
[1996]95    {
[2261]96        if(quest == NULL) //!< Doh! Just as if there were actual quests behind NULL-pointers.
[2068]97        {
98            COUT(2) << "Registration of Quest in QuestManager failed, because inserted Quest-pointer was NULL." << std::endl;
99            return false;
[2093]100        }
[2092]101
[2261]102        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
[2911]103        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); //!< Inserting the Quest.
[2092]104
[2261]105        if(result.second) //!< If inserting was a success.
[2068]106        {
107            COUT(3) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl;
108            return true;
[2093]109        }
110        else
111        {
112           COUT(2) << "Quest with the same id was already present." << std::endl;
113           return false;
114        }
[1996]115    }
[2092]116
[1996]117    /**
118    @brief
119        Registers a QuestHint with the QuestManager to make it globally accessable.
[2261]120        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]121    @param hint
122        The QuestHint to be registered.
123    @return
124        Returns true if successful, false if not.
125    */
[2911]126    bool QuestManager::registerHint(QuestHint* hint)
[1996]127    {
[2261]128        if(hint == NULL) //!< Still not liking NULL-pointers.
[2068]129        {
130            COUT(2) << "Registration of QuestHint in QuestManager failed, because inserted QuestHint-pointer was NULL." << std::endl;
131            return false;
132        }
[2092]133
[2261]134        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
[2911]135        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); //!< Inserting the QuestHSint.
[2092]136
[2261]137        if(result.second) //!< If inserting was a success.
[2068]138        {
139            COUT(3) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl;
140            return true;
[2093]141        }
142        else
143        {
144           COUT(2) << "QuestHint with the same id was already present." << std::endl;
145           return false;
146        }
[1996]147    }
[2092]148
[1996]149    /**
150    @brief
[2261]151        Finds a Quest with the given id.
[1996]152    @param questId
[2261]153        The id of the Quest sought for.
[1996]154    @return
[2261]155        Returns a pointer to the Quest with the input id.
156        Returns NULL if there is no Quest with the given questId.
[2068]157    @throws
158        Throws an exception if the given questId is invalid.
[1996]159    */
[2911]160    Quest* QuestManager::findQuest(const std::string & questId)
[1996]161    {
[2261]162        if(!QuestItem::isId(questId)) //!< Check vor validity of the given id.
[2093]163        {
[2068]164            ThrowException(Argument, "Invalid questId.");
[2093]165        }
[2092]166
[1996]167        Quest* quest;
[2911]168        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
169        if (it != this->questMap_.end()) //!< If the Quest is registered.
[2093]170        {
171            quest = it->second;
172        }
173        else
174        {
175           quest = NULL;
176           COUT(2) << "The quest with id {" << questId << "} is nowhere to be found." << std::endl;
177        }
[2092]178
[2093]179        return quest;
[1996]180
181    }
[2092]182
[1996]183    /**
184    @brief
[2261]185        Finds a QuestHint with the given id.
[1996]186    @param hintId
[2261]187        The id of the QuestHint sought for.
[1996]188    @return
[2261]189        Returns a pointer to the QuestHint with the input id.
190        Returns NULL if there is no QuestHint with the given hintId.
[2068]191    @throws
192        Throws an exception if the given hintId is invalid.
[1996]193    */
[2911]194    QuestHint* QuestManager::findHint(const std::string & hintId)
[1996]195    {
[2261]196        if(!QuestItem::isId(hintId)) //!< Check vor validity of the given id.
[2093]197        {
[2068]198            ThrowException(Argument, "Invalid hintId.");
[2093]199        }
[2092]200
[1996]201        QuestHint* hint;
[2911]202        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
203        if (it != this->hintMap_.end()) //!< If the QuestHint is registered.
[2093]204        {
205            hint = it->second;
206        }
207        else
208        {
209           hint = NULL;
210           COUT(2) << "The hint with id {" << hintId << "} is nowhere to be found." << std::endl;
211        }
[2092]212
[2093]213        return hint;
[1996]214
215    }
216
[2993]217    /**
218    @brief
219       
220    @param name
221    @return
222    */
[2963]223    QuestContainer* QuestManager::getQuestTree(std::string & name)
224    {
225        GUIOverlay* gui = GUIManager::getInstance().getOverlay(name);
[2092]226
[2963]227        PlayerInfo* player;
228        if(gui == NULL)
229        {
[2993]230            COUT(1) << "Error: No GUIOverlay with the given name '" << name << "' present." << std::endl;
[2963]231            return NULL;
232        }
[2993]233        BaseObject* obj = gui->getOwner();
234        if(obj == NULL)
235        {
236            COUT(1) << "Error: GUIOverlay has no owner. " << std::endl;
237            return NULL;
238        }
239        player = dynamic_cast<PlayerInfo*>(obj);
[2963]240   
241        QuestContainer* root = NULL;
242        QuestContainer* current = NULL;
243       
[2993]244        std::list<Quest*>* rootQuests = new std::list<Quest*>();
245        getRootQuests(player, *rootQuests);
[2963]246       
[2993]247        for(std::list<Quest*>::iterator it = rootQuests->begin(); it != rootQuests->end(); it++)
[2963]248        {
[2993]249            QuestContainer* container = addSubQuest(*it, player);
[2963]250
251            if(root == NULL)
252            {
253                root = container;
254            }
255            else
256            {
257                current->next = container;
258            }
259           
260            current = container;
261
262        }
263        if(current != NULL)
264            current->next = NULL;
265
[2993]266        delete rootQuests;
[2963]267
268        return root;
269    }
270
[2993]271    /**
272    @brief
273       
274    @param player
275    @param list
276    @return
277    */
[2963]278    void QuestManager::getRootQuests(const PlayerInfo* player, std::list<Quest*> & list)
279    {
280        for(std::map<std::string, Quest*>::iterator it=this->questMap_.begin(); it!=this->questMap_.end(); it++)
281        {
282            Quest* quest = (*it).second;
283            if(quest->getParentQuest() == NULL && !quest->isInactive(player))
284            {
285                list.push_back(quest);
286            }
287        }
288    }
289
[2993]290    /**
291    @brief
292       
293    @param quest
294    @param player
295    @return
296    */
297    QuestContainer* QuestManager::addSubQuest(Quest* quest, const PlayerInfo* player)
[2963]298    {
[2993]299        if(quest == NULL)
300            return NULL;
301
302        QuestContainer* container = new QuestContainer;
303        container->description = quest->getDescription();
304        container->hint = addHints(quest, player);
305
306        if(quest->isActive(player))
307        {
308            container->status = "active";
309        }
310        else if(quest->isCompleted(player))
311        {
312            container->status = "completed";
313        }
314        else if(quest->isFailed(player))
315        {
316            container->status = "failed";
317        }
318        else
319        {
320            container->status = "";
321            COUT(1) << "An error occured. A Quest of un-specified status wanted to be displayed." << std::endl;
322        }
323       
324        std::list<Quest*> quests = quest->getSubQuestList();
[2963]325        QuestContainer* current = NULL;
326        QuestContainer* first = NULL;
327        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
328        {
329            Quest* subQuest = *it;
330            if(!subQuest->isInactive(player))
331            {
[2993]332                QuestContainer* subContainer = addSubQuest(subQuest, player);
[2963]333
334                if(first == NULL)
335                {
[2993]336                    first = subContainer;
[2963]337                }
338                else
339                {
[2993]340                    current->next = subContainer;
[2963]341                }
342               
[2993]343                current = subContainer;
[2963]344            }
345        }
346        if(current != NULL)
347            current->next = NULL;
348        container->subQuests = first;
349       
[2993]350        return container;
[2963]351    }
352
[2993]353    /**
354    @brief
355       
356    @param quest
357    @param player
358    @return
359    */
360    HintContainer* QuestManager::addHints(Quest* quest, const PlayerInfo* player)
[2963]361    {
362        HintContainer* current = NULL;
363        HintContainer* first = NULL;
364
365        std::list<QuestHint*> hints = quest->getHintsList();
366        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
367        {
368            if((*it)->isActive(player))
369            {
370                HintContainer* hint = new HintContainer;
371                hint->description = (*it)->getDescription();
372
373                if(first == NULL)
374                {
375                    first = hint;
376                }
377                else
378                {
379                    current->next = hint;
380                }
381               
382                current = hint;
383            }
384        }
385
386        if(current != NULL)
387            current->next = NULL;
[2993]388        return first;
[2963]389    }
390
391
[1996]392}
Note: See TracBrowser for help on using the repository browser.