From 31fadda69508ddb1f225a2a471c1be888148ffac Mon Sep 17 00:00:00 2001 From: Ketmar Dark Date: Sat, 19 Aug 2017 15:20:45 +0300 Subject: [PATCH] slightly better memory locality --- src/game/g_grid.pas | 10 +++++----- src/game/g_map.pas | 26 ++++++++++++-------------- src/game/z_aabbtree.pas | 11 ++++++----- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/game/g_grid.pas b/src/game/g_grid.pas index 3ed6612..5b2f8b1 100644 --- a/src/game/g_grid.pas +++ b/src/game/g_grid.pas @@ -25,7 +25,7 @@ const GridCellBucketSize = 8; // WARNING! can't be less than 2! type - GridQueryCB = function (obj: TObject; var proxy: PBodyProxyRec; tag: Integer): Boolean is nested; // return `true` to stop + TGridQueryCB = function (obj: TObject; objx, objy, objw, objh: Integer; tag: Integer): Boolean is nested; // return `true` to stop type TBodyGrid = class; @@ -108,7 +108,7 @@ type procedure resizeBody (body: TBodyProxy; sx, sy: Integer); procedure moveResizeBody (body: TBodyProxy; dx, dy, sx, sy: Integer); - function forEachInAABB (x, y, w, h: Integer; cb: GridQueryCB; tagmask: Integer=-1): Boolean; + function forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1): Boolean; //function getProxyForBody (aObj: TObject; x, y, w, h: Integer): TBodyProxy; @@ -450,7 +450,7 @@ begin end; -function TBodyGrid.forEachInAABB (x, y, w, h: Integer; cb: GridQueryCB; tagmask: Integer=-1): Boolean; +function TBodyGrid.forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1): Boolean; function iterator (grida: Integer): Boolean; var idx: Integer; @@ -474,7 +474,7 @@ function TBodyGrid.forEachInAABB (x, y, w, h: Integer; cb: GridQueryCB; tagmask: begin //e_WriteLog(Format(' query #%d body hit: (%d,%d)-(%dx%d) tag:%d', [mLastQuery, mCells[idx].body.mX, mCells[idx].body.mY, mCells[idx].body.mWidth, mCells[idx].body.mHeight, mCells[idx].body.mTag]), MSG_NOTIFY); px.mQueryMark := mLastQuery; - if (cb(px.mObj, px.mTag)) then begin result := true; exit; end; + if (cb(px.mObj, px.mX, px.mY, px.mWidth, px.mHeight, px.mTag)) then begin result := true; exit; end; end; end; idx := pi.next; @@ -486,7 +486,7 @@ function TBodyGrid.forEachInAABB (x, y, w, h: Integer; cb: GridQueryCB; tagmask: begin //e_WriteLog(Format(' query #%d body hit: (%d,%d)-(%dx%d) tag:%d', [mLastQuery, mCells[idx].body.mX, mCells[idx].body.mY, mCells[idx].body.mWidth, mCells[idx].body.mHeight, mCells[idx].body.mTag]), MSG_NOTIFY); px.mQueryMark := mLastQuery; - if (cb(px.mObj, px.mTag)) then begin result := true; exit; end; + if (cb(px.mObj, px.mX, px.mY, px.mWidth, px.mHeight, px.mTag)) then begin result := true; exit; end; end; end; idx := mCells[idx].next; diff --git a/src/game/g_map.pas b/src/game/g_map.pas index dd2f455..85d5510 100644 --- a/src/game/g_map.pas +++ b/src/game/g_map.pas @@ -1924,7 +1924,7 @@ end; // new algo procedure g_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer); - function checker (obj: TObject; ; var proxy: PBodyProxyRectag: Integer): Boolean; + function checker (obj: TObject; objx, objy, objw, objh: Integer; tag: Integer): Boolean; var pan: TPanel; begin @@ -1962,7 +1962,7 @@ end; procedure g_Map_DrawPanelShadowVolumes(lightX: Integer; lightY: Integer; radius: Integer); - function checker (obj: TObject; ; var proxy: PBodyProxyRectag: Integer): Boolean; + function checker (obj: TObject; objx, objy, objw, objh: Integer; tag: Integer): Boolean; var pan: TPanel; begin @@ -2146,7 +2146,7 @@ end; function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; b1x3: Boolean): Boolean; - function checker (obj: TObject; ; var proxy: PBodyProxyRectag: Integer): Boolean; + function checker (obj: TObject; objx, objy, objwidth, objheight: Integer; tag: Integer): Boolean; var pan: TPanel; begin @@ -2159,16 +2159,16 @@ function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; (WordBool(PanelType and (PANEL_LIFTDOWN)) and (pan.LiftType = 1)) or (WordBool(PanelType and (PANEL_LIFTLEFT)) and (pan.LiftType = 2)) or (WordBool(PanelType and (PANEL_LIFTRIGHT)) and (pan.LiftType = 3))) and - g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then + g_Collide(X, Y, Width, Height, objx, objy, objwidth, objheight) then begin result := true; exit; end; end; - if ((pan.tag and GridTagBlockMon) <> 0) then + if ((tag and GridTagBlockMon) <> 0) then begin - if ((not b1x3) or (pan.Width+pan.Height >= 64)) and g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then + if ((not b1x3) or (objwidth+objheight >= 64)) and g_Collide(X, Y, Width, Height, objx, objy, objwidth, objheight) then begin result := True; exit; @@ -2176,7 +2176,7 @@ function g_Map_CollidePanel(X, Y: Integer; Width, Height: Word; PanelType: Word; end; // other shit - result := g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height); + result := g_Collide(X, Y, Width, Height, objx, objy, objwidth, objheight); end; var @@ -2225,14 +2225,11 @@ var texid: DWORD; // slightly different from the old code, but meh... - function checker (obj: TObject; tag: Integer): Boolean; - var - pan: TPanel; + function checker (obj: TObject; objx, objy, objwidth, objheight: Integer; tag: Integer): Boolean; + //var pan: TPanel; begin result := false; // don't stop, ever if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2)) = 0) then exit; - //pan := (obj as TPanel); - pan := TPanel(obj); // check priorities case cctype of 0: if ((tag and GridTagWater) = 0) then exit; // only water @@ -2240,9 +2237,10 @@ var //2: if ((tag and (GridTagWater or GridTagAcid1 or GridTagAcid2) = 0) then exit; // water, acid1, acid2 end; // collision? - if not g_Collide(X, Y, Width, Height, pan.X, pan.Y, pan.Width, pan.Height) then exit; + if not g_Collide(X, Y, Width, Height, objx, objy, objwidth, objheight) then exit; // yeah - texid := pan.GetTextureID(); + //pan := (obj as TPanel); + texid := TPanel(obj).GetTextureID(); // water? water has the highest priority, so stop right here if ((tag and GridTagWater) <> 0) then begin cctype := 0; result := true; exit; end; // acid2 diff --git a/src/game/z_aabbtree.pas b/src/game/z_aabbtree.pas index 7d346de..31b6dd4 100644 --- a/src/game/z_aabbtree.pas +++ b/src/game/z_aabbtree.pas @@ -21,7 +21,8 @@ unit z_aabbtree; interface -uses e_log; +uses + e_log, g_grid; // ////////////////////////////////////////////////////////////////////////// // @@ -194,7 +195,7 @@ type public // called when a overlapping node has been found during the call to forEachAABBOverlap() // return `true` to stop - type TQueryOverlapCB = function (abody: TTreeFlesh; atag: Integer): Boolean is nested; + type TQueryOverlapCB = function (abody: TTreeFlesh; objx, objy, objw, objh: Integer; atag: Integer): Boolean is nested; type TSegQueryCallback = function (abody: TTreeFlesh; ax, ay, bx, by: Single): Single is nested; // return dist from (ax,ay) to abody TSegmentQueryResult = record @@ -1248,7 +1249,7 @@ begin {$IFDEF aabbtree_query_count}Inc(mNodesDeepVisited);{$ENDIF} if ((node.tag and tagmask) <> 0) then begin - if (visitor(node.flesh, node.tag)) then begin result := nodeId; bigstack := nil; exit; end; + if (visitor(node.flesh, node.aabb.minX, node.aabb.minY, node.aabb.maxX, node.aabb.maxY, node.tag)) then begin result := nodeId; bigstack := nil; exit; end; end; end else @@ -1457,7 +1458,7 @@ function TDynAABBTree.pointQuery (ax, ay: TreeNumber; cb: TQueryOverlapCB): TTre begin result := node.aabb.contains(ax, ay); end; - function dummycb (abody: TTreeFlesh; atag: Integer): Boolean; begin result := false; end; + function dummycb (abody: TTreeFlesh; objx, objy, objw, objh: Integer; atag: Integer): Boolean; begin result := false; end; var nid: Integer; caabb: AABB2D; @@ -1485,7 +1486,7 @@ var result := node.aabb.intersects(curax, curay, curbx, curby); end; - function visitor (flesh: TTreeFlesh; tag: Integer): Boolean; + function visitor (flesh: TTreeFlesh; objx, objy, objw, objh: Integer; tag: Integer): Boolean; var hitFraction: Single; begin -- 2.29.2