lua: boolean TestRectForObstacle(x1, y1, x2, y2, landOnly)
authorsheepluva
Wed, 14 Sep 2011 17:17:50 +0200
changeset 5896 9ce1cf4e5a32
parent 5895 212d3b459658
child 5898 68038d8d6d29
child 5899 0c3db82e9f4d
lua: boolean TestRectForObstacle(x1, y1, x2, y2, landOnly)
hedgewars/GSHandlers.inc
hedgewars/uCollisions.pas
hedgewars/uScript.pas
--- a/hedgewars/GSHandlers.inc	Tue Sep 13 21:25:13 2011 -0400
+++ b/hedgewars/GSHandlers.inc	Wed Sep 14 17:17:50 2011 +0200
@@ -4209,7 +4209,7 @@
     begin
         Gear^.State := Gear^.State or gstCollision;
         Gear^.State := Gear^.State and not gstMoving;
-        if not calcSlopeTangent(Gear, x, y, tx, ty, 255)
+        if not CalcSlopeTangent(Gear, x, y, tx, ty, 255)
            or (DistanceI(tx,ty) < _12) then // reject shots at too irregular terrain
         begin
             loadNewPortalBall(Gear, true);
--- a/hedgewars/uCollisions.pas	Tue Sep 13 21:25:13 2011 -0400
+++ b/hedgewars/uCollisions.pas	Wed Sep 14 17:17:50 2011 +0200
@@ -50,7 +50,8 @@
 function  TestCollisionXwithXYShift(Gear: PGear; ShiftX: hwFloat; ShiftY: LongInt; Dir: LongInt; withGear: boolean = true): boolean;
 function  TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt; withGear: boolean = true): boolean;
 
-function  calcSlopeTangent(Gear: PGear; collisionX, collisionY: LongInt; var outDeltaX, outDeltaY: LongInt; TestWord: LongWord): Boolean;
+function  TestRectancleForObstacle(x1, y1, x2, y2: LongInt; landOnly: boolean): boolean;
+function  CalcSlopeTangent(Gear: PGear; collisionX, collisionY: LongInt; var outDeltaX, outDeltaY: LongInt; TestWord: LongWord): Boolean;
 
 implementation
 uses uConsts, uLandGraphics, uVariables, uDebug, uGears;
@@ -349,8 +350,39 @@
 Gear^.Y:= Gear^.Y - int2hwFloat(ShiftY)
 end;
 
+function TestRectancleForObstacle(x1, y1, x2, y2: LongInt; landOnly: boolean): boolean;
+var x, y: LongInt;
+    TestWord: LongWord;
+begin
+if landOnly then
+    TestWord:= 255
+else
+    TestWord:= 0;
 
-function calcSlopeTangent(Gear: PGear; collisionX, collisionY: LongInt; var outDeltaX, outDeltaY: LongInt; TestWord: LongWord): boolean;
+if x1 > x2 then
+begin
+    x  := x1;
+    x1 := x2;
+    x2 := x;
+end;
+
+if y1 > y2 then
+begin
+    y  := y1;
+    y1 := y2;
+    y2 := y;
+end;
+
+for y := y1 to y2 do
+    for x := x1 to x2 do
+        if ((y and LAND_HEIGHT_MASK) = 0) and ((x and LAND_WIDTH_MASK) = 0)
+          and (Land[y, x] > TestWord) then
+            exit(true);
+
+TestRectancleForObstacle:= false
+end;
+
+function CalcSlopeTangent(Gear: PGear; collisionX, collisionY: LongInt; var outDeltaX, outDeltaY: LongInt; TestWord: LongWord): boolean;
 var ldx, ldy, rdx, rdy: LongInt;
     i, j, mx, my, li, ri, jfr, jto, tmpo : ShortInt;
     tmpx, tmpy: LongWord;
--- a/hedgewars/uScript.pas	Tue Sep 13 21:25:13 2011 -0400
+++ b/hedgewars/uScript.pas	Wed Sep 14 17:17:50 2011 +0200
@@ -1503,6 +1503,29 @@
         lua_pushinteger(L, ord(CurrentHedgehog^.CurAmmoType));
     lc_getcurammotype := 1;
 end;
+
+// boolean TestRectForObstacle(x1, y1, x2, y2, landOnly)
+function lc_testrectforobstacle(L : Plua_State) : LongInt; Cdecl;
+var rtn: Boolean;
+begin
+    if lua_gettop(L) <> 5 then
+        begin
+        LuaError('Lua: Wrong number of parameters passed to TestRectForObstacle!');
+        lua_pushnil(L); // return value on stack (nil)
+        end
+    else
+        begin
+        rtn:= TestRectancleForObstacle(
+                    lua_tointeger(L, 1),
+                    lua_tointeger(L, 2),
+                    lua_tointeger(L, 3),
+                    lua_tointeger(L, 4),
+                    lua_toboolean(L, 5)
+                    );
+        lua_pushboolean(L, rtn);
+        end;
+    lc_testrectforobstacle:= 1
+end;
 ///////////////////
 
 procedure ScriptPrintStack;
@@ -1983,6 +2006,7 @@
 lua_register(luaState, 'SetHogHat', @lc_sethoghat);
 lua_register(luaState, 'PlaceGirder', @lc_placegirder);
 lua_register(luaState, 'GetCurAmmoType', @lc_getcurammotype);
+lua_register(luaState, 'TestRectForObstacle', @lc_testrectforobstacle);
 
 
 ScriptClearStack; // just to be sure stack is empty