Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2914 for code/branches


Ignore:
Timestamp:
Apr 10, 2009, 12:05:01 AM (16 years ago)
Author:
landauf
Message:

More changes in the WeaponSystem:

  • WeaponSets can now contain several WeaponPacks
  • WeaponPacks can now belong to several WeaponSets

(until now this seemingly was a 1:1 relationship… now it's n:m)

  • Started support for multiple weaponmodes
  • Added some code to the destructor of some classes… according to some legends, this helps destroying objects. (WeaponSets and WeaponPacks weren't deleted before…)

Not yet finished, but I have to synchronize desktop computer and notebook.

Location:
code/branches/weapons/src/orxonox/objects
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • code/branches/weapons/src/orxonox/objects/weaponSystem/Weapon.cc

    r2912 r2914  
    4747        this->magazineReadyToShoot_ = true;
    4848        this->weaponSystem_ = 0;
    49         this->attachedToWeaponSlot_ = 0;
     49        this->weaponPack_ = 0;
     50        this->weaponSlot_ = 0;
    5051        this->bulletLoadingTime_ = 0;
    5152        this->magazineLoadingTime_ = 0;
  • code/branches/weapons/src/orxonox/objects/weaponSystem/Weapon.h

    r2912 r2914  
    8686                { return this->weaponSystem_; };
    8787
    88             inline void setAttachedToWeaponSlot(WeaponSlot * wSlot)
    89                 { this->attachedToWeaponSlot_ = wSlot; }
    90             inline WeaponSlot * getAttachedToWeaponSlot() const
    91                 { return this->attachedToWeaponSlot_; }
     88            inline void setWeaponPack(WeaponPack *weaponPack)
     89                { this->weaponPack_ = weaponPack; };
     90            inline WeaponPack * getWeaponPack() const
     91                { return this->weaponPack_; };
     92
     93            inline void setWeaponSlot(WeaponSlot * wSlot)
     94                { this->weaponSlot_ = wSlot; }
     95            inline WeaponSlot * getWeaponSlot() const
     96                { return this->weaponSlot_; }
    9297
    9398        protected:
     
    102107            std::string munitionType_;
    103108
    104             WeaponSlot * attachedToWeaponSlot_;
     109            WeaponSlot * weaponSlot_;
    105110            Munition * munition_;
    106111            WeaponSystem * weaponSystem_;
     112            WeaponPack* weaponPack_;
    107113
    108114            SubclassIdentifier<Munition> munitionIdentifier_;
  • code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponPack.cc

    r2912 r2914  
    3535#include "Weapon.h"
    3636#include "WeaponSlot.h"
     37#include "WeaponSystem.h"
    3738
    3839namespace orxonox
     
    4546
    4647        this->weaponSystem_ = 0;
    47         this->firemode_ = 0;
    4848
    4949COUT(0) << "+WeaponPack" << std::endl;
     
    5353    {
    5454COUT(0) << "~WeaponPack" << std::endl;
     55
     56        if (this->isInitialized() && this->weaponSystem_)
     57            this->weaponSystem_->removeWeaponPack(this);
    5558    }
    5659
     
    6063
    6164        XMLPortObject(WeaponPack, Weapon, "", addWeapon, getWeapon, xmlelement, mode);
    62         XMLPortParam(WeaponPack, "firemode", setFireMode, getFireMode, xmlelement, mode);
    6365    }
    6466
    65     int WeaponPack::getSize() const
     67    void WeaponPack::fire(unsigned int weaponmode)
    6668    {
    67         return this->weapons_.size();
    68     }
    69 
    70     void WeaponPack::fire()
    71     {
    72         for (int i=0; i < (int) this->weapons_.size(); i++)
    73         {
    74             this->weapons_[i]->getAttachedToWeaponSlot()->fire();
    75         }
    76     }
    77 
    78     Weapon * WeaponPack::getWeaponPointer(unsigned int n) const
    79     {
    80         return this->weapons_[n];
    81     }
    82 
    83     void WeaponPack::setFireMode(unsigned int firemode)
    84     {
    85         this->firemode_ = firemode;
    86     }
    87 
    88     unsigned int WeaponPack::getFireMode() const
    89     {
    90         return this->firemode_;
     69        for (std::set<Weapon *>::iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
     70            (*it)->fire();
    9171    }
    9272
    9373    void WeaponPack::addWeapon(Weapon * weapon)
    9474    {
    95         this->weapons_.push_back(weapon);
     75        if (!weapon)
     76            return;
     77
     78        this->weapons_.insert(weapon);
     79        weapon->setWeaponPack(this);
    9680    }
    9781
    98     const Weapon * WeaponPack::getWeapon(unsigned int index) const
     82    Weapon * WeaponPack::getWeapon(unsigned int index) const
    9983    {
    100         return weapons_[index];
     84        unsigned int i = 0;
     85
     86        for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
     87        {
     88            if (i == index)
     89                return (*it);
     90            ++i;
     91        }
     92
     93        return 0;
    10194    }
    10295
    10396    void WeaponPack::setWeaponSystemToAllWeapons(WeaponSystem * weaponSystem)
    10497    {
    105         for (size_t i = 0; i < this->weapons_.size(); i++)
    106             this->weapons_[i]->setWeaponSystem(weaponSystem);
     98        for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
     99            (*it)->setWeaponSystem(weaponSystem);
    107100    }
    108101
    109102    void WeaponPack::attachNeededMunitionToAllWeapons()
    110103    {
    111         for (size_t i = 0; i < this->weapons_.size(); i++)
     104        for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
    112105        {
    113             this->weapons_[i]->attachNeededMunition(weapons_[i]->getMunitionType());
    114             this->weapons_[i]->setWeapon();
     106            (*it)->attachNeededMunition((*it)->getMunitionType());
     107            (*it)->setWeapon();
    115108        }
    116109    }
  • code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponPack.h

    r2912 r2914  
    3333#include "OrxonoxPrereqs.h"
    3434
    35 #include <vector>
     35#include <set>
    3636
    3737#include "core/BaseObject.h"
     
    4747            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    4848
    49             void fire();
    50 
    51             Weapon * getWeaponPointer(unsigned int n) const;
    52             int getSize() const;
    53 
    54             void setFireMode(unsigned int firemode);
    55             unsigned int getFireMode() const;
     49            void fire(unsigned int weaponmode);
    5650
    5751            void addWeapon(Weapon * weapon);
    58             const Weapon * getWeapon(unsigned int index) const;
     52            Weapon * getWeapon(unsigned int index) const;
     53
     54            inline size_t getNumWeapons() const
     55                { return this->weapons_.size(); }
     56
     57            unsigned int getDesiredWeaponmode(unsigned int firemode) { return 0; } // TODO
    5958
    6059            void attachNeededMunitionToAllWeapons();
    6160
    62             //functions with effect to all weapons of the weaponPack
    63             //functions needed for creating Pointer to the right objects (-->Pawn.cc)
    6461            inline void setWeaponSystem(WeaponSystem *weaponSystem)
    6562                { this->weaponSystem_ = weaponSystem; this->setWeaponSystemToAllWeapons(weaponSystem); }
     
    7067            void setWeaponSystemToAllWeapons(WeaponSystem * weaponSystem);
    7168
    72             std::vector<Weapon *> weapons_;
    73             WeaponSystem *weaponSystem_;
    74             unsigned int firemode_;
     69            std::set<Weapon *> weapons_;
     70            WeaponSystem * weaponSystem_;
    7571    };
    7672}
  • code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponSet.cc

    r2912 r2914  
    3131#include "core/CoreIncludes.h"
    3232#include "core/XMLPort.h"
    33 #include "objects/worldentities/pawns/Pawn.h"
    3433
    35 #include "Weapon.h"
    36 #include "WeaponSlot.h"
     34#include "WeaponSystem.h"
    3735#include "WeaponPack.h"
    38 #include "WeaponSystem.h"
    3936
    4037namespace orxonox
     
    4239    CreateFactory(WeaponSet);
    4340
    44     WeaponSet::WeaponSet(BaseObject* creator, int k) : BaseObject(creator)
     41    WeaponSet::WeaponSet(BaseObject* creator) : BaseObject(creator)
    4542    {
    4643        RegisterObject(WeaponSet);
    4744
    4845        this->weaponSystem_ = 0;
    49         this->attachedWeaponPack_ = 0;
     46        this->desiredFiremode_ = WeaponSystem::FIRE_MODE_UNASSIGNED;
    5047
    5148COUT(0) << "+WeaponSet" << std::endl;
     
    5552    {
    5653COUT(0) << "~WeaponSet" << std::endl;
     54
     55        if (this->isInitialized() && this->weaponSystem_)
     56            this->weaponSystem_->removeWeaponSet(this);
    5757    }
    5858
     
    6161        SUPER(WeaponSet, XMLPort, xmlelement, mode);
    6262
    63         XMLPortParam(WeaponSet, "firemode", setFireMode, getFireMode, xmlelement, mode);
     63        XMLPortParam(WeaponSet, "firemode", setDesiredFiremode, getDesiredFiremode, xmlelement, mode);
    6464    }
    65 
     65/*
    6666    void WeaponSet::attachWeaponPack(WeaponPack *wPack)
    6767    {
     
    100100        }
    101101    }
    102 
     102*/
    103103
    104104    void WeaponSet::fire()
    105105    {
    106         //fires all WeaponSlots available for this weaponSet attached from the WeaponPack
    107         if (this->attachedWeaponPack_)
    108             this->attachedWeaponPack_->fire();
     106        // fire all WeaponPacks with their defined weaponmode
     107        for (std::map<WeaponPack*, unsigned int>::iterator it = this->weaponpacks_.begin(); it != this->weaponpacks_.end(); ++it)
     108            if (it->second != WeaponSystem::WEAPON_MODE_UNASSIGNED)
     109                it->first->fire(it->second);
     110    }
     111
     112    void WeaponSet::setWeaponmodeLink(WeaponPack* weaponpack, unsigned int weaponmode)
     113    {
     114        this->weaponpacks_[weaponpack] = weaponmode;
     115    }
     116
     117    void WeaponSet::removeWeaponmodeLink(WeaponPack* weaponpack)
     118    {
     119        this->weaponpacks_.erase(weaponpack);
     120    }
     121
     122    unsigned int WeaponSet::getWeaponmodeLink(WeaponPack* weaponpack)
     123    {
     124        std::map<WeaponPack*, unsigned int>::iterator it = this->weaponpacks_.find(weaponpack);
     125        if (it != this->weaponpacks_.end())
     126            return it->second;
     127        else
     128            return WeaponSystem::WEAPON_MODE_UNASSIGNED;
    109129    }
    110130}
  • code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponSet.h

    r2912 r2914  
    3333#include "OrxonoxPrereqs.h"
    3434
    35 #include <vector>
     35#include <map>
    3636
    3737#include "core/BaseObject.h"
     
    4242    {
    4343        public:
    44             WeaponSet(BaseObject* creator, int k = 0);
     44            WeaponSet(BaseObject* creator);
    4545            virtual ~WeaponSet();
    4646
    4747            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    4848
    49             void attachWeaponPack(WeaponPack *wPack);
     49//            void attachWeaponPack(WeaponPack *wPack);
     50
    5051            void fire();
    5152
    52             inline void setFireMode(const unsigned int firemode)
    53                 { this->firemode_ = firemode; }
    54             inline unsigned int getFireMode() const
    55                 { return this->firemode_; }
     53            void setWeaponmodeLink(WeaponPack* weaponpack, unsigned int weaponmode);
     54            void removeWeaponmodeLink(WeaponPack* weaponpack);
     55            unsigned int getWeaponmodeLink(WeaponPack* weaponpack);
     56
     57            inline void setDesiredFiremode(const unsigned int firemode)
     58                { this->desiredFiremode_ = firemode; }
     59            inline unsigned int getDesiredFiremode() const
     60                { return this->desiredFiremode_; }
    5661
    5762            inline void setWeaponSystem(WeaponSystem *weaponSystem)
     
    6267        private:
    6368            WeaponSystem *weaponSystem_;
    64             std::vector<WeaponSlot *> setWeaponSlots_;
    65             unsigned int firemode_;
    66             WeaponPack * attachedWeaponPack_;
     69            unsigned int desiredFiremode_;
     70            std::map<WeaponPack*, unsigned int> weaponpacks_;
    6771    };
    6872}
  • code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponSlot.cc

    r2912 r2914  
    3434
    3535#include "Weapon.h"
     36#include "WeaponSystem.h"
    3637
    3738namespace orxonox
     
    4344        RegisterObject(WeaponSlot);
    4445
    45         this->unlimitedAmmo_ = false;
    46         this->attachedWeapon_ = 0;
     46        this->weaponSystem_ = 0;
     47        this->weapon_ = 0;
     48
    4749        this->setObjectMode(0x0);
    4850
     
    5355    {
    5456COUT(0) << "~WeaponSlot" << std::endl;
     57
     58        if (this->isInitialized() && this->weaponSystem_)
     59            this->weaponSystem_->removeWeaponSlot(this);
    5560    }
    5661
     
    5863    {
    5964        SUPER(WeaponSlot, XMLPort, xmlelement, mode);
    60     }
    6165
    62     /*sets the munition type
    63      *unlimited: true
    64      *limited:   false  (in this case there will be munition)
    65      */
    66     void WeaponSlot::setAmmoType(bool isUnlimited)
    67     {
    68         unlimitedAmmo_ = isUnlimited;
    69     }
    70 
    71 
    72     void WeaponSlot::fire()
    73     {
    74         if ( this->attachedWeapon_ )
    75 //COUT(0) << "WeaponSlot::fire" << std::endl;
    76         this->attachedWeapon_->fire();
     66        // ...
    7767    }
    7868
    7969    void WeaponSlot::attachWeapon(Weapon *weapon)
    8070    {
    81         this->attachedWeapon_ = weapon;
    82         weapon->setAttachedToWeaponSlot(this);
    83 //COUT(0) << "WeaponSlot::attachWeapon position=" << this->getWorldPosition() << std::endl;
    84         weapon->setPosition(this->getPosition());
     71        if (this->weapon_)
     72            this->removeWeapon();
     73
     74        this->weapon_ = weapon;
     75
     76        if (this->weapon_)
     77        {
     78            this->weapon_->setWeaponSlot(this);
     79            this->weapon_->setPosition(this->getPosition());
     80        }
    8581    }
    8682
    87     Weapon * WeaponSlot::getAttachedWeapon() const
     83    void WeaponSlot::removeWeapon()
    8884    {
    89         return this->attachedWeapon_;
     85        if (this->weapon_)
     86        {
     87            this->weapon_->setWeaponSlot(0);
     88            this->weapon_ = 0;
     89        }
    9090    }
    9191}
  • code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponSlot.h

    r2912 r2914  
    4343            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    4444
    45             void attachWeapon(Weapon *weapon);
    46             Weapon * getAttachedWeapon() const;
    47             void setAmmoType(bool isUnlimited);
    48             void fire();
     45            void attachWeapon(Weapon * weapon);
     46            void removeWeapon();
     47            Weapon * getWeapon() const
     48                { return this->weapon_; }
    4949
    50             inline void setWeaponSystem(WeaponSystem *weaponSystem)
     50            inline bool isOccupied() const
     51                { return (this->weapon_ != 0); }
     52
     53            inline void setWeaponSystem(WeaponSystem * weaponSystem)
    5154                { this->weaponSystem_ = weaponSystem; }
    5255            inline WeaponSystem * getWeaponSystem() const
     
    5558
    5659        private:
    57             Weapon *attachedWeapon_;
    58             bool unlimitedAmmo_;
    59 
    60             WeaponSystem *weaponSystem_;
     60            WeaponSystem * weaponSystem_;
     61            Weapon * weapon_;
    6162    };
    6263}
  • code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponSystem.cc

    r2912 r2914  
    3636#include "WeaponPack.h"
    3737#include "WeaponSet.h"
     38#include "Weapon.h"
    3839
    3940/* WeaponSystem
     
    6162            if (this->pawn_)
    6263                this->pawn_->setWeaponSystem(0);
    63         }
    64     }
    65 
    66     void WeaponSystem::attachWeaponSet(WeaponSet *wSet)
    67     {
    68         if (!wSet)
    69             return;
    70 
    71         this->weaponSets_[wSet->getFireMode()] = wSet;
    72         wSet->setWeaponSystem(this);
     64
     65            for (std::map<unsigned int, WeaponSet*>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); )
     66                delete (it++)->second;
     67
     68            for (std::set<WeaponPack*>::iterator it = this->weaponPacks_.begin(); it != this->weaponPacks_.end(); )
     69                delete (*(it++));
     70
     71            for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); )
     72                delete (*(it++));
     73        }
     74    }
     75
     76    void WeaponSystem::addWeaponSlot(WeaponSlot * wSlot)
     77    {
     78        if (!wSlot)
     79            return;
     80
     81        this->weaponSlots_.insert(wSlot);
     82        wSlot->setWeaponSystem(this);
     83    }
     84
     85    void WeaponSystem::removeWeaponSlot(WeaponSlot * wSlot)
     86    {
     87        if (!wSlot)
     88            return;
     89
     90        if (wSlot->getWeapon())
     91            this->removeWeaponPack(wSlot->getWeapon()->getWeaponPack());
     92
     93        this->weaponSlots_.erase(wSlot);
     94    }
     95
     96    WeaponSlot * WeaponSystem::getWeaponSlot(unsigned int index) const
     97    {
     98        unsigned int i = 0;
     99        for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
     100        {
     101            ++i;
     102            if (i > index)
     103                return (*it);
     104        }
     105        return 0;
     106    }
     107
     108    bool WeaponSystem::addWeaponSet(WeaponSet * wSet)
     109    {
     110        if (wSet)
     111            return this->addWeaponSet(wSet, wSet->getDesiredFiremode());
     112        else
     113            return false;
     114    }
     115
     116    bool WeaponSystem::addWeaponSet(WeaponSet * wSet, unsigned int firemode)
     117    {
     118        if (!wSet || firemode >= WeaponSystem::MAX_FIRE_MODES)
     119            return false;
     120
     121        std::map<unsigned int, WeaponSet*>::const_iterator it = this->weaponSets_.find(firemode);
     122        if (it == this->weaponSets_.end())
     123        {
     124            this->weaponSets_[firemode] = wSet;
     125            wSet->setWeaponSystem(this);
     126            return true;
     127        }
     128
     129        return false;
     130    }
     131
     132    void WeaponSystem::removeWeaponSet(WeaponSet * wSet)
     133    {
     134        for (std::map<unsigned int, WeaponSet*>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); )
     135        {
     136            if (it->second == wSet)
     137                this->weaponSets_.erase(it++);
     138            else
     139                ++it;
     140        }
    73141    }
    74142
     
    85153    }
    86154
    87     void WeaponSystem::attachWeaponSlot(WeaponSlot *wSlot)
    88     {
    89         if (!wSlot)
    90             return;
    91 
    92         this->weaponSlots_.insert(wSlot);
    93         wSlot->setWeaponSystem(this);
    94     }
    95 
    96     WeaponSlot * WeaponSystem::getWeaponSlot(unsigned int index) const
    97     {
    98         unsigned int i = 0;
     155    bool WeaponSystem::canAddWeaponPack(WeaponPack * wPack)
     156    {
     157        if (!wPack)
     158            return false;
     159
     160        unsigned int freeSlots = 0;
    99161        for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
     162        {
     163            if (!(*it)->isOccupied())
     164                ++freeSlots;
     165        }
     166
     167        return (freeSlots >= wPack->getNumWeapons());
     168    }
     169
     170    bool WeaponSystem::addWeaponPack(WeaponPack * wPack)
     171    {
     172        if (!this->canAddWeaponPack(wPack))
     173            return false;
     174
     175        // Attach all weapons to the first free slots (and to the Pawn)
     176        unsigned int i = 0;
     177        for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
     178        {
     179            if (!(*it)->isOccupied() && i < wPack->getNumWeapons())
     180            {
     181                Weapon* weapon = wPack->getWeapon(i);
     182                (*it)->attachWeapon(weapon);
     183                this->getPawn()->attach(weapon);
     184                ++i;
     185            }
     186        }
     187
     188        // Assign the desired weaponmode to the firemodes
     189        for (std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
     190        {
     191            unsigned int weaponmode = wPack->getDesiredWeaponmode(it->first);
     192            if (weaponmode != WeaponSystem::WEAPON_MODE_UNASSIGNED)
     193                it->second->setWeaponmodeLink(wPack, weaponmode);
     194        }
     195
     196        this->weaponPacks_.insert(wPack);
     197        wPack->setWeaponSystem(this);
     198        wPack->attachNeededMunitionToAllWeapons(); // TODO - what is this?
     199
     200        return true;
     201    }
     202
     203    void WeaponSystem::removeWeaponPack(WeaponPack * wPack)
     204    {
     205        // Remove all weapons from their WeaponSlot
     206        unsigned int i = 0;
     207        Weapon* weapon = 0;
     208        while (weapon = wPack->getWeapon(i++))
     209            weapon->getWeaponSlot()->removeWeapon();
     210
     211        // Remove all added links from the WeaponSets
     212        for (std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
     213            it->second->removeWeaponmodeLink(wPack);
     214
     215        // Remove the WeaponPack from the WeaponSystem
     216        this->weaponPacks_.erase(wPack);
     217    }
     218
     219    WeaponPack * WeaponSystem::getWeaponPack(unsigned int index) const
     220    {
     221        unsigned int i = 0;
     222        for (std::set<WeaponPack*>::iterator it = this->weaponPacks_.begin(); it != this->weaponPacks_.end(); ++it)
    100223        {
    101224            ++i;
     
    106229    }
    107230
    108     void WeaponSystem::attachWeaponPack(WeaponPack *wPack, unsigned int wSetNumber)
    109     {
    110         if (!wPack)
    111             return;
    112 
    113         std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.find(wSetNumber);
    114         if (it != this->weaponSets_.end() && it->second)
    115             it->second->attachWeaponPack(wPack);
    116 
    117         this->weaponPacks_.insert(wPack);
    118         wPack->setWeaponSystem(this);
    119         wPack->attachNeededMunitionToAllWeapons(); // TODO - what is this?
    120     }
    121 
    122     WeaponPack * WeaponSystem::getWeaponPack(unsigned int index) const
    123     {
    124         unsigned int i = 0;
    125         for (std::set<WeaponPack*>::iterator it = this->weaponPacks_.begin(); it != this->weaponPacks_.end(); ++it)
    126         {
    127             ++i;
    128             if (i > index)
    129                 return (*it);
    130         }
    131         return 0;
    132    }
     231    bool WeaponSystem::swapWeaponSlots(WeaponSlot * wSlot1, WeaponSlot * wSlot2)
     232    {
     233        // TODO
     234    }
     235
     236    void WeaponSystem::changeWeaponmode(WeaponPack * wPack, WeaponSet * wSet, unsigned int weaponmode)
     237    {
     238        if (!wPack || !wSet)
     239            return;
     240
     241        // Check if the WeaponPack belongs to this WeaponSystem
     242        std::set<WeaponPack *>::iterator it1 = this->weaponPacks_.find(wPack);
     243        if (it1 == this->weaponPacks_.end())
     244            return;
     245
     246        // Check if the WeaponSet belongs to this WeaponSystem
     247        bool foundWeaponSet = false;
     248        for (std::map<unsigned int, WeaponSet *>::iterator it2 = this->weaponSets_.begin(); it2 != this->weaponSets_.end(); ++it2)
     249        {
     250            if (it2->second == wSet)
     251            {
     252                foundWeaponSet = true;
     253                break;
     254            }
     255        }
     256        if (!foundWeaponSet)
     257            return;
     258
     259        // Finally set the link between firemode and weaponmode
     260        wSet->setWeaponmodeLink(wPack, weaponmode);
     261    }
    133262
    134263    void WeaponSystem::setNewMunition(const std::string& munitionType, Munition * munitionToAdd)
     
    148277    }
    149278
    150 
    151     //n is the n'th weaponSet, starting with zero
    152     //SpaceShip.cc only needs to have the keybinding to a specific Set-number n (=firemode)
    153     //in future this could be well defined and not only for 3 different WeaponModes
    154279    void WeaponSystem::fire(unsigned int firemode)
    155280    {
  • code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponSystem.h

    r2912 r2914  
    4040namespace orxonox
    4141{
    42     const unsigned int MAX_FIRE_MODES = 8;
    43 
    4442    class _OrxonoxExport WeaponSystem : public BaseObject
    4543    {
     
    4846            virtual ~WeaponSystem();
    4947
    50             void attachWeaponSlot(WeaponSlot * wSlot);
     48            // adding and removing WeaponSlots
     49            void addWeaponSlot(WeaponSlot * wSlot);
     50            void removeWeaponSlot(WeaponSlot * wSlot);
    5151            WeaponSlot * getWeaponSlot(unsigned int index) const;
    5252
    53             void attachWeaponSet(WeaponSet * wSet);
     53            // adding and removing WeaponSets
     54            bool addWeaponSet(WeaponSet * wSet);
     55            bool addWeaponSet(WeaponSet * wSet, unsigned int firemode);
     56            void removeWeaponSet(WeaponSet * wSet);
    5457            WeaponSet * getWeaponSet(unsigned int index) const;
    5558
    56             void attachWeaponPack(WeaponPack * wPack, unsigned int wSetNumber);
     59            // adding and removing WeaponPacks
     60            bool canAddWeaponPack(WeaponPack * wPack);
     61            bool addWeaponPack(WeaponPack * wPack);
     62            void removeWeaponPack(WeaponPack * wPack);
    5763            WeaponPack * getWeaponPack(unsigned int index) const;
    5864
     65            // configure slots and firemodes
     66            bool swapWeaponSlots(WeaponSlot * wSlot1, WeaponSlot * wSlot2);
     67            void changeWeaponmode(WeaponPack * wPack, WeaponSet * wSet, unsigned int weaponmode);
     68
    5969            void fire(unsigned int firemode);
     70
    6071
    6172            void setNewMunition(const std::string& munitionType, Munition * munitionToAdd);
     
    7182                { return this->weaponSlots_.size(); }
    7283
    73             static inline unsigned int getMaxFireModes()
    74                 { return MAX_FIRE_MODES; }
    75             static inline unsigned int getFireModeMask(unsigned int firemode)
     84            static inline unsigned int getFiremodeMask(unsigned int firemode)
    7685                { return (0x1 << firemode); }
     86
     87            static const unsigned int MAX_FIRE_MODES = 8;
     88            static const unsigned int FIRE_MODE_UNASSIGNED = (unsigned int)-1;
     89
     90            static const unsigned int MAX_WEAPON_MODES = 8;
     91            static const unsigned int WEAPON_MODE_UNASSIGNED = (unsigned int)-1;
    7792
    7893        private:
  • code/branches/weapons/src/orxonox/objects/worldentities/pawns/Pawn.cc

    r2912 r2914  
    123123        if (this->weaponSystem_)
    124124        {
    125             for (unsigned int firemode = 0; firemode < WeaponSystem::getMaxFireModes(); firemode++)
    126                 if (this->fire_ & WeaponSystem::getFireModeMask(firemode))
     125            for (unsigned int firemode = 0; firemode < WeaponSystem::MAX_FIRE_MODES; firemode++)
     126                if (this->fire_ & WeaponSystem::getFiremodeMask(firemode))
    127127                    this->weaponSystem_->fire(firemode);
    128128        }
     
    255255    void Pawn::fire(unsigned int firemode)
    256256    {
    257         this->firehack_ |= WeaponSystem::getFireModeMask(firemode);
     257        this->firehack_ |= WeaponSystem::getFiremodeMask(firemode);
    258258    }
    259259
     
    280280        this->attach(wSlot);
    281281        if (this->weaponSystem_)
    282             this->weaponSystem_->attachWeaponSlot(wSlot);
     282            this->weaponSystem_->addWeaponSlot(wSlot);
    283283    }
    284284
     
    291291    }
    292292
     293    void Pawn::addWeaponSet(WeaponSet * wSet)
     294    {
     295        if (this->weaponSystem_)
     296            this->weaponSystem_->addWeaponSet(wSet);
     297    }
     298
     299    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
     300    {
     301        if (this->weaponSystem_)
     302            return this->weaponSystem_->getWeaponSet(index);
     303        else
     304            return 0;
     305    }
     306
    293307    void Pawn::addWeaponPack(WeaponPack * wPack)
    294308    {
    295309        if (this->weaponSystem_)
    296             this->weaponSystem_->attachWeaponPack(wPack, wPack->getFireMode());
    297     }
    298 
    299     WeaponPack * Pawn::getWeaponPack(unsigned int firemode) const
    300     {
    301         if (this->weaponSystem_)
    302             return this->weaponSystem_->getWeaponPack(firemode);
    303         else
    304             return 0;
    305     }
    306 
    307     void Pawn::addWeaponSet(WeaponSet * wSet)
    308     {
    309         if (this->weaponSystem_)
    310             this->weaponSystem_->attachWeaponSet(wSet);
    311     }
    312 
    313     WeaponSet * Pawn::getWeaponSet(unsigned int index) const
    314     {
    315         if (this->weaponSystem_)
    316             return this->weaponSystem_->getWeaponSet(index);
     310            this->weaponSystem_->addWeaponPack(wPack);
     311    }
     312
     313    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
     314    {
     315        if (this->weaponSystem_)
     316            return this->weaponSystem_->getWeaponPack(index);
    317317        else
    318318            return 0;
  • code/branches/weapons/src/orxonox/objects/worldentities/pawns/Pawn.h

    r2912 r2914  
    8585            void addWeaponSlot(WeaponSlot * wSlot);
    8686            WeaponSlot * getWeaponSlot(unsigned int index) const;
    87             void addWeaponPack(WeaponPack * wPack);
    88             WeaponPack * getWeaponPack(unsigned int firemode) const;
    8987            void addWeaponSet(WeaponSet * wSet);
    9088            WeaponSet * getWeaponSet(unsigned int index) const;
     89            void addWeaponPack(WeaponPack * wPack);
     90            WeaponPack * getWeaponPack(unsigned int index) const;
    9191
    9292            inline const WorldEntity* getWorldEntity() const
Note: See TracChangeset for help on using the changeset viewer.