- Utility script for parameters handling
authorunc0rr
Fri, 03 Jan 2014 00:37:48 +0400
changeset 9908 81eb25ddf594
parent 9906 93c16a4b0c6a
child 9910 b0988e40e411
- Utility script for parameters handling - Add varying gravity to Gravity script
share/hedgewars/Data/Scripts/Multiplayer/Gravity.lua
share/hedgewars/Data/Scripts/Params.lua
--- a/share/hedgewars/Data/Scripts/Multiplayer/Gravity.lua	Wed Jan 01 23:41:49 2014 +0400
+++ b/share/hedgewars/Data/Scripts/Multiplayer/Gravity.lua	Fri Jan 03 00:37:48 2014 +0400
@@ -2,8 +2,14 @@
 HedgewarsScriptLoad("/Scripts/Params.lua")
 
 local gravity = 100
+local mingravity
+local maxgravity
+local delta = 0
+local period
+local periodtimer = 0
 local wdGameTicks = 0
 local wdTTL = 0
+local mln = 1000000
 
 function onNewTurn()
     SetGravity(gravity)
@@ -13,9 +19,33 @@
 function onGameTick20()
     if wdGameTicks + 15000 < GameTime then
         SetGravity(100)
-    elseif wdTTL ~= TurnTimeLeft then
-        wdGameTicks = GameTime
-        SetGravity(gravity)
+    else
+        if wdTTL ~= TurnTimeLeft then
+            wdGameTicks = GameTime
+        end
+
+        if delta == nil then
+            if periodtimer == 0 then
+                periodtimer = period * 2
+                SetGravity(div(GetRandom(maxgravity - mingravity) + mingravity, mln))
+            else
+                periodtimer = periodtimer - 1
+            end
+        elseif delta == 0 then
+            SetGravity(gravity)
+        else
+            if delta > 0 and gravity + delta > maxgravity then
+                gravity = maxgravity
+                delta = -delta
+            elseif delta < 0 and gravity - delta < mingravity then
+                gravity = mingravity
+                delta = -delta
+            else
+                gravity = gravity + delta
+            end
+
+            SetGravity(div(gravity, mln))
+        end
     end
 
     wdTTL = TurnTimeLeft
@@ -23,12 +53,54 @@
 
 function onGameInit()
     parseParams()
-    gravity = ScriptParam
+
+    gravity = params["g"]
+
+    mingravity = gravity
+    maxgravity = params["g2"]
+    period = params["period"]
+
+    if mingravity ~= nil and maxgravity ~= nil then
+        if period ~= nil then
+            period = div(period, 40)
+        else
+            period = 125
+        end
+
+        if mingravity > maxgravity then
+            mingravity, maxgravity = maxgravity, mingravity
+        end
+
+        mingravity = mingravity * mln
+        maxgravity = maxgravity * mln
+        gravity = mingravity
+
+        if period > 0 then
+            delta = div(maxgravity - mingravity, period)
+        else
+            period = -period
+            delta = nil
+        end
+    end
+
+    if gravity == nil then
+        gravity = 100
+    end
 end
 
 function onGameStart()
+    if delta == nil then
+        v = string.format(loc("random in range from %i%% to %i%% with period of %i msec"), div(mingravity, mln), div(maxgravity, mln), period * 40)
+    elseif period ~= nil then
+        v = string.format(loc("changing range from %i%% to %i%% with period of %i msec"), div(mingravity, mln), div(maxgravity, mln), period * 40)
+    else
+        v = div(gravity, mln) .. "%"
+    end
+
     ShowMission(loc("Gravity"),
-                loc("Current value is ") .. gravity .. "%",
-                loc("Set any gravity value you want by adjusting get away time"),
+                loc("Current setting is ") .. v,
+                loc("Setup:|'g=150', where 150 is 150% of normal gravity") .. "|"
+                .. loc("or 'g=50, g2=150, period=4000' for gravity changing|from 50 to 150 and back with period of 4000 msec")
+                .. "||" .. loc("Set period to negative value for random gravity"),
                 0, 5000)
 end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/share/hedgewars/Data/Scripts/Params.lua	Fri Jan 03 00:37:48 2014 +0400
@@ -0,0 +1,9 @@
+-- Library for parameters handling
+
+params = {}
+
+function parseParams()
+    for k, v in string.gmatch(ScriptParam, "(%w+)=([^,]+)") do
+        params[k] = v
+    end
+end