Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2989 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
Line 
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
29/**
30    @file QuestManager.cc
31    @brief Implementation of the QuestManager class.
32*/
33
34#include "OrxonoxStableHeaders.h"
35#include "QuestManager.h"
36
37#include "core/CoreIncludes.h"
38#include "core/ConsoleCommand.h"
39#include "core/input/InputManager.h"
40#include "util/Convert.h"
41
42#include "util/Exception.h"
43#include "gui/GUIManager.h"
44#include "Quest.h"
45#include "QuestHint.h"
46
47namespace orxonox
48{
49    //! Pointer to the current (and single) instance of this class.
50    /*static*/ QuestManager* QuestManager::singletonRef_s = NULL;
51    /*static*/ bool QuestManager::GUIOpen = false;
52
53    SetConsoleCommand(QuestManager, toggleQuestGUI, true);
54
55    /**
56    @brief
57        Constructor. Registers the object.
58    @todo
59        Is inheriting from BaseObject proper?
60    */
61    QuestManager::QuestManager()
62    {
63        RegisterRootObject(QuestManager);
64
65        assert(singletonRef_s == 0);
66        singletonRef_s = this;
67    }
68
69    /**
70    @brief
71        Destructor.
72    */
73    QuestManager::~QuestManager()
74    {
75
76    }
77
78    /**
79    @brief
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
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.
94    @param quest
95        The Quest that is to be registered.
96    @return
97        Returns true if successful, false if not.
98    */
99    bool QuestManager::registerQuest(Quest* quest)
100    {
101        if(quest == NULL) //!< Doh! Just as if there were actual quests behind NULL-pointers.
102        {
103            COUT(2) << "Registration of Quest in QuestManager failed, because inserted Quest-pointer was NULL." << std::endl;
104            return false;
105        }
106
107        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
108        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); //!< Inserting the Quest.
109
110        if(result.second) //!< If inserting was a success.
111        {
112            COUT(3) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl;
113            return true;
114        }
115        else
116        {
117           COUT(2) << "Quest with the same id was already present." << std::endl;
118           return false;
119        }
120    }
121
122    /**
123    @brief
124        Registers a QuestHint with the QuestManager to make it globally accessable.
125        Uses it's id to make sure to be able to be identify and retrieve it later.
126    @param hint
127        The QuestHint to be registered.
128    @return
129        Returns true if successful, false if not.
130    */
131    bool QuestManager::registerHint(QuestHint* hint)
132    {
133        if(hint == NULL) //!< Still not liking NULL-pointers.
134        {
135            COUT(2) << "Registration of QuestHint in QuestManager failed, because inserted QuestHint-pointer was NULL." << std::endl;
136            return false;
137        }
138
139        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
140        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); //!< Inserting the QuestHSint.
141
142        if(result.second) //!< If inserting was a success.
143        {
144            COUT(3) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl;
145            return true;
146        }
147        else
148        {
149           COUT(2) << "QuestHint with the same id was already present." << std::endl;
150           return false;
151        }
152    }
153
154    /**
155    @brief
156        Finds a Quest with the given id.
157    @param questId
158        The id of the Quest sought for.
159    @return
160        Returns a pointer to the Quest with the input id.
161        Returns NULL if there is no Quest with the given questId.
162    @throws
163        Throws an exception if the given questId is invalid.
164    */
165    Quest* QuestManager::findQuest(const std::string & questId)
166    {
167        if(!QuestItem::isId(questId)) //!< Check vor validity of the given id.
168        {
169            ThrowException(Argument, "Invalid questId.");
170        }
171
172        Quest* quest;
173        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
174        if (it != this->questMap_.end()) //!< If the Quest is registered.
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        }
183
184        return quest;
185
186    }
187
188    /**
189    @brief
190        Finds a QuestHint with the given id.
191    @param hintId
192        The id of the QuestHint sought for.
193    @return
194        Returns a pointer to the QuestHint with the input id.
195        Returns NULL if there is no QuestHint with the given hintId.
196    @throws
197        Throws an exception if the given hintId is invalid.
198    */
199    QuestHint* QuestManager::findHint(const std::string & hintId)
200    {
201        if(!QuestItem::isId(hintId)) //!< Check vor validity of the given id.
202        {
203            ThrowException(Argument, "Invalid hintId.");
204        }
205
206        QuestHint* hint;
207        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
208        if (it != this->hintMap_.end()) //!< If the QuestHint is registered.
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        }
217
218        return hint;
219
220    }
221
222    QuestContainer* QuestManager::getQuestTree(std::string & name)
223    {
224        GUIOverlay* gui = GUIManager::getInstance().getOverlay(name);
225
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
371}
Note: See TracBrowser for help on using the repository browser.