Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup2/src/orxonox/pickup/PickupCollection.cc @ 6554

Last change on this file since 6554 was 6412, checked in by dafrick, 15 years ago

Merged presentation2 branch into pickup2 branch.

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