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