Fix BRW breaking when weapon scheme assigns 2 bullet for all ammos
authorWuzzy <Wuzzy2@mail.ru>
Fri, 31 Aug 2018 11:11:58 +0200
changeset 13717 ed22eb551a75
parent 13716 6d57f5f61c09
child 13718 7284233d9132
Fix BRW breaking when weapon scheme assigns 2 bullet for all ammos
share/hedgewars/Data/Scripts/Multiplayer/Balanced_Random_Weapon.lua
--- a/share/hedgewars/Data/Scripts/Multiplayer/Balanced_Random_Weapon.lua	Thu Aug 30 22:07:01 2018 +0200
+++ b/share/hedgewars/Data/Scripts/Multiplayer/Balanced_Random_Weapon.lua	Fri Aug 31 11:11:58 2018 +0200
@@ -26,6 +26,7 @@
 
 local weapons = {}
 local weapons_values = {}
+local weapons_count = 0
 local airweapons = {}
 local airweapons_values = {}
 local utilities = {}
@@ -57,7 +58,7 @@
     local n = 3   --"points" to be allocated on weapons
 
     --pick random weapon and subtract cost
-    local r = GetRandom(table.maxn(weapons_values)) + 1
+    local r = GetRandom(weapons_count) + 1
     local picked_items = {}
     table.insert(picked_items, weapons[r])
     n = n - weapons_values[r]
@@ -68,6 +69,7 @@
     while n > 0 do
         local items = {}
         local items_values = {}
+        local items_count = 0
 
         for i, w in pairs(weapons_values) do
             local used = false
@@ -81,6 +83,7 @@
                 if not used then
                     table.insert(items_values, w)
                     table.insert(items, weapons[i])
+                    items_count = items_count + 1
                 end
             end
         end
@@ -97,13 +100,18 @@
                 if not used then
                     table.insert(items_values, w)
                     table.insert(items, utilities[i])
+                    items_count = items_count + 1
                 end
             end
         end
 
-        local r = GetRandom(table.maxn(items_values)) + 1
-        table.insert(picked_items, items[r])
-        n = n - items_values[r]
+        if items_count > 0 then
+            local r = GetRandom(items_count) + 1
+            table.insert(picked_items, items[r])
+            n = n - items_values[r]
+        else
+            break
+        end
     end
 
     return picked_items
@@ -214,6 +222,7 @@
                     else
                         table.insert(weapons, a)
                         table.insert(weapons_values, cost)
+                        weapons_count = weapons_count + 1
                     end
                 end
             else