DEADSOFTWARE

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