X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fgame%2Fg_map.pas;h=11a7d79459680c945d174ca81c4bb45c4618d279;hb=fe14f41dc8700dee5a80949a41c58e7d3943f464;hp=54d3763290be72f4d486f905a6d08265f5f18af1;hpb=dd0d8ac4cc2a0aa774f25c8a1a774f7358acfae7;p=d2df-sdl.git diff --git a/src/game/g_map.pas b/src/game/g_map.pas index 54d3763..11a7d79 100644 --- a/src/game/g_map.pas +++ b/src/game/g_map.pas @@ -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, z_aabbtree, 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; @@ -131,6 +134,12 @@ var BackID: DWORD = DWORD(-1); gExternalResources: TStringList; + gdbg_map_use_grid_render: Boolean = true; + gdbg_map_use_grid_coldet: Boolean = true; + gdbg_map_use_tree_draw: Boolean = true; + gdbg_map_use_tree_coldet: Boolean = false; + profMapCollision: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY! + implementation uses @@ -138,7 +147,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; @@ -182,6 +191,20 @@ 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 + pan := (flesh as TPanel); + aabb.setDims(pan.X, pan.Y, pan.X+pan.Width-1, pan.Y+pan.Height-1); + result := true; +end; + var PanelById: array of TPanelID; Textures: TLevelTextureArray; @@ -189,6 +212,27 @@ var FlagPoints: Array [FLAG_RED..FLAG_BLUE] of PFlagPoint; //DOMFlagPoints: Array of TFlagPoint; gMapGrid: TBodyGrid = nil; + mapTree: TDynAABBTree = 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; @@ -400,6 +444,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; @@ -956,13 +1001,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); + mapTree.insertObject(panels[idx], true); // as static object end; end; begin gMapGrid.Free(); gMapGrid := nil; + mapTree.Free(); + mapTree := nil; fixMinMax(gWalls); fixMinMax(gRenderBackgrounds); @@ -981,6 +1030,7 @@ begin end; gMapGrid := TBodyGrid.Create(mapX0, mapY0, mapX1-mapX0+1, mapY1-mapY0+1); + mapTree := TDynAABBTreeMap.Create(); addPanelsToGrid(gWalls, PANEL_WALL); // and PANEL_CLOSEDOOR addPanelsToGrid(gRenderBackgrounds, PANEL_BACK); @@ -993,6 +1043,8 @@ begin addPanelsToGrid(gBlockMon, PANEL_BLOCKMON); gMapGrid.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; @@ -1029,6 +1081,8 @@ var begin gMapGrid.Free(); gMapGrid := nil; + mapTree.Free(); + mapTree := nil; Result := False; gMapInfo.Map := Res; @@ -1856,6 +1910,18 @@ var dplAddPanel(pan); end; + function checkerTree (obj: TObject): Boolean; + var + pan: TPanel; + begin + result := false; // don't stop, ever + pan := (obj as TPanel); + if (pan.tag <> ptag) then exit; + if (PanelType = PANEL_CLOSEDOOR) then begin if not pan.Door then exit; end else begin if pan.Door then exit; end; + //e_WriteLog(Format(' body hit: (%d,%d)-(%dx%d) tag: %d; qtag:%d', [pan.X, pan.Y, pan.Width, pan.Height, tag, PanelType]), MSG_NOTIFY); + dplAddPanel(pan); + end; + procedure DrawPanels (stp: Integer; var panels: TPanelArray; drawDoors: Boolean=False); var idx: Integer; @@ -1869,10 +1935,13 @@ var if not (drawDoors xor panels[idx].Door) then begin pan := panels[idx]; + { if (pan.Width < 1) or (pan.Height < 1) then continue; if (pan.X+pan.Width <= x0) or (pan.Y+pan.Height <= y0) then continue; if (pan.X >= x0+wdt) or (pan.Y >= y0+hgt) then continue; - e_WriteLog(Format(' *body hit: (%d,%d)-(%dx%d) tag: %d; qtag:%d', [pan.X, pan.Y, pan.Width, pan.Height, PanelType, PanelType]), MSG_NOTIFY); + } + pan.Draw(); + //e_WriteLog(Format(' *body hit: (%d,%d)-(%dx%d) tag: %d; qtag:%d', [pan.X, pan.Y, pan.Width, pan.Height, PanelType, PanelType]), MSG_NOTIFY); end; end; end; @@ -1884,30 +1953,40 @@ begin //e_WriteLog(Format('***QQQ: qtag:%d', [PanelType]), MSG_NOTIFY); dplClear(); ptag := panelTypeToTag(PanelType); - gMapGrid.forEachInAABB(x0, y0, wdt, hgt, checker); - - // debug - { - e_WriteLog(Format('+++QQQ: qtag:%d', [PanelType]), MSG_NOTIFY); - case PanelType of - PANEL_WALL: DrawPanels(0, gWalls); - PANEL_CLOSEDOOR: DrawPanels(0, gWalls, True); - PANEL_BACK: DrawPanels(1, gRenderBackgrounds); - PANEL_FORE: DrawPanels(2, gRenderForegrounds); - PANEL_WATER: DrawPanels(3, gWater); - PANEL_ACID1: DrawPanels(4, gAcid1); - PANEL_ACID2: DrawPanels(5, gAcid2); - PANEL_STEP: DrawPanels(6, gSteps); - end; - e_WriteLog('==================', MSG_NOTIFY); - } - // sort and draw the list (we need to sort it, or rendering is fucked) - while gDrawPanelList.count > 0 do + if gdbg_map_use_grid_render then + begin + if gdbg_map_use_tree_draw then + begin + mapTree.aabbQuery(x0, y0, wdt, hgt, checkerTree); + 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 + (gDrawPanelList.front() as TPanel).Draw(); + gDrawPanelList.popFront(); + end; + end + else begin - (gDrawPanelList.front() as TPanel).Draw(); - gDrawPanelList.popFront(); + //e_WriteLog(Format('+++QQQ: qtag:%d', [PanelType]), MSG_NOTIFY); + case PanelType of + PANEL_WALL: DrawPanels(0, gWalls); + PANEL_CLOSEDOOR: DrawPanels(0, gWalls, True); + PANEL_BACK: DrawPanels(1, gRenderBackgrounds); + PANEL_FORE: DrawPanels(2, gRenderForegrounds); + PANEL_WATER: DrawPanels(3, gWater); + PANEL_ACID1: DrawPanels(4, gAcid1); + PANEL_ACID2: DrawPanels(5, gAcid2); + PANEL_STEP: DrawPanels(6, gSteps); + end; end; + + //e_WriteLog('==================', MSG_NOTIFY); end; @@ -1922,8 +2001,25 @@ procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: pan.DrawShadowVolume(lightX, lightY, radius); end; + function checkerTree (obj: TObject): Boolean; + var + pan: TPanel; + begin + result := false; // don't stop, ever + pan := (obj as TPanel); + if (pan.tag <> GridTagWallDoor) then exit; // only walls + pan.DrawShadowVolume(lightX, lightY, radius); + end; + begin - gMapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker); + if gdbg_map_use_tree_draw then + begin + mapTree.aabbQuery(lightX-radius, lightY-radius, radius*2, radius*2, checkerTree); + end + else + begin + gMapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker); + end; end; @@ -1935,7 +2031,6 @@ begin e_Clear(GL_COLOR_BUFFER_BIT, 0, 0, 0); end; -(* function g_Map_CollidePanelOld(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean; var @@ -2055,7 +2150,37 @@ begin end; end; end; -*) + +function g_Map_CollideLiquid_TextureOld(X, Y: Integer; Width, Height: Word): DWORD; +var + texid: DWORD; + + function checkPanels (var panels: TPanelArray): Boolean; + var + a: Integer; + begin + result := false; + if panels = nil then exit; + for a := 0 to High(panels) do + begin + if g_Collide(X, Y, Width, Height, panels[a].X, panels[a].Y, panels[a].Width, panels[a].Height) then + begin + result := true; + texid := panels[a].GetTextureID(); + exit; + end; + end; + end; + +begin + texid := TEXTURE_NONE; + result := texid; + if not checkPanels(gWater) then + if not checkPanels(gAcid1) then + if not checkPanels(gAcid2) then exit; + result := texid; +end; + function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean; @@ -2139,7 +2264,7 @@ function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; end; end; - if WordBool(PanelType and PANEL_BLOCKMON)and (tag = GridTagBlockMon) then + if WordBool(PanelType and PANEL_BLOCKMON) and (tag = GridTagBlockMon) then begin if ((not b1x3) or ((gBlockMon[a].Width + gBlockMon[a].Height) >= 64)) and g_Collide(X, Y, Width, Height, gBlockMon[a].X, gBlockMon[a].Y, gBlockMon[a].Width, gBlockMon[a].Height) then @@ -2150,8 +2275,36 @@ function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; end; end; + function checkerTree (obj: TObject): Boolean; + var + pan: TPanel; + begin + pan := (obj as TPanel); + result := checker(obj, pan.tag); + end; + begin - result := gMapGrid.forEachInAABB(X, Y, Width, Height, checker); + //TODO: detailed profile + if (profMapCollision <> nil) then profMapCollision.sectionBeginAccum('wall coldet'); + try + if gdbg_map_use_grid_coldet then + begin + if gdbg_map_use_tree_coldet then + begin + mapTree.aabbQuery(X, Y, Width, Height, checkerTree); + 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; @@ -2201,10 +2354,38 @@ var end; end; + function checkerTree (obj: TObject): Boolean; + var + pan: TPanel; + begin + pan := (obj as TPanel); + result := checker(obj, pan.tag); + end; + 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_tree_coldet then + begin + mapTree.aabbQuery(X, Y, Width, Height, checkerTree); + 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; procedure g_Map_EnableWall(ID: DWORD);