cleared up Random Weapons and implemented remembering of weapons in Balanced Random Weapons too, also seems to work
authorHenek
Mon, 11 Apr 2011 18:23:43 +0200
changeset 5141 2fb6555011d3
parent 5140 932307228d05
child 5142 cb822f8d52ff
cleared up Random Weapons and implemented remembering of weapons in Balanced Random Weapons too, also seems to work
share/hedgewars/Data/Scripts/Multiplayer/Balanced_Random_Weapon.lua
share/hedgewars/Data/Scripts/Multiplayer/Random_Weapon.lua
--- a/share/hedgewars/Data/Scripts/Multiplayer/Balanced_Random_Weapon.lua	Mon Apr 11 11:22:10 2011 -0400
+++ b/share/hedgewars/Data/Scripts/Multiplayer/Balanced_Random_Weapon.lua	Mon Apr 11 18:23:43 2011 +0200
@@ -1,4 +1,5 @@
 loadfile(GetDataPath() .. "Scripts/Locale.lua")()
+loadfile(GetDataPath() .. "Scripts/Tracker.lua")()
 
 local weapons = { amGrenade, amClusterBomb, amBazooka, amBee, amShotgun, amMine, amDEagle, amDynamite, amFirePunch, amWhip, amPickHammer, amBaseballBat, amMortar, amCake, amSeduction, amWatermelon, amHellishBomb, amDrill, amBallgun, amRCPlane, amSniperRifle, amMolotov, amBirdy, amBlowTorch, amGasBomb, amFlamethrower, amSMine, amKamikaze }
 
@@ -15,12 +16,89 @@
 --                        T,G,S,L,R,R,P,J,P,S
 local utilities_values = {1,2,2,1,2,2,1,2,2,2}
 
+function randomAmmo()
+    local n = 3   --"points" to be allocated on weapons
+    
+    --pick random weapon and subtract cost
+    local r = GetRandom(table.maxn(weapons_values)) + 1
+    local picked_items = {}
+    table.insert(picked_items, weapons[r])
+    n = n - weapons_values[r]
+    
+    
+    --choose any weapons or utilities to use up remaining n
+    
+    while n > 0 do
+        local items = {}
+        local items_values = {}
+
+        for i, w in pairs(weapons_values) do
+            local used = false
+            if w <= n then
+                --check that this weapon hasn't been given already
+                for j, k in pairs(picked_items) do
+                    if weapons[i] == k then
+                        used = true
+                    end
+                end
+                if not used then
+                    table.insert(items_values, w)
+                    table.insert(items, weapons[i])
+                end
+            end
+        end
+
+        for i, w in pairs(utilities_values) do
+            local used = false
+            if w <= n then
+                --check that this weapon hasn't been given already
+                for j, k in pairs(picked_items) do
+                    if utilities[i] == k then
+                        used = true
+                    end
+                end
+                if not used then
+                    table.insert(items_values, w)
+                    table.insert(items, utilities[i])
+                end
+            end
+        end
+        
+        local r = GetRandom(table.maxn(items_values)) + 1
+        table.insert(picked_items, items[r])
+        n = n - items_values[r]
+    end
+    
+    return picked_items
+end
+
+function assignAmmo(hog)
+    local name = GetHogTeamName(hog)
+    local processed = getTeamValue(name, "processed")
+    if processed == nil or not processed then
+        local ammo = getTeamValue(name, "ammo")
+        if ammo == nil then
+            ammo = randomAmmo()
+            setTeamValue(name, "ammo", ammo)
+        end
+        for i, w in pairs(ammo) do
+            AddAmmo(hog, w)
+        end
+        setTeamValue(name, "processed", true)
+    end
+end
+
+function reset(hog)
+    setTeamValue(GetHogTeamName(hog), "processed", false)
+end
+
 function onGameInit()
     GameFlags = band(bor(GameFlags, gfResetWeps), bnot(gfPerHogAmmo))
-    Goals = loc("Each turn you get 1-3 random weapons|The stronger they are, the fewer you get")
+    Goals = loc("Each turn you get 1-3 random weapons")
 end
 
 function onGameStart()
+    trackTeams()
     if MapHasBorder() == false then
         for i, w in pairs(airweapons) do
             table.insert(weapons, w)
@@ -29,8 +107,6 @@
             table.insert(weapons_values, w)
         end
     end
-
-    --ShowMission(loc("Balanced Random Weapons"), loc("A game of luck"), loc("Each turn you'll get a weapon, and if it sucks you'll get some more!"), -amSkip, 0)
 end
 
 function onAmmoStoreInit()
@@ -56,57 +132,17 @@
 end
 
 function onNewTurn()
-    local n = 3   --"points" to be allocated on weapons
-    
-    --pick random weapon and subtract cost
-    local r = GetRandom(table.maxn(weapons_values)) + 1
-    AddAmmo(CurrentHedgehog, weapons[r])
-    local items_used = {}
-    items_used[1] = weapons[r]
-    n = n - weapons_values[r]
-    
-    
-    --choose any weapons or utilities to use up remaining n
-    
-    while n > 0 do
-        local items = {}
-        local items_values = {}
+    runOnGears(assignAmmo)
+    runOnGears(reset)
+    setTeamValue(GetHogTeamName(CurrentHedgehog), "ammo", nil)
+end
 
-        for i, w in pairs(weapons_values) do
-            local used = false
-            if w <= n then
-                --check that this weapon hasn't been given already
-                for j = 1, table.maxn(items_used) do
-                    if weapons[i] == items_used[j] then
-                        used = true
-                    end
-                end
-                if not used then
-                    table.insert(items_values, w)
-                    table.insert(items, weapons[i])
-                end
-            end
-        end
-
-        for i, w in pairs(utilities_values) do
-            local used = false
-            if w <= n then
-                --check that this weapon hasn't been given already
-                for j = 1, table.maxn(items_used) do
-                    if utilities[i] == items_used[j] then
-                        used = true
-                    end
-                end
-                if not used then
-                    table.insert(items_values, w)
-                    table.insert(items, utilities[i])
-                end
-            end
-        end
-        
-        local r = GetRandom(table.maxn(items_values)) + 1
-        AddAmmo(CurrentHedgehog, items[r])
-        table.insert(items_used, items[r])
-        n = n - items_values[r]
+function onGearAdd(gear)
+    if GetGearType(gear) == gtHedgehog then
+        trackGear(gear)
     end
 end
+
+function onGearDelete(gear)
+    trackDeletion(gear)
+end
--- a/share/hedgewars/Data/Scripts/Multiplayer/Random_Weapon.lua	Mon Apr 11 11:22:10 2011 -0400
+++ b/share/hedgewars/Data/Scripts/Multiplayer/Random_Weapon.lua	Mon Apr 11 18:23:43 2011 +0200
@@ -18,17 +18,30 @@
 local airweapons = { amAirAttack, amMineStrike, amNapalm, amDrillStrike }
 
 -- Function that assigns the team their weapon
--- Due to the fact that the gameplay uses reset weapons and no inf attack there is no point in limiting the ammo count
-function assignWeapon(hog)
-    -- Get the ammo for this hog's team
-    local ammo = getTeamValue(GetHogTeamName(hog), "ammo")
-    -- If there is no ammo, get a random one from the list and store it
-    if ammo == nil then
-        ammo = weapons[GetRandom(table.maxn(weapons)) + 1]
-        setTeamValue(GetHogTeamName(hog), "ammo", ammo)
+function assignAmmo(hog)
+    -- Get name of the current team
+    local name = GetHogTeamName(hog)
+    -- Get whither the team has been processed
+    local processed = getTeamValue(name, "processed")
+    -- If it has not, process it
+    if processed == nil or not processed then
+        -- Get the ammo for this hog's team
+        local ammo = getTeamValue(name, "ammo")
+        -- If there is no ammo, get a random one from the list and store it
+        if ammo == nil then
+            ammo = weapons[GetRandom(table.maxn(weapons)) + 1]
+            setTeamValue(name, "ammo", ammo)
+        end
+        -- Add the ammo for the hog
+        AddAmmo(hog, ammo)
+        -- Mark as processed
+        setTeamValue(name, "processed", true)
     end
-    -- Add the ammo for the hog
-    AddAmmo(hog, ammo)
+end
+
+-- Mark team as not processed
+function reset(hog)
+    setTeamValue(GetHogTeamName(hog), "processed", false)
 end
 
 function onGameInit()
@@ -80,7 +93,9 @@
 
 function onNewTurn()
     -- Give every team their weapons, so one can plan during anothers turn
-    runOnGears(assignWeapon)
+    runOnGears(assignAmmo)
+    -- Mark all teams as not processed
+    runOnGears(reset)
     -- Set the current teams weapons to nil so they will get new after the turn has ended
     setTeamValue(GetHogTeamName(CurrentHedgehog), "ammo", nil)
 end