From faef8ad22cbb1b4375647136c9ddca6705a8e21f Mon Sep 17 00:00:00 2001 From: Ketmar Dark Date: Tue, 22 Aug 2017 18:03:00 +0300 Subject: [PATCH] more code for tracing and other shit; NOTHING IS WORKING YET --- src/game/g_basic.pas | 19 +-- src/game/g_console.pas | 1 + src/game/g_game.pas | 14 +-- src/game/g_grid.pas | 263 +++++++++++++++++++++++++--------------- src/game/g_map.pas | 83 ++++++++----- src/game/g_monsters.pas | 41 ++++++- src/game/g_window.pas | 8 +- 7 files changed, 276 insertions(+), 153 deletions(-) diff --git a/src/game/g_basic.pas b/src/game/g_basic.pas index e3655bb..2300b13 100644 --- a/src/game/g_basic.pas +++ b/src/game/g_basic.pas @@ -95,8 +95,8 @@ var implementation uses - Math, g_map, g_gfx, g_player, SysUtils, MAPDEF, - StrUtils, e_graphics, g_monsters, g_items; + Math, e_log, g_map, g_gfx, g_player, SysUtils, MAPDEF, + StrUtils, e_graphics, g_monsters, g_items, g_game; function g_PatchLength(X1, Y1, X2, Y2: Integer): Word; begin @@ -205,17 +205,10 @@ begin Result := True; *) - result := false; - if g_Map_traceToNearestWall(x1, y1, x2, y2, @wallHitX, @wallHitY) then - begin - // check distance - //result := ((wallHitX-x1)*(wallHitX-x1)+(wallHitY-y1)*(wallHitY-y1) > (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); - result := false; - end - else - begin - result := true; // no obstacles - end; + // `true` if no obstacles + if (g_profile_los) then g_Mons_LOS_Start(); + result := not g_Map_traceToNearestWall(x1, y1, x2, y2, @wallHitX, @wallHitY); + if (g_profile_los) then g_Mons_LOS_End(); end; diff --git a/src/game/g_console.pas b/src/game/g_console.pas index 7d563ae..a5652ce 100644 --- a/src/game/g_console.pas +++ b/src/game/g_console.pas @@ -404,6 +404,7 @@ begin AddCommand('pf_draw_frame', ProfilerCommands, 'draw frame rendering profiles'); //AddCommand('pf_update_frame', ProfilerCommands); AddCommand('pf_coldet', ProfilerCommands, 'draw collision detection profiles'); + AddCommand('pf_los', ProfilerCommands, 'draw monster LOS profiles'); AddCommand('r_sq_draw', ProfilerCommands, 'accelerated spatial queries in rendering'); AddCommand('cd_sq_enabled', ProfilerCommands, 'accelerated spatial queries in map coldet'); AddCommand('mon_sq_enabled', ProfilerCommands, 'use accelerated spatial queries for monsters'); diff --git a/src/game/g_game.pas b/src/game/g_game.pas index 6cd9970..c6ea496 100644 --- a/src/game/g_game.pas +++ b/src/game/g_game.pas @@ -313,6 +313,7 @@ var g_profile_frame_update: Boolean = false; g_profile_frame_draw: Boolean = false; g_profile_collision: Boolean = false; + g_profile_los: Boolean = false; g_profile_history_size: Integer = 1000; procedure g_ResetDynlights (); @@ -2932,9 +2933,11 @@ end; procedure drawProfilers (); var px: Integer = -1; + py: Integer = -1; begin - if g_profile_frame_draw then px := px-drawProfiles(px, -1, profileFrameDraw); - if g_profile_collision then px := px-drawProfiles(px, -1, profMapCollision); + if g_profile_frame_draw then px := px-drawProfiles(px, py, profileFrameDraw); + if g_profile_collision then begin px := px-drawProfiles(px, py, profMapCollision); py -= calcProfilesHeight(profMonsLOS); end; + if g_profile_los then begin px := px-drawProfiles(px, py, profMonsLOS); py -= calcProfilesHeight(profMonsLOS); end; end; procedure g_Game_Draw(); @@ -4965,6 +4968,7 @@ begin if (cmd = 'pf_draw_frame') then begin binaryFlag(g_profile_frame_draw, 'render profiles'); exit; end; if (cmd = 'pf_update_frame') then begin binaryFlag(g_profile_frame_update, 'update profiles (not yet)'); exit; end; if (cmd = 'pf_coldet') then begin binaryFlag(g_profile_collision, 'coldet profiles'); exit; end; + if (cmd = 'pf_los') then begin binaryFlag(g_profile_los, 'monster LOS profiles'); exit; end; if (cmd = 'r_sq_draw') then begin binaryFlag(gdbg_map_use_accel_render, 'accelerated rendering'); exit; end; if (cmd = 'cd_sq_enabled') then begin binaryFlag(gdbg_map_use_accel_coldet, 'accelerated map coldet'); exit; end; if (cmd = 'mon_sq_enabled') then begin binaryFlag(gmon_debug_use_sqaccel, 'accelerated monster coldet'); exit; end; @@ -6800,12 +6804,6 @@ var begin Parse_Params(pars); - s := Find_Param_Value(pars, '--profile-render'); - if (s <> '') then g_profile_frame_draw := true; - - s := Find_Param_Value(pars, '--profile-coldet'); - if (s <> '') then g_profile_collision := true; - // Debug mode: s := Find_Param_Value(pars, '--debug'); if (s <> '') then diff --git a/src/game/g_grid.pas b/src/game/g_grid.pas index 13c57c9..70703af 100644 --- a/src/game/g_grid.pas +++ b/src/game/g_grid.pas @@ -29,6 +29,7 @@ type type TGridRayQueryCB = function (obj: ITP; tag: Integer; x, y, prevx, prevy: Integer): Boolean is nested; // return `true` to stop const TagDisabled = $40000000; + const TagFullMask = $3fffffff; private const @@ -43,7 +44,7 @@ type mX, mY, mWidth, mHeight: Integer; // aabb mQueryMark: LongWord; // was this object visited at this query? mObj: ITP; - mTag: Integer; + mTag: Integer; // `TagDisabled` set: disabled ;-) nextLink: TBodyProxyId; // next free or nothing private @@ -56,7 +57,7 @@ type next: Integer; // in this cell; index in mCells end; - TGridInternalCB = function (grida: Integer): Boolean of object; // return `true` to stop + TGridInternalCB = function (grida: Integer; bodyId: TBodyProxyId): Boolean of object; // return `true` to stop private mTileSize: Integer; @@ -72,10 +73,6 @@ type mProxyCount: Integer; // currently used mProxyMaxCount: Integer; - mUData: TBodyProxyId; // for inserter/remover - mTagMask: Integer; // for iterator - mItCB: TGridQueryCB; // for iterator - private function allocCell: Integer; procedure freeCell (idx: Integer); // `next` is simply overwritten @@ -83,13 +80,13 @@ type function allocProxy (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer): TBodyProxyId; procedure freeProxy (body: TBodyProxyId); - procedure insert (body: TBodyProxyId); - procedure remove (body: TBodyProxyId); + procedure insertInternal (body: TBodyProxyId); + procedure removeInternal (body: TBodyProxyId); - function forGridRect (x, y, w, h: Integer; cb: TGridInternalCB): Boolean; + function forGridRect (x, y, w, h: Integer; cb: TGridInternalCB; bodyId: TBodyProxyId): Boolean; - function inserter (grida: Integer): Boolean; - function remover (grida: Integer): Boolean; + function inserter (grida: Integer; bodyId: TBodyProxyId): Boolean; + function remover (grida: Integer; bodyId: TBodyProxyId): Boolean; function getProxyEnabled (pid: TBodyProxyId): Boolean; inline; procedure setProxyEnabled (pid: TBodyProxyId; val: Boolean); inline; @@ -98,7 +95,7 @@ type constructor Create (aMinPixX, aMinPixY, aPixWidth, aPixHeight: Integer; aTileSize: Integer=GridDefaultTileSize); destructor Destroy (); override; - function insertBody (aObj: ITP; ax, ay, aWidth, aHeight: Integer; aTag: Integer=0): TBodyProxyId; + function insertBody (aObj: ITP; ax, ay, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId; procedure removeBody (aObj: TBodyProxyId); // WARNING! this WILL destroy proxy! procedure moveBody (body: TBodyProxyId; dx, dy: Integer); @@ -107,19 +104,22 @@ type function insideGrid (x, y: Integer): Boolean; inline; - //WARNING: can't do recursive queries + //WARNING: don't modify grid while any query is in progress (no checks are made!) + // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it) // no callback: return `true` on the first hit - function forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1): Boolean; + function forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP; - //WARNING: can't do recursive queries + //WARNING: don't modify grid while any query is in progress (no checks are made!) + // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it) // no callback: return `true` on the first hit - function forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1): Boolean; + function forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1): ITP; - //WARNING: can't do recursive queries + //WARNING: don't modify grid while any query is in progress (no checks are made!) + // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it) // cb with `(nil)` will be called before processing new tile // no callback: return `true` on the nearest hit - function traceRay (x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): Boolean; overload; - function traceRay (out ex, ey: Integer; x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): Boolean; + function traceRay (x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP; overload; + function traceRay (out ex, ey: Integer; x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP; procedure dumpStats (); @@ -183,9 +183,6 @@ begin mProxyFree := 0; mProxyCount := 0; mProxyMaxCount := 0; - mUData := 0; - mTagMask := -1; - mItCB := nil; e_WriteLog(Format('created grid with size: %dx%d (tile size: %d); pix: %dx%d', [mWidth, mHeight, mTileSize, mWidth*mTileSize, mHeight*mTileSize]), MSG_NOTIFY); end; @@ -244,7 +241,7 @@ begin end else begin - mProxies[pid].mTag := mProxies[pid].mTag or TagDisabled + mProxies[pid].mTag := mProxies[pid].mTag or TagDisabled; end; end; end; @@ -326,7 +323,7 @@ begin end; -function TBodyGridBase.forGridRect (x, y, w, h: Integer; cb: TGridInternalCB): Boolean; +function TBodyGridBase.forGridRect (x, y, w, h: Integer; cb: TGridInternalCB; bodyId: TBodyProxyId): Boolean; var gx, gy: Integer; gw, gh, tsize: Integer; @@ -350,14 +347,14 @@ begin begin if (gx < 0) then continue; if (gx >= gw) then break; - result := cb(gy*gw+gx); + result := cb(gy*gw+gx, bodyId); if result then exit; end; end; end; -function TBodyGridBase.inserter (grida: Integer): Boolean; +function TBodyGridBase.inserter (grida: Integer; bodyId: TBodyProxyId): Boolean; var cidx: Integer; pc: Integer; @@ -376,7 +373,7 @@ begin if (pi.bodies[f] = -1) then begin // can add here - pi.bodies[f] := mUData; + pi.bodies[f] := bodyId; if (f+1 < Length(TGridCell.bodies)) then pi.bodies[f+1] := -1; exit; end; @@ -384,25 +381,24 @@ begin end; // either no room, or no cell at all cidx := allocCell(); - mCells[cidx].bodies[0] := mUData; + mCells[cidx].bodies[0] := bodyId; mCells[cidx].bodies[1] := -1; mCells[cidx].next := pc; mGrid[grida] := cidx; end; - -procedure TBodyGridBase.insert (body: TBodyProxyId); +procedure TBodyGridBase.insertInternal (body: TBodyProxyId); var px: PBodyProxyRec; begin if (body < 0) or (body > High(mProxies)) then exit; // just in case px := @mProxies[body]; - mUData := body; - forGridRect(px.mX, px.mY, px.mWidth, px.mHeight, inserter); + forGridRect(px.mX, px.mY, px.mWidth, px.mHeight, inserter, body); end; -function TBodyGridBase.remover (grida: Integer): Boolean; +// absolutely not tested +function TBodyGridBase.remover (grida: Integer; bodyId: TBodyProxyId): Boolean; var f: Integer; pidx, idx, tmp: Integer; @@ -419,7 +415,7 @@ begin f := 0; while (f < High(TGridCell.bodies)) do begin - if (pc.bodies[f] = mUData) then + if (pc.bodies[f] = bodyId) then begin // i found her! if (f = 0) and (pc.bodies[1] = -1) then @@ -450,30 +446,29 @@ begin end; end; - // absolutely not tested -procedure TBodyGridBase.remove (body: TBodyProxyId); +procedure TBodyGridBase.removeInternal (body: TBodyProxyId); var px: PBodyProxyRec; begin if (body < 0) or (body > High(mProxies)) then exit; // just in case px := @mProxies[body]; - mUData := body; - forGridRect(px.mX, px.mY, px.mWidth, px.mHeight, remover); + forGridRect(px.mX, px.mY, px.mWidth, px.mHeight, remover, body); end; -function TBodyGridBase.insertBody (aObj: ITP; aX, aY, aWidth, aHeight: Integer; aTag: Integer=0): TBodyProxyId; +function TBodyGridBase.insertBody (aObj: ITP; aX, aY, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId; begin + aTag := aTag and TagFullMask; result := allocProxy(aX, aY, aWidth, aHeight, aObj, aTag); - insert(result); + insertInternal(result); end; procedure TBodyGridBase.removeBody (aObj: TBodyProxyId); begin if (aObj < 0) or (aObj > High(mProxies)) then exit; // just in case - remove(aObj); + removeInternal(aObj); freeProxy(aObj); end; @@ -484,13 +479,13 @@ var begin if (body < 0) or (body > High(mProxies)) then exit; // just in case if ((dx = 0) and (dy = 0) and (sx = 0) and (sy = 0)) then exit; - remove(body); + removeInternal(body); px := @mProxies[body]; Inc(px.mX, dx); Inc(px.mY, dy); Inc(px.mWidth, sx); Inc(px.mHeight, sy); - insert(body); + insertInternal(body); end; procedure TBodyGridBase.moveBody (body: TBodyProxyId; dx, dy: Integer); @@ -506,7 +501,7 @@ end; // ////////////////////////////////////////////////////////////////////////// // // no callback: return `true` on the first hit -function TBodyGridBase.forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1): Boolean; +function TBodyGridBase.forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1): ITP; var f: Integer; idx, curci: Integer; @@ -515,7 +510,8 @@ var lq: LongWord; ptag: Integer; begin - result := false; + result := Default(ITP); + tagmask := tagmask and TagFullMask; if (tagmask = 0) then exit; // make coords (0,0)-based @@ -545,16 +541,20 @@ begin begin if (cc.bodies[f] = -1) then break; px := @mProxies[cc.bodies[f]]; - if (px.mQueryMark <> lq) then + ptag := px.mTag; + if ((ptag and TagDisabled) = 0) and ((ptag and tagmask) <> 0) and (px.mQueryMark <> lq) then begin - ptag := px.mTag; - if ((ptag and TagDisabled) = 0) and ((px.mTag and tagmask) <> 0) then + if (x >= px.mX) and (y >= px.mY) and (x < px.mX+px.mWidth) and (y < px.mY+px.mHeight) then begin - if (x >= px.mX) and (y >= px.mY) and (x < px.mX+px.mWidth) and (y < px.mY+px.mHeight) then + px.mQueryMark := lq; + if assigned(cb) then begin - px.mQueryMark := lq; - if assigned(cb) then result := cb(px.mObj, px.mTag) else result := true; - if result then exit; + if cb(px.mObj, ptag) then begin result := px.mObj; exit; end; + end + else + begin + result := px.mObj; + exit; end; end; end; @@ -565,7 +565,7 @@ end; // no callback: return `true` on the first hit -function TBodyGridBase.forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1): Boolean; +function TBodyGridBase.forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP; var idx: Integer; gx, gy: Integer; @@ -578,8 +578,10 @@ var x0, y0: Integer; ptag: Integer; begin - result := false; + result := Default(ITP); if (w < 1) or (h < 1) then exit; + tagmask := tagmask and TagFullMask; + if (tagmask = 0) then exit; x0 := x; y0 := y; @@ -623,16 +625,23 @@ begin begin if (cc.bodies[f] = -1) then break; px := @mProxies[cc.bodies[f]]; - if (px.mQueryMark <> lq) then + ptag := px.mTag; + if (not allowDisabled) and ((ptag and TagDisabled) <> 0) then continue; + if ((ptag and tagmask) <> 0) and (px.mQueryMark <> lq) then + //if ((ptag and TagDisabled) = 0) and ((ptag and tagmask) <> 0) and (px.mQueryMark <> lq) then + //if ( ((ptag and TagDisabled) = 0) = ignoreDisabled) and ((ptag and tagmask) <> 0) and (px.mQueryMark <> lq) then begin - ptag := px.mTag; - if ((ptag and TagDisabled) = 0) and ((px.mTag and tagmask) <> 0) then + if (x0 >= px.mX+px.mWidth) or (y0 >= px.mY+px.mHeight) then continue; + if (x0+w <= px.mX) or (y0+h <= px.mY) then continue; + px.mQueryMark := lq; + if assigned(cb) then begin - if (x0 >= px.mX+px.mWidth) or (y0 >= px.mY+px.mHeight) then continue; - if (x0+w <= px.mX) or (y0+h <= px.mY) then continue; - px.mQueryMark := lq; - if assigned(cb) then result := cb(px.mObj, px.mTag) else result := true; - if result then exit; + if cb(px.mObj, ptag) then begin result := px.mObj; exit; end; + end + else + begin + result := px.mObj; + exit; end; end; end; @@ -645,7 +654,7 @@ end; // ////////////////////////////////////////////////////////////////////////// // // no callback: return `true` on the nearest hit -function TBodyGridBase.traceRay (x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): Boolean; +function TBodyGridBase.traceRay (x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP; var ex, ey: Integer; begin @@ -654,7 +663,7 @@ end; // no callback: return `true` on the nearest hit -function TBodyGridBase.traceRay (out ex, ey: Integer; x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): Boolean; +function TBodyGridBase.traceRay (out ex, ey: Integer; x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP; var i: Integer; dx, dy: Integer; @@ -680,18 +689,21 @@ var ptag: Integer; lastDistSq, distSq: Integer; wasHit: Boolean = false; + lastObj: ITP; + lastWasInGrid: Boolean = false; begin - result := false; - + result := Default(ITP); + lastObj := Default(ITP); + tagmask := tagmask and TagFullMask; if (tagmask = 0) then begin ex := x0; ey := y0; exit; end; // make coords (0,0)-based minx := mMinX; miny := mMinY; - Dec(x0, minx); - Dec(y0, miny); - Dec(x1, minx); - Dec(y1, miny); + //Dec(x0, minx); + //Dec(y0, miny); + //Dec(x1, minx); + //Dec(y1, miny); dx := x1-x0; dy := y1-y0; @@ -704,8 +716,9 @@ begin if (dx > dy) then d := dx else d := dy; - x := x0; - y := y0; + // `x` and `y` will be in grid coords + x := x0-minx; + y := y0-miny; // increase query counter Inc(mLastQuery); @@ -723,38 +736,72 @@ begin maxx := gw*tsize-1; maxy := gh*tsize-1; lastDistSq := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)+1; + lastWasInGrid := (x >= 0) and (y >= 0) and (x <= maxx) and (y <= maxy); for i := 1 to d do begin + // prevs are always in map coords prevX := x+minx; prevY := y+miny; - Inc(xerr, dx); if (xerr >= d) then begin Dec(xerr, d); Inc(x, incx); end; - Inc(yerr, dy); if (yerr >= d) then begin Dec(yerr, d); Inc(y, incy); end; + // do one step + xerr += dx; if (xerr >= d) then begin xerr -= d; x += incx; end; + yerr += dy; if (yerr >= d) then begin yerr -= d; y += incy; end; + // check for new tile if (x >= 0) and (y >= 0) and (x <= maxx) and (y <= maxy) then begin ga := (y div tsize)*gw+(x div tsize); if (lastGA <> ga) then begin // new cell + lastWasInGrid := true; // do it here, yeah lastGA := ga; + // had something in the cell we're leaving? + if (ccidx <> -1) then + begin + // yes, signal cell completion + if assigned(cb) then + begin + if cb(nil, 0, x+minx, y+miny, prevX, prevY) then begin result := lastObj; exit; end; + end + else if wasHit then + begin + result := lastObj; + exit; + end; + end; + // have something in this cell? ccidx := mGrid[lastGA]; end; end else begin + // out of grid, had something in the cell we're last processed? if (ccidx <> -1) then begin + // yes, signal cell completion ccidx := -1; - if assigned(cb) then result := cb(nil, 0, x+minx, y+miny, prevX, prevY) else result := wasHit; - if result then exit; + if assigned(cb) then + begin + if cb(nil, 0, x+minx, y+miny, prevX, prevY) then begin result := lastObj; exit; end; + end + else if wasHit then + begin + result := lastObj; + exit; + end; end; + if lastWasInGrid then exit; // oops, stepped out of the grid -- there is no way to return + //lastWasInGrid := false; end; + // has something to process in the current cell? if (ccidx <> -1) then begin + // process cell curci := ccidx; - hasUntried := false; + hasUntried := false; // this will be set to `true` if we have some panels we still want to process at the next step + // convert coords to map Inc(x, minx); Inc(y, miny); while (curci <> -1) do @@ -764,47 +811,63 @@ begin begin if (cc.bodies[f] = -1) then break; px := @mProxies[cc.bodies[f]]; - if (px.mQueryMark <> lq) then + ptag := px.mTag; + if ((ptag and TagDisabled) = 0) and ((ptag and tagmask) <> 0) and (px.mQueryMark <> lq) then begin - ptag := px.mTag; - if ((ptag and TagDisabled) = 0) and ((px.mTag and tagmask) <> 0) then + // can we process this wall? + if (x >= px.mX) and (y >= px.mY) and (x < px.mX+px.mWidth) and (y < px.mY+px.mHeight) then begin - if (x >= px.mX) and (y >= px.mY) and (x < px.mX+px.mWidth) and (y < px.mY+px.mHeight) then + px.mQueryMark := lq; // mark as processed + if assigned(cb) then begin - px.mQueryMark := lq; - if assigned(cb) then - begin - result := cb(px.mObj, px.mTag, x, y, prevX, prevY); - if result then begin ex := prevX; ey := prevY; exit; end; - end - else + if cb(px.mObj, ptag, x, y, prevX, prevY) then begin - distSq := (prevX-x)*(prevX-x)+(prevY-y)*(prevY-y); - if (distSq < lastDistSq) then - begin - wasHit := true; - lastDistSq := distSq; - ex := prevx; - ey := prevy; - end; + result := lastObj; + ex := prevX; + ey := prevY; + exit; end; end else begin - hasUntried := true; + // remember this hitpoint if it is nearer than an old one + distSq := (prevX-x0)*(prevX-x0)+(prevY-y0)*(prevY-y0); + if (distSq < lastDistSq) then + begin + wasHit := true; + lastDistSq := distSq; + ex := prevX; + ey := prevY; + lastObj := px.mObj; + end; end; + end + else + begin + // this is possibly interesting wall, set "has more to check" flag + hasUntried := true; end; end; end; + // next cell curci := cc.next; end; + // still has something interesting in this cell? if not hasUntried then begin - // don't process this cell anymore + // nope, don't process this cell anymore; signal cell completion ccidx := -1; - if assigned(cb) then result := cb(nil, 0, x, y, prevX, prevY) else result := wasHit; - if result then exit; // don't update lasthit: it is done in real checker + if assigned(cb) then + begin + if cb(nil, 0, x, y, prevX, prevY) then begin result := lastObj; exit; end; + end + else if wasHit then + begin + result := lastObj; + exit; + end; end; + // convert coords to grid Dec(x, minx); Dec(y, miny); end; diff --git a/src/game/g_map.pas b/src/game/g_map.pas index d1d824a..7d6f3dd 100644 --- a/src/game/g_map.pas +++ b/src/game/g_map.pas @@ -14,6 +14,7 @@ * along with this program. If not, see . *) {$INCLUDE ../shared/a_modes.inc} +{$DEFINE MAP_DEBUG_ENABLED_FLAG} unit g_map; interface @@ -157,6 +158,8 @@ const GridTagLift = 1 shl 8; GridTagBlockMon = 1 shl 9; + GridDrawableMask = (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore); + var gWalls: TPanelArray; @@ -416,7 +419,7 @@ begin if (hitx <> nil) then hitx^ := lastX; if (hity <> nil) then hity^ := lastY; *) - result := mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor)); + result := (mapGrid.traceRay(ex, ey, x0, y0, x1, y1, nil, (GridTagWall or GridTagDoor)) <> nil); end; @@ -463,12 +466,12 @@ begin if ((tagmask and GridTagLift) <> 0) then begin // slow - result := mapGrid.forEachAtPoint(x, y, checker, tagmask); + result := (mapGrid.forEachAtPoint(x, y, checker, tagmask) <> nil); end else begin // fast - result := mapGrid.forEachAtPoint(x, y, nil, tagmask); + result := (mapGrid.forEachAtPoint(x, y, nil, tagmask) <> nil); end; end; @@ -1255,7 +1258,14 @@ var pan.tag := tag; if not pan.visvalid then continue; pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, tag); + // "enabled" flag has meaning only for doors and walls (engine assumes it); but meh... mapGrid.proxyEnabled[pan.proxyId] := pan.Enabled; + {$IFDEF MAP_DEBUG_ENABLED_FLAG} + if ((tag and (GridTagWall or GridTagDoor)) <> 0) then + begin + e_WriteLog(Format('INSERTED wall #%d(%d) enabled (%d)', [Integer(idx), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId])]), MSG_NOTIFY); + end; + {$ENDIF} //mapTree.insertObject(pan, tag, true); // as static object end; end; @@ -2139,7 +2149,7 @@ begin end else} begin - mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, (GridTagBack or GridTagStep or GridTagWall or GridTagDoor or GridTagAcid1 or GridTagAcid2 or GridTagWater or GridTagFore)); + mapGrid.forEachInAABB(x0, y0, wdt, hgt, checker, GridDrawableMask, true); end; // list will be rendered in `g_game.DrawPlayer()` end; @@ -2160,7 +2170,7 @@ begin end else} begin - mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor)); + mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, checker, (GridTagWall or GridTagDoor), true); end; end; @@ -2325,10 +2335,10 @@ end; function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean; +const + SlowMask = GridTagLift or GridTagBlockMon; function checker (pan: TPanel; tag: Integer): Boolean; begin - result := false; // don't stop, ever - { if ((tag and (GridTagWall or GridTagDoor)) <> 0) then begin @@ -2356,7 +2366,7 @@ function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; // other shit //result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height); - result := true; + result := true; // i found her! end; var @@ -2389,28 +2399,28 @@ begin begin if (Width = 1) and (Height = 1) then begin - if ((tagmask and (GridTagLift or GridTagBlockMon)) <> 0) then + if ((tagmask and SlowMask) <> 0) then begin // slow - result := mapGrid.forEachAtPoint(X, Y, checker, tagmask); + result := (mapGrid.forEachAtPoint(X, Y, checker, tagmask) <> nil); end else begin // fast - result := mapGrid.forEachAtPoint(X, Y, nil, tagmask); + result := (mapGrid.forEachAtPoint(X, Y, nil, tagmask) <> nil); end; end else begin - if ((tagmask and (GridTagLift or GridTagBlockMon)) <> 0) then + if ((tagmask and SlowMask) <> 0) then begin // slow - result := mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask); + result := (mapGrid.forEachInAABB(X, Y, Width, Height, checker, tagmask) <> nil); end else begin // fast - result := mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask); + result := (mapGrid.forEachInAABB(X, Y, Width, Height, nil, tagmask) <> nil); end; end; end; @@ -2482,27 +2492,40 @@ end; procedure g_Map_EnableWall(ID: DWORD); +var + pan: TPanel; begin - with gWalls[ID] do - begin - Enabled := True; - g_Mark(X, Y, Width, Height, MARK_DOOR, True); - mapGrid.proxyEnabled[proxyId] := true; + pan := gWalls[ID]; + pan.Enabled := True; + g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, True); - if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID); - end; + mapGrid.proxyEnabled[pan.proxyId] := true; + //if (pan.proxyId >= 0) then mapGrid.proxyEnabled[pan.proxyId] := true + //else pan.proxyId := mapGrid.insertBody(pan, pan.X, pan.Y, pan.Width, pan.Height, GridTagDoor); + + if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(gWalls[ID].PanelType, ID); + + {$IFDEF MAP_DEBUG_ENABLED_FLAG} + e_WriteLog(Format('wall #%d(%d) enabled (%d) (%d,%d)-(%d,%d)', [Integer(ID), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.x, pan.y, pan.width, pan.height]), MSG_NOTIFY); + {$ENDIF} end; procedure g_Map_DisableWall(ID: DWORD); +var + pan: TPanel; begin - with gWalls[ID] do - begin - Enabled := False; - g_Mark(X, Y, Width, Height, MARK_DOOR, False); - mapGrid.proxyEnabled[proxyId] := false; + pan := gWalls[ID]; + pan.Enabled := False; + g_Mark(pan.X, pan.Y, pan.Width, pan.Height, MARK_DOOR, False); - if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(PanelType, ID); - end; + mapGrid.proxyEnabled[pan.proxyId] := false; + //if (pan.proxyId >= 0) then begin mapGrid.removeBody(pan.proxyId); pan.proxyId := -1; end; + + if g_Game_IsServer and g_Game_IsNet then MH_SEND_PanelState(pan.PanelType, ID); + + {$IFDEF MAP_DEBUG_ENABLED_FLAG} + e_WriteLog(Format('wall #%d(%d) disabled (%d) (%d,%d)-(%d,%d)', [Integer(ID), Integer(pan.proxyId), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.x, pan.y, pan.width, pan.height]), MSG_NOTIFY); + {$ENDIF} end; procedure g_Map_SwitchTexture(PanelType: Word; ID: DWORD; AnimLoop: Byte = 0); @@ -2930,14 +2953,14 @@ begin topx := x; topy := y; // started outside of the liquid? - if not mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) then begin result := false; exit; end; + if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then begin result := false; exit; end; if (dx = 0) and (dy = 0) then begin result := false; exit; end; // sanity check result := true; while true do begin Inc(x, dx); Inc(y, dy); - if not mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) then exit; // out of the water, just exit + if (mapGrid.forEachAtPoint(x, y, nil, MaskLiquid) = nil) then exit; // out of the water, just exit topx := x; topy := y; end; diff --git a/src/game/g_monsters.pas b/src/game/g_monsters.pas index 229748d..f94e8e8 100644 --- a/src/game/g_monsters.pas +++ b/src/game/g_monsters.pas @@ -20,7 +20,7 @@ interface uses g_basic, e_graphics, g_phys, g_textures, - g_saveload, BinEditor, g_panel, z_aabbtree; + g_saveload, BinEditor, g_panel, z_aabbtree, xprofiler; const MONSTATE_SLEEP = 0; @@ -214,6 +214,17 @@ var gmon_debug_use_sqaccel: Boolean = true; +//HACK! +procedure g_Mons_ProfilersBegin (); +procedure g_Mons_ProfilersEnd (); + +procedure g_Mons_LOS_Start (); inline; +procedure g_Mons_LOS_End (); inline; + +var + profMonsLOS: TProfiler = nil; //WARNING: FOR DEBUGGING ONLY! + + implementation uses @@ -223,6 +234,34 @@ uses g_language, g_netmsg; +// ////////////////////////////////////////////////////////////////////////// // +procedure g_Mons_ProfilersBegin (); +begin + if (profMonsLOS = nil) then profMonsLOS := TProfiler.Create('LOS CALC', g_profile_history_size); + profMonsLOS.mainBegin(g_profile_los); + if g_profile_los then + begin + profMonsLOS.sectionBegin('loscalc'); + profMonsLOS.sectionEnd(); + end; +end; + +procedure g_Mons_ProfilersEnd (); +begin + if (profMonsLOS <> nil) and (g_profile_los) then profMapCollision.mainEnd(); +end; + +procedure g_Mons_LOS_Start (); inline; +begin + profMonsLOS.sectionBeginAccum('loscalc'); +end; + +procedure g_Mons_LOS_End (); inline; +begin + profMonsLOS.sectionEnd(); +end; + + // ////////////////////////////////////////////////////////////////////////// // var monCheckTrapLastFrameId: DWord; diff --git a/src/game/g_window.pas b/src/game/g_window.pas index f7770bb..c53cba0 100644 --- a/src/game/g_window.pas +++ b/src/game/g_window.pas @@ -48,7 +48,7 @@ uses SDL2, GL, GLExt, e_graphics, e_log, g_main, g_console, SysUtils, e_input, g_options, g_game, g_basic, g_textures, e_sound, g_sound, g_menu, ENet, g_net, - g_map, g_gfx; + g_map, g_gfx, g_monsters; var h_Wnd: PSDL_Window; @@ -579,6 +579,7 @@ begin end; g_Map_ProfilersBegin(); + g_Mons_ProfilersBegin(); t := Time_Delta div 28{(27777 div 1000)}; if t > 0 then @@ -598,6 +599,7 @@ begin end; g_Map_ProfilersEnd(); + g_Mons_ProfilersEnd(); if wLoadingQuit then begin @@ -716,6 +718,10 @@ begin if ParamStr(idx) = '--no-particles' then gpart_dbg_enabled := false; if ParamStr(idx) = '--no-los' then gmon_dbg_los_enabled := false; + if ParamStr(idx) = '--profile-render' then g_profile_frame_draw := true; + if ParamStr(idx) = '--profile-coldet' then g_profile_collision := true; + if ParamStr(idx) = '--profile-los' then g_profile_los := true; + if ParamStr(idx) = '--no-part-phys' then gpart_dbg_phys_enabled := false; if ParamStr(idx) = '--no-part-physics' then gpart_dbg_phys_enabled := false; if ParamStr(idx) = '--no-particles-phys' then gpart_dbg_phys_enabled := false; -- 2.29.2