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