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 | namespace orxonox |
---|
50 | { |
---|
51 | ManageScopedSingleton(QuestManager, ScopeID::Root, false); |
---|
52 | |
---|
53 | /** |
---|
54 | @brief |
---|
55 | Constructor. Registers the object. |
---|
56 | @todo |
---|
57 | Is inheriting from BaseObject proper? |
---|
58 | */ |
---|
59 | QuestManager::QuestManager() |
---|
60 | { |
---|
61 | RegisterRootObject(QuestManager); |
---|
62 | |
---|
63 | COUT(3) << "QuestManager created." << std::endl; |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | @brief |
---|
68 | Destructor. |
---|
69 | */ |
---|
70 | QuestManager::~QuestManager() |
---|
71 | { |
---|
72 | COUT(3) << "QuestManager destroyed." << std::endl; |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | @brief |
---|
77 | Retreive all Quests. |
---|
78 | @return |
---|
79 | Returns a map with all Quests indexed by their id's. |
---|
80 | */ |
---|
81 | std::map<std::string, Quest*> & QuestManager::getQuests(void) |
---|
82 | { |
---|
83 | return this->questMap_; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | @brief |
---|
88 | Registers a Quest with the QuestManager to make it globally accessible. |
---|
89 | Uses it's id to make sure to be able to be identify and retrieve it later. |
---|
90 | @param quest |
---|
91 | The Quest that is to be registered. |
---|
92 | @return |
---|
93 | Returns true if successful, false if not. |
---|
94 | */ |
---|
95 | bool QuestManager::registerQuest(Quest* quest) |
---|
96 | { |
---|
97 | assert(quest); |
---|
98 | |
---|
99 | std::pair<std::map<std::string, Quest*>::iterator,bool> result; |
---|
100 | result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); // Inserting the Quest. |
---|
101 | |
---|
102 | if(result.second) // If inserting was a success. |
---|
103 | { |
---|
104 | quest->setRegistered(); |
---|
105 | COUT(4) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl; |
---|
106 | return true; |
---|
107 | } |
---|
108 | else |
---|
109 | { |
---|
110 | COUT(2) << "Quest with the same id was already present." << std::endl; |
---|
111 | return false; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | @brief |
---|
117 | Unregisters a Quest in the QuestManager. |
---|
118 | */ |
---|
119 | bool QuestManager::unregisterQuest(Quest* quest) |
---|
120 | { |
---|
121 | return this->questMap_.erase(quest->getId()) == 1; |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | @brief |
---|
126 | Registers a QuestHint with the QuestManager to make it globally accessible. |
---|
127 | Uses it's id to make sure to be able to be identify and retrieve it later. |
---|
128 | @param hint |
---|
129 | The QuestHint to be registered. |
---|
130 | @return |
---|
131 | Returns true if successful, false if not. |
---|
132 | */ |
---|
133 | bool QuestManager::registerHint(QuestHint* hint) |
---|
134 | { |
---|
135 | assert(hint); |
---|
136 | |
---|
137 | std::pair<std::map<std::string, QuestHint*>::iterator,bool> result; |
---|
138 | result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); // Inserting the QuestHSint. |
---|
139 | |
---|
140 | if(result.second) // If inserting was a success. |
---|
141 | { |
---|
142 | hint->setRegistered(); |
---|
143 | COUT(4) << "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 | Unregisters a QuestHint in the QuestManager. |
---|
156 | */ |
---|
157 | bool QuestManager::unregisterHint(QuestHint* hint) |
---|
158 | { |
---|
159 | return this->hintMap_.erase(hint->getId()) == 1; |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | @brief |
---|
164 | Finds a Quest with the given id. |
---|
165 | @param questId |
---|
166 | The id of the Quest sought for. |
---|
167 | @return |
---|
168 | Returns a pointer to the Quest with the input id. |
---|
169 | Returns NULL if there is no Quest with the given questId. |
---|
170 | @throws |
---|
171 | Throws an exception if the given questId is invalid. |
---|
172 | */ |
---|
173 | Quest* QuestManager::findQuest(const std::string & questId) |
---|
174 | { |
---|
175 | if(questId == "") // Check for validity of the given id. |
---|
176 | ThrowException(Argument, "Invalid questId."); |
---|
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 | quest = it->second; |
---|
182 | else |
---|
183 | { |
---|
184 | quest = NULL; |
---|
185 | COUT(2) << "The quest with id {" << questId << "} is nowhere to be found." << std::endl; |
---|
186 | } |
---|
187 | |
---|
188 | return quest; |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | @brief |
---|
193 | Finds a QuestHint with the given id. |
---|
194 | @param hintId |
---|
195 | The id of the QuestHint sought for. |
---|
196 | @return |
---|
197 | Returns a pointer to the QuestHint with the input id. |
---|
198 | Returns NULL if there is no QuestHint with the given hintId. |
---|
199 | @throws |
---|
200 | Throws an exception if the given hintId is invalid. |
---|
201 | */ |
---|
202 | QuestHint* QuestManager::findHint(const std::string & hintId) |
---|
203 | { |
---|
204 | if(hintId == "") // Check for validity of the given id. |
---|
205 | ThrowException(Argument, "Invalid hintId."); |
---|
206 | |
---|
207 | QuestHint* hint; |
---|
208 | std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId); |
---|
209 | if (it != this->hintMap_.end()) // If the QuestHint is registered. |
---|
210 | hint = it->second; |
---|
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 | @brief |
---|
222 | Get the number of Quests the input player has, that are root quests. |
---|
223 | @param player |
---|
224 | The player. |
---|
225 | @return |
---|
226 | Returns the number of Quests the input player has, that are root quests. |
---|
227 | */ |
---|
228 | int QuestManager::getNumRootQuests(PlayerInfo* player) |
---|
229 | { |
---|
230 | int numQuests = 0; |
---|
231 | for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++) |
---|
232 | { |
---|
233 | if(it->second->getParentQuest() == NULL && !it->second->isInactive(player)) |
---|
234 | numQuests++; |
---|
235 | } |
---|
236 | return numQuests; |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | @brief |
---|
241 | Get the index-th root quest of the input player. |
---|
242 | @param player |
---|
243 | The player. |
---|
244 | @param index |
---|
245 | The index of the root quest. |
---|
246 | @return |
---|
247 | Returns the index-th root quest of the input player. |
---|
248 | */ |
---|
249 | Quest* QuestManager::getRootQuest(PlayerInfo* player, int index) |
---|
250 | { |
---|
251 | for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++) |
---|
252 | { |
---|
253 | if(it->second->getParentQuest() == NULL && !it->second->isInactive(player) && index-- == 0) |
---|
254 | return it->second; |
---|
255 | } |
---|
256 | return NULL; |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | @brief |
---|
261 | Get the number of sub-quest of an input Quest for the input player. |
---|
262 | @param quest |
---|
263 | The quest to get the sub-quests of. |
---|
264 | @param player |
---|
265 | The player. |
---|
266 | @return |
---|
267 | Returns the number of sub-quest of an input Quest for the input player. |
---|
268 | */ |
---|
269 | int QuestManager::getNumSubQuests(Quest* quest, PlayerInfo* player) |
---|
270 | { |
---|
271 | std::list<Quest*> quests = quest->getSubQuestList(); |
---|
272 | int numQuests = 0; |
---|
273 | for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++) |
---|
274 | { |
---|
275 | if(!(*it)->isInactive(player)) |
---|
276 | numQuests++; |
---|
277 | } |
---|
278 | return numQuests; |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | @brief |
---|
283 | Get the index-th sub-quest of the input Quest for the input player. |
---|
284 | @param quest |
---|
285 | The Quest to get the sub-quest of. |
---|
286 | @param player |
---|
287 | The player. |
---|
288 | @param index |
---|
289 | The index of the sub-quest. |
---|
290 | */ |
---|
291 | Quest* QuestManager::getSubQuest(Quest* quest, PlayerInfo* player, int index) |
---|
292 | { |
---|
293 | std::list<Quest*> quests = quest->getSubQuestList(); |
---|
294 | for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++) |
---|
295 | { |
---|
296 | if(!(*it)->isInactive(player) && index-- == 0) |
---|
297 | return *it; |
---|
298 | } |
---|
299 | return NULL; |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | @brief |
---|
304 | Get the number of QuestHints of the input Quest for the input player. |
---|
305 | @param quest |
---|
306 | The quest to get the hints of. |
---|
307 | @param player |
---|
308 | The player. |
---|
309 | @return Returns the number of QuestHints of the input Quest for the input player. |
---|
310 | */ |
---|
311 | int QuestManager::getNumHints(Quest* quest, PlayerInfo* player) |
---|
312 | { |
---|
313 | std::list<QuestHint*> hints = quest->getHintsList(); |
---|
314 | int numHints = 0; |
---|
315 | for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++) |
---|
316 | { |
---|
317 | if((*it)->isActive(player)) |
---|
318 | numHints++; |
---|
319 | } |
---|
320 | return numHints; |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | @brief |
---|
325 | Get the index-th QuestHint of the input Quest for the input player. |
---|
326 | @param quest |
---|
327 | The Quest to get the QuestHint of. |
---|
328 | @param player |
---|
329 | The player. |
---|
330 | @param index |
---|
331 | The index of the QuestHint. |
---|
332 | */ |
---|
333 | QuestHint* QuestManager::getHints(Quest* quest, PlayerInfo* player, int index) |
---|
334 | { |
---|
335 | std::list<QuestHint*> hints = quest->getHintsList(); |
---|
336 | for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++) |
---|
337 | { |
---|
338 | if((*it)->isActive(player) && index-- == 0) |
---|
339 | return *it; |
---|
340 | } |
---|
341 | return NULL; |
---|
342 | } |
---|
343 | |
---|
344 | /** |
---|
345 | @brief |
---|
346 | Get the QuestDescription of the input Quest. |
---|
347 | @param item |
---|
348 | The Quest to get the QuestDescription of. |
---|
349 | @return |
---|
350 | Return a pointer ot the QuestDescription of the input Quest. |
---|
351 | */ |
---|
352 | QuestDescription* QuestManager::getDescription(Quest* item) |
---|
353 | { |
---|
354 | return item->getDescription(); |
---|
355 | } |
---|
356 | |
---|
357 | /** |
---|
358 | @brief |
---|
359 | Get the QuestDescription of the input QuestHint. |
---|
360 | @param item |
---|
361 | The QuestHint to get the QuestDescription of. |
---|
362 | @return |
---|
363 | Returns a pointer to the QuestDescription of the input QuestHint. |
---|
364 | */ |
---|
365 | QuestDescription* QuestManager::getDescription(QuestHint* item) |
---|
366 | { |
---|
367 | return item->getDescription(); |
---|
368 | } |
---|
369 | |
---|
370 | /** |
---|
371 | @brief |
---|
372 | Retrieve the player for a certain GUI. |
---|
373 | @param guiName |
---|
374 | The name of the GUI the player is retrieved for. |
---|
375 | @return |
---|
376 | Returns the player. |
---|
377 | @todo |
---|
378 | This very well might be outdated. So: Check if still needed, and update if necessary. |
---|
379 | */ |
---|
380 | PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName) |
---|
381 | { |
---|
382 | PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName); |
---|
383 | if(player == NULL) |
---|
384 | { |
---|
385 | COUT(1) << "Error: GUIOverlay with name '" << guiName << "' has no player." << std::endl; |
---|
386 | return NULL; |
---|
387 | } |
---|
388 | |
---|
389 | return player; |
---|
390 | } |
---|
391 | |
---|
392 | } |
---|