Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3158 was 3158, checked in by rgrieder, 15 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
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
31    @brief Implementation of the QuestManager class.
32*/
33
34#include "QuestManager.h"
35
36#include "core/CoreIncludes.h"
37#include "core/ConsoleCommand.h"
38#include "core/input/InputManager.h"
39
40#include "gui/GUIManager.h"
41#include "objects/infos/PlayerInfo.h"
42#include "Quest.h"
43#include "QuestHint.h"
44
45namespace orxonox
46{
47    //! Pointer to the current (and single) instance of this class.
48    /*static*/ QuestManager* QuestManager::singletonRef_s = NULL;
49
50    /**
51    @brief
52        Constructor. Registers the object.
53    @todo
54        Is inheriting from BaseObject proper?
55    */
56    QuestManager::QuestManager()
57    {
58        RegisterRootObject(QuestManager);
59
60        assert(singletonRef_s == 0);
61        singletonRef_s = this;
62    }
63
64    /**
65    @brief
66        Destructor.
67    */
68    QuestManager::~QuestManager()
69    {
70
71    }
72
73    /**
74    @brief
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
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.
89    @param quest
90        The Quest that is to be registered.
91    @return
92        Returns true if successful, false if not.
93    */
94    bool QuestManager::registerQuest(Quest* quest)
95    {
96        if(quest == NULL) //!< Doh! Just as if there were actual quests behind NULL-pointers.
97        {
98            COUT(2) << "Registration of Quest in QuestManager failed, because inserted Quest-pointer was NULL." << std::endl;
99            return false;
100        }
101
102        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
103        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); //!< Inserting the Quest.
104
105        if(result.second) //!< If inserting was a success.
106        {
107            COUT(3) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl;
108            return true;
109        }
110        else
111        {
112           COUT(2) << "Quest with the same id was already present." << std::endl;
113           return false;
114        }
115    }
116
117    /**
118    @brief
119        Registers a QuestHint with the QuestManager to make it globally accessable.
120        Uses it's id to make sure to be able to be identify and retrieve it later.
121    @param hint
122        The QuestHint to be registered.
123    @return
124        Returns true if successful, false if not.
125    */
126    bool QuestManager::registerHint(QuestHint* hint)
127    {
128        if(hint == NULL) //!< Still not liking NULL-pointers.
129        {
130            COUT(2) << "Registration of QuestHint in QuestManager failed, because inserted QuestHint-pointer was NULL." << std::endl;
131            return false;
132        }
133
134        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
135        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); //!< Inserting the QuestHSint.
136
137        if(result.second) //!< If inserting was a success.
138        {
139            COUT(3) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl;
140            return true;
141        }
142        else
143        {
144           COUT(2) << "QuestHint with the same id was already present." << std::endl;
145           return false;
146        }
147    }
148
149    /**
150    @brief
151        Finds a Quest with the given id.
152    @param questId
153        The id of the Quest sought for.
154    @return
155        Returns a pointer to the Quest with the input id.
156        Returns NULL if there is no Quest with the given questId.
157    @throws
158        Throws an exception if the given questId is invalid.
159    */
160    Quest* QuestManager::findQuest(const std::string & questId)
161    {
162        if(!QuestItem::isId(questId)) //!< Check vor validity of the given id.
163        {
164            ThrowException(Argument, "Invalid questId.");
165        }
166
167        Quest* quest;
168        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
169        if (it != this->questMap_.end()) //!< If the Quest is registered.
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        }
178
179        return quest;
180
181    }
182
183    /**
184    @brief
185        Finds a QuestHint with the given id.
186    @param hintId
187        The id of the QuestHint sought for.
188    @return
189        Returns a pointer to the QuestHint with the input id.
190        Returns NULL if there is no QuestHint with the given hintId.
191    @throws
192        Throws an exception if the given hintId is invalid.
193    */
194    QuestHint* QuestManager::findHint(const std::string & hintId)
195    {
196        if(!QuestItem::isId(hintId)) //!< Check vor validity of the given id.
197        {
198            ThrowException(Argument, "Invalid hintId.");
199        }
200
201        QuestHint* hint;
202        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
203        if (it != this->hintMap_.end()) //!< If the QuestHint is registered.
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        }
212
213        return hint;
214
215    }
216
217    /**
218    @brief
219       
220    @param name
221    @return
222    */
223    QuestContainer* QuestManager::getQuestTree(std::string & name)
224    {
225        GUIOverlay* gui = GUIManager::getInstance().getOverlay(name);
226
227        PlayerInfo* player;
228        if(gui == NULL)
229        {
230            COUT(1) << "Error: No GUIOverlay with the given name '" << name << "' present." << std::endl;
231            return NULL;
232        }
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);
240   
241        QuestContainer* root = NULL;
242        QuestContainer* current = NULL;
243       
244        std::list<Quest*>* rootQuests = new std::list<Quest*>();
245        getRootQuests(player, *rootQuests);
246       
247        for(std::list<Quest*>::iterator it = rootQuests->begin(); it != rootQuests->end(); it++)
248        {
249            QuestContainer* container = addSubQuest(*it, player);
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
266        delete rootQuests;
267
268        return root;
269    }
270
271    /**
272    @brief
273       
274    @param player
275    @param list
276    @return
277    */
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
290    /**
291    @brief
292       
293    @param quest
294    @param player
295    @return
296    */
297    QuestContainer* QuestManager::addSubQuest(Quest* quest, const PlayerInfo* player)
298    {
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();
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            {
332                QuestContainer* subContainer = addSubQuest(subQuest, player);
333
334                if(first == NULL)
335                {
336                    first = subContainer;
337                }
338                else
339                {
340                    current->next = subContainer;
341                }
342               
343                current = subContainer;
344            }
345        }
346        if(current != NULL)
347            current->next = NULL;
348        container->subQuests = first;
349       
350        return container;
351    }
352
353    /**
354    @brief
355       
356    @param quest
357    @param player
358    @return
359    */
360    HintContainer* QuestManager::addHints(Quest* quest, const PlayerInfo* player)
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;
388        return first;
389    }
390
391
392}
Note: See TracBrowser for help on using the repository browser.