Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutoriallevel/src/modules/questsystem/QuestManager.cc @ 8703

Last change on this file since 8703 was 7830, checked in by dafrick, 14 years ago

New implementation of QuestGUI. Should be much more usable now, however there still remain some adjustments to be made.

  • Property svn:eol-style set to native
File size: 12.4 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
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 accessable.
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        assert(quest);
103
104        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
105        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); // Inserting the Quest.
106
107        if(result.second) // If inserting was a success.
108        {
109            quest->setRegistered();
110            COUT(4) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl;
111            return true;
112        }
113        else
114        {
115           COUT(2) << "Quest with the same id was already present." << std::endl;
116           return false;
117        }
118    }
119
120    /**
121    @brief
122        Unregisters a Quest in the QuestManager.
123    */
124    bool QuestManager::unregisterQuest(Quest* quest)
125    {
126        return this->questMap_.erase(quest->getId()) == 1;
127    }
128
129    /**
130    @brief
131        Registers a QuestHint with the QuestManager to make it globally accessable.
132        Uses it's id to make sure to be able to be identify and retrieve it later.
133    @param hint
134        The QuestHint to be registered.
135    @return
136        Returns true if successful, false if not.
137    */
138    bool QuestManager::registerHint(QuestHint* hint)
139    {
140        assert(hint);
141
142        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
143        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); // Inserting the QuestHSint.
144
145        if(result.second) // If inserting was a success.
146        {
147            hint->setRegistered();
148            COUT(4) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl;
149            return true;
150        }
151        else
152        {
153           COUT(2) << "QuestHint with the same id was already present." << std::endl;
154           return false;
155        }
156    }
157
158    /**
159    @brief
160        Unregisters a QuestHint in the QuestManager.
161    */
162    bool QuestManager::unregisterHint(QuestHint* hint)
163    {
164        return this->hintMap_.erase(hint->getId()) == 1;
165    }
166
167    /**
168    @brief
169        Finds a Quest with the given id.
170    @param questId
171        The id of the Quest sought for.
172    @return
173        Returns a pointer to the Quest with the input id.
174        Returns NULL if there is no Quest with the given questId.
175    @throws
176        Throws an exception if the given questId is invalid.
177    */
178    Quest* QuestManager::findQuest(const std::string & questId)
179    {
180        if(questId.compare(BLANKSTRING) == 1) // Check vor validity of the given id.
181            ThrowException(Argument, "Invalid questId.");
182
183        Quest* quest;
184        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
185        if (it != this->questMap_.end()) // If the Quest is registered.
186            quest = it->second;
187        else
188        {
189           quest = NULL;
190           COUT(2) << "The quest with id {" << questId << "} is nowhere to be found." << std::endl;
191        }
192
193        return quest;
194    }
195
196    /**
197    @brief
198        Finds a QuestHint with the given id.
199    @param hintId
200        The id of the QuestHint sought for.
201    @return
202        Returns a pointer to the QuestHint with the input id.
203        Returns NULL if there is no QuestHint with the given hintId.
204    @throws
205        Throws an exception if the given hintId is invalid.
206    */
207    QuestHint* QuestManager::findHint(const std::string & hintId)
208    {
209        if(hintId.compare(BLANKSTRING) == 1) // Check vor validity of the given id.
210            ThrowException(Argument, "Invalid hintId.");
211
212        QuestHint* hint;
213        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
214        if (it != this->hintMap_.end()) // If the QuestHint is registered.
215            hint = it->second;
216        else
217        {
218           hint = NULL;
219           COUT(2) << "The hint with id {" << hintId << "} is nowhere to be found." << std::endl;
220        }
221
222        return hint;
223    }
224
225    /**
226    @brief
227        Get the number of Quests the input player has, that are root quests.
228    @param player
229        The player.
230    @return
231        Returns the number of Quests the input player has, that are root quests.
232    */
233    int QuestManager::getNumRootQuests(PlayerInfo* player)
234    {
235        int numQuests = 0;
236        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
237        {
238            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player))
239                numQuests++;
240        }
241        return numQuests;
242    }
243
244    /**
245    @brief
246        Get the index-th root quest of the input player.
247    @param player
248        The player.
249    @param index
250        The index of the root quest.
251    @return
252        Returns the index-th root quest of the input player.
253    */
254    Quest* QuestManager::getRootQuest(PlayerInfo* player, int index)
255    {
256        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
257        {
258            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player) && index-- == 0)
259                return it->second;
260        }
261        return NULL;
262    }
263
264    /**
265    @brief
266        Get the number of sub-quest of an input Quest for the input player.
267    @param quest
268        The quest to get the sub-quests of.
269    @param player
270        The player.
271    @return
272        Returns the number of sub-quest of an input Quest for the input player.
273    */
274    int QuestManager::getNumSubQuests(Quest* quest, PlayerInfo* player)
275    {
276        if(quest == NULL)
277            return this->getNumRootQuests(player);
278
279        std::list<Quest*> quests = quest->getSubQuestList();
280        int numQuests = 0;
281        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
282        {
283            if(!(*it)->isInactive(player))
284                numQuests++;
285        }
286        return numQuests;
287    }
288
289    /**
290    @brief
291        Get the index-th sub-quest of the input Quest for the input player.
292    @param quest
293        The Quest to get the sub-quest of.
294    @param player
295        The player.
296    @param index
297        The index of the sub-quest.
298    */
299    Quest* QuestManager::getSubQuest(Quest* quest, PlayerInfo* player, int index)
300    {
301        if(quest == NULL)
302            return this->getRootQuest(player, index);
303
304        std::list<Quest*> quests = quest->getSubQuestList();
305        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
306        {
307            if(!(*it)->isInactive(player) && index-- == 0)
308                return *it;
309        }
310        return NULL;
311    }
312
313    /**
314    @brief
315        Get the number of QuestHints of the input Quest for the input player.
316    @param quest
317        The quest to get the hints of.
318    @param player
319        The player.
320    @return 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    */
344    QuestHint* QuestManager::getHints(Quest* quest, PlayerInfo* player, int index)
345    {
346        std::list<QuestHint*> hints = quest->getHintsList();
347        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
348        {
349            if((*it)->isActive(player) && index-- == 0)
350                return *it;
351        }
352        return NULL;
353    }
354
355    /**
356    @brief
357        Get the parent-quest of the input Quest for the input player.
358    @param quest
359        The Quest to get the parent-quest of.
360    @param player
361        The player.
362    */
363    Quest* QuestManager::getParentQuest(Quest* quest)
364    {
365        return quest->getParentQuest();
366    }
367
368    /**
369    @brief
370        Get the QuestDescription of the input Quest.
371    @param item
372        The Quest to get the QuestDescription of.
373    @return
374        Return a pointer ot the QuestDescription of the input Quest.
375    */
376    QuestDescription* QuestManager::getDescription(Quest* item)
377    {
378        return item->getDescription();
379    }
380
381    /**
382    @brief
383        Get the QuestDescription of the input QuestHint.
384    @param item
385        The QuestHint to get the QuestDescription of.
386    @return
387        Returns a pointer to the QuestDescription of the input QuestHint.
388    */
389    QuestDescription* QuestManager::getDescription(QuestHint* item)
390    {
391        return item->getDescription();
392    }
393
394    /**
395    @brief
396        Get the id of the input Quest.
397    @param item
398        The Quest to get the id of.
399    @return
400        Returns the id of the input Quest.
401    */
402    const std::string QuestManager::getId(Quest* item) const
403    {
404        return item->getId();
405    }
406
407    /**
408    @brief
409        Get the id of the input QuestHint.
410    @param item
411        The QuestHint to get the id of.
412    @return
413        Returns the id of the input QuestHint.
414    */
415    const std::string QuestManager::getId(QuestHint* item) const
416    {
417        return item->getId();
418    }
419
420    /**
421    @brief
422        Retrieve the player for a certain GUI.
423    @param guiName
424        The name of the GUI the player is retrieved for.
425    @return
426        Returns the player.
427    @todo
428        This very well might be outdated. So: Check if still needed, and update if necessary.
429    */
430    PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName)
431    {
432        PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName);
433        if(player == NULL)
434        {
435            COUT(1) << "Error: GUIOverlay with name '" << guiName << "' has no player." << std::endl;
436            return NULL;
437        }
438
439        return player;
440    }
441
442}
Note: See TracBrowser for help on using the repository browser.