/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Damian 'Mozork' Frick * Co-authors: * ... * */ /** @file GlobalQuest.cc @brief Implementation of the GlobalQuest class. */ #include "OrxonoxStableHeaders.h" #include "GlobalQuest.h" #include "orxonox/objects/worldentities/ControllableEntity.h" #include "core/CoreIncludes.h" #include "util/Exception.h" #include "QuestEffect.h" namespace orxonox { CreateFactory(GlobalQuest); /** @brief Constructor. Registers the object. */ GlobalQuest::GlobalQuest(BaseObject* creator) : Quest(creator) { RegisterObject(GlobalQuest); } /** @brief Destructor. */ GlobalQuest::~GlobalQuest() { } /** @brief Method for creating a GlobalQuest object through XML. */ void GlobalQuest::XMLPort(Element& xmlelement, XMLPort::Mode mode) { SUPER(GlobalQuest, XMLPort, xmlelement, mode); XMLPortObject(GlobalQuest, QuestEffect, "reward-effects", addRewardEffect, getRewardEffects, xmlelement, mode); COUT(3) << "New GlobalQuest {" << this->getId() << "} created." << std::endl; } /** @brief Fails the quest for all players. Invokes the failEffects on all the players possessing this quest. @param player The player failing it. @return Returns true if the quest could be failed, false if not. */ bool GlobalQuest::fail(ControllableEntity* player) { if(this->isFailable(player)) //!< Check whether the quest can be failed. { this->setStatus(player, questStatus::failed); //! Iterate through all players possessing this quest. for(std::set::const_iterator it = players_.begin(); it != players_.end(); it++) { QuestEffect::invokeEffects(*it, this->getFailEffectList()); } return true; } COUT(2) << "A non-completable quest was trying to be failed." << std::endl; return false; } /** @brief Completes the quest for all players. Invokes the completeEffects on all the players possessing this quest. Invokes the reward effects on the player completing the quest. @param player The player completing it. @return Returns true if the quest could be completed, false if not. */ bool GlobalQuest::complete(ControllableEntity* player) { if(this->isCompletable(player)) //!< Check whether the quest can be completed. { this->setStatus(player, questStatus::completed); //! Iterate through all players possessing the quest. for(std::set::const_iterator it = players_.begin(); it != players_.end(); it++) { QuestEffect::invokeEffects(*it, this->getCompleteEffectList()); } QuestEffect::invokeEffects(player, this->rewards_); //!< Invoke reward effects on the player completing the quest. return true; } COUT(2) << "A non-completable quest was trying to be completed." << std::endl; return false; } /** @brief Checks whether the quest can be started. @param player The player for whom is to be checked. @return Returns true if the quest can be started, false if not. @throws Throws an exception if either isInactive() of isActive() throws one. */ bool GlobalQuest::isStartable(const ControllableEntity* player) const { return this->isInactive(player) || this->isActive(player); } /** @brief Checks whether the quest can be failed. @param player The player for whom is to be checked. @return Returns true if the quest can be failed, false if not. @throws Throws an Exception if isActive() throws one. */ bool GlobalQuest::isFailable(const ControllableEntity* player) const { return this->isActive(player); } /** @brief Checks whether the quest can be completed. @param player The player for whom is to be checked. @return Returns true if the quest can be completed, false if not. @throws Throws an Exception if isActive() throws one. */ bool GlobalQuest::isCompletable(const ControllableEntity* player) const { return this->isActive(player); } /** @brief Returns the status of the quest for a specific player. @param player The player. @throws Throws an Exception if player is NULL. */ questStatus::Enum GlobalQuest::getStatus(const ControllableEntity* player) const { if(player == NULL) //!< We don't want NULL-Pointers! { ThrowException(Argument, "The input ControllableEntity* is NULL."); } //! Find the player. std::set::const_iterator it = this->players_.find((ControllableEntity*)(void*)player); if (it != this->players_.end()) //!< If the player was found. { return this->status_; } return questStatus::inactive; } /** @brief Sets the status for a specific player. But be careful wit this one, the status will just be set without checking for its validity. You have to know what you're doing. @param player The player. @param status The status to be set. @return Returns false if player is NULL. */ bool GlobalQuest::setStatus(ControllableEntity* player, const questStatus::Enum & status) { if(player == NULL) //!< We don't want NULL-Pointers! { return false; } //! Find the player. std::set::const_iterator it = this->players_.find(player); if (it == this->players_.end()) //!< Player is not yet in the list. { this->players_.insert(player); //!< Add the player to the set. } this->status_ = status; //!< Set the status, which is global, remember...? return true; } /** @brief Adds a reward effect to the list of reward effects. @param effect The effect to be added. @return Returns true if successful. */ bool GlobalQuest::addRewardEffect(QuestEffect* effect) { if(effect == NULL) //!< We don't want NULL-Pointers! { COUT(2) << "The reward effect to be added to quest {" << this->getId() << "} was NULL." << std::endl; return false; } this->rewards_.push_back(effect); //!< Add the effect to the list. COUT(3) << "Reward effect was added to Quest {" << this->getId() << "}." << std::endl; return true; } /** @brief Returns the reward effect at the given index. @param index The index. @return Returns the QuestEffect at the given index. */ const QuestEffect* GlobalQuest::getRewardEffects(unsigned int index) const { int i = index; for (std::list::const_iterator effect = this->rewards_.begin(); effect != this->rewards_.end(); ++effect) { if(i == 0) { return *effect; } i--; } return NULL; } }