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