-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRN_NavMeshTesterTool.pas
1428 lines (1248 loc) · 46.4 KB
/
RN_NavMeshTesterTool.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
{$POINTERMATH ON}
unit RN_NavMeshTesterTool;
interface
uses
Classes, Controls, StdCtrls, ExtCtrls, Unit_FrameTesterTool,
RN_DetourNavMesh, RN_DetourNavMeshHelper, RN_DetourNavMeshQuery, RN_DetourStatus, RN_Sample;
type
TToolMode =
(
TOOLMODE_PATHFIND_FOLLOW,
TOOLMODE_PATHFIND_STRAIGHT,
TOOLMODE_PATHFIND_SLICED,
TOOLMODE_RAYCAST,
TOOLMODE_DISTANCE_TO_WALL,
TOOLMODE_FIND_POLYS_IN_CIRCLE,
TOOLMODE_FIND_POLYS_IN_SHAPE,
TOOLMODE_FIND_LOCAL_NEIGHBOURHOOD
);
const MAX_POLYS = 256;
const MAX_SMOOTH = 2048;
const MAX_RAND_POINTS = 64;
const MAX_STEER_POINTS = 10;
type
TNavMeshTesterTool = class (TSampleTool)
private
fFrame: TFrameTesterTool;
fUpdateUI: Boolean;
m_sample: TSample;
m_navMesh: TdtNavMesh;
m_navQuery: TdtNavMeshQuery;
m_filter: TdtQueryFilter;
m_pathFindStatus: TdtStatus;
m_toolMode: TToolMode;
m_straightPathOptions: TdtStraightPathOptions;
m_startRef: TdtPolyRef;
m_endRef: TdtPolyRef;
m_polys: array [0..MAX_POLYS-1] of TdtPolyRef;
m_parent: array [0..MAX_POLYS-1] of TdtPolyRef;
m_npolys: Integer;
m_straightPath: array [0..MAX_POLYS*3-1] of Single;
m_straightPathFlags: array [0..MAX_POLYS-1] of Byte;
m_straightPathPolys: array [0..MAX_POLYS-1] of TdtPolyRef;
m_nstraightPath: Integer;
m_polyPickExt: array [0..2] of Single;
m_smoothPath: array [0..MAX_SMOOTH*3-1] of Single;
m_nsmoothPath: Integer;
m_queryPoly: array [0..4*3-1] of Single;
m_randPoints: array [0..MAX_RAND_POINTS*3-1] of Single;
m_nrandPoints: Integer;
m_randPointsInCircle: Boolean;
m_spos: array [0..2] of Single;
m_epos: array [0..2] of Single;
m_hitPos: array [0..2] of Single;
m_hitNormal: array [0..2] of Single;
m_hitResult: Boolean;
m_distanceToWall: Single;
m_neighbourhoodRadius: Single;
m_randomRadius: Single;
m_sposSet: Boolean;
m_eposSet: Boolean;
m_pathIterNum: Integer;
m_pathIterPolys: array [0..MAX_POLYS-1] of TdtPolyRef;
m_pathIterPolyCount: Integer;
m_prevIterPos, m_iterPos, m_steerPos, m_targetPos: array [0..2] of Single;
m_steerPoints: array [0..MAX_STEER_POINTS*3] of Single;
m_steerPointCount: Integer;
public
constructor Create(aOwner: TWinControl);
destructor Destroy; override;
procedure init(sample: TSample); override;
procedure reset(); override;
procedure handleMenu(Sender: TObject); override;
procedure handleClick(s,p: PSingle; shift: Boolean); override;
procedure handleToggle(); override;
procedure handleStep(); override;
procedure handleUpdate(dt: Single); override;
procedure handleRender(); override;
procedure handleRenderOverlay(proj, model: PDouble; view: PInteger); override;
procedure recalc();
procedure drawAgent(pos: PSingle; r, h, c: Single; col: Cardinal);
end;
implementation
uses Math,
RN_Recast, RN_RecastHelper, RN_DebugDraw, RN_RecastDebugDraw, RN_SampleInterfaces,
RN_DetourNavMeshBuilder, RN_DetourDebugDraw, RN_DetourCommon;
// Uncomment this to dump all the requests in stdout.
//#define DUMP_REQS
// Returns a random number [0..1)
//static float frand()
{
// return ((float)(rand() & 0xffff)/(float)0xffff);
return (float)rand()/(float)RAND_MAX;
}
function inRange(v1,v2: PSingle; r,h: Single): Boolean;
var dx,dy,dz: Single;
begin
dx := v2[0] - v1[0];
dy := v2[1] - v1[1];
dz := v2[2] - v1[2];
Result := ((dx*dx + dz*dz) < r*r) and (abs(dy) < h);
end;
function fixupCorridor(path: PdtPolyRef; npath, maxPath: Integer; visited: PdtPolyRef; nvisited: Integer): Integer;
var furthestPath, furthestVisited, i, j: Integer; found: Boolean; req, orig, size: Integer;
begin
furthestPath := -1;
furthestVisited := -1;
// Find furthest common polygon.
for i := npath-1 downto 0 do
begin
found := false;
for j := nvisited-1 downto 0 do
begin
if (path[i] = visited[j]) then
begin
furthestPath := i;
furthestVisited := j;
found := true;
end;
end;
if (found) then
break;
end;
// If no intersection found just return current path.
if (furthestPath = -1) or (furthestVisited = -1) then
Exit(npath);
// Concatenate paths.
// Adjust beginning of the buffer to include the visited.
req := nvisited - furthestVisited;
orig := rcMin(furthestPath+1, npath);
size := rcMax(0, npath-orig);
if (req+size > maxPath) then
size := maxPath-req;
if (size <> 0) then
Move((path+orig)^, (path+req)^, size*sizeof(TdtPolyRef));
// Store visited
for i := 0 to req - 1 do
path[i] := visited[(nvisited-1)-i];
Result := req+size;
end;
// This function checks if the path has a small U-turn, that is,
// a polygon further in the path is adjacent to the first polygon
// in the path. If that happens, a shortcut is taken.
// This can happen if the target (T) location is at tile boundary,
// and we're (S) approaching it parallel to the tile edge.
// The choice at the vertex can be arbitrary,
// +---+---+
// |:::|:::|
// +-S-+-T-+
// |:::| | <-- the step can end up in here, resulting U-turn path.
// +---+---+
function fixupShortcuts(path: PdtPolyRef; npath: Integer; navQuery: TdtNavMeshQuery): Integer;
const maxNeis = 16; maxLookAhead = 6;
var neis: array [0..maxNeis-1] of TdtPolyRef; nneis: Integer; tile: PdtMeshTile; poly: PdtPoly; k: Cardinal; link: PdtLink;
cut,i,j,offset: Integer;
begin
if (npath < 3) then
Exit(npath);
// Get connected polygons
nneis := 0;
tile := nil;
poly := nil;
if (dtStatusFailed(navQuery.getAttachedNavMesh.getTileAndPolyByRef(path[0], @tile, @poly))) then
Exit(npath);
k := poly.firstLink;
while (k <> DT_NULL_LINK) do
begin
link := @tile.links[k];
if (link.ref <> 0) then
begin
if (nneis < maxNeis) then
begin
neis[nneis] := link.ref;
Inc(nneis);
end;
end;
k := tile.links[k].next;
end;
// If any of the neighbour polygons is within the next few polygons
// in the path, short cut to that polygon directly.
cut := 0;
i := dtMin(maxLookAhead, npath) - 1;
while (i > 1) and (cut = 0) do
begin
for j := 0 to nneis - 1 do
begin
if (path[i] = neis[j]) then
begin
cut := i;
break;
end;
end;
Dec(i);
end;
if (cut > 1) then
begin
offset := cut-1;
Dec(npath, offset);
for i := 1 to npath - 1 do
path[i] := path[i+offset];
end;
Result := npath;
end;
function getSteerTarget(navQuery: TdtNavMeshQuery; startPos, endPos: PSingle;
minTargetDist: Single;
path: PdtPolyRef; pathSize: Integer;
steerPos: PSingle; steerPosFlag: PByte; steerPosRef: PdtPolyRef;
outPoints: PSingle = nil; outPointCount: PInteger = nil): Boolean;
const MAX_STEER_POINTS = 3;
var steerPath: array [0..MAX_STEER_POINTS*3-1] of Single; steerPathFlags: array [0..MAX_STEER_POINTS-1] of Byte;
steerPathPolys: array [0..MAX_STEER_POINTS-1] of TdtPolyRef; nsteerPath,i,ns: Integer;
begin
// Find steer target.
nsteerPath := 0;
navQuery.findStraightPath(startPos, endPos, path, pathSize,
@steerPath[0], @steerPathFlags[0], @steerPathPolys[0], @nsteerPath, MAX_STEER_POINTS);
if (nsteerPath = 0) then
Exit(false);
if (outPoints <> nil) and (outPointCount <> nil) then
begin
outPointCount^ := nsteerPath;
for i := 0 to nsteerPath - 1 do
dtVcopy(@outPoints[i*3], @steerPath[i*3]);
end;
// Find vertex far enough to steer to.
ns := 0;
while (ns < nsteerPath) do
begin
// Stop at Off-Mesh link or when point is further than slop away.
if ((steerPathFlags[ns] and Byte(DT_STRAIGHTPATH_OFFMESH_CONNECTION) <> 0) or
not inRange(@steerPath[ns*3], startPos, minTargetDist, 1000.0)) then
break;
Inc(ns);
end;
// Failed to find good point to steer to.
if (ns >= nsteerPath) then
Exit(false);
dtVcopy(steerPos, @steerPath[ns*3]);
steerPos[1] := startPos[1];
steerPosFlag^ := steerPathFlags[ns];
steerPosRef^ := steerPathPolys[ns];
Result := true;
end;
constructor TNavMeshTesterTool.Create(aOwner: TWinControl);
begin
inherited Create;
&type := TOOL_NAVMESH_TESTER;
fFrame := TFrameTesterTool.Create(aOwner);
fFrame.Align := alClient;
fFrame.Parent := aOwner;
fFrame.Visible := True;
fFrame.rgToolMode.OnClick := handleMenu;
fFrame.rgVerticesAtCrossings.OnClick := handleMenu;
fFrame.btnSetRandomStart.OnClick := handleMenu;
fFrame.btnSetRandomEnd.OnClick := handleMenu;
fFrame.btnMakeRandomPoints.OnClick := handleMenu;
fFrame.btnMakeRandomPointsAround.OnClick := handleMenu;
fFrame.cbInclWalk.OnClick := handleMenu;
fFrame.cbInclSwim.OnClick := handleMenu;
fFrame.cbInclDoor.OnClick := handleMenu;
fFrame.cbInclJump.OnClick := handleMenu;
fFrame.cbExclWalk.OnClick := handleMenu;
fFrame.cbExclSwim.OnClick := handleMenu;
fFrame.cbExclDoor.OnClick := handleMenu;
fFrame.cbExclJump.OnClick := handleMenu;
// Delphi: Assume C++ invokes constructor
m_filter := TdtQueryFilter.Create;
m_pathFindStatus := DT_FAILURE;
m_toolMode := TOOLMODE_PATHFIND_FOLLOW;
m_filter.setIncludeFlags(SAMPLE_POLYFLAGS_ALL xor SAMPLE_POLYFLAGS_DISABLED);
m_filter.setExcludeFlags(0);
m_polyPickExt[0] := 2;
m_polyPickExt[1] := 4;
m_polyPickExt[2] := 2;
m_neighbourhoodRadius := 2.5;
m_randomRadius := 5.0;
end;
destructor TNavMeshTesterTool.Destroy;
begin
// Delphi: Assume C++ does the same when var goes out of scope
m_filter.Free;
fFrame.Free;
inherited;
end;
procedure TNavMeshTesterTool.init(sample: TSample);
begin
m_sample := sample;
m_navMesh := sample.getNavMesh;
m_navQuery := sample.getNavMeshQuery;
recalc();
if (m_navQuery <> nil) then
begin
// Change costs.
m_filter.setAreaCost(Byte(SAMPLE_POLYAREA_GROUND), 1.0);
m_filter.setAreaCost(Byte(SAMPLE_POLYAREA_WATER), 10.0);
m_filter.setAreaCost(Byte(SAMPLE_POLYAREA_ROAD), 1.0);
m_filter.setAreaCost(Byte(SAMPLE_POLYAREA_DOOR), 1.0);
m_filter.setAreaCost(Byte(SAMPLE_POLYAREA_GRASS), 2.0);
m_filter.setAreaCost(Byte(SAMPLE_POLYAREA_JUMP), 1.5);
end;
m_neighbourhoodRadius := sample.getAgentRadius * 20.0;
m_randomRadius := sample.getAgentRadius * 30.0;
end;
procedure TNavMeshTesterTool.handleMenu(Sender: TObject);
var status: TdtStatus; i: Integer; pt: array[0..2] of Single; ref: TdtPolyRef;
begin
if fUpdateUI then Exit;
// Delphi: When the Sender is NIL, we fill the controls with current state values
if Sender = nil then
begin
fUpdateUI := True;
fFrame.rgToolMode.ItemIndex := Byte(m_toolMode);
fFrame.rgVerticesAtCrossings.Enabled := m_toolMode = TOOLMODE_PATHFIND_STRAIGHT;
fFrame.rgVerticesAtCrossings.ItemIndex := Byte(m_straightPathOptions);
fFrame.btnSetRandomEnd.Enabled := m_sposSet;
fFrame.btnMakeRandomPointsAround.Enabled := m_sposSet;
fFrame.cbInclWalk.Checked := (m_filter.getIncludeFlags() and SAMPLE_POLYFLAGS_WALK) <> 0;
fFrame.cbInclSwim.Checked := (m_filter.getIncludeFlags() and SAMPLE_POLYFLAGS_SWIM) <> 0;
fFrame.cbInclDoor.Checked := (m_filter.getIncludeFlags() and SAMPLE_POLYFLAGS_DOOR) <> 0;
fFrame.cbInclJump.Checked := (m_filter.getIncludeFlags() and SAMPLE_POLYFLAGS_JUMP) <> 0;
fFrame.cbExclWalk.Checked := (m_filter.getExcludeFlags() and SAMPLE_POLYFLAGS_WALK) <> 0;
fFrame.cbExclSwim.Checked := (m_filter.getExcludeFlags() and SAMPLE_POLYFLAGS_SWIM) <> 0;
fFrame.cbExclDoor.Checked := (m_filter.getExcludeFlags() and SAMPLE_POLYFLAGS_DOOR) <> 0;
fFrame.cbExclJump.Checked := (m_filter.getExcludeFlags() and SAMPLE_POLYFLAGS_JUMP) <> 0;
fUpdateUI := False;
end;
if Sender = fFrame.rgToolMode then
begin
m_toolMode := TToolMode(fFrame.rgToolMode.ItemIndex);
recalc();
end;
fFrame.rgVerticesAtCrossings.Visible := (m_toolMode = TOOLMODE_PATHFIND_STRAIGHT);
if (m_toolMode = TOOLMODE_PATHFIND_STRAIGHT) then
begin
m_straightPathOptions := TdtStraightPathOptions(fFrame.rgVerticesAtCrossings.ItemIndex);
recalc();
end;
if Sender = fFrame.btnSetRandomStart then
begin
status := m_navQuery.findRandomPoint(m_filter, {frand,} @m_startRef, @m_spos[0]);
if (dtStatusSucceed(status)) then
begin
m_sposSet := true;
recalc();
end;
end;
if Sender = fFrame.btnSetRandomEnd then
begin
if (m_sposSet) then
begin
status := m_navQuery.findRandomPointAroundCircle(m_startRef, @m_spos[0], m_randomRadius, m_filter, {frand,} @m_endRef, @m_epos[0]);
if (dtStatusSucceed(status)) then
begin
m_eposSet := true;
recalc();
end;
end;
end;
if Sender = fFrame.btnMakeRandomPoints then
begin
m_randPointsInCircle := false;
m_nrandPoints := 0;
for i := 0 to MAX_RAND_POINTS - 1 do
begin
status := m_navQuery.findRandomPoint(m_filter, {frand,} @ref, @pt[0]);
if (dtStatusSucceed(status)) then
begin
dtVcopy(@m_randPoints[m_nrandPoints*3], @pt[0]);
Inc(m_nrandPoints);
end;
end;
end;
if Sender = fFrame.btnMakeRandomPointsAround then
begin
if (m_sposSet) then
begin
m_nrandPoints := 0;
m_randPointsInCircle := true;
for i := 0 to MAX_RAND_POINTS - 1 do
begin
status := m_navQuery.findRandomPointAroundCircle(m_startRef, @m_spos[0], m_randomRadius, m_filter, {frand,} @ref, @pt[0]);
if (dtStatusSucceed(status)) then
begin
dtVcopy(@m_randPoints[m_nrandPoints*3], @pt[0]);
Inc(m_nrandPoints);
end;
end;
end;
end;
m_filter.setIncludeFlags(Byte(fFrame.cbInclWalk.Checked) * SAMPLE_POLYFLAGS_WALK +
Byte(fFrame.cbInclSwim.Checked) * SAMPLE_POLYFLAGS_SWIM +
Byte(fFrame.cbInclDoor.Checked) * SAMPLE_POLYFLAGS_DOOR +
Byte(fFrame.cbInclJump.Checked) * SAMPLE_POLYFLAGS_JUMP);
recalc();
m_filter.setExcludeFlags(Byte(fFrame.cbExclWalk.Checked) * SAMPLE_POLYFLAGS_WALK +
Byte(fFrame.cbExclSwim.Checked) * SAMPLE_POLYFLAGS_SWIM +
Byte(fFrame.cbExclDoor.Checked) * SAMPLE_POLYFLAGS_DOOR +
Byte(fFrame.cbExclJump.Checked) * SAMPLE_POLYFLAGS_JUMP);
recalc();
end;
procedure TNavMeshTesterTool.handleClick(s,p: PSingle; shift: Boolean);
begin
if (shift) then
begin
m_sposSet := true;
dtVcopy(@m_spos[0], p);
end
else
begin
m_eposSet := true;
dtVcopy(@m_epos[0], p);
end;
recalc();
end;
procedure TNavMeshTesterTool.handleStep();
begin
end;
procedure TNavMeshTesterTool.handleToggle();
const STEP_SIZE = 0.5;
const SLOP = 0.01;
var steerPos,delta,moveTgt,reslt,startPos,endPos: array [0..2] of Single; steerPosFlag: Byte; steerPosRef,prevRef,polyRef: TdtPolyRef;
endOfPath, offMeshConnection: Boolean; len: Single; visited: array [0..15] of TdtPolyRef; nvisited,npos,i: Integer; h,eh: Single;
status: TdtStatus;
begin
// TODO: merge separate to a path iterator. Use same code in recalc() too.
if (m_toolMode <> TOOLMODE_PATHFIND_FOLLOW) then
Exit;
if (not m_sposSet or not m_eposSet or (m_startRef = 0) or (m_endRef = 0)) then
Exit;
if (m_pathIterNum = 0) then
begin
m_navQuery.findPath(m_startRef, m_endRef, @m_spos[0], @m_epos[0], m_filter, @m_polys[0], @m_npolys, MAX_POLYS);
m_nsmoothPath := 0;
m_pathIterPolyCount := m_npolys;
if (m_pathIterPolyCount <> 0) then
Move(m_polys[0], m_pathIterPolys[0], sizeof(TdtPolyRef)*m_pathIterPolyCount);
if (m_pathIterPolyCount <> 0) then
begin
// Iterate over the path to find smooth path on the detail mesh surface.
m_navQuery.closestPointOnPoly(m_startRef, @m_spos[0], @m_iterPos[0], nil);
m_navQuery.closestPointOnPoly(m_pathIterPolys[m_pathIterPolyCount-1], @m_epos[0], @m_targetPos[0], nil);
m_nsmoothPath := 0;
dtVcopy(@m_smoothPath[m_nsmoothPath*3], @m_iterPos[0]);
Inc(m_nsmoothPath);
end;
end;
dtVcopy(@m_prevIterPos[0], @m_iterPos[0]);
Inc(m_pathIterNum);
if (m_pathIterPolyCount = 0) then
Exit;
if (m_nsmoothPath >= MAX_SMOOTH) then
Exit;
// Move towards target a small advancement at a time until target reached or
// when ran out of memory to store the path.
// Find location to steer towards.
if (not getSteerTarget(m_navQuery, @m_iterPos[0], @m_targetPos[0], SLOP,
@m_pathIterPolys[0], m_pathIterPolyCount, @steerPos[0], @steerPosFlag, @steerPosRef,
@m_steerPoints[0], @m_steerPointCount)) then
Exit;
dtVcopy(@m_steerPos[0], @steerPos[0]);
endOfPath := (steerPosFlag and Byte(DT_STRAIGHTPATH_END)) <> 0;
offMeshConnection := (steerPosFlag and Byte(DT_STRAIGHTPATH_OFFMESH_CONNECTION)) <> 0;
// Find movement delta.
dtVsub(@delta[0], @steerPos[0], @m_iterPos[0]);
len := sqrt(dtVdot(@delta[0],@delta[0]));
// If the steer target is end of path or off-mesh link, do not move past the location.
if (endOfPath or offMeshConnection) and (len < STEP_SIZE) then
len := 1
else
len := STEP_SIZE / len;
dtVmad(@moveTgt[0], @m_iterPos[0], @delta[0], len);
// Move
nvisited := 0;
m_navQuery.moveAlongSurface(m_pathIterPolys[0], @m_iterPos[0], @moveTgt[0], m_filter,
@reslt[0], @visited[0], @nvisited, 16);
m_pathIterPolyCount := fixupCorridor(@m_pathIterPolys[0], m_pathIterPolyCount, MAX_POLYS, @visited[0], nvisited);
m_pathIterPolyCount := fixupShortcuts(@m_pathIterPolys[0], m_pathIterPolyCount, m_navQuery);
h := 0;
m_navQuery.getPolyHeight(m_pathIterPolys[0], @reslt[0], @h);
reslt[1] := h;
dtVcopy(@m_iterPos[0], @reslt[0]);
// Handle end of path and off-mesh links when close enough.
if (endOfPath and inRange(@m_iterPos[0], @steerPos[0], SLOP, 1.0)) then
begin
// Reached end of path.
dtVcopy(@m_iterPos[0], @m_targetPos[0]);
if (m_nsmoothPath < MAX_SMOOTH) then
begin
dtVcopy(@m_smoothPath[m_nsmoothPath*3], @m_iterPos[0]);
Inc(m_nsmoothPath);
end;
Exit;
end
else if (offMeshConnection and inRange(@m_iterPos[0], @steerPos[0], SLOP, 1.0)) then
begin
// Reached off-mesh connection.
// Advance the path up to and over the off-mesh connection.
prevRef := 0; polyRef := m_pathIterPolys[0];
npos := 0;
while (npos < m_pathIterPolyCount) and (polyRef <> steerPosRef) do
begin
prevRef := polyRef;
polyRef := m_pathIterPolys[npos];
Inc(npos);
end;
for i := npos to m_pathIterPolyCount - 1 do
m_pathIterPolys[i-npos] := m_pathIterPolys[i];
Dec(m_pathIterPolyCount, npos);
// Handle the connection.
status := m_navMesh.getOffMeshConnectionPolyEndPoints(prevRef, polyRef, @startPos[0], @endPos[0]);
if (dtStatusSucceed(status)) then
begin
if (m_nsmoothPath < MAX_SMOOTH) then
begin
dtVcopy(@m_smoothPath[m_nsmoothPath*3], @startPos[0]);
Inc(m_nsmoothPath);
// Hack to make the dotted path not visible during off-mesh connection.
if (m_nsmoothPath and 1) <> 0 then
begin
dtVcopy(@m_smoothPath[m_nsmoothPath*3], @startPos[0]);
Inc(m_nsmoothPath);
end;
end;
// Move position at the other side of the off-mesh link.
dtVcopy(@m_iterPos[0], @endPos[0]);
eh := 0.0;
m_navQuery.getPolyHeight(m_pathIterPolys[0], @m_iterPos[0], @eh);
m_iterPos[1] := eh;
end;
end;
// Store results.
if (m_nsmoothPath < MAX_SMOOTH) then
begin
dtVcopy(@m_smoothPath[m_nsmoothPath*3], @m_iterPos[0]);
Inc(m_nsmoothPath);
end;
end;
procedure TNavMeshTesterTool.handleUpdate(dt: Single);
var epos: array [0..2] of Single;
begin
if (m_toolMode = TOOLMODE_PATHFIND_SLICED) then
begin
if (dtStatusInProgress(m_pathFindStatus)) then
begin
m_pathFindStatus := m_navQuery.updateSlicedFindPath(1, nil);
end;
if (dtStatusSucceed(m_pathFindStatus)) then
begin
m_navQuery.finalizeSlicedFindPath(@m_polys[0], @m_npolys, MAX_POLYS);
m_nstraightPath := 0;
if (m_npolys <> 0) then
begin
// In case of partial path, make sure the end point is clamped to the last polygon.
dtVcopy(@epos[0], @m_epos[0]);
if (m_polys[m_npolys-1] <> m_endRef) then
m_navQuery.closestPointOnPoly(m_polys[m_npolys-1], @m_epos[0], @epos[0], nil);
m_navQuery.findStraightPath(@m_spos[0], @epos[0], @m_polys[0], m_npolys,
@m_straightPath[0], @m_straightPathFlags[0],
@m_straightPathPolys[0], @m_nstraightPath, MAX_POLYS, Byte(DT_STRAIGHTPATH_ALL_CROSSINGS));
end;
m_pathFindStatus := DT_FAILURE;
end;
end;
end;
procedure TNavMeshTesterTool.reset();
begin
m_startRef := 0;
m_endRef := 0;
m_npolys := 0;
m_nstraightPath := 0;
m_nsmoothPath := 0;
FillChar(m_hitPos[0], sizeof(m_hitPos), 0);
FillChar(m_hitNormal[0], sizeof(m_hitNormal), 0);
m_distanceToWall := 0;
end;
procedure TNavMeshTesterTool.recalc();
const STEP_SIZE = 0.5;
const SLOP = 0.01;
var polys: array [0..MAX_POLYS-1] of TdtPolyRef; npolys: Integer; iterPos,targetPos,steerPos,delta,moveTgt,reslt,startPos,endPos,epos: array [0..2] of Single;
steerPosFlag: Byte; steerPosRef,prevRef,polyRef: TdtPolyRef; endOfPath,offMeshConnection: Boolean; len: Single;
visited: array [0..15] of TdtPolyRef; nvisited,npos,i: Integer; h,eh,t: Single; status: TdtStatus; dx,dz,dist: Single;
nx,nz,agentHeight: Single;
begin
if (m_navMesh = nil) then
Exit;
if (m_sposSet) then
m_navQuery.findNearestPoly(@m_spos[0], @m_polyPickExt[0], m_filter, @m_startRef, nil)
else
m_startRef := 0;
if (m_eposSet) then
m_navQuery.findNearestPoly(@m_epos[0], @m_polyPickExt[0], m_filter, @m_endRef, nil)
else
m_endRef := 0;
m_pathFindStatus := DT_FAILURE;
if (m_toolMode = TOOLMODE_PATHFIND_FOLLOW) then
begin
m_pathIterNum := 0;
if (m_sposSet and m_eposSet and (m_startRef <> 0) and (m_endRef <> 0)) then
begin
{$ifdef DUMP_REQS}
printf("pi %f %f %f %f %f %f 0x%x 0x%x\n",
m_spos[0],m_spos[1],m_spos[2], m_epos[0],m_epos[1],m_epos[2],
m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
{$endif}
m_navQuery.findPath(m_startRef, m_endRef, @m_spos[0], @m_epos[0], m_filter, @m_polys[0], @m_npolys, MAX_POLYS);
m_nsmoothPath := 0;
if (m_npolys <> 0) then
begin
// Iterate over the path to find smooth path on the detail mesh surface.
Move(m_polys[0], polys[0], sizeof(TdtPolyRef)*m_npolys);
npolys := m_npolys;
m_navQuery.closestPointOnPoly(m_startRef, @m_spos[0], @iterPos[0], nil);
m_navQuery.closestPointOnPoly(polys[npolys-1], @m_epos[0], @targetPos[0], nil);
m_nsmoothPath := 0;
dtVcopy(@m_smoothPath[m_nsmoothPath*3], @iterPos[0]);
Inc(m_nsmoothPath);
// Move towards target a small advancement at a time until target reached or
// when ran out of memory to store the path.
while (npolys <> 0) and (m_nsmoothPath < MAX_SMOOTH) do
begin
// Find location to steer towards.
if (not getSteerTarget(m_navQuery, @iterPos[0], @targetPos[0], SLOP,
@polys[0], npolys, @steerPos[0], @steerPosFlag, @steerPosRef)) then
break;
endOfPath := (steerPosFlag and Byte(DT_STRAIGHTPATH_END)) <> 0;
offMeshConnection := (steerPosFlag and Byte(DT_STRAIGHTPATH_OFFMESH_CONNECTION)) <> 0;
// Find movement delta.
dtVsub(@delta[0], @steerPos[0], @iterPos[0]);
len := Sqrt(dtVdot(@delta[0],@delta[0]));
// If the steer target is end of path or off-mesh link, do not move past the location.
if (endOfPath or offMeshConnection) and (len < STEP_SIZE) then
len := 1
else
len := STEP_SIZE / len;
dtVmad(@moveTgt[0], @iterPos[0], @delta[0], len);
// Move
nvisited := 0;
m_navQuery.moveAlongSurface(polys[0], @iterPos[0], @moveTgt[0], m_filter,
@reslt[0], @visited[0], @nvisited, 16);
npolys := fixupCorridor(@polys[0], npolys, MAX_POLYS, @visited[0], nvisited);
npolys := fixupShortcuts(@polys[0], npolys, m_navQuery);
h := 0;
m_navQuery.getPolyHeight(polys[0], @reslt[0], @h);
reslt[1] := h;
dtVcopy(@iterPos[0], @reslt[0]);
// Handle end of path and off-mesh links when close enough.
if (endOfPath and inRange(@iterPos[0], @steerPos[0], SLOP, 1.0)) then
begin
// Reached end of path.
dtVcopy(@iterPos[0], @targetPos[0]);
if (m_nsmoothPath < MAX_SMOOTH) then
begin
dtVcopy(@m_smoothPath[m_nsmoothPath*3], @iterPos[0]);
Inc(m_nsmoothPath);
end;
break;
end
else if (offMeshConnection and inRange(@iterPos[0], @steerPos[0], SLOP, 1.0)) then
begin
// Reached off-mesh connection.
// Advance the path up to and over the off-mesh connection.
prevRef := 0; polyRef := polys[0];
npos := 0;
while (npos < npolys) and (polyRef <> steerPosRef) do
begin
prevRef := polyRef;
polyRef := polys[npos];
Inc(npos);
end;
for i := npos to npolys - 1 do
polys[i-npos] := polys[i];
Dec(npolys, npos);
// Handle the connection.
status := m_navMesh.getOffMeshConnectionPolyEndPoints(prevRef, polyRef, @startPos[0], @endPos[0]);
if (dtStatusSucceed(status)) then
begin
if (m_nsmoothPath < MAX_SMOOTH) then
begin
dtVcopy(@m_smoothPath[m_nsmoothPath*3], @startPos[0]);
Inc(m_nsmoothPath);
// Hack to make the dotted path not visible during off-mesh connection.
if (m_nsmoothPath and 1) <> 0 then
begin
dtVcopy(@m_smoothPath[m_nsmoothPath*3], @startPos[0]);
Inc(m_nsmoothPath);
end;
end;
// Move position at the other side of the off-mesh link.
dtVcopy(@iterPos[0], @endPos[0]);
eh := 0.0;
m_navQuery.getPolyHeight(polys[0], @iterPos[0], @eh);
iterPos[1] := eh;
end;
end;
// Store results.
if (m_nsmoothPath < MAX_SMOOTH) then
begin
dtVcopy(@m_smoothPath[m_nsmoothPath*3], @iterPos[0]);
Inc(m_nsmoothPath);
end;
end;
end;
end
else
begin
m_npolys := 0;
m_nsmoothPath := 0;
end;
end
else if (m_toolMode = TOOLMODE_PATHFIND_STRAIGHT) then
begin
if (m_sposSet and m_eposSet and (m_startRef <> 0) and (m_endRef <> 0)) then
begin
{$ifdef DUMP_REQS}
printf("ps %f %f %f %f %f %f 0x%x 0x%x\n",
m_spos[0],m_spos[1],m_spos[2], m_epos[0],m_epos[1],m_epos[2],
m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
{$endif}
m_navQuery.findPath(m_startRef, m_endRef, @m_spos[0], @m_epos[0], m_filter, @m_polys[0], @m_npolys, MAX_POLYS);
m_nstraightPath := 0;
if (m_npolys <> 0) then
begin
// In case of partial path, make sure the end point is clamped to the last polygon.
dtVcopy(@epos[0], @m_epos[0]);
if (m_polys[m_npolys-1] <> m_endRef) then
m_navQuery.closestPointOnPoly(m_polys[m_npolys-1], @m_epos[0], @epos[0], nil);
m_navQuery.findStraightPath(@m_spos[0], @epos[0], @m_polys[0], m_npolys,
@m_straightPath[0], @m_straightPathFlags[0],
@m_straightPathPolys[0], @m_nstraightPath, MAX_POLYS, Byte(m_straightPathOptions));
end;
end
else
begin
m_npolys := 0;
m_nstraightPath := 0;
end;
end
else if (m_toolMode = TOOLMODE_PATHFIND_SLICED) then
begin
if (m_sposSet and m_eposSet and (m_startRef <> 0) and (m_endRef <> 0)) then
begin
{$ifdef DUMP_REQS}
printf("ps %f %f %f %f %f %f 0x%x 0x%x\n",
m_spos[0],m_spos[1],m_spos[2], m_epos[0],m_epos[1],m_epos[2],
m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
{$endif}
m_npolys := 0;
m_nstraightPath := 0;
m_pathFindStatus := m_navQuery.initSlicedFindPath(m_startRef, m_endRef, @m_spos[0], @m_epos[0], m_filter, Byte(DT_FINDPATH_ANY_ANGLE));
end
else
begin
m_npolys := 0;
m_nstraightPath := 0;
end;
end
else if (m_toolMode = TOOLMODE_RAYCAST) then
begin
m_nstraightPath := 0;
if (m_sposSet and m_eposSet and (m_startRef <> 0)) then
begin
{$ifdef DUMP_REQS}
printf("rc %f %f %f %f %f %f 0x%x 0x%x\n",
m_spos[0],m_spos[1],m_spos[2], m_epos[0],m_epos[1],m_epos[2],
m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
{$endif}
t := 0;
m_npolys := 0;
m_nstraightPath := 2;
m_straightPath[0] := m_spos[0];
m_straightPath[1] := m_spos[1];
m_straightPath[2] := m_spos[2];
m_navQuery.raycast(m_startRef, @m_spos[0], @m_epos[0], m_filter, @t, @m_hitNormal[0], @m_polys[0], @m_npolys, MAX_POLYS);
if (t > 1) then
begin
// No hit
dtVcopy(@m_hitPos[0], @m_epos[0]);
m_hitResult := false;
end
else
begin
// Hit
dtVlerp(@m_hitPos[0], @m_spos[0], @m_epos[0], t);
m_hitResult := true;
end;
// Adjust height.
if (m_npolys > 0) then
begin
h := 0;
m_navQuery.getPolyHeight(m_polys[m_npolys-1], @m_hitPos[0], @h);
m_hitPos[1] := h;
end;
dtVcopy(@m_straightPath[3], @m_hitPos[0]);
end;
end
else if (m_toolMode = TOOLMODE_DISTANCE_TO_WALL) then
begin
m_distanceToWall := 0;
if (m_sposSet and (m_startRef <> 0)) then
begin
{$ifdef DUMP_REQS}
printf("dw %f %f %f %f 0x%x 0x%x\n",
m_spos[0],m_spos[1],m_spos[2], 100.0f,
m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
{$endif}
m_distanceToWall := 0.0;
m_navQuery.findDistanceToWall(m_startRef, @m_spos[0], 100.0, m_filter, @m_distanceToWall, @m_hitPos[0], @m_hitNormal[0]);
end;
end
else if (m_toolMode = TOOLMODE_FIND_POLYS_IN_CIRCLE) then
begin
if (m_sposSet and m_eposSet and (m_startRef <> 0)) then
begin
dx := m_epos[0] - m_spos[0];
dz := m_epos[2] - m_spos[2];
dist := sqrt(dx*dx + dz*dz);
{$ifdef DUMP_REQS}
printf("fpc %f %f %f %f 0x%x 0x%x\n",
m_spos[0],m_spos[1],m_spos[2], dist,
m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
{$endif}
m_navQuery.findPolysAroundCircle(m_startRef, @m_spos[0], dist, m_filter,
@m_polys[0], @m_parent[0], nil, @m_npolys, MAX_POLYS);
end;
end
else if (m_toolMode = TOOLMODE_FIND_POLYS_IN_SHAPE) then
begin
if (m_sposSet and m_eposSet and (m_startRef <> 0)) then
begin
nx := (m_epos[2] - m_spos[2])*0.25;
nz := -(m_epos[0] - m_spos[0])*0.25;
if m_sample <> nil then agentHeight := m_sample.getAgentHeight else agentHeight := 0;
m_queryPoly[0] := m_spos[0] + nx*1.2;
m_queryPoly[1] := m_spos[1] + agentHeight/2;
m_queryPoly[2] := m_spos[2] + nz*1.2;
m_queryPoly[3] := m_spos[0] - nx*1.3;
m_queryPoly[4] := m_spos[1] + agentHeight/2;
m_queryPoly[5] := m_spos[2] - nz*1.3;
m_queryPoly[6] := m_epos[0] - nx*0.8;
m_queryPoly[7] := m_epos[1] + agentHeight/2;
m_queryPoly[8] := m_epos[2] - nz*0.8;
m_queryPoly[9] := m_epos[0] + nx;
m_queryPoly[10] := m_epos[1] + agentHeight/2;
m_queryPoly[11] := m_epos[2] + nz;
{$ifdef DUMP_REQS}
printf("fpp %f %f %f %f %f %f %f %f %f %f %f %f 0x%x 0x%x\n",
m_queryPoly[0],m_queryPoly[1],m_queryPoly[2],
m_queryPoly[3],m_queryPoly[4],m_queryPoly[5],
m_queryPoly[6],m_queryPoly[7],m_queryPoly[8],
m_queryPoly[9],m_queryPoly[10],m_queryPoly[11],
m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
{$endif}
m_navQuery.findPolysAroundShape(m_startRef, @m_queryPoly[0], 4, m_filter,
@m_polys[0], @m_parent[0], nil, @m_npolys, MAX_POLYS);
end;