move functionality of Draw.lua into engine
authorsheepluva
Tue, 02 Dec 2014 23:33:28 +0100
changeset 10611 58cad46782ff
parent 10609 15f2908113a1
child 10613 f2597a21f562
move functionality of Draw.lua into engine
hedgewars/uScript.pas
share/hedgewars/Data/Scripts/Draw.lua
share/hedgewars/Data/Scripts/Multiplayer/DiagonalMaze.lua
share/hedgewars/Data/Scripts/Multiplayer/ShoppaMap.lua
share/hedgewars/Data/Scripts/Multiplayer/TechRacer.lua
share/hedgewars/Data/Scripts/Multiplayer/Tunnels.lua
tests/lua/drillrockets_boom.lua
tests/lua/drillrockets_drill.lua
tests/lua/hellfire_burns.lua
tests/lua/twothousandmines.lua
--- a/hedgewars/uScript.pas	Tue Dec 02 22:11:22 2014 +0100
+++ b/hedgewars/uScript.pas	Tue Dec 02 23:33:28 2014 +0100
@@ -100,6 +100,7 @@
     ScriptAmmoReinforcement : shortstring;
     ScriptLoaded : boolean;
     mapDims : boolean;
+    PointsBuffer: shortString;
 
 procedure ScriptPrepareAmmoStore; forward;
 procedure ScriptApplyAmmoStore; forward;
@@ -190,6 +191,19 @@
     CheckAndFetchParamCount:= true;
 end;
 
+// check if is in range of count1 and count2
+function CheckAndFetchParamCountRange(L : Plua_State; count1, count2: LongInt; call, paramsyntax: shortstring; out actual: LongInt): boolean; inline;
+begin
+    actual:= lua_gettop(L);
+    if (actual < count1) or (actual > count2) then
+        begin
+        LuaParameterCountError('at least ' + inttostr(count1) + ', but at most ' + inttostr(count2), call, paramsyntax, actual);
+        exit(false);
+        end;
+
+    CheckAndFetchParamCountRange:= true;
+end;
+
 // check if is same or higher as minCount
 function CheckAndFetchLuaParamMinCount(L : Plua_State; minCount: LongInt; call, paramsyntax: shortstring; out actual: LongInt): boolean; inline;
 begin
@@ -2252,6 +2266,60 @@
     lc_declareachievement:= 0
 end;
 
+
+procedure ScriptFlushPoints();
+begin
+    ParseCommand('draw ' + PointsBuffer, true, true);
+    PointsBuffer:= '';
+end;
+
+
+function lc_addPoint(L : Plua_State) : LongInt; Cdecl;
+var np, param: integer;
+begin
+    if CheckAndFetchParamCountRange(L, 2, 4, 'AddPoint', 'x, y, width [, erase]', np) then
+        begin
+        // x
+        param:= lua_tointeger(L,1);
+        PointsBuffer:= PointsBuffer + char((param and $FF00) shr 8);
+        PointsBuffer:= PointsBuffer + char((param and $FF));
+        // y
+        param:= lua_tointeger(L,2);
+        PointsBuffer:= PointsBuffer + char((param and $FF00) shr 8);
+        PointsBuffer:= PointsBuffer + char((param and $FF));
+        // width
+        if np > 2 then
+            param:= lua_tointeger(L,3)
+        else
+            param:= 0;
+
+        if param <> 0 then
+            begin
+            param:= (param or $80);
+            // erase
+            if (np > 3) and lua_toboolean(L, 4) then
+                param:= (param or $40);
+            PointsBuffer:= PointsBuffer + char(param);
+            end
+        // width is 0
+        else
+            PointsBuffer:= PointsBuffer + char(0);
+
+        // flush before shortstring limit length is reached
+        if length(PointsBuffer) > 245 then
+            ScriptFlushPoints();
+        end;
+    lc_addPoint:= 0
+end;
+
+
+function lc_flushPoints(L : Plua_State) : LongInt; Cdecl;
+begin
+    if CheckLuaParamCount(L, 0, 'FlushPoints', '') then
+        ScriptFlushPoints();
+    lc_flushPoints:= 0
+end;
+
 // stuff for testing the lua API
 function lc_endluatest(L : Plua_State) : LongInt; Cdecl;
 begin
@@ -2947,6 +3015,9 @@
 lua_register(luaState, _P'SetWaterLine', @lc_setwaterline);
 lua_register(luaState, _P'SetNextWeapon', @lc_setnextweapon);
 lua_register(luaState, _P'SetWeapon', @lc_setweapon);
+// drawn map functions
+lua_register(luaState, _P'AddPoint', @lc_addPoint);
+lua_register(luaState, _P'FlushPoints', @lc_flushPoints);
 
 lua_register(luaState, _P'SetGearAIHints', @lc_setaihintsongear);
 lua_register(luaState, _P'HedgewarsScriptLoad', @lc_hedgewarsscriptload);
@@ -3051,6 +3122,7 @@
 procedure initModule;
 begin
 mapDims:= false;
+PointsBuffer:= '';
 end;
 
 procedure freeModule;
