| 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 GlobalQuest class. | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #include "GlobalQuest.h" | 
|---|
| 35 |  | 
|---|
| 36 | #include "core/CoreIncludes.h" | 
|---|
| 37 | #include "core/XMLPort.h" | 
|---|
| 38 | #include "QuestEffect.h" | 
|---|
| 39 |  | 
|---|
| 40 | namespace orxonox | 
|---|
| 41 | { | 
|---|
| 42 |     CreateFactory(GlobalQuest); | 
|---|
| 43 |  | 
|---|
| 44 |     /** | 
|---|
| 45 |     @brief | 
|---|
| 46 |         Constructor. Registers the object. | 
|---|
| 47 |     */ | 
|---|
| 48 |     GlobalQuest::GlobalQuest(BaseObject* creator) : Quest(creator) | 
|---|
| 49 |     { | 
|---|
| 50 |         RegisterObject(GlobalQuest); | 
|---|
| 51 |     } | 
|---|
| 52 |  | 
|---|
| 53 |     /** | 
|---|
| 54 |     @brief | 
|---|
| 55 |         Destructor. | 
|---|
| 56 |     */ | 
|---|
| 57 |     GlobalQuest::~GlobalQuest() | 
|---|
| 58 |     { | 
|---|
| 59 |  | 
|---|
| 60 |     } | 
|---|
| 61 |  | 
|---|
| 62 |     /** | 
|---|
| 63 |     @brief | 
|---|
| 64 |         Method for creating a GlobalQuest object through XML. | 
|---|
| 65 |     */ | 
|---|
| 66 |     void GlobalQuest::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 67 |     { | 
|---|
| 68 |         SUPER(GlobalQuest, XMLPort, xmlelement, mode); | 
|---|
| 69 |  | 
|---|
| 70 |         XMLPortObject(GlobalQuest, QuestEffect, "reward-effects", addRewardEffect, getRewardEffects, xmlelement, mode); | 
|---|
| 71 |  | 
|---|
| 72 |         COUT(3) << "New GlobalQuest {" << this->getId() << "} created." << std::endl; | 
|---|
| 73 |     } | 
|---|
| 74 |  | 
|---|
| 75 |     /** | 
|---|
| 76 |     @brief | 
|---|
| 77 |         Fails the Quest for all players. | 
|---|
| 78 |         Invokes the fail QuestEffects on all the players possessing this Quest. | 
|---|
| 79 |     @param player | 
|---|
| 80 |         The player failing it. | 
|---|
| 81 |     @return | 
|---|
| 82 |         Returns true if the Quest could be failed, false if not. | 
|---|
| 83 |     */ | 
|---|
| 84 |     bool GlobalQuest::fail(PlayerInfo* player) | 
|---|
| 85 |     { | 
|---|
| 86 |         if(!this->isFailable(player)) //!< Check whether the Quest can be failed. | 
|---|
| 87 |         { | 
|---|
| 88 |             COUT(4) << "A non-completable quest was trying to be failed." << std::endl; | 
|---|
| 89 |             return false; | 
|---|
| 90 |         } | 
|---|
| 91 |  | 
|---|
| 92 |         Quest::fail(player); | 
|---|
| 93 |  | 
|---|
| 94 |     //! Iterate through all players possessing this Quest. | 
|---|
| 95 |     for(std::set<PlayerInfo*>::const_iterator it = players_.begin(); it != players_.end(); it++) | 
|---|
| 96 |     { | 
|---|
| 97 |         QuestEffect::invokeEffects(*it, this->getFailEffectList()); | 
|---|
| 98 |     } | 
|---|
| 99 |  | 
|---|
| 100 |     return true; | 
|---|
| 101 |     } | 
|---|
| 102 |  | 
|---|
| 103 |     /** | 
|---|
| 104 |     @brief | 
|---|
| 105 |         Completes the Quest for all players. | 
|---|
| 106 |         Invokes the complete QuestEffects on all the players possessing this Quest. | 
|---|
| 107 |         Invokes the reward QuestEffects on the player completing the Quest. | 
|---|
| 108 |     @param player | 
|---|
| 109 |         The player completing it. | 
|---|
| 110 |     @return | 
|---|
| 111 |         Returns true if the Quest could be completed, false if not. | 
|---|
| 112 |     */ | 
|---|
| 113 |     bool GlobalQuest::complete(PlayerInfo* player) | 
|---|
| 114 |     { | 
|---|
| 115 |         if(!this->isCompletable(player)) //!< Check whether the Quest can be completed. | 
|---|
| 116 |         { | 
|---|
| 117 |             COUT(4) << "A non-completable quest was trying to be completed." << std::endl; | 
|---|
| 118 |             return false; | 
|---|
| 119 |         } | 
|---|
| 120 |  | 
|---|
| 121 |         //! Iterate through all players possessing the Quest. | 
|---|
| 122 |         for(std::set<PlayerInfo*>::const_iterator it = players_.begin(); it != players_.end(); it++) | 
|---|
| 123 |         { | 
|---|
| 124 |             QuestEffect::invokeEffects(*it, this->getCompleteEffectList()); | 
|---|
| 125 |         } | 
|---|
| 126 |  | 
|---|
| 127 |         Quest::complete(player); | 
|---|
| 128 |  | 
|---|
| 129 |         QuestEffect::invokeEffects(player, this->rewards_); //!< Invoke reward QuestEffects on the player completing the Quest. | 
|---|
| 130 |         return true; | 
|---|
| 131 |     } | 
|---|
| 132 |  | 
|---|
| 133 |     /** | 
|---|
| 134 |     @brief | 
|---|
| 135 |         Checks whether the Quest can be started. | 
|---|
| 136 |     @param player | 
|---|
| 137 |         The player for whom is to be checked. | 
|---|
| 138 |     @return | 
|---|
| 139 |         Returns true if the quest can be started, false if not. | 
|---|
| 140 |     @throws | 
|---|
| 141 |         Throws an exception if either isInactive() of isActive() throws one. | 
|---|
| 142 |     */ | 
|---|
| 143 |     bool GlobalQuest::isStartable(const PlayerInfo* player) const | 
|---|
| 144 |     { | 
|---|
| 145 |         if(!(this->getParentQuest() == NULL || this->getParentQuest()->isActive(player))) | 
|---|
| 146 |         { | 
|---|
| 147 |             return false; | 
|---|
| 148 |         } | 
|---|
| 149 |         return (this->isInactive(player) && !(this->status_ == QuestStatus::Completed || this->status_ == QuestStatus::Failed)); | 
|---|
| 150 |     } | 
|---|
| 151 |  | 
|---|
| 152 |     /** | 
|---|
| 153 |     @brief | 
|---|
| 154 |         Checks whether the Quest can be failed. | 
|---|
| 155 |     @param player | 
|---|
| 156 |         The player for whom is to be checked. | 
|---|
| 157 |     @return | 
|---|
| 158 |         Returns true if the Quest can be failed, false if not. | 
|---|
| 159 |     @throws | 
|---|
| 160 |         Throws an Exception if isActive() throws one. | 
|---|
| 161 |     */ | 
|---|
| 162 |     bool GlobalQuest::isFailable(const PlayerInfo* player) const | 
|---|
| 163 |     { | 
|---|
| 164 |         return this->isActive(player); | 
|---|
| 165 |  | 
|---|
| 166 |     } | 
|---|
| 167 |  | 
|---|
| 168 |     /** | 
|---|
| 169 |     @brief | 
|---|
| 170 |         Checks whether the Quest can be completed. | 
|---|
| 171 |     @param player | 
|---|
| 172 |         The player for whom is to be checked. | 
|---|
| 173 |     @return | 
|---|
| 174 |         Returns true if the Quest can be completed, false if not. | 
|---|
| 175 |     @throws | 
|---|
| 176 |         Throws an Exception if isActive() throws one. | 
|---|
| 177 |     */ | 
|---|
| 178 |     bool GlobalQuest::isCompletable(const PlayerInfo* player) const | 
|---|
| 179 |     { | 
|---|
| 180 |         return this->isActive(player); | 
|---|
| 181 |     } | 
|---|
| 182 |  | 
|---|
| 183 |     /** | 
|---|
| 184 |     @brief | 
|---|
| 185 |         Returns the status of the Quest for a specific player. | 
|---|
| 186 |     @param player | 
|---|
| 187 |         The player. | 
|---|
| 188 |     @throws | 
|---|
| 189 |         Throws an Exception if player is NULL. | 
|---|
| 190 |     */ | 
|---|
| 191 |     QuestStatus::Value GlobalQuest::getStatus(const PlayerInfo* player) const | 
|---|
| 192 |     { | 
|---|
| 193 |         if(player == NULL) //!< We don't want NULL-Pointers! | 
|---|
| 194 |         { | 
|---|
| 195 |             ThrowException(Argument, "The input PlayerInfo* is NULL."); | 
|---|
| 196 |         } | 
|---|
| 197 |  | 
|---|
| 198 |         //! Find the player. | 
|---|
| 199 |         std::set<PlayerInfo*>::const_iterator it = this->players_.find((PlayerInfo*)(void*)player); | 
|---|
| 200 |         if (it != this->players_.end()) //!< If the player was found. | 
|---|
| 201 |         { | 
|---|
| 202 |             return this->status_; | 
|---|
| 203 |         } | 
|---|
| 204 |  | 
|---|
| 205 |         return QuestStatus::Inactive; | 
|---|
| 206 |     } | 
|---|
| 207 |  | 
|---|
| 208 |     /** | 
|---|
| 209 |     @brief | 
|---|
| 210 |         Sets the status for a specific player. | 
|---|
| 211 |         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. | 
|---|
| 212 |     @param player | 
|---|
| 213 |         The player. | 
|---|
| 214 |     @param status | 
|---|
| 215 |         The status to be set. | 
|---|
| 216 |     @return | 
|---|
| 217 |         Returns false if player is NULL. | 
|---|
| 218 |     */ | 
|---|
| 219 |     bool GlobalQuest::setStatus(PlayerInfo* player, const QuestStatus::Value & status) | 
|---|
| 220 |     { | 
|---|
| 221 |         if(player == NULL) //!< We don't want NULL-Pointers! | 
|---|
| 222 |         { | 
|---|
| 223 |             return false; | 
|---|
| 224 |         } | 
|---|
| 225 |  | 
|---|
| 226 |         //! Find the player. | 
|---|
| 227 |         std::set<PlayerInfo*>::const_iterator it = this->players_.find(player); | 
|---|
| 228 |         if (it == this->players_.end()) //!< Player is not yet in the list. | 
|---|
| 229 |         { | 
|---|
| 230 |             this->players_.insert(player); //!< Add the player to the set. | 
|---|
| 231 |         } | 
|---|
| 232 |  | 
|---|
| 233 |         this->status_ = status; //!< Set the status, which is global, remember...? | 
|---|
| 234 |         return true; | 
|---|
| 235 |     } | 
|---|
| 236 |  | 
|---|
| 237 |     /** | 
|---|
| 238 |     @brief | 
|---|
| 239 |         Adds a reward QuestEffect to the list of reward QuestEffects. | 
|---|
| 240 |     @param effect | 
|---|
| 241 |         The QuestEffect to be added. | 
|---|
| 242 |     @return | 
|---|
| 243 |         Returns true if successful. | 
|---|
| 244 |     */ | 
|---|
| 245 |     bool GlobalQuest::addRewardEffect(QuestEffect* effect) | 
|---|
| 246 |     { | 
|---|
| 247 |         if(effect == NULL) //!< We don't want NULL-Pointers! | 
|---|
| 248 |         { | 
|---|
| 249 |             COUT(2) << "The reward effect to be added to quest {" << this->getId() << "} was NULL." << std::endl; | 
|---|
| 250 |             return false; | 
|---|
| 251 |         } | 
|---|
| 252 |  | 
|---|
| 253 |         this->rewards_.push_back(effect); //!< Add the QuestEffect to the list. | 
|---|
| 254 |  | 
|---|
| 255 |         COUT(3) << "Reward effect was added to Quest {" << this->getId() << "}." << std::endl; | 
|---|
| 256 |         return true; | 
|---|
| 257 |     } | 
|---|
| 258 |  | 
|---|
| 259 |     /** | 
|---|
| 260 |     @brief | 
|---|
| 261 |         Returns the reward QuestEffect at the given index. | 
|---|
| 262 |     @param index | 
|---|
| 263 |         The index. | 
|---|
| 264 |     @return | 
|---|
| 265 |         Returns the QuestEffect at the given index. | 
|---|
| 266 |     */ | 
|---|
| 267 |     const QuestEffect* GlobalQuest::getRewardEffects(unsigned int index) const | 
|---|
| 268 |     { | 
|---|
| 269 |         int i = index; | 
|---|
| 270 |         for (std::list<QuestEffect*>::const_iterator effect = this->rewards_.begin(); effect != this->rewards_.end(); ++effect) | 
|---|
| 271 |         { | 
|---|
| 272 |             if(i == 0) | 
|---|
| 273 |             { | 
|---|
| 274 |                return *effect; | 
|---|
| 275 |             } | 
|---|
| 276 |             i--; | 
|---|
| 277 |         } | 
|---|
| 278 |         return NULL; | 
|---|
| 279 |     } | 
|---|
| 280 |  | 
|---|
| 281 |  | 
|---|
| 282 | } | 
|---|