[2864] | 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 | * Daniel 'Huty' Haggenmueller |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Implementation of PickupCollection. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "PickupCollection.h" |
---|
| 35 | |
---|
| 36 | #include "BaseItem.h" |
---|
| 37 | #include "EquipmentItem.h" |
---|
| 38 | #include "PassiveItem.h" |
---|
| 39 | #include "UsableItem.h" |
---|
| 40 | |
---|
| 41 | #include "objects/worldentities/pawns/Pawn.h" |
---|
| 42 | |
---|
| 43 | namespace orxonox |
---|
| 44 | { |
---|
| 45 | //! Constructor |
---|
| 46 | PickupCollection::PickupCollection() |
---|
| 47 | { |
---|
| 48 | this->bBlockRemovals_ = false; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | @brief |
---|
| 53 | Add an item to the collection. |
---|
| 54 | |
---|
| 55 | Only adds the item if there's a free slot for it. |
---|
| 56 | |
---|
| 57 | @param item Item to add to the collection. |
---|
| 58 | @return Returns whether the item has been added to the collection. |
---|
| 59 | */ |
---|
| 60 | bool PickupCollection::add(BaseItem* item) |
---|
| 61 | { |
---|
| 62 | if (this->checkSlot(item)) |
---|
| 63 | { |
---|
| 64 | this->items_.insert( std::pair<std::string, BaseItem*> (item->getPickupIdentifier(), item) ); |
---|
| 65 | return true; |
---|
| 66 | } |
---|
| 67 | else |
---|
| 68 | return false; |
---|
| 69 | } |
---|
| 70 | /** |
---|
| 71 | @brief |
---|
| 72 | Check if there's a free slot for an item. |
---|
| 73 | |
---|
| 74 | Compares the amount of the item-type in the collection |
---|
| 75 | against the maximal amount of the item that can be carried. |
---|
| 76 | |
---|
| 77 | @param item Item to check for a slot. |
---|
| 78 | @return Returns if there's a free slot for the item. |
---|
| 79 | */ |
---|
| 80 | bool PickupCollection::checkSlot(BaseItem* item) |
---|
| 81 | { |
---|
| 82 | return (this->items_.count(item->getPickupIdentifier()) < item->getMaxCarryAmount()); |
---|
| 83 | } |
---|
| 84 | /** |
---|
| 85 | @brief |
---|
| 86 | Empty the collection. |
---|
| 87 | |
---|
| 88 | Calls dropped() on all the items in the collection, |
---|
| 89 | then clears the collection. |
---|
| 90 | */ |
---|
| 91 | void PickupCollection::clear() |
---|
| 92 | { |
---|
| 93 | this->bBlockRemovals_ = true; |
---|
| 94 | for (std::multimap<std::string, BaseItem*>::iterator it = this->items_.begin(); it != this->items_.end(); it++) |
---|
| 95 | { |
---|
| 96 | (*it).second->dropped((*it).second->getOwner()); |
---|
| 97 | |
---|
| 98 | delete (*it).second; |
---|
| 99 | } |
---|
| 100 | this->items_.clear(); |
---|
| 101 | this->bBlockRemovals_ = false; |
---|
| 102 | } |
---|
| 103 | /** |
---|
| 104 | @brief Check if an item/type of item is in the collection. |
---|
| 105 | @param item Item to check. |
---|
| 106 | @param anyOfType If it should look for any item of the item's type (default: false). |
---|
| 107 | @return Whether the collection contains the item/type of item. |
---|
| 108 | */ |
---|
| 109 | bool PickupCollection::contains(BaseItem* item, bool anyOfType) |
---|
| 110 | { |
---|
| 111 | if (anyOfType) |
---|
| 112 | { |
---|
| 113 | return (this->items_.count(item->getPickupIdentifier()) > 0); |
---|
| 114 | } |
---|
| 115 | else |
---|
| 116 | { |
---|
| 117 | std::multimap<std::string, BaseItem*>::_Pairii bounds = this->items_.equal_range(item->getPickupIdentifier()); |
---|
| 118 | for (std::multimap<std::string, BaseItem*>::iterator it = bounds.first; it != bounds.second && it != this->items_.end(); it++) |
---|
| 119 | { |
---|
| 120 | if ((*it).second == item) |
---|
| 121 | { |
---|
| 122 | return true; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | return false; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | /** |
---|
| 129 | @brief Remove an item/all of a type from the collection. |
---|
| 130 | @param item Item to remove. |
---|
| 131 | @param removeAllOfType Whether to remove all the items with the item's type (default: false). |
---|
| 132 | */ |
---|
| 133 | void PickupCollection::remove(BaseItem* item, bool removeAllOfType) |
---|
| 134 | { |
---|
| 135 | if (!item || !this->contains(item, removeAllOfType) || this->bBlockRemovals_) |
---|
| 136 | return; |
---|
| 137 | |
---|
| 138 | if (removeAllOfType) |
---|
| 139 | { |
---|
| 140 | std::multimap<std::string, BaseItem*>::iterator it; |
---|
| 141 | while ((it = this->items_.find(item->getPickupIdentifier())) != this->items_.end()) |
---|
| 142 | { |
---|
| 143 | this->items_.erase(it); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | else |
---|
| 147 | { |
---|
| 148 | std::multimap<std::string, BaseItem*>::_Pairii bounds = this->items_.equal_range(item->getPickupIdentifier()); |
---|
| 149 | for (std::multimap<std::string, BaseItem*>::iterator it = bounds.first; it != bounds.second && it != this->items_.end(); it++) |
---|
| 150 | { |
---|
| 151 | if ((*it).second == item) |
---|
| 152 | { |
---|
| 153 | this->items_.erase(it); |
---|
| 154 | return; |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | /** |
---|
| 160 | @brief Add an additive modifier. |
---|
| 161 | @param type ModifierType to add. |
---|
| 162 | @param value Value for the modifier. |
---|
| 163 | */ |
---|
| 164 | void PickupCollection::addAdditiveModifier(ModifierType::Enum type, float value) |
---|
| 165 | { |
---|
| 166 | this->additiveModifiers_.insert( std::pair<ModifierType::Enum, float>(type, value) ); |
---|
| 167 | } |
---|
| 168 | /** |
---|
| 169 | @brief Get the total amount of an additive modifier. |
---|
| 170 | @param type Type for which to get the total. |
---|
| 171 | @return Returns the sum of the additive modifiers of the type. |
---|
| 172 | */ |
---|
| 173 | float PickupCollection::getAdditiveModifier(ModifierType::Enum type) |
---|
| 174 | { |
---|
| 175 | float v = 0.0f; |
---|
| 176 | |
---|
| 177 | std::multimap<ModifierType::Enum, float>::_Pairii range = this->additiveModifiers_.equal_range(type); |
---|
| 178 | |
---|
| 179 | for (std::multimap<ModifierType::Enum, float>::iterator it = range.first; it != range.second && it != this->additiveModifiers_.end(); it++) |
---|
| 180 | { |
---|
| 181 | v += (*it).second; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | return v; |
---|
| 185 | } |
---|
| 186 | /** |
---|
| 187 | @brief Remove an additive modifier. |
---|
| 188 | @param type Type of modifier. |
---|
| 189 | @param value Value which is to be removed. |
---|
| 190 | */ |
---|
| 191 | void PickupCollection::removeAdditiveModifier(ModifierType::Enum type, float value) |
---|
| 192 | { |
---|
| 193 | std::multimap<ModifierType::Enum, float>::_Pairii range = this->additiveModifiers_.equal_range(type); |
---|
| 194 | for (std::multimap<ModifierType::Enum, float>::iterator it = range.first; it != range.second && it != this->additiveModifiers_.end(); it++) |
---|
| 195 | { |
---|
| 196 | if ((*it).second == value) |
---|
| 197 | { |
---|
| 198 | this->additiveModifiers_.erase(it); |
---|
| 199 | return; |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | /** |
---|
| 204 | @brief Add a multiplicative modifier. |
---|
| 205 | @param type ModifierType to add. |
---|
| 206 | @param value Value for the modifier. |
---|
| 207 | */ |
---|
| 208 | void PickupCollection::addMultiplicativeModifier(ModifierType::Enum type, float value) |
---|
| 209 | { |
---|
| 210 | this->multiplicativeModifiers_.insert( std::pair<ModifierType::Enum, float>(type, value) ); |
---|
| 211 | } |
---|
| 212 | /** |
---|
| 213 | @brief Get the total amount of a multiplicative modifier. |
---|
| 214 | @param type Type for which to get the total. |
---|
| 215 | @return Returns the product of the multiplicative modifiers of the type. |
---|
| 216 | */ |
---|
| 217 | float PickupCollection::getMultiplicativeModifier(ModifierType::Enum type) |
---|
| 218 | { |
---|
| 219 | float v = 1.0f; |
---|
| 220 | |
---|
| 221 | std::multimap<ModifierType::Enum, float>::_Pairii range = this->multiplicativeModifiers_.equal_range(type); |
---|
| 222 | for (std::multimap<ModifierType::Enum, float>::iterator it = range.first; it != range.second && it != this->multiplicativeModifiers_.end(); it++) |
---|
| 223 | { |
---|
| 224 | v *= (*it).second; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | return v; |
---|
| 228 | } |
---|
| 229 | /** |
---|
| 230 | @brief Remove a multiplicative modifier. |
---|
| 231 | @param type Type of modifier. |
---|
| 232 | @param value Value which is to be removed. |
---|
| 233 | */ |
---|
| 234 | void PickupCollection::removeMultiplicativeModifier(ModifierType::Enum type, float value) |
---|
| 235 | { |
---|
| 236 | std::multimap<ModifierType::Enum, float>::_Pairii range = this->multiplicativeModifiers_.equal_range(type); |
---|
| 237 | for (std::multimap<ModifierType::Enum, float>::iterator it = range.first; it != range.second && it != this->multiplicativeModifiers_.end(); it++) |
---|
| 238 | { |
---|
| 239 | if ((*it).second == value) |
---|
| 240 | { |
---|
| 241 | this->multiplicativeModifiers_.erase(it); |
---|
| 242 | return; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | } |
---|
| 246 | /** |
---|
| 247 | @brief Applies modifiers to a float. |
---|
| 248 | @param type Type of modifier tp apply. |
---|
| 249 | @param inputValue Value which is to be processed. |
---|
| 250 | @param addBeforeMultiplication Whether to apply the additive modifier before the multiplicative one (default: false). |
---|
| 251 | @return Returns the value after being processed. |
---|
| 252 | */ |
---|
| 253 | float PickupCollection::processModifiers(ModifierType::Enum type, float inputValue, bool addBeforeMultiplication) |
---|
| 254 | { |
---|
| 255 | float outputValue = inputValue; |
---|
| 256 | |
---|
| 257 | if (addBeforeMultiplication) |
---|
| 258 | outputValue += this->getAdditiveModifier(type); |
---|
| 259 | |
---|
| 260 | outputValue *= this->getMultiplicativeModifier(type); |
---|
| 261 | |
---|
| 262 | if (!addBeforeMultiplication) |
---|
| 263 | outputValue += this->getAdditiveModifier(type); |
---|
| 264 | |
---|
| 265 | return outputValue; |
---|
| 266 | } |
---|
| 267 | /** |
---|
| 268 | @brief Applies modifiers to a Vector3. |
---|
| 269 | @param type Type of modifier tp apply. |
---|
| 270 | @param inputValue Value which is to be processed. |
---|
| 271 | @param addBeforeMultiplication Whether to apply the additive modifier before the multiplicative one (default: false). |
---|
| 272 | @return Returns the value after being processed. |
---|
| 273 | */ |
---|
| 274 | Vector3 PickupCollection::processModifiers(ModifierType::Enum type, Vector3 inputValue, bool addBeforeMultiplication) |
---|
| 275 | { |
---|
| 276 | Vector3 outputValue = inputValue; |
---|
| 277 | |
---|
| 278 | if (addBeforeMultiplication) |
---|
| 279 | outputValue += Vector3(this->getAdditiveModifier(type)); |
---|
| 280 | |
---|
| 281 | outputValue *= this->getMultiplicativeModifier(type); |
---|
| 282 | |
---|
| 283 | if (!addBeforeMultiplication) |
---|
| 284 | outputValue += Vector3(this->getAdditiveModifier(type)); |
---|
| 285 | |
---|
| 286 | return outputValue; |
---|
| 287 | } |
---|
| 288 | /** |
---|
| 289 | @brief Get a list of equipment-type items. |
---|
| 290 | @return Returns a list of all the equipment-type items in the collection. |
---|
| 291 | */ |
---|
| 292 | std::set<BaseItem*> PickupCollection::getEquipmentItems() |
---|
| 293 | { |
---|
| 294 | std::set<BaseItem*> ret; |
---|
| 295 | Identifier* ident = Class(EquipmentItem); |
---|
| 296 | |
---|
| 297 | for (std::multimap<std::string, BaseItem*>::iterator it = this->items_.begin(); it != this->items_.end(); it++) |
---|
| 298 | { |
---|
| 299 | if ((*it).second->isA(ident)) |
---|
| 300 | ret.insert((*it).second); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | return ret; |
---|
| 304 | } |
---|
| 305 | /** |
---|
| 306 | @brief Get a list of passive items. |
---|
| 307 | @return Returns a list of all the passive items in the collection. |
---|
| 308 | */ |
---|
| 309 | std::set<BaseItem*> PickupCollection::getPassiveItems() |
---|
| 310 | { |
---|
| 311 | std::set<BaseItem*> ret; |
---|
| 312 | Identifier* ident = Class(PassiveItem); |
---|
| 313 | |
---|
| 314 | for (std::multimap<std::string, BaseItem*>::iterator it = this->items_.begin(); it != this->items_.end(); it++) |
---|
| 315 | { |
---|
| 316 | if ((*it).second->isA(ident)) |
---|
| 317 | ret.insert((*it).second); |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | return ret; |
---|
| 321 | } |
---|
| 322 | /** |
---|
| 323 | @brief Get a list of usable items. |
---|
| 324 | @return Returns a list of all the usable items in the collection. |
---|
| 325 | */ |
---|
| 326 | std::set<BaseItem*> PickupCollection::getUsableItems() |
---|
| 327 | { |
---|
| 328 | std::set<BaseItem*> ret; |
---|
| 329 | Identifier* ident = Class(UsableItem); |
---|
| 330 | |
---|
| 331 | for (std::multimap<std::string, BaseItem*>::iterator it = this->items_.begin(); it != this->items_.end(); it++) |
---|
| 332 | { |
---|
| 333 | if ((*it).second->isA(ident)) |
---|
| 334 | ret.insert((*it).second); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | return ret; |
---|
| 338 | } |
---|
| 339 | } |
---|