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}
20 interface
23 type
27 public
28 type TGridQueryCB = function (obj: ITP; tag: Integer): Boolean is nested; // return `true` to stop
29 type TGridRayQueryCB = function (obj: ITP; tag: Integer; x, y, prevx, prevy: Integer): Boolean is nested; // return `true` to stop
34 private
35 const
39 private
40 type
43 private
50 private
60 TGridInternalCB = function (grida: Integer; bodyId: TBodyProxyId): Boolean of object; // return `true` to stop
62 private
76 private
94 public
95 constructor Create (aMinPixX, aMinPixY, aPixWidth, aPixHeight: Integer; aTileSize: Integer=GridDefaultTileSize);
98 function insertBody (aObj: ITP; ax, ay, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId;
107 //WARNING: don't modify grid while any query is in progress (no checks are made!)
108 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
109 // no callback: return `true` on the first hit
110 function forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP;
112 //WARNING: don't modify grid while any query is in progress (no checks are made!)
113 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
114 // no callback: return `true` on the first hit
117 //WARNING: don't modify grid while any query is in progress (no checks are made!)
118 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
119 // cb with `(nil)` will be called before processing new tile
120 // no callback: return `true` on the nearest hit
121 function traceRay (x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP; overload;
122 function traceRay (out ex, ey: Integer; x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
126 //WARNING! no sanity checks!
131 implementation
133 uses
137 // ////////////////////////////////////////////////////////////////////////// //
138 procedure TBodyGridBase.TBodyProxyRec.setup (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer);
139 begin
151 // ////////////////////////////////////////////////////////////////////////// //
152 constructor TBodyGridBase.Create (aMinPixX, aMinPixY, aPixWidth, aPixHeight: Integer; aTileSize: Integer=GridDefaultTileSize);
153 var
155 begin
169 // init free list
171 begin
176 // init grid
178 // init proxies
186 e_WriteLog(Format('created grid with size: %dx%d (tile size: %d); pix: %dx%d', [mWidth, mHeight, mTileSize, mWidth*mTileSize, mHeight*mTileSize]), MSG_NOTIFY);
191 begin
200 var
202 begin
205 begin
209 begin
215 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);
220 begin
221 // fix coords
229 begin
235 begin
237 begin
239 begin
241 end
242 else
243 begin
251 var
253 begin
255 begin
256 // no free cells, want more
260 begin
271 //e_WriteLog(Format('grid: allocated new cell #%d (total: %d)', [result, mUsedCells]), MSG_NOTIFY);
276 begin
278 begin
279 //if mCells[idx].body = -1 then exit; // the thing that should not be
288 function TBodyGridBase.allocProxy (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer): TBodyProxyId;
289 var
292 begin
294 begin
295 // no free proxies, resize list
302 // get one from list
307 // add to used list
309 // statistics
315 begin
317 if (mProxyCount = 0) then raise Exception.Create('wutafuuuuu in grid (no allocated proxies, what i should free now?)');
318 // add to free list
326 function TBodyGridBase.forGridRect (x, y, w, h: Integer; cb: TGridInternalCB; bodyId: TBodyProxyId): Boolean;
327 var
330 begin
333 // fix coords
336 // go on
343 begin
347 begin
358 var
363 begin
365 // add body to the given grid cell
368 begin
372 begin
374 begin
375 // can add here
378 exit;
382 // either no room, or no cell at all
391 var
393 begin
400 // absolutely not tested
402 var
406 begin
408 // find and remove cell
412 begin
417 begin
419 begin
420 // i found her!
422 begin
423 // this cell contains no elements, remove it
427 end
428 else
429 begin
430 // remove element from bucket
433 begin
449 // absolutely not tested
451 var
453 begin
460 function TBodyGridBase.insertBody (aObj: ITP; aX, aY, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId;
461 begin
469 begin
477 var
479 begin
492 begin
497 begin
502 // ////////////////////////////////////////////////////////////////////////// //
503 // no callback: return `true` on the first hit
504 function TBodyGridBase.forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1): ITP;
505 var
512 begin
517 // make coords (0,0)-based
523 // restore coords
527 // increase query counter
530 begin
531 // just in case of overflow
538 begin
541 begin
546 begin
548 begin
551 begin
553 end
554 else
555 begin
557 exit;
567 // no callback: return `true` on the first hit
568 function TBodyGridBase.forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP;
569 var
580 begin
589 // fix coords
599 // increase query counter
602 begin
603 // just in case of overflow
607 //e_WriteLog(Format('grid: query #%d: (%d,%d)-(%dx%d)', [mLastQuery, minx, miny, maxx, maxy]), MSG_NOTIFY);
610 // go on
612 begin
616 begin
619 // process cells
622 begin
625 begin
631 //if ((ptag and TagDisabled) = 0) and ((ptag and tagmask) <> 0) and (px.mQueryMark <> lq) then
632 //if ( ((ptag and TagDisabled) = 0) = ignoreDisabled) and ((ptag and tagmask) <> 0) and (px.mQueryMark <> lq) then
633 begin
638 begin
640 end
641 else
642 begin
644 exit;
655 // ////////////////////////////////////////////////////////////////////////// //
656 // no callback: return `true` on the nearest hit
657 function TBodyGridBase.traceRay (x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
658 var
660 begin
665 // no callback: return `true` on the nearest hit
666 function TBodyGridBase.traceRay (out ex, ey: Integer; x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
667 var
694 begin
700 // make coords (0,0)-based
703 //Dec(x0, minx);
704 //Dec(y0, miny);
705 //Dec(x1, minx);
706 //Dec(y1, miny);
719 // `x` and `y` will be in grid coords
723 // increase query counter
726 begin
727 // just in case of overflow
742 begin
743 // prevs are always in map coords
746 // do one step
750 // check for new tile
752 begin
755 begin
756 // new cell
759 // had something in the cell we're leaving?
761 begin
762 // yes, signal cell completion
764 begin
766 end
768 begin
770 exit;
773 // have something in this cell?
776 end
777 else
778 begin
779 // out of grid, had something in the cell we're last processed?
781 begin
782 // yes, signal cell completion
785 begin
787 end
789 begin
791 exit;
795 //lastWasInGrid := false;
798 // has something to process in the current cell?
800 begin
801 // process cell
803 hasUntried := false; // this will be set to `true` if we have some panels we still want to process at the next step
804 // convert coords to map
808 begin
811 begin
816 begin
817 // can we process this wall?
819 begin
822 begin
824 begin
828 exit;
830 end
831 else
832 begin
833 // remember this hitpoint if it is nearer than an old one
836 begin
844 end
845 else
846 begin
847 // this is possibly interesting wall, set "has more to check" flag
852 // next cell
855 // still has something interesting in this cell?
857 begin
858 // nope, don't process this cell anymore; signal cell completion
861 begin
863 end
865 begin
867 exit;
870 // convert coords to grid