Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/quest/QuestManager.cc @ 3201

Last change on this file since 3201 was 3196, checked in by rgrieder, 16 years ago

Merged pch branch back to trunk.

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