DEADSOFTWARE

don't use tree for drawing by default
[d2df-sdl.git] / src / game / g_map.pas
index 55f1e4aae46348be6ac6d66fdb93f255f39bbb22..b97cc00a24f2a3ed8a6a06dc51b9b10e54b850fd 100644 (file)
@@ -20,7 +20,7 @@ interface
 
 uses
   e_graphics, g_basic, MAPSTRUCT, g_textures, Classes,
-  g_phys, wadreader, BinEditor, g_panel, g_grid, g_sap, md5, xprofiler;
+  g_phys, wadreader, BinEditor, g_panel, g_grid, z_aabbtree, md5, xprofiler;
 
 type
   TMapInfo = record
@@ -136,8 +136,9 @@ var
 
   gdbg_map_use_grid_render: Boolean = true;
   gdbg_map_use_grid_coldet: Boolean = true;
-  gdbg_map_use_sap_draw: Boolean = true;
-  gdbg_map_use_sap_coldet: Boolean = false;
+  gdbg_map_use_tree_draw: Boolean = false;
+  gdbg_map_use_tree_coldet: Boolean = false;
+  gdbg_map_dump_coldet_tree_queries: Boolean = false;
   profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
 
 implementation
@@ -156,16 +157,16 @@ const
   MUSIC_SIGNATURE = $4953554D; // 'MUSI'
   FLAG_SIGNATURE = $47414C46; // 'FLAG'
 
-  GridTagInvalid = -1;
-  GridTagWallDoor = 0;
-  GridTagBack = 1;
-  GridTagFore = 2;
-  GridTagWater = 3;
-  GridTagAcid1 = 4;
-  GridTagAcid2 = 5;
-  GridTagStep = 6;
-  GridTagLift = 7;
-  GridTagBlockMon = 8;
+  GridTagInvalid = 0;
+  GridTagWallDoor = $0001;
+  GridTagBack = $0002;
+  GridTagFore = $0004;
+  GridTagWater = $0008;
+  GridTagAcid1 = $0010;
+  GridTagAcid2 = $0020;
+  GridTagStep = $0040;
+  GridTagLift = $0080;
+  GridTagBlockMon = $0100;
 
 
 function panelTypeToTag (panelType: Word): Integer;
@@ -191,6 +192,27 @@ type
     PArrID: Integer;
   end;
 
+type
+  TDynAABBTreeMap = class(TDynAABBTree)
+    function getFleshAABB (var aabb: AABB2D; flesh: TTreeFlesh): Boolean; override;
+  end;
+
+function TDynAABBTreeMap.getFleshAABB (var aabb: AABB2D; flesh: TTreeFlesh): Boolean;
+var
+  pan: TPanel;
+begin
+  result := false;
+  if (flesh = nil) then exit;
+  pan := (flesh as TPanel);
+  if (pan.Width < 1) or (pan.Height < 1) then exit;
+  aabb := AABB2D.Create(pan.X, pan.Y, pan.X+pan.Width, pan.Y+pan.Height);
+  //if (pan.Width = 1) then aabb.maxX += 1;
+  //if (pan.Height = 1) then aabb.maxY += 1;
+  if not aabb.valid then raise Exception.Create('wutafuuuuuuu?!');
+  //e_WriteLog(Format('getFleshAABB(%d;%d) AABB:(%f,%f)-(%f,%f); valid=%d; volume=%f; x=%d; y=%d; w=%d; h=%d', [pan.tag, pan.ArrIdx, aabb.minX, aabb.minY, aabb.maxX, aabb.maxY, Integer(aabb.valid), aabb.volume, pan.X, pan.Y, pan.Width, pan.Height]), MSG_NOTIFY);
+  result := aabb.valid;
+end;
+
 var
   PanelById:     array of TPanelID;
   Textures:      TLevelTextureArray;
@@ -198,7 +220,7 @@ var
   FlagPoints:    Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
   //DOMFlagPoints: Array of TFlagPoint;
   gMapGrid: TBodyGrid = nil;
-  gMapSAP: TSweepAndPrune = nil;
+  mapTree: TDynAABBTree = nil;
 
 
 procedure g_Map_ProfilersBegin ();
@@ -430,6 +452,7 @@ begin
 
   panels^[len] := TPanel.Create(PanelRec, AddTextures, CurTex, Textures);
   panels^[len].ArrIdx := len;
+  panels^[len].tag := panelTypeToTag(PanelRec.PanelType);
   if sav then
     panels^[len].SaveIt := True;
 
@@ -986,16 +1009,17 @@ var
     tag := panelTypeToTag(tag);
     for idx := High(panels) downto 0 do
     begin
+      panels[idx].tag := tag;
       gMapGrid.insertBody(panels[idx], panels[idx].X, panels[idx].Y, panels[idx].Width, panels[idx].Height, tag);
-      gMapSAP.insertBody(panels[idx], panels[idx].X, panels[idx].Y, panels[idx].Width, panels[idx].Height, tag);
+      mapTree.insertObject(panels[idx], tag, true); // as static object
     end;
   end;
 
 begin
   gMapGrid.Free();
   gMapGrid := nil;
-  gMapSAP.Free();
-  gMapSAP := nil;
+  mapTree.Free();
+  mapTree := nil;
 
   fixMinMax(gWalls);
   fixMinMax(gRenderBackgrounds);
@@ -1014,9 +1038,7 @@ begin
   end;
 
   gMapGrid := TBodyGrid.Create(mapX0, mapY0, mapX1-mapX0+1, mapY1-mapY0+1);
