Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3172 was 3110, checked in by rgrieder, 16 years ago

Removed old msvc specific support for precompiled header files.

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