Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2985 was 2963, checked in by dafrick, 16 years ago

Some Quest stuff, and the rest for the GUIOverlay I failed to commit before.

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