DEADSOFTWARE

map ray tracer now using grid instead of tree
[d2df-sdl.git] / src / game / g_map.pas
index 36ac9072a2fc315286abddb31042cba92eb9e053..d4fbcf1830150dc64250d74e257e790b0635fc5b 100644 (file)
@@ -67,7 +67,7 @@ procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
 
 procedure g_Map_DrawBack(dx, dy: Integer);
 function  g_Map_CollidePanel(X, Y: Integer; Width, Height: Word;
-                             PanelType: Word; b1x3: Boolean): Boolean;
+                             PanelType: Word; b1x3: Boolean=false): Boolean;
 function  g_Map_CollideLiquid_Texture(X, Y: Integer; Width, Height: Word): DWORD;
 procedure g_Map_EnableWall(ID: DWORD);
 procedure g_Map_DisableWall(ID: DWORD);
@@ -91,6 +91,15 @@ procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
 
 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
 
+// returns wall index in `gWalls` or -1
+function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Boolean;
+
+type
+  TForEachPanelCB = function (pan: TPanel): Boolean; // return `true` to stop
+
+function g_Map_ForEachPanelAt (x, y: Integer; cb: TForEachPanelCB; panelType: Word): Boolean;
+
+
 procedure g_Map_ProfilersBegin ();
 procedure g_Map_ProfilersEnd ();
 
@@ -172,8 +181,6 @@ var
 
 function panelTypeToTag (panelType: Word): Integer; // returns GridTagXXX
 
-function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Integer;
-
 
 implementation
 
@@ -285,7 +292,8 @@ end;
 
 
 // wall index in `gWalls` or -1
