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