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