Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 27, 2015, 6:49:54 PM (9 years ago)
Author:
fvultier
Message:
 
Location:
code/branches/fabienHS15/src/orxonox/weaponsystem
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/fabienHS15/src/orxonox/weaponsystem/Munition.cc

    r10791 r10878  
    4444        this->maxMunitionPerMagazine_ = 10;
    4545        this->maxMagazines_ = 10;
    46         this->magazines_ = 10;
     46        this->unassignedMagazines_ = 10;
    4747
    4848        this->deployment_ = MunitionDeployment::Stack;
     
    5757    Munition::~Munition()
    5858    {
    59         for (std::map<WeaponMode*, Magazine*>::iterator it = this->currentMagazines_.begin(); it != this->currentMagazines_.end(); ++it)
     59        for (std::map<WeaponMode*, Magazine*>::iterator it = this->assignedMagazines_.begin(); it != this->assignedMagazines_.end(); ++it)
    6060            delete it->second;
    6161    }
     
    7979
    8080            // Use the map to get the magazine assigned to the given user
    81             std::map<WeaponMode*, Magazine*>::const_iterator it = this->currentMagazines_.find(user);
    82             if (it != this->currentMagazines_.end())
     81            std::map<WeaponMode*, Magazine*>::const_iterator it = this->assignedMagazines_.find(user);
     82            if (it != this->assignedMagazines_.end())
    8383                return it->second;
    8484        }
     
    8686        {
    8787            // We don't use separate magazines for each user, so just take the first magazine
    88             if (this->currentMagazines_.size() > 0)
    89                 return this->currentMagazines_.begin()->second;
     88            if (this->assignedMagazines_.size() > 0)
     89                return this->assignedMagazines_.begin()->second;
    9090        }
    9191
     
    100100            if (deployment_ == MunitionDeployment::Stack)
    101101                // With stacked munition every magazine contributes to the total amount
    102                 return this->maxMunitionPerMagazine_ * this->magazines_ + magazine->munition_;
     102                return this->maxMunitionPerMagazine_ * this->unassignedMagazines_ + magazine->munition_;
    103103            else
    104104                // Wihtout stacked munition we just consider the current magazine
     
    125125            Magazine* magazine = this->getMagazine(0);
    126126            if (magazine && magazine->munition_ == this->maxMunitionPerMagazine_)
    127                 return this->magazines_ + 1;
    128         }
    129 
    130         return this->magazines_;
     127                return this->unassignedMagazines_ + 1;
     128        }
     129
     130        return this->unassignedMagazines_;
    131131    }
    132132
    133133    void Munition::setNumMagazines(unsigned int numMagazines)
    134134    {
    135         this->magazines_ = numMagazines;
     135        this->unassignedMagazines_ = numMagazines;
    136136    }
    137137
     
    154154            if (deployment_ == MunitionDeployment::Stack)
    155155            {
    156                 munition += this->maxMunitionPerMagazine_ * this->magazines_;
     156                munition += this->maxMunitionPerMagazine_ * this->unassignedMagazines_;
    157157            }
    158158
     
    200200                    magazine->munition_ = 0;
    201201
    202                     if (this->reload(0))
     202                    if (this->reload(NULL))
    203203                        // Successfully reloaded, continue recursively
    204204                        return this->takeMunition(amount, 0);
     
    224224    {
    225225        // As long as we have enough magazines (and don't stack munition) we can reload
    226         return (this->magazines_ > 0 && deployment_ != MunitionDeployment::Stack);
     226        return (this->unassignedMagazines_ > 0 && deployment_ != MunitionDeployment::Stack);
    227227    }
    228228
     
    252252
    253253        // Check if we actually can reload
    254         if (this->magazines_ == 0)
     254        if (this->unassignedMagazines_ == 0)
    255255            return false;
    256256
     
    266266
    267267        // Remove the current magazine for the given user
    268         std::map<WeaponMode*, Magazine*>::iterator it = this->currentMagazines_.find(user);
    269         if (it != this->currentMagazines_.end())
     268        std::map<WeaponMode*, Magazine*>::iterator it = this->assignedMagazines_.find(user);
     269        if (it != this->assignedMagazines_.end())
    270270        {
    271271            if (it->first == lastFilledWeaponMode_)
     
    274274            }           
    275275            delete it->second;
    276             this->currentMagazines_.erase(it);
     276            this->assignedMagazines_.erase(it);
    277277        }
    278278
    279279        // Load a new magazine
    280         this->currentMagazines_[user] = new Magazine(this, bUseReloadTime);
    281         this->magazines_--;
     280        this->assignedMagazines_[user] = new Magazine(this, bUseReloadTime);
     281        this->unassignedMagazines_--;
    282282
    283283        return true;
     
    299299        {
    300300            // Return true if any of the current magazines is not full (loading counts as full although it returns 0 munition)
    301             for (std::map<WeaponMode*, Magazine*>::const_iterator it = this->currentMagazines_.begin(); it != this->currentMagazines_.end(); ++it)
     301            for (std::map<WeaponMode*, Magazine*>::const_iterator it = this->assignedMagazines_.begin(); it != this->assignedMagazines_.end(); ++it)
    302302                if (it->second->munition_ < this->maxMunitionPerMagazine_ && it->second->bLoaded_)
    303303                    return true;
     
    325325                {
    326326                    magazine->munition_ -= this->maxMunitionPerMagazine_;
    327                     this->magazines_++;
     327                    this->unassignedMagazines_++;
    328328                }
    329329
    330330                // If we reached the limit, reduce both magazines and munition to the maximum
    331                 if (this->magazines_ >= this->maxMagazines_)
    332                 {
    333                     this->magazines_ = this->maxMagazines_;
     331                if (this->unassignedMagazines_ >= this->maxMagazines_)
     332                {
     333                    this->unassignedMagazines_ = this->maxMagazines_;
    334334                    magazine->munition_ = this->maxMunitionPerMagazine_;
    335335                }
     
    349349            if (lastFilledWeaponMode_ == NULL)
    350350            {
    351                 it = this->currentMagazines_.begin();
     351                it = this->assignedMagazines_.begin();
    352352            }
    353353            else
    354354            {
    355                 it = this->currentMagazines_.find(lastFilledWeaponMode_);
     355                it = this->assignedMagazines_.find(lastFilledWeaponMode_);
    356356                ++ it;
    357357            }
     
    362362            {
    363363                bool change = false;
    364                 while (it != this->currentMagazines_.end())
     364                while (it != this->assignedMagazines_.end())
    365365                {
    366366                    // Add munition if the magazine isn't full (but only to loaded magazines)
     
    383383                }                   
    384384
    385                 it = this->currentMagazines_.begin();
     385                it = this->assignedMagazines_.begin();
    386386                firstLoop = false;
    387387            }
     
    400400        else
    401401            // If we don't stack munition, we're more limited
    402             return ((this->currentMagazines_.size() + this->magazines_) < this->maxMagazines_);
    403     }
    404 
    405     bool Munition::addMagazines(unsigned int amount)
    406     {
     402            return ((this->assignedMagazines_.size() + this->unassignedMagazines_) < this->maxMagazines_);
     403    }
     404
     405    unsigned int Munition::addMagazines(unsigned int amount)
     406    {
     407        unsigned int addedMagazines = 0;
     408
    407409        if (!this->canAddMagazines(amount))
    408             return false;
     410            return 0;
    409411
    410412        // Calculate how many magazines are needed
    411         int needed_magazines = this->maxMagazines_ - this->magazines_ - this->currentMagazines_.size();
     413        int needed_magazines = this->maxMagazines_ - this->unassignedMagazines_ - this->assignedMagazines_.size();
    412414
    413415        // If zero or less magazines are needed, we definitively don't need more magazines (unless we stack munition - then a magazine contributes directly to the munition)
    414416        if (needed_magazines <= 0 && deployment_ != MunitionDeployment::Stack)
    415             return false;
     417            return 0;
    416418
    417419        if (amount <= static_cast<unsigned int>(needed_magazines))
    418420        {
    419421            // We need more magazines than we get, so just add them
    420             this->magazines_ += amount;
     422            this->unassignedMagazines_ += amount;
     423            addedMagazines = amount;
    421424        }
    422425        else
    423426        {
    424427            // We get more magazines than we need, so just add the needed amount
    425             this->magazines_ += needed_magazines;
     428            this->unassignedMagazines_ += needed_magazines;
     429            addedMagazines = needed_magazines;
    426430            if (deployment_ == MunitionDeployment::Stack)
    427431            {
     
    433437        }
    434438
    435         return true;
     439        // Reload as many empty magazines as possible
     440        // Double loop and break is needed because the reload function changes the assigned magazines. This may confuse the iterator.
     441        for (unsigned int i = 0; i < addedMagazines; ++i)
     442        {
     443            for (std::map<WeaponMode*, Magazine*>::iterator it = this->assignedMagazines_.begin(); it != this->assignedMagazines_.end(); ++it)
     444            {
     445                if (needReload(it->first))
     446                {
     447                    reload(it->first);
     448                    break;
     449                }
     450            }
     451        }
     452
     453        return addedMagazines;
    436454    }
    437455
     
    440458        if (deployment_ == MunitionDeployment::Stack)
    441459        {
    442             if (this->magazines_ >= amount)
     460            if (this->unassignedMagazines_ >= amount)
    443461            {
    444462                // We have enough magazines
    445463                return true;
    446464            }
    447             else if (this->magazines_ == amount - 1)
     465            else if (this->unassignedMagazines_ == amount - 1)
    448466            {
    449467                // We lack one magazine, check if the current magazine is still full, if yes we're fine
     
    461479        {
    462480            // In case we're not stacking munition, just check the number of magazines
    463             return (this->magazines_ >= amount);
     481            return (this->unassignedMagazines_ >= amount);
    464482        }
    465483
     
    472490            return false;
    473491
    474         if (this->magazines_ >= amount)
     492        if (this->unassignedMagazines_ >= amount)
    475493        {
    476494            // We have enough magazines, just remove the amount
    477             this->magazines_ -= amount;
     495            this->unassignedMagazines_ -= amount;
    478496        }
    479497        else if (deployment_ == MunitionDeployment::Stack)
    480498        {
    481499            // We don't have enough magazines, but we're stacking munition, so additionally remove the bullets from the current magazine
    482             this->magazines_ = 0;
     500            this->unassignedMagazines_ = 0;
    483501            Magazine* magazine = this->getMagazine(0);
    484502            if (magazine)
     
    500518
    501519        // Remove the current magazine for the given user
    502         std::map<WeaponMode*, Magazine*>::iterator it = this->currentMagazines_.find(user);
    503         if (it != this->currentMagazines_.end())
     520        std::map<WeaponMode*, Magazine*>::iterator it = this->assignedMagazines_.find(user);
     521        if (it != this->assignedMagazines_.end())
    504522        {
    505523            if (it->first == lastFilledWeaponMode_)
     
    508526            }
    509527            delete it->second;
    510             this->currentMagazines_.erase(it);           
     528            this->assignedMagazines_.erase(it);           
    511529            return true;
    512530        }
  • code/branches/fabienHS15/src/orxonox/weaponsystem/Munition.h

    r10794 r10878  
    9797
    9898            bool canAddMagazines(unsigned int amount) const;
    99             bool addMagazines(unsigned int amount);
     99            /**
     100            @brief Try to add magazines.
     101            @param amount The amount of magazines tried to add.
     102            @return The amount of magazines sucessfully added.
     103            */
     104            unsigned int addMagazines(unsigned int amount);
    100105
    101106            bool canRemoveMagazines(unsigned int amount) const;
     
    107112            unsigned int maxMunitionPerMagazine_;
    108113            unsigned int maxMagazines_;
    109             unsigned int magazines_;
    110             std::map<WeaponMode*, Magazine*> currentMagazines_; // Maps weapon modes to magazines that are currently used.
     114            unsigned int unassignedMagazines_; // Number of magazines that are not assigned to a weapon mode. These are alway treated as full.
     115            std::map<WeaponMode*, Magazine*> assignedMagazines_; // Maps weapon modes to magazines that are currently used.
    111116
    112117            MunitionDeployment::Value deployment_; // Defines the behaviour how munition and magazines are distributed to the consuming weapon modes.
     
    115120            bool bAllowMultiMunitionRemovementUnderflow_;
    116121
    117             float reloadTime_;
     122            float reloadTime_; // The time needed to replace a magazine by a new one.
    118123            WeaponMode* lastFilledWeaponMode_; // Pointer to the weapon mode that got the last munition during the last call of addMunition.
    119124
  • code/branches/fabienHS15/src/orxonox/weaponsystem/ReplenishingMunition.cc

    r10791 r10878  
    7676    void ReplenishingMunition::replenish()
    7777    {
    78         if (this->canAddMunition(this->replenishAmount_))
     78        if (this->canAddMagazines(this->replenishAmount_))
    7979        {
    8080            // Make a temporary copy of bAllowMunitionRefilling_, because this might be disallowed in every
     
    8484
    8585            // Replenish munition
    86             this->addMunition(this->replenishAmount_);
     86            this->addMagazines(this->replenishAmount_);
    8787
    8888            // Write back the temporary value
  • code/branches/fabienHS15/src/orxonox/weaponsystem/ReplenishingMunition.h

    r10791 r10878  
    6464        protected:                         
    6565            float replenishInterval_; //!< The interval in which the munition is replenished.
    66             unsigned int replenishAmount_; //!< The amount by which it is replenished.
     66            unsigned int replenishAmount_; //!< The amount of magazines by which it is replenished.
    6767
    6868        private:
    69             void replenish();
     69            void replenish(); //!< Every call of this function replenishes the munition. This function is called by the timer.
    7070            void initializeTimer();
    7171
Note: See TracChangeset for help on using the changeset viewer.