| 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 Quest.cc | 
|---|
| 31 |     @brief Implementation of the Quest class. | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #include "OrxonoxStableHeaders.h" | 
|---|
| 35 | #include "Quest.h" | 
|---|
| 36 |  | 
|---|
| 37 | #include "core/CoreIncludes.h" | 
|---|
| 38 |  | 
|---|
| 39 | #include "orxonox/objects/infos/PlayerInfo.h" | 
|---|
| 40 | #include "QuestManager.h" | 
|---|
| 41 | #include "QuestDescription.h" | 
|---|
| 42 | #include "QuestHint.h" | 
|---|
| 43 | #include "QuestEffect.h" | 
|---|
| 44 | #include "QuestListener.h" | 
|---|
| 45 |  | 
|---|
| 46 | namespace orxonox | 
|---|
| 47 | { | 
|---|
| 48 |     /** | 
|---|
| 49 |     @brief | 
|---|
| 50 |         Constructor. Registers and initializes object. | 
|---|
| 51 |     */ | 
|---|
| 52 |     Quest::Quest(BaseObject* creator) : QuestItem(creator) | 
|---|
| 53 |     { | 
|---|
| 54 |         RegisterObject(Quest); | 
|---|
| 55 |  | 
|---|
| 56 |         this->parentQuest_ = NULL; | 
|---|
| 57 |     } | 
|---|
| 58 |  | 
|---|
| 59 |     /** | 
|---|
| 60 |     @brief | 
|---|
| 61 |         Destructor. | 
|---|
| 62 |     */ | 
|---|
| 63 |     Quest::~Quest() | 
|---|
| 64 |     { | 
|---|
| 65 |  | 
|---|
| 66 |     } | 
|---|
| 67 |  | 
|---|
| 68 |     /** | 
|---|
| 69 |     @brief | 
|---|
| 70 |         Method for creating a Quest object through XML. | 
|---|
| 71 |     */ | 
|---|
| 72 |     void Quest::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 73 |     { | 
|---|
| 74 |         SUPER(Quest, XMLPort, xmlelement, mode); | 
|---|
| 75 |  | 
|---|
| 76 |         XMLPortObject(Quest, Quest, "subquests", addSubQuest, getSubQuest, xmlelement, mode); | 
|---|
| 77 |         XMLPortObject(Quest, QuestHint, "hints", addHint, getHint, xmlelement, mode); | 
|---|
| 78 |         XMLPortObject(Quest, QuestEffect, "fail-effects", addFailEffect, getFailEffect, xmlelement, mode); | 
|---|
| 79 |         XMLPortObject(Quest, QuestEffect, "complete-effects", addCompleteEffect, getCompleteEffect, xmlelement, mode); | 
|---|
| 80 |  | 
|---|
| 81 |         QuestManager::getInstance().registerQuest(this); //!<Registers the Quest with the QuestManager. | 
|---|
| 82 |     } | 
|---|
| 83 |  | 
|---|
| 84 |     /** | 
|---|
| 85 |     @brief | 
|---|
| 86 |         Sets the parentquest of the Quest. | 
|---|
| 87 |     @param quest | 
|---|
| 88 |         A pointer to the Quest to be set as parentquest. | 
|---|
| 89 |     @return | 
|---|
| 90 |         Returns true if the parentquest could be set. | 
|---|
| 91 |     */ | 
|---|
| 92 |     bool Quest::setParentQuest(Quest* quest) | 
|---|
| 93 |     { | 
|---|
| 94 |         if(quest == NULL) //!< We don't want to set NULL-Pointers. | 
|---|
| 95 |         { | 
|---|
| 96 |             COUT(2) << "The parentquest to be added to quest {" << this->getId() << "} was NULL." << std::endl; | 
|---|
| 97 |             return false; | 
|---|
| 98 |         } | 
|---|
| 99 |  | 
|---|
| 100 |         this->parentQuest_ = quest; | 
|---|
| 101 |  | 
|---|
| 102 |         COUT(3) << "Parent Quest {" << quest->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl; | 
|---|
| 103 |         return true; | 
|---|
| 104 |     } | 
|---|
| 105 |  | 
|---|
| 106 |     /** | 
|---|
| 107 |     @brief | 
|---|
| 108 |         Adds a subquest to the Quest. | 
|---|
| 109 |     @param quest | 
|---|
| 110 |         A pointer to the Quest to be set as subquest. | 
|---|
| 111 |     @return | 
|---|
| 112 |         Returns true if the subquest could be set. | 
|---|
| 113 |     */ | 
|---|
| 114 |     bool Quest::addSubQuest(Quest* quest) | 
|---|
| 115 |     { | 
|---|
| 116 |         if(quest == NULL) //!< We don't want to set NULL-Pointers. | 
|---|
| 117 |         { | 
|---|
| 118 |             COUT(2) << "The subquest to be added to quest {" << this->getId() << "} was NULL." << std::endl; | 
|---|
| 119 |             return false; | 
|---|
| 120 |         } | 
|---|
| 121 |  | 
|---|
| 122 |         quest->setParentQuest(this); //!< Sets the currentQuest (this) as parentquest for the added subquest. | 
|---|
| 123 |         this->subQuests_.push_back(quest); //!< Adds the Quest to the end of the list of subquests. | 
|---|
| 124 |  | 
|---|
| 125 |         COUT(3) << "Sub Quest {" << quest->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl; | 
|---|
| 126 |         return true; | 
|---|
| 127 |     } | 
|---|
| 128 |  | 
|---|
| 129 |  | 
|---|
| 130 |     /** | 
|---|
| 131 |     @brief | 
|---|
| 132 |         Adds a QuestHint to the list of QuestHints | 
|---|
| 133 |     @param hint | 
|---|
| 134 |         The QuestHint that should be added to the list of QuestHints. | 
|---|
| 135 |     @return | 
|---|
| 136 |         Returns true if the hint was successfully added. | 
|---|
| 137 |     */ | 
|---|
| 138 |     bool Quest::addHint(QuestHint* hint) | 
|---|
| 139 |     { | 
|---|
| 140 |         if(hint == NULL) //!< We don't want to set NULL-Pointers. Seriously! | 
|---|
| 141 |         { | 
|---|
| 142 |             COUT(2) << "A NULL-QuestHint was trying to be added." << std::endl; | 
|---|
| 143 |             return false; | 
|---|
| 144 |         } | 
|---|
| 145 |  | 
|---|
| 146 |         hint->setQuest(this); //!< Sets the current Quest (this) as Quest for the added QuestHint. | 
|---|
| 147 |         this->hints_.push_back(hint); //!< Adds the QuestHint to the end of the list of QuestHints. | 
|---|
| 148 |  | 
|---|
| 149 |         COUT(3) << "QuestHint {" << hint->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl; | 
|---|
| 150 |         return true; | 
|---|
| 151 |     } | 
|---|
| 152 |  | 
|---|
| 153 |     /** | 
|---|
| 154 |     @brief | 
|---|
| 155 |         Adds an QuestEffect to the list of fail QuestEffects. | 
|---|
| 156 |     @param effect | 
|---|
| 157 |         The QuestEffect to be added. | 
|---|
| 158 |     @return | 
|---|
| 159 |         Returns true if successful. | 
|---|
| 160 |     */ | 
|---|
| 161 |     bool Quest::addFailEffect(QuestEffect* effect) | 
|---|
| 162 |     { | 
|---|
| 163 |         if(effect == NULL) //!< We don't want to set NULL-Pointers. | 
|---|
| 164 |         { | 
|---|
| 165 |             COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl; | 
|---|
| 166 |             return false; | 
|---|
| 167 |         } | 
|---|
| 168 |  | 
|---|
| 169 |         this->failEffects_.push_back(effect); //!< Adds the QuestEffect to the end of the list of fail QuestEffects. | 
|---|
| 170 |  | 
|---|
| 171 |         COUT(3) << "A FailEffect was added to Quest {" << this->getId() << "}." << std::endl; | 
|---|
| 172 |         return true; | 
|---|
| 173 |     } | 
|---|
| 174 |  | 
|---|
| 175 |     /** | 
|---|
| 176 |     @brief | 
|---|
| 177 |         Adds an QuestEffect to the list of complete QuestEffects. | 
|---|
| 178 |     @param effect | 
|---|
| 179 |         The QuestEffect to be added. | 
|---|
| 180 |     @return | 
|---|
| 181 |         Returns true if successful. | 
|---|
| 182 |     */ | 
|---|
| 183 |     bool Quest::addCompleteEffect(QuestEffect* effect) | 
|---|
| 184 |     { | 
|---|
| 185 |         if(effect == NULL) //!< We don't want to set NULL-Pointers. | 
|---|
| 186 |         { | 
|---|
| 187 |             COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl; | 
|---|
| 188 |             return false; | 
|---|
| 189 |         } | 
|---|
| 190 |  | 
|---|
| 191 |         this->completeEffects_.push_back(effect); //!< Adds the QuestEffect to the end of the list of complete QuestEffects. | 
|---|
| 192 |  | 
|---|
| 193 |         COUT(3) << "A CompleteEffect was added to Quest {" << this->getId() << "}." << std::endl; | 
|---|
| 194 |         return true; | 
|---|
| 195 |     } | 
|---|
| 196 |  | 
|---|
| 197 |     /** | 
|---|
| 198 |     @brief | 
|---|
| 199 |         Returns the subquest at the given index. | 
|---|
| 200 |     @param | 
|---|
| 201 |         The index. | 
|---|
| 202 |     @return | 
|---|
| 203 |         Returns a pointer to the subquest at the given index. NULL if there is no element at the given index. | 
|---|
| 204 |     */ | 
|---|
| 205 |     const Quest* Quest::getSubQuest(unsigned int index) const | 
|---|
| 206 |     { | 
|---|
| 207 |         int i = index; | 
|---|
| 208 |          | 
|---|
| 209 |         //! Iterate through all subquests. | 
|---|
| 210 |         for (std::list<Quest*>::const_iterator subQuest = this->subQuests_.begin(); subQuest != this->subQuests_.end(); ++subQuest) | 
|---|
| 211 |         { | 
|---|
| 212 |             if(i == 0) //!< We're counting down... | 
|---|
| 213 |             { | 
|---|
| 214 |                return *subQuest; | 
|---|
| 215 |             } | 
|---|
| 216 |             i--; | 
|---|
| 217 |         } | 
|---|
| 218 |          | 
|---|
| 219 |         return NULL; //!< If the index is greater than the number of elements in the list. | 
|---|
| 220 |     } | 
|---|
| 221 |  | 
|---|
| 222 |     /** | 
|---|
| 223 |     @brief | 
|---|
| 224 |         Returns the QuestHint at the given index. | 
|---|
| 225 |     @param | 
|---|
| 226 |         The index. | 
|---|
| 227 |     @return | 
|---|
| 228 |         Returns a pointer to the QuestHint at the given index. NULL if there is no element at the given index. | 
|---|
| 229 |     */ | 
|---|
| 230 |     const QuestHint* Quest::getHint(unsigned int index) const | 
|---|
| 231 |     { | 
|---|
| 232 |         int i = index; | 
|---|
| 233 |          | 
|---|
| 234 |         //! Iterate through all QuestHints. | 
|---|
| 235 |         for (std::list<QuestHint*>::const_iterator hint = this->hints_.begin(); hint != this->hints_.end(); ++hint) | 
|---|
| 236 |         { | 
|---|
| 237 |             if(i == 0) //!< We're counting down... | 
|---|
| 238 |             { | 
|---|
| 239 |                return *hint; | 
|---|
| 240 |             } | 
|---|
| 241 |             i--; | 
|---|
| 242 |         } | 
|---|
| 243 |         return NULL; //!< If the index is greater than the number of elements in the list. | 
|---|
| 244 |     } | 
|---|
| 245 |  | 
|---|
| 246 |     /** | 
|---|
| 247 |     @brief | 
|---|
| 248 |         Returns the fail QuestEffect at the given index. | 
|---|
| 249 |     @param | 
|---|
| 250 |         The index. | 
|---|
| 251 |     @return | 
|---|
| 252 |         Returns a pointer to the fail QuestEffect at the given index. NULL if there is no element at the given index. | 
|---|
| 253 |     */ | 
|---|
| 254 |     const QuestEffect* Quest::getFailEffect(unsigned int index) const | 
|---|
| 255 |     { | 
|---|
| 256 |         int i = index; | 
|---|
| 257 |          | 
|---|
| 258 |         //! Iterate through all fail QuestEffects. | 
|---|
| 259 |         for (std::list<QuestEffect*>::const_iterator effect = this->failEffects_.begin(); effect != this->failEffects_.end(); ++effect) | 
|---|
| 260 |         { | 
|---|
| 261 |             if(i == 0) //!< We're counting down... | 
|---|
| 262 |             { | 
|---|
| 263 |                return *effect; | 
|---|
| 264 |             } | 
|---|
| 265 |             i--; | 
|---|
| 266 |         } | 
|---|
| 267 |         return NULL; //!< If the index is greater than the number of elements in the list. | 
|---|
| 268 |     } | 
|---|
| 269 |  | 
|---|
| 270 |     /** | 
|---|
| 271 |     @brief | 
|---|
| 272 |         Returns the complete QuestEffect at the given index. | 
|---|
| 273 |     @param | 
|---|
| 274 |         The index. | 
|---|
| 275 |     @return | 
|---|
| 276 |         Returns a pointer to the complete QuestEffect at the given index. NULL if there is no element at the given index. | 
|---|
| 277 |     */ | 
|---|
| 278 |     const QuestEffect* Quest::getCompleteEffect(unsigned int index) const | 
|---|
| 279 |     { | 
|---|
| 280 |         int i = index; | 
|---|
| 281 |          | 
|---|
| 282 |         //! Iterate through all complete QuestEffects. | 
|---|
| 283 |         for (std::list<QuestEffect*>::const_iterator effect = this->completeEffects_.begin(); effect != this->completeEffects_.end(); ++effect) | 
|---|
| 284 |         { | 
|---|
| 285 |             if(i == 0) //!< We're counting down... | 
|---|
| 286 |             { | 
|---|
| 287 |                return *effect; | 
|---|
| 288 |             } | 
|---|
| 289 |             i--; | 
|---|
| 290 |         } | 
|---|
| 291 |         return NULL; //!< If the index is greater than the number of elements in the list. | 
|---|
| 292 |     } | 
|---|
| 293 |  | 
|---|
| 294 |     /** | 
|---|
| 295 |     @brief | 
|---|
| 296 |         Returns true if the quest status for the specific player is 'inactive'. | 
|---|
| 297 |     @param player | 
|---|
| 298 |         The player. | 
|---|
| 299 |     @return | 
|---|
| 300 |         Returns true if the quest status for the specific player is 'inactive'. | 
|---|
| 301 |     @throws | 
|---|
| 302 |         Throws an exception if getStatus throws one. | 
|---|
| 303 |     */ | 
|---|
| 304 |     bool Quest::isInactive(const PlayerInfo* player) const | 
|---|
| 305 |     { | 
|---|
| 306 |         return this->getStatus(player) == questStatus::inactive; | 
|---|
| 307 |     } | 
|---|
| 308 |  | 
|---|
| 309 |     /** | 
|---|
| 310 |     @brief | 
|---|
| 311 |         Returns true if the quest status for the specific player is 'active'. | 
|---|
| 312 |     @param player | 
|---|
| 313 |         The player. | 
|---|
| 314 |     @return | 
|---|
| 315 |         Returns true if the quest status for the specific player is 'active'. | 
|---|
| 316 |     @throws | 
|---|
| 317 |         Throws an exception if getStatus throws one. | 
|---|
| 318 |     */ | 
|---|
| 319 |     bool Quest::isActive(const PlayerInfo* player) const | 
|---|
| 320 |     { | 
|---|
| 321 |  | 
|---|
| 322 |         return this->getStatus(player) == questStatus::active; | 
|---|
| 323 |     } | 
|---|
| 324 |  | 
|---|
| 325 |     /** | 
|---|
| 326 |     @brief | 
|---|
| 327 |         Returns true if the quest status for the specific player is 'failed'. | 
|---|
| 328 |     @param player | 
|---|
| 329 |         The player. | 
|---|
| 330 |     @return | 
|---|
| 331 |         Returns true if the quest status for the specific player is 'failed'. | 
|---|
| 332 |     @throws | 
|---|
| 333 |         Throws an exception if getStatus throws one. | 
|---|
| 334 |     */ | 
|---|
| 335 |     bool Quest::isFailed(const PlayerInfo* player) const | 
|---|
| 336 |     { | 
|---|
| 337 |         return this->getStatus(player) == questStatus::failed; | 
|---|
| 338 |     } | 
|---|
| 339 |  | 
|---|
| 340 |     /** | 
|---|
| 341 |     @brief | 
|---|
| 342 |         Returns true if the quest status for the specific player is 'completed'. | 
|---|
| 343 |     @param player | 
|---|
| 344 |         The player. | 
|---|
| 345 |     @return | 
|---|
| 346 |         Returns true if the quest status for the specific player is 'completed'. | 
|---|
| 347 |     @throws | 
|---|
| 348 |         Throws an exception if getStatus throws one. | 
|---|
| 349 |     */ | 
|---|
| 350 |     bool Quest::isCompleted(const PlayerInfo* player) const | 
|---|
| 351 |     { | 
|---|
| 352 |         return this->getStatus(player) == questStatus::completed; | 
|---|
| 353 |     } | 
|---|
| 354 |      | 
|---|
| 355 |     /** | 
|---|
| 356 |     @brief | 
|---|
| 357 |         Fails the Quest for an input player. | 
|---|
| 358 |     @param player | 
|---|
| 359 |         The player. | 
|---|
| 360 |     @return | 
|---|
| 361 |         Returns true if the Quest could be failed, false if not. | 
|---|
| 362 |     */ | 
|---|
| 363 |     bool Quest::fail(PlayerInfo* player) | 
|---|
| 364 |     { | 
|---|
| 365 |         QuestListener::advertiseStatusChange(this->listeners_, "fail"); //!< Tells the QuestListeners, that the status has changed to failed. | 
|---|
| 366 |         this->setStatus(player, questStatus::failed); | 
|---|
| 367 |          | 
|---|
| 368 |         COUT(4) << "Quest {" << this->getId() << "} is failed for player: " << player << " ." <<std::endl; | 
|---|
| 369 |          | 
|---|
| 370 |         this->getDescription()->sendFailQuestNotification(); | 
|---|
| 371 |         return true; | 
|---|
| 372 |     } | 
|---|
| 373 |      | 
|---|
| 374 |     /** | 
|---|
| 375 |     @brief | 
|---|
| 376 |         Completes the Quest for an input player. | 
|---|
| 377 |     @param player | 
|---|
| 378 |         The player. | 
|---|
| 379 |     @return | 
|---|
| 380 |         Returns true if the Quest could be completed, false if not. | 
|---|
| 381 |     */ | 
|---|
| 382 |     bool Quest::complete(PlayerInfo* player) | 
|---|
| 383 |     { | 
|---|
| 384 |         QuestListener::advertiseStatusChange(this->listeners_, "complete"); //!< Tells the QuestListeners, that the status has changed to completed. | 
|---|
| 385 |         this->setStatus(player, questStatus::completed); | 
|---|
| 386 |          | 
|---|
| 387 |         COUT(4) << "Quest {" << this->getId() << "} is completed for player: " << player << " ." <<std::endl; | 
|---|
| 388 |          | 
|---|
| 389 |         this->getDescription()->sendCompleteQuestNotification(); | 
|---|
| 390 |         return true; | 
|---|
| 391 |     } | 
|---|
| 392 |  | 
|---|
| 393 |     /** | 
|---|
| 394 |     @brief | 
|---|
| 395 |         Starts the Quest for an input player. | 
|---|
| 396 |     @param player | 
|---|
| 397 |         The player. | 
|---|
| 398 |     @return | 
|---|
| 399 |         Returns true if the Quest could be started, false if not. | 
|---|
| 400 |     */ | 
|---|
| 401 |     bool Quest::start(PlayerInfo* player) | 
|---|
| 402 |     { | 
|---|
| 403 |         if(!this->isStartable(player)) //!< Checks whether the quest can be started. | 
|---|
| 404 |         { | 
|---|
| 405 |             COUT(4) << "A non-startable quest was trying to be started." << std::endl; | 
|---|
| 406 |             return false; | 
|---|
| 407 |         } | 
|---|
| 408 |          | 
|---|
| 409 |         COUT(4) << "Quest {" << this->getId() << "} is started for player: " << player << " ." <<std::endl; | 
|---|
| 410 |          | 
|---|
| 411 |         QuestListener::advertiseStatusChange(this->listeners_, "start"); //!< Tells the QuestListeners, that the status has changed to active. | 
|---|
| 412 |          | 
|---|
| 413 |         this->setStatus(player, questStatus::active); | 
|---|
| 414 |          | 
|---|
| 415 |         this->getDescription()->sendAddQuestNotification(); | 
|---|
| 416 |         return true; | 
|---|
| 417 |     } | 
|---|
| 418 |      | 
|---|
| 419 |     /** | 
|---|
| 420 |     @brief | 
|---|
| 421 |         Adds a QuestListener to the list of QuestListeners listening to this Quest. | 
|---|
| 422 |     @param listener | 
|---|
| 423 |         The QuestListener to be added. | 
|---|
| 424 |     @return | 
|---|
| 425 |         Returns true if successful, false if not. | 
|---|
| 426 |     */ | 
|---|
| 427 |     bool Quest::addListener(QuestListener* listener) | 
|---|
| 428 |     { | 
|---|
| 429 |         if(listener == NULL) | 
|---|
| 430 |         { | 
|---|
| 431 |             COUT(2) << "A NULL-QuestListener was trying to be added to a Quests listeners." << std::endl; | 
|---|
| 432 |             return false; | 
|---|
| 433 |         } | 
|---|
| 434 |          | 
|---|
| 435 |         this->listeners_.push_back(listener); | 
|---|
| 436 |         return true; | 
|---|
| 437 |     } | 
|---|
| 438 |  | 
|---|
| 439 | } | 
|---|