Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/modules/questsystem/QuestManager.cc @ 5925

Last change on this file since 5925 was 5877, checked in by rgrieder, 15 years ago

Added new an option for the ScopedSingletonManager that can allow the Singleton to fail (throw an exception).
Also improved exception-safety in Scope so that when for a Singleton fails, the Scope will deactivate all activated listeners and properly destroy itself.

  • Property svn:eol-style set to native
File size: 7.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
31    @brief Implementation of the QuestManager class.
32*/
33
34#include "QuestManager.h"
35
36#include <CEGUIWindow.h>
37
38#include "util/Exception.h"
39#include "core/CoreIncludes.h"
40#include "core/GUIManager.h"
41#include "core/ConsoleCommand.h"
42#include "core/LuaState.h"
43#include "core/ScopedSingletonManager.h"
44#include "infos/PlayerInfo.h"
45#include "overlays/GUIOverlay.h"
46
47#include "ToluaBindQuestsystem.h"
48#include "Quest.h"
49#include "QuestHint.h"
50#include "QuestItem.h"
51
52namespace orxonox
53{
54    // Register tolua_open function when loading the library
55    DeclareToluaInterface(Questsystem);
56
57    //! Pointer to the current (and single) instance of this class.
58    /*static*/ QuestManager* QuestManager::singletonPtr_s = NULL;
59    ManageScopedSingleton(QuestManager, ScopeID::Root, false);
60
61    /**
62    @brief
63        Constructor. Registers the object.
64    @todo
65        Is inheriting from BaseObject proper?
66    */
67    QuestManager::QuestManager()
68    {
69        RegisterRootObject(QuestManager);
70    }
71
72    /**
73    @brief
74        Destructor.
75    */
76    QuestManager::~QuestManager()
77    {
78        for(std::map<PlayerInfo*, QuestGUI*>::iterator it = this->questGUIs_.begin(); it != this->questGUIs_.end(); it++)
79        {
80            (*it).second->destroy();
81        }
82        this->questGUIs_.clear();
83    }
84
85    /**
86    @brief
87        Retreive all Quests.
88    @return
89        Returns a map with all Quests indexed by their id's.
90    */
91    std::map<std::string, Quest*> & QuestManager::getQuests(void)
92    {
93        return this->questMap_;
94    }
95
96    /**
97    @brief
98        Registers a Quest with the QuestManager to make it globally accessable.
99        Uses it's id to make sure to be able to be identify and retrieve it later.
100    @param quest
101        The Quest that is to be registered.
102    @return
103        Returns true if successful, false if not.
104    */
105    bool QuestManager::registerQuest(Quest* quest)
106    {
107        if(quest == NULL) //!< Doh! Just as if there were actual quests behind NULL-pointers.
108        {
109            COUT(2) << "Registration of Quest in QuestManager failed, because inserted Quest-pointer was NULL." << std::endl;
110            return false;
111        }
112
113        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
114        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); //!< Inserting the Quest.
115
116        if(result.second) //!< If inserting was a success.
117        {
118            COUT(3) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl;
119            return true;
120        }
121        else
122        {
123           COUT(2) << "Quest with the same id was already present." << std::endl;
124           return false;
125        }
126    }
127
128    /**
129    @brief
130        Registers a QuestHint with the QuestManager to make it globally accessable.
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        if(hint == NULL) //!< Still not liking NULL-pointers.
140        {
141            COUT(2) << "Registration of QuestHint in QuestManager failed, because inserted QuestHint-pointer was NULL." << std::endl;
142            return false;
143        }
144
145        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
146        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); //!< Inserting the QuestHSint.
147
148        if(result.second) //!< If inserting was a success.
149        {
150            COUT(3) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl;
151            return true;
152        }
153        else
154        {
155           COUT(2) << "QuestHint with the same id was already present." << std::endl;
156           return false;
157        }
158    }
159
160    /**
161    @brief
162        Finds a Quest with the given id.
163    @param questId
164        The id of the Quest sought for.
165    @return
166        Returns a pointer to the Quest with the input id.
167        Returns NULL if there is no Quest with the given questId.
168    @throws
169        Throws an exception if the given questId is invalid.
170    */
171    Quest* QuestManager::findQuest(const std::string & questId)
172    {
173        if(!QuestItem::isId(questId)) //!< Check vor validity of the given id.
174        {
175            ThrowException(Argument, "Invalid questId.");
176        }
177
178        Quest* quest;
179        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
180        if (it != this->questMap_.end()) //!< If the Quest is registered.
181        {
182            quest = it->second;
183        }
184        else
185        {
186           quest = NULL;
187           COUT(2) << "The quest with id {" << questId << "} is nowhere to be found." << std::endl;
188        }
189
190        return quest;
191
192    }
193
194    /**
195    @brief
196        Finds a QuestHint with the given id.
197    @param hintId
198        The id of the QuestHint sought for.
199    @return
200        Returns a pointer to the QuestHint with the input id.
201        Returns NULL if there is no QuestHint with the given hintId.
202    @throws
203        Throws an exception if the given hintId is invalid.
204    */
205    QuestHint* QuestManager::findHint(const std::string & hintId)
206    {
207        if(!QuestItem::isId(hintId)) //!< Check vor validity of the given id.
208        {
209            ThrowException(Argument, "Invalid hintId.");
210        }
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        {
216            hint = it->second;
217        }
218        else
219        {
220           hint = NULL;
221           COUT(2) << "The hint with id {" << hintId << "} is nowhere to be found." << std::endl;
222        }
223
224        return hint;
225
226    }
227
228    /**
229    @brief
230        Retreive the main window for the GUI.
231        This is for the use in the lua script tu start the QuestGUI.
232    @param guiName
233        The name of the GUI.
234    @return
235        Returns a CEGUI Window.
236    */
237    CEGUI::Window* QuestManager::getQuestGUI(const std::string & guiName)
238    {
239        PlayerInfo* player = this->retreivePlayer(guiName);
240
241        if(this->questGUIs_.find(player) == this->questGUIs_.end()) //!< Create a new GUI, if there is none, yet.
242            this->questGUIs_[player] = new QuestGUI(player);
243
244        return this->questGUIs_[player]->getGUI();
245    }
246
247    /**
248    @brief
249        Retrieve the player for a certain GUI.
250    @param guiName
251        The name of the GUI the player is retrieved for.
252    @return
253        Returns the player.
254    @todo
255        This very well might be outdated. So: Check if still needed, and update if necessary.
256    */
257    PlayerInfo* QuestManager::retreivePlayer(const std::string & guiName)
258    {
259        PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName);
260        if(player == NULL)
261        {
262            COUT(1) << "Error: GUIOverlay with name '" << guiName << "' has no player." << std::endl;
263            return NULL;
264        }
265
266        return player;
267    }
268
269}
Note: See TracBrowser for help on using the repository browser.