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}
25 interface
28 type
32 public
33 type TGridQueryCB = function (obj: ITP; tag: Integer): Boolean is nested; // return `true` to stop
34 type TGridRayQueryCB = function (obj: ITP; tag: Integer; x, y, prevx, prevy: Integer): Boolean is nested; // return `true` to stop
35 type TGridAlongQueryCB = function (obj: ITP; tag: Integer): Boolean is nested; // return `true` to stop
42 private
43 const
47 private
48 type
51 private
58 private
68 TGridInternalCB = function (grida: Integer; bodyId: TBodyProxyId): Boolean of object; // return `true` to stop
70 private
71 //mTileSize: Integer;
74 public
77 private
90 public
92 {$IF DEFINED(D2F_DEBUG)}
94 {$ENDIF}
96 private
117 public
118 constructor Create (aMinPixX, aMinPixY, aPixWidth, aPixHeight: Integer{; aTileSize: Integer=GridDefaultTileSize});
121 function insertBody (aObj: ITP; ax, ay, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId;
130 // `false` if `body` is surely invalid
135 //WARNING: don't modify grid while any query is in progress (no checks are made!)
136 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
137 // no callback: return `true` on the first hit
138 function forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP;
140 //WARNING: don't modify grid while any query is in progress (no checks are made!)
141 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
142 // no callback: return object on the first hit or nil
143 function forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1; exittag: PInteger=nil): ITP;
145 //WARNING: don't modify grid while any query is in progress (no checks are made!)
146 // you can set enabled/disabled flag, tho (but iterator can still return objects disabled inside it)
147 // cb with `(nil)` will be called before processing new tile
148 // no callback: return object of the nearest hit or nil
149 //WARNING: don't change tags in callbacks here!
150 function traceRay (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP; overload;
151 function traceRay (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
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 // trace line along the grid, calling `cb` for all objects in passed cells, in no particular order
156 //WARNING: don't change tags in callbacks here!
157 function forEachAlongLine (const x0, y0, x1, y1: Integer; cb: TGridAlongQueryCB; tagmask: Integer=-1; log: Boolean=false): ITP;
159 // debug
164 //WARNING! no sanity checks!
174 // you are not supposed to understand this
175 // returns `true` if there is an intersection, and enter coords
176 // enter coords will be equal to (x0, y0) if starting point is inside the box
177 // if result is `false`, `inx` and `iny` are undefined
178 function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
187 implementation
189 uses
193 // ////////////////////////////////////////////////////////////////////////// //
194 procedure swapInt (var a: Integer; var b: Integer); inline; var t: Integer; begin t := a; a := b; b := t; end;
195 function minInt (a, b: Integer): Integer; inline; begin if (a < b) then result := a else result := b; end;
196 function maxInt (a, b: Integer): Integer; inline; begin if (a > b) then result := a else result := b; end;
198 function distanceSq (x0, y0, x1, y1: Integer): Integer; inline; begin result := (x1-x0)*(x1-x0)+(y1-y0)*(y1-y0); end;
201 // ////////////////////////////////////////////////////////////////////////// //
202 // you are not supposed to understand this
203 // returns `true` if there is an intersection, and enter coords
204 // enter coords will be equal to (x0, y0) if starting point is inside the box
205 // if result is `false`, `inx` and `iny` are undefined
206 function lineAABBIntersects (x0, y0, x1, y1: Integer; bx, by, bw, bh: Integer; out inx, iny: Integer): Boolean;
207 var
215 //!term: Integer;
219 begin
221 // why not
227 begin
228 // check this point
230 exit;
233 // check if staring point is inside the box
234 if (x0 >= bx) and (y0 >= by) and (x0 < bx+bw) and (y0 < by+bh) then begin result := true; exit; end;
236 // clip rectange
242 // horizontal setup
244 begin
245 // from left to right
248 end
249 else
250 begin
251 // from right to left
261 // vertical setup
263 begin
264 // from top to bottom
267 end
268 else
269 begin
270 // from bottom to top
284 begin
293 end
294 else
295 begin
305 //!term := x1;
309 begin
310 // clip at top
316 begin
325 begin
326 // clip at left
336 (*
337 if (y1 > wy1) then
338 begin
339 // clip at bottom
340 temp := dx2*(wy1-y0)+dsx;
341 term := x0+temp div dy2;
342 rem := temp mod dy2;
343 if (rem = 0) then Dec(term);
344 end;
346 if (term > wx1) then term := wx1; // clip at right
348 Inc(term); // draw last point
349 //if (term = xd) then exit; // this is the only point, get out of here
350 *)
354 //!dx2 -= dy2;
362 // ////////////////////////////////////////////////////////////////////////// //
363 procedure TBodyGridBase.TBodyProxyRec.setup (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer);
364 begin
376 // ////////////////////////////////////////////////////////////////////////// //
377 constructor TBodyGridBase.Create (aMinPixX, aMinPixY, aPixWidth, aPixHeight: Integer{; aTileSize: Integer=GridDefaultTileSize});
378 var
380 begin
382 {$IF DEFINED(D2F_DEBUG)}
384 {$ENDIF}
385 {
386 if aTileSize < 1 then aTileSize := 1;
387 if aTileSize > 8192 then aTileSize := 8192; // arbitrary limit
388 mTileSize := aTileSize;
389 }
400 // init free list
402 begin
408 // init grid
410 // init proxies
418 e_WriteLog(Format('created grid with size: %dx%d (tile size: %d); pix: %dx%d', [mWidth, mHeight, mTileSize, mWidth*mTileSize, mHeight*mTileSize]), MSG_NOTIFY);
423 begin
431 // ////////////////////////////////////////////////////////////////////////// //
433 var
435 begin
438 begin
442 begin
448 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);
453 var
456 begin
459 begin
462 begin
465 begin
467 if (cc.bodies[f] = body) then cb((g mod mWidth)*mTileSize+mMinX, (g div mWidth)*mTileSize+mMinY);
469 // next cell
477 var
480 begin
488 begin
491 begin
493 if cb(mProxies[cc.bodies[f]].mObj, mProxies[cc.bodies[f]].mTag) then begin result := mProxies[cc.bodies[f]].mObj; exit; end;
495 // next cell
501 // ////////////////////////////////////////////////////////////////////////// //
502 function TBodyGridBase.getGridWidthPx (): Integer; inline; begin result := mWidth*mTileSize; end;
503 function TBodyGridBase.getGridHeightPx (): Integer; inline; begin result := mHeight*mTileSize; end;
507 begin
508 // fix coords
516 begin
518 begin
521 end
522 else
523 begin
532 begin
534 begin
537 end
538 else
539 begin
547 function TBodyGridBase.getBodyDims (body: TBodyProxyId; out rx, ry, rw, rh: Integer): Boolean; inline;
548 begin
550 begin
553 end
554 else
555 begin
566 // ////////////////////////////////////////////////////////////////////////// //
568 begin
574 begin
576 begin
578 begin
580 end
581 else
582 begin
589 // ////////////////////////////////////////////////////////////////////////// //
591 var
594 begin
596 begin
597 // no free cells, want more
601 begin
613 //e_WriteLog(Format('grid: allocated new cell #%d (total: %d)', [result, mUsedCells]), MSG_NOTIFY);
618 begin
620 begin
622 begin
633 // ////////////////////////////////////////////////////////////////////////// //
634 function TBodyGridBase.allocProxy (aX, aY, aWidth, aHeight: Integer; aObj: ITP; aTag: Integer): TBodyProxyId;
635 var
638 begin
640 begin
641 // no free proxies, resize list
648 // get one from list
653 // add to used list
655 // statistics
661 begin
663 if (mProxyCount = 0) then raise Exception.Create('wutafuuuuu in grid (no allocated proxies, what i should free now?)');
664 // add to free list
672 // ////////////////////////////////////////////////////////////////////////// //
673 function TBodyGridBase.forGridRect (x, y, w, h: Integer; cb: TGridInternalCB; bodyId: TBodyProxyId): Boolean;
674 const
676 var
679 begin
682 // fix coords
685 // go on
689 //tsize := mTileSize;
692 begin
696 begin
706 // ////////////////////////////////////////////////////////////////////////// //
708 var
713 begin
715 // add body to the given grid cell
718 begin
719 {$IF DEFINED(D2F_DEBUG)}
722 begin
725 begin
727 if (pi.bodies[f] = bodyId) then raise Exception.Create('trying to insert already inserted proxy');
731 {$ENDIF}
734 begin
736 // check "has room" flag
738 begin
739 // can add here
741 begin
743 begin
746 exit;
751 // no room, go to next cell in list (if there is any)
754 // no room in cells, add new cell to list
756 // either no room, or no cell at all
766 var
768 begin
775 // assume that we cannot have one object added to bucket twice
777 var
781 begin
783 // find and remove cell
787 begin
790 begin
792 begin
793 // i found her!
795 begin
796 // this cell contains no elements, remove it
799 exit;
801 // remove element from bucket
803 begin
808 exit;
817 var
819 begin
826 // ////////////////////////////////////////////////////////////////////////// //
827 function TBodyGridBase.insertBody (aObj: ITP; aX, aY, aWidth, aHeight: Integer; aTag: Integer=-1): TBodyProxyId;
828 begin
836 begin
843 // ////////////////////////////////////////////////////////////////////////// //
845 var
848 begin
855 {$IF DEFINED(D2F_DEBUG_MOVER)}
856 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);
857 {$ENDIF}
859 // map -> grid
864 // did any corner crossed tile boundary?
869 begin
876 end
877 else
878 begin
886 //TODO: optimize for horizontal/vertical moves
888 var
896 begin
898 // check if tile coords was changed
903 // map -> grid
908 // check for heavy work
919 {$IF DEFINED(D2F_DEBUG_MOVER)}
920 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);
921 {$ENDIF}
923 begin
924 // crossed tile boundary, do heavy work
927 // cycle with old rect, remove body where it is necessary
928 // optimized for horizontal moves
929 {$IF DEFINED(D2F_DEBUG_MOVER)}
930 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);
931 {$ENDIF}
932 // remove stale marks
935 begin
940 {$IF DEFINED(D2F_DEBUG_MOVER)}
942 {$ENDIF}
944 begin
946 begin
947 // this column is completely outside of new rect
949 begin
950 {$IF DEFINED(D2F_DEBUG_MOVER)}
952 {$ENDIF}
955 end
956 else
957 begin
958 // heavy checks
960 begin
962 begin
963 {$IF DEFINED(D2F_DEBUG_MOVER)}
965 {$ENDIF}
972 // cycle with new rect, add body where it is necessary
975 begin
980 {$IF DEFINED(D2F_DEBUG_MOVER)}
982 {$ENDIF}
984 begin
986 begin
987 // this column is completely outside of old rect
989 begin
990 {$IF DEFINED(D2F_DEBUG_MOVER)}
992 {$ENDIF}
995 end
996 else
997 begin
998 // heavy checks
1000 begin
1002 begin
1003 {$IF DEFINED(D2F_DEBUG_MOVER)}
1005 {$ENDIF}
1012 // done
1013 end
1014 else
1015 begin
1016 {$IF DEFINED(D2F_DEBUG_MOVER)}
1017 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);
1018 {$ENDIF}
1020 // update coordinates
1026 var
1029 begin
1031 // check if tile coords was changed
1037 {$IF DEFINED(D2F_DEBUG_MOVER)}
1038 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);
1039 {$ENDIF}
1042 begin
1043 // crossed tile boundary, do heavy work
1048 end
1049 else
1050 begin
1051 // nothing to do with the grid, just fix size
1058 // ////////////////////////////////////////////////////////////////////////// //
1059 // no callback: return `true` on the first hit
1060 function TBodyGridBase.forEachAtPoint (x, y: Integer; cb: TGridQueryCB; tagmask: Integer=-1; exittag: PInteger=nil): ITP;
1061 var
1068 begin
1074 {$IF DEFINED(D2F_DEBUG_XXQ)}
1076 {$ENDIF}
1078 // make coords (0,0)-based
1085 {$IF DEFINED(D2F_DEBUG_XXQ)}
1086 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);
1087 {$ENDIF}
1089 // restore coords
1093 // increase query counter
1096 begin
1097 // just in case of overflow
1103 {$IF DEFINED(D2F_DEBUG_XXQ)}
1104 if (assigned(cb)) then e_WriteLog(Format('2: grid pointquery: (%d,%d); lq=%u', [x, y, lq]), MSG_NOTIFY);
1105 {$ENDIF}
1108 begin
1109 {$IF DEFINED(D2F_DEBUG_XXQ)}
1111 {$ENDIF}
1114 begin
1117 {$IF DEFINED(D2F_DEBUG_XXQ)}
1118 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);
1119 {$ENDIF}
1120 // shit. has to do it this way, so i can change tag in callback
1122 begin
1127 begin
1129 begin
1131 begin
1134 exit;
1136 end
1137 else
1138 begin
1141 exit;
1151 // ////////////////////////////////////////////////////////////////////////// //
1152 // no callback: return `true` on the first hit
1153 function TBodyGridBase.forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP;
1154 const
1156 var
1167 begin
1176 // fix coords
1181 //tsize := mTileSize;
1186 // increase query counter
1189 begin
1190 // just in case of overflow
1194 //e_WriteLog(Format('grid: query #%d: (%d,%d)-(%dx%d)', [mLastQuery, minx, miny, maxx, maxy]), MSG_NOTIFY);
1197 // go on
1199 begin
1203 begin
1206 // process cells
1209 begin
1212 begin
1215 // shit. has to do it this way, so i can change tag in callback
1224 begin
1226 end
1227 else
1228 begin
1230 exit;
1240 // ////////////////////////////////////////////////////////////////////////// //
1241 // no callback: return `true` on the nearest hit
1242 function TBodyGridBase.traceRay (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
1243 var
1245 begin
1250 // no callback: return `true` on the nearest hit
1251 // you are not supposed to understand this
1252 function TBodyGridBase.traceRay (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
1253 const
1255 var
1281 begin
1290 begin
1293 begin
1295 begin
1297 begin
1300 end
1301 else
1302 begin
1305 end
1306 else
1307 begin
1312 exit;
1324 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1325 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);
1326 {$ENDIF}
1333 // offset query coords to (0,0)-based
1339 // clip rectange
1345 // horizontal setup
1347 begin
1348 // from left to right
1351 end
1352 else
1353 begin
1354 // from right to left
1364 // vertical setup
1366 begin
1367 // from top to bottom
1370 end
1371 else
1372 begin
1373 // from bottom to top
1387 begin
1396 end
1397 else
1398 begin
1412 begin
1413 // clip at top
1419 begin
1428 begin
1429 // clip at left
1440 begin
1441 // clip at bottom
1451 //if (term = xd) then exit; // this is the only point, get out of here
1457 // first move, to skip starting point
1458 // DON'T DO THIS! loop will take care of that
1460 begin
1463 begin
1465 begin
1467 begin
1470 end
1471 else
1472 begin
1475 end
1476 else
1477 begin
1482 exit;
1487 (*
1488 // move coords
1489 if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
1490 xd += stx;
1491 // done?
1492 if (xd = term) then exit;
1493 *)
1495 {$IF DEFINED(D2F_DEBUG)}
1496 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
1497 {$ENDIF}
1498 // DON'T DO THIS! loop will take care of that
1499 //lastGA := (yptr^ div tsize)*gw+(xptr^ div tsize);
1500 //ccidx := mGrid[lastGA];
1502 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1503 //if assigned(dbgRayTraceTileHitCB) then e_WriteLog('1:TRACING!', MSG_NOTIFY);
1504 {$ENDIF}
1506 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1507 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
1508 {$ENDIF}
1510 //if (dbgShowTraceLog) then e_WriteLog(Format('raycast start: (%d,%d)-(%d,%d); xptr^=%d; yptr^=%d', [ax0, ay0, ax1, ay1, xptr^, yptr^]), MSG_NOTIFY);
1512 // increase query counter
1515 begin
1516 // just in case of overflow
1523 // draw it; can omit checks
1525 begin
1526 // check cell(s)
1527 {$IF DEFINED(D2F_DEBUG)}
1528 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
1529 {$ENDIF}
1530 // new tile?
1532 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1533 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);
1534 {$ENDIF}
1536 begin
1537 // yes
1538 {$IF DEFINED(D2F_DEBUG)}
1539 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
1540 {$ENDIF}
1542 begin
1543 // signal cell completion
1545 begin
1547 end
1549 begin
1551 exit;
1557 // has something to process in this tile?
1559 begin
1560 // process cell
1562 hasUntried := false; // this will be set to `true` if we have some proxies we still want to process at the next step
1563 // convert coords to map (to avoid ajdusting coords inside the loop)
1566 // process cell list
1568 begin
1571 begin
1576 begin
1577 // can we process this proxy?
1579 begin
1582 begin
1584 begin
1588 exit;
1590 (*
1591 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1592 distSq := distanceSq(ax0, ay0, prevx, prevy);
1593 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);
1594 if (distSq < lastDistSq) then
1595 begin
1596 wasHit := true;
1597 lastDistSq := distSq;
1598 ex := prevx;
1599 ey := prevy;
1600 lastObj := px.mObj;
1601 end;
1602 {$ENDIF}
1603 *)
1604 end
1605 else
1606 begin
1607 // remember this hitpoint if it is nearer than an old one
1609 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1610 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);
1611 {$ENDIF}
1613 begin
1621 end
1622 else
1623 begin
1624 // this is possibly interesting proxy, set "has more to check" flag
1629 // next cell
1632 // still has something interesting in this cell?
1634 begin
1635 // nope, don't process this cell anymore; signal cell completion
1638 begin
1640 end
1642 begin
1644 exit;
1648 //putPixel(xptr^, yptr^);
1649 // move coords
1655 // we can travel less than one cell
1657 begin
1659 end
1660 else
1661 begin
1668 // ////////////////////////////////////////////////////////////////////////// //
1669 //FIXME! optimize this with real tile walking
1670 function TBodyGridBase.forEachAlongLine (const x0, y0, x1, y1: Integer; cb: TGridAlongQueryCB; tagmask: Integer=-1; log: Boolean=false): ITP;
1671 const
1673 var
1692 //tedist: Integer;
1693 begin
1715 // `x` and `y` will be in grid coords
1719 // increase query counter
1722 begin
1723 // just in case of overflow
1729 // cache various things
1730 //tsize := mTileSize;
1736 // setup distance and flags
1739 // setup starting tile ('cause we'll adjust tile vars only on tile edge crossing)
1742 // it is slightly faster this way
1746 if (log) then e_WriteLog(Format('tracing: (%d,%d)-(%d,%d)', [x, y, x1-minx, y1-miny]), MSG_NOTIFY);
1748 // now trace
1751 begin
1753 // do one step
1756 // invariant: one of those always changed
1757 {$IF DEFINED(D2F_DEBUG)}
1758 if (xerr < 0) and (yerr < 0) then raise Exception.Create('internal bug in grid raycaster (0)');
1759 {$ENDIF}
1762 // invariant: we always doing a step
1763 {$IF DEFINED(D2F_DEBUG)}
1765 {$ENDIF}
1766 begin
1767 // check for crossing tile/grid boundary
1769 begin
1770 // we're still in grid
1772 // check for tile edge crossing
1778 // crossed tile edge?
1780 begin
1781 // setup new cell index
1783 if (log) then e_WriteLog(Format(' stepped to new tile (%d,%d) -- (%d,%d)', [(x div tsize), (y div tsize), x, y]), MSG_NOTIFY);
1784 end
1785 else
1787 begin
1788 // we have nothing interesting here anymore, jump directly to tile edge
1789 (*
1790 if (incx = 0) then
1791 begin
1792 // vertical line
1793 if (incy < 0) then tedist := y-(y and (not tsize)) else tedist := (y or (tsize-1))-y;
1794 if (tedist > 1) then
1795 begin
1796 if (log) then e_WriteLog(Format(' doing vertical jump from tile (%d,%d) - (%d,%d) by %d steps', [(x div tsize), (y div tsize), x, y, tedist]), MSG_NOTIFY);
1797 y += incy*tedist;
1798 Inc(i, tedist);
1799 if (log) then e_WriteLog(Format(' jumped to tile (%d,%d) - (%d,%d) by %d steps', [(x div tsize), (y div tsize), x, y, tedist]), MSG_NOTIFY);
1800 end;
1801 end
1802 else if (incy = 0) then
1803 begin
1804 // horizontal line
1805 if (incx < 0) then tedist := x-(x and (not tsize)) else tedist := (x or (tsize-1))-x;
1806 if (tedist > 1) then
1807 begin
1808 if (log) then e_WriteLog(Format(' doing horizontal jump from tile (%d,%d) - (%d,%d) by %d steps', [(x div tsize), (y div tsize), x, y, tedist]), MSG_NOTIFY);
1809 x += incx*tedist;
1810 Inc(i, tedist);
1811 if (log) then e_WriteLog(Format(' jumped to tile (%d,%d) - (%d,%d) by %d steps', [(x div tsize), (y div tsize), x, y, tedist]), MSG_NOTIFY);
1812 end;
1813 end;
1814 *)
1815 (*
1816 else if (
1817 // get minimal distance to tile edges
1818 if (incx < 0) then tedist := x-(x and (not tsize)) else if (incx > 0) then tedist := (x or (tsize+1))-x else tedist := 0;
1819 {$IF DEFINED(D2F_DEBUG)}
1820 if (tedist < 0) then raise Exception.Create('internal bug in grid raycaster (2.x)');
1821 {$ENDIF}
1822 if (incy < 0) then f := y-(y and (not tsize)) else if (incy > 0) then f := (y or (tsize+1))-y else f := 0;
1823 {$IF DEFINED(D2F_DEBUG)}
1824 if (f < 0) then raise Exception.Create('internal bug in grid raycaster (2.y)');
1825 {$ENDIF}
1826 if (tedist = 0) then tedist := f else if (f <> 0) then tedist := minInt(tedist, f);
1827 // do jump
1828 if (tedist > 1) then
1829 begin
1830 if (log) then e_WriteLog(Format(' doing jump from tile (%d,%d) - (%d,%d) by %d steps', [(x div tsize), (y div tsize), x, y, tedist]), MSG_NOTIFY);
1831 xerr += dx*tedist;
1832 yerr += dy*tedist;
1833 if (xerr >= 0) then begin x += incx*((xerr div d)+1); xerr := (xerr mod d)-d; end;
1834 if (yerr >= 0) then begin y += incy*((yerr div d)+1); yerr := (yerr mod d)-d; end;
1835 Inc(i, tedist);
1836 if (log) then e_WriteLog(Format(' jumped to tile (%d,%d) - (%d,%d) by %d steps', [(x div tsize), (y div tsize), x, y, tedist]), MSG_NOTIFY);
1837 end;
1838 *)
1840 end
1841 else
1842 begin
1843 // out of grid
1848 // has something to process in the current cell?
1850 begin
1851 // process cell
1853 // convert coords to map (to avoid ajdusting coords inside the loop)
1854 //Inc(x, minx);
1855 //Inc(y, miny);
1856 // process cell list
1858 begin
1861 begin
1866 begin
1871 // next cell
1875 // convert coords to grid
1876 //Dec(x, minx);
1877 //Dec(y, miny);