[1996] | 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 | |
---|
[2261] | 29 | /** |
---|
[7456] | 30 | @file QuestManager.cc |
---|
[2662] | 31 | @brief Implementation of the QuestManager class. |
---|
[2261] | 32 | */ |
---|
| 33 | |
---|
[2105] | 34 | #include "QuestManager.h" |
---|
| 35 | |
---|
[3196] | 36 | #include "util/Exception.h" |
---|
[8891] | 37 | #include "util/OrxAssert.h" |
---|
[7284] | 38 | #include "util/ScopedSingletonManager.h" |
---|
[7456] | 39 | #include "core/command/ConsoleCommand.h" |
---|
[1996] | 40 | #include "core/CoreIncludes.h" |
---|
[5693] | 41 | #include "core/GUIManager.h" |
---|
[5755] | 42 | #include "core/LuaState.h" |
---|
[7456] | 43 | |
---|
[5748] | 44 | #include "infos/PlayerInfo.h" |
---|
[2105] | 45 | |
---|
[2095] | 46 | #include "Quest.h" |
---|
| 47 | #include "QuestHint.h" |
---|
[5748] | 48 | #include "QuestItem.h" |
---|
[1996] | 49 | |
---|
[2662] | 50 | namespace orxonox |
---|
| 51 | { |
---|
[5929] | 52 | ManageScopedSingleton(QuestManager, ScopeID::Root, false); |
---|
[1996] | 53 | |
---|
[2261] | 54 | /** |
---|
| 55 | @brief |
---|
| 56 | Constructor. Registers the object. |
---|
[2911] | 57 | @todo |
---|
| 58 | Is inheriting from BaseObject proper? |
---|
[2261] | 59 | */ |
---|
[2911] | 60 | QuestManager::QuestManager() |
---|
[1996] | 61 | { |
---|
[2911] | 62 | RegisterRootObject(QuestManager); |
---|
[8858] | 63 | orxout(internal_info, context::quests) << "QuestManager created." << endl; |
---|
[1996] | 64 | } |
---|
[2092] | 65 | |
---|
[2261] | 66 | /** |
---|
| 67 | @brief |
---|
| 68 | Destructor. |
---|
| 69 | */ |
---|
[1996] | 70 | QuestManager::~QuestManager() |
---|
| 71 | { |
---|
[8858] | 72 | orxout(internal_info, context::quests) << "QuestManager destroyed." << endl; |
---|
[1996] | 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | @brief |
---|
[5745] | 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 |
---|
[8079] | 88 | Registers a Quest with the QuestManager to make it globally accessible. |
---|
[2261] | 89 | Uses it's id to make sure to be able to be identify and retrieve it later. |
---|
[1996] | 90 | @param quest |
---|
[2261] | 91 | The Quest that is to be registered. |
---|
[1996] | 92 | @return |
---|
| 93 | Returns true if successful, false if not. |
---|
| 94 | */ |
---|
[2911] | 95 | bool QuestManager::registerQuest(Quest* quest) |
---|
[1996] | 96 | { |
---|
[8891] | 97 | if(quest == NULL) |
---|
| 98 | { |
---|
[8952] | 99 | orxout(internal_error, context::quests) << "Quest pointer is NULL." << endl; |
---|
[8891] | 100 | return false; |
---|
| 101 | } |
---|
[2092] | 102 | |
---|
[2261] | 103 | std::pair<std::map<std::string, Quest*>::iterator,bool> result; |
---|
[7456] | 104 | result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); // Inserting the Quest. |
---|
[2092] | 105 | |
---|
[7456] | 106 | if(result.second) // If inserting was a success. |
---|
[2068] | 107 | { |
---|
[7163] | 108 | quest->setRegistered(); |
---|
[8858] | 109 | orxout(verbose, context::quests) << "Quest with questId {" << quest->getId() << "} successfully inserted." << endl; |
---|
[2068] | 110 | return true; |
---|
[2093] | 111 | } |
---|
| 112 | else |
---|
| 113 | { |
---|
[8858] | 114 | orxout(internal_warning, context::quests) << "Quest with the same id was already present." << endl; |
---|
[2093] | 115 | return false; |
---|
| 116 | } |
---|
[1996] | 117 | } |
---|
[2092] | 118 | |
---|
[1996] | 119 | /** |
---|
| 120 | @brief |
---|
[7163] | 121 | Unregisters a Quest in the QuestManager. |
---|
| 122 | */ |
---|
| 123 | bool QuestManager::unregisterQuest(Quest* quest) |
---|
| 124 | { |
---|
| 125 | return this->questMap_.erase(quest->getId()) == 1; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | @brief |
---|
[8079] | 130 | Registers a QuestHint with the QuestManager to make it globally accessible. |
---|
[2261] | 131 | Uses it's id to make sure to be able to be identify and retrieve it later. |
---|
[1996] | 132 | @param hint |
---|
| 133 | The QuestHint to be registered. |
---|
| 134 | @return |
---|
| 135 | Returns true if successful, false if not. |
---|
| 136 | */ |
---|
[2911] | 137 | bool QuestManager::registerHint(QuestHint* hint) |
---|
[1996] | 138 | { |
---|
[8891] | 139 | if(hint == NULL) |
---|
| 140 | { |
---|
[8952] | 141 | orxout(internal_error, context::quests) << "Quest pointer is NULL." << endl; |
---|
[8891] | 142 | return false; |
---|
| 143 | } |
---|
[2092] | 144 | |
---|
[2261] | 145 | std::pair<std::map<std::string, QuestHint*>::iterator,bool> result; |
---|
[7456] | 146 | result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); // Inserting the QuestHSint. |
---|
[2092] | 147 | |
---|
[7456] | 148 | if(result.second) // If inserting was a success. |
---|
[2068] | 149 | { |
---|
[7163] | 150 | hint->setRegistered(); |
---|
[8858] | 151 | orxout(verbose, context::quests) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << endl; |
---|
[2068] | 152 | return true; |
---|
[2093] | 153 | } |
---|
| 154 | else |
---|
| 155 | { |
---|
[8858] | 156 | orxout(internal_warning, context::quests) << "QuestHint with the same id was already present." << endl; |
---|
[2093] | 157 | return false; |
---|
| 158 | } |
---|
[1996] | 159 | } |
---|
[2092] | 160 | |
---|
[1996] | 161 | /** |
---|
| 162 | @brief |
---|
[7163] | 163 | Unregisters a QuestHint in the QuestManager. |
---|
| 164 | */ |
---|
| 165 | bool QuestManager::unregisterHint(QuestHint* hint) |
---|
| 166 | { |
---|
| 167 | return this->hintMap_.erase(hint->getId()) == 1; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | @brief |
---|
[2261] | 172 | Finds a Quest with the given id. |
---|
[1996] | 173 | @param questId |
---|
[2261] | 174 | The id of the Quest sought for. |
---|
[1996] | 175 | @return |
---|
[2261] | 176 | Returns a pointer to the Quest with the input id. |
---|
| 177 | Returns NULL if there is no Quest with the given questId. |
---|
[2068] | 178 | @throws |
---|
| 179 | Throws an exception if the given questId is invalid. |
---|
[1996] | 180 | */ |
---|
[2911] | 181 | Quest* QuestManager::findQuest(const std::string & questId) |
---|
[1996] | 182 | { |
---|
[8079] | 183 | if(questId == "") // Check for validity of the given id. |
---|
[2068] | 184 | ThrowException(Argument, "Invalid questId."); |
---|
[2092] | 185 | |
---|
[1996] | 186 | Quest* quest; |
---|
[2911] | 187 | std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId); |
---|
[7456] | 188 | if (it != this->questMap_.end()) // If the Quest is registered. |
---|
[2093] | 189 | quest = it->second; |
---|
| 190 | else |
---|
| 191 | { |
---|
| 192 | quest = NULL; |
---|
[8858] | 193 | orxout(internal_warning, context::quests) << "The quest with id {" << questId << "} is nowhere to be found." << endl; |
---|
[2093] | 194 | } |
---|
[2092] | 195 | |
---|
[2093] | 196 | return quest; |
---|
[1996] | 197 | } |
---|
[2092] | 198 | |
---|
[1996] | 199 | /** |
---|
| 200 | @brief |
---|
[2261] | 201 | Finds a QuestHint with the given id. |
---|
[1996] | 202 | @param hintId |
---|
[2261] | 203 | The id of the QuestHint sought for. |
---|
[1996] | 204 | @return |
---|
[2261] | 205 | Returns a pointer to the QuestHint with the input id. |
---|
| 206 | Returns NULL if there is no QuestHint with the given hintId. |
---|
[2068] | 207 | @throws |
---|
| 208 | Throws an exception if the given hintId is invalid. |
---|
[1996] | 209 | */ |
---|
[2911] | 210 | QuestHint* QuestManager::findHint(const std::string & hintId) |
---|
[1996] | 211 | { |
---|
[8079] | 212 | if(hintId == "") // Check for validity of the given id. |
---|
[2068] | 213 | ThrowException(Argument, "Invalid hintId."); |
---|
[2092] | 214 | |
---|
[1996] | 215 | QuestHint* hint; |
---|
[2911] | 216 | std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId); |
---|
[7456] | 217 | if (it != this->hintMap_.end()) // If the QuestHint is registered. |
---|
[2093] | 218 | hint = it->second; |
---|
| 219 | else |
---|
| 220 | { |
---|
| 221 | hint = NULL; |
---|
[8858] | 222 | orxout(internal_warning, context::quests) << "The hint with id {" << hintId << "} is nowhere to be found." << endl; |
---|
[2093] | 223 | } |
---|
[2092] | 224 | |
---|
[2093] | 225 | return hint; |
---|
[1996] | 226 | } |
---|
| 227 | |
---|
[7456] | 228 | /** |
---|
| 229 | @brief |
---|
| 230 | Get the number of Quests the input player has, that are root quests. |
---|
| 231 | @param player |
---|
| 232 | The player. |
---|
| 233 | @return |
---|
| 234 | Returns the number of Quests the input player has, that are root quests. |
---|
| 235 | */ |
---|
| 236 | int QuestManager::getNumRootQuests(PlayerInfo* player) |
---|
[2963] | 237 | { |
---|
[7163] | 238 | int numQuests = 0; |
---|
| 239 | for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++) |
---|
| 240 | { |
---|
| 241 | if(it->second->getParentQuest() == NULL && !it->second->isInactive(player)) |
---|
| 242 | numQuests++; |
---|
| 243 | } |
---|
| 244 | return numQuests; |
---|
| 245 | } |
---|
[5693] | 246 | |
---|
[7456] | 247 | /** |
---|
| 248 | @brief |
---|
| 249 | Get the index-th root quest of the input player. |
---|
| 250 | @param player |
---|
| 251 | The player. |
---|
| 252 | @param index |
---|
| 253 | The index of the root quest. |
---|
| 254 | @return |
---|
| 255 | Returns the index-th root quest of the input player. |
---|
| 256 | */ |
---|
| 257 | Quest* QuestManager::getRootQuest(PlayerInfo* player, int index) |
---|
[7163] | 258 | { |
---|
| 259 | for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++) |
---|
| 260 | { |
---|
| 261 | if(it->second->getParentQuest() == NULL && !it->second->isInactive(player) && index-- == 0) |
---|
| 262 | return it->second; |
---|
| 263 | } |
---|
| 264 | return NULL; |
---|
| 265 | } |
---|
[5693] | 266 | |
---|
[7456] | 267 | /** |
---|
| 268 | @brief |
---|
| 269 | Get the number of sub-quest of an input Quest for the input player. |
---|
| 270 | @param quest |
---|
| 271 | The quest to get the sub-quests of. |
---|
| 272 | @param player |
---|
| 273 | The player. |
---|
| 274 | @return |
---|
| 275 | Returns the number of sub-quest of an input Quest for the input player. |
---|
| 276 | */ |
---|
[7163] | 277 | int QuestManager::getNumSubQuests(Quest* quest, PlayerInfo* player) |
---|
| 278 | { |
---|
[8706] | 279 | if(quest == NULL) |
---|
| 280 | return this->getNumRootQuests(player); |
---|
| 281 | |
---|
[7163] | 282 | std::list<Quest*> quests = quest->getSubQuestList(); |
---|
| 283 | int numQuests = 0; |
---|
| 284 | for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++) |
---|
| 285 | { |
---|
| 286 | if(!(*it)->isInactive(player)) |
---|
| 287 | numQuests++; |
---|
| 288 | } |
---|
| 289 | return numQuests; |
---|
[2963] | 290 | } |
---|
| 291 | |
---|
[7456] | 292 | /** |
---|
| 293 | @brief |
---|
| 294 | Get the index-th sub-quest of the input Quest for the input player. |
---|
| 295 | @param quest |
---|
| 296 | The Quest to get the sub-quest of. |
---|
| 297 | @param player |
---|
| 298 | The player. |
---|
| 299 | @param index |
---|
| 300 | The index of the sub-quest. |
---|
| 301 | */ |
---|
[7163] | 302 | Quest* QuestManager::getSubQuest(Quest* quest, PlayerInfo* player, int index) |
---|
| 303 | { |
---|
[8706] | 304 | if(quest == NULL) |
---|
| 305 | return this->getRootQuest(player, index); |
---|
| 306 | |
---|
[7163] | 307 | std::list<Quest*> quests = quest->getSubQuestList(); |
---|
| 308 | for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++) |
---|
| 309 | { |
---|
| 310 | if(!(*it)->isInactive(player) && index-- == 0) |
---|
| 311 | return *it; |
---|
| 312 | } |
---|
| 313 | return NULL; |
---|
| 314 | } |
---|
| 315 | |
---|
[7456] | 316 | /** |
---|
| 317 | @brief |
---|
| 318 | Get the number of QuestHints of the input Quest for the input player. |
---|
| 319 | @param quest |
---|
| 320 | The quest to get the hints of. |
---|
| 321 | @param player |
---|
| 322 | The player. |
---|
[8706] | 323 | @return |
---|
| 324 | Returns the number of QuestHints of the input Quest for the input player. |
---|
[7456] | 325 | */ |
---|
[7163] | 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 | |
---|
[7456] | 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. |
---|
[8706] | 347 | @return |
---|
| 348 | Returns a pointer to the index-th QuestHint of the input Quest for the input player. |
---|
[7456] | 349 | */ |
---|
[7163] | 350 | QuestHint* QuestManager::getHints(Quest* quest, PlayerInfo* player, int index) |
---|
| 351 | { |
---|
| 352 | std::list<QuestHint*> hints = quest->getHintsList(); |
---|
| 353 | for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++) |
---|
| 354 | { |
---|
| 355 | if((*it)->isActive(player) && index-- == 0) |
---|
| 356 | return *it; |
---|
| 357 | } |
---|
| 358 | return NULL; |
---|
| 359 | } |
---|
| 360 | |
---|
[7456] | 361 | /** |
---|
| 362 | @brief |
---|
[8706] | 363 | Get the parent-quest of the input Quest. |
---|
| 364 | @param quest |
---|
| 365 | The Quest to get the parent-quest of. |
---|
| 366 | @return |
---|
| 367 | Returns a pointer to the parent-quest of the input Quest. |
---|
| 368 | */ |
---|
| 369 | Quest* QuestManager::getParentQuest(Quest* quest) |
---|
| 370 | { |
---|
[8891] | 371 | OrxAssert(quest, "The input Quest is NULL."); |
---|
[8706] | 372 | return quest->getParentQuest(); |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | /** |
---|
| 376 | @brief |
---|
[7456] | 377 | Get the QuestDescription of the input Quest. |
---|
| 378 | @param item |
---|
| 379 | The Quest to get the QuestDescription of. |
---|
| 380 | @return |
---|
| 381 | Return a pointer ot the QuestDescription of the input Quest. |
---|
| 382 | */ |
---|
[7163] | 383 | QuestDescription* QuestManager::getDescription(Quest* item) |
---|
| 384 | { |
---|
[8891] | 385 | OrxAssert(item, "The input Quest is NULL."); |
---|
[7163] | 386 | return item->getDescription(); |
---|
| 387 | } |
---|
| 388 | |
---|
[7456] | 389 | /** |
---|
| 390 | @brief |
---|
| 391 | Get the QuestDescription of the input QuestHint. |
---|
| 392 | @param item |
---|
| 393 | The QuestHint to get the QuestDescription of. |
---|
| 394 | @return |
---|
| 395 | Returns a pointer to the QuestDescription of the input QuestHint. |
---|
| 396 | */ |
---|
[7163] | 397 | QuestDescription* QuestManager::getDescription(QuestHint* item) |
---|
| 398 | { |
---|
[8891] | 399 | OrxAssert(item, "The input QuestHint is NULL."); |
---|
[7163] | 400 | return item->getDescription(); |
---|
| 401 | } |
---|
| 402 | |
---|
[2993] | 403 | /** |
---|
| 404 | @brief |
---|
[8706] | 405 | Get the id of the input Quest. |
---|
| 406 | @param item |
---|
| 407 | The Quest to get the id of. |
---|
| 408 | @return |
---|
| 409 | Returns the id of the input Quest. |
---|
| 410 | */ |
---|
| 411 | const std::string QuestManager::getId(Quest* item) const |
---|
| 412 | { |
---|
[8891] | 413 | OrxAssert(item, "The input Quest is NULL."); |
---|
[8706] | 414 | return item->getId(); |
---|
| 415 | } |
---|
| 416 | |
---|
| 417 | /** |
---|
| 418 | @brief |
---|
| 419 | Get the id of the input QuestHint. |
---|
| 420 | @param item |
---|
| 421 | The QuestHint to get the id of. |
---|
| 422 | @return |
---|
| 423 | Returns the id of the input QuestHint. |
---|
| 424 | */ |
---|
| 425 | const std::string QuestManager::getId(QuestHint* item) const |
---|
| 426 | { |
---|
[8891] | 427 | OrxAssert(item, "The input QuestHint is NULL."); |
---|
[8706] | 428 | return item->getId(); |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | /** |
---|
| 432 | @brief |
---|
[5745] | 433 | Retrieve the player for a certain GUI. |
---|
| 434 | @param guiName |
---|
| 435 | The name of the GUI the player is retrieved for. |
---|
[2993] | 436 | @return |
---|
[5745] | 437 | Returns the player. |
---|
| 438 | @todo |
---|
| 439 | This very well might be outdated. So: Check if still needed, and update if necessary. |
---|
[2993] | 440 | */ |
---|
[6536] | 441 | PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName) |
---|
[2963] | 442 | { |
---|
[5745] | 443 | PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName); |
---|
| 444 | if(player == NULL) |
---|
[2963] | 445 | { |
---|
[8858] | 446 | orxout(internal_error, context::quests) << "GUIOverlay with name '" << guiName << "' has no player." << endl; |
---|
[2993] | 447 | return NULL; |
---|
| 448 | } |
---|
[5693] | 449 | |
---|
[5745] | 450 | return player; |
---|
[2963] | 451 | } |
---|
| 452 | |
---|
[1996] | 453 | } |
---|