Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai2/src/modules/questsystem/QuestManager.cc @ 8841

Last change on this file since 8841 was 8841, checked in by dafrick, 13 years ago

Adding some asserts.

  • Property svn:eol-style set to native
File size: 13.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 QuestManager.cc
31    @brief Implementation of the QuestManager class.
32*/
33
34#include "QuestManager.h"
35
36#include "util/Exception.h"
37#include "util/OrxAssert.h"
38#include "util/ScopedSingletonManager.h"
39#include "core/command/ConsoleCommand.h"
40#include "core/CoreIncludes.h"
41#include "core/GUIManager.h"
42#include "core/LuaState.h"
43
44#include "infos/PlayerInfo.h"
45
46#include "Quest.h"
47#include "QuestHint.h"
48#include "QuestItem.h"
49
50#include "ToluaBindQuestsystem.h"
51
52namespace orxonox
53{
54    // Register tolua_open function when loading the library
55    DeclareToluaInterface(Questsystem);
56
57    ManageScopedSingleton(QuestManager, ScopeID::Root, false);
58
59    /**
60    @brief
61        Constructor. Registers the object.
62    @todo
63        Is inheriting from BaseObject proper?
64    */
65    QuestManager::QuestManager()
66    {
67        RegisterRootObject(QuestManager);
68        COUT(3) << "QuestManager created." << std::endl;
69    }
70
71    /**
72    @brief
73        Destructor.
74    */
75    QuestManager::~QuestManager()
76    {
77        COUT(3) << "QuestManager destroyed." << std::endl;
78    }
79
80    /**
81    @brief
82        Retreive all Quests.
83    @return
84        Returns a map with all Quests indexed by their id's.
85    */
86    std::map<std::string, Quest*> & QuestManager::getQuests(void)
87    {
88        return this->questMap_;
89    }
90
91    /**
92    @brief
93        Registers a Quest with the QuestManager to make it globally accessible.
94        Uses it's id to make sure to be able to be identify and retrieve it later.
95    @param quest
96        The Quest that is to be registered.
97    @return
98        Returns true if successful, false if not.
99    */
100    bool QuestManager::registerQuest(Quest* quest)
101    {
102        if(quest == NULL)
103        {
104            COUT(1) << "Quest pointer is NULL." << endl;
105            return false;
106        }
107
108        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
109        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); // Inserting the Quest.
110
111        if(result.second) // If inserting was a success.
112        {
113            quest->setRegistered();
114            COUT(4) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl;
115            return true;
116        }
117        else
118        {
119           COUT(2) << "Quest with the same id was already present." << std::endl;
120           return false;
121        }
122    }
123
124    /**
125    @brief
126        Unregisters a Quest in the QuestManager.
127    */
128    bool QuestManager::unregisterQuest(Quest* quest)
129    {
130        return this->questMap_.erase(quest->getId()) == 1;
131    }
132
133    /**
134    @brief
135        Registers a QuestHint with the QuestManager to make it globally accessible.
136        Uses it's id to make sure to be able to be identify and retrieve it later.
137    @param hint
138        The QuestHint to be registered.
139    @return
140        Returns true if successful, false if not.
141    */
142    bool QuestManager::registerHint(QuestHint* hint)
143    {
144        if(hint == NULL)
145        {
146            COUT(1) << "Hint pointer is NULL." << endl;
147            return false;
148        }
149
150        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
151        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); // Inserting the QuestHSint.
152
153        if(result.second) // If inserting was a success.
154        {
155            hint->setRegistered();
156            COUT(4) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl;
157            return true;
158        }
159        else
160        {
161           COUT(2) << "QuestHint with the same id was already present." << std::endl;
162           return false;
163        }
164    }
165
166    /**
167    @brief
168        Unregisters a QuestHint in the QuestManager.
169    */
170    bool QuestManager::unregisterHint(QuestHint* hint)
171    {
172        return this->hintMap_.erase(hint->getId()) == 1;
173    }
174
175    /**
176    @brief
177        Finds a Quest with the given id.
178    @param questId
179        The id of the Quest sought for.
180    @return
181        Returns a pointer to the Quest with the input id.
182        Returns NULL if there is no Quest with the given questId.
183    @throws
184        Throws an exception if the given questId is invalid.
185    */
186    Quest* QuestManager::findQuest(const std::string & questId)
187    {
188        if(questId == "") // Check for validity of the given id.
189            ThrowException(Argument, "Invalid questId.");
190
191        Quest* quest;
192        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
193        if (it != this->questMap_.end()) // If the Quest is registered.
194            quest = it->second;
195        else
196        {
197           quest = NULL;
198           COUT(2) << "The quest with id {" << questId << "} is nowhere to be found." << std::endl;
199        }
200
201        return quest;
202    }
203
204    /**
205    @brief
206        Finds a QuestHint with the given id.
207    @param hintId
208        The id of the QuestHint sought for.
209    @return
210        Returns a pointer to the QuestHint with the input id.
211        Returns NULL if there is no QuestHint with the given hintId.
212    @throws
213        Throws an exception if the given hintId is invalid.
214    */
215    QuestHint* QuestManager::findHint(const std::string & hintId)
216    {
217        if(hintId == "") // Check for validity of the given id.
218            ThrowException(Argument, "Invalid hintId.");
219
220        QuestHint* hint;
221        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
222        if (it != this->hintMap_.end()) // If the QuestHint is registered.
223            hint = it->second;
224        else
225        {
226           hint = NULL;
227           COUT(2) << "The hint with id {" << hintId << "} is nowhere to be found." << std::endl;
228        }
229
230        return hint;
231    }
232
233    /**
234    @brief
235        Get the number of Quests the input player has, that are root quests.
236    @param player
237        The player.
238    @return
239        Returns the number of Quests the input player has, that are root quests.
240    */
241    int QuestManager::getNumRootQuests(PlayerInfo* player)
242    {
243        int numQuests = 0;
244        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
245        {
246            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player))
247                numQuests++;
248        }
249        return numQuests;
250    }
251
252    /**
253    @brief
254        Get the index-th root quest of the input player.
255    @param player
256        The player.
257    @param index
258        The index of the root quest.
259    @return
260        Returns the index-th root quest of the input player.
261    */
262    Quest* QuestManager::getRootQuest(PlayerInfo* player, int index)
263    {
264        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
265        {
266            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player) && index-- == 0)
267                return it->second;
268        }
269        return NULL;
270    }
271
272    /**
273    @brief
274        Get the number of sub-quest of an input Quest for the input player.
275    @param quest
276        The quest to get the sub-quests of.
277    @param player
278        The player.
279    @return
280        Returns the number of sub-quest of an input Quest for the input player.
281    */
282    int QuestManager::getNumSubQuests(Quest* quest, PlayerInfo* player)
283    {
284        if(quest == NULL)
285            return this->getNumRootQuests(player);
286
287        std::list<Quest*> quests = quest->getSubQuestList();
288        int numQuests = 0;
289        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
290        {
291            if(!(*it)->isInactive(player))
292                numQuests++;
293        }
294        return numQuests;
295    }
296
297    /**
298    @brief
299        Get the index-th sub-quest of the input Quest for the input player.
300    @param quest
301        The Quest to get the sub-quest of.
302    @param player
303        The player.
304    @param index
305        The index of the sub-quest.
306    */
307    Quest* QuestManager::getSubQuest(Quest* quest, PlayerInfo* player, int index)
308    {
309        if(quest == NULL)
310            return this->getRootQuest(player, index);
311
312        std::list<Quest*> quests = quest->getSubQuestList();
313        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
314        {
315            if(!(*it)->isInactive(player) && index-- == 0)
316                return *it;
317        }
318        return NULL;
319    }
320
321    /**
322    @brief
323        Get the number of QuestHints of the input Quest for the input player.
324    @param quest
325        The quest to get the hints of.
326    @param player
327        The player.
328    @return
329        Returns the number of QuestHints of the input Quest for the input player.
330    */
331    int QuestManager::getNumHints(Quest* quest, PlayerInfo* player)
332    {
333        std::list<QuestHint*> hints = quest->getHintsList();
334        int numHints = 0;
335        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
336        {
337            if((*it)->isActive(player))
338                numHints++;
339        }
340        return numHints;
341    }
342
343    /**
344    @brief
345        Get the index-th QuestHint of the input Quest for the input player.
346    @param quest
347        The Quest to get the QuestHint of.
348    @param player
349        The player.
350    @param index
351        The index of the QuestHint.
352    @return
353        Returns a pointer to the index-th QuestHint of the input Quest for the input player.
354    */
355    QuestHint* QuestManager::getHints(Quest* quest, PlayerInfo* player, int index)
356    {
357        std::list<QuestHint*> hints = quest->getHintsList();
358        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
359        {
360            if((*it)->isActive(player) && index-- == 0)
361                return *it;
362        }
363        return NULL;
364    }
365
366    /**
367    @brief
368        Get the parent-quest of the input Quest.
369    @param quest
370        The Quest to get the parent-quest of.
371    @return
372        Returns a pointer to the parent-quest of the input Quest.
373    */
374    Quest* QuestManager::getParentQuest(Quest* quest)
375    {
376        OrxAssert(quest, "The input Quest is NULL.");
377        return quest->getParentQuest();
378    }
379
380    /**
381    @brief
382        Get the QuestDescription of the input Quest.
383    @param item
384        The Quest to get the QuestDescription of.
385    @return
386        Return a pointer ot the QuestDescription of the input Quest.
387    */
388    QuestDescription* QuestManager::getDescription(Quest* item)
389    {
390        OrxAssert(item, "The input Quest is NULL.");
391        return item->getDescription();
392    }
393
394    /**
395    @brief
396        Get the QuestDescription of the input QuestHint.
397    @param item
398        The QuestHint to get the QuestDescription of.
399    @return
400        Returns a pointer to the QuestDescription of the input QuestHint.
401    */
402    QuestDescription* QuestManager::getDescription(QuestHint* item)
403    {
404        OrxAssert(item, "The input QuestHint is NULL.");
405        return item->getDescription();
406    }
407
408    /**
409    @brief
410        Get the id of the input Quest.
411    @param item
412        The Quest to get the id of.
413    @return
414        Returns the id of the input Quest.
415    */
416    const std::string QuestManager::getId(Quest* item) const
417    {
418        OrxAssert(item, "The input Quest is NULL.");
419        return item->getId();
420    }
421
422    /**
423    @brief
424        Get the id of the input QuestHint.
425    @param item
426        The QuestHint to get the id of.
427    @return
428        Returns the id of the input QuestHint.
429    */
430    const std::string QuestManager::getId(QuestHint* item) const
431    {
432        OrxAssert(item, "The input QuestHint is NULL.");
433        return item->getId();
434    }
435
436    /**
437    @brief
438        Retrieve the player for a certain GUI.
439    @param guiName
440        The name of the GUI the player is retrieved for.
441    @return
442        Returns the player.
443    @todo
444        This very well might be outdated. So: Check if still needed, and update if necessary.
445    */
446    PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName)
447    {
448        PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName);
449        if(player == NULL)
450        {
451            COUT(1) << "Error: GUIOverlay with name '" << guiName << "' has no player." << std::endl;
452            return NULL;
453        }
454
455        return player;
456    }
457
458}
Note: See TracBrowser for help on using the repository browser.