From 76c10f8f65097116f25958db5285f13d040f6ee8 Mon Sep 17 00:00:00 2001 From: Ketmar Dark Date: Mon, 4 Sep 2017 22:25:34 +0300 Subject: [PATCH] `traceBox()` API bugfix; squashing now works (i hope) --- src/game/g_grid.pas | 35 ++++++++++++++++++++--------------- src/game/g_holmes.pas | 42 +++++++++++++++++++++++++++++++++++++++--- src/game/g_panel.pas | 40 +++++++++++++++++++--------------------- 3 files changed, 78 insertions(+), 39 deletions(-) diff --git a/src/game/g_grid.pas b/src/game/g_grid.pas index fae595d..092f13e 100644 --- a/src/game/g_grid.pas +++ b/src/game/g_grid.pas @@ -238,12 +238,10 @@ type function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean; // sweep two AABB's to see if and when they are overlapping -// returns `true` if collision was detected (or boxes overlaps) +// returns `true` if collision was detected (but boxes doesn't overlap) +// u1 and u1 has no sense if no collision was detected // u0 = normalized time of first collision (i.e. collision starts at myMove*u0) // u1 = normalized time of second collision (i.e. collision stops after myMove*u1) -// if no collision was detected: -// u1 < 0: no collision at all -// u1 >= 0: boxes are overlapping at the start; u0 has no meaning, u1 is exit time, hitedge is undefined // hitedge for `it`: 0: top; 1: right; 2: bottom; 3: left // enter/exit coords will form non-intersecting configuration (i.e. will be before/after the actual collision) function sweepAABB (mex0, mey0, mew, meh: Integer; medx, medy: Integer; itx0, ity0, itw, ith: Integer; @@ -2552,7 +2550,7 @@ var lq: LongWord; f, ptag: Integer; minu0: Single = 100000.0; - u0: Single; + u0, u1: Single; hedge: Integer; cx0, cy0, cx1, cy1: Integer; begin @@ -2591,13 +2589,11 @@ begin end; lq := mLastQuery; - gy := cy0; - while (gy <= cy1) do + for gy := cy0 div mTileSize to cy1 div mTileSize do begin - gx := cx0; - while (gx <= cx1) do + for gx := cx0 div mTileSize to cx1 div mTileSize do begin - cidx := mGrid[(gy div mTileSize)*mWidth+(gx div mTileSize)]; + cidx := mGrid[gy*mWidth+gx]; while (cidx <> -1) do begin cc := @mCells[cidx]; @@ -2613,8 +2609,19 @@ begin begin if not cb(px.mObj, ptag) then continue; end; - if not sweepAABB(cx0+mMinX, cy0+mMinY, aw, ah, dx, dy, px.mX, px.mY, px.mWidth, px.mHeight, @u0, @hedge) then + if not sweepAABB(ax0, ay0, aw, ah, dx, dy, px.mX, px.mY, px.mWidth, px.mHeight, @u0, @hedge, @u1) then begin + { + if (u1 >= 0) then + begin + // overlapping + ex := ax0; + ey := ay0; + result := px.mObj; + mInQuery := false; + exit; + end; + } continue; end; if (minu0 > u0) then @@ -2623,8 +2630,8 @@ begin minu0 := u0; if (u0 = 0.0) then begin - ex := cx0+mMinX; - ey := cy0+mMinY; + ex := ax0; + ey := ay0; mInQuery := false; exit; end; @@ -2634,9 +2641,7 @@ begin // next cell cidx := cc.next; end; - Inc(gx, mTileSize); end; - Inc(gy, mTileSize); end; if (minu0 <= 1.0) then diff --git a/src/game/g_holmes.pas b/src/game/g_holmes.pas index 1f548e4..47d3481 100644 --- a/src/game/g_holmes.pas +++ b/src/game/g_holmes.pas @@ -122,6 +122,7 @@ var showLayersWindow: Boolean = false; showOutlineWindow: Boolean = false; showTriggers: Boolean = {$IF DEFINED(D2F_DEBUG)}false{$ELSE}false{$ENDIF}; + showTraceBox: Boolean = {$IF DEFINED(D2F_DEBUG)}false{$ELSE}false{$ENDIF}; // ////////////////////////////////////////////////////////////////////////// // @@ -924,6 +925,34 @@ procedure plrDebugDraw (); end; end; + procedure drawTraceBox (); + var + plr: TPlayer; + px, py, pw, ph: Integer; + pdx, pdy: Integer; + ex, ey: Integer; + pan: TPanel; + begin + if (Length(gPlayers) < 1) then exit; + plr := gPlayers[0]; + if (plr = nil) then exit; + plr.getMapBox(px, py, pw, ph); + drawRect(px, py, pw, ph, 255, 0, 255, 200); + pdx := pmsCurMapX-(px+pw div 2); + pdy := pmsCurMapY-(py+ph div 2); + drawLine(px+pw div 2, py+ph div 2, px+pw div 2+pdx, py+ph div 2+pdy, 255, 0, 255, 200); + pan := mapGrid.traceBox(ex, ey, px, py, pw, ph, pdx, pdy, nil, GridTagObstacle); + if (pan = nil) then + begin + drawRect(px+pdx, py+pdy, pw, ph, 255, 255, 255, 180); + end + else + begin + drawRect(px+pdx, py+pdy, pw, ph, 255, 255, 0, 180); + end; + drawRect(ex, ey, pw, ph, 255, 127, 0, 180); + end; + procedure hilightCell (cx, cy: Integer); begin fillRect(cx, cy, monsGrid.tileSize, monsGrid.tileSize, 0, 128, 0, 64); @@ -1200,7 +1229,9 @@ begin if showTriggers then drawTriggers(); if showGrid then drawSelectedPlatformCells(); - drawAwakeCells(); + //drawAwakeCells(); + + if showTraceBox then drawTraceBox(); //drawGibsBoxes(); @@ -1378,6 +1409,8 @@ begin end; end; +procedure dbgToggleTraceBox (arg: Integer=-1); begin if (arg < 0) then showTraceBox := not showTraceBox else showTraceBox := (arg > 0); end; + procedure cbAtcurSelectMonster (); function monsAtDump (mon: TMonster; tag: Integer): Boolean; begin @@ -1425,7 +1458,7 @@ procedure cbAtcurDumpWalls (); begin result := false; // don't stop if (platMarkedGUID = -1) then platMarkedGUID := pan.guid; - e_LogWritefln('wall #%d(%d); enabled=%d (%d); (%d,%d)-(%d,%d)', [pan.arrIdx, pan.proxyId, Integer(pan.Enabled), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.X, pan.Y, pan.Width, pan.Height]); + e_LogWritefln('wall ''%s'' #%d(%d); enabled=%d (%d); (%d,%d)-(%d,%d)', [pan.mapId, pan.arrIdx, pan.proxyId, Integer(pan.Enabled), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.X, pan.Y, pan.Width, pan.Height]); dumpPublishedProperties(pan); end; var @@ -1479,7 +1512,7 @@ begin cmdAdd('mon_info', bcToggleMonsterInfo, 'toggle monster info', 'monster control'); cmdAdd('mon_los_plr', bcToggleMonsterLOSPlr, 'toggle monster LOS to player', 'monster control'); cmdAdd('mon_cells', bcToggleMonsterCells, 'toggle "show all cells occupied by monsters" (SLOW!)', 'monster control'); - cmdAdd('mon_wakeup', bcMonsterWakeup, 'toggle "show all cells occupied by monsters" (SLOW!)', 'monster control'); + cmdAdd('mon_wakeup', bcMonsterWakeup, 'wake up selected monster', 'monster control'); cmdAdd('mon_spawn', bcMonsterSpawn, 'spawn monster', 'monster control'); @@ -1496,6 +1529,8 @@ begin cmdAdd('atcur_dump_monsters', cbAtcurDumpMonsters, 'dump monsters in cell', 'monster control'); cmdAdd('atcur_dump_walls', cbAtcurDumpWalls, 'dump walls in cell', 'wall control'); cmdAdd('atcur_disable_walls', cbAtcurToggleWalls, 'disable walls', 'wall control'); + + cmdAdd('dbg_tracebox', dbgToggleTraceBox, 'disable walls', 'wall control'); end; @@ -1526,6 +1561,7 @@ begin keybindAdd('M-O', 'mplat_toggle'); keybindAdd('C-T', 'plr_teleport'); + keybindAdd('M-T', 'dbg_tracebox'); keybindAdd('C-P', 'dbg_curpos'); keybindAdd('C-G', 'dbg_grid'); diff --git a/src/game/g_panel.pas b/src/game/g_panel.pas index 5197654..6160929 100644 --- a/src/game/g_panel.pas +++ b/src/game/g_panel.pas @@ -168,7 +168,7 @@ type TPanelArray = Array of TPanel; var - g_dbgpan_mplat_active: Boolean = {$IF DEFINED(D2F_DEBUG)}false{$ELSE}true{$ENDIF}; + g_dbgpan_mplat_active: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}true{$ENDIF}; g_dbgpan_mplat_step: Boolean = false; // one step, and stop @@ -528,10 +528,11 @@ var u0, u1: Single; tex, tey: Integer; pdx, pdy: Integer; + pan: TPanel; begin squash := false; - dx := 0; - dy := 0; + tex := px; + tey := py; pdx := mMovingSpeed.X; pdy := mMovingSpeed.Y; // standing on the platform? @@ -539,12 +540,12 @@ var begin if (ontop <> nil) then ontop^ := true; // yes, move with it - mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, nil, GridTagObstacle); - //e_LogWritefln('entity on the platform; tracing=(%s,%s); endpoint=(%s,%s); mustbe=(%s,%s)', [px, py, tex, tey, px+pdx, py+pdy]); - // still in platform? - squash := g_Collide(tex, tey, pw, ph, nx, ny, mpw, mph); - dx := tex-px; - dy := tey-py; + pan := mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, nil, GridTagObstacle); + if (pan <> nil) then + begin + //e_LogWritefln('entity on the platform; tracing=(%s,%s); endpoint=(%s,%s); mustbe=(%s,%s)', [px, py, tex, tey, px+pdx, py+pdy]); + if (tex = px) and (tey = py) then squash := true; + end; end else begin @@ -568,20 +569,10 @@ var if (pdx <> 0) or (pdy <> 0) then begin // has some path to go, trace the entity - mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, nil, GridTagObstacle); + pan := mapGrid.traceBox(tex, tey, px, py, pw, ph, pdx, pdy, nil, GridTagObstacle); //e_LogWritefln(' tracebox: te=(%s,%s)', [tex, tey]); - end - else - begin - // no movement - tex := px; - tey := py; + if (pan <> nil) and (tex = px) and (tey = py) then squash := true; end; - // free to push along the whole path, or path was corrected - // still in platform? - squash := g_Collide(tex, tey, pw, ph, nx, ny, mpw, mph); - dx := tex-px; - dy := tey-py; end else begin @@ -590,7 +581,14 @@ var squash := (u1 >= 0.0); end; end; + dx := tex-px; + dy := tey-py; result := (dx <> 0) or (dy <> 0); + if result and (not squash) then + begin + squash := g_Collide(tex, tey, pw, ph, nx, ny, mpw, mph); // still in platform? + if not squash then squash := g_Map_CollidePanel(tex, tey, pw, ph, (PANEL_WALL or PANEL_OPENDOOR or PANEL_CLOSEDOOR)); + end; end; function monCollect (mon: TMonster): Boolean; -- 2.29.2