DEADSOFTWARE

Sweep-And-Prune broad phase implementation; not working yet
[d2df-sdl.git] / src / game / g_map.pas
index 42831447a301bf9409e28a2eb38bd66590b9f4a5..c9851ee57bb6ec63e8822a8aeae444c19f4442eb 100644 (file)
@@ -20,7 +20,7 @@ interface
 
 uses
   e_graphics, g_basic, MAPSTRUCT, g_textures, Classes,
-  g_phys, wadreader, BinEditor, g_panel, g_grid, md5;
+  g_phys, wadreader, BinEditor, g_panel, g_grid, g_sap, md5, xprofiler;
 
 type
   TMapInfo = record
@@ -90,6 +90,9 @@ procedure g_Map_LoadState(Var Mem: TBinMemoryReader);
 
 procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer);
 
+procedure g_Map_ProfilersBegin ();
+procedure g_Map_ProfilersEnd ();
+
 const
   RESPAWNPOINT_PLAYER1 = 1;
   RESPAWNPOINT_PLAYER2 = 2;
@@ -133,6 +136,8 @@ var
 
   gdbg_map_use_grid_render: Boolean = true;
   gdbg_map_use_grid_coldet: Boolean = true;
+  gdbg_map_use_sap: Boolean = false;
+  profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY!
 
 implementation
 
@@ -141,7 +146,7 @@ uses
   GL, GLExt, g_weapons, g_game, g_sound, e_sound, CONFIG,
   g_options, MAPREADER, g_triggers, g_player, MAPDEF,
   Math, g_monsters, g_saveload, g_language, g_netmsg,
-  utils, sfs,
+  utils, sfs, binheap,
   ImagingTypes, Imaging, ImagingUtility,
   ImagingGif, ImagingNetworkGraphics;
 
@@ -192,6 +197,27 @@ var
   FlagPoints:    Array [FLAG_RED..FLAG_BLUE] of PFlagPoint;
   //DOMFlagPoints: Array of TFlagPoint;
   gMapGrid: TBodyGrid = nil;
+  gMapSAP: TSweepAndPrune = nil;
+
+
+procedure g_Map_ProfilersBegin ();
+begin
+  if (profMapCollision = nil) then profMapCollision := TProfiler.Create('MAP COLLISION', g_profile_history_size);
+  profMapCollision.mainBegin(g_profile_collision);
+  // create sections
+  if g_profile_collision then
+  begin
+    profMapCollision.sectionBegin('wall coldet');
+    profMapCollision.sectionEnd();
+    profMapCollision.sectionBegin('liquid coldet');
+    profMapCollision.sectionEnd();
+  end;
+end;
+
+procedure g_Map_ProfilersEnd ();
+begin
+  if (profMapCollision <> nil) then profMapCollision.mainEnd();
+end;
 
 
 function g_Map_IsSpecialTexture(Texture: String): Boolean;
@@ -960,12 +986,15 @@ var
     for idx := High(panels) downto 0 do
     begin
       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);
     end;
   end;
 
 begin
   gMapGrid.Free();
   gMapGrid := nil;
+  gMapSAP.Free();
+  gMapSAP := nil;
 
   fixMinMax(gWalls);
   fixMinMax(gRenderBackgrounds);
@@ -984,6 +1013,7 @@ begin
   end;
 
   gMapGrid := TBodyGrid.Create(mapX0, mapY0, mapX1-mapX0+1, mapY1-mapY0+1);
+  gMapSAP := TSweepAndPrune.Create();
 
   addPanelsToGrid(gWalls, PANEL_WALL); // and PANEL_CLOSEDOOR
   addPanelsToGrid(gRenderBackgrounds, PANEL_BACK);
@@ -996,6 +1026,7 @@ begin
   addPanelsToGrid(gBlockMon, PANEL_BLOCKMON);
 
   gMapGrid.dumpStats();
+  gMapSAP.dumpStats();
 end;
 
 function g_Map_Load(Res: String): Boolean;
@@ -1032,6 +1063,8 @@ var
 begin
   gMapGrid.Free();
   gMapGrid := nil;
+  gMapSAP.Free();
+  gMapSAP := nil;
 
   Result := False;
   gMapInfo.Map := Res;
@@ -1893,7 +1926,14 @@ begin
 
   if gdbg_map_use_grid_render then
   begin
-    gMapGrid.forEachInAABB(x0, y0, wdt, hgt, checker);
+    if gdbg_map_use_sap then
+    begin
+      gMapSAP.forEachInAABB(x0, y0, wdt, hgt, checker);
+    end
+    else
+    begin
+      gMapGrid.forEachInAABB(x0, y0, wdt, hgt, checker);
+    end;
     // sort and draw the list (we need to sort it, or rendering is fucked)
     while gDrawPanelList.count > 0 do
     begin
@@ -1932,7 +1972,14 @@ procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius:
   end;
 
 begin
-  gMapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker);
+  if gdbg_map_use_sap then
+  begin
+    gMapSAP.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker);
+  end
+  else
+  begin
+    gMapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker);
+  end;
 end;
 
 
@@ -2189,13 +2236,26 @@ function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word;
   end;
 
 begin
-  if gdbg_map_use_grid_coldet then
-  begin
-    result := gMapGrid.forEachInAABB(X, Y, Width, Height, checker);
-  end
-  else
-  begin
-    result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
+  //TODO: detailed profile
+  if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('wall coldet');
+  try
+    if gdbg_map_use_grid_coldet then
+    begin
+      if gdbg_map_use_sap then
+      begin
+        gMapSAP.forEachInAABB(X, Y, Width, Height, checker);
+      end
+      else
+      begin
+        result := gMapGrid.forEachInAABB(X, Y, Width, Height, checker);
+      end;
+    end
+    else
+    begin
+      result := g_Map_CollidePanelOld(X, Y, Width, Height, PanelType, b1x3);
+    end;
+  finally
+    if (profMapCollision <> nil) then profMapCollision.sectionEnd();
   end;
 end;
 
@@ -2247,15 +2307,28 @@ var
   end;
 
 begin
-  if not gdbg_map_use_grid_coldet then
-  begin
-    result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
-  end
-  else
-  begin
-    texid := TEXTURE_NONE;
-    gMapGrid.forEachInAABB(X, Y, Width, Height, checker);
-    result := texid;
+  //TODO: detailed profile?
+  if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('liquid coldet');
+  try
+    if gdbg_map_use_grid_coldet then
+    begin
+      texid := TEXTURE_NONE;
+      if gdbg_map_use_sap then
+      begin
+        gMapSAP.forEachInAABB(X, Y, Width, Height, checker);
+      end
+      else
+      begin
+        gMapGrid.forEachInAABB(X, Y, Width, Height, checker);
+      end;
+      result := texid;
+    end
+    else
+    begin
+      result := g_Map_CollideLiquid_TextureOld(X, Y, Width, Height);
+    end;
+  finally
+    if (profMapCollision <> nil) then profMapCollision.sectionEnd();
   end;
 end;