Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mainmenu/src/modules/questsystem/QuestManager.cc @ 5910

Last change on this file since 5910 was 5738, checked in by landauf, 15 years ago

merged libraries2 back to trunk

  • Property svn:eol-style set to native
File size: 10.0 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 "core/GUIManager.h"
39
40#include "infos/PlayerInfo.h"
41#include "Quest.h"
42#include "QuestHint.h"
43#include "QuestItem.h"
44
45namespace orxonox
46{
47    //! Pointer to the current (and single) instance of this class.
48    /*static*/ QuestManager* QuestManager::singletonPtr_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
61    /**
62    @brief
63        Destructor.
64    */
65    QuestManager::~QuestManager()
66    {
67    }
68
69    /**
70    @brief
71        Registers a Quest with the QuestManager to make it globally accessable.
72        Uses it's id to make sure to be able to be identify and retrieve it later.
73    @param quest
74        The Quest that is to be registered.
75    @return
76        Returns true if successful, false if not.
77    */
78    bool QuestManager::registerQuest(Quest* quest)
79    {
80        if(quest == NULL) //!< Doh! Just as if there were actual quests behind NULL-pointers.
81        {
82            COUT(2) << "Registration of Quest in QuestManager failed, because inserted Quest-pointer was NULL." << std::endl;
83            return false;
84        }
85
86        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
87        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); //!< Inserting the Quest.
88
89        if(result.second) //!< If inserting was a success.
90        {
91            COUT(3) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl;
92            return true;
93        }
94        else
95        {
96           COUT(2) << "Quest with the same id was already present." << std::endl;
97           return false;
98        }
99    }
100
101    /**
102    @brief
103        Registers a QuestHint with the QuestManager to make it globally accessable.
104        Uses it's id to make sure to be able to be identify and retrieve it later.
105    @param hint
106        The QuestHint to be registered.
107    @return
108        Returns true if successful, false if not.
109    */
110    bool QuestManager::registerHint(QuestHint* hint)
111    {
112        if(hint == NULL) //!< Still not liking NULL-pointers.
113        {
114            COUT(2) << "Registration of QuestHint in QuestManager failed, because inserted QuestHint-pointer was NULL." << std::endl;
115            return false;
116        }
117
118        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
119        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); //!< Inserting the QuestHSint.
120
121        if(result.second) //!< If inserting was a success.
122        {
123            COUT(3) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl;
124            return true;
125        }
126        else
127        {
128           COUT(2) << "QuestHint with the same id was already present." << std::endl;
129           return false;
130        }
131    }
132
133    /**
134    @brief
135        Finds a Quest with the given id.
136    @param questId
137        The id of the Quest sought for.
138    @return
139        Returns a pointer to the Quest with the input id.
140        Returns NULL if there is no Quest with the given questId.
141    @throws
142        Throws an exception if the given questId is invalid.
143    */
144    Quest* QuestManager::findQuest(const std::string & questId)
145    {
146        if(!QuestItem::isId(questId)) //!< Check vor validity of the given id.
147        {
148            ThrowException(Argument, "Invalid questId.");
149        }
150
151        Quest* quest;
152        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
153        if (it != this->questMap_.end()) //!< If the Quest is registered.
154        {
155            quest = it->second;
156        }
157        else
158        {
159           quest = NULL;
160           COUT(2) << "The quest with id {" << questId << "} is nowhere to be found." << std::endl;
161        }
162
163        return quest;
164
165    }
166
167    /**
168    @brief
169        Finds a QuestHint with the given id.
170    @param hintId
171        The id of the QuestHint sought for.
172    @return
173        Returns a pointer to the QuestHint with the input id.
174        Returns NULL if there is no QuestHint with the given hintId.
175    @throws
176        Throws an exception if the given hintId is invalid.
177    */
178    QuestHint* QuestManager::findHint(const std::string & hintId)
179    {
180        if(!QuestItem::isId(hintId)) //!< Check vor validity of the given id.
181        {
182            ThrowException(Argument, "Invalid hintId.");
183        }
184
185        QuestHint* hint;
186        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
187        if (it != this->hintMap_.end()) //!< If the QuestHint is registered.
188        {
189            hint = it->second;
190        }
191        else
192        {
193           hint = NULL;
194           COUT(2) << "The hint with id {" << hintId << "} is nowhere to be found." << std::endl;
195        }
196
197        return hint;
198
199    }
200
201    /**
202    @brief
203
204    @param name
205    @return
206    */
207    QuestContainer* QuestManager::getQuestTree(std::string & name)
208    {
209        PlayerInfo* player = GUIManager::getInstance().getPlayer(name);
210        if(player == NULL)
211        {
212            COUT(1) << "Error: GUIOverlay with name '" << name << "' has no player." << std::endl;
213            return NULL;
214        }
215
216        QuestContainer* root = NULL;
217        QuestContainer* current = NULL;
218
219        std::list<Quest*>* rootQuests = new std::list<Quest*>();
220        getRootQuests(player, *rootQuests);
221
222        for(std::list<Quest*>::iterator it = rootQuests->begin(); it != rootQuests->end(); it++)
223        {
224            QuestContainer* container = addSubQuest(*it, player);
225
226            if(root == NULL)
227            {
228                root = container;
229            }
230            else
231            {
232                current->next = container;
233            }
234
235            current = container;
236
237        }
238        if(current != NULL)
239            current->next = NULL;
240
241        delete rootQuests;
242
243        return root;
244    }
245
246    /**
247    @brief
248
249    @param player
250    @param list
251    @return
252    */
253    void QuestManager::getRootQuests(const PlayerInfo* player, std::list<Quest*> & list)
254    {
255        for(std::map<std::string, Quest*>::iterator it=this->questMap_.begin(); it!=this->questMap_.end(); it++)
256        {
257            Quest* quest = (*it).second;
258            if(quest->getParentQuest() == NULL && !quest->isInactive(player))
259            {
260                list.push_back(quest);
261            }
262        }
263    }
264
265    /**
266    @brief
267
268    @param quest
269    @param player
270    @return
271    */
272    QuestContainer* QuestManager::addSubQuest(Quest* quest, const PlayerInfo* player)
273    {
274        if(quest == NULL)
275            return NULL;
276
277        QuestContainer* container = new QuestContainer;
278        container->description = quest->getDescription();
279        container->hint = addHints(quest, player);
280
281        if(quest->isActive(player))
282        {
283            container->status = "active";
284        }
285        else if(quest->isCompleted(player))
286        {
287            container->status = "completed";
288        }
289        else if(quest->isFailed(player))
290        {
291            container->status = "failed";
292        }
293        else
294        {
295            container->status = "";
296            COUT(1) << "An error occurred. A Quest of un-specified status wanted to be displayed." << std::endl;
297        }
298
299        std::list<Quest*> quests = quest->getSubQuestList();
300        QuestContainer* current = NULL;
301        QuestContainer* first = NULL;
302        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
303        {
304            Quest* subQuest = *it;
305            if(!subQuest->isInactive(player))
306            {
307                QuestContainer* subContainer = addSubQuest(subQuest, player);
308
309                if(first == NULL)
310                {
311                    first = subContainer;
312                }
313                else
314                {
315                    current->next = subContainer;
316                }
317
318                current = subContainer;
319            }
320        }
321        if(current != NULL)
322            current->next = NULL;
323        container->subQuests = first;
324
325        return container;
326    }
327
328    /**
329    @brief
330
331    @param quest
332    @param player
333    @return
334    */
335    HintContainer* QuestManager::addHints(Quest* quest, const PlayerInfo* player)
336    {
337        HintContainer* current = NULL;
338        HintContainer* first = NULL;
339
340        std::list<QuestHint*> hints = quest->getHintsList();
341        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
342        {
343            if((*it)->isActive(player))
344            {
345                HintContainer* hint = new HintContainer;
346                hint->description = (*it)->getDescription();
347
348                if(first == NULL)
349                {
350                    first = hint;
351                }
352                else
353                {
354                    current->next = hint;
355                }
356
357                current = hint;
358            }
359        }
360
361        if(current != NULL)
362            current->next = NULL;
363        return first;
364    }
365
366
367}
Note: See TracBrowser for help on using the repository browser.