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
30 type TGridAlongQueryCB = function (obj: ITP; tag: Integer): Boolean is nested; // return `true` to stop
35 private
36 const
40 private
41 type
44 private
51 private
61 TGridInternalCB = function (grida: Integer; bodyId: TBodyProxyId): Boolean of object; // return `true` to stop
63 private
64 //mTileSize: Integer;
67 private
80 public
83 private
104 public
105 constructor Create (aMinPixX, aMinPixY, aPixWidth, aPixHeight: Integer{; aTileSize: Integer=GridDefaultTileSize});
108 function insertBody (aObj: ITP; ax, ay, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId;
117 // `false` if `body` is surely invalid
120 //WARNING: don't modify grid while any query is in progress (no checks are made!)
121 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
122 // no callback: return `true` on the first hit
123 function forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP;
125 //WARNING: don't modify grid while any query is in progress (no checks are made!)
126 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
127 // no callback: return `true` on the first hit
130 //WARNING: don't modify grid while any query is in progress (no checks are made!)
131 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
132 // cb with `(nil)` will be called before processing new tile
133 // no callback: return `true` on the nearest hit
134 function traceRay (x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP; overload;
135 function traceRay (out ex, ey: Integer; ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
137 //WARNING: don't modify grid while any query is in progress (no checks are made!)
138 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
139 // trace line along the grid, calling `cb` for all objects in passed cells, in no particular order
140 function forEachAlongLine (x0, y0, x1, y1: Integer; cb: TGridAlongQueryCB; tagmask: Integer=-1): ITP;
144 //WARNING! no sanity checks!
154 // you are not supposed to understand this
155 // returns `true` if there is an intersection, and enter coords
156 // enter coords will be equal to (x0, y0) if starting point is inside the box
157 // if result is `false`, `inx` and `iny` are undefined
158 function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
164 implementation
166 uses
170 // ////////////////////////////////////////////////////////////////////////// //
171 procedure swapInt (var a: Integer; var b: Integer); inline; var t: Integer; begin t := a; a := b; b := t; end;
173 function distanceSq (x0, y0, x1, y1: Integer): Integer; inline; begin result := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0); end;
176 // ////////////////////////////////////////////////////////////////////////// //
177 // you are not supposed to understand this
178 // returns `true` if there is an intersection, and enter coords
179 // enter coords will be equal to (x0, y0) if starting point is inside the box
180 // if result is `false`, `inx` and `iny` are undefined
181 function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
182 var
190 //!term: Integer;
194 begin
196 // why not
202 begin
203 // check this point
205 exit;
208 // check if staring point is inside the box
209 if (x0 >= bx) and (y0 >= by) and (x0 < bx+bw) and (y0 < by+bh) then begin result := true; exit; end;
211 // clip rectange
217 // horizontal setup
219 begin
220 // from left to right
223 end
224 else
225 begin
226 // from right to left
236 // vertical setup
238 begin
239 // from top to bottom
242 end
243 else
244 begin
245 // from bottom to top
259 begin
268 end
269 else
270 begin
280 //!term := x1;
284 begin
285 // clip at top
291 begin
300 begin
301 // clip at left
311 (*
312 if (y1 > wy1) then
313 begin
314 // clip at bottom
315 temp := dx2*(wy1-y0)+dsx;
316 term := x0+temp div dy2;
317 rem := temp mod dy2;
318 if (rem = 0) then Dec(term);
319 end;
321 if (term > wx1) then term := wx1; // clip at right
323 Inc(term); // draw last point
324 //if (term = xd) then exit; // this is the only point, get out of here
325 *)
329 //!dx2 -= dy2;
337 // ////////////////////////////////////////////////////////////////////////// //
338 procedure TBodyGridBase.TBodyProxyRec.setup (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer);
339 begin
351 // ////////////////////////////////////////////////////////////////////////// //
352 constructor TBodyGridBase.Create (aMinPixX, aMinPixY, aPixWidth, aPixHeight: Integer{; aTileSize: Integer=GridDefaultTileSize});
353 var
355 begin
357 {
358 if aTileSize < 1 then aTileSize := 1;
359 if aTileSize > 8192 then aTileSize := 8192; // arbitrary limit
360 mTileSize := aTileSize;
361 }
372 // init free list
374 begin
379 // init grid
381 // init proxies
389 e_WriteLog(Format('created grid with size: %dx%d (tile size: %d); pix: %dx%d', [mWidth, mHeight, mTileSize, mWidth*mTileSize, mHeight*mTileSize]), MSG_NOTIFY);
394 begin
402 // ////////////////////////////////////////////////////////////////////////// //
404 var
406 begin
409 begin
413 begin
419 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);
423 // ////////////////////////////////////////////////////////////////////////// //
424 function TBodyGridBase.getGridWidthPx (): Integer; inline; begin result := mWidth*mTileSize; end;
425 function TBodyGridBase.getGridHeightPx (): Integer; inline; begin result := mHeight*mTileSize; end;
429 begin
430 // fix coords
438 begin
440 begin
443 end
444 else
445 begin
453 // ////////////////////////////////////////////////////////////////////////// //
455 begin
461 begin
463 begin
465 begin
467 end
468 else
469 begin
476 // ////////////////////////////////////////////////////////////////////////// //
478 var
480 begin
482 begin
483 // no free cells, want more
487 begin
498 //e_WriteLog(Format('grid: allocated new cell #%d (total: %d)', [result, mUsedCells]), MSG_NOTIFY);
503 begin
505 begin
506 //if mCells[idx].body = -1 then exit; // the thing that should not be
515 // ////////////////////////////////////////////////////////////////////////// //
516 function TBodyGridBase.allocProxy (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer): TBodyProxyId;
517 var
520 begin
522 begin
523 // no free proxies, resize list
530 // get one from list
535 // add to used list
537 // statistics
543 begin
545 if (mProxyCount = 0) then raise Exception.Create('wutafuuuuu in grid (no allocated proxies, what i should free now?)');
546 // add to free list
554 // ////////////////////////////////////////////////////////////////////////// //
555 function TBodyGridBase.forGridRect (x, y, w, h: Integer; cb: TGridInternalCB; bodyId: TBodyProxyId): Boolean;
556 const
558 var
561 begin
564 // fix coords
567 // go on
571 //tsize := mTileSize;
574 begin
578 begin
588 // ////////////////////////////////////////////////////////////////////////// //
590 var
595 begin
597 // add body to the given grid cell
600 begin
604 begin
606 begin
607 // can add here
610 exit;
614 // either no room, or no cell at all
623 var
625 begin
632 // absolutely not tested
634 var
638 begin
640 // find and remove cell
644 begin
649 begin
651 begin
652 // i found her!
654 begin
655 // this cell contains no elements, remove it
659 end
660 else
661 begin
662 // remove element from bucket
665 begin
681 // absolutely not tested
683 var
685 begin
692 // ////////////////////////////////////////////////////////////////////////// //
693 function TBodyGridBase.insertBody (aObj: ITP; aX, aY, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId;
694 begin
702 begin
709 // ////////////////////////////////////////////////////////////////////////// //
711 var
714 begin
722 // did any corner crossed tile boundary?
727 begin
734 end
735 else
736 begin
745 var
748 begin
751 // check if tile coords was changed
756 begin
757 // crossed tile boundary, do heavy work
759 end
760 else
761 begin
762 // nothing to do with the grid, just fix coordinates
769 var
773 begin
776 // check if tile coords was changed
784 begin
785 // crossed tile boundary, do heavy work
787 end
788 else
789 begin
790 // nothing to do with the grid, just fix size
797 // ////////////////////////////////////////////////////////////////////////// //
798 // no callback: return `true` on the first hit
799 function TBodyGridBase.forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1): ITP;
800 var
807 begin
812 // make coords (0,0)-based
818 // restore coords
822 // increase query counter
825 begin
826 // just in case of overflow
833 begin
836 begin
841 begin
843 begin
846 begin
848 end
849 else
850 begin
852 exit;
862 // ////////////////////////////////////////////////////////////////////////// //
863 // no callback: return `true` on the first hit
864 function TBodyGridBase.forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP;
865 const
867 var
878 begin
887 // fix coords
892 //tsize := mTileSize;
897 // increase query counter
900 begin
901 // just in case of overflow
905 //e_WriteLog(Format('grid: query #%d: (%d,%d)-(%dx%d)', [mLastQuery, minx, miny, maxx, maxy]), MSG_NOTIFY);
908 // go on
910 begin
914 begin
917 // process cells
920 begin
923 begin
929 //if ((ptag and TagDisabled) = 0) and ((ptag and tagmask) <> 0) and (px.mQueryMark <> lq) then
930 //if ( ((ptag and TagDisabled) = 0) = ignoreDisabled) and ((ptag and tagmask) <> 0) and (px.mQueryMark <> lq) then
931 begin
936 begin
938 end
939 else
940 begin
942 exit;
953 // ////////////////////////////////////////////////////////////////////////// //
954 // no callback: return `true` on the nearest hit
955 function TBodyGridBase.traceRay (x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
956 var
958 begin
963 // no callback: return `true` on the nearest hit
964 // you are not supposed to understand this
965 function TBodyGridBase.traceRay (out ex, ey: Integer; ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
966 const
968 var
994 begin
1002 if (ax0 = ax1) and (ay0 = ay1) then exit; // as the first point is ignored, just get outta here
1018 // offset query coords to (0,0)-based
1024 // clip rectange
1030 // horizontal setup
1032 begin
1033 // from left to right
1036 end
1037 else
1038 begin
1039 // from right to left
1049 // vertical setup
1051 begin
1052 // from top to bottom
1055 end
1056 else
1057 begin
1058 // from bottom to top
1072 begin
1081 end
1082 else
1083 begin
1097 begin
1098 // clip at top
1104 begin
1113 begin
1114 // clip at left
1125 begin
1126 // clip at bottom
1136 //if (term = xd) then exit; // this is the only point, get out of here
1142 // first move, to skip starting point
1146 // move coords
1149 // done?
1152 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ > mHeight*tsize) then raise Exception.Create('raycaster internal error (0)');
1154 //if (dbgShowTraceLog) then e_WriteLog(Format('raycast start: (%d,%d)-(%d,%d); xptr^=%d; yptr^=%d', [ax0, ay0, ax1, ay1, xptr^, yptr^]), MSG_NOTIFY);
1156 // restore query coords
1159 //Inc(ax1, minx);
1160 //Inc(ay1, miny);
1162 // increase query counter
1165 begin
1166 // just in case of overflow
1173 // draw it; can omit checks
1175 begin
1176 // check cell(s)
1177 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ > mHeight*tsize) then raise Exception.Create('raycaster internal error (0)');
1178 // new tile?
1181 begin
1182 // yes
1184 begin
1185 // signal cell completion
1187 begin
1189 end
1191 begin
1193 exit;
1199 // has something to process in this tile?
1201 begin
1202 // process cell
1204 hasUntried := false; // this will be set to `true` if we have some proxies we still want to process at the next step
1205 // convert coords to map (to avoid ajdusting coords inside the loop)
1208 // process cell list
1210 begin
1213 begin
1218 begin
1219 // can we process this proxy?
1221 begin
1224 begin
1226 begin
1230 exit;
1232 end
1233 else
1234 begin
1235 // remember this hitpoint if it is nearer than an old one
1238 begin
1246 end
1247 else
1248 begin
1249 // this is possibly interesting proxy, set "has more to check" flag
1254 // next cell
1257 // still has something interesting in this cell?
1259 begin
1260 // nope, don't process this cell anymore; signal cell completion
1263 begin
1265 end
1267 begin
1269 exit;
1273 //putPixel(xptr^, yptr^);
1274 // move coords
1283 // ////////////////////////////////////////////////////////////////////////// //
1284 //FIXME! optimize this with real tile walking
1285 function TBodyGridBase.forEachAlongLine (x0, y0, x1, y1: Integer; cb: TGridAlongQueryCB; tagmask: Integer=-1): ITP;
1286 const
1288 var
1307 begin
1326 // `x` and `y` will be in grid coords
1330 // increase query counter
1333 begin
1334 // just in case of overflow
1340 // cache various things
1341 //tsize := mTileSize;
1347 // setup distance and flags
1350 // setup starting tile ('cause we'll adjust tile vars only on tile edge crossing)
1353 // it is slightly faster this way
1357 // now trace
1359 begin
1360 // do one step
1363 // invariant: one of those always changed
1364 if (xerr < 0) and (yerr < 0) then raise Exception.Create('internal bug in grid raycaster (0)');
1367 // invariant: we always doing a step
1369 begin
1370 // check for crossing tile/grid boundary
1372 begin
1373 // we're still in grid
1375 // check for tile edge crossing
1381 // crossed tile edge?
1383 begin
1384 // setup new cell index
1387 end
1388 else
1389 begin
1390 // out of grid
1395 // has something to process in the current cell?
1397 begin
1398 // process cell
1400 // convert coords to map (to avoid ajdusting coords inside the loop)
1403 // process cell list
1405 begin
1408 begin
1413 begin
1418 // next cell
1422 // convert coords to grid