Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Removed old msvc specific support for precompiled header files.

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