| 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 "util/Exception.h" | 
|---|
| 37 | #include "core/CoreIncludes.h" | 
|---|
| 38 |  | 
|---|
| 39 | #include "objects/infos/PlayerInfo.h" | 
|---|
| 40 | #include "objects/infos/PlayerInfo.h" | 
|---|
| 41 | #include "overlays/GUIOverlay.h" | 
|---|
| 42 | #include "Quest.h" | 
|---|
| 43 | #include "QuestHint.h" | 
|---|
| 44 | #include "QuestItem.h" | 
|---|
| 45 |  | 
|---|
| 46 | namespace orxonox | 
|---|
| 47 | { | 
|---|
| 48 |     //! Pointer to the current (and single) instance of this class. | 
|---|
| 49 |     /*static*/ QuestManager* QuestManager::singletonRef_s = NULL; | 
|---|
| 50 |  | 
|---|
| 51 |     /** | 
|---|
| 52 |     @brief | 
|---|
| 53 |         Constructor. Registers the object. | 
|---|
| 54 |     @todo | 
|---|
| 55 |         Is inheriting from BaseObject proper? | 
|---|
| 56 |     */ | 
|---|
| 57 |     QuestManager::QuestManager() | 
|---|
| 58 |     { | 
|---|
| 59 |         RegisterRootObject(QuestManager); | 
|---|
| 60 |  | 
|---|
| 61 |         assert(singletonRef_s == 0); | 
|---|
| 62 |         singletonRef_s = this; | 
|---|
| 63 |     } | 
|---|
| 64 |  | 
|---|
| 65 |     /** | 
|---|
| 66 |     @brief | 
|---|
| 67 |         Destructor. | 
|---|
| 68 |     */ | 
|---|
| 69 |     QuestManager::~QuestManager() | 
|---|
| 70 |     { | 
|---|
| 71 |  | 
|---|
| 72 |     } | 
|---|
| 73 |  | 
|---|
| 74 |     /** | 
|---|
| 75 |     @brief | 
|---|
| 76 |         Returns a reference to the current (and single) instance of the QuestManager, and creates one if there isn't one to begin with. | 
|---|
| 77 |     @return | 
|---|
| 78 |         Returns a reference to the single instance of the Quest Manager. | 
|---|
| 79 |     */ | 
|---|
| 80 |     /*static*/ QuestManager & QuestManager::getInstance() | 
|---|
| 81 |     { | 
|---|
| 82 |         assert(singletonRef_s); | 
|---|
| 83 |         return *singletonRef_s; | 
|---|
| 84 |     } | 
|---|
| 85 |  | 
|---|
| 86 |     /** | 
|---|
| 87 |     @brief | 
|---|
| 88 |         Registers a Quest with the QuestManager to make it globally accessable. | 
|---|
| 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 |         if(quest == NULL) //!< Doh! Just as if there were actual quests behind NULL-pointers. | 
|---|
| 98 |         { | 
|---|
| 99 |             COUT(2) << "Registration of Quest in QuestManager failed, because inserted Quest-pointer was NULL." << std::endl; | 
|---|
| 100 |             return false; | 
|---|
| 101 |         } | 
|---|
| 102 |  | 
|---|
| 103 |         std::pair<std::map<std::string, Quest*>::iterator,bool> result; | 
|---|
| 104 |         result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); //!< Inserting the Quest. | 
|---|
| 105 |  | 
|---|
| 106 |         if(result.second) //!< If inserting was a success. | 
|---|
| 107 |         { | 
|---|
| 108 |             COUT(3) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl; | 
|---|
| 109 |             return true; | 
|---|
| 110 |         } | 
|---|
| 111 |         else | 
|---|
| 112 |         { | 
|---|
| 113 |            COUT(2) << "Quest with the same id was already present." << std::endl; | 
|---|
| 114 |            return false; | 
|---|
| 115 |         } | 
|---|
| 116 |     } | 
|---|
| 117 |  | 
|---|
| 118 |     /** | 
|---|
| 119 |     @brief | 
|---|
| 120 |         Registers a QuestHint with the QuestManager to make it globally accessable. | 
|---|
| 121 |         Uses it's id to make sure to be able to be identify and retrieve it later. | 
|---|
| 122 |     @param hint | 
|---|
| 123 |         The QuestHint to be registered. | 
|---|
| 124 |     @return | 
|---|
| 125 |         Returns true if successful, false if not. | 
|---|
| 126 |     */ | 
|---|
| 127 |     bool QuestManager::registerHint(QuestHint* hint) | 
|---|
| 128 |     { | 
|---|
| 129 |         if(hint == NULL) //!< Still not liking NULL-pointers. | 
|---|
| 130 |         { | 
|---|
| 131 |             COUT(2) << "Registration of QuestHint in QuestManager failed, because inserted QuestHint-pointer was NULL." << std::endl; | 
|---|
| 132 |             return false; | 
|---|
| 133 |         } | 
|---|
| 134 |  | 
|---|
| 135 |         std::pair<std::map<std::string, QuestHint*>::iterator,bool> result; | 
|---|
| 136 |         result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); //!< Inserting the QuestHSint. | 
|---|
| 137 |  | 
|---|
| 138 |         if(result.second) //!< If inserting was a success. | 
|---|
| 139 |         { | 
|---|
| 140 |             COUT(3) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl; | 
|---|
| 141 |             return true; | 
|---|
| 142 |         } | 
|---|
| 143 |         else | 
|---|
| 144 |         { | 
|---|
| 145 |            COUT(2) << "QuestHint with the same id was already present." << std::endl; | 
|---|
| 146 |            return false; | 
|---|
| 147 |         } | 
|---|
| 148 |     } | 
|---|
| 149 |  | 
|---|
| 150 |     /** | 
|---|
| 151 |     @brief | 
|---|
| 152 |         Finds a Quest with the given id. | 
|---|
| 153 |     @param questId | 
|---|
| 154 |         The id of the Quest sought for. | 
|---|
| 155 |     @return | 
|---|
| 156 |         Returns a pointer to the Quest with the input id. | 
|---|
| 157 |         Returns NULL if there is no Quest with the given questId. | 
|---|
| 158 |     @throws | 
|---|
| 159 |         Throws an exception if the given questId is invalid. | 
|---|
| 160 |     */ | 
|---|
| 161 |     Quest* QuestManager::findQuest(const std::string & questId) | 
|---|
| 162 |     { | 
|---|
| 163 |         if(!QuestItem::isId(questId)) //!< Check vor validity of the given id. | 
|---|
| 164 |         { | 
|---|
| 165 |             ThrowException(Argument, "Invalid questId."); | 
|---|
| 166 |         } | 
|---|
| 167 |  | 
|---|
| 168 |         Quest* quest; | 
|---|
| 169 |         std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId); | 
|---|
| 170 |         if (it != this->questMap_.end()) //!< If the Quest is registered. | 
|---|
| 171 |         { | 
|---|
| 172 |             quest = it->second; | 
|---|
| 173 |         } | 
|---|
| 174 |         else | 
|---|
| 175 |         { | 
|---|
| 176 |            quest = NULL; | 
|---|
| 177 |            COUT(2) << "The quest with id {" << questId << "} is nowhere to be found." << std::endl; | 
|---|
| 178 |         } | 
|---|
| 179 |  | 
|---|
| 180 |         return quest; | 
|---|
| 181 |  | 
|---|
| 182 |     } | 
|---|
| 183 |  | 
|---|
| 184 |     /** | 
|---|
| 185 |     @brief | 
|---|
| 186 |         Finds a QuestHint with the given id. | 
|---|
| 187 |     @param hintId | 
|---|
| 188 |         The id of the QuestHint sought for. | 
|---|
| 189 |     @return | 
|---|
| 190 |         Returns a pointer to the QuestHint with the input id. | 
|---|
| 191 |         Returns NULL if there is no QuestHint with the given hintId. | 
|---|
| 192 |     @throws | 
|---|
| 193 |         Throws an exception if the given hintId is invalid. | 
|---|
| 194 |     */ | 
|---|
| 195 |     QuestHint* QuestManager::findHint(const std::string & hintId) | 
|---|
| 196 |     { | 
|---|
| 197 |         if(!QuestItem::isId(hintId)) //!< Check vor validity of the given id. | 
|---|
| 198 |         { | 
|---|
| 199 |             ThrowException(Argument, "Invalid hintId."); | 
|---|
| 200 |         } | 
|---|
| 201 |  | 
|---|
| 202 |         QuestHint* hint; | 
|---|
| 203 |         std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId); | 
|---|
| 204 |         if (it != this->hintMap_.end()) //!< If the QuestHint is registered. | 
|---|
| 205 |         { | 
|---|
| 206 |             hint = it->second; | 
|---|
| 207 |         } | 
|---|
| 208 |         else | 
|---|
| 209 |         { | 
|---|
| 210 |            hint = NULL; | 
|---|
| 211 |            COUT(2) << "The hint with id {" << hintId << "} is nowhere to be found." << std::endl; | 
|---|
| 212 |         } | 
|---|
| 213 |  | 
|---|
| 214 |         return hint; | 
|---|
| 215 |  | 
|---|
| 216 |     } | 
|---|
| 217 |  | 
|---|
| 218 |     /** | 
|---|
| 219 |     @brief | 
|---|
| 220 |          | 
|---|
| 221 |     @param name | 
|---|
| 222 |     @return | 
|---|
| 223 |     */ | 
|---|
| 224 |     QuestContainer* QuestManager::getQuestTree(std::string & name) | 
|---|
| 225 |     { | 
|---|
| 226 |         GUIOverlay* gui = NULL; | 
|---|
| 227 |         for (ObjectList<GUIOverlay>::iterator it = ObjectList<GUIOverlay>::begin(); it != ObjectList<GUIOverlay>::end(); ++it) | 
|---|
| 228 |             if (it->getGUIName() == name) | 
|---|
| 229 |                 gui = *it; | 
|---|
| 230 |  | 
|---|
| 231 |         PlayerInfo* player; | 
|---|
| 232 |         if(gui == NULL) | 
|---|
| 233 |         { | 
|---|
| 234 |             COUT(1) << "Error: No GUIOverlay with the given name '" << name << "' present." << std::endl; | 
|---|
| 235 |             return NULL; | 
|---|
| 236 |         } | 
|---|
| 237 |         BaseObject* obj = gui->getOwner(); | 
|---|
| 238 |         if(obj == NULL) | 
|---|
| 239 |         { | 
|---|
| 240 |             COUT(1) << "Error: GUIOverlay has no owner. " << std::endl; | 
|---|
| 241 |             return NULL; | 
|---|
| 242 |         } | 
|---|
| 243 |         player = orxonox_cast<PlayerInfo*>(obj); | 
|---|
| 244 |      | 
|---|
| 245 |         QuestContainer* root = NULL; | 
|---|
| 246 |         QuestContainer* current = NULL; | 
|---|
| 247 |          | 
|---|
| 248 |         std::list<Quest*>* rootQuests = new std::list<Quest*>(); | 
|---|
| 249 |         getRootQuests(player, *rootQuests); | 
|---|
| 250 |          | 
|---|
| 251 |         for(std::list<Quest*>::iterator it = rootQuests->begin(); it != rootQuests->end(); it++) | 
|---|
| 252 |         { | 
|---|
| 253 |             QuestContainer* container = addSubQuest(*it, player); | 
|---|
| 254 |  | 
|---|
| 255 |             if(root == NULL) | 
|---|
| 256 |             { | 
|---|
| 257 |                 root = container; | 
|---|
| 258 |             } | 
|---|
| 259 |             else | 
|---|
| 260 |             { | 
|---|
| 261 |                 current->next = container; | 
|---|
| 262 |             } | 
|---|
| 263 |              | 
|---|
| 264 |             current = container; | 
|---|
| 265 |  | 
|---|
| 266 |         } | 
|---|
| 267 |         if(current != NULL) | 
|---|
| 268 |             current->next = NULL; | 
|---|
| 269 |  | 
|---|
| 270 |         delete rootQuests; | 
|---|
| 271 |  | 
|---|
| 272 |         return root; | 
|---|
| 273 |     } | 
|---|
| 274 |  | 
|---|
| 275 |     /** | 
|---|
| 276 |     @brief | 
|---|
| 277 |          | 
|---|
| 278 |     @param player | 
|---|
| 279 |     @param list | 
|---|
| 280 |     @return | 
|---|
| 281 |     */ | 
|---|
| 282 |     void QuestManager::getRootQuests(const PlayerInfo* player, std::list<Quest*> & list) | 
|---|
| 283 |     { | 
|---|
| 284 |         for(std::map<std::string, Quest*>::iterator it=this->questMap_.begin(); it!=this->questMap_.end(); it++) | 
|---|
| 285 |         { | 
|---|
| 286 |             Quest* quest = (*it).second; | 
|---|
| 287 |             if(quest->getParentQuest() == NULL && !quest->isInactive(player)) | 
|---|
| 288 |             { | 
|---|
| 289 |                 list.push_back(quest); | 
|---|
| 290 |             } | 
|---|
| 291 |         } | 
|---|
| 292 |     } | 
|---|
| 293 |  | 
|---|
| 294 |     /** | 
|---|
| 295 |     @brief | 
|---|
| 296 |          | 
|---|
| 297 |     @param quest | 
|---|
| 298 |     @param player | 
|---|
| 299 |     @return | 
|---|
| 300 |     */ | 
|---|
| 301 |     QuestContainer* QuestManager::addSubQuest(Quest* quest, const PlayerInfo* player) | 
|---|
| 302 |     { | 
|---|
| 303 |         if(quest == NULL) | 
|---|
| 304 |             return NULL; | 
|---|
| 305 |  | 
|---|
| 306 |         QuestContainer* container = new QuestContainer; | 
|---|
| 307 |         container->description = quest->getDescription(); | 
|---|
| 308 |         container->hint = addHints(quest, player); | 
|---|
| 309 |  | 
|---|
| 310 |         if(quest->isActive(player)) | 
|---|
| 311 |         { | 
|---|
| 312 |             container->status = "active"; | 
|---|
| 313 |         } | 
|---|
| 314 |         else if(quest->isCompleted(player)) | 
|---|
| 315 |         { | 
|---|
| 316 |             container->status = "completed"; | 
|---|
| 317 |         } | 
|---|
| 318 |         else if(quest->isFailed(player)) | 
|---|
| 319 |         { | 
|---|
| 320 |             container->status = "failed"; | 
|---|
| 321 |         } | 
|---|
| 322 |         else | 
|---|
| 323 |         { | 
|---|
| 324 |             container->status = ""; | 
|---|
| 325 |             COUT(1) << "An error occured. A Quest of un-specified status wanted to be displayed." << std::endl; | 
|---|
| 326 |         } | 
|---|
| 327 |          | 
|---|
| 328 |         std::list<Quest*> quests = quest->getSubQuestList(); | 
|---|
| 329 |         QuestContainer* current = NULL; | 
|---|
| 330 |         QuestContainer* first = NULL; | 
|---|
| 331 |         for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++) | 
|---|
| 332 |         { | 
|---|
| 333 |             Quest* subQuest = *it; | 
|---|
| 334 |             if(!subQuest->isInactive(player)) | 
|---|
| 335 |             { | 
|---|
| 336 |                 QuestContainer* subContainer = addSubQuest(subQuest, player); | 
|---|
| 337 |  | 
|---|
| 338 |                 if(first == NULL) | 
|---|
| 339 |                 { | 
|---|
| 340 |                     first = subContainer; | 
|---|
| 341 |                 } | 
|---|
| 342 |                 else | 
|---|
| 343 |                 { | 
|---|
| 344 |                     current->next = subContainer; | 
|---|
| 345 |                 } | 
|---|
| 346 |                  | 
|---|
| 347 |                 current = subContainer; | 
|---|
| 348 |             } | 
|---|
| 349 |         } | 
|---|
| 350 |         if(current != NULL) | 
|---|
| 351 |             current->next = NULL; | 
|---|
| 352 |         container->subQuests = first; | 
|---|
| 353 |          | 
|---|
| 354 |         return container; | 
|---|
| 355 |     } | 
|---|
| 356 |  | 
|---|
| 357 |     /** | 
|---|
| 358 |     @brief | 
|---|
| 359 |          | 
|---|
| 360 |     @param quest | 
|---|
| 361 |     @param player | 
|---|
| 362 |     @return | 
|---|
| 363 |     */ | 
|---|
| 364 |     HintContainer* QuestManager::addHints(Quest* quest, const PlayerInfo* player) | 
|---|
| 365 |     { | 
|---|
| 366 |         HintContainer* current = NULL; | 
|---|
| 367 |         HintContainer* first = NULL; | 
|---|
| 368 |  | 
|---|
| 369 |         std::list<QuestHint*> hints = quest->getHintsList(); | 
|---|
| 370 |         for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++) | 
|---|
| 371 |         { | 
|---|
| 372 |             if((*it)->isActive(player)) | 
|---|
| 373 |             { | 
|---|
| 374 |                 HintContainer* hint = new HintContainer; | 
|---|
| 375 |                 hint->description = (*it)->getDescription(); | 
|---|
| 376 |  | 
|---|
| 377 |                 if(first == NULL) | 
|---|
| 378 |                 { | 
|---|
| 379 |                     first = hint; | 
|---|
| 380 |                 } | 
|---|
| 381 |                 else | 
|---|
| 382 |                 { | 
|---|
| 383 |                     current->next = hint; | 
|---|
| 384 |                 } | 
|---|
| 385 |                  | 
|---|
| 386 |                 current = hint; | 
|---|
| 387 |             } | 
|---|
| 388 |         } | 
|---|
| 389 |  | 
|---|
| 390 |         if(current != NULL) | 
|---|
| 391 |             current->next = NULL; | 
|---|
| 392 |         return first; | 
|---|
| 393 |     } | 
|---|
| 394 |  | 
|---|
| 395 |  | 
|---|
| 396 | } | 
|---|