Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8839 was 8839, checked in by jo, 13 years ago

sorry, I forgot to remove a wrong buxfix entirely.

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