Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SuperOrxoBros_FS17/src/modules/questsystem/QuestManager.cc @ 11365

Last change on this file since 11365 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

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