Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/orxonox/objects/quest/QuestManager.cc @ 3864

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