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