-function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Integer;
+(*
+function g_Map_traceToNearestWallOld (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Integer;
 
   function sqchecker (pan: TPanel; var ray: Ray2D): Single;
   var
@@ -298,6 +306,7 @@ function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil;
     if not aabb.valid then exit;
     if aabb.intersects(ray, @tmin) then
     begin
+      //if (tmin*tmin > maxDistSq) then exit;
       if (tmin >= 0.0) then
       begin
         //e_WriteLog(Format('sqchecker(%d,%d,%d,%d): panel #%d (%d,%d)-(%d,%d); tmin=%f', [x0, y0, x1, y1, pan.arrIdx, pan.X, pan.Y, pan.Width, pan.Height, tmin]), MSG_NOTIFY);
@@ -312,12 +321,14 @@ var
   ray: Ray2D;
   hxf, hyf: Single;
   hx, hy: Integer;
+  maxDistSq: Single;
 begin
   result := -1;
   if (mapTree = nil) then exit;
   if mapTree.segmentQuery(qr, x0, y0, x1, y1, sqchecker, (GridTagWall or GridTagDoor)) then
   begin
-    if (qr.flesh <> nil) then
+    maxDistSq := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0);
+    if (qr.flesh <> nil) and (qr.time*qr.time <= maxDistSq) then
     begin
       result := qr.flesh.arrIdx;
       if (hitx <> nil) or (hity <> nil) then
@@ -346,6 +357,99 @@ begin
     end;
   end;
 end;
+*)
+
+
+// wall index in `gWalls` or -1
+function g_Map_traceToNearestWall (x0, y0, x1, y1: Integer; hitx: PInteger=nil; hity: PInteger=nil): Boolean;
+var
+  lastX, lastY, lastDist: Integer;
+  wasHit: Boolean = false;
+
+  // pan=nil: before processing new tile
+  function sqchecker (pan: TPanel; tag: Integer; x, y, prevx, prevy: Integer): Boolean;
+  var
+    dist: Integer;
+  begin
+    if (pan = nil) then
+    begin
+      // stop if something was hit at the previous tile
+      result := wasHit;
+    end
+    else
+    begin
+      result := false;
+      if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
+      begin
+        if not pan.Enabled then exit;
+      end;
+      dist := (prevx-x0)*(prevx-x0)+(prevy-y0)*(prevy-y0);
+      if (dist < lastDist) then
+      begin
+        wasHit := true;
+        lastDist := dist;
+        lastX := prevx;
+        lastY := prevy;
+      end;
+    end;
+  end;
+
+begin
+  result := false;
+  if (gMapGrid = nil) then exit;
+  lastDist := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)+1;
+  lastX := 0;
+  lastY := 0;
+  result := gMapGrid.traceRay(x0, y0, x1, y1, sqchecker, (GridTagWall or GridTagDoor));
+  if (hitx <> nil) then hitx^ := lastX;
+  if (hity <> nil) then hity^ := lastY;
+end;
+
+
+function g_Map_ForEachPanelAt (x, y: Integer; cb: TForEachPanelCB; panelType: Word): Boolean;
+
+  function checker (pan: TPanel; tag: Integer): Boolean;
+  begin
+    result := false; // don't stop, ever
+
+    if ((tag and (GridTagWall or GridTagDoor)) <> 0) then
+    begin
+      if not pan.Enabled then exit;
+    end;
+
+    result := (x >= pan.X) and (y >= pan.Y) and (x < pan.X+pan.Width) and (y < pan.Y+pan.Height);
+    if not result then exit;
+
+    if ((tag and GridTagLift) <> 0) then
+    begin
+      result :=
+        ((WordBool(PanelType and PANEL_LIFTUP) and (pan.LiftType = 0)) or
+         (WordBool(PanelType and PANEL_LIFTDOWN) and (pan.LiftType = 1)) or
+         (WordBool(PanelType and PANEL_LIFTLEFT) and (pan.LiftType = 2)) or
+         (WordBool(PanelType and PANEL_LIFTRIGHT) and (pan.LiftType = 3)));
+    end;
+
+    // other shit
+    if result then result := cb(pan);
+  end;
+
+var
+  tagmask: Integer = 0;
+begin
+  result := false;
+  if not assigned(cb) then exit;
+
+  if WordBool(PanelType and (PANEL_WALL or PANEL_CLOSEDOOR or PANEL_OPENDOOR)) then tagmask := tagmask or (GridTagWall or GridTagDoor);
+  if WordBool(PanelType and PANEL_WATER) then tagmask := tagmask or GridTagWater;
+  if WordBool(PanelType and PANEL_ACID1) then tagmask := tagmask or GridTagAcid1;
+  if WordBool(PanelType and PANEL_ACID2) then tagmask := tagmask or GridTagAcid2;
+  if WordBool(PanelType and PANEL_STEP) then tagmask := tagmask or GridTagStep;
+  if WordBool(PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT)) then tagmask := tagmask or GridTagLift;
+  if WordBool(PanelType and PANEL_BLOCKMON) then tagmask := tagmask or GridTagBlockMon;
+
+  if (tagmask = 0) then exit;// just in case
+  result := gMapGrid.forEachInAABB(x, y, 1, 1, checker, tagmask);
+end;
 
 
 function g_Map_IsSpecialTexture(Texture: String): Boolean;
@@ -2045,7 +2149,7 @@ begin
 end;
 
 function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word;
-                            PanelType: Word; b1x3: Boolean): Boolean;
+                            PanelType: Word; b1x3: Boolean=false): Boolean;
 var
   a, h: Integer;
 begin
@@ -2320,7 +2424,7 @@ begin
   with gWalls[ID] do
   begin
     Enabled := True;
-    g_Mark(X, Y, Width, Height, MARK_DOOR, True);
+    //g_Mark(X, Y, Width, Height, MARK_DOOR, True);
 
     if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
   end;
@@ -2331,7 +2435,7 @@ begin
   with gWalls[ID] do
   begin
     Enabled := False;
-    g_Mark(X, Y, Width, Height, MARK_DOOR, False);
+    //g_Mark(X, Y, Width, Height, MARK_DOOR, False);
 
     if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
   end;
@@ -2374,6 +2478,7 @@ begin
   begin
     LiftType := t;
 
+    {
     g_Mark(X, Y, Width, Height, MARK_LIFT, False);
 
     if LiftType = 0 then
@@ -2384,6 +2489,7 @@ begin
       g_Mark(X, Y, Width, Height, MARK_LIFTLEFT, True)
     else if LiftType = 3 then
       g_Mark(X, Y, Width, Height, MARK_LIFTRIGHT, True);
+    }
 
     if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID);
   end;