summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 966e889)
raw | patch | inline | side by side (parent: 966e889)
author | Ketmar Dark <ketmar@ketmar.no-ip.org> | |
Wed, 23 Aug 2017 17:42:46 +0000 (20:42 +0300) | ||
committer | Ketmar Dark <ketmar@ketmar.no-ip.org> | |
Wed, 23 Aug 2017 18:23:56 +0000 (21:23 +0300) |
src/game/g_grid.pas | patch | blob | history | |
src/game/g_holmes.pas | patch | blob | history |
diff --git a/src/game/g_grid.pas b/src/game/g_grid.pas
index 81f19c12af6fbfc64072bbd7dbb35510ef8cf6b2..2e7105e7729190fbe6e1062c834034beaa43de47 100644 (file)
--- a/src/game/g_grid.pas
+++ b/src/game/g_grid.pas
*)
// universal spatial grid
{$INCLUDE ../shared/a_modes.inc}
+{$IF DEFINED(D2F_DEBUG)}
+ {.$DEFINE D2F_DEBUG_RAYTRACE}
+{$ENDIF}
unit g_grid;
interface
public
dbgShowTraceLog: Boolean;
+ {$IF DEFINED(D2F_DEBUG)}
+ dbgRayTraceTileHitCB: TCellQueryCB;
+ {$ENDIF}
private
function allocCell (): Integer;
// 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): ITP; overload;
- function traceRay (out ex, ey: Integer; ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
+ function traceRay (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP; overload;
+ function traceRay (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
//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)
// trace line along the grid, calling `cb` for all objects in passed cells, in no particular order
- function forEachAlongLine (x0, y0, x1, y1: Integer; cb: TGridAlongQueryCB; tagmask: Integer=-1; log: Boolean=false): ITP;
+ function forEachAlongLine (const x0, y0, x1, y1: Integer; cb: TGridAlongQueryCB; tagmask: Integer=-1; log: Boolean=false): ITP;
// debug
procedure forEachBodyCell (body: TBodyProxyId; cb: TCellQueryCB);
idx: Integer;
begin
dbgShowTraceLog := false;
+ {$IF DEFINED(D2F_DEBUG)}
+ dbgRayTraceTileHitCB := nil;
+ {$ENDIF}
{
if aTileSize < 1 then aTileSize := 1;
if aTileSize > 8192 then aTileSize := 8192; // arbitrary limit
// ////////////////////////////////////////////////////////////////////////// //
// no callback: return `true` on the nearest hit
-function TBodyGridBase.traceRay (x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
+function TBodyGridBase.traceRay (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
var
ex, ey: Integer;
begin
// no callback: return `true` on the nearest hit
// you are not supposed to understand this
-function TBodyGridBase.traceRay (out ex, ey: Integer; ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
+function TBodyGridBase.traceRay (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
const
tsize = mTileSize;
var
maxx := gw*tsize-1;
maxy := gh*tsize-1;
+ {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
+ if assigned(dbgRayTraceTileHitCB) then e_WriteLog(Format('TRACING: (%d,%d)-(%d,%d) [(%d,%d)-(%d,%d)]; maxdistsq=%d', [ax0, ay0, ax1, ay1, minx, miny, maxx, maxy, lastDistSq]), MSG_NOTIFY);
+ {$ENDIF}
+
x0 := ax0;
y0 := ay0;
x1 := ax1;
if (xd = term) then exit;
{$IF DEFINED(D2F_DEBUG)}
- if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ > mHeight*tsize) then raise Exception.Create('raycaster internal error (0)');
+ if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
{$ENDIF}
+ lastGA := (yptr^ div tsize)*gw+(xptr^ div tsize);
+ ccidx := mGrid[lastGA];
- //if (dbgShowTraceLog) then e_WriteLog(Format('raycast start: (%d,%d)-(%d,%d); xptr^=%d; yptr^=%d', [ax0, ay0, ax1, ay1, xptr^, yptr^]), MSG_NOTIFY);
+ {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
+ //if assigned(dbgRayTraceTileHitCB) then e_WriteLog('1:TRACING!', MSG_NOTIFY);
+ {$ENDIF}
- // restore query coords
- Inc(ax0, minx);
- Inc(ay0, miny);
- //Inc(ax1, minx);
- //Inc(ay1, miny);
+ {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
+ if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
+ {$ENDIF}
+
+ //if (dbgShowTraceLog) then e_WriteLog(Format('raycast start: (%d,%d)-(%d,%d); xptr^=%d; yptr^=%d', [ax0, ay0, ax1, ay1, xptr^, yptr^]), MSG_NOTIFY);
// increase query counter
Inc(mLastQuery);
begin
// check cell(s)
{$IF DEFINED(D2F_DEBUG)}
- if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ > mHeight*tsize) then raise Exception.Create('raycaster internal error (0)');
+ if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
{$ENDIF}
// new tile?
ga := (yptr^ div tsize)*gw+(xptr^ div tsize);
if (ga <> lastGA) then
begin
// yes
+ {$IF DEFINED(D2F_DEBUG)}
+ if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
+ {$ENDIF}
if (ccidx <> -1) then
begin
// signal cell completion
ey := prevy;
exit;
end;
+ (*
+ {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
+ distSq := distanceSq(ax0, ay0, prevx, prevy);
+ if assigned(dbgRayTraceTileHitCB) then e_WriteLog(Format(' hit(%d): a=(%d,%d), h=(%d,%d), p=(%d,%d); distsq=%d; lastsq=%d', [cc.bodies[f], ax0, ay0, x, y, prevx, prevy, distSq, lastDistSq]), MSG_NOTIFY);
+ if (distSq < lastDistSq) then
+ begin
+ wasHit := true;
+ lastDistSq := distSq;
+ ex := prevx;
+ ey := prevy;
+ lastObj := px.mObj;
+ end;
+ {$ENDIF}
+ *)
end
else
begin
// remember this hitpoint if it is nearer than an old one
distSq := distanceSq(ax0, ay0, prevx, prevy);
+ {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
+ if assigned(dbgRayTraceTileHitCB) then e_WriteLog(Format(' hit(%d): a=(%d,%d), h=(%d,%d), p=(%d,%d); distsq=%d; lastsq=%d', [cc.bodies[f], ax0, ay0, x, y, prevx, prevy, distSq, lastDistSq]), MSG_NOTIFY);
+ {$ENDIF}
if (distSq < lastDistSq) then
begin
wasHit := true;
// ////////////////////////////////////////////////////////////////////////// //
//FIXME! optimize this with real tile walking
-function TBodyGridBase.forEachAlongLine (x0, y0, x1, y1: Integer; cb: TGridAlongQueryCB; tagmask: Integer=-1; log: Boolean=false): ITP;
+function TBodyGridBase.forEachAlongLine (const x0, y0, x1, y1: Integer; cb: TGridAlongQueryCB; tagmask: Integer=-1; log: Boolean=false): ITP;
const
tsize = mTileSize;
var
diff --git a/src/game/g_holmes.pas b/src/game/g_holmes.pas
index 39669fa1d8dd922c7a08c7cc2ed3080993c94137..210368deda7b60c463d95b1acc1b13fe11395527 100644 (file)
--- a/src/game/g_holmes.pas
+++ b/src/game/g_holmes.pas
msB: Word = 0; // button state
kbS: Word = 0; // keyboard modifiers state
showMonsInfo: Boolean = false;
+ showMonsLOS2Plr: Boolean = false;
// ////////////////////////////////////////////////////////////////////////// //
fillRect(cx, cy, monsGrid.tileSize, monsGrid.tileSize, 0, 128, 0, 64);
end;
+ procedure hilightCell1 (cx, cy: Integer);
+ begin
+ //e_WriteLog(Format('h1: (%d,%d)', [cx, cy]), MSG_NOTIFY);
+ fillRect(cx, cy, monsGrid.tileSize, monsGrid.tileSize, 255, 255, 0, 92);
+ end;
+
+ function hilightWallTrc (pan: TPanel; tag: Integer; x, y, prevx, prevy: Integer): Boolean;
+ begin
+ result := false; // don't stop
+ if (pan = nil) then exit; // cell completion, ignore
+ //e_WriteLog(Format('h1: (%d,%d)', [cx, cy]), MSG_NOTIFY);
+ fillRect(pan.X, pan.Y, pan.Width, pan.Height, 0, 128, 128, 64);
+ end;
+
function monsCollector (mon: TMonster; tag: Integer): Boolean;
var
ex, ey: Integer;
exit;
end;
mon.getMapBox(mx, my, mw, mh);
- drawLine(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, 255, 0, 0, 128);
+ drawLine(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, 255, 0, 0, 255);
+ if (g_Map_traceToNearestWall(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, @ex, @ey) <> nil) then
+ begin
+ drawLine(mx+mw div 2, my+mh div 2, ex, ey, 0, 255, 0, 255);
+ end;
+ end;
+
+ procedure drawLOS2Plr ();
+ var
+ emx, emy, emw, emh: Integer;
+ eplr: TPlayer;
+ ex, ey: Integer;
+ begin
+ eplr := gPlayers[0];
+ if (eplr = nil) then exit;
+ eplr.getMapBox(emx, emy, emw, emh);
+ mon.getMapBox(mx, my, mw, mh);
+ drawLine(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, 255, 0, 0, 255);
+ {$IF DEFINED(D2F_DEBUG)}
+ //mapGrid.dbgRayTraceTileHitCB := hilightCell1;
+ {$ENDIF}
if (g_Map_traceToNearestWall(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, @ex, @ey) <> nil) then
+ //if (mapGrid.traceRay(ex, ey, mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, hilightWallTrc, (GridTagWall or GridTagDoor)) <> nil) then
begin
- drawLine(mx+mw div 2, my+mh div 2, ex, ey, 0, 255, 0, 128);
+ drawLine(mx+mw div 2, my+mh div 2, ex, ey, 0, 255, 0, 255);
end;
+ {$IF DEFINED(D2F_DEBUG)}
+ //mapGrid.dbgRayTraceTileHitCB := nil;
+ {$ENDIF}
end;
begin
end;
drawMonsterTargetLine();
+ if showMonsLOS2Plr then drawLOS2Plr();
{
property MonsterRemoved: Boolean read FRemoved write FRemoved;
property MonsterPain: Integer read FPain write FPain;
showMonsInfo := not showMonsInfo;
exit;
end;
+ // M-L: toggle monster LOS to player
+ if (ev.scan = SDL_SCANCODE_L) and ((ev.kstate and THKeyEvent.ModAlt) <> 0) then
+ begin
+ result := true;
+ showMonsLOS2Plr := not showMonsLOS2Plr;
+ exit;
+ end;
end;
end;