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