Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/masterserver/src/modules/questsystem/QuestManager.cc @ 7972

Last change on this file since 7972 was 7456, checked in by dafrick, 14 years ago

Reviewing documentation fo Questsystem, moving documentation fully into doxygen.
Added some files to modules they belong to.

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