Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/questsystem/QuestManager.cc @ 5748

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

In quest system:

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