Index: code/branches/fabienHS15/src/orxonox/weaponsystem/Munition.cc
===================================================================
--- code/branches/fabienHS15/src/orxonox/weaponsystem/Munition.cc	(revision 10688)
+++ code/branches/fabienHS15/src/orxonox/weaponsystem/Munition.cc	(revision 10713)
@@ -45,9 +45,11 @@
         this->magazines_ = 10;
 
-        this->deployment_ = DEPLOYMENT_STACK;
+        this->deployment_ = MunitionDeployment::Stack;
         this->bAllowMunitionRefilling_ = true;
         this->bAllowMultiMunitionRemovementUnderflow_ = true;
 
         this->reloadTime_ = 0.5f;
+
+        this->lastFilledWeaponMode_ = NULL;
     }
 
@@ -60,5 +62,5 @@
     Munition::Magazine* Munition::getMagazine(WeaponMode* user) const
     {
-        if (deployment_ == DEPLOYMENT_SEPARATE)
+        if (deployment_ == MunitionDeployment::Separate)
         {
             // For separated magazines we definitively need a given user
@@ -86,5 +88,5 @@
         if (magazine)
         {
-            if (deployment_ == DEPLOYMENT_STACK)
+            if (deployment_ == MunitionDeployment::Stack)
                 // With stacked munition every magazine contributes to the total amount
                 return this->maxMunitionPerMagazine_ * this->magazines_ + magazine->munition_;
@@ -108,5 +110,5 @@
     unsigned int Munition::getNumMagazines() const
     {
-        if (deployment_ == DEPLOYMENT_STACK)
+        if (deployment_ == MunitionDeployment::Stack)
         {
             // If we stack munition and the current magazine is still full, it counts too
@@ -121,5 +123,5 @@
     unsigned int Munition::getMaxMunition() const
     {
-        if (deployment_ == DEPLOYMENT_STACK)
+        if (deployment_ == MunitionDeployment::Stack)
             return this->maxMunitionPerMagazine_ * this->maxMagazines_;
         else
@@ -135,5 +137,5 @@
 
             // If we stack munition, we don't care about the current magazine - we just need enough munition in total
-            if (deployment_ == DEPLOYMENT_STACK)
+            if (deployment_ == MunitionDeployment::Stack)
                 munition += this->maxMunitionPerMagazine_ * this->magazines_;
 
@@ -168,5 +170,5 @@
             {
                 // Not enough munition
-                if (deployment_ == DEPLOYMENT_STACK)
+                if (deployment_ == MunitionDeployment::Stack)
                 {
                     // We stack munition, so just take what we can and then load the next magazine
@@ -198,5 +200,5 @@
     {
         // As long as we have enough magazines (and don't stack munition) we can reload
-        return (this->magazines_ > 0 && !deployment_ == DEPLOYMENT_STACK);
+        return (this->magazines_ > 0 && !deployment_ == MunitionDeployment::Stack);
     }
 
@@ -206,5 +208,5 @@
         if (magazine)
         {
-            if (deployment_ == DEPLOYMENT_STACK)
+            if (deployment_ == MunitionDeployment::Stack)
                 // With stacked munition, we never have to reload
                 return false;
@@ -230,9 +232,9 @@
 
         // If we use separate magazines for each user, we definitively need a user given
-        if (deployment_ == DEPLOYMENT_SEPARATE && !user)
+        if (deployment_ == MunitionDeployment::Separate && !user)
             return false;
 
         // If we don't use separate magazines, set user to 0
-        if (!deployment_ == DEPLOYMENT_SEPARATE)
+        if (!deployment_ == MunitionDeployment::Separate)
             user = 0;
 
@@ -241,4 +243,8 @@
         if (it != this->currentMagazines_.end())
         {
+            if (it->first == lastFilledWeaponMode_)
+            {
+                lastFilledWeaponMode_ = NULL;
+            }            
             delete it->second;
             this->currentMagazines_.erase(it);
@@ -259,5 +265,5 @@
             return false;
 
-        if (deployment_ == DEPLOYMENT_STACK)
+        if (deployment_ == MunitionDeployment::Stack)
         {
             // If we stack munition, we can always add munition until we reach the limit
@@ -280,5 +286,5 @@
             return false;
 
-        if (deployment_ == DEPLOYMENT_STACK)
+        if (deployment_ == MunitionDeployment::Stack)
         {
             // Stacking munition means, if a magazine gets full, the munition adds to a new magazine
@@ -311,4 +317,55 @@
         else
         {
+            std::map<WeaponMode*, Magazine*>::iterator it;
+
+            // If the pointer to the weapon mode whose magazine got munition added to is NULL, then set the iterator to the beginning of the map
+            // Otherwise set it to the next weapon mode
+            if (lastFilledWeaponMode_ == NULL)
+            {
+                it = this->currentMagazines_.begin();
+            }
+            else
+            {
+                it = this->currentMagazines_.find(lastFilledWeaponMode_);
+                ++ it;
+            }
+
+            // Share the munition equally to the current magazines
+            bool firstLoop = true;
+            while (amount > 0)
+            {
+                bool change = false;
+                while (it != this->currentMagazines_.end())
+                {
+                    // Add munition if the magazine isn't full (but only to loaded magazines)
+                    if (amount > 0 && it->second->munition_ < this->maxMunitionPerMagazine_ && it->second->bLoaded_)
+                    {
+                        it->second->munition_++;
+                        amount--;
+                        lastFilledWeaponMode_ = it->first;
+                        change = true;
+                    }
+
+                    ++it;
+                }
+
+                // If there was no change in a loop, all magazines are full (or locked due to loading)
+                // Because the first loop does not stat at the beginning of the map we have to treat is separately
+                if (!change && !firstLoop)
+                {
+                    break;
+                }                    
+
+                it = this->currentMagazines_.begin();
+                firstLoop = false;
+            }
+
+            return true;
+
+
+
+
+
+            /*
             // Share the munition equally to the current magazines
             while (amount > 0)
@@ -331,5 +388,5 @@
             }
 
-            return true;
+            return true;*/
         }
     }
@@ -339,5 +396,5 @@
         // TODO: 'amount' is not used
 
-        if (deployment_ == DEPLOYMENT_STACK)
+        if (deployment_ == MunitionDeployment::Stack)
             // If we stack munition, we can always add new magazines because they contribute directly to the munition
             return (this->getNumMunition(0) < this->getMaxMunition());
@@ -356,5 +413,5 @@
 
         // 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)
-        if (needed_magazines <= 0 && !deployment_ == DEPLOYMENT_STACK)
+        if (needed_magazines <= 0 && !deployment_ == MunitionDeployment::Stack)
             return false;
 
@@ -368,5 +425,5 @@
             // We get more magazines than we need, so just add the needed amount
             this->magazines_ += needed_magazines;
-            if (deployment_ == DEPLOYMENT_STACK)
+            if (deployment_ == MunitionDeployment::Stack)
             {
                 // We stack munition, so the additional amount contributes directly to the munition of the current magazine
@@ -382,5 +439,5 @@
     bool Munition::canRemoveMagazines(unsigned int amount) const
     {
-        if (deployment_ == DEPLOYMENT_STACK)
+        if (deployment_ == MunitionDeployment::Stack)
         {
             if (this->magazines_ >= amount)
@@ -421,5 +478,5 @@
             this->magazines_ -= amount;
         }
-        else if (deployment_ == DEPLOYMENT_STACK)
+        else if (deployment_ == MunitionDeployment::Stack)
         {
             // We don't have enough magazines, but we're stacking munition, so additionally remove the bullets from the current magazine
@@ -436,9 +493,9 @@
     {
         // If we use separate magazines, we need a user
-        if (deployment_ == DEPLOYMENT_SEPARATE && !user)
+        if (deployment_ == MunitionDeployment::Separate && !user)
             return false;
 
         // If we don't use separate magazines, set user to 0
-        if (!deployment_ == DEPLOYMENT_SEPARATE)
+        if (!deployment_ == MunitionDeployment::Separate)
             user = 0;
 
@@ -447,6 +504,10 @@
         if (it != this->currentMagazines_.end())
         {
+            if (it->first == lastFilledWeaponMode_)
+            {
+                lastFilledWeaponMode_ = NULL;
+            }
             delete it->second;
-            this->currentMagazines_.erase(it);
+            this->currentMagazines_.erase(it);           
             return true;
         }
@@ -464,5 +525,5 @@
         this->bLoaded_ = false;
 
-        if (bUseReloadTime && munition->reloadTime_ > 0 && !munition->deployment_ == DEPLOYMENT_STACK)
+        if (bUseReloadTime && munition->reloadTime_ > 0 && !munition->deployment_ == MunitionDeployment::Stack)
         {
             const ExecutorPtr& executor = createExecutor(createFunctor(&Magazine::loaded, this));