-  gMapSAP := TSweepAndPrune.Create();
-
-  gMapSAP.batchUpdateBegin();
+  mapTree := TDynAABBTreeMap.Create();
 
   addPanelsToGrid(gWalls, PANEL_WALL); // and PANEL_CLOSEDOOR
   addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
@@ -1028,10 +1050,9 @@ begin
   addPanelsToGrid(gLifts, PANEL_LIFTUP); // it doesn't matter which LIFT type is used here
   addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
 
-  gMapSAP.batchUpdateEnd();
-
   gMapGrid.dumpStats();
-  gMapSAP.dumpStats();
+  e_WriteLog(Format('tree depth: %d; %d nodes used, %d nodes allocated', [mapTree.computeTreeHeight, mapTree.nodeCount, mapTree.nodeAlloced]), MSG_NOTIFY);
+  mapTree.forEachLeaf(nil);
 end;
 
 function g_Map_Load(Res: String): Boolean;
@@ -1068,8 +1089,8 @@ var
 begin
   gMapGrid.Free();
   gMapGrid := nil;
-  gMapSAP.Free();
-  gMapSAP := nil;
+  mapTree.Free();
+  mapTree := nil;
 
   Result := False;
   gMapInfo.Map := Res;
@@ -1883,8 +1904,8 @@ var
     pan: TPanel;
   begin
     result := false; // don't stop, ever
+    //e_WriteLog(Format('  *body: tag:%d; ptag:%d; pantype=%d', [tag, ptag, PanelType]), MSG_NOTIFY);
     if (tag <> ptag) then exit;
-    //e_WriteLog(Format('  *body: tag:%d; qtag:%d', [tag, PanelType]), MSG_NOTIFY);
 
     if obj = nil then begin e_WriteLog(Format('  !bodyFUUUUU0: tag:%d; qtag:%d', [tag, PanelType]), MSG_NOTIFY); exit; end;
     if not (obj is TPanel) then begin e_WriteLog(Format('  !bodyFUUUUU1: tag:%d; qtag:%d', [tag, PanelType]), MSG_NOTIFY); exit; end;
@@ -1931,9 +1952,9 @@ begin
 
   if gdbg_map_use_grid_render then
   begin
-    if gdbg_map_use_sap_draw then
+    if gdbg_map_use_tree_draw then
     begin
-      gMapSAP.forEachInAABB(x0, y0, wdt, hgt, checker);
+      mapTree.aabbQuery(x0, y0, wdt, hgt, checker, ptag);
     end
     else
     begin
@@ -1977,9 +1998,9 @@ procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius:
   end;
 
 begin
-  if gdbg_map_use_sap_draw then
+  if gdbg_map_use_tree_draw then
   begin
-    gMapSAP.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker);
+    mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checker, GridTagWallDoor);
   end
   else
   begin
@@ -2246,9 +2267,14 @@ begin
   try
     if gdbg_map_use_grid_coldet then
     begin
-      if gdbg_map_use_sap_coldet then
+      if gdbg_map_use_tree_coldet then
       begin
-        gMapSAP.forEachInAABB(X, Y, Width, Height, checker);
+        result := (mapTree.aabbQuery(X, Y, Width, Height, checker, (GridTagWallDoor or GridTagWater or GridTagAcid1 or GridTagAcid2 or GridTagStep or GridTagLift or GridTagBlockMon)) <> nil);
+        if (gdbg_map_dump_coldet_tree_queries) and (mapTree.nodesVisited <> 0) then
+        begin
+          //e_WriteLog(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]), MSG_NOTIFY);
+          g_Console_Add(Format('map collision: %d nodes visited (%d deep)', [mapTree.nodesVisited, mapTree.nodesDeepVisited]));
+        end;
       end
       else
       begin
@@ -2277,6 +2303,7 @@ var
     a: Integer;
   begin
     result := false; // don't stop, ever
+    if (tag <> GridTagWater) and (tag <> GridTagAcid1) and (tag <> GridTagAcid2) then exit;
     pan := (obj as TPanel);
     a := pan.ArrIdx;
     // water
@@ -2311,6 +2338,9 @@ var
     end;
   end;
 
+{var
+  cctype1: Integer = 3; // priority: 0: water, 1: acid1, 2: acid2; 3: others (nothing)
+  texid1: DWORD;}
 begin
   //TODO: detailed profile?
   if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquid coldet');
@@ -2318,9 +2348,19 @@ begin
     if gdbg_map_use_grid_coldet then
     begin
       texid := TEXTURE_NONE;
-      if gdbg_map_use_sap_coldet then
+      if gdbg_map_use_tree_coldet then
       begin
-        gMapSAP.forEachInAABB(X, Y, Width, Height, checker);
+        mapTree.aabbQuery(X, Y, Width, Height, checker);
+        {
+        cctype1 := cctype;
+        texid1 := texid;
+        cctype := 3;
+        texid := TEXTURE_NONE;
+        gMapGrid.forEachInAABB(X, Y, Width, Height, checker);
+        if (cctype1 <> cctype) or (texid1 <> texid) then
+        begin
+          e_WriteLog(Format('g_Map_CollideLiquid_Texture(%d, %d, %u, %u): tree(cctype:%d;texid:%u); grid(cctype:%d;texid:%u)', [X, Y, Width, Height, cctype1, texid1, cctype, texid]), MSG_WARNING);
+        end;}
       end
       else
       begin