GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now.
authorunc0rr
Tue, 14 Jan 2014 12:44:29 +0400
changeset 9985 42cd42e44c9a
parent 9983 1b62d993cd2f
child 9987 8f07b47a641b
GravRacer, GravHigh, GravMutant, Grav... uhm... shoppamap? Easy! Gravity script could be combined with any other script now. TODO: security concerns? script name to load is passed via game scheme.
hedgewars/uCommandHandlers.pas
hedgewars/uScript.pas
share/hedgewars/Data/Scripts/Multiplayer/Gravity.lua
share/hedgewars/Data/Scripts/Params.lua
--- a/hedgewars/uCommandHandlers.pas	Mon Jan 13 14:40:06 2014 -0500
+++ b/hedgewars/uCommandHandlers.pas	Tue Jan 14 12:44:29 2014 +0400
@@ -121,7 +121,8 @@
 
 procedure chScriptParam(var s: shortstring);
 begin
-    cScriptParam:= s;
+    ScriptSetString('ScriptParam', s);
+    ScriptCall('onParameters');
 end;
 
 procedure chCurU_p(var s: shortstring);
--- a/hedgewars/uScript.pas	Mon Jan 13 14:40:06 2014 -0500
+++ b/hedgewars/uScript.pas	Tue Jan 14 12:44:29 2014 +0400
@@ -38,6 +38,7 @@
 procedure ScriptOnGameInit;
 procedure ScriptOnScreenResize;
 procedure ScriptSetInteger(name : shortstring; value : LongInt);
+procedure ScriptSetString(name : shortstring; value : shortstring);
 
 procedure ScriptCall(fname : shortstring);
 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
@@ -2070,7 +2071,6 @@
 ScriptSetString('Map', cMapName);
 ScriptSetString('Theme', '');
 ScriptSetString('Goals', '');
-ScriptSetString('ScriptParam', cScriptParam);
 
 ScriptCall('onGameInit');
 
--- a/share/hedgewars/Data/Scripts/Multiplayer/Gravity.lua	Mon Jan 13 14:40:06 2014 -0500
+++ b/share/hedgewars/Data/Scripts/Multiplayer/Gravity.lua	Tue Jan 14 12:44:29 2014 +0400
@@ -11,12 +11,21 @@
 local wdTTL = 0
 local mln = 1000000
 
-function onNewTurn()
+local script2_onNewTurn
+local script2_onGameTick20
+local script2_onGameStart
+
+
+function grav_onNewTurn()
     SetGravity(gravity)
     wdGameTicks = GameTime
+    
+    if script2_onNewTurn ~= nil then
+        script2_onNewTurn()
+    end    
 end
 
-function onGameTick20()
+function grav_onGameTick20()
     if wdGameTicks + 15000 < GameTime then
         SetGravity(100)
     else
@@ -49,9 +58,13 @@
     end
 
     wdTTL = TurnTimeLeft
+    
+    if script2_onGameTick20 ~= nil then
+        script2_onGameTick20()
+    end    
 end
 
-function onGameInit()
+function onParameters()
     parseParams()
 
     gravity = params["g"]
@@ -86,9 +99,28 @@
     if gravity == nil then
         gravity = 100
     end
+    
+    secondScript = params["script2"]
+    
+    if secondScript ~= nil then
+        onParameters = nil
+        HedgewarsScriptLoad("/Scripts/Multiplayer/" .. secondScript .. ".lua")
+        
+        script2_onNewTurn = onNewTurn
+        script2_onGameTick20 = onGameTick20
+        script2_onGameStart = onGameStart
+                
+        if onParameters ~= nil then
+            onParameters()
+        end
+    end
+    
+    onNewTurn = grav_onNewTurn
+    onGameTick20 = grav_onGameTick20
+    onGameStart = grav_onGameStart
 end
 
-function onGameStart()
+function grav_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
@@ -103,4 +135,10 @@
                 .. 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)
+                
+    if script2_onGameStart ~= nil then
+        script2_onGameStart()
+    end
 end
+
+
--- a/share/hedgewars/Data/Scripts/Params.lua	Mon Jan 13 14:40:06 2014 -0500
+++ b/share/hedgewars/Data/Scripts/Params.lua	Tue Jan 14 12:44:29 2014 +0400
@@ -3,7 +3,9 @@
 params = {}
 
 function parseParams()
-    for k, v in string.gmatch(ScriptParam, "(%w+)=([^,]+)") do
-        params[k] = v
+    if ScriptParam ~= nil then
+        for k, v in string.gmatch(ScriptParam, "(%w+)=([^,]+)") do
+            params[k] = v
+        end
     end
 end