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}
26 interface
29 type
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
42 private
43 const
47 public
48 type
51 private
58 private
67 public
76 private
77 type
84 TGridInternalCB = function (grida: Integer; bodyId: TBodyProxyId): Boolean of object; // return `true` to stop
86 private
87 //mTileSize: Integer;
90 public
93 private
106 public
108 {$IF DEFINED(D2F_DEBUG)}
110 {$ENDIF}
112 private
135 public
136 constructor Create (aMinPixX, aMinPixY, aPixWidth, aPixHeight: Integer{; aTileSize: Integer=GridDefaultTileSize});
139 function insertBody (aObj: ITP; ax, ay, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId;
148 // `false` if `body` is surely invalid
153 //WARNING: don't modify grid while any query is in progress (no checks are made!)
154 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
155 // no callback: return `true` on the first hit
156 function forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP;
158 //WARNING: don't modify grid while any query is in progress (no checks are made!)
159 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
160 // no callback: return object on the first hit or nil
161 function forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1; exittag: PInteger=nil): ITP;
163 //WARNING: don't modify grid while any query is in progress (no checks are made!)
164 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
165 // cb with `(nil)` will be called before processing new tile
166 // no callback: return object of the nearest hit or nil
167 // if `inverted` is true, trace will register bodies *exluding* tagmask
168 //WARNING: don't change tags in callbacks here!
169 function traceRay (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP; overload;
170 function traceRay (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
172 //function traceOrthoRayWhileIn (const x0, y0, x1, y1: Integer; tagmask: Integer=-1): ITP; overload;
173 //function traceOrthoRayWhileIn (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; tagmask: Integer=-1): ITP;
175 //WARNING: don't modify grid while any query is in progress (no checks are made!)
176 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
177 // trace line along the grid, calling `cb` for all objects in passed cells, in no particular order
178 //WARNING: don't change tags in callbacks here!
179 function forEachAlongLine (ax0, ay0, ax1, ay1: Integer; cb: TGridQueryCB; tagmask: Integer=-1; log: Boolean=false): ITP;
181 // debug
186 public
187 //WARNING! no sanity checks!
199 // you are not supposed to understand this
200 // returns `true` if there is an intersection, and enter coords
201 // enter coords will be equal to (x0, y0) if starting point is inside the box
202 // if result is `false`, `inx` and `iny` are undefined
203 function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
212 implementation
214 uses
218 // ////////////////////////////////////////////////////////////////////////// //
219 procedure swapInt (var a: Integer; var b: Integer); inline; var t: Integer; begin t := a; a := b; b := t; end;
220 function minInt (a, b: Integer): Integer; inline; begin if (a < b) then result := a else result := b; end;
221 function maxInt (a, b: Integer): Integer; inline; begin if (a > b) then result := a else result := b; end;
223 function distanceSq (x0, y0, x1, y1: Integer): Integer; inline; begin result := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0); end;
226 // ////////////////////////////////////////////////////////////////////////// //
227 // you are not supposed to understand this
228 // returns `true` if there is an intersection, and enter coords
229 // enter coords will be equal to (x0, y0) if starting point is inside the box
230 // if result is `false`, `inx` and `iny` are undefined
231 function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
232 var
240 //!term: Integer;
244 begin
246 // why not
252 begin
253 // check this point
255 exit;
258 // check if staring point is inside the box
259 if (x0 >= bx) and (y0 >= by) and (x0 < bx+bw) and (y0 < by+bh) then begin result := true; exit; end;
261 // clip rectange
267 // horizontal setup
269 begin
270 // from left to right
273 end
274 else
275 begin
276 // from right to left
286 // vertical setup
288 begin
289 // from top to bottom
292 end
293 else
294 begin
295 // from bottom to top
309 begin
318 end
319 else
320 begin
330 //!term := x1;
334 begin
335 // clip at top
341 begin
350 begin
351 // clip at left
361 (*
362 if (y1 > wy1) then
363 begin
364 // clip at bottom
365 temp := dx2*(wy1-y0)+dsx;
366 term := x0+temp div dy2;
367 rem := temp mod dy2;
368 if (rem = 0) then Dec(term);
369 end;
371 if (term > wx1) then term := wx1; // clip at right
373 Inc(term); // draw last point
374 //if (term = xd) then exit; // this is the only point, get out of here
375 *)
379 //!dx2 -= dy2;
387 // ////////////////////////////////////////////////////////////////////////// //
388 procedure TBodyGridBase.TBodyProxyRec.setup (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer);
389 begin
402 begin
407 begin
412 begin
417 begin
422 // ////////////////////////////////////////////////////////////////////////// //
423 constructor TBodyGridBase.Create (aMinPixX, aMinPixY, aPixWidth, aPixHeight: Integer{; aTileSize: Integer=GridDefaultTileSize});
424 var
426 begin
428 {$IF DEFINED(D2F_DEBUG)}
430 {$ENDIF}
431 {
432 if aTileSize < 1 then aTileSize := 1;
433 if aTileSize > 8192 then aTileSize := 8192; // arbitrary limit
434 mTileSize := aTileSize;
435 }
446 // init free list
448 begin
454 // init grid
456 // init proxies
464 e_WriteLog(Format('created grid with size: %dx%d (tile size: %d); pix: %dx%d', [mWidth, mHeight, mTileSize, mWidth*mTileSize, mHeight*mTileSize]), MSG_NOTIFY);
469 begin
477 // ////////////////////////////////////////////////////////////////////////// //
479 var
481 begin
484 begin
488 begin
494 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);
499 var
502 begin
505 begin
508 begin
511 begin
513 if (cc.bodies[f] = body) then cb((g mod mWidth)*mTileSize+mMinX, (g div mWidth)*mTileSize+mMinY);
515 // next cell
523 var
526 begin
534 begin
537 begin
539 if cb(mProxies[cc.bodies[f]].mObj, mProxies[cc.bodies[f]].mTag) then begin result := mProxies[cc.bodies[f]].mObj; exit; end;
541 // next cell
547 // ////////////////////////////////////////////////////////////////////////// //
548 function TBodyGridBase.getGridWidthPx (): Integer; inline; begin result := mWidth*mTileSize; end;
549 function TBodyGridBase.getGridHeightPx (): Integer; inline; begin result := mHeight*mTileSize; end;
553 begin
554 // fix coords
562 begin
564 begin
567 end
568 else
569 begin
578 begin
580 begin
583 end
584 else
585 begin
593 function TBodyGridBase.getBodyDims (body: TBodyProxyId; out rx, ry, rw, rh: Integer): Boolean; inline;
594 begin
596 begin
599 end
600 else
601 begin
612 // ////////////////////////////////////////////////////////////////////////// //
614 begin
620 begin
622 begin
624 begin
626 end
627 else
628 begin
636 begin
641 // ////////////////////////////////////////////////////////////////////////// //
643 var
646 begin
648 begin
649 // no free cells, want more
653 begin
665 //e_WriteLog(Format('grid: allocated new cell #%d (total: %d)', [result, mUsedCells]), MSG_NOTIFY);
670 begin
672 begin
674 begin
685 // ////////////////////////////////////////////////////////////////////////// //
686 function TBodyGridBase.allocProxy (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer): TBodyProxyId;
687 var
690 begin
692 begin
693 // no free proxies, resize list
700 // get one from list
705 // add to used list
707 // statistics
713 begin
715 if (mProxyCount = 0) then raise Exception.Create('wutafuuuuu in grid (no allocated proxies, what i should free now?)');
716 // add to free list
724 // ////////////////////////////////////////////////////////////////////////// //
725 function TBodyGridBase.forGridRect (x, y, w, h: Integer; cb: TGridInternalCB; bodyId: TBodyProxyId): Boolean;
726 const
728 var
731 begin
734 // fix coords
737 // go on
741 //tsize := mTileSize;
744 begin
748 begin
758 // ////////////////////////////////////////////////////////////////////////// //
760 var
765 begin
767 // add body to the given grid cell
770 begin
771 {$IF DEFINED(D2F_DEBUG)}
774 begin
777 begin
779 if (pi.bodies[f] = bodyId) then raise Exception.Create('trying to insert already inserted proxy');
783 {$ENDIF}
786 begin
788 // check "has room" flag
790 begin
791 // can add here
793 begin
795 begin
798 exit;
803 // no room, go to next cell in list (if there is any)
806 // no room in cells, add new cell to list
808 // either no room, or no cell at all
818 var
820 begin
827 // assume that we cannot have one object added to bucket twice
829 var
833 begin
835 // find and remove cell
839 begin
842 begin
844 begin
845 // i found her!
847 begin
848 // this cell contains no elements, remove it
851 exit;
853 // remove element from bucket
855 begin
860 exit;
869 var
871 begin
878 // ////////////////////////////////////////////////////////////////////////// //
879 function TBodyGridBase.insertBody (aObj: ITP; aX, aY, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId;
880 begin
888 begin
895 // ////////////////////////////////////////////////////////////////////////// //
897 var
900 begin
907 {$IF DEFINED(D2F_DEBUG_MOVER)}
908 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);
909 {$ENDIF}
911 // map -> grid
916 // did any corner crossed tile boundary?
921 begin
928 end
929 else
930 begin
938 //TODO: optimize for horizontal/vertical moves
940 var
948 begin
950 // check if tile coords was changed
955 // map -> grid
960 // check for heavy work
971 {$IF DEFINED(D2F_DEBUG_MOVER)}
972 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);
973 {$ENDIF}
975 begin
976 // crossed tile boundary, do heavy work
979 // cycle with old rect, remove body where it is necessary
980 // optimized for horizontal moves
981 {$IF DEFINED(D2F_DEBUG_MOVER)}
982 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);
983 {$ENDIF}
984 // remove stale marks
987 begin
992 {$IF DEFINED(D2F_DEBUG_MOVER)}
994 {$ENDIF}
996 begin
998 begin
999 // this column is completely outside of new rect
1001 begin
1002 {$IF DEFINED(D2F_DEBUG_MOVER)}
1004 {$ENDIF}
1007 end
1008 else
1009 begin
1010 // heavy checks
1012 begin
1014 begin
1015 {$IF DEFINED(D2F_DEBUG_MOVER)}
1017 {$ENDIF}
1024 // cycle with new rect, add body where it is necessary
1027 begin
1032 {$IF DEFINED(D2F_DEBUG_MOVER)}
1034 {$ENDIF}
1036 begin
1038 begin
1039 // this column is completely outside of old rect
1041 begin
1042 {$IF DEFINED(D2F_DEBUG_MOVER)}
1044 {$ENDIF}
1047 end
1048 else
1049 begin
1050 // heavy checks
1052 begin
1054 begin
1055 {$IF DEFINED(D2F_DEBUG_MOVER)}
1057 {$ENDIF}
1064 // done
1065 end
1066 else
1067 begin
1068 {$IF DEFINED(D2F_DEBUG_MOVER)}
1069 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);
1070 {$ENDIF}
1072 // update coordinates
1078 var
1081 begin
1083 // check if tile coords was changed
1089 {$IF DEFINED(D2F_DEBUG_MOVER)}
1090 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);
1091 {$ENDIF}
1094 begin
1095 // crossed tile boundary, do heavy work
1100 end
1101 else
1102 begin
1103 // nothing to do with the grid, just fix size
1110 // ////////////////////////////////////////////////////////////////////////// //
1111 // no callback: return `true` on the first hit
1112 function TBodyGridBase.forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1; exittag: PInteger=nil): ITP;
1113 var
1120 begin
1126 {$IF DEFINED(D2F_DEBUG_XXQ)}
1128 {$ENDIF}
1130 // make coords (0,0)-based
1137 {$IF DEFINED(D2F_DEBUG_XXQ)}
1138 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);
1139 {$ENDIF}
1141 // restore coords
1145 // increase query counter
1148 begin
1149 // just in case of overflow
1155 {$IF DEFINED(D2F_DEBUG_XXQ)}
1156 if (assigned(cb)) then e_WriteLog(Format('2: grid pointquery: (%d,%d); lq=%u', [x, y, lq]), MSG_NOTIFY);
1157 {$ENDIF}
1160 begin
1161 {$IF DEFINED(D2F_DEBUG_XXQ)}
1163 {$ENDIF}
1166 begin
1169 {$IF DEFINED(D2F_DEBUG_XXQ)}
1170 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);
1171 {$ENDIF}
1172 // shit. has to do it this way, so i can change tag in callback
1174 begin
1179 begin
1181 begin
1183 begin
1186 exit;
1188 end
1189 else
1190 begin
1193 exit;
1203 // ////////////////////////////////////////////////////////////////////////// //
1204 // no callback: return `true` on the first hit
1205 function TBodyGridBase.forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP;
1206 const
1208 var
1219 begin
1228 // fix coords
1233 //tsize := mTileSize;
1238 // increase query counter
1241 begin
1242 // just in case of overflow
1246 //e_WriteLog(Format('grid: query #%d: (%d,%d)-(%dx%d)', [mLastQuery, minx, miny, maxx, maxy]), MSG_NOTIFY);
1249 // go on
1251 begin
1255 begin
1258 // process cells
1261 begin
1264 begin
1267 // shit. has to do it this way, so i can change tag in callback
1276 begin
1278 end
1279 else
1280 begin
1282 exit;
1292 // ////////////////////////////////////////////////////////////////////////// //
1293 // no callback: return `true` on the nearest hit
1294 function TBodyGridBase.traceRay (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
1295 var
1297 begin
1302 // no callback: return `true` on the nearest hit
1303 // you are not supposed to understand this
1304 function TBodyGridBase.traceRay (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
1305 const
1307 var
1333 //swapped: Boolean = false; // true: xd is yd, and vice versa
1334 // horizontal walker
1335 {$IFDEF GRID_USE_ORTHO_ACCEL}
1337 //wksign: Integer;
1339 {$ENDIF}
1340 // skipper
1342 begin
1351 begin
1354 begin
1357 exit;
1369 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1370 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);
1371 {$ENDIF}
1378 // offset query coords to (0,0)-based
1384 // clip rectange
1390 // horizontal setup
1392 begin
1393 // from left to right
1396 end
1397 else
1398 begin
1399 // from right to left
1409 // vertical setup
1411 begin
1412 // from top to bottom
1415 end
1416 else
1417 begin
1418 // from bottom to top
1432 begin
1433 //swapped := true;
1442 end
1443 else
1444 begin
1458 begin
1459 // clip at top
1465 begin
1474 begin
1475 // clip at left
1486 begin
1487 // clip at bottom
1497 //if (term = xd) then exit; // this is the only point, get out of here
1503 // first move, to skip starting point
1504 // DON'T DO THIS! loop will take care of that
1506 begin
1507 //FIXME!
1510 begin
1512 begin
1514 begin
1517 end
1518 else
1519 begin
1522 end
1523 else
1524 begin
1529 exit;
1534 (*
1535 // move coords
1536 if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
1537 xd += stx;
1538 // done?
1539 if (xd = term) then exit;
1540 *)
1542 {$IF DEFINED(D2F_DEBUG)}
1543 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
1544 {$ENDIF}
1545 // DON'T DO THIS! loop will take care of that
1546 //lastGA := (yptr^ div tsize)*gw+(xptr^ div tsize);
1547 //ccidx := mGrid[lastGA];
1549 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1550 //if assigned(dbgRayTraceTileHitCB) then e_WriteLog('1:TRACING!', MSG_NOTIFY);
1551 {$ENDIF}
1553 //if (dbgShowTraceLog) then e_WriteLog(Format('raycast start: (%d,%d)-(%d,%d); xptr^=%d; yptr^=%d', [ax0, ay0, ax1, ay1, xptr^, yptr^]), MSG_NOTIFY);
1555 // increase query counter
1558 begin
1559 // just in case of overflow
1565 {$IFDEF GRID_USE_ORTHO_ACCEL}
1566 // if this is strict horizontal/vertical trace, use optimized codepath
1568 begin
1569 // horizontal trace: walk the whole tiles, calculating mindist once for each proxy in cell
1570 // stx < 0: going left, otherwise `stx` is > 0, and we're going right
1571 // vertical trace: walk the whole tiles, calculating mindist once for each proxy in cell
1572 // stx < 0: going up, otherwise `stx` is > 0, and we're going down
1574 if (stx < 0) then begin {wksign := -1;} wklen := -(term-xd); end else begin {wksign := 1;} wklen := term-xd; end;
1575 {$IF DEFINED(D2F_DEBUG)}
1577 {$ENDIF}
1579 // one of those will never change
1582 //prevx := x;
1583 //prevy := y;
1584 {$IF DEFINED(D2F_DEBUG)}
1586 begin
1588 end
1589 else
1590 begin
1593 {$ENDIF}
1595 begin
1596 {$IF DEFINED(D2F_DEBUG)}
1597 if dbgShowTraceLog then e_LogWritefln(' htrace; ga=%d; x=%d, y=%d; y=%d; y=%d', [ga, xptr^+minx, yptr^+miny, y, ay0]);
1598 {$ENDIF}
1599 // new tile?
1601 begin
1604 // convert coords to map (to avoid ajdusting coords inside the loop)
1607 begin
1610 begin
1615 // constant coord should be inside
1618 begin
1620 // inside the proxy?
1623 begin
1624 // setup prev[xy]
1626 begin
1628 begin
1632 exit;
1634 end
1635 else
1636 begin
1638 {$IF DEFINED(D2F_DEBUG)}
1639 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]);
1640 {$ENDIF}
1642 begin
1646 exit;
1649 continue;
1651 // remember this hitpoint if it is nearer than an old one
1652 // setup prev[xy]
1654 begin
1655 // horizontal trace
1659 begin
1660 // going left
1664 end
1665 else
1666 begin
1667 // going right
1672 end
1673 else
1674 begin
1675 // vertical trace
1679 begin
1680 // going up
1684 end
1685 else
1686 begin
1687 // going down
1694 begin
1696 begin
1700 exit;
1702 end
1703 else
1704 begin
1706 {$IF DEFINED(D2F_DEBUG)}
1707 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]);
1708 {$ENDIF}
1710 begin
1720 // next cell
1726 // skip to next tile
1728 begin
1730 begin
1731 // to the right
1733 {$IF DEFINED(D2F_DEBUG)}
1735 {$ENDIF}
1739 end
1740 else
1741 begin
1742 // to the left
1744 {$IF DEFINED(D2F_DEBUG)}
1746 {$ENDIF}
1751 end
1752 else
1753 begin
1755 begin
1756 // to the down
1758 {$IF DEFINED(D2F_DEBUG)}
1760 {$ENDIF}
1764 end
1765 else
1766 begin
1767 // to the up
1769 {$IF DEFINED(D2F_DEBUG)}
1771 {$ENDIF}
1779 // we can travel less than one cell
1781 exit;
1783 {$ENDIF}
1785 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1786 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
1787 {$ENDIF}
1789 //e_LogWritefln('*********************', []);
1791 // can omit checks
1793 begin
1794 // check cell(s)
1795 {$IF DEFINED(D2F_DEBUG)}
1796 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
1797 {$ENDIF}
1798 // new tile?
1800 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1801 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);
1802 {$ENDIF}
1804 begin
1805 // yes
1806 {$IF DEFINED(D2F_DEBUG)}
1807 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
1808 {$ENDIF}
1810 begin
1811 // signal cell completion
1813 begin
1815 end
1817 begin
1819 exit;
1825 // has something to process in this tile?
1827 begin
1828 // process cell
1830 hasUntried := false; // this will be set to `true` if we have some proxies we still want to process at the next step
1831 // convert coords to map (to avoid ajdusting coords inside the loop)
1834 // process cell list
1836 begin
1839 begin
1844 begin
1845 // can we process this proxy?
1847 begin
1850 begin
1852 begin
1856 exit;
1858 end
1859 else
1860 begin
1861 // remember this hitpoint if it is nearer than an old one
1863 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1864 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);
1865 {$ENDIF}
1867 begin
1875 end
1876 else
1877 begin
1878 // this is possibly interesting proxy, set "has more to check" flag
1883 // next cell
1886 // still has something interesting in this cell?
1888 begin
1889 // nope, don't process this cell anymore; signal cell completion
1892 begin
1894 end
1896 begin
1898 exit;
1903 begin
1904 // move to cell edge, as we have nothing to trace here anymore
1907 //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]);
1909 begin
1910 // step
1913 //e_LogWritefln(' xd=%d; yd=%d', [xd, yd]);
1916 //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]);
1919 //putPixel(xptr^, yptr^);
1920 // move coords
1926 // we can travel less than one cell
1928 begin
1930 end
1931 else
1932 begin
1939 // ////////////////////////////////////////////////////////////////////////// //
1940 //FIXME! optimize this with real tile walking
1941 function TBodyGridBase.forEachAlongLine (ax0, ay0, ax1, ay1: Integer; cb: TGridQueryCB; tagmask: Integer=-1; log: Boolean=false): ITP;
1942 const
1944 var
1965 //swapped: Boolean = false; // true: xd is yd, and vice versa
1966 // horizontal walker
1967 {$IFDEF GRID_USE_ORTHO_ACCEL}
1969 //wksign: Integer;
1971 {$ENDIF}
1972 // skipper
1974 begin
1981 begin
1983 exit;
1998 // offset query coords to (0,0)-based
2004 // clip rectange
2010 // horizontal setup
2012 begin
2013 // from left to right
2016 end
2017 else
2018 begin
2019 // from right to left
2029 // vertical setup
2031 begin
2032 // from top to bottom
2035 end
2036 else
2037 begin
2038 // from bottom to top
2052 begin
2053 //swapped := true;
2062 end
2063 else
2064 begin
2078 begin
2079 // clip at top
2085 begin
2094 begin
2095 // clip at left
2106 begin
2107 // clip at bottom
2117 //if (term = xd) then exit; // this is the only point, get out of here
2123 // first move, to skip starting point
2124 // DON'T DO THIS! loop will take care of that
2126 begin
2128 exit;
2131 (*
2132 // move coords
2133 if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
2134 xd += stx;
2135 // done?
2136 if (xd = term) then exit;
2137 *)
2139 {$IF DEFINED(D2F_DEBUG)}
2140 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
2141 {$ENDIF}
2142 // DON'T DO THIS! loop will take care of that
2143 //lastGA := (yptr^ div tsize)*gw+(xptr^ div tsize);
2144 //ccidx := mGrid[lastGA];
2146 // increase query counter
2149 begin
2150 // just in case of overflow
2156 {$IFDEF GRID_USE_ORTHO_ACCEL}
2157 // if this is strict horizontal/vertical trace, use optimized codepath
2159 begin
2160 // horizontal trace: walk the whole tiles, calculating mindist once for each proxy in cell
2161 // stx < 0: going left, otherwise `stx` is > 0, and we're going right
2162 // vertical trace: walk the whole tiles, calculating mindist once for each proxy in cell
2163 // stx < 0: going up, otherwise `stx` is > 0, and we're going down
2165 if (stx < 0) then begin {wksign := -1;} wklen := -(term-xd); end else begin {wksign := 1;} wklen := term-xd; end;
2166 {$IF DEFINED(D2F_DEBUG)}
2168 {$ENDIF}
2170 // one of those will never change
2173 {$IF DEFINED(D2F_DEBUG)}
2175 begin
2177 end
2178 else
2179 begin
2182 {$ENDIF}
2184 begin
2185 {$IF DEFINED(D2F_DEBUG)}
2186 if dbgShowTraceLog then e_LogWritefln(' htrace; ga=%d; x=%d, y=%d; y=%d; y=%d', [ga, xptr^+minx, yptr^+miny, y, ay0]);
2187 {$ENDIF}
2188 // new tile?
2190 begin
2193 // convert coords to map (to avoid ajdusting coords inside the loop)
2196 begin
2199 begin
2204 begin
2207 begin
2209 end
2210 else
2211 begin
2213 exit;
2217 // next cell
2221 // skip to next tile
2223 begin
2225 begin
2226 // to the right
2228 {$IF DEFINED(D2F_DEBUG)}
2230 {$ENDIF}
2234 end
2235 else
2236 begin
2237 // to the left
2239 {$IF DEFINED(D2F_DEBUG)}
2241 {$ENDIF}
2246 end
2247 else
2248 begin
2250 begin
2251 // to the down
2253 {$IF DEFINED(D2F_DEBUG)}
2255 {$ENDIF}
2259 end
2260 else
2261 begin
2262 // to the up
2264 {$IF DEFINED(D2F_DEBUG)}
2266 {$ENDIF}
2274 exit;
2276 {$ENDIF}
2278 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
2279 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
2280 {$ENDIF}
2283 // can omit checks
2285 begin
2286 // check cell(s)
2287 {$IF DEFINED(D2F_DEBUG)}
2288 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
2289 {$ENDIF}
2290 // new tile?
2292 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
2293 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);
2294 {$ENDIF}
2296 begin
2297 // yes
2298 {$IF DEFINED(D2F_DEBUG)}
2299 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
2300 {$ENDIF}
2304 // has something to process in this tile?
2306 begin
2307 // process cell
2309 // convert coords to map (to avoid ajdusting coords inside the loop)
2312 // process cell list
2314 begin
2317 begin
2322 begin
2325 begin
2327 end
2328 else
2329 begin
2331 exit;
2335 // next cell
2338 // nothing more interesting in this cell
2341 // move to cell edge, as we have nothing to trace here anymore
2344 //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]);
2346 begin
2347 // step
2350 //e_LogWritefln(' xd=%d; yd=%d', [xd, yd]);
2353 //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]);
2355 //putPixel(xptr^, yptr^);
2356 // move coords