| [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" |
|---|
| [7284] | 37 | #include "util/ScopedSingletonManager.h" |
|---|
| [7456] | 38 | #include "core/command/ConsoleCommand.h" |
|---|
| [1996] | 39 | #include "core/CoreIncludes.h" |
|---|
| [5693] | 40 | #include "core/GUIManager.h" |
|---|
| [5755] | 41 | #include "core/LuaState.h" |
|---|
| [7456] | 42 | |
|---|
| [5748] | 43 | #include "infos/PlayerInfo.h" |
|---|
| [2105] | 44 | |
|---|
| [2095] | 45 | #include "Quest.h" |
|---|
| 46 | #include "QuestHint.h" |
|---|
| [5748] | 47 | #include "QuestItem.h" |
|---|
| [1996] | 48 | |
|---|
| [2662] | 49 | namespace orxonox |
|---|
| 50 | { |
|---|
| [5929] | 51 | ManageScopedSingleton(QuestManager, ScopeID::Root, false); |
|---|
| [1996] | 52 | |
|---|
| [2261] | 53 | /** |
|---|
| 54 | @brief |
|---|
| 55 | Constructor. Registers the object. |
|---|
| [2911] | 56 | @todo |
|---|
| 57 | Is inheriting from BaseObject proper? |
|---|
| [2261] | 58 | */ |
|---|
| [2911] | 59 | QuestManager::QuestManager() |
|---|
| [1996] | 60 | { |
|---|
| [2911] | 61 | RegisterRootObject(QuestManager); |
|---|
| [7456] | 62 | |
|---|
| 63 | COUT(3) << "QuestManager created." << std::endl; |
|---|
| [1996] | 64 | } |
|---|
| [2092] | 65 | |
|---|
| [2261] | 66 | /** |
|---|
| 67 | @brief |
|---|
| 68 | Destructor. |
|---|
| 69 | */ |
|---|
| [1996] | 70 | QuestManager::~QuestManager() |
|---|
| 71 | { |
|---|
| [7456] | 72 | COUT(3) << "QuestManager destroyed." << std::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 | { |
|---|
| [7552] | 97 | assert(quest); |
|---|
| [2092] | 98 | |
|---|
| [2261] | 99 | std::pair<std::map<std::string, Quest*>::iterator,bool> result; |
|---|
| [7456] | 100 | result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); // Inserting the Quest. |
|---|
| [2092] | 101 | |
|---|
| [7456] | 102 | if(result.second) // If inserting was a success. |
|---|
| [2068] | 103 | { |
|---|
| [7163] | 104 | quest->setRegistered(); |
|---|
| 105 | COUT(4) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl; |
|---|
| [2068] | 106 | return true; |
|---|
| [2093] | 107 | } |
|---|
| 108 | else |
|---|
| 109 | { |
|---|
| 110 | COUT(2) << "Quest with the same id was already present." << std::endl; |
|---|
| 111 | return false; |
|---|
| 112 | } |
|---|
| [1996] | 113 | } |
|---|
| [2092] | 114 | |
|---|
| [1996] | 115 | /** |
|---|
| 116 | @brief |
|---|
| [7163] | 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 |
|---|
| [8079] | 126 | Registers a QuestHint with the QuestManager to make it globally accessible. |
|---|
| [2261] | 127 | Uses it's id to make sure to be able to be identify and retrieve it later. |
|---|
| [1996] | 128 | @param hint |
|---|
| 129 | The QuestHint to be registered. |
|---|
| 130 | @return |
|---|
| 131 | Returns true if successful, false if not. |
|---|
| 132 | */ |
|---|
| [2911] | 133 | bool QuestManager::registerHint(QuestHint* hint) |
|---|
| [1996] | 134 | { |
|---|
| [7552] | 135 | assert(hint); |
|---|
| [2092] | 136 | |
|---|
| [2261] | 137 | std::pair<std::map<std::string, QuestHint*>::iterator,bool> result; |
|---|
| [7456] | 138 | result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); // Inserting the QuestHSint. |
|---|
| [2092] | 139 | |
|---|
| [7456] | 140 | if(result.second) // If inserting was a success. |
|---|
| [2068] | 141 | { |
|---|
| [7163] | 142 | hint->setRegistered(); |
|---|
| 143 | COUT(4) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl; |
|---|
| [2068] | 144 | return true; |
|---|
| [2093] | 145 | } |
|---|
| 146 | else |
|---|
| 147 | { |
|---|
| 148 | COUT(2) << "QuestHint with the same id was already present." << std::endl; |
|---|
| 149 | return false; |
|---|
| 150 | } |
|---|
| [1996] | 151 | } |
|---|
| [2092] | 152 | |
|---|
| [1996] | 153 | /** |
|---|
| 154 | @brief |
|---|
| [7163] | 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 |
|---|
| [2261] | 164 | Finds a Quest with the given id. |
|---|
| [1996] | 165 | @param questId |
|---|
| [2261] | 166 | The id of the Quest sought for. |
|---|
| [1996] | 167 | @return |
|---|
| [2261] | 168 | Returns a pointer to the Quest with the input id. |
|---|
| 169 | Returns NULL if there is no Quest with the given questId. |
|---|
| [2068] | 170 | @throws |
|---|
| 171 | Throws an exception if the given questId is invalid. |
|---|
| [1996] | 172 | */ |
|---|
| [2911] | 173 | Quest* QuestManager::findQuest(const std::string & questId) |
|---|
| [1996] | 174 | { |
|---|
| [8079] | 175 | if(questId == "") // Check for validity of the given id. |
|---|
| [2068] | 176 | ThrowException(Argument, "Invalid questId."); |
|---|
| [2092] | 177 | |
|---|
| [1996] | 178 | Quest* quest; |
|---|
| [2911] | 179 | std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId); |
|---|
| [7456] | 180 | if (it != this->questMap_.end()) // If the Quest is registered. |
|---|
| [2093] | 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 | } |
|---|
| [2092] | 187 | |
|---|
| [2093] | 188 | return quest; |
|---|
| [1996] | 189 | } |
|---|
| [2092] | 190 | |
|---|
| [1996] | 191 | /** |
|---|
| 192 | @brief |
|---|
| [2261] | 193 | Finds a QuestHint with the given id. |
|---|
| [1996] | 194 | @param hintId |
|---|
| [2261] | 195 | The id of the QuestHint sought for. |
|---|
| [1996] | 196 | @return |
|---|
| [2261] | 197 | Returns a pointer to the QuestHint with the input id. |
|---|
| 198 | Returns NULL if there is no QuestHint with the given hintId. |
|---|
| [2068] | 199 | @throws |
|---|
| 200 | Throws an exception if the given hintId is invalid. |
|---|
| [1996] | 201 | */ |
|---|
| [2911] | 202 | QuestHint* QuestManager::findHint(const std::string & hintId) |
|---|
| [1996] | 203 | { |
|---|
| [8079] | 204 | if(hintId == "") // Check for validity of the given id. |
|---|
| [2068] | 205 | ThrowException(Argument, "Invalid hintId."); |
|---|
| [2092] | 206 | |
|---|
| [1996] | 207 | QuestHint* hint; |
|---|
| [2911] | 208 | std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId); |
|---|
| [7456] | 209 | if (it != this->hintMap_.end()) // If the QuestHint is registered. |
|---|
| [2093] | 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 | } |
|---|
| [2092] | 216 | |
|---|
| [2093] | 217 | return hint; |
|---|
| [1996] | 218 | } |
|---|
| 219 | |
|---|
| [7456] | 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) |
|---|
| [2963] | 229 | { |
|---|
| [7163] | 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 | } |
|---|
| [5693] | 238 | |
|---|
| [7456] | 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) |
|---|
| [7163] | 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 | } |
|---|
| [5693] | 258 | |
|---|
| [7456] | 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 | */ |
|---|
| [7163] | 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; |
|---|
| [2963] | 279 | } |
|---|
| 280 | |
|---|
| [7456] | 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 | */ |
|---|
| [7163] | 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 | |
|---|
| [7456] | 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 | */ |
|---|
| [7163] | 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 | |
|---|
| [7456] | 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 | */ |
|---|
| [7163] | 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 | |
|---|
| [7456] | 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 | */ |
|---|
| [7163] | 352 | QuestDescription* QuestManager::getDescription(Quest* item) |
|---|
| 353 | { |
|---|
| 354 | return item->getDescription(); |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| [7456] | 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 | */ |
|---|
| [7163] | 365 | QuestDescription* QuestManager::getDescription(QuestHint* item) |
|---|
| 366 | { |
|---|
| 367 | return item->getDescription(); |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| [2993] | 370 | /** |
|---|
| 371 | @brief |
|---|
| [5745] | 372 | Retrieve the player for a certain GUI. |
|---|
| 373 | @param guiName |
|---|
| 374 | The name of the GUI the player is retrieved for. |
|---|
| [2993] | 375 | @return |
|---|
| [5745] | 376 | Returns the player. |
|---|
| 377 | @todo |
|---|
| 378 | This very well might be outdated. So: Check if still needed, and update if necessary. |
|---|
| [2993] | 379 | */ |
|---|
| [6536] | 380 | PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName) |
|---|
| [2963] | 381 | { |
|---|
| [5745] | 382 | PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName); |
|---|
| 383 | if(player == NULL) |
|---|
| [2963] | 384 | { |
|---|
| [5745] | 385 | COUT(1) << "Error: GUIOverlay with name '" << guiName << "' has no player." << std::endl; |
|---|
| [2993] | 386 | return NULL; |
|---|
| 387 | } |
|---|
| [5693] | 388 | |
|---|
| [5745] | 389 | return player; |
|---|
| [2963] | 390 | } |
|---|
| 391 | |
|---|
| [1996] | 392 | } |
|---|