--- a/share/hedgewars/Data/Scripts/Draw.lua	Tue Dec 02 22:11:22 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,25 +0,0 @@
-local PointsBuffer = ''  -- A string to accumulate points in
-
-function AddPoint(x, y, width, erase)
- PointsBuffer = PointsBuffer .. string.char(band(x,0xff00) / 256 , band(x,0xff) , band(y,0xff00) / 256 , band(y,0xff))
- if width then
-     width = bor(width,0x80)
-     if erase then
-         width = bor(width,0x40)
-     end
-     PointsBuffer = PointsBuffer .. string.char(width)
- else
-     PointsBuffer = PointsBuffer .. string.char(0)
- end
- if #PointsBuffer > 245 then
-     ParseCommand('draw '..PointsBuffer)
-     PointsBuffer = ''
- end
-end
-
-function FlushPoints()
- if #PointsBuffer > 0 then
-     ParseCommand('draw '..PointsBuffer)
-     PointsBuffer = ''
- end
-end
--- a/share/hedgewars/Data/Scripts/Multiplayer/DiagonalMaze.lua	Tue Dec 02 22:11:22 2014 +0100
+++ b/share/hedgewars/Data/Scripts/Multiplayer/DiagonalMaze.lua	Tue Dec 02 23:33:28 2014 +0100
@@ -1,4 +1,3 @@
-HedgewarsScriptLoad("/Scripts/Draw.lua")
 
 function onPreviewInit()
 onGameInit()
--- a/share/hedgewars/Data/Scripts/Multiplayer/ShoppaMap.lua	Tue Dec 02 22:11:22 2014 +0100
+++ b/share/hedgewars/Data/Scripts/Multiplayer/ShoppaMap.lua	Tue Dec 02 23:33:28 2014 +0100
@@ -1,7 +1,5 @@
 ObjectList = {}
 
-HedgewarsScriptLoad("/Scripts/Draw.lua")
-
 -- Overall padding for roping freedom
 Padding = 430
 
--- a/share/hedgewars/Data/Scripts/Multiplayer/TechRacer.lua	Tue Dec 02 22:11:22 2014 +0100
+++ b/share/hedgewars/Data/Scripts/Multiplayer/TechRacer.lua	Tue Dec 02 23:33:28 2014 +0100
@@ -1103,32 +1103,6 @@
 	mapID = params["m"]
 end
 
-PointsBuffer = ''  -- A string to accumulate points in
-
-function AddPoint(x, y, width, erase)
- PointsBuffer = PointsBuffer .. string.char(band(x,0xff00) / 256 , band(x,0xff) , band(y,0xff00) / 256 , band(y,0xff))
- if width then
-     width = bor(width,0x80)
-     if erase then
-         width = bor(width,0x40)
-     end
-     PointsBuffer = PointsBuffer .. string.char(width)
- else
-     PointsBuffer = PointsBuffer .. string.char(0)
- end
- if #PointsBuffer > 245 then
-     ParseCommand('draw '..PointsBuffer)
-     PointsBuffer = ''
- end
-end
-
-function FlushPoints()
- if #PointsBuffer > 0 then
-     ParseCommand('draw '..PointsBuffer)
-     PointsBuffer = ''
- end
-end
-
 function onPreviewInit()
 	onGameInit()
 end
--- a/share/hedgewars/Data/Scripts/Multiplayer/Tunnels.lua	Tue Dec 02 22:11:22 2014 +0100
+++ b/share/hedgewars/Data/Scripts/Multiplayer/Tunnels.lua	Tue Dec 02 23:33:28 2014 +0100
@@ -1,4 +1,3 @@
-HedgewarsScriptLoad("/Scripts/Draw.lua")
 
 function onPreviewInit()
 onGameInit()
--- a/tests/lua/drillrockets_boom.lua	Tue Dec 02 22:11:22 2014 +0100
+++ b/tests/lua/drillrockets_boom.lua	Tue Dec 02 23:33:28 2014 +0100
@@ -1,5 +1,3 @@
-
-HedgewarsScriptLoad("/Scripts/Draw.lua")
 
 local ta_pointsize = 63
 local ta_radius = (ta_pointsize * 10 + 6) / 2
--- a/tests/lua/drillrockets_drill.lua	Tue Dec 02 22:11:22 2014 +0100
+++ b/tests/lua/drillrockets_drill.lua	Tue Dec 02 23:33:28 2014 +0100
@@ -1,5 +1,3 @@
-
-HedgewarsScriptLoad("/Scripts/Draw.lua")
 
 local ta_pointsize = 63
 local ta_radius = (ta_pointsize * 10 + 6) / 2
--- a/tests/lua/hellfire_burns.lua	Tue Dec 02 22:11:22 2014 +0100
+++ b/tests/lua/hellfire_burns.lua	Tue Dec 02 23:33:28 2014 +0100
@@ -1,5 +1,3 @@
-
-HedgewarsScriptLoad("/Scripts/Draw.lua")
 
 local ta_pointsize = 63
 local ta_radius = (ta_pointsize * 10 + 6) / 2
--- a/tests/lua/twothousandmines.lua	Tue Dec 02 22:11:22 2014 +0100
+++ b/tests/lua/twothousandmines.lua	Tue Dec 02 23:33:28 2014 +0100
@@ -1,5 +1,3 @@
-
-HedgewarsScriptLoad("/Scripts/Draw.lua")
 
 local ta_pointsize = 63
 local ta_radius = (ta_pointsize * 10 + 6) / 2