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
77 private
78 type
87 TGridInternalCB = function (grida: Integer; bodyId: TBodyProxyId): Boolean of object; // return `true` to stop
89 private
90 //mTileSize: Integer;
94 public
97 type
99 private
103 public
110 private
124 public
126 {$IF DEFINED(D2F_DEBUG)}
128 {$ENDIF}
130 private
153 public
154 constructor Create (aMinPixX, aMinPixY, aPixWidth, aPixHeight: Integer{; aTileSize: Integer=GridDefaultTileSize});
157 function insertBody (aObj: ITP; ax, ay, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId;
166 // `false` if `body` is surely invalid
171 //WARNING: don't modify grid while any query is in progress (no checks are made!)
172 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
173 // no callback: return `true` on the first hit
174 function forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP;
176 //WARNING: don't modify grid while any query is in progress (no checks are made!)
177 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
178 // no callback: return object on the first hit or nil
179 function forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1; exittag: PInteger=nil): ITP;
183 //WARNING: don't modify grid while any query is in progress (no checks are made!)
184 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
185 // cb with `(nil)` will be called before processing new tile
186 // no callback: return object of the nearest hit or nil
187 // if `inverted` is true, trace will register bodies *exluding* tagmask
188 //WARNING: don't change tags in callbacks here!
189 function traceRay (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP; overload;
190 function traceRay (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
192 //function traceOrthoRayWhileIn (const x0, y0, x1, y1: Integer; tagmask: Integer=-1): ITP; overload;
193 //function traceOrthoRayWhileIn (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; tagmask: Integer=-1): ITP;
195 //WARNING: don't modify grid while any query is in progress (no checks are made!)
196 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
197 // trace line along the grid, calling `cb` for all objects in passed cells, in no particular order
198 //WARNING: don't change tags in callbacks here!
199 function forEachAlongLine (ax0, ay0, ax1, ay1: Integer; cb: TGridQueryCB; tagmask: Integer=-1; log: Boolean=false): ITP;
201 // debug
206 public
207 //WARNING! no sanity checks!
219 // you are not supposed to understand this
220 // returns `true` if there is an intersection, and enter coords
221 // enter coords will be equal to (x0, y0) if starting point is inside the box
222 // if result is `false`, `inx` and `iny` are undefined
223 function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
232 implementation
234 uses
238 // ////////////////////////////////////////////////////////////////////////// //
239 procedure swapInt (var a: Integer; var b: Integer); inline; var t: Integer; begin t := a; a := b; b := t; end;
240 function minInt (a, b: Integer): Integer; inline; begin if (a < b) then result := a else result := b; end;
241 function maxInt (a, b: Integer): Integer; inline; begin if (a > b) then result := a else result := b; end;
243 function distanceSq (x0, y0, x1, y1: Integer): Integer; inline; begin result := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0); end;
246 // ////////////////////////////////////////////////////////////////////////// //
247 // you are not supposed to understand this
248 // returns `true` if there is an intersection, and enter coords
249 // enter coords will be equal to (x0, y0) if starting point is inside the box
250 // if result is `false`, `inx` and `iny` are undefined
251 function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
252 var
260 //!term: Integer;
264 begin
266 // why not
272 begin
273 // check this point
275 exit;
278 // check if staring point is inside the box
279 if (x0 >= bx) and (y0 >= by) and (x0 < bx+bw) and (y0 < by+bh) then begin result := true; exit; end;
281 // clip rectange
287 // horizontal setup
289 begin
290 // from left to right
293 end
294 else
295 begin
296 // from right to left
306 // vertical setup
308 begin
309 // from top to bottom
312 end
313 else
314 begin
315 // from bottom to top
329 begin
338 end
339 else
340 begin
350 //!term := x1;
354 begin
355 // clip at top
361 begin
370 begin
371 // clip at left
381 (*
382 if (y1 > wy1) then
383 begin
384 // clip at bottom
385 temp := dx2*(wy1-y0)+dsx;
386 term := x0+temp div dy2;
387 rem := temp mod dy2;
388 if (rem = 0) then Dec(term);
389 end;
391 if (term > wx1) then term := wx1; // clip at right
393 Inc(term); // draw last point
394 //if (term = xd) then exit; // this is the only point, get out of here
395 *)
399 //!dx2 -= dy2;
407 // ////////////////////////////////////////////////////////////////////////// //
408 procedure TBodyGridBase.TBodyProxyRec.setup (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer);
409 begin
422 begin
427 begin
432 begin
437 begin
442 // ////////////////////////////////////////////////////////////////////////// //
443 constructor TBodyGridBase.TAtPointEnumerator.Create (acells: TCellArray; aidx: Integer; agetpx: TGetProxyFn);
444 begin
453 begin
455 begin
457 begin
461 exit;
471 begin
476 // ////////////////////////////////////////////////////////////////////////// //
477 constructor TBodyGridBase.Create (aMinPixX, aMinPixY, aPixWidth, aPixHeight: Integer{; aTileSize: Integer=GridDefaultTileSize});
478 var
480 begin
482 {$IF DEFINED(D2F_DEBUG)}
484 {$ENDIF}
485 {
486 if aTileSize < 1 then aTileSize := 1;
487 if aTileSize > 8192 then aTileSize := 8192; // arbitrary limit
488 mTileSize := aTileSize;
489 }
500 // init free list
502 begin
508 // init grid
510 // init proxies
518 e_WriteLog(Format('created grid with size: %dx%d (tile size: %d); pix: %dx%d', [mWidth, mHeight, mTileSize, mWidth*mTileSize, mHeight*mTileSize]), MSG_NOTIFY);
523 begin
531 // ////////////////////////////////////////////////////////////////////////// //
533 var
535 begin
538 begin
542 begin
548 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);
553 var
556 begin
559 begin
562 begin
565 begin
567 if (cc.bodies[f] = body) then cb((g mod mWidth)*mTileSize+mMinX, (g div mWidth)*mTileSize+mMinY);
569 // next cell
577 var
580 begin
588 begin
591 begin
593 if cb(mProxies[cc.bodies[f]].mObj, mProxies[cc.bodies[f]].mTag) then begin result := mProxies[cc.bodies[f]].mObj; exit; end;
595 // next cell
601 // ////////////////////////////////////////////////////////////////////////// //
602 function TBodyGridBase.getGridWidthPx (): Integer; inline; begin result := mWidth*mTileSize; end;
603 function TBodyGridBase.getGridHeightPx (): Integer; inline; begin result := mHeight*mTileSize; end;
607 begin
608 // fix coords
616 begin
618 begin
621 end
622 else
623 begin
632 begin
634 begin
637 end
638 else
639 begin
647 function TBodyGridBase.getBodyDims (body: TBodyProxyId; out rx, ry, rw, rh: Integer): Boolean; inline;
648 begin
650 begin
653 end
654 else
655 begin
666 // ////////////////////////////////////////////////////////////////////////// //
668 begin
674 begin
676 begin
678 begin
680 end
681 else
682 begin
690 begin
695 // ////////////////////////////////////////////////////////////////////////// //
697 var
700 begin
702 begin
703 // no free cells, want more
707 begin
719 //e_WriteLog(Format('grid: allocated new cell #%d (total: %d)', [result, mUsedCells]), MSG_NOTIFY);
724 begin
726 begin
728 begin
739 // ////////////////////////////////////////////////////////////////////////// //
740 function TBodyGridBase.allocProxy (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer): TBodyProxyId;
741 var
744 begin
746 begin
747 // no free proxies, resize list
754 // get one from list
759 // add to used list
761 // statistics
767 begin
769 if (mProxyCount = 0) then raise Exception.Create('wutafuuuuu in grid (no allocated proxies, what i should free now?)');
770 // add to free list
778 // ////////////////////////////////////////////////////////////////////////// //
779 function TBodyGridBase.forGridRect (x, y, w, h: Integer; cb: TGridInternalCB; bodyId: TBodyProxyId): Boolean;
780 const
782 var
785 begin
788 // fix coords
791 // go on
795 //tsize := mTileSize;
798 begin
802 begin
812 // ////////////////////////////////////////////////////////////////////////// //
814 var
819 begin
821 // add body to the given grid cell
824 begin
825 {$IF DEFINED(D2F_DEBUG)}
828 begin
831 begin
833 if (pi.bodies[f] = bodyId) then raise Exception.Create('trying to insert already inserted proxy');
837 {$ENDIF}
840 begin
842 // check "has room" flag
844 begin
845 // can add here
847 begin
849 begin
852 exit;
857 // no room, go to next cell in list (if there is any)
860 // no room in cells, add new cell to list
862 // either no room, or no cell at all
872 var
874 begin
881 // assume that we cannot have one object added to bucket twice
883 var
887 begin
889 // find and remove cell
893 begin
896 begin
898 begin
899 // i found her!
901 begin
902 // this cell contains no elements, remove it
905 exit;
907 // remove element from bucket
909 begin
914 exit;
923 var
925 begin
932 // ////////////////////////////////////////////////////////////////////////// //
933 function TBodyGridBase.insertBody (aObj: ITP; aX, aY, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId;
934 begin
942 begin
949 // ////////////////////////////////////////////////////////////////////////// //
951 var
954 begin
961 {$IF DEFINED(D2F_DEBUG_MOVER)}
962 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);
963 {$ENDIF}
965 // map -> grid
970 // did any corner crossed tile boundary?
975 begin
982 end
983 else
984 begin
992 //TODO: optimize for horizontal/vertical moves
994 var
1002 begin
1004 // check if tile coords was changed
1009 // map -> grid
1014 // check for heavy work
1025 {$IF DEFINED(D2F_DEBUG_MOVER)}
1026 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);
1027 {$ENDIF}
1029 begin
1030 // crossed tile boundary, do heavy work
1033 // cycle with old rect, remove body where it is necessary
1034 // optimized for horizontal moves
1035 {$IF DEFINED(D2F_DEBUG_MOVER)}
1036 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);
1037 {$ENDIF}
1038 // remove stale marks
1041 begin
1046 {$IF DEFINED(D2F_DEBUG_MOVER)}
1048 {$ENDIF}
1050 begin
1052 begin
1053 // this column is completely outside of new rect
1055 begin
1056 {$IF DEFINED(D2F_DEBUG_MOVER)}
1058 {$ENDIF}
1061 end
1062 else
1063 begin
1064 // heavy checks
1066 begin
1068 begin
1069 {$IF DEFINED(D2F_DEBUG_MOVER)}
1071 {$ENDIF}
1078 // cycle with new rect, add body where it is necessary
1081 begin
1086 {$IF DEFINED(D2F_DEBUG_MOVER)}
1088 {$ENDIF}
1090 begin
1092 begin
1093 // this column is completely outside of old rect
1095 begin
1096 {$IF DEFINED(D2F_DEBUG_MOVER)}
1098 {$ENDIF}
1101 end
1102 else
1103 begin
1104 // heavy checks
1106 begin
1108 begin
1109 {$IF DEFINED(D2F_DEBUG_MOVER)}
1111 {$ENDIF}
1118 // done
1119 end
1120 else
1121 begin
1122 {$IF DEFINED(D2F_DEBUG_MOVER)}
1123 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);
1124 {$ENDIF}
1126 // update coordinates
1132 var
1135 begin
1137 // check if tile coords was changed
1143 {$IF DEFINED(D2F_DEBUG_MOVER)}
1144 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);
1145 {$ENDIF}
1148 begin
1149 // crossed tile boundary, do heavy work
1154 end
1155 else
1156 begin
1157 // nothing to do with the grid, just fix size
1164 // ////////////////////////////////////////////////////////////////////////// //
1166 var
1168 begin
1171 if (x >= 0) and (y >= 0) and (x < mWidth*mTileSize) and (y < mHeight*mTileSize) then cidx := mGrid[(y div mTileSize)*mWidth+(x div mTileSize)];
1176 // ////////////////////////////////////////////////////////////////////////// //
1177 // no callback: return `true` on the first hit
1178 function TBodyGridBase.forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1; exittag: PInteger=nil): ITP;
1179 var
1186 begin
1192 {$IF DEFINED(D2F_DEBUG_XXQ)}
1194 {$ENDIF}
1196 // make coords (0,0)-based
1203 {$IF DEFINED(D2F_DEBUG_XXQ)}
1204 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);
1205 {$ENDIF}
1207 // restore coords
1211 // increase query counter
1214 begin
1215 // just in case of overflow
1221 {$IF DEFINED(D2F_DEBUG_XXQ)}
1222 if (assigned(cb)) then e_WriteLog(Format('2: grid pointquery: (%d,%d); lq=%u', [x, y, lq]), MSG_NOTIFY);
1223 {$ENDIF}
1226 begin
1227 {$IF DEFINED(D2F_DEBUG_XXQ)}
1229 {$ENDIF}
1232 begin
1235 {$IF DEFINED(D2F_DEBUG_XXQ)}
1236 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);
1237 {$ENDIF}
1238 // shit. has to do it this way, so i can change tag in callback
1240 begin
1245 begin
1247 begin
1249 begin
1252 exit;
1254 end
1255 else
1256 begin
1259 exit;
1269 // ////////////////////////////////////////////////////////////////////////// //
1270 // no callback: return `true` on the first hit
1271 function TBodyGridBase.forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP;
1272 const
1274 var
1285 begin
1294 // fix coords
1299 //tsize := mTileSize;
1307 // increase query counter
1310 begin
1311 // just in case of overflow
1315 //e_WriteLog(Format('grid: query #%d: (%d,%d)-(%dx%d)', [mLastQuery, minx, miny, maxx, maxy]), MSG_NOTIFY);
1318 // go on
1320 begin
1324 begin
1327 // process cells
1330 begin
1333 begin
1336 // shit. has to do it this way, so i can change tag in callback
1345 begin
1347 end
1348 else
1349 begin
1352 exit;
1364 // ////////////////////////////////////////////////////////////////////////// //
1365 // no callback: return `true` on the nearest hit
1366 function TBodyGridBase.traceRay (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
1367 var
1369 begin
1374 // no callback: return `true` on the nearest hit
1375 // you are not supposed to understand this
1376 function TBodyGridBase.traceRay (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
1377 const
1379 var
1405 //swapped: Boolean = false; // true: xd is yd, and vice versa
1406 // horizontal walker
1407 {$IFDEF GRID_USE_ORTHO_ACCEL}
1409 //wksign: Integer;
1411 {$ENDIF}
1412 // skipper
1414 begin
1423 begin
1426 begin
1429 exit;
1441 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1442 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);
1443 {$ENDIF}
1450 // offset query coords to (0,0)-based
1456 // clip rectange
1462 // horizontal setup
1464 begin
1465 // from left to right
1468 end
1469 else
1470 begin
1471 // from right to left
1481 // vertical setup
1483 begin
1484 // from top to bottom
1487 end
1488 else
1489 begin
1490 // from bottom to top
1504 begin
1505 //swapped := true;
1514 end
1515 else
1516 begin
1530 begin
1531 // clip at top
1537 begin
1546 begin
1547 // clip at left
1558 begin
1559 // clip at bottom
1569 //if (term = xd) then exit; // this is the only point, get out of here
1575 // first move, to skip starting point
1576 // DON'T DO THIS! loop will take care of that
1578 begin
1579 //FIXME!
1582 begin
1584 begin
1586 begin
1589 end
1590 else
1591 begin
1594 end
1595 else
1596 begin
1601 exit;
1606 (*
1607 // move coords
1608 if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
1609 xd += stx;
1610 // done?
1611 if (xd = term) then exit;
1612 *)
1614 {$IF DEFINED(D2F_DEBUG)}
1615 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
1616 {$ENDIF}
1617 // DON'T DO THIS! loop will take care of that
1618 //lastGA := (yptr^ div tsize)*gw+(xptr^ div tsize);
1619 //ccidx := mGrid[lastGA];
1621 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1622 //if assigned(dbgRayTraceTileHitCB) then e_WriteLog('1:TRACING!', MSG_NOTIFY);
1623 {$ENDIF}
1625 //if (dbgShowTraceLog) then e_WriteLog(Format('raycast start: (%d,%d)-(%d,%d); xptr^=%d; yptr^=%d', [ax0, ay0, ax1, ay1, xptr^, yptr^]), MSG_NOTIFY);
1630 // increase query counter
1633 begin
1634 // just in case of overflow
1640 {$IFDEF GRID_USE_ORTHO_ACCEL}
1641 // if this is strict horizontal/vertical trace, use optimized codepath
1643 begin
1644 // horizontal trace: walk the whole tiles, calculating mindist once for each proxy in cell
1645 // stx < 0: going left, otherwise `stx` is > 0, and we're going right
1646 // vertical trace: walk the whole tiles, calculating mindist once for each proxy in cell
1647 // stx < 0: going up, otherwise `stx` is > 0, and we're going down
1649 if (stx < 0) then begin {wksign := -1;} wklen := -(term-xd); end else begin {wksign := 1;} wklen := term-xd; end;
1650 {$IF DEFINED(D2F_DEBUG)}
1652 {$ENDIF}
1654 // one of those will never change
1657 //prevx := x;
1658 //prevy := y;
1660 begin
1661 {$IF DEFINED(D2F_DEBUG)}
1662 if dbgShowTraceLog then e_LogWritefln(' htrace; ga=%d; x=%d, y=%d; y=%d; y=%d', [ga, xptr^+minx, yptr^+miny, y, ay0]);
1663 {$ENDIF}
1664 // new tile?
1666 begin
1669 // convert coords to map (to avoid ajdusting coords inside the loop)
1672 begin
1675 begin
1680 // constant coord should be inside
1683 begin
1685 // inside the proxy?
1688 begin
1689 // setup prev[xy]
1691 begin
1693 begin
1698 exit;
1700 end
1701 else
1702 begin
1704 {$IF DEFINED(D2F_DEBUG)}
1705 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]);
1706 {$ENDIF}
1708 begin
1713 exit;
1716 continue;
1718 // remember this hitpoint if it is nearer than an old one
1719 // setup prev[xy]
1721 begin
1722 // horizontal trace
1726 begin
1727 // going left
1731 end
1732 else
1733 begin
1734 // going right
1739 end
1740 else
1741 begin
1742 // vertical trace
1746 begin
1747 // going up
1751 end
1752 else
1753 begin
1754 // going down
1761 begin
1763 begin
1768 exit;
1770 end
1771 else
1772 begin
1774 {$IF DEFINED(D2F_DEBUG)}
1775 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]);
1776 {$ENDIF}
1778 begin
1788 // next cell
1792 if assigned(cb) and cb(nil, 0, x, y, x, y) then begin result := lastObj; mInQuery := false; exit; end;
1794 // skip to next tile
1796 begin
1798 begin
1799 // to the right
1801 {$IF DEFINED(D2F_DEBUG)}
1803 {$ENDIF}
1807 end
1808 else
1809 begin
1810 // to the left
1812 {$IF DEFINED(D2F_DEBUG)}
1814 {$ENDIF}
1819 end
1820 else
1821 begin
1823 begin
1824 // to the down
1826 {$IF DEFINED(D2F_DEBUG)}
1828 {$ENDIF}
1832 end
1833 else
1834 begin
1835 // to the up
1837 {$IF DEFINED(D2F_DEBUG)}
1839 {$ENDIF}
1847 // we can travel less than one cell
1850 exit;
1852 {$ENDIF}
1854 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1855 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
1856 {$ENDIF}
1858 //e_LogWritefln('*********************', []);
1860 // can omit checks
1862 begin
1863 // check cell(s)
1864 {$IF DEFINED(D2F_DEBUG)}
1865 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
1866 {$ENDIF}
1867 // new tile?
1869 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1870 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);
1871 {$ENDIF}
1873 begin
1874 // yes
1875 {$IF DEFINED(D2F_DEBUG)}
1876 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
1877 {$ENDIF}
1879 begin
1880 // signal cell completion
1882 begin
1883 if cb(nil, 0, xptr^+minx, yptr^+miny, prevx, prevy) then begin result := lastObj; mInQuery := false; exit; end;
1884 end
1886 begin
1889 exit;
1895 // has something to process in this tile?
1897 begin
1898 // process cell
1900 hasUntried := false; // this will be set to `true` if we have some proxies we still want to process at the next step
1901 // convert coords to map (to avoid ajdusting coords inside the loop)
1904 // process cell list
1906 begin
1909 begin
1914 begin
1915 // can we process this proxy?
1917 begin
1920 begin
1922 begin
1927 exit;
1929 end
1930 else
1931 begin
1932 // remember this hitpoint if it is nearer than an old one
1934 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1935 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);
1936 {$ENDIF}
1938 begin
1946 end
1947 else
1948 begin
1949 // this is possibly interesting proxy, set "has more to check" flag
1954 // next cell
1957 // still has something interesting in this cell?
1959 begin
1960 // nope, don't process this cell anymore; signal cell completion
1963 begin
1965 end
1967 begin
1970 exit;
1975 begin
1976 // move to cell edge, as we have nothing to trace here anymore
1979 //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]);
1981 begin
1982 // step
1985 //e_LogWritefln(' xd=%d; yd=%d', [xd, yd]);
1988 //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]);
1991 //putPixel(xptr^, yptr^);
1992 // move coords
1998 // we can travel less than one cell
2000 begin
2002 end
2003 else
2004 begin
2013 // ////////////////////////////////////////////////////////////////////////// //
2014 //FIXME! optimize this with real tile walking
2015 function TBodyGridBase.forEachAlongLine (ax0, ay0, ax1, ay1: Integer; cb: TGridQueryCB; tagmask: Integer=-1; log: Boolean=false): ITP;
2016 const
2018 var
2039 //swapped: Boolean = false; // true: xd is yd, and vice versa
2040 // horizontal walker
2041 {$IFDEF GRID_USE_ORTHO_ACCEL}
2043 //wksign: Integer;
2045 {$ENDIF}
2046 // skipper
2048 begin
2055 begin
2057 exit;
2072 // offset query coords to (0,0)-based
2078 // clip rectange
2084 // horizontal setup
2086 begin
2087 // from left to right
2090 end
2091 else
2092 begin
2093 // from right to left
2103 // vertical setup
2105 begin
2106 // from top to bottom
2109 end
2110 else
2111 begin
2112 // from bottom to top
2126 begin
2127 //swapped := true;
2136 end
2137 else
2138 begin
2152 begin
2153 // clip at top
2159 begin
2168 begin
2169 // clip at left
2180 begin
2181 // clip at bottom
2191 //if (term = xd) then exit; // this is the only point, get out of here
2197 // first move, to skip starting point
2198 // DON'T DO THIS! loop will take care of that
2200 begin
2202 exit;
2205 (*
2206 // move coords
2207 if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
2208 xd += stx;
2209 // done?
2210 if (xd = term) then exit;
2211 *)
2213 {$IF DEFINED(D2F_DEBUG)}
2214 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
2215 {$ENDIF}
2216 // DON'T DO THIS! loop will take care of that
2217 //lastGA := (yptr^ div tsize)*gw+(xptr^ div tsize);
2218 //ccidx := mGrid[lastGA];
2223 // increase query counter
2226 begin
2227 // just in case of overflow
2233 {$IFDEF GRID_USE_ORTHO_ACCEL}
2234 // if this is strict horizontal/vertical trace, use optimized codepath
2236 begin
2237 // horizontal trace: walk the whole tiles, calculating mindist once for each proxy in cell
2238 // stx < 0: going left, otherwise `stx` is > 0, and we're going right
2239 // vertical trace: walk the whole tiles, calculating mindist once for each proxy in cell
2240 // stx < 0: going up, otherwise `stx` is > 0, and we're going down
2242 if (stx < 0) then begin {wksign := -1;} wklen := -(term-xd); end else begin {wksign := 1;} wklen := term-xd; end;
2243 {$IF DEFINED(D2F_DEBUG)}
2245 {$ENDIF}
2248 begin
2249 {$IF DEFINED(D2F_DEBUG)}
2250 if dbgShowTraceLog then e_LogWritefln(' htrace; ga=%d; x=%d, y=%d; ay0=%d', [ga, xptr^+minx, yptr^+miny, ay0]);
2251 {$ENDIF}
2252 // new tile?
2254 begin
2257 // convert coords to map (to avoid ajdusting coords inside the loop)
2259 begin
2262 begin
2267 begin
2270 begin
2272 end
2273 else
2274 begin
2277 exit;
2281 // next cell
2285 // skip to next tile
2287 begin
2289 begin
2290 // to the right
2292 {$IF DEFINED(D2F_DEBUG)}
2294 {$ENDIF}
2298 end
2299 else
2300 begin
2301 // to the left
2303 {$IF DEFINED(D2F_DEBUG)}
2305 {$ENDIF}
2310 end
2311 else
2312 begin
2314 begin
2315 // to the down
2317 {$IF DEFINED(D2F_DEBUG)}
2319 {$ENDIF}
2323 end
2324 else
2325 begin
2326 // to the up
2328 {$IF DEFINED(D2F_DEBUG)}
2330 {$ENDIF}
2339 exit;
2341 {$ENDIF}
2343 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
2344 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
2345 {$ENDIF}
2348 // can omit checks
2350 begin
2351 // check cell(s)
2352 {$IF DEFINED(D2F_DEBUG)}
2353 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
2354 {$ENDIF}
2355 // new tile?
2357 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
2358 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);
2359 {$ENDIF}
2361 begin
2362 // yes
2363 {$IF DEFINED(D2F_DEBUG)}
2364 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
2365 {$ENDIF}
2369 // has something to process in this tile?
2371 begin
2372 // process cell
2374 // process cell list
2376 begin
2379 begin
2384 begin
2387 begin
2389 end
2390 else
2391 begin
2394 exit;
2398 // next cell
2401 // nothing more interesting in this cell
2404 // move to cell edge, as we have nothing to trace here anymore
2407 //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]);
2409 begin
2410 // step
2413 //e_LogWritefln(' xd=%d; yd=%d', [xd, yd]);
2416 //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]);
2418 //putPixel(xptr^, yptr^);
2419 // move coords