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 `true` on the first hit
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 `true` on the nearest hit
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): ITP;
1061 var
1068 begin
1073 {$IF DEFINED(D2F_DEBUG_XXQ)}
1075 {$ENDIF}
1077 // make coords (0,0)-based
1084 {$IF DEFINED(D2F_DEBUG_XXQ)}
1085 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);
1086 {$ENDIF}
1088 // restore coords
1092 // increase query counter
1095 begin
1096 // just in case of overflow
1102 {$IF DEFINED(D2F_DEBUG_XXQ)}
1103 if (assigned(cb)) then e_WriteLog(Format('2: grid pointquery: (%d,%d); lq=%u', [x, y, lq]), MSG_NOTIFY);
1104 {$ENDIF}
1107 begin
1108 {$IF DEFINED(D2F_DEBUG_XXQ)}
1110 {$ENDIF}
1113 begin
1116 {$IF DEFINED(D2F_DEBUG_XXQ)}
1117 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);
1118 {$ENDIF}
1119 // shit. has to do it this way, so i can change tag in callback
1121 begin
1126 begin
1128 begin
1130 end
1131 else
1132 begin
1134 exit;
1144 // ////////////////////////////////////////////////////////////////////////// //
1145 // no callback: return `true` on the first hit
1146 function TBodyGridBase.forEachInAABB (x, y, w, h: Integer; cb: TGridQueryCB; tagmask: Integer=-1; allowDisabled: Boolean=false): ITP;
1147 const
1149 var
1160 begin
1169 // fix coords
1174 //tsize := mTileSize;
1179 // increase query counter
1182 begin
1183 // just in case of overflow
1187 //e_WriteLog(Format('grid: query #%d: (%d,%d)-(%dx%d)', [mLastQuery, minx, miny, maxx, maxy]), MSG_NOTIFY);
1190 // go on
1192 begin
1196 begin
1199 // process cells
1202 begin
1205 begin
1208 // shit. has to do it this way, so i can change tag in callback
1217 begin
1219 end
1220 else
1221 begin
1223 exit;
1233 // ////////////////////////////////////////////////////////////////////////// //
1234 // no callback: return `true` on the nearest hit
1235 function TBodyGridBase.traceRay (const x0, y0, x1, y1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
1236 var
1238 begin
1243 // no callback: return `true` on the nearest hit
1244 // you are not supposed to understand this
1245 function TBodyGridBase.traceRay (out ex, ey: Integer; const ax0, ay0, ax1, ay1: Integer; cb: TGridRayQueryCB; tagmask: Integer=-1): ITP;
1246 const
1248 var
1274 begin
1282 if (ax0 = ax1) and (ay0 = ay1) then exit; // as the first point is ignored, just get outta here
1293 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1294 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);
1295 {$ENDIF}
1302 // offset query coords to (0,0)-based
1308 // clip rectange
1314 // horizontal setup
1316 begin
1317 // from left to right
1320 end
1321 else
1322 begin
1323 // from right to left
1333 // vertical setup
1335 begin
1336 // from top to bottom
1339 end
1340 else
1341 begin
1342 // from bottom to top
1356 begin
1365 end
1366 else
1367 begin
1381 begin
1382 // clip at top
1388 begin
1397 begin
1398 // clip at left
1409 begin
1410 // clip at bottom
1420 //if (term = xd) then exit; // this is the only point, get out of here
1426 // first move, to skip starting point
1427 // DON'T DO THIS! loop will take care of that
1431 (*
1432 // move coords
1433 if (e >= 0) then begin yd += sty; e -= dx2; end else e += dy2;
1434 xd += stx;
1435 // done?
1436 if (xd = term) then exit;
1437 *)
1439 {$IF DEFINED(D2F_DEBUG)}
1440 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
1441 {$ENDIF}
1442 // DON'T DO THIS! loop will take care of that
1443 //lastGA := (yptr^ div tsize)*gw+(xptr^ div tsize);
1444 //ccidx := mGrid[lastGA];
1446 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1447 //if assigned(dbgRayTraceTileHitCB) then e_WriteLog('1:TRACING!', MSG_NOTIFY);
1448 {$ENDIF}
1450 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1451 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
1452 {$ENDIF}
1454 //if (dbgShowTraceLog) then e_WriteLog(Format('raycast start: (%d,%d)-(%d,%d); xptr^=%d; yptr^=%d', [ax0, ay0, ax1, ay1, xptr^, yptr^]), MSG_NOTIFY);
1456 // increase query counter
1459 begin
1460 // just in case of overflow
1467 // draw it; can omit checks
1469 begin
1470 // check cell(s)
1471 {$IF DEFINED(D2F_DEBUG)}
1472 if (xptr^ < 0) or (yptr^ < 0) or (xptr^ >= gw*tsize) and (yptr^ >= gh*tsize) then raise Exception.Create('raycaster internal error (0)');
1473 {$ENDIF}
1474 // new tile?
1477 begin
1478 // yes
1479 {$IF DEFINED(D2F_DEBUG)}
1480 if assigned(dbgRayTraceTileHitCB) then dbgRayTraceTileHitCB((xptr^ div tsize*tsize)+minx, (yptr^ div tsize*tsize)+miny);
1481 {$ENDIF}
1483 begin
1484 // signal cell completion
1486 begin
1488 end
1490 begin
1492 exit;
1498 // has something to process in this tile?
1500 begin
1501 // process cell
1503 hasUntried := false; // this will be set to `true` if we have some proxies we still want to process at the next step
1504 // convert coords to map (to avoid ajdusting coords inside the loop)
1507 // process cell list
1509 begin
1512 begin
1517 begin
1518 // can we process this proxy?
1520 begin
1523 begin
1525 begin
1529 exit;
1531 (*
1532 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1533 distSq := distanceSq(ax0, ay0, prevx, prevy);
1534 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);
1535 if (distSq < lastDistSq) then
1536 begin
1537 wasHit := true;
1538 lastDistSq := distSq;
1539 ex := prevx;
1540 ey := prevy;
1541 lastObj := px.mObj;
1542 end;
1543 {$ENDIF}
1544 *)
1545 end
1546 else
1547 begin
1548 // remember this hitpoint if it is nearer than an old one
1550 {$IF DEFINED(D2F_DEBUG_RAYTRACE)}
1551 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);
1552 {$ENDIF}
1554 begin
1562 end
1563 else
1564 begin
1565 // this is possibly interesting proxy, set "has more to check" flag
1570 // next cell
1573 // still has something interesting in this cell?
1575 begin
1576 // nope, don't process this cell anymore; signal cell completion
1579 begin
1581 end
1583 begin
1585 exit;
1589 //putPixel(xptr^, yptr^);
1590 // move coords
1599 // ////////////////////////////////////////////////////////////////////////// //
1600 //FIXME! optimize this with real tile walking
1601 function TBodyGridBase.forEachAlongLine (const x0, y0, x1, y1: Integer; cb: TGridAlongQueryCB; tagmask: Integer=-1; log: Boolean=false): ITP;
1602 const
1604 var
1623 //tedist: Integer;
1624 begin
1646 // `x` and `y` will be in grid coords
1650 // increase query counter
1653 begin
1654 // just in case of overflow
1660 // cache various things
1661 //tsize := mTileSize;
1667 // setup distance and flags
1670 // setup starting tile ('cause we'll adjust tile vars only on tile edge crossing)
1673 // it is slightly faster this way
1677 if (log) then e_WriteLog(Format('tracing: (%d,%d)-(%d,%d)', [x, y, x1-minx, y1-miny]), MSG_NOTIFY);
1679 // now trace
1682 begin
1684 // do one step
1687 // invariant: one of those always changed
1688 {$IF DEFINED(D2F_DEBUG)}
1689 if (xerr < 0) and (yerr < 0) then raise Exception.Create('internal bug in grid raycaster (0)');
1690 {$ENDIF}
1693 // invariant: we always doing a step
1694 {$IF DEFINED(D2F_DEBUG)}
1696 {$ENDIF}
1697 begin
1698 // check for crossing tile/grid boundary
1700 begin
1701 // we're still in grid
1703 // check for tile edge crossing
1709 // crossed tile edge?
1711 begin
1712 // setup new cell index
1714 if (log) then e_WriteLog(Format(' stepped to new tile (%d,%d) -- (%d,%d)', [(x div tsize), (y div tsize), x, y]), MSG_NOTIFY);
1715 end
1716 else
1718 begin
1719 // we have nothing interesting here anymore, jump directly to tile edge
1720 (*
1721 if (incx = 0) then
1722 begin
1723 // vertical line
1724 if (incy < 0) then tedist := y-(y and (not tsize)) else tedist := (y or (tsize-1))-y;
1725 if (tedist > 1) then
1726 begin
1727 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);
1728 y += incy*tedist;
1729 Inc(i, tedist);
1730 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);
1731 end;
1732 end
1733 else if (incy = 0) then
1734 begin
1735 // horizontal line
1736 if (incx < 0) then tedist := x-(x and (not tsize)) else tedist := (x or (tsize-1))-x;
1737 if (tedist > 1) then
1738 begin
1739 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);
1740 x += incx*tedist;
1741 Inc(i, tedist);
1742 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);
1743 end;
1744 end;
1745 *)
1746 (*
1747 else if (
1748 // get minimal distance to tile edges
1749 if (incx < 0) then tedist := x-(x and (not tsize)) else if (incx > 0) then tedist := (x or (tsize+1))-x else tedist := 0;
1750 {$IF DEFINED(D2F_DEBUG)}
1751 if (tedist < 0) then raise Exception.Create('internal bug in grid raycaster (2.x)');
1752 {$ENDIF}
1753 if (incy < 0) then f := y-(y and (not tsize)) else if (incy > 0) then f := (y or (tsize+1))-y else f := 0;
1754 {$IF DEFINED(D2F_DEBUG)}
1755 if (f < 0) then raise Exception.Create('internal bug in grid raycaster (2.y)');
1756 {$ENDIF}
1757 if (tedist = 0) then tedist := f else if (f <> 0) then tedist := minInt(tedist, f);
1758 // do jump
1759 if (tedist > 1) then
1760 begin
1761 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);
1762 xerr += dx*tedist;
1763 yerr += dy*tedist;
1764 if (xerr >= 0) then begin x += incx*((xerr div d)+1); xerr := (xerr mod d)-d; end;
1765 if (yerr >= 0) then begin y += incy*((yerr div d)+1); yerr := (yerr mod d)-d; end;
1766 Inc(i, tedist);
1767 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);
1768 end;
1769 *)
1771 end
1772 else
1773 begin
1774 // out of grid
1779 // has something to process in the current cell?
1781 begin
1782 // process cell
1784 // convert coords to map (to avoid ajdusting coords inside the loop)
1785 //Inc(x, minx);
1786 //Inc(y, miny);
1787 // process cell list
1789 begin
1792 begin
1797 begin
1802 // next cell
1806 // convert coords to grid
1807 //Dec(x, minx);
1808 //Dec(y, miny);