DEADSOFTWARE

17ad2fceed471807171d121fac437b57837e099c
[d2df-sdl.git] / src / game / g_grid.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 // universal spatial grid
17 {$INCLUDE ../shared/a_modes.inc}
18 {$IF DEFINED(D2F_DEBUG)}
19 {.$DEFINE D2F_DEBUG_RAYTRACE}
20 {.$DEFINE D2F_DEBUG_XXQ}
21 {.$DEFINE D2F_DEBUG_MOVER}
22 {$ENDIF}
23 {$DEFINE GRID_USE_ORTHO_ACCEL}
24 unit g_grid;
26 interface
29 type
30 TBodyProxyId = Integer;
32 generic TBodyGridBase<ITP> = class(TObject)
33 public
34 type TGridQueryCB = function (obj: ITP; tag: Integer): Boolean is nested; // return `true` to stop
35 type TGridRayQueryCB = function (obj: ITP; tag: Integer; x, y, prevx, prevy: Integer): Boolean is nested; // return `true` to stop
37 type TCellQueryCB = procedure (x, y: Integer) is nested; // top-left cell corner coords
39 const TagDisabled = $40000000;
40 const TagFullMask = $3fffffff;
42 private
43 const
44 GridDefaultTileSize = 32; // must be power of two!
45 GridCellBucketSize = 8; // WARNING! can't be less than 2!
47 public
48 type
49 PBodyProxyRec = ^TBodyProxyRec;
50 TBodyProxyRec = record
51 private
52 mX, mY, mWidth, mHeight: Integer; // aabb
53 mQueryMark: LongWord; // was this object visited at this query?
54 mObj: ITP;
55 mTag: Integer; // `TagDisabled` set: disabled ;-)
56 nextLink: TBodyProxyId; // next free or nothing
58 private
59 procedure setup (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer);
61 function getTag (): Integer; inline;
62 procedure setTag (v: Integer); inline;
64 function getEnabled (): Boolean; inline;
65 procedure setEnabled (v: Boolean); inline;
67 function getX1 (): Integer; inline;
68 function getY1 (): Integer; inline;
70 public
71 property x: Integer read mX;
72 property y: Integer read mY;
73 property width: Integer read mWidth;
74 property height: Integer read mHeight;
75 property tag: Integer read getTag write setTag;
76 property enabled: Boolean read getEnabled write setEnabled;
77 property obj: ITP read mObj;
79 property x0: Integer read mX;
80 property y0: Integer read mY;
81 property x1: Integer read getX1;
82 property y1: Integer read getY1;
83 end;
85 private
86 type
87 PGridCell = ^TGridCell;
88 TGridCell = record
89 bodies: array [0..GridCellBucketSize-1] of Integer; // -1: end of list
90 next: Integer; // in this cell; index in mCells
91 end;
93 TCellArray = array of TGridCell;
95 TGridInternalCB = function (grida: Integer; bodyId: TBodyProxyId): Boolean of object; // return `true` to stop
97 private
98 //mTileSize: Integer;
99 const mTileSize = GridDefaultTileSize;
100 type TGetProxyFn = function (pxidx: Integer): PBodyProxyRec of object;
102 public
103 const tileSize = mTileSize;
105 type
106 TAtPointEnumerator = record
107 private
108 mCells: TCellArray;
109 curidx, curbki: Integer;
110 getpx: TGetProxyFn;
111 public
112 constructor Create (acells: TCellArray; aidx: Integer; agetpx: TGetProxyFn);
113 function MoveNext (): Boolean; inline;
114 function getCurrent (): PBodyProxyRec; inline;
115 property Current: PBodyProxyRec read getCurrent;
116 end;
118 private
119 mMinX, mMinY: Integer; // so grids can start at any origin
120 mWidth, mHeight: Integer; // in tiles
121 mGrid: array of Integer; // mWidth*mHeight, index in mCells
122 mCells: TCellArray; // cell pool
123 mFreeCell: Integer; // first free cell index or -1
124 mLastQuery: LongWord;
125 mUsedCells: Integer;
126 mProxies: array of TBodyProxyRec;
127 mProxyFree: TBodyProxyId; // free
128 mProxyCount: Integer; // currently used
129 mProxyMaxCount: Integer;
130 mInQuery: Boolean;
132 public
133 dbgShowTraceLog: Boolean;
134 {$IF DEFINED(D2F_DEBUG)}
135 dbgRayTraceTileHitCB: TCellQueryCB;
136 {$ENDIF}
138 private
139 function allocCell (): Integer;
140 procedure freeCell (idx: Integer); // `next` is simply overwritten
142 function allocProxy (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer): TBodyProxyId;
143 procedure freeProxy (body: TBodyProxyId);
145 procedure insertInternal (body: TBodyProxyId);
146 procedure removeInternal (body: TBodyProxyId);
148 function forGridRect (x, y, w, h: Integer; cb: TGridInternalCB; bodyId: TBodyProxyId): Boolean;
150 function inserter (grida: Integer; bodyId: TBodyProxyId): Boolean;
151 function remover (grida: Integer; bodyId: TBodyProxyId): Boolean;
153 function getProxyEnabled (pid: TBodyProxyId): Boolean; inline;
154 procedure setProxyEnabled (pid: TBodyProxyId; val: Boolean); inline;
156 function getGridWidthPx (): Integer; inline;
157 function getGridHeightPx (): Integer; inline;
159 function getProxyById (idx: TBodyProxyId): PBodyProxyRec; inline;
161 public
162 constructor Create (aMinPixX, aMinPixY, aPixWidth, aPixHeight: Integer{; aTileSize: Integer=GridDefaultTileSize});
163 destructor Destroy (); override;
165 function insertBody (aObj: ITP; ax, ay, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId;
166 procedure removeBody (body: TBodyProxyId); // WARNING! this WILL destroy proxy!
168 procedure moveBody (body: TBodyProxyId; nx, ny: Integer);
169 procedure resizeBody (body: TBodyProxyId; nw, nh: Integer);
170 procedure moveResizeBody (body: TBodyProxyId; nx, ny, nw, nh: Integer);
172 function insideGrid (x, y: Integer): Boolean; inline;
174 // `false` if `body` is surely invalid
175 function getBodyXY (body: TBodyProxyId; out rx, ry: Integer): Boolean; inline;
176 function getBodyWH (body: TBodyProxyId; out rw, rh: Integer): Boolean; inline;
177 function getBodyDims (body: TBodyProxyId; out rx, ry, rw, rh: Integer): Boolean; inline;
179 //WARNING: don't modify grid while any query is in progress (no checks are made!)
180 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
181 // no callback: return `true` on the first hit
182 function forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP;
184 //WARNING: don't modify grid while any query is in progress (no checks are made!)
185 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
186 // no callback: return object on the first hit or nil
187 function forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1; exittag: PInteger=nil): ITP;
189 function atCellInPoint (x, y: Integer): TAtPointEnumerator;
191 //WARNING: don't modify grid while any query is in progress (no checks are made!)
192 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
193 // cb with `(nil)` will be called before processing new tile
194 // no callback: return object of the nearest hit or nil
195 // if `inverted` is true, trace will register bodies *exluding* tagmask
196 //WARNING: don't change tags in callbacks here!
197 function traceRay (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP; overload;
198 function traceRay (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
200 // return `false` if we're still inside at the end
201 // line should be either strict horizontal, or strict vertical, otherwise an exception will be thrown
202 function traceOrthoRayWhileIn (out ex, ey: Integer; ax0, ay0, ax1, ay1: Integer; tagmask: Integer=-1): Boolean;
204 //WARNING: don't modify grid while any query is in progress (no checks are made!)
205 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
206 // trace line along the grid, calling `cb` for all objects in passed cells, in no particular order
207 //WARNING: don't change tags in callbacks here!
208 function forEachAlongLine (ax0, ay0, ax1, ay1: Integer; cb: TGridQueryCB; tagmask: Integer=-1; log: Boolean=false): ITP;
210 // debug
211 procedure forEachBodyCell (body: TBodyProxyId; cb: TCellQueryCB);
212 function forEachInCell (x, y: Integer; cb: TGridQueryCB): ITP;
213 procedure dumpStats ();
215 public
216 //WARNING! no sanity checks!
217 property proxyEnabled[pid: TBodyProxyId]: Boolean read getProxyEnabled write setProxyEnabled;
219 property gridX0: Integer read mMinX;
220 property gridY0: Integer read mMinY;
221 property gridWidth: Integer read getGridWidthPx; // in pixels
222 property gridHeight: Integer read getGridHeightPx; // in pixels
224 property proxy[idx: TBodyProxyId]: PBodyProxyRec read getProxyById;
225 end;
228 // you are not supposed to understand this
229 // returns `true` if there is an intersection, and enter coords
230 // enter coords will be equal to (x0, y0) if starting point is inside the box
231 // if result is `false`, `inx` and `iny` are undefined
232 function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
234 function distanceSq (x0, y0, x1, y1: Integer): Integer; inline;
236 procedure swapInt (var a: Integer; var b: Integer); inline;
237 function minInt (a, b: Integer): Integer; inline;
238 function maxInt (a, b: Integer): Integer; inline;
241 implementation
243 uses
244 SysUtils, e_log, g_console, utils;
247 // ////////////////////////////////////////////////////////////////////////// //
248 procedure swapInt (var a: Integer; var b: Integer); inline; var t: Integer; begin t := a; a := b; b := t; end;
249 function minInt (a, b: Integer): Integer; inline; begin if (a < b) then result := a else result := b; end;
250 function maxInt (a, b: Integer): Integer; inline; begin if (a > b) then result := a else result := b; end;
252 function distanceSq (x0, y0, x1, y1: Integer): Integer; inline; begin result := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0); end;
255 // ////////////////////////////////////////////////////////////////////////// //
256 // you are not supposed to understand this
257 // returns `true` if there is an intersection, and enter coords
258 // enter coords will be equal to (x0, y0) if starting point is inside the box
259 // if result is `false`, `inx` and `iny` are undefined
260 function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
261 var
262 wx0, wy0, wx1, wy1: Integer; // window coordinates
263 stx, sty: Integer; // "steps" for x and y axes
264 dsx, dsy: Integer; // "lengthes" for x and y axes
265 dx2, dy2: Integer; // "double lengthes" for x and y axes
266 xd, yd: Integer; // current coord
267 e: Integer; // "error" (as in bresenham algo)
268 rem: Integer;
269 //!term: Integer;
270 d0, d1: PInteger;
271 xfixed: Boolean;
272 temp: Integer;
273 begin
274 result := false;
275 // why not
276 inx := x0;
277 iny := y0;
278 if (bw < 1) or (bh < 1) then exit; // impossible box
280 if (x0 = x1) and (y0 = y1) then
281 begin
282 // check this point
283 result := (x0 >= bx) and (y0 >= by) and (x0 < bx+bw) and (y0 < by+bh);
284 exit;
285 end;
287 // check if staring point is inside the box
288 if (x0 >= bx) and (y0 >= by) and (x0 < bx+bw) and (y0 < by+bh) then begin result := true; exit; end;
290 // clip rectange
291 wx0 := bx;
292 wy0 := by;
293 wx1 := bx+bw-1;
294 wy1 := by+bh-1;
296 // horizontal setup
297 if (x0 < x1) then
298 begin
299 // from left to right
300 if (x0 > wx1) or (x1 < wx0) then exit; // out of screen
301 stx := 1; // going right
302 end
303 else
304 begin
305 // from right to left
306 if (x1 > wx1) or (x0 < wx0) then exit; // out of screen
307 stx := -1; // going left
308 x0 := -x0;
309 x1 := -x1;
310 wx0 := -wx0;
311 wx1 := -wx1;
312 swapInt(wx0, wx1);
313 end;
315 // vertical setup
316 if (y0 < y1) then
317 begin
318 // from top to bottom
319 if (y0 > wy1) or (y1 < wy0) then exit; // out of screen
320 sty := 1; // going down
321 end
322 else
323 begin
324 // from bottom to top
325 if (y1 > wy1) or (y0 < wy0) then exit; // out of screen
326 sty := -1; // going up
327 y0 := -y0;
328 y1 := -y1;
329 wy0 := -wy0;
330 wy1 := -wy1;
331 swapInt(wy0, wy1);
332 end;
334 dsx := x1-x0;
335 dsy := y1-y0;
337 if (dsx < dsy) then
338 begin
339 d0 := @yd;
340 d1 := @xd;
341 swapInt(x0, y0);
342 swapInt(x1, y1);
343 swapInt(dsx, dsy);
344 swapInt(wx0, wy0);
345 swapInt(wx1, wy1);
346 swapInt(stx, sty);
347 end
348 else
349 begin
350 d0 := @xd;
351 d1 := @yd;
352 end;
354 dx2 := 2*dsx;
355 dy2 := 2*dsy;
356 xd := x0;
357 yd := y0;
358 e := 2*dsy-dsx;
359 //!term := x1;
361 xfixed := false;
362 if (y0 < wy0) then
363 begin
364 // clip at top
365 temp := dx2*(wy0-y0)-dsx;
366 xd += temp div dy2;
367 rem := temp mod dy2;
368 if (xd > wx1) then exit; // x is moved out of clipping rect, nothing to do
369 if (xd+1 >= wx0) then
370 begin
371 yd := wy0;
372 e -= rem+dsx;
373 if (rem > 0) then begin Inc(xd); e += dy2; end;
374 xfixed := true;
375 end;
376 end;
378 if (not xfixed) and (x0 < wx0) then
379 begin
380 // clip at left
381 temp := dy2*(wx0-x0);
382 yd += temp div dx2;
383 rem := temp mod dx2;
384 if (yd > wy1) or (yd = wy1) and (rem >= dsx) then exit;
385 xd := wx0;
386 e += rem;
387 if (rem >= dsx) then begin Inc(yd); e -= dx2; end;
388 end;
390 (*
391 if (y1 > wy1) then
392 begin
393 // clip at bottom
394 temp := dx2*(wy1-y0)+dsx;
395 term := x0+temp div dy2;
396 rem := temp mod dy2;
397 if (rem = 0) then Dec(term);
398 end;
400 if (term > wx1) then term := wx1; // clip at right
402 Inc(term); // draw last point
403 //if (term = xd) then exit; // this is the only point, get out of here
404 *)
406 if (sty = -1) then yd := -yd;
407 if (stx = -1) then begin xd := -xd; {!term := -term;} end;
408 //!dx2 -= dy2;
410 inx := d0^;
411 iny := d1^;
412 result := true;
413 end;
416 // ////////////////////////////////////////////////////////////////////////// //
417 procedure TBodyGridBase.TBodyProxyRec.setup (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer);
418 begin
419 mX := aX;
420 mY := aY;
421 mWidth := aWidth;
422 mHeight := aHeight;
423 mQueryMark := 0;
424 mObj := aObj;
425 mTag := aTag;
426 nextLink := -1;
427 end;
430 function TBodyGridBase.TBodyProxyRec.getTag (): Integer; inline;
431 begin
432 result := mTag and TagFullMask;
433 end;
435 procedure TBodyGridBase.TBodyProxyRec.setTag (v: Integer); inline;
436 begin
437 mTag := (mTag and TagDisabled) or (v and TagFullMask);
438 end;
440 function TBodyGridBase.TBodyProxyRec.getEnabled (): Boolean; inline;
441 begin
442 result := ((mTag and TagDisabled) = 0);
443 end;
445 procedure TBodyGridBase.TBodyProxyRec.setEnabled (v: Boolean); inline;
446 begin
447 if v then mTag := mTag and (not TagDisabled) else mTag := mTag or TagDisabled;
448 end;
450 function TBodyGridBase.TBodyProxyRec.getX1 (): Integer; inline;
451 begin
452 result := mX+mWidth-1;
453 end;
455 function TBodyGridBase.TBodyProxyRec.getY1 (): Integer; inline;
456 begin
457 result := mY+mHeight-1;
458 end;
461 // ////////////////////////////////////////////////////////////////////////// //
462 constructor TBodyGridBase.TAtPointEnumerator.Create (acells: TCellArray; aidx: Integer; agetpx: TGetProxyFn);
463 begin
464 mCells := acells;
465 curidx := aidx;
466 curbki := -1;
467 getpx := agetpx;
468 end;
471 function TBodyGridBase.TAtPointEnumerator.MoveNext (): Boolean; inline;
472 begin
473 while (curidx <> -1) do
474 begin
475 while (curbki < GridCellBucketSize) do
476 begin
477 Inc(curbki);
478 if (mCells[curidx].bodies[curbki] = -1) then break;
479 result := true;
480 exit;
481 end;
482 curidx := mCells[curidx].next;
483 curbki := -1;
484 end;
485 result := false;
486 end;
489 function TBodyGridBase.TAtPointEnumerator.getCurrent (): PBodyProxyRec; inline;
490 begin
491 result := getpx(mCells[curidx].bodies[curbki]);
492 end;
495 // ////////////////////////////////////////////////////////////////////////// //
496 constructor TBodyGridBase.Create (aMinPixX, aMinPixY, aPixWidth, aPixHeight: Integer{; aTileSize: Integer=GridDefaultTileSize});
497 var
498 idx: Integer;
499 begin
500 dbgShowTraceLog := false;
501 {$IF DEFINED(D2F_DEBUG)}
502 dbgRayTraceTileHitCB := nil;
503 {$ENDIF}
505 if aTileSize < 1 then aTileSize := 1;
506 if aTileSize > 8192 then aTileSize := 8192; // arbitrary limit
507 mTileSize := aTileSize;
509 if (aPixWidth < mTileSize) then aPixWidth := mTileSize;
510 if (aPixHeight < mTileSize) then aPixHeight := mTileSize;
511 mMinX := aMinPixX;
512 mMinY := aMinPixY;
513 mWidth := (aPixWidth+mTileSize-1) div mTileSize;
514 mHeight := (aPixHeight+mTileSize-1) div mTileSize;
515 SetLength(mGrid, mWidth*mHeight);
516 SetLength(mCells, mWidth*mHeight);
517 SetLength(mProxies, 8192);
518 mFreeCell := 0;
519 // init free list
520 for idx := 0 to High(mCells) do
521 begin
522 mCells[idx].bodies[0] := -1;
523 mCells[idx].bodies[GridCellBucketSize-1] := -1; // "has free room" flag
524 mCells[idx].next := idx+1;
525 end;
526 mCells[High(mCells)].next := -1; // last cell
527 // init grid
528 for idx := 0 to High(mGrid) do mGrid[idx] := -1;
529 // init proxies
530 for idx := 0 to High(mProxies) do mProxies[idx].nextLink := idx+1;
531 mProxies[High(mProxies)].nextLink := -1;
532 mLastQuery := 0;
533 mUsedCells := 0;
534 mProxyFree := 0;
535 mProxyCount := 0;
536 mProxyMaxCount := 0;
537 e_WriteLog(Format('created grid with size: %dx%d (tile size: %d); pix: %dx%d', [mWidth, mHeight, mTileSize, mWidth*mTileSize, mHeight*mTileSize]), MSG_NOTIFY);
538 end;
541 destructor TBodyGridBase.Destroy ();
542 begin
543 mCells := nil;
544 mGrid := nil;
545 mProxies := nil;
546 inherited;
547 end;
550 // ////////////////////////////////////////////////////////////////////////// //
551 procedure TBodyGridBase.dumpStats ();
552 var
553 idx, mcb, cidx, cnt: Integer;
554 begin
555 mcb := 0;
556 for idx := 0 to High(mGrid) do
557 begin
558 cidx := mGrid[idx];
559 cnt := 0;
560 while cidx >= 0 do
561 begin
562 Inc(cnt);
563 cidx := mCells[cidx].next;
564 end;
565 if (mcb < cnt) then mcb := cnt;
566 end;
567 e_WriteLog(Format('grid size: %dx%d (tile size: %d); pix: %dx%d; used cells: %d; max bodies in cell: %d; max proxies allocated: %d; proxies used: %d', [mWidth, mHeight, mTileSize, mWidth*mTileSize, mHeight*mTileSize, mUsedCells, mcb, mProxyMaxCount, mProxyCount]), MSG_NOTIFY);
568 end;
571 procedure TBodyGridBase.forEachBodyCell (body: TBodyProxyId; cb: TCellQueryCB);
572 var
573 g, f, cidx: Integer;
574 cc: PGridCell;
575 begin
576 if (body < 0) or (body > High(mProxies)) or not assigned(cb) then exit;
577 for g := 0 to High(mGrid) do
578 begin
579 cidx := mGrid[g];
580 while (cidx <> -1) do
581 begin
582 cc := @mCells[cidx];
583 for f := 0 to GridCellBucketSize-1 do
584 begin
585 if (cc.bodies[f] = -1) then break;
586 if (cc.bodies[f] = body) then cb((g mod mWidth)*mTileSize+mMinX, (g div mWidth)*mTileSize+mMinY);
587 end;
588 // next cell
589 cidx := cc.next;
590 end;
591 end;
592 end;
595 function TBodyGridBase.forEachInCell (x, y: Integer; cb: TGridQueryCB): ITP;
596 var
597 f, cidx: Integer;
598 cc: PGridCell;
599 begin
600 result := Default(ITP);
601 if not assigned(cb) then exit;
602 Dec(x, mMinX);
603 Dec(y, mMinY);
604 if (x < 0) or (y < 0) or (x >= mWidth*mTileSize) or (y > mHeight*mTileSize) then exit;
605 cidx := mGrid[(y div mTileSize)*mWidth+(x div mTileSize)];
606 while (cidx <> -1) do
607 begin
608 cc := @mCells[cidx];
609 for f := 0 to GridCellBucketSize-1 do
610 begin
611 if (cc.bodies[f] = -1) then break;
612 if cb(mProxies[cc.bodies[f]].mObj, mProxies[cc.bodies[f]].mTag) then begin result := mProxies[cc.bodies[f]].mObj; exit; end;
613 end;
614 // next cell
615 cidx := cc.next;
616 end;
617 end;
620 // ////////////////////////////////////////////////////////////////////////// //
621 function TBodyGridBase.getGridWidthPx (): Integer; inline; begin result := mWidth*mTileSize; end;
622 function TBodyGridBase.getGridHeightPx (): Integer; inline; begin result := mHeight*mTileSize; end;
625 function TBodyGridBase.insideGrid (x, y: Integer): Boolean; inline;
626 begin
627 // fix coords
628 Dec(x, mMinX);
629 Dec(y, mMinY);
630 result := (x >= 0) and (y >= 0) and (x < mWidth*mTileSize) and (y < mHeight*mTileSize);
631 end;
634 function TBodyGridBase.getBodyXY (body: TBodyProxyId; out rx, ry: Integer): Boolean; inline;
635 begin
636 if (body >= 0) and (body < Length(mProxies)) then
637 begin
638 with mProxies[body] do begin rx := mX; ry := mY; end;
639 result := true;
640 end
641 else
642 begin
643 rx := 0;
644 ry := 0;
645 result := false;
646 end;
647 end;
650 function TBodyGridBase.getBodyWH (body: TBodyProxyId; out rw, rh: Integer): Boolean; inline;
651 begin
652 if (body >= 0) and (body < Length(mProxies)) then
653 begin
654 with mProxies[body] do begin rw := mWidth; rh := mHeight; end;
655 result := true;
656 end
657 else
658 begin
659 rw := 0;
660 rh := 0;
661 result := false;
662 end;
663 end;
666 function TBodyGridBase.getBodyDims (body: TBodyProxyId; out rx, ry, rw, rh: Integer): Boolean; inline;
667 begin
668 if (body >= 0) and (body < Length(mProxies)) then
669 begin
670 with mProxies[body] do begin rx := mX; ry := mY; rw := mWidth; rh := mHeight; end;
671 result := true;
672 end
673 else
674 begin
675 rx := 0;
676 ry := 0;
677 rw := 0;
678 rh := 0;
679 result := false;
680 end;
681 end;
685 // ////////////////////////////////////////////////////////////////////////// //
686 function TBodyGridBase.getProxyEnabled (pid: TBodyProxyId): Boolean; inline;
687 begin
688 if (pid >= 0) and (pid < Length(mProxies)) then result := ((mProxies[pid].mTag and TagDisabled) = 0) else result := false;
689 end;
692 procedure TBodyGridBase.setProxyEnabled (pid: TBodyProxyId; val: Boolean); inline;
693 begin
694 if (pid >= 0) and (pid < Length(mProxies)) then
695 begin
696 if val then
697 begin
698 mProxies[pid].mTag := mProxies[pid].mTag and not TagDisabled;
699 end
700 else
701 begin
702 mProxies[pid].mTag := mProxies[pid].mTag or TagDisabled;
703 end;
704 end;
705 end;
708 function TBodyGridBase.getProxyById (idx: TBodyProxyId): PBodyProxyRec; inline;
709 begin
710 if (idx >= 0) and (idx < Length(mProxies)) then result := @mProxies[idx] else result := nil;
711 end;
714 // ////////////////////////////////////////////////////////////////////////// //
715 function TBodyGridBase.allocCell (): Integer;
716 var
717 idx: Integer;
718 pc: PGridCell;
719 begin
720 if (mFreeCell < 0) then
721 begin
722 // no free cells, want more
723 mFreeCell := Length(mCells);
724 SetLength(mCells, mFreeCell+32768); // arbitrary number
725 for idx := mFreeCell to High(mCells) do
726 begin
727 mCells[idx].bodies[0] := -1;
728 mCells[idx].bodies[GridCellBucketSize-1] := -1; // 'has free room' flag
729 mCells[idx].next := idx+1;
730 end;
731 mCells[High(mCells)].next := -1; // last cell
732 end;
733 result := mFreeCell;
734 pc := @mCells[result];
735 mFreeCell := pc.next;
736 pc.next := -1;
737 Inc(mUsedCells);
738 //e_WriteLog(Format('grid: allocated new cell #%d (total: %d)', [result, mUsedCells]), MSG_NOTIFY);
739 end;
742 procedure TBodyGridBase.freeCell (idx: Integer);
743 begin
744 if (idx >= 0) and (idx < Length(mCells)) then
745 begin
746 with mCells[idx] do
747 begin
748 bodies[0] := -1;
749 bodies[GridCellBucketSize-1] := -1; // 'has free room' flag
750 next := mFreeCell;
751 end;
752 mFreeCell := idx;
753 Dec(mUsedCells);
754 end;
755 end;
758 // ////////////////////////////////////////////////////////////////////////// //
759 function TBodyGridBase.allocProxy (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer): TBodyProxyId;
760 var
761 olen, idx: Integer;
762 px: PBodyProxyRec;
763 begin
764 if (mProxyFree = -1) then
765 begin
766 // no free proxies, resize list
767 olen := Length(mProxies);
768 SetLength(mProxies, olen+8192); // arbitrary number
769 for idx := olen to High(mProxies) do mProxies[idx].nextLink := idx+1;
770 mProxies[High(mProxies)].nextLink := -1;
771 mProxyFree := olen;
772 end;
773 // get one from list
774 result := mProxyFree;
775 px := @mProxies[result];
776 mProxyFree := px.nextLink;
777 px.setup(aX, aY, aWidth, aHeight, aObj, aTag);
778 // add to used list
779 px.nextLink := -1;
780 // statistics
781 Inc(mProxyCount);
782 if (mProxyMaxCount < mProxyCount) then mProxyMaxCount := mProxyCount;
783 end;
785 procedure TBodyGridBase.freeProxy (body: TBodyProxyId);
786 begin
787 if (body < 0) or (body > High(mProxies)) then exit; // just in case
788 if (mProxyCount = 0) then raise Exception.Create('wutafuuuuu in grid (no allocated proxies, what i should free now?)');
789 // add to free list
790 mProxies[body].mObj := nil;
791 mProxies[body].nextLink := mProxyFree;
792 mProxyFree := body;
793 Dec(mProxyCount);
794 end;
797 // ////////////////////////////////////////////////////////////////////////// //
798 function TBodyGridBase.forGridRect (x, y, w, h: Integer; cb: TGridInternalCB; bodyId: TBodyProxyId): Boolean;
799 const
800 tsize = mTileSize;
801 var
802 gx, gy: Integer;
803 gw, gh: Integer;
804 begin
805 result := false;
806 if (w < 1) or (h < 1) or not assigned(cb) then exit;
807 // fix coords
808 Dec(x, mMinX);
809 Dec(y, mMinY);
810 // go on
811 if (x+w <= 0) or (y+h <= 0) then exit;
812 gw := mWidth;
813 gh := mHeight;
814 //tsize := mTileSize;
815 if (x >= gw*tsize) or (y >= gh*tsize) then exit;
816 for gy := y div tsize to (y+h-1) div tsize do
817 begin
818 if (gy < 0) then continue;
819 if (gy >= gh) then break;
820 for gx := x div tsize to (x+w-1) div tsize do
821 begin
822 if (gx < 0) then continue;
823 if (gx >= gw) then break;
824 result := cb(gy*gw+gx, bodyId);
825 if result then exit;
826 end;
827 end;
828 end;
831 // ////////////////////////////////////////////////////////////////////////// //
832 function TBodyGridBase.inserter (grida: Integer; bodyId: TBodyProxyId): Boolean;
833 var
834 cidx: Integer;
835 pc: Integer;
836 pi: PGridCell;
837 f: Integer;
838 begin
839 result := false; // never stop
840 // add body to the given grid cell
841 pc := mGrid[grida];
842 if (pc <> -1) then
843 begin
844 {$IF DEFINED(D2F_DEBUG)}
845 cidx := pc;
846 while (cidx <> -1) do
847 begin
848 pi := @mCells[cidx];
849 for f := 0 to GridCellBucketSize-1 do
850 begin
851 if (pi.bodies[f] = -1) then break;
852 if (pi.bodies[f] = bodyId) then raise Exception.Create('trying to insert already inserted proxy');
853 end;
854 cidx := pi.next;
855 end;
856 {$ENDIF}
857 cidx := pc;
858 while (cidx <> -1) do
859 begin
860 pi := @mCells[cidx];
861 // check "has room" flag
862 if (pi.bodies[GridCellBucketSize-1] = -1) then
863 begin
864 // can add here
865 for f := 0 to GridCellBucketSize-1 do
866 begin
867 if (pi.bodies[f] = -1) then
868 begin
869 pi.bodies[f] := bodyId;
870 if (f+1 < GridCellBucketSize) then pi.bodies[f+1] := -1;
871 exit;
872 end;
873 end;
874 raise Exception.Create('internal error in grid inserter');
875 end;
876 // no room, go to next cell in list (if there is any)
877 cidx := pi.next;
878 end;
879 // no room in cells, add new cell to list
880 end;
881 // either no room, or no cell at all
882 cidx := allocCell();
883 pi := @mCells[cidx];
884 pi.bodies[0] := bodyId;
885 pi.bodies[1] := -1;
886 pi.next := pc;
887 mGrid[grida] := cidx;
888 end;
890 procedure TBodyGridBase.insertInternal (body: TBodyProxyId);
891 var
892 px: PBodyProxyRec;
893 begin
894 if (body < 0) or (body > High(mProxies)) then exit; // just in case
895 px := @mProxies[body];
896 forGridRect(px.mX, px.mY, px.mWidth, px.mHeight, inserter, body);
897 end;
900 // assume that we cannot have one object added to bucket twice
901 function TBodyGridBase.remover (grida: Integer; bodyId: TBodyProxyId): Boolean;
902 var
903 f, c: Integer;
904 pidx, cidx: Integer;
905 pc: PGridCell;
906 begin
907 result := false; // never stop
908 // find and remove cell
909 pidx := -1; // previous cell index
910 cidx := mGrid[grida]; // current cell index
911 while (cidx <> -1) do
912 begin
913 pc := @mCells[cidx];
914 for f := 0 to GridCellBucketSize-1 do
915 begin
916 if (pc.bodies[f] = bodyId) then
917 begin
918 // i found her!
919 if (f = 0) and (pc.bodies[1] = -1) then
920 begin
921 // this cell contains no elements, remove it
922 if (pidx = -1) then mGrid[grida] := pc.next else mCells[pidx].next := pc.next;
923 freeCell(cidx);
924 exit;
925 end;
926 // remove element from bucket
927 for c := f to GridCellBucketSize-2 do
928 begin
929 pc.bodies[c] := pc.bodies[c+1];
930 if (pc.bodies[c] = -1) then break;
931 end;
932 pc.bodies[GridCellBucketSize-1] := -1; // "has free room" flag
933 exit;
934 end;
935 end;
936 pidx := cidx;
937 cidx := pc.next;
938 end;
939 end;
941 procedure TBodyGridBase.removeInternal (body: TBodyProxyId);
942 var
943 px: PBodyProxyRec;
944 begin
945 if (body < 0) or (body > High(mProxies)) then exit; // just in case
946 px := @mProxies[body];
947 forGridRect(px.mX, px.mY, px.mWidth, px.mHeight, remover, body);
948 end;
951 // ////////////////////////////////////////////////////////////////////////// //
952 function TBodyGridBase.insertBody (aObj: ITP; aX, aY, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId;
953 begin
954 aTag := aTag and TagFullMask;
955 result := allocProxy(aX, aY, aWidth, aHeight, aObj, aTag);
956 insertInternal(result);
957 end;
960 procedure TBodyGridBase.removeBody (body: TBodyProxyId);
961 begin
962 if (body < 0) or (body > High(mProxies)) then exit; // just in case
963 removeInternal(body);
964 freeProxy(body);
965 end;
968 // ////////////////////////////////////////////////////////////////////////// //
969 procedure TBodyGridBase.moveResizeBody (body: TBodyProxyId; nx, ny, nw, nh: Integer);
970 var
971 px: PBodyProxyRec;
972 x0, y0, w, h: Integer;
973 begin
974 if (body < 0) or (body > High(mProxies)) then exit; // just in case
975 px := @mProxies[body];
976 x0 := px.mX;
977 y0 := px.mY;
978 w := px.mWidth;
979 h := px.mHeight;
980 {$IF DEFINED(D2F_DEBUG_MOVER)}
981 e_WriteLog(Format('proxy #%d: MOVERESIZE: xg=%d;yg=%d;w=%d;h=%d;nx=%d;ny=%d;nw=%d;nh=%d', [body, x0-mMinX, y0-mMinY, w, h, nx-mMinX, ny-mMinY, nw, nh]), MSG_NOTIFY);
982 {$ENDIF}
983 if (nx = x0) and (ny = y0) and (nw = w) and (nh = h) then exit;
984 // map -> grid
985 Dec(x0, mMinX);
986 Dec(y0, mMinY);
987 Dec(nx, mMinX);
988 Dec(ny, mMinY);
989 // did any corner crossed tile boundary?
990 if (x0 div mTileSize <> nx div mTileSize) or
991 (y0 div mTileSize <> ny div mTileSize) or
992 ((x0+w) div mTileSize <> (nx+nw) div mTileSize) or
993 ((y0+h) div mTileSize <> (ny+nh) div mTileSize) then
994 begin
995 removeInternal(body);
996 px.mX := nx+mMinX;
997 px.mY := ny+mMinY;
998 px.mWidth := nw;
999 px.mHeight := nh;
1000 insertInternal(body);
1001 end
1002 else
1003 begin
1004 px.mX := nx+mMinX;
1005 px.mY := ny+mMinY;
1006 px.mWidth := nw;
1007 px.mHeight := nh;
1008 end;
1009 end;
1011 //TODO: optimize for horizontal/vertical moves
1012 procedure TBodyGridBase.moveBody (body: TBodyProxyId; nx, ny: Integer);
1013 var
1014 px: PBodyProxyRec;
1015 x0, y0: Integer;
1016 ogx0, ogx1, ogy0, ogy1: Integer; // old grid rect
1017 ngx0, ngx1, ngy0, ngy1: Integer; // new grid rect
1018 gx, gy: Integer;
1019 gw, gh: Integer;
1020 pw, ph: Integer;
1021 begin
1022 if (body < 0) or (body > High(mProxies)) then exit; // just in case
1023 // check if tile coords was changed
1024 px := @mProxies[body];
1025 x0 := px.mX;
1026 y0 := px.mY;
1027 if (nx = x0) and (ny = y0) then exit;
1028 // map -> grid
1029 Dec(x0, mMinX);
1030 Dec(y0, mMinY);
1031 Dec(nx, mMinX);
1032 Dec(ny, mMinY);
1033 // check for heavy work
1034 pw := px.mWidth;
1035 ph := px.mHeight;
1036 ogx0 := x0 div mTileSize;
1037 ogy0 := y0 div mTileSize;
1038 ngx0 := nx div mTileSize;
1039 ngy0 := ny div mTileSize;
1040 ogx1 := (x0+pw-1) div mTileSize;
1041 ogy1 := (y0+ph-1) div mTileSize;
1042 ngx1 := (nx+pw-1) div mTileSize;
1043 ngy1 := (ny+ph-1) div mTileSize;
1044 {$IF DEFINED(D2F_DEBUG_MOVER)}
1045 e_WriteLog(Format('proxy #%d: checkmove: xg=%d;yg=%d;w=%d;h=%d;nx=%d;ny=%d og:(%d,%d)-(%d,%d); ng:(%d,%d)-(%d,%d)', [body, x0, y0, pw, ph, nx, ny, ogx0, ogy0, ogx1, ogy1, ngx0, ngy0, ngx1, ngy1]), MSG_NOTIFY);
1046 {$ENDIF}
1047 if (ogx0 <> ngx0) or (ogy0 <> ngy0) or (ogx1 <> ngx1) or (ogy1 <> ngy1) then
1048 begin
1049 // crossed tile boundary, do heavy work
1050 gw := mWidth;
1051 gh := mHeight;
1052 // cycle with old rect, remove body where it is necessary
1053 // optimized for horizontal moves
1054 {$IF DEFINED(D2F_DEBUG_MOVER)}
1055 e_WriteLog(Format('proxy #%d: xg=%d;yg=%d;w=%d;h=%d;nx=%d;ny=%d og:(%d,%d)-(%d,%d); ng:(%d,%d)-(%d,%d)', [body, x0, y0, pw, ph, nx, ny, ogx0, ogy0, ogx1, ogy1, ngx0, ngy0, ngx1, ngy1]), MSG_NOTIFY);
1056 {$ENDIF}
1057 // remove stale marks
1058 if not ((ogy0 >= gh) or (ogy1 < 0)) and
1059 not ((ogx0 >= gw) or (ogx1 < 0)) then
1060 begin
1061 if (ogx0 < 0) then ogx0 := 0;
1062 if (ogy0 < 0) then ogy0 := 0;
1063 if (ogx1 > gw-1) then ogx1 := gw-1;
1064 if (ogy1 > gh-1) then ogy1 := gh-1;
1065 {$IF DEFINED(D2F_DEBUG_MOVER)}
1066 e_WriteLog(Format(' norm og:(%d,%d)-(%d,%d)', [ogx0, ogy0, ogx1, ogy1]), MSG_NOTIFY);
1067 {$ENDIF}
1068 for gx := ogx0 to ogx1 do
1069 begin
1070 if (gx < ngx0) or (gx > ngx1) then
1071 begin
1072 // this column is completely outside of new rect
1073 for gy := ogy0 to ogy1 do
1074 begin
1075 {$IF DEFINED(D2F_DEBUG_MOVER)}
1076 e_WriteLog(Format(' remove0:(%d,%d)', [gx, gy]), MSG_NOTIFY);
1077 {$ENDIF}
1078 remover(gy*gw+gx, body);
1079 end;
1080 end
1081 else
1082 begin
1083 // heavy checks
1084 for gy := ogy0 to ogy1 do
1085 begin
1086 if (gy < ngy0) or (gy > ngy1) then
1087 begin
1088 {$IF DEFINED(D2F_DEBUG_MOVER)}
1089 e_WriteLog(Format(' remove1:(%d,%d)', [gx, gy]), MSG_NOTIFY);
1090 {$ENDIF}
1091 remover(gy*gw+gx, body);
1092 end;
1093 end;
1094 end;
1095 end;
1096 end;
1097 // cycle with new rect, add body where it is necessary
1098 if not ((ngy0 >= gh) or (ngy1 < 0)) and
1099 not ((ngx0 >= gw) or (ngx1 < 0)) then
1100 begin
1101 if (ngx0 < 0) then ngx0 := 0;
1102 if (ngy0 < 0) then ngy0 := 0;
1103 if (ngx1 > gw-1) then ngx1 := gw-1;
1104 if (ngy1 > gh-1) then ngy1 := gh-1;
1105 {$IF DEFINED(D2F_DEBUG_MOVER)}
1106 e_WriteLog(Format(' norm ng:(%d,%d)-(%d,%d)', [ngx0, ngy0, ngx1, ngy1]), MSG_NOTIFY);
1107 {$ENDIF}
1108 for gx := ngx0 to ngx1 do
1109 begin
1110 if (gx < ogx0) or (gx > ogx1) then
1111 begin
1112 // this column is completely outside of old rect
1113 for gy := ngy0 to ngy1 do
1114 begin
1115 {$IF DEFINED(D2F_DEBUG_MOVER)}
1116 e_WriteLog(Format(' insert0:(%d,%d)', [gx, gy]), MSG_NOTIFY);
1117 {$ENDIF}
1118 inserter(gy*gw+gx, body);
1119 end;
1120 end
1121 else
1122 begin
1123 // heavy checks
1124 for gy := ngy0 to ngy1 do
1125 begin
1126 if (gy < ogy0) or (gy > ogy1) then
1127 begin
1128 {$IF DEFINED(D2F_DEBUG_MOVER)}
1129 e_WriteLog(Format(' insert1:(%d,%d)', [gx, gy]), MSG_NOTIFY);
1130 {$ENDIF}
1131 inserter(gy*gw+gx, body);
1132 end;
1133 end;
1134 end;
1135 end;
1136 end;
1137 // done
1138 end
1139 else
1140 begin
1141 {$IF DEFINED(D2F_DEBUG_MOVER)}
1142 e_WriteLog(Format('proxy #%d: GRID OK: xg=%d;yg=%d;w=%d;h=%d;nx=%d;ny=%d og:(%d,%d)-(%d,%d); ng:(%d,%d)-(%d,%d)', [body, x0, y0, pw, ph, nx, ny, ogx0, ogy0, ogx1, ogy1, ngx0, ngy0, ngx1, ngy1]), MSG_NOTIFY);
1143 {$ENDIF}
1144 end;
1145 // update coordinates
1146 px.mX := nx+mMinX;
1147 px.mY := ny+mMinY;
1148 end;
1150 procedure TBodyGridBase.resizeBody (body: TBodyProxyId; nw, nh: Integer);
1151 var
1152 px: PBodyProxyRec;
1153 x0, y0, w, h: Integer;
1154 begin
1155 if (body < 0) or (body > High(mProxies)) then exit; // just in case
1156 // check if tile coords was changed
1157 px := @mProxies[body];
1158 x0 := px.mX-mMinX;
1159 y0 := px.mY-mMinY;
1160 w := px.mWidth;
1161 h := px.mHeight;
1162 {$IF DEFINED(D2F_DEBUG_MOVER)}
1163 e_WriteLog(Format('proxy #%d: RESIZE: xg=%d;yg=%d;w=%d;h=%d;nw=%d;nh=%d', [body, x0, y0, w, h, nw, nh]), MSG_NOTIFY);
1164 {$ENDIF}
1165 if ((x0+w) div mTileSize <> (x0+nw) div mTileSize) or
1166 ((y0+h) div mTileSize <> (y0+nh) div mTileSize) then
1167 begin
1168 // crossed tile boundary, do heavy work
1169 removeInternal(body);
1170 px.mWidth := nw;
1171 px.mHeight := nh;
1172 insertInternal(body);
1173 end
1174 else
1175 begin
1176 // nothing to do with the grid, just fix size
1177 px.mWidth := nw;
1178 px.mHeight := nh;
1179 end;
1180 end;
1183 // ////////////////////////////////////////////////////////////////////////// //
1184 function TBodyGridBase.atCellInPoint (x, y: Integer): TAtPointEnumerator;
1185 var
1186 cidx: Integer = -1;
1187 begin
1188 Dec(x, mMinX);
1189 Dec(y, mMinY);
1190 if (x >= 0) and (y >= 0) and (x < mWidth*mTileSize) and (y < mHeight*mTileSize) then cidx := mGrid[(y div mTileSize)*mWidth+(x div mTileSize)];
1191 result := TAtPointEnumerator.Create(mCells, cidx, getProxyById);
1192 end;
1195 // ////////////////////////////////////////////////////////////////////////// //
1196 // no callback: return `true` on the first hit
1197 function TBodyGridBase.forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1; exittag: PInteger=nil): ITP;
1198 var
1199 f: Integer;
1200 idx, curci: Integer;
1201 cc: PGridCell = nil;
1202 px: PBodyProxyRec;
1203 lq: LongWord;
1204 ptag: Integer;
1205 begin
1206 result := Default(ITP);
1207 if (exittag <> nil) then exittag^ := 0;
1208 tagmask := tagmask and TagFullMask;
1209 if (tagmask = 0) then exit;
1211 {$IF DEFINED(D2F_DEBUG_XXQ)}
1212 if (assigned(cb)) then e_WriteLog(Format('0: grid pointquery: (%d,%d)', [x, y]), MSG_NOTIFY);
1213 {$ENDIF}
1215 // make coords (0,0)-based
1216 Dec(x, mMinX);
1217 Dec(y, mMinY);
1218 if (x < 0) or (y < 0) or (x >= mWidth*mTileSize) or (y >= mHeight*mTileSize) then exit;
1220 curci := mGrid[(y div mTileSize)*mWidth+(x div mTileSize)];
1222 {$IF DEFINED(D2F_DEBUG_XXQ)}
1223 if (assigned(cb)) then e_WriteLog(Format('1: grid pointquery: (%d,%d) (%d,%d) %d', [x, y, (x div mTileSize), (y div mTileSize), curci]), MSG_NOTIFY);
1224 {$ENDIF}
1226 // restore coords
1227 Inc(x, mMinX);
1228 Inc(y, mMinY);
1230 // increase query counter
1231 Inc(mLastQuery);
1232 if (mLastQuery = 0) then
1233 begin
1234 // just in case of overflow
1235 mLastQuery := 1;
1236 for idx := 0 to High(mProxies) do mProxies[idx].mQueryMark := 0;
1237 end;
1238 lq := mLastQuery;
1240 {$IF DEFINED(D2F_DEBUG_XXQ)}
1241 if (assigned(cb)) then e_WriteLog(Format('2: grid pointquery: (%d,%d); lq=%u', [x, y, lq]), MSG_NOTIFY);
1242 {$ENDIF}
1244 while (curci <> -1) do
1245 begin
1246 {$IF DEFINED(D2F_DEBUG_XXQ)}
1247 if (assigned(cb)) then e_WriteLog(Format(' cell #%d', [curci]), MSG_NOTIFY);
1248 {$ENDIF}
1249 cc := @mCells[curci];
1250 for f := 0 to GridCellBucketSize-1 do
1251 begin
1252 if (cc.bodies[f] = -1) then break;
1253 px := @mProxies[cc.bodies[f]];
1254 {$IF DEFINED(D2F_DEBUG_XXQ)}
1255 if (assigned(cb)) then e_WriteLog(Format(' proxy #%d; qm:%u; tag:%08x; tagflag:%d %u', [cc.bodies[f], px.mQueryMark, px.mTag, (px.mTag and tagmask), LongWord(px.mObj)]), MSG_NOTIFY);
1256 {$ENDIF}
1257 // shit. has to do it this way, so i can change tag in callback
1258 if (px.mQueryMark <> lq) then
1259 begin
1260 px.mQueryMark := lq;
1261 ptag := px.mTag;
1262 if ((ptag and TagDisabled) = 0) and ((ptag and tagmask) <> 0) and
1263 (x >= px.mX) and (y >= px.mY) and (x < px.mX+px.mWidth) and (y < px.mY+px.mHeight) then
1264 begin
1265 if assigned(cb) then
1266 begin
1267 if cb(px.mObj, ptag) then
1268 begin
1269 result := px.mObj;
1270 if (exittag <> nil) then exittag^ := ptag;
1271 exit;
1272 end;
1273 end
1274 else
1275 begin
1276 result := px.mObj;
1277 if (exittag <> nil) then exittag^ := ptag;
1278 exit;
1279 end;
1280 end;
1281 end;
1282 end;
1283 curci := cc.next;
1284 end;
1285 end;
1288 // ////////////////////////////////////////////////////////////////////////// //
1289 // no callback: return `true` on the first hit
1290 function TBodyGridBase.forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP;
1291 const
1292 tsize = mTileSize;
1293 var
1294 idx: Integer;
1295 gx, gy: Integer;
1296 curci: Integer;
1297 f: Integer;
1298 cc: PGridCell = nil;
1299 px: PBodyProxyRec;
1300 lq: LongWord;
1301 gw: Integer;
1302 x0, y0: Integer;
1303 ptag: Integer;
1304 begin
1305 result := Default(ITP);
1306 if (w < 1) or (h < 1) then exit;
1307 tagmask := tagmask and TagFullMask;
1308 if (tagmask = 0) then exit;
1310 x0 := x;
1311 y0 := y;
1313 // fix coords
1314 Dec(x, mMinX);
1315 Dec(y, mMinY);
1317 gw := mWidth;
1318 //tsize := mTileSize;
1320 if (x+w <= 0) or (y+h <= 0) then exit;
1321 if (x >= gw*tsize) or (y >= mHeight*tsize) then exit;
1323 if mInQuery then raise Exception.Create('recursive queries aren''t supported');
1324 mInQuery := true;
1326 // increase query counter
1327 Inc(mLastQuery);
1328 if (mLastQuery = 0) then
1329 begin
1330 // just in case of overflow
1331 mLastQuery := 1;
1332 for idx := 0 to High(mProxies) do mProxies[idx].mQueryMark := 0;
1333 end;
1334 //e_WriteLog(Format('grid: query #%d: (%d,%d)-(%dx%d)', [mLastQuery, minx, miny, maxx, maxy]), MSG_NOTIFY);
1335 lq := mLastQuery;
1337 // go on
1338 for gy := y div tsize to (y+h-1) div tsize do
1339 begin
1340 if (gy < 0) then continue;
1341 if (gy >= mHeight) then break;
1342 for gx := x div tsize to (x+w-1) div tsize do
1343 begin
1344 if (gx < 0) then continue;
1345 if (gx >= gw) then break;
1346 // process cells
1347 curci := mGrid[gy*gw+gx];
1348 while (curci <> -1) do
1349 begin
1350 cc := @mCells[curci];
1351 for f := 0 to GridCellBucketSize-1 do
1352 begin
1353 if (cc.bodies[f] = -1) then break;
1354 px := @mProxies[cc.bodies[f]];
1355 // shit. has to do it this way, so i can change tag in callback
1356 if (px.mQueryMark = lq) then continue;
1357 px.mQueryMark := lq;
1358 ptag := px.mTag;
1359 if (not allowDisabled) and ((ptag and TagDisabled) <> 0) then continue;
1360 if ((ptag and tagmask) = 0) then continue;
1361 if (x0 >= px.mX+px.mWidth) or (y0 >= px.mY+px.mHeight) then continue;
1362 if (x0+w <= px.mX) or (y0+h <= px.mY) then continue;
1363 if assigned(cb) then
1364 begin
1365 if cb(px.mObj, ptag) then begin result := px.mObj; mInQuery := false; exit; end;
1366 end
1367 else
1368 begin
1369 result := px.mObj;
1370 mInQuery := false;
1371 exit;
1372 end;
1373 end;
1374 curci := cc.next;
1375 end;
1376 end;
1377 end;
1379 mInQuery := false;
1380 end;
1383 // ////////////////////////////////////////////////////////////////////////// //
1384 // no callback: return `true` on the nearest hit
1385 function TBodyGridBase.traceRay (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
1386 var
1387 ex, ey: Integer;
1388 begin
1389 result := traceRay(ex, ey, x0, y0, x1, y1, cb, tagmask);
1390 end;
1393 // no callback: return `true` on the nearest hit
1394 // you are not supposed to understand this
1395 function TBodyGridBase.traceRay (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
1396 const
1397 tsize = mTileSize;
1398 var
1399 wx0, wy0, wx1, wy1: Integer; // window coordinates
1400 stx, sty: Integer; // "steps" for x and y axes
1401 dsx, dsy: Integer; // "lengthes" for x and y axes
1402 dx2, dy2: Integer; // "double lengthes" for x and y axes
1403 xd, yd: Integer; // current coord
1404 e: Integer; // "error" (as in bresenham algo)
1405 rem: Integer;
1406 term: Integer;
1407 xptr, yptr: PInteger;
1408 xfixed: Boolean;
1409 temp: Integer;
1410 prevx, prevy: Integer;
1411 lastDistSq: Integer;
1412 ccidx, curci: Integer;
1413 hasUntried: Boolean;
1414 lastGA: Integer = -1;
1415 ga, x, y: Integer;
1416 lastObj: ITP;
1417 wasHit: Boolean = false;
1418 gw, gh, minx, miny, maxx, maxy: Integer;
1419 cc: PGridCell;
1420 px: PBodyProxyRec;
1421 lq: LongWord;
1422 f, ptag, distSq: Integer;
1423 x0, y0, x1, y1: Integer;
1424 //swapped: Boolean = false; // true: xd is yd, and vice versa
1425 // horizontal walker
1426 {$IFDEF GRID_USE_ORTHO_ACCEL}
1427 wklen, wkstep: Integer;
1428 //wksign: Integer;
1429 hopt: Boolean;
1430 {$ENDIF}
1431 // skipper
1432 xdist, ydist: Integer;
1433 begin
1434 result := Default(ITP);
1435 lastObj := Default(ITP);
1436 tagmask := tagmask and TagFullMask;
1437 ex := ax1; // why not?
1438 ey := ay1; // why not?
1439 if (tagmask = 0) then exit;
1441 if (ax0 = ax1) and (ay0 = ay1) then
1442 begin
1443 result := forEachAtPoint(ax0, ay0, nil, tagmask, @ptag);
1444 if (result <> nil) then
1445 begin
1446 if assigned(cb) and not cb(result, ptag, ax0, ay0, ax0, ay0) then result := Default(ITP);
1447 end;
1448 exit;
1449 end;
1451 lastDistSq := distanceSq(ax0, ay0, ax1, ay1)+1;
1453 gw := mWidth;
1454 gh := mHeight;
1455 minx := mMinX;
1456 miny := mMinY;
1457 maxx := gw*tsize-1;
1458 maxy := gh*tsize-1;
1460 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1461 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);
1462 {$ENDIF}
1464 x0 := ax0;
1465 y0 := ay0;
1466 x1 := ax1;
1467 y1 := ay1;
1469 // offset query coords to (0,0)-based
1470 Dec(x0, minx);
1471 Dec(y0, miny);
1472 Dec(x1, minx);
1473 Dec(y1, miny);
1475 // clip rectange
1476 wx0 := 0;
1477 wy0 := 0;
1478 wx1 := maxx;
1479 wy1 := maxy;
1481 // horizontal setup
1482 if (x0 < x1) then
1483 begin
1484 // from left to right
1485 if (x0 > wx1) or (x1 < wx0) then exit; // out of screen
1486 stx := 1; // going right
1487 end
1488 else
1489 begin
1490 // from right to left
1491 if (x1 > wx1) or (x0 < wx0) then exit; // out of screen
1492 stx := -1; // going left
1493 x0 := -x0;
1494 x1 := -x1;
1495 wx0 := -wx0;
1496 wx1 := -wx1;
1497 swapInt(wx0, wx1);
1498 end;
1500 // vertical setup
1501 if (y0 < y1) then
1502 begin
1503 // from top to bottom
1504 if (y0 > wy1) or (y1 < wy0) then exit; // out of screen
1505 sty := 1; // going down
1506 end
1507 else
1508 begin
1509 // from bottom to top
1510 if (y1 > wy1) or (y0 < wy0) then exit; // out of screen
1511 sty := -1; // going up
1512 y0 := -y0;
1513 y1 := -y1;
1514 wy0 := -wy0;
1515 wy1 := -wy1;
1516 swapInt(wy0, wy1);
1517 end;
1519 dsx := x1-x0;
1520 dsy := y1-y0;
1522 if (dsx < dsy) then
1523 begin
1524 //swapped := true;
1525 xptr := @yd;
1526 yptr := @xd;
1527 swapInt(x0, y0);
1528 swapInt(x1, y1);
1529 swapInt(dsx, dsy);
1530 swapInt(wx0, wy0);
1531 swapInt(wx1, wy1);
1532 swapInt(stx, sty);
1533 end
1534 else
1535 begin
1536 xptr := @xd;
1537 yptr := @yd;
1538 end;
1540 dx2 := 2*dsx;
1541 dy2 := 2*dsy;
1542 xd := x0;
1543 yd := y0;
1544 e := 2*dsy-dsx;
1545 term := x1;
1547 xfixed := false;
1548 if (y0 < wy0) then
1549 begin
1550 // clip at top
1551 temp := dx2*(wy0-y0)-dsx;
1552 xd += temp div dy2;
1553 rem := temp mod dy2;
1554 if (xd > wx1) then exit; // x is moved out of clipping rect, nothing to do
1555 if (xd+1 >= wx0) then
1556 begin
1557 yd := wy0;
1558 e -= rem+dsx;
1559 if (rem > 0) then begin Inc(xd); e += dy2; end;
1560 xfixed := true;
1561 end;
1562 end;
1564 if (not xfixed) and (x0 < wx0) then
1565 begin
1566 // clip at left
1567 temp := dy2*(wx0-x0);
1568 yd += temp div dx2;
1569 rem := temp mod dx2;
1570 if (yd > wy1) or (yd = wy1) and (rem >= dsx) then exit;
1571 xd := wx0;
1572 e += rem;
1573 if (rem >= dsx) then begin Inc(yd); e -= dx2; end;
1574 end;
1576 if (y1 > wy1) then
1577 begin
1578 // clip at bottom
1579 temp := dx2*(wy1-y0)+dsx;
1580 term := x0+temp div dy2;
1581 rem := temp mod dy2;
1582 if (rem = 0) then Dec(term);
1583 end;
1585 if (term > wx1) then term := wx1; // clip at right
1587 Inc(term); // draw last point
1588 //if (term = xd) then exit; // this is the only point, get out of here
1590 if (sty = -1) then yd := -yd;
1591 if (stx = -1) then begin xd := -xd; term := -term; end;
1592 dx2 -= dy2;
1594 // first move, to skip starting point
1595 // DON'T DO THIS! loop will take care of that
1596 if (xd = term) then
1597 begin
1598 //FIXME!
1599 result := forEachAtPoint(ax0, ay0, nil, tagmask, @ptag);
1600 if (result <> nil) then
1601 begin
1602 if assigned(cb) then
1603 begin
1604 if cb(result, ptag, ax0, ay0, ax0, ay0) then
1605 begin
1606 ex := ax0;
1607 ey := ay0;
1608 end
1609 else
1610 begin
1611 result := nil;
1612 end;
1613 end
1614 else
1615 begin
1616 ex := ax0;
1617 ey := ay0;
1618 end;
1619 end;
1620 exit;
1621 end;
1623 prevx := xptr^+minx;
1624 prevy := yptr^+miny;
1625 (*
1626 // move coords
1627 if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
1628 xd += stx;
1629 // done?
1630 if (xd = term) then exit;
1631 *)
1633 {$IF DEFINED(D2F_DEBUG)}
1634 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
1635 {$ENDIF}
1636 // DON'T DO THIS! loop will take care of that
1637 //lastGA := (yptr^ div tsize)*gw+(xptr^ div tsize);
1638 //ccidx := mGrid[lastGA];
1640 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1641 //if assigned(dbgRayTraceTileHitCB) then e_WriteLog('1:TRACING!', MSG_NOTIFY);
1642 {$ENDIF}
1644 //if (dbgShowTraceLog) then e_WriteLog(Format('raycast start: (%d,%d)-(%d,%d); xptr^=%d; yptr^=%d', [ax0, ay0, ax1, ay1, xptr^, yptr^]), MSG_NOTIFY);
1646 if mInQuery then raise Exception.Create('recursive queries aren''t supported');
1647 mInQuery := true;
1649 // increase query counter
1650 Inc(mLastQuery);
1651 if (mLastQuery = 0) then
1652 begin
1653 // just in case of overflow
1654 mLastQuery := 1;
1655 for f := 0 to High(mProxies) do mProxies[f].mQueryMark := 0;
1656 end;
1657 lq := mLastQuery;
1659 {$IFDEF GRID_USE_ORTHO_ACCEL}
1660 // if this is strict horizontal/vertical trace, use optimized codepath
1661 if (ax0 = ax1) or (ay0 = ay1) then
1662 begin
1663 // horizontal trace: walk the whole tiles, calculating mindist once for each proxy in cell
1664 // stx < 0: going left, otherwise `stx` is > 0, and we're going right
1665 // vertical trace: walk the whole tiles, calculating mindist once for each proxy in cell
1666 // stx < 0: going up, otherwise `stx` is > 0, and we're going down
1667 hopt := (ay0 = ay1); // horizontal?
1668 if (stx < 0) then begin {wksign := -1;} wklen := -(term-xd); end else begin {wksign := 1;} wklen := term-xd; end;
1669 {$IF DEFINED(D2F_DEBUG)}
1670 if dbgShowTraceLog then e_LogWritefln('optimized htrace; wklen=%d', [wklen]);
1671 {$ENDIF}
1672 ga := (yptr^ div tsize)*gw+(xptr^ div tsize);
1673 // one of those will never change
1674 x := xptr^+minx;
1675 y := yptr^+miny;
1676 while (wklen > 0) do
1677 begin
1678 {$IF DEFINED(D2F_DEBUG)}
1679 if dbgShowTraceLog then e_LogWritefln(' htrace; ga=%d; x=%d, y=%d; y=%d; y=%d', [ga, xptr^+minx, yptr^+miny, y, ay0]);
1680 {$ENDIF}
1681 // new tile?
1682 if (ga <> lastGA) then
1683 begin
1684 lastGA := ga;
1685 ccidx := mGrid[lastGA];
1686 // convert coords to map (to avoid ajdusting coords inside the loop)
1687 if hopt then x := xptr^+minx else y := yptr^+miny;
1688 while (ccidx <> -1) do
1689 begin
1690 cc := @mCells[ccidx];
1691 for f := 0 to GridCellBucketSize-1 do
1692 begin
1693 if (cc.bodies[f] = -1) then break;
1694 px := @mProxies[cc.bodies[f]];
1695 ptag := px.mTag;
1696 if ((ptag and TagDisabled) = 0) and ((ptag and tagmask) <> 0) and (px.mQueryMark <> lq) and
1697 // constant coord should be inside
1698 ((hopt and (y >= px.y0) and (y <= px.y1)) or
1699 ((not hopt) and (x >= px.x0) and (x <= px.x1))) then
1700 begin
1701 px.mQueryMark := lq; // mark as processed
1702 // inside the proxy?
1703 if (hopt and (x > px.x0) and (x < px.x1)) or
1704 ((not hopt) and (y > px.y0) and (y < px.y1)) then
1705 begin
1706 // setup prev[xy]
1707 if assigned(cb) then
1708 begin
1709 if cb(px.mObj, ptag, x, y, x, y) then
1710 begin
1711 result := px.mObj;
1712 ex := x;
1713 ey := y;
1714 mInQuery := false;
1715 exit;
1716 end;
1717 end
1718 else
1719 begin
1720 distSq := distanceSq(ax0, ay0, x, y);
1721 {$IF DEFINED(D2F_DEBUG)}
1722 if dbgShowTraceLog then e_LogWritefln(' EMBEDDED hhit(%d): a=(%d,%d), h=(%d,%d), distsq=%d; lastsq=%d', [cc.bodies[f], ax0, ay0, x, y, distSq, lastDistSq]);
1723 {$ENDIF}
1724 if (distSq < lastDistSq) then
1725 begin
1726 ex := x;
1727 ey := y;
1728 result := px.mObj;
1729 mInQuery := false;
1730 exit;
1731 end;
1732 end;
1733 continue;
1734 end;
1735 // remember this hitpoint if it is nearer than an old one
1736 // setup prev[xy]
1737 if hopt then
1738 begin
1739 // horizontal trace
1740 prevy := y;
1741 y := yptr^+miny;
1742 if (stx < 0) then
1743 begin
1744 // going left
1745 if (x < px.x1) then continue; // not on the right edge
1746 x := px.x1;
1747 prevx := x+1;
1748 end
1749 else
1750 begin
1751 // going right
1752 if (x > px.x0) then continue; // not on the left edge
1753 x := px.x0;
1754 prevx := x-1;
1755 end;
1756 end
1757 else
1758 begin
1759 // vertical trace
1760 prevx := x;
1761 x := xptr^+minx;
1762 if (stx < 0) then
1763 begin
1764 // going up
1765 if (y < px.y1) then continue; // not on the bottom edge
1766 y := px.y1;
1767 prevy := x+1;
1768 end
1769 else
1770 begin
1771 // going down
1772 if (y > px.y0) then continue; // not on the top edge
1773 y := px.y0;
1774 prevy := y-1;
1775 end;
1776 end;
1777 if assigned(cb) then
1778 begin
1779 if cb(px.mObj, ptag, x, y, prevx, prevy) then
1780 begin
1781 result := px.mObj;
1782 ex := prevx;
1783 ey := prevy;
1784 mInQuery := false;
1785 exit;
1786 end;
1787 end
1788 else
1789 begin
1790 distSq := distanceSq(ax0, ay0, prevx, prevy);
1791 {$IF DEFINED(D2F_DEBUG)}
1792 if dbgShowTraceLog then e_LogWritefln(' hhit(%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]);
1793 {$ENDIF}
1794 if (distSq < lastDistSq) then
1795 begin
1796 wasHit := true;
1797 lastDistSq := distSq;
1798 ex := prevx;
1799 ey := prevy;
1800 lastObj := px.mObj;
1801 end;
1802 end;
1803 end;
1804 end;
1805 // next cell
1806 ccidx := cc.next;
1807 end;
1808 if wasHit and not assigned(cb) then begin result := lastObj; mInQuery := false; exit; end;
1809 if assigned(cb) and cb(nil, 0, x, y, x, y) then begin result := lastObj; mInQuery := false; exit; end;
1810 end;
1811 // skip to next tile
1812 if hopt then
1813 begin
1814 if (stx > 0) then
1815 begin
1816 // to the right
1817 wkstep := ((xptr^ or (mTileSize-1))+1)-xptr^;
1818 {$IF DEFINED(D2F_DEBUG)}
1819 if dbgShowTraceLog then e_LogWritefln(' right step: wklen=%d; wkstep=%d', [wklen, wkstep]);
1820 {$ENDIF}
1821 if (wkstep >= wklen) then break;
1822 Inc(xptr^, wkstep);
1823 Inc(ga);
1824 end
1825 else
1826 begin
1827 // to the left
1828 wkstep := xptr^-((xptr^ and (not (mTileSize-1)))-1);
1829 {$IF DEFINED(D2F_DEBUG)}
1830 if dbgShowTraceLog then e_LogWritefln(' left step: wklen=%d; wkstep=%d', [wklen, wkstep]);
1831 {$ENDIF}
1832 if (wkstep >= wklen) then break;
1833 Dec(xptr^, wkstep);
1834 Dec(ga);
1835 end;
1836 end
1837 else
1838 begin
1839 if (stx > 0) then
1840 begin
1841 // to the down
1842 wkstep := ((yptr^ or (mTileSize-1))+1)-yptr^;
1843 {$IF DEFINED(D2F_DEBUG)}
1844 if dbgShowTraceLog then e_LogWritefln(' down step: wklen=%d; wkstep=%d', [wklen, wkstep]);
1845 {$ENDIF}
1846 if (wkstep >= wklen) then break;
1847 Inc(yptr^, wkstep);
1848 Inc(ga, mWidth);
1849 end
1850 else
1851 begin
1852 // to the up
1853 wkstep := yptr^-((yptr^ and (not (mTileSize-1)))-1);
1854 {$IF DEFINED(D2F_DEBUG)}
1855 if dbgShowTraceLog then e_LogWritefln(' up step: wklen=%d; wkstep=%d', [wklen, wkstep]);
1856 {$ENDIF}
1857 if (wkstep >= wklen) then break;
1858 Dec(yptr^, wkstep);
1859 Dec(ga, mWidth);
1860 end;
1861 end;
1862 Dec(wklen, wkstep);
1863 end;
1864 // we can travel less than one cell
1865 if wasHit and not assigned(cb) then result := lastObj else begin ex := ax1; ey := ay1; end;
1866 mInQuery := false;
1867 exit;
1868 end;
1869 {$ENDIF}
1871 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1872 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
1873 {$ENDIF}
1875 //e_LogWritefln('*********************', []);
1876 ccidx := -1;
1877 // can omit checks
1878 while (xd <> term) do
1879 begin
1880 // check cell(s)
1881 {$IF DEFINED(D2F_DEBUG)}
1882 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
1883 {$ENDIF}
1884 // new tile?
1885 ga := (yptr^ div tsize)*gw+(xptr^ div tsize);
1886 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1887 if assigned(dbgRayTraceTileHitCB) then e_WriteLog(Format(' xd=%d; term=%d; gx=%d; gy=%d; ga=%d; lastga=%d', [xd, term, xptr^, yptr^, ga, lastGA]), MSG_NOTIFY);
1888 {$ENDIF}
1889 if (ga <> lastGA) then
1890 begin
1891 // yes
1892 {$IF DEFINED(D2F_DEBUG)}
1893 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
1894 {$ENDIF}
1895 if (ccidx <> -1) then
1896 begin
1897 // signal cell completion
1898 if assigned(cb) then
1899 begin
1900 if cb(nil, 0, xptr^+minx, yptr^+miny, prevx, prevy) then begin result := lastObj; mInQuery := false; exit; end;
1901 end
1902 else if wasHit then
1903 begin
1904 result := lastObj;
1905 mInQuery := false;
1906 exit;
1907 end;
1908 end;
1909 lastGA := ga;
1910 ccidx := mGrid[lastGA];
1911 end;
1912 // has something to process in this tile?
1913 if (ccidx <> -1) then
1914 begin
1915 // process cell
1916 curci := ccidx;
1917 hasUntried := false; // this will be set to `true` if we have some proxies we still want to process at the next step
1918 // convert coords to map (to avoid ajdusting coords inside the loop)
1919 x := xptr^+minx;
1920 y := yptr^+miny;
1921 // process cell list
1922 while (curci <> -1) do
1923 begin
1924 cc := @mCells[curci];
1925 for f := 0 to GridCellBucketSize-1 do
1926 begin
1927 if (cc.bodies[f] = -1) then break;
1928 px := @mProxies[cc.bodies[f]];
1929 ptag := px.mTag;
1930 if ((ptag and TagDisabled) = 0) and ((ptag and tagmask) <> 0) and (px.mQueryMark <> lq) then
1931 begin
1932 // can we process this proxy?
1933 if (x >= px.mX) and (y >= px.mY) and (x < px.mX+px.mWidth) and (y < px.mY+px.mHeight) then
1934 begin
1935 px.mQueryMark := lq; // mark as processed
1936 if assigned(cb) then
1937 begin
1938 if cb(px.mObj, ptag, x, y, prevx, prevy) then
1939 begin
1940 result := px.mObj;
1941 ex := prevx;
1942 ey := prevy;
1943 mInQuery := false;
1944 exit;
1945 end;
1946 end
1947 else
1948 begin
1949 // remember this hitpoint if it is nearer than an old one
1950 distSq := distanceSq(ax0, ay0, prevx, prevy);
1951 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1952 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);
1953 {$ENDIF}
1954 if (distSq < lastDistSq) then
1955 begin
1956 wasHit := true;
1957 lastDistSq := distSq;
1958 ex := prevx;
1959 ey := prevy;
1960 lastObj := px.mObj;
1961 end;
1962 end;
1963 end
1964 else
1965 begin
1966 // this is possibly interesting proxy, set "has more to check" flag
1967 hasUntried := true;
1968 end;
1969 end;
1970 end;
1971 // next cell
1972 curci := cc.next;
1973 end;
1974 // still has something interesting in this cell?
1975 if not hasUntried then
1976 begin
1977 // nope, don't process this cell anymore; signal cell completion
1978 ccidx := -1;
1979 if assigned(cb) then
1980 begin
1981 if cb(nil, 0, x, y, prevx, prevy) then begin result := lastObj; mInQuery := false; exit; end;
1982 end
1983 else if wasHit then
1984 begin
1985 result := lastObj;
1986 mInQuery := false;
1987 exit;
1988 end;
1989 end;
1990 end;
1991 if (ccidx = -1) then
1992 begin
1993 // move to cell edge, as we have nothing to trace here anymore
1994 if (stx < 0) then xdist := xd and (not (mTileSize-1)) else xdist := xd or (mTileSize-1);
1995 if (sty < 0) then ydist := yd and (not (mTileSize-1)) else ydist := yd or (mTileSize-1);
1996 //e_LogWritefln('0: swapped=%d; xd=%d; yd=%d; stx=%d; sty=%d; e=%d; dx2=%d; dy2=%d; term=%d; xdist=%d; ydist=%d', [swapped, xd, yd, stx, sty, e, dx2, dy2, term, xdist, ydist]);
1997 while (xd <> xdist) and (yd <> ydist) do
1998 begin
1999 // step
2000 xd += stx;
2001 if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
2002 //e_LogWritefln(' xd=%d; yd=%d', [xd, yd]);
2003 if (xd = term) then break;
2004 end;
2005 //e_LogWritefln('1: swapped=%d; xd=%d; yd=%d; stx=%d; sty=%d; e=%d; dx2=%d; dy2=%d; term=%d; xdist=%d; ydist=%d', [swapped, xd, yd, stx, sty, e, dx2, dy2, term, xdist, ydist]);
2006 if (xd = term) then break;
2007 end;
2008 //putPixel(xptr^, yptr^);
2009 // move coords
2010 prevx := xptr^+minx;
2011 prevy := yptr^+miny;
2012 if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
2013 xd += stx;
2014 end;
2015 // we can travel less than one cell
2016 if wasHit and not assigned(cb) then
2017 begin
2018 result := lastObj;
2019 end
2020 else
2021 begin
2022 ex := ax1; // why not?
2023 ey := ay1; // why not?
2024 end;
2026 mInQuery := false;
2027 end;
2030 // ////////////////////////////////////////////////////////////////////////// //
2031 //FIXME! optimize this with real tile walking
2032 function TBodyGridBase.forEachAlongLine (ax0, ay0, ax1, ay1: Integer; cb: TGridQueryCB; tagmask: Integer=-1; log: Boolean=false): ITP;
2033 const
2034 tsize = mTileSize;
2035 var
2036 wx0, wy0, wx1, wy1: Integer; // window coordinates
2037 stx, sty: Integer; // "steps" for x and y axes
2038 dsx, dsy: Integer; // "lengthes" for x and y axes
2039 dx2, dy2: Integer; // "double lengthes" for x and y axes
2040 xd, yd: Integer; // current coord
2041 e: Integer; // "error" (as in bresenham algo)
2042 rem: Integer;
2043 term: Integer;
2044 xptr, yptr: PInteger;
2045 xfixed: Boolean;
2046 temp: Integer;
2047 ccidx, curci: Integer;
2048 lastGA: Integer = -1;
2049 ga: Integer;
2050 gw, gh, minx, miny, maxx, maxy: Integer;
2051 cc: PGridCell;
2052 px: PBodyProxyRec;
2053 lq: LongWord;
2054 f, ptag: Integer;
2055 x0, y0, x1, y1: Integer;
2056 //swapped: Boolean = false; // true: xd is yd, and vice versa
2057 // horizontal walker
2058 {$IFDEF GRID_USE_ORTHO_ACCEL}
2059 wklen, wkstep: Integer;
2060 //wksign: Integer;
2061 hopt: Boolean;
2062 {$ENDIF}
2063 // skipper
2064 xdist, ydist: Integer;
2065 begin
2066 log := false;
2067 result := Default(ITP);
2068 tagmask := tagmask and TagFullMask;
2069 if (tagmask = 0) or not assigned(cb) then exit;
2071 if (ax0 = ax1) and (ay0 = ay1) then
2072 begin
2073 result := forEachAtPoint(ax0, ay0, cb, tagmask, @ptag);
2074 exit;
2075 end;
2077 gw := mWidth;
2078 gh := mHeight;
2079 minx := mMinX;
2080 miny := mMinY;
2081 maxx := gw*tsize-1;
2082 maxy := gh*tsize-1;
2084 x0 := ax0;
2085 y0 := ay0;
2086 x1 := ax1;
2087 y1 := ay1;
2089 // offset query coords to (0,0)-based
2090 Dec(x0, minx);
2091 Dec(y0, miny);
2092 Dec(x1, minx);
2093 Dec(y1, miny);
2095 // clip rectange
2096 wx0 := 0;
2097 wy0 := 0;
2098 wx1 := maxx;
2099 wy1 := maxy;
2101 // horizontal setup
2102 if (x0 < x1) then
2103 begin
2104 // from left to right
2105 if (x0 > wx1) or (x1 < wx0) then exit; // out of screen
2106 stx := 1; // going right
2107 end
2108 else
2109 begin
2110 // from right to left
2111 if (x1 > wx1) or (x0 < wx0) then exit; // out of screen
2112 stx := -1; // going left
2113 x0 := -x0;
2114 x1 := -x1;
2115 wx0 := -wx0;
2116 wx1 := -wx1;
2117 swapInt(wx0, wx1);
2118 end;
2120 // vertical setup
2121 if (y0 < y1) then
2122 begin
2123 // from top to bottom
2124 if (y0 > wy1) or (y1 < wy0) then exit; // out of screen
2125 sty := 1; // going down
2126 end
2127 else
2128 begin
2129 // from bottom to top
2130 if (y1 > wy1) or (y0 < wy0) then exit; // out of screen
2131 sty := -1; // going up
2132 y0 := -y0;
2133 y1 := -y1;
2134 wy0 := -wy0;
2135 wy1 := -wy1;
2136 swapInt(wy0, wy1);
2137 end;
2139 dsx := x1-x0;
2140 dsy := y1-y0;
2142 if (dsx < dsy) then
2143 begin
2144 //swapped := true;
2145 xptr := @yd;
2146 yptr := @xd;
2147 swapInt(x0, y0);
2148 swapInt(x1, y1);
2149 swapInt(dsx, dsy);
2150 swapInt(wx0, wy0);
2151 swapInt(wx1, wy1);
2152 swapInt(stx, sty);
2153 end
2154 else
2155 begin
2156 xptr := @xd;
2157 yptr := @yd;
2158 end;
2160 dx2 := 2*dsx;
2161 dy2 := 2*dsy;
2162 xd := x0;
2163 yd := y0;
2164 e := 2*dsy-dsx;
2165 term := x1;
2167 xfixed := false;
2168 if (y0 < wy0) then
2169 begin
2170 // clip at top
2171 temp := dx2*(wy0-y0)-dsx;
2172 xd += temp div dy2;
2173 rem := temp mod dy2;
2174 if (xd > wx1) then exit; // x is moved out of clipping rect, nothing to do
2175 if (xd+1 >= wx0) then
2176 begin
2177 yd := wy0;
2178 e -= rem+dsx;
2179 if (rem > 0) then begin Inc(xd); e += dy2; end;
2180 xfixed := true;
2181 end;
2182 end;
2184 if (not xfixed) and (x0 < wx0) then
2185 begin
2186 // clip at left
2187 temp := dy2*(wx0-x0);
2188 yd += temp div dx2;
2189 rem := temp mod dx2;
2190 if (yd > wy1) or (yd = wy1) and (rem >= dsx) then exit;
2191 xd := wx0;
2192 e += rem;
2193 if (rem >= dsx) then begin Inc(yd); e -= dx2; end;
2194 end;
2196 if (y1 > wy1) then
2197 begin
2198 // clip at bottom
2199 temp := dx2*(wy1-y0)+dsx;
2200 term := x0+temp div dy2;
2201 rem := temp mod dy2;
2202 if (rem = 0) then Dec(term);
2203 end;
2205 if (term > wx1) then term := wx1; // clip at right
2207 Inc(term); // draw last point
2208 //if (term = xd) then exit; // this is the only point, get out of here
2210 if (sty = -1) then yd := -yd;
2211 if (stx = -1) then begin xd := -xd; term := -term; end;
2212 dx2 -= dy2;
2214 // first move, to skip starting point
2215 // DON'T DO THIS! loop will take care of that
2216 if (xd = term) then
2217 begin
2218 result := forEachAtPoint(ax0, ay0, cb, tagmask, @ptag);
2219 exit;
2220 end;
2222 (*
2223 // move coords
2224 if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
2225 xd += stx;
2226 // done?
2227 if (xd = term) then exit;
2228 *)
2230 {$IF DEFINED(D2F_DEBUG)}
2231 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
2232 {$ENDIF}
2233 // DON'T DO THIS! loop will take care of that
2234 //lastGA := (yptr^ div tsize)*gw+(xptr^ div tsize);
2235 //ccidx := mGrid[lastGA];
2237 if mInQuery then raise Exception.Create('recursive queries aren''t supported');
2238 mInQuery := true;
2240 // increase query counter
2241 Inc(mLastQuery);
2242 if (mLastQuery = 0) then
2243 begin
2244 // just in case of overflow
2245 mLastQuery := 1;
2246 for f := 0 to High(mProxies) do mProxies[f].mQueryMark := 0;
2247 end;
2248 lq := mLastQuery;
2250 {$IFDEF GRID_USE_ORTHO_ACCEL}
2251 // if this is strict horizontal/vertical trace, use optimized codepath
2252 if (ax0 = ax1) or (ay0 = ay1) then
2253 begin
2254 // horizontal trace: walk the whole tiles, calculating mindist once for each proxy in cell
2255 // stx < 0: going left, otherwise `stx` is > 0, and we're going right
2256 // vertical trace: walk the whole tiles, calculating mindist once for each proxy in cell
2257 // stx < 0: going up, otherwise `stx` is > 0, and we're going down
2258 hopt := (ay0 = ay1); // horizontal?
2259 if (stx < 0) then begin {wksign := -1;} wklen := -(term-xd); end else begin {wksign := 1;} wklen := term-xd; end;
2260 {$IF DEFINED(D2F_DEBUG)}
2261 if dbgShowTraceLog then e_LogWritefln('optimized htrace; wklen=%d', [wklen]);
2262 {$ENDIF}
2263 ga := (yptr^ div tsize)*gw+(xptr^ div tsize);
2264 while (wklen > 0) do
2265 begin
2266 {$IF DEFINED(D2F_DEBUG)}
2267 if dbgShowTraceLog then e_LogWritefln(' htrace; ga=%d; x=%d, y=%d; ay0=%d', [ga, xptr^+minx, yptr^+miny, ay0]);
2268 {$ENDIF}
2269 // new tile?
2270 if (ga <> lastGA) then
2271 begin
2272 lastGA := ga;
2273 ccidx := mGrid[lastGA];
2274 // convert coords to map (to avoid ajdusting coords inside the loop)
2275 while (ccidx <> -1) do
2276 begin
2277 cc := @mCells[ccidx];
2278 for f := 0 to GridCellBucketSize-1 do
2279 begin
2280 if (cc.bodies[f] = -1) then break;
2281 px := @mProxies[cc.bodies[f]];
2282 ptag := px.mTag;
2283 if ((ptag and TagDisabled) = 0) and ((ptag and tagmask) <> 0) and (px.mQueryMark <> lq) then
2284 begin
2285 px.mQueryMark := lq; // mark as processed
2286 if assigned(cb) then
2287 begin
2288 if cb(px.mObj, ptag) then begin result := px.mObj; mInQuery := false; exit; end;
2289 end
2290 else
2291 begin
2292 result := px.mObj;
2293 mInQuery := false;
2294 exit;
2295 end;
2296 end;
2297 end;
2298 // next cell
2299 ccidx := cc.next;
2300 end;
2301 end;
2302 // skip to next tile
2303 if hopt then
2304 begin
2305 if (stx > 0) then
2306 begin
2307 // to the right
2308 wkstep := ((xptr^ or (mTileSize-1))+1)-xptr^;
2309 {$IF DEFINED(D2F_DEBUG)}
2310 if dbgShowTraceLog then e_LogWritefln(' right step: wklen=%d; wkstep=%d', [wklen, wkstep]);
2311 {$ENDIF}
2312 if (wkstep >= wklen) then break;
2313 Inc(xptr^, wkstep);
2314 Inc(ga);
2315 end
2316 else
2317 begin
2318 // to the left
2319 wkstep := xptr^-((xptr^ and (not (mTileSize-1)))-1);
2320 {$IF DEFINED(D2F_DEBUG)}
2321 if dbgShowTraceLog then e_LogWritefln(' left step: wklen=%d; wkstep=%d', [wklen, wkstep]);
2322 {$ENDIF}
2323 if (wkstep >= wklen) then break;
2324 Dec(xptr^, wkstep);
2325 Dec(ga);
2326 end;
2327 end
2328 else
2329 begin
2330 if (stx > 0) then
2331 begin
2332 // to the down
2333 wkstep := ((yptr^ or (mTileSize-1))+1)-yptr^;
2334 {$IF DEFINED(D2F_DEBUG)}
2335 if dbgShowTraceLog then e_LogWritefln(' down step: wklen=%d; wkstep=%d', [wklen, wkstep]);
2336 {$ENDIF}
2337 if (wkstep >= wklen) then break;
2338 Inc(yptr^, wkstep);
2339 Inc(ga, mWidth);
2340 end
2341 else
2342 begin
2343 // to the up
2344 wkstep := yptr^-((yptr^ and (not (mTileSize-1)))-1);
2345 {$IF DEFINED(D2F_DEBUG)}
2346 if dbgShowTraceLog then e_LogWritefln(' up step: wklen=%d; wkstep=%d', [wklen, wkstep]);
2347 {$ENDIF}
2348 if (wkstep >= wklen) then break;
2349 Dec(yptr^, wkstep);
2350 Dec(ga, mWidth);
2351 end;
2352 end;
2353 Dec(wklen, wkstep);
2354 end;
2355 mInQuery := false;
2356 exit;
2357 end;
2358 {$ENDIF}
2360 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
2361 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
2362 {$ENDIF}
2364 ccidx := -1;
2365 // can omit checks
2366 while (xd <> term) do
2367 begin
2368 // check cell(s)
2369 {$IF DEFINED(D2F_DEBUG)}
2370 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
2371 {$ENDIF}
2372 // new tile?
2373 ga := (yptr^ div tsize)*gw+(xptr^ div tsize);
2374 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
2375 if assigned(dbgRayTraceTileHitCB) then e_WriteLog(Format(' xd=%d; term=%d; gx=%d; gy=%d; ga=%d; lastga=%d', [xd, term, xptr^, yptr^, ga, lastGA]), MSG_NOTIFY);
2376 {$ENDIF}
2377 if (ga <> lastGA) then
2378 begin
2379 // yes
2380 {$IF DEFINED(D2F_DEBUG)}
2381 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
2382 {$ENDIF}
2383 lastGA := ga;
2384 ccidx := mGrid[lastGA];
2385 end;
2386 // has something to process in this tile?
2387 if (ccidx <> -1) then
2388 begin
2389 // process cell
2390 curci := ccidx;
2391 // process cell list
2392 while (curci <> -1) do
2393 begin
2394 cc := @mCells[curci];
2395 for f := 0 to GridCellBucketSize-1 do
2396 begin
2397 if (cc.bodies[f] = -1) then break;
2398 px := @mProxies[cc.bodies[f]];
2399 ptag := px.mTag;
2400 if ((ptag and TagDisabled) = 0) and ((ptag and tagmask) <> 0) and (px.mQueryMark <> lq) then
2401 begin
2402 px.mQueryMark := lq; // mark as processed
2403 if assigned(cb) then
2404 begin
2405 if cb(px.mObj, ptag) then begin result := px.mObj; mInQuery := false; exit; end;
2406 end
2407 else
2408 begin
2409 result := px.mObj;
2410 mInQuery := false;
2411 exit;
2412 end;
2413 end;
2414 end;
2415 // next cell
2416 curci := cc.next;
2417 end;
2418 // nothing more interesting in this cell
2419 ccidx := -1;
2420 end;
2421 // move to cell edge, as we have nothing to trace here anymore
2422 if (stx < 0) then xdist := xd and (not (mTileSize-1)) else xdist := xd or (mTileSize-1);
2423 if (sty < 0) then ydist := yd and (not (mTileSize-1)) else ydist := yd or (mTileSize-1);
2424 //e_LogWritefln('0: swapped=%d; xd=%d; yd=%d; stx=%d; sty=%d; e=%d; dx2=%d; dy2=%d; term=%d; xdist=%d; ydist=%d', [swapped, xd, yd, stx, sty, e, dx2, dy2, term, xdist, ydist]);
2425 while (xd <> xdist) and (yd <> ydist) do
2426 begin
2427 // step
2428 xd += stx;
2429 if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
2430 //e_LogWritefln(' xd=%d; yd=%d', [xd, yd]);
2431 if (xd = term) then break;
2432 end;
2433 //e_LogWritefln('1: swapped=%d; xd=%d; yd=%d; stx=%d; sty=%d; e=%d; dx2=%d; dy2=%d; term=%d; xdist=%d; ydist=%d', [swapped, xd, yd, stx, sty, e, dx2, dy2, term, xdist, ydist]);
2434 if (xd = term) then break;
2435 //putPixel(xptr^, yptr^);
2436 // move coords
2437 if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
2438 xd += stx;
2439 end;
2441 mInQuery := false;
2442 end;
2445 {.$DEFINE D2F_DEBUG_OTR}
2446 function TBodyGridBase.traceOrthoRayWhileIn (out ex, ey: Integer; ax0, ay0, ax1, ay1: Integer; tagmask: Integer=-1): Boolean;
2447 var
2448 ccidx: Integer;
2449 cc: PGridCell;
2450 px: PBodyProxyRec;
2451 ptag: Integer;
2452 minx, miny: Integer;
2453 f, c0, c1: Integer;
2454 x0, y0, x1, y1: Integer;
2455 celly0, celly1: Integer;
2456 dy: Integer;
2457 filled: array[0..mTileSize-1] of Byte;
2458 {$IF DEFINED(D2F_DEBUG_OTR)}
2459 s: AnsiString = '';
2460 {$ENDIF}
2461 begin
2462 result := false;
2463 ex := ax1;
2464 ey := ay1;
2465 if not ((ax0 = ax1) or (ay0 = ay1)) then raise Exception.Create('orthoray is not orthogonal');
2467 tagmask := tagmask and TagFullMask;
2468 if (tagmask = 0) then exit;
2470 if (forEachAtPoint(ax0, ay0, nil, tagmask) = nil) then exit;
2472 minx := mMinX;
2473 miny := mMinY;
2475 // offset query coords to (0,0)-based
2476 x0 := ax0-minx;
2477 y0 := ay0-miny;
2478 x1 := ax1-minx;
2479 y1 := ay1-miny;
2481 if (x0 = x1) then
2482 begin
2483 if (x0 < 0) or (x0 >= mWidth*mTileSize) then exit; // oops
2484 // vertical
2485 if (y0 < y1) then
2486 begin
2487 // down
2488 if (y1 < 0) or (y0 >= mHeight*mTileSize) then exit;
2489 //if (ay0 < 0) then ay0 := 0;
2490 if (y0 < 0) then exit;
2491 if (y1 >= mHeight*mTileSize) then y1 := mHeight*mTileSize-1;
2492 dy := 1;
2493 end
2494 else
2495 begin
2496 // up
2497 if (y0 < 0) or (y1 >= mHeight*mTileSize) then exit;
2498 //if (ay1 < 0) then ay1 := 0;
2499 if (y1 < 0) then exit;
2500 if (y0 >= mHeight*mTileSize) then y0 := mHeight*mTileSize-1;
2501 dy := -1;
2502 end;
2503 // check tile
2504 while true do
2505 begin
2506 ccidx := mGrid[(y0 div mTileSize)*mWidth+(x0 div mTileSize)];
2507 FillChar(filled, sizeof(filled), 0);
2508 celly0 := y0 and (not (mTileSize-1));
2509 celly1 := celly0+mTileSize-1;
2510 while (ccidx <> -1) do
2511 begin
2512 cc := @mCells[ccidx];
2513 for f := 0 to GridCellBucketSize-1 do
2514 begin
2515 if (cc.bodies[f] = -1) then break;
2516 px := @mProxies[cc.bodies[f]];
2517 ptag := px.mTag;
2518 if ((ptag and TagDisabled) = 0) and ((ptag and tagmask) <> 0) and
2519 (ax0 >= px.x0) and (ax0 <= px.x1) then
2520 begin
2521 // bound c0 and c1 to cell
2522 c0 := nclamp(px.y0-miny, celly0, celly1);
2523 c1 := nclamp(px.y1-miny, celly0, celly1);
2524 // fill the thing
2525 {$IF DEFINED(D2F_DEBUG_OTR)}
2526 e_LogWritefln('**px.y0=%s; px.y1=%s; c0=%s; c1=%s; celly0=%s; celly1=%s; [%s..%s]', [px.y0-miny, px.y1-miny, c0, c1, celly0, celly1, c0-celly0, (c0-celly0)+(c1-c0)]);
2527 {$ENDIF}
2528 //assert(c0 <= c1);
2529 FillChar(filled[c0-celly0], c1-c0+1, 1);
2530 end;
2531 end;
2532 // next cell
2533 ccidx := cc.next;
2534 end;
2535 {$IF DEFINED(D2F_DEBUG_OTR)}
2536 s := formatstrf(' x=%s; ay0=%s; ay1=%s; y0=%s; celly0=%s; celly1=%s; dy=%s; [', [ax0, ay0, ay1, y0, celly0, celly1, dy]);
2537 for f := 0 to High(filled) do if (filled[f] <> 0) then s += '1' else s += '0';
2538 s += ']';
2539 e_LogWriteln(s);
2540 {$ENDIF}
2541 // now go till we hit cell boundary or empty space
2542 if (dy < 0) then
2543 begin
2544 // up
2545 while (y0 >= celly0) and (filled[y0-celly0] <> 0) do
2546 begin
2547 {$IF DEFINED(D2F_DEBUG_OTR)}
2548 e_LogWritefln(' filled: cdy=%s; y0=%s; celly0=%s; ay0=%s; ay1=%s', [y0-celly0, y0, celly0, ay0, ay1]);
2549 {$ENDIF}
2550 Dec(y0);
2551 Dec(ay0);
2552 end;
2553 {$IF DEFINED(D2F_DEBUG_OTR)}
2554 e_LogWritefln(' span done: cdy=%s; y0=%s; celly0=%s; ay0=%s; ay1=%s', [y0-celly0, y0, celly0, ay0, ay1]);
2555 {$ENDIF}
2556 if (ay0 <= ay1) then begin ey := ay1; result := false; exit; end;
2557 if (y0 >= celly0) then begin ey := ay0+1; {assert(forEachAtPoint(ex, ey, nil, tagmask) <> nil);} result := true; exit; end;
2558 end
2559 else
2560 begin
2561 // down
2562 while (y0 <= celly1) and (filled[y1-celly0] <> 0) do begin Inc(y0); Inc(ay0); end;
2563 if (ay0 >= ay1) then begin ey := ay1; result := false; exit; end;
2564 if (y0 <= celly1) then begin ey := ay0-1; result := true; exit; end;
2565 end;
2566 end;
2567 end
2568 else
2569 begin
2570 // horizontal
2571 assert(false);
2572 end;
2573 end;
2576 end.