[Orxonox-commit 262] r2914 - in branches/weapons/src/orxonox/objects: weaponSystem worldentities/pawns

landauf at orxonox.net landauf at orxonox.net
Fri Apr 10 00:05:02 CEST 2009


Author: landauf
Date: 2009-04-10 00:05:01 +0200 (Fri, 10 Apr 2009)
New Revision: 2914

Modified:
   branches/weapons/src/orxonox/objects/weaponSystem/Weapon.cc
   branches/weapons/src/orxonox/objects/weaponSystem/Weapon.h
   branches/weapons/src/orxonox/objects/weaponSystem/WeaponPack.cc
   branches/weapons/src/orxonox/objects/weaponSystem/WeaponPack.h
   branches/weapons/src/orxonox/objects/weaponSystem/WeaponSet.cc
   branches/weapons/src/orxonox/objects/weaponSystem/WeaponSet.h
   branches/weapons/src/orxonox/objects/weaponSystem/WeaponSlot.cc
   branches/weapons/src/orxonox/objects/weaponSystem/WeaponSlot.h
   branches/weapons/src/orxonox/objects/weaponSystem/WeaponSystem.cc
   branches/weapons/src/orxonox/objects/weaponSystem/WeaponSystem.h
   branches/weapons/src/orxonox/objects/worldentities/pawns/Pawn.cc
   branches/weapons/src/orxonox/objects/worldentities/pawns/Pawn.h
Log:
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.

Modified: branches/weapons/src/orxonox/objects/weaponSystem/Weapon.cc
===================================================================
--- branches/weapons/src/orxonox/objects/weaponSystem/Weapon.cc	2009-04-09 14:52:36 UTC (rev 2913)
+++ branches/weapons/src/orxonox/objects/weaponSystem/Weapon.cc	2009-04-09 22:05:01 UTC (rev 2914)
@@ -46,7 +46,8 @@
         this->bulletReadyToShoot_ = true;
         this->magazineReadyToShoot_ = true;
         this->weaponSystem_ = 0;
-        this->attachedToWeaponSlot_ = 0;
+        this->weaponPack_ = 0;
+        this->weaponSlot_ = 0;
         this->bulletLoadingTime_ = 0;
         this->magazineLoadingTime_ = 0;
         this->bReloading_ = false;

Modified: branches/weapons/src/orxonox/objects/weaponSystem/Weapon.h
===================================================================
--- branches/weapons/src/orxonox/objects/weaponSystem/Weapon.h	2009-04-09 14:52:36 UTC (rev 2913)
+++ branches/weapons/src/orxonox/objects/weaponSystem/Weapon.h	2009-04-09 22:05:01 UTC (rev 2914)
@@ -85,11 +85,16 @@
             inline WeaponSystem * getWeaponSystem() const
                 { return this->weaponSystem_; };
 
-            inline void setAttachedToWeaponSlot(WeaponSlot * wSlot)
-                { this->attachedToWeaponSlot_ = wSlot; }
-            inline WeaponSlot * getAttachedToWeaponSlot() const
-                { return this->attachedToWeaponSlot_; }
+            inline void setWeaponPack(WeaponPack *weaponPack)
+                { this->weaponPack_ = weaponPack; };
+            inline WeaponPack * getWeaponPack() const
+                { return this->weaponPack_; };
 
+            inline void setWeaponSlot(WeaponSlot * wSlot)
+                { this->weaponSlot_ = wSlot; }
+            inline WeaponSlot * getWeaponSlot() const
+                { return this->weaponSlot_; }
+
         protected:
             bool bReloading_;
             bool bulletReadyToShoot_;
@@ -101,9 +106,10 @@
             unsigned int magazineAmount_;
             std::string munitionType_;
 
-            WeaponSlot * attachedToWeaponSlot_;
+            WeaponSlot * weaponSlot_;
             Munition * munition_;
             WeaponSystem * weaponSystem_;
+            WeaponPack* weaponPack_;
 
             SubclassIdentifier<Munition> munitionIdentifier_;
 

Modified: branches/weapons/src/orxonox/objects/weaponSystem/WeaponPack.cc
===================================================================
--- branches/weapons/src/orxonox/objects/weaponSystem/WeaponPack.cc	2009-04-09 14:52:36 UTC (rev 2913)
+++ branches/weapons/src/orxonox/objects/weaponSystem/WeaponPack.cc	2009-04-09 22:05:01 UTC (rev 2914)
@@ -34,6 +34,7 @@
 
 #include "Weapon.h"
 #include "WeaponSlot.h"
+#include "WeaponSystem.h"
 
 namespace orxonox
 {
@@ -44,7 +45,6 @@
         RegisterObject(WeaponPack);
 
         this->weaponSystem_ = 0;
-        this->firemode_ = 0;
 
 COUT(0) << "+WeaponPack" << std::endl;
     }
@@ -52,6 +52,9 @@
     WeaponPack::~WeaponPack()
     {
 COUT(0) << "~WeaponPack" << std::endl;
+
+        if (this->isInitialized() && this->weaponSystem_)
+            this->weaponSystem_->removeWeaponPack(this);
     }
 
     void WeaponPack::XMLPort(Element& xmlelement, XMLPort::Mode mode)
@@ -59,59 +62,49 @@
         SUPER(WeaponPack, XMLPort, xmlelement, mode);
 
         XMLPortObject(WeaponPack, Weapon, "", addWeapon, getWeapon, xmlelement, mode);
-        XMLPortParam(WeaponPack, "firemode", setFireMode, getFireMode, xmlelement, mode);
     }
 
-    int WeaponPack::getSize() const
+    void WeaponPack::fire(unsigned int weaponmode)
     {
-        return this->weapons_.size();
+        for (std::set<Weapon *>::iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
+            (*it)->fire();
     }
 
-    void WeaponPack::fire()
+    void WeaponPack::addWeapon(Weapon * weapon)
     {
-        for (int i=0; i < (int) this->weapons_.size(); i++)
-        {
-            this->weapons_[i]->getAttachedToWeaponSlot()->fire();
-        }
-    }
+        if (!weapon)
+            return;
 
-    Weapon * WeaponPack::getWeaponPointer(unsigned int n) const
-    {
-        return this->weapons_[n];
+        this->weapons_.insert(weapon);
+        weapon->setWeaponPack(this);
     }
 
-    void WeaponPack::setFireMode(unsigned int firemode)
+    Weapon * WeaponPack::getWeapon(unsigned int index) const
     {
-        this->firemode_ = firemode;
-    }
+        unsigned int i = 0;
 
-    unsigned int WeaponPack::getFireMode() const
-    {
-        return this->firemode_;
-    }
+        for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
+        {
+            if (i == index)
+                return (*it);
+            ++i;
+        }
 
-    void WeaponPack::addWeapon(Weapon * weapon)
-    {
-        this->weapons_.push_back(weapon);
+        return 0;
     }
 
-    const Weapon * WeaponPack::getWeapon(unsigned int index) const
-    {
-        return weapons_[index];
-    }
-
     void WeaponPack::setWeaponSystemToAllWeapons(WeaponSystem * weaponSystem)
     {
-        for (size_t i = 0; i < this->weapons_.size(); i++)
-            this->weapons_[i]->setWeaponSystem(weaponSystem);
+        for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
+            (*it)->setWeaponSystem(weaponSystem);
     }
 
     void WeaponPack::attachNeededMunitionToAllWeapons()
     {
-        for (size_t i = 0; i < this->weapons_.size(); i++)
+        for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
         {
-            this->weapons_[i]->attachNeededMunition(weapons_[i]->getMunitionType());
-            this->weapons_[i]->setWeapon();
+            (*it)->attachNeededMunition((*it)->getMunitionType());
+            (*it)->setWeapon();
         }
     }
 }

Modified: branches/weapons/src/orxonox/objects/weaponSystem/WeaponPack.h
===================================================================
--- branches/weapons/src/orxonox/objects/weaponSystem/WeaponPack.h	2009-04-09 14:52:36 UTC (rev 2913)
+++ branches/weapons/src/orxonox/objects/weaponSystem/WeaponPack.h	2009-04-09 22:05:01 UTC (rev 2914)
@@ -32,7 +32,7 @@
 
 #include "OrxonoxPrereqs.h"
 
-#include <vector>
+#include <set>
 
 #include "core/BaseObject.h"
 
@@ -46,21 +46,18 @@
 
             virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
 
-            void fire();
+            void fire(unsigned int weaponmode);
 
-            Weapon * getWeaponPointer(unsigned int n) const;
-            int getSize() const;
+            void addWeapon(Weapon * weapon);
+            Weapon * getWeapon(unsigned int index) const;
 
-            void setFireMode(unsigned int firemode);
-            unsigned int getFireMode() const;
+            inline size_t getNumWeapons() const
+                { return this->weapons_.size(); }
 
-            void addWeapon(Weapon * weapon);
-            const Weapon * getWeapon(unsigned int index) const;
+            unsigned int getDesiredWeaponmode(unsigned int firemode) { return 0; } // TODO
 
             void attachNeededMunitionToAllWeapons();
 
-            //functions with effect to all weapons of the weaponPack
-            //functions needed for creating Pointer to the right objects (-->Pawn.cc)
             inline void setWeaponSystem(WeaponSystem *weaponSystem)
                 { this->weaponSystem_ = weaponSystem; this->setWeaponSystemToAllWeapons(weaponSystem); }
             inline WeaponSystem * getWeaponSystem() const
@@ -69,9 +66,8 @@
         private:
             void setWeaponSystemToAllWeapons(WeaponSystem * weaponSystem);
 
-            std::vector<Weapon *> weapons_;
-            WeaponSystem *weaponSystem_;
-            unsigned int firemode_;
+            std::set<Weapon *> weapons_;
+            WeaponSystem * weaponSystem_;
     };
 }
 

Modified: branches/weapons/src/orxonox/objects/weaponSystem/WeaponSet.cc
===================================================================
--- branches/weapons/src/orxonox/objects/weaponSystem/WeaponSet.cc	2009-04-09 14:52:36 UTC (rev 2913)
+++ branches/weapons/src/orxonox/objects/weaponSystem/WeaponSet.cc	2009-04-09 22:05:01 UTC (rev 2914)
@@ -30,23 +30,20 @@
 
 #include "core/CoreIncludes.h"
 #include "core/XMLPort.h"
-#include "objects/worldentities/pawns/Pawn.h"
 
-#include "Weapon.h"
-#include "WeaponSlot.h"
+#include "WeaponSystem.h"
 #include "WeaponPack.h"
-#include "WeaponSystem.h"
 
 namespace orxonox
 {
     CreateFactory(WeaponSet);
 
-    WeaponSet::WeaponSet(BaseObject* creator, int k) : BaseObject(creator)
+    WeaponSet::WeaponSet(BaseObject* creator) : BaseObject(creator)
     {
         RegisterObject(WeaponSet);
 
         this->weaponSystem_ = 0;
-        this->attachedWeaponPack_ = 0;
+        this->desiredFiremode_ = WeaponSystem::FIRE_MODE_UNASSIGNED;
 
 COUT(0) << "+WeaponSet" << std::endl;
     }
@@ -54,15 +51,18 @@
     WeaponSet::~WeaponSet()
     {
 COUT(0) << "~WeaponSet" << std::endl;
+
+        if (this->isInitialized() && this->weaponSystem_)
+            this->weaponSystem_->removeWeaponSet(this);
     }
 
     void WeaponSet::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     {
         SUPER(WeaponSet, XMLPort, xmlelement, mode);
 
-        XMLPortParam(WeaponSet, "firemode", setFireMode, getFireMode, xmlelement, mode);
+        XMLPortParam(WeaponSet, "firemode", setDesiredFiremode, getDesiredFiremode, xmlelement, mode);
     }
-
+/*
     void WeaponSet::attachWeaponPack(WeaponPack *wPack)
     {
         if ( this->weaponSystem_->getWeaponSlotSize()>0 && wPack->getSize()>0 && ( wPack->getSize() <= this->weaponSystem_->getWeaponSlotSize() ) )
@@ -99,12 +99,32 @@
             }
         }
     }
+*/
 
-
     void WeaponSet::fire()
     {
-        //fires all WeaponSlots available for this weaponSet attached from the WeaponPack
-        if (this->attachedWeaponPack_)
-            this->attachedWeaponPack_->fire();
+        // fire all WeaponPacks with their defined weaponmode
+        for (std::map<WeaponPack*, unsigned int>::iterator it = this->weaponpacks_.begin(); it != this->weaponpacks_.end(); ++it)
+            if (it->second != WeaponSystem::WEAPON_MODE_UNASSIGNED)
+                it->first->fire(it->second);
     }
+
+    void WeaponSet::setWeaponmodeLink(WeaponPack* weaponpack, unsigned int weaponmode)
+    {
+        this->weaponpacks_[weaponpack] = weaponmode;
+    }
+
+    void WeaponSet::removeWeaponmodeLink(WeaponPack* weaponpack)
+    {
+        this->weaponpacks_.erase(weaponpack);
+    }
+
+    unsigned int WeaponSet::getWeaponmodeLink(WeaponPack* weaponpack)
+    {
+        std::map<WeaponPack*, unsigned int>::iterator it = this->weaponpacks_.find(weaponpack);
+        if (it != this->weaponpacks_.end())
+            return it->second;
+        else
+            return WeaponSystem::WEAPON_MODE_UNASSIGNED;
+    }
 }

Modified: branches/weapons/src/orxonox/objects/weaponSystem/WeaponSet.h
===================================================================
--- branches/weapons/src/orxonox/objects/weaponSystem/WeaponSet.h	2009-04-09 14:52:36 UTC (rev 2913)
+++ branches/weapons/src/orxonox/objects/weaponSystem/WeaponSet.h	2009-04-09 22:05:01 UTC (rev 2914)
@@ -32,7 +32,7 @@
 
 #include "OrxonoxPrereqs.h"
 
-#include <vector>
+#include <map>
 
 #include "core/BaseObject.h"
 
@@ -41,19 +41,24 @@
     class _OrxonoxExport WeaponSet : public BaseObject
     {
         public:
-            WeaponSet(BaseObject* creator, int k = 0);
+            WeaponSet(BaseObject* creator);
             virtual ~WeaponSet();
 
             virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
 
-            void attachWeaponPack(WeaponPack *wPack);
+//            void attachWeaponPack(WeaponPack *wPack);
+
             void fire();
 
-            inline void setFireMode(const unsigned int firemode)
-                { this->firemode_ = firemode; }
-            inline unsigned int getFireMode() const
-                { return this->firemode_; }
+            void setWeaponmodeLink(WeaponPack* weaponpack, unsigned int weaponmode);
+            void removeWeaponmodeLink(WeaponPack* weaponpack);
+            unsigned int getWeaponmodeLink(WeaponPack* weaponpack);
 
+            inline void setDesiredFiremode(const unsigned int firemode)
+                { this->desiredFiremode_ = firemode; }
+            inline unsigned int getDesiredFiremode() const
+                { return this->desiredFiremode_; }
+
             inline void setWeaponSystem(WeaponSystem *weaponSystem)
                 { this->weaponSystem_ = weaponSystem; }
             inline WeaponSystem * getWeaponSystem() const
@@ -61,9 +66,8 @@
 
         private:
             WeaponSystem *weaponSystem_;
-            std::vector<WeaponSlot *> setWeaponSlots_;
-            unsigned int firemode_;
-            WeaponPack * attachedWeaponPack_;
+            unsigned int desiredFiremode_;
+            std::map<WeaponPack*, unsigned int> weaponpacks_;
     };
 }
 

Modified: branches/weapons/src/orxonox/objects/weaponSystem/WeaponSlot.cc
===================================================================
--- branches/weapons/src/orxonox/objects/weaponSystem/WeaponSlot.cc	2009-04-09 14:52:36 UTC (rev 2913)
+++ branches/weapons/src/orxonox/objects/weaponSystem/WeaponSlot.cc	2009-04-09 22:05:01 UTC (rev 2914)
@@ -33,6 +33,7 @@
 #include "core/XMLPort.h"
 
 #include "Weapon.h"
+#include "WeaponSystem.h"
 
 namespace orxonox
 {
@@ -42,8 +43,9 @@
     {
         RegisterObject(WeaponSlot);
 
-        this->unlimitedAmmo_ = false;
-        this->attachedWeapon_ = 0;
+        this->weaponSystem_ = 0;
+        this->weapon_ = 0;
+
         this->setObjectMode(0x0);
 
 COUT(0) << "+WeaponSlot" << std::endl;
@@ -52,40 +54,38 @@
     WeaponSlot::~WeaponSlot()
     {
 COUT(0) << "~WeaponSlot" << std::endl;
+
+        if (this->isInitialized() && this->weaponSystem_)
+            this->weaponSystem_->removeWeaponSlot(this);
     }
 
     void WeaponSlot::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     {
         SUPER(WeaponSlot, XMLPort, xmlelement, mode);
+
+        // ...
     }
 
-    /*sets the munition type
-     *unlimited: true
-     *limited:   false  (in this case there will be munition)
-     */
-    void WeaponSlot::setAmmoType(bool isUnlimited)
+    void WeaponSlot::attachWeapon(Weapon *weapon)
     {
-        unlimitedAmmo_ = isUnlimited;
-    }
+        if (this->weapon_)
+            this->removeWeapon();
 
+        this->weapon_ = weapon;
 
-    void WeaponSlot::fire()
-    {
-        if ( this->attachedWeapon_ )
-//COUT(0) << "WeaponSlot::fire" << std::endl;
-        this->attachedWeapon_->fire();
+        if (this->weapon_)
+        {
+            this->weapon_->setWeaponSlot(this);
+            this->weapon_->setPosition(this->getPosition());
+        }
     }
 
-    void WeaponSlot::attachWeapon(Weapon *weapon)
+    void WeaponSlot::removeWeapon()
     {
-        this->attachedWeapon_ = weapon;
-        weapon->setAttachedToWeaponSlot(this);
-//COUT(0) << "WeaponSlot::attachWeapon position=" << this->getWorldPosition() << std::endl;
-        weapon->setPosition(this->getPosition());
+        if (this->weapon_)
+        {
+            this->weapon_->setWeaponSlot(0);
+            this->weapon_ = 0;
+        }
     }
-
-    Weapon * WeaponSlot::getAttachedWeapon() const
-    {
-        return this->attachedWeapon_;
-    }
 }

Modified: branches/weapons/src/orxonox/objects/weaponSystem/WeaponSlot.h
===================================================================
--- branches/weapons/src/orxonox/objects/weaponSystem/WeaponSlot.h	2009-04-09 14:52:36 UTC (rev 2913)
+++ branches/weapons/src/orxonox/objects/weaponSystem/WeaponSlot.h	2009-04-09 22:05:01 UTC (rev 2914)
@@ -42,22 +42,23 @@
 
             virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
 
-            void attachWeapon(Weapon *weapon);
-            Weapon * getAttachedWeapon() const;
-            void setAmmoType(bool isUnlimited);
-            void fire();
+            void attachWeapon(Weapon * weapon);
+            void removeWeapon();
+            Weapon * getWeapon() const
+                { return this->weapon_; }
 
-            inline void setWeaponSystem(WeaponSystem *weaponSystem)
+            inline bool isOccupied() const
+                { return (this->weapon_ != 0); }
+
+            inline void setWeaponSystem(WeaponSystem * weaponSystem)
                 { this->weaponSystem_ = weaponSystem; }
             inline WeaponSystem * getWeaponSystem() const
                 { return this->weaponSystem_; }
 
 
         private:
-            Weapon *attachedWeapon_;
-            bool unlimitedAmmo_;
-
-            WeaponSystem *weaponSystem_;
+            WeaponSystem * weaponSystem_;
+            Weapon * weapon_;
     };
 }
 

Modified: branches/weapons/src/orxonox/objects/weaponSystem/WeaponSystem.cc
===================================================================
--- branches/weapons/src/orxonox/objects/weaponSystem/WeaponSystem.cc	2009-04-09 14:52:36 UTC (rev 2913)
+++ branches/weapons/src/orxonox/objects/weaponSystem/WeaponSystem.cc	2009-04-09 22:05:01 UTC (rev 2914)
@@ -35,6 +35,7 @@
 #include "WeaponSlot.h"
 #include "WeaponPack.h"
 #include "WeaponSet.h"
+#include "Weapon.h"
 
 /* WeaponSystem
  *
@@ -60,65 +61,161 @@
         {
             if (this->pawn_)
                 this->pawn_->setWeaponSystem(0);
+
+            for (std::map<unsigned int, WeaponSet*>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); )
+                delete (it++)->second;
+
+            for (std::set<WeaponPack*>::iterator it = this->weaponPacks_.begin(); it != this->weaponPacks_.end(); )
+                delete (*(it++));
+
+            for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); )
+                delete (*(it++));
         }
     }
 
-    void WeaponSystem::attachWeaponSet(WeaponSet *wSet)
+    void WeaponSystem::addWeaponSlot(WeaponSlot * wSlot)
     {
-        if (!wSet)
+        if (!wSlot)
             return;
 
-        this->weaponSets_[wSet->getFireMode()] = wSet;
-        wSet->setWeaponSystem(this);
+        this->weaponSlots_.insert(wSlot);
+        wSlot->setWeaponSystem(this);
     }
 
-    WeaponSet * WeaponSystem::getWeaponSet(unsigned int index) const
+    void WeaponSystem::removeWeaponSlot(WeaponSlot * wSlot)
     {
+        if (!wSlot)
+            return;
+
+        if (wSlot->getWeapon())
+            this->removeWeaponPack(wSlot->getWeapon()->getWeaponPack());
+
+        this->weaponSlots_.erase(wSlot);
+    }
+
+    WeaponSlot * WeaponSystem::getWeaponSlot(unsigned int index) const
+    {
         unsigned int i = 0;
-        for (std::map<unsigned int, WeaponSet*>::const_iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
+        for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
         {
             ++i;
             if (i > index)
-                return it->second;
+                return (*it);
         }
         return 0;
     }
 
-    void WeaponSystem::attachWeaponSlot(WeaponSlot *wSlot)
+    bool WeaponSystem::addWeaponSet(WeaponSet * wSet)
     {
-        if (!wSlot)
-            return;
+        if (wSet)
+            return this->addWeaponSet(wSet, wSet->getDesiredFiremode());
+        else
+            return false;
+    }
 
-        this->weaponSlots_.insert(wSlot);
-        wSlot->setWeaponSystem(this);
+    bool WeaponSystem::addWeaponSet(WeaponSet * wSet, unsigned int firemode)
+    {
+        if (!wSet || firemode >= WeaponSystem::MAX_FIRE_MODES)
+            return false;
+
+        std::map<unsigned int, WeaponSet*>::const_iterator it = this->weaponSets_.find(firemode);
+        if (it == this->weaponSets_.end())
+        {
+            this->weaponSets_[firemode] = wSet;
+            wSet->setWeaponSystem(this);
+            return true;
+        }
+
+        return false;
     }
 
-    WeaponSlot * WeaponSystem::getWeaponSlot(unsigned int index) const
+    void WeaponSystem::removeWeaponSet(WeaponSet * wSet)
     {
+        for (std::map<unsigned int, WeaponSet*>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); )
+        {
+            if (it->second == wSet)
+                this->weaponSets_.erase(it++);
+            else
+                ++it;
+        }
+    }
+
+    WeaponSet * WeaponSystem::getWeaponSet(unsigned int index) const
+    {
         unsigned int i = 0;
-        for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
+        for (std::map<unsigned int, WeaponSet*>::const_iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
         {
             ++i;
             if (i > index)
-                return (*it);
+                return it->second;
         }
         return 0;
     }
 
-    void WeaponSystem::attachWeaponPack(WeaponPack *wPack, unsigned int wSetNumber)
+    bool WeaponSystem::canAddWeaponPack(WeaponPack * wPack)
     {
         if (!wPack)
-            return;
+            return false;
 
-        std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.find(wSetNumber);
-        if (it != this->weaponSets_.end() && it->second)
-            it->second->attachWeaponPack(wPack);
+        unsigned int freeSlots = 0;
+        for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
+        {
+            if (!(*it)->isOccupied())
+                ++freeSlots;
+        }
 
+        return (freeSlots >= wPack->getNumWeapons());
+    }
+
+    bool WeaponSystem::addWeaponPack(WeaponPack * wPack)
+    {
+        if (!this->canAddWeaponPack(wPack))
+            return false;
+
+        // Attach all weapons to the first free slots (and to the Pawn)
+        unsigned int i = 0;
+        for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
+        {
+            if (!(*it)->isOccupied() && i < wPack->getNumWeapons())
+            {
+                Weapon* weapon = wPack->getWeapon(i);
+                (*it)->attachWeapon(weapon);
+                this->getPawn()->attach(weapon);
+                ++i;
+            }
+        }
+
+        // Assign the desired weaponmode to the firemodes
+        for (std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
+        {
+            unsigned int weaponmode = wPack->getDesiredWeaponmode(it->first);
+            if (weaponmode != WeaponSystem::WEAPON_MODE_UNASSIGNED)
+                it->second->setWeaponmodeLink(wPack, weaponmode);
+        }
+
         this->weaponPacks_.insert(wPack);
         wPack->setWeaponSystem(this);
         wPack->attachNeededMunitionToAllWeapons(); // TODO - what is this?
+
+        return true;
     }
 
+    void WeaponSystem::removeWeaponPack(WeaponPack * wPack)
+    {
+        // Remove all weapons from their WeaponSlot
+        unsigned int i = 0;
+        Weapon* weapon = 0;
+        while (weapon = wPack->getWeapon(i++))
+            weapon->getWeaponSlot()->removeWeapon();
+
+        // Remove all added links from the WeaponSets
+        for (std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
+            it->second->removeWeaponmodeLink(wPack);
+
+        // Remove the WeaponPack from the WeaponSystem
+        this->weaponPacks_.erase(wPack);
+    }
+
     WeaponPack * WeaponSystem::getWeaponPack(unsigned int index) const
     {
         unsigned int i = 0;
@@ -129,8 +226,40 @@
                 return (*it);
         }
         return 0;
-   }
+    }
 
+    bool WeaponSystem::swapWeaponSlots(WeaponSlot * wSlot1, WeaponSlot * wSlot2)
+    {
+        // TODO
+    }
+
+    void WeaponSystem::changeWeaponmode(WeaponPack * wPack, WeaponSet * wSet, unsigned int weaponmode)
+    {
+        if (!wPack || !wSet)
+            return;
+
+        // Check if the WeaponPack belongs to this WeaponSystem
+        std::set<WeaponPack *>::iterator it1 = this->weaponPacks_.find(wPack);
+        if (it1 == this->weaponPacks_.end())
+            return;
+
+        // Check if the WeaponSet belongs to this WeaponSystem
+        bool foundWeaponSet = false;
+        for (std::map<unsigned int, WeaponSet *>::iterator it2 = this->weaponSets_.begin(); it2 != this->weaponSets_.end(); ++it2)
+        {
+            if (it2->second == wSet)
+            {
+                foundWeaponSet = true;
+                break;
+            }
+        }
+        if (!foundWeaponSet)
+            return;
+
+        // Finally set the link between firemode and weaponmode
+        wSet->setWeaponmodeLink(wPack, weaponmode);
+    }
+
     void WeaponSystem::setNewMunition(const std::string& munitionType, Munition * munitionToAdd)
     {
         this->munitionSet_[munitionType] = munitionToAdd;
@@ -147,10 +276,6 @@
             return 0;
     }
 
-
-    //n is the n'th weaponSet, starting with zero
-    //SpaceShip.cc only needs to have the keybinding to a specific Set-number n (=firemode)
-    //in future this could be well defined and not only for 3 different WeaponModes
     void WeaponSystem::fire(unsigned int firemode)
     {
         std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.find(firemode);

Modified: branches/weapons/src/orxonox/objects/weaponSystem/WeaponSystem.h
===================================================================
--- branches/weapons/src/orxonox/objects/weaponSystem/WeaponSystem.h	2009-04-09 14:52:36 UTC (rev 2913)
+++ branches/weapons/src/orxonox/objects/weaponSystem/WeaponSystem.h	2009-04-09 22:05:01 UTC (rev 2914)
@@ -39,25 +39,36 @@
 
 namespace orxonox
 {
-    const unsigned int MAX_FIRE_MODES = 8;
-
     class _OrxonoxExport WeaponSystem : public BaseObject
     {
         public:
             WeaponSystem(BaseObject* creator);
             virtual ~WeaponSystem();
 
-            void attachWeaponSlot(WeaponSlot * wSlot);
+            // adding and removing WeaponSlots
+            void addWeaponSlot(WeaponSlot * wSlot);
+            void removeWeaponSlot(WeaponSlot * wSlot);
             WeaponSlot * getWeaponSlot(unsigned int index) const;
 
-            void attachWeaponSet(WeaponSet * wSet);
+            // adding and removing WeaponSets
+            bool addWeaponSet(WeaponSet * wSet);
+            bool addWeaponSet(WeaponSet * wSet, unsigned int firemode);
+            void removeWeaponSet(WeaponSet * wSet);
             WeaponSet * getWeaponSet(unsigned int index) const;
 
-            void attachWeaponPack(WeaponPack * wPack, unsigned int wSetNumber);
+            // adding and removing WeaponPacks
+            bool canAddWeaponPack(WeaponPack * wPack);
+            bool addWeaponPack(WeaponPack * wPack);
+            void removeWeaponPack(WeaponPack * wPack);
             WeaponPack * getWeaponPack(unsigned int index) const;
 
+            // configure slots and firemodes
+            bool swapWeaponSlots(WeaponSlot * wSlot1, WeaponSlot * wSlot2);
+            void changeWeaponmode(WeaponPack * wPack, WeaponSet * wSet, unsigned int weaponmode);
+
             void fire(unsigned int firemode);
 
+
             void setNewMunition(const std::string& munitionType, Munition * munitionToAdd);
             void setNewSharedMunition(const std::string& munitionType, Munition * munitionToAdd);
             Munition * getMunitionType(const std::string& munitionType) const;
@@ -70,11 +81,15 @@
             inline int getWeaponSlotSize() const
                 { return this->weaponSlots_.size(); }
 
-            static inline unsigned int getMaxFireModes()
-                { return MAX_FIRE_MODES; }
-            static inline unsigned int getFireModeMask(unsigned int firemode)
+            static inline unsigned int getFiremodeMask(unsigned int firemode)
                 { return (0x1 << firemode); }
 
+            static const unsigned int MAX_FIRE_MODES = 8;
+            static const unsigned int FIRE_MODE_UNASSIGNED = (unsigned int)-1;
+
+            static const unsigned int MAX_WEAPON_MODES = 8;
+            static const unsigned int WEAPON_MODE_UNASSIGNED = (unsigned int)-1;
+
         private:
             std::map<unsigned int, WeaponSet *> weaponSets_;
             std::set<WeaponSlot *> weaponSlots_;

Modified: branches/weapons/src/orxonox/objects/worldentities/pawns/Pawn.cc
===================================================================
--- branches/weapons/src/orxonox/objects/worldentities/pawns/Pawn.cc	2009-04-09 14:52:36 UTC (rev 2913)
+++ branches/weapons/src/orxonox/objects/worldentities/pawns/Pawn.cc	2009-04-09 22:05:01 UTC (rev 2914)
@@ -122,8 +122,8 @@
 
         if (this->weaponSystem_)
         {
-            for (unsigned int firemode = 0; firemode < WeaponSystem::getMaxFireModes(); firemode++)
-                if (this->fire_ & WeaponSystem::getFireModeMask(firemode))
+            for (unsigned int firemode = 0; firemode < WeaponSystem::MAX_FIRE_MODES; firemode++)
+                if (this->fire_ & WeaponSystem::getFiremodeMask(firemode))
                     this->weaponSystem_->fire(firemode);
         }
         this->fire_ = this->firehack_;
@@ -254,7 +254,7 @@
 
     void Pawn::fire(unsigned int firemode)
     {
-        this->firehack_ |= WeaponSystem::getFireModeMask(firemode);
+        this->firehack_ |= WeaponSystem::getFiremodeMask(firemode);
     }
 
     void Pawn::postSpawn()
@@ -279,7 +279,7 @@
     {
         this->attach(wSlot);
         if (this->weaponSystem_)
-            this->weaponSystem_->attachWeaponSlot(wSlot);
+            this->weaponSystem_->addWeaponSlot(wSlot);
     }
 
     WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
@@ -290,30 +290,30 @@
             return 0;
     }
 
-    void Pawn::addWeaponPack(WeaponPack * wPack)
+    void Pawn::addWeaponSet(WeaponSet * wSet)
     {
         if (this->weaponSystem_)
-            this->weaponSystem_->attachWeaponPack(wPack, wPack->getFireMode());
+            this->weaponSystem_->addWeaponSet(wSet);
     }
 
-    WeaponPack * Pawn::getWeaponPack(unsigned int firemode) const
+    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
     {
         if (this->weaponSystem_)
-            return this->weaponSystem_->getWeaponPack(firemode);
+            return this->weaponSystem_->getWeaponSet(index);
         else
             return 0;
     }
 
-    void Pawn::addWeaponSet(WeaponSet * wSet)
+    void Pawn::addWeaponPack(WeaponPack * wPack)
     {
         if (this->weaponSystem_)
-            this->weaponSystem_->attachWeaponSet(wSet);
+            this->weaponSystem_->addWeaponPack(wPack);
     }
 
-    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
+    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
     {
         if (this->weaponSystem_)
-            return this->weaponSystem_->getWeaponSet(index);
+            return this->weaponSystem_->getWeaponPack(index);
         else
             return 0;
     }

Modified: branches/weapons/src/orxonox/objects/worldentities/pawns/Pawn.h
===================================================================
--- branches/weapons/src/orxonox/objects/worldentities/pawns/Pawn.h	2009-04-09 14:52:36 UTC (rev 2913)
+++ branches/weapons/src/orxonox/objects/worldentities/pawns/Pawn.h	2009-04-09 22:05:01 UTC (rev 2914)
@@ -84,10 +84,10 @@
 
             void addWeaponSlot(WeaponSlot * wSlot);
             WeaponSlot * getWeaponSlot(unsigned int index) const;
-            void addWeaponPack(WeaponPack * wPack);
-            WeaponPack * getWeaponPack(unsigned int firemode) const;
             void addWeaponSet(WeaponSet * wSet);
             WeaponSet * getWeaponSet(unsigned int index) const;
+            void addWeaponPack(WeaponPack * wPack);
+            WeaponPack * getWeaponPack(unsigned int index) const;
 
             inline const WorldEntity* getWorldEntity() const
                 { return const_cast<Pawn*>(this); }




More information about the Orxonox-commit mailing list