Repository navigation
Expand file tree
/
Copy pathPathing.cs
More file actions
5360 lines (4570 loc) · 216 KB
/
Copy pathPathing.cs
File metadata and controls
5360 lines (4570 loc) · 216 KB
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
using Blocks;
using Blocks.ExtensionMethods;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Blocks
{
public enum PathingFlags
{
Visited = (1 << 30),
CanWeFitAbove = (1 << 29),
CachedIfWeCanFitAbove = (1 << 28),
CachedCanWeStandOn = (1 << 27),
CanWeStandOn = (1 << 26)
}
public class MobilityCriteria
{
public int jumpHeight;
public int neededSizeForward, neededSizeSide, neededSizeUp;
public MobilityCriteria(int neededSizeForward, int neededSizeSide, int neededSizeUp, int jumpHeight)
{
this.neededSizeForward = neededSizeForward;
this.neededSizeSide = neededSizeSide;
this.neededSizeUp = neededSizeUp;
this.jumpHeight = jumpHeight;
}
// from https://stackoverflow.com/questions/2363143/whats-the-best-strategy-for-equals-and-gethashcode
public override bool Equals(object obj)
{
var myClass = obj as MobilityCriteria;
if (myClass != null)
{
// Order these by the most different first.
// That is, whatever value is most selective, and the fewest
// instances have the same value, put that first.
return this.neededSizeUp == myClass.neededSizeUp
&& this.jumpHeight == myClass.jumpHeight
&& this.neededSizeForward == myClass.neededSizeForward
&& this.neededSizeSide == myClass.neededSizeSide;
}
else
{
// This may not make sense unless GetHashCode refers to `base` as well!
return base.Equals(obj);
}
}
// from https://stackoverflow.com/questions/2363143/whats-the-best-strategy-for-equals-and-gethashcode
public override int GetHashCode()
{
int hash = 19;
unchecked
{ // allow "wrap around" in the int
hash = hash * 31 + this.jumpHeight; // assuming integer
hash = hash * 31 + this.neededSizeForward; // assuming integer
hash = hash * 31 + this.neededSizeSide; // assuming integer
hash = hash * 31 + this.neededSizeUp; // assuming integer
}
return hash;
}
public bool HasEnoughAirToFitHere(GetRelativeBlock blockGetter)
{
return HasEnoughAirAbove((rx, ry, rz) =>
{
return blockGetter(rx, ry - 1, rz);
});
}
public bool HasEnoughAirAbove(GetRelativeBlock blockGetter)
{
if (neededSizeSide == 1 && neededSizeForward == 1)
{
for (int i = 0; i < neededSizeUp; i++)
{
if(blockGetter(0,i+1, 0) != BlockValue.Air)
{
return false;
}
}
}
else
{
for (int x = 0; x < neededSizeSide; x++)
{
for (int z = 0; z < neededSizeForward; z++)
{
for (int i = 0; i < neededSizeUp; i++)
{
if (blockGetter(x, i + 1, z) != BlockValue.Air)
{
return false;
}
}
}
}
}
return true;
}
public bool CanJumpBetween(PathNode blockFrom, PathNode blockTo, GetRelativeBlock blockGetterFrom, GetRelativeBlock blockGetterTo)
{
if (CanStandOn(blockGetterFrom) && CanStandOn(blockGetterTo))
{
long xDist = Math.Abs(blockFrom.pos.x - blockTo.pos.x);
long yDist = Math.Abs(blockFrom.pos.y - blockTo.pos.y);
long zDist = Math.Abs(blockFrom.pos.z - blockTo.pos.z);
long xzDist = xDist + zDist;
if (blockFrom.pos.y == blockTo.pos.y)
{
// easy, walk
if (xzDist == 1)
{
return true;
}
// check for things in the way: todo
else
{
Debug.LogWarning("Warning: unimplemented walking diagonally in CanJumpBetween, for now this is just returning false");
return false;
}
}
// falling, doable, make sure we can actually do that fall
else if(blockFrom.pos.y > blockTo.pos.y)
{
for (int y = 1; y <= yDist; y++)
{
if (!HasEnoughAirAbove((rx, ry, rz) => { return blockGetterTo(rx, ry + y, rz);}))
{
return false;
}
}
return true;
}
// jumping, see if we can make the jump
else // if(blockFrom.pos.y < blockTo.pos.y) // this condition is implied and c# thinks we haven't covered all the cases if we write it
{
// we can't jump that high
if (yDist > jumpHeight)
{
return false;
}
// we can jump that high, make sure there isn't stuff in the way
for (int y = 1; y <= yDist; y++)
{
if (!HasEnoughAirAbove((rx, ry, rz) => { return blockGetterFrom(rx, ry + y, rz); }))
{
return false;
}
}
return true;
}
}
else
{
return false;
}
}
public bool TryGetRelativeBlockYAboveThatWeCanStandOn(GetRelativeBlock blockGetter, int heightToLookUp, out int yBlockAbove)
{
yBlockAbove = 0;
for (int y = 0; y <= heightToLookUp; y++)
{
if (CanStandOn((rx, ry, rz) =>
{
return blockGetter(rx, ry + y, rz);
}))
{
yBlockAbove = y;
return true;
}
}
return false;
}
public bool TryGetRelativeBlockYBelowThatWeCanStandOn(GetRelativeBlock blockGetter, int heightToLookDown, out int yBlockBelow)
{
yBlockBelow = 0;
for (int y = 0; y >= -heightToLookDown; y--)
{
if (CanStandOn((rx, ry, rz) =>
{
return blockGetter(rx, ry + y, rz);
}))
{
yBlockBelow = y;
return true;
}
}
return false;
}
public bool CanStandOn(GetRelativeBlock blockGetter)
{
if (neededSizeSide == 1 && neededSizeForward == 1)
{
if (blockGetter(0,0,0) != BlockValue.Air)
{
return HasEnoughAirAbove(blockGetter);
}
else
{
return false;
}
}
else
{
// just needs at least one
for (int x = 0; x < neededSizeSide; x++)
{
for (int z = 0; z < neededSizeForward; z++)
{
if (blockGetter(x, 0, z) != BlockValue.Air)
{
// check if has air in the whole area we need to breathe in
return HasEnoughAirAbove(blockGetter);
}
}
}
return false;
}
}
}
public class PathingResult
{
public PathingRegionPos result;
public List<LVector3> positions = new List<LVector3>();
public PathNode pathNode;
public PathNode GetPathNode()
{
return pathNode;
}
public PathingResult(PathingRegionPos result)
{
this.result = result;
int length = 0;
PathingRegionPos tmp = result;
while (tmp != null)
{
LVector3 curPos = new LVector3(tmp.wx, tmp.emptyBlockAboveY, tmp.wz);
// don't add duplicate positions (this sometimes happens due to pathing stuffs)
if (positions.Count > 0 && positions[positions.Count - 1] == curPos)
{
//Debug.Log(curPos + " duplicate with " + positions[positions.Count - 1]);
}
else
{
positions.Add(curPos);
}
tmp = tmp.prev;
}
// path normally goes from finish to start
positions.Reverse();
PathNode prev = null;
for (int i = 0; i < positions.Count; i++)
{
PathNode cur = new PathNode(positions[i], prev);
if (prev != null)
{
prev.nextNode = cur;
}
prev = cur;
}
pathNode = prev;
}
}
public enum PathingRegionType2
{
Falling,
Walking
}
public delegate BlockValue GetRelativeBlock(int relativeX, int relativeY, int relativeZ);
public enum ExpandFlags
{
PosY = (1 << 0),
NegY = (1 << 1),
PosX = (1 << 2),
NegX = (1 << 3),
PosZ = (1 << 4),
NegZ = (1 << 5),
Horizontal = XAxis | ZAxis,
Vertical = YAxis,
All6Neighbors = Horizontal | Vertical,
XAxis = PosX | NegX,
YAxis = PosY | NegY,
ZAxis = PosZ | NegZ,
Up = PosY,
Down = NegY
}
public enum RegionSettingsFlags
{
/// <summary>
/// Pathing will try to see if we can start a chunk from any air blocks via calling IsValidToStartPathingRegion on them, no spawning from other pathing regions is required
/// </summary>
DefaultCheckAirBlocks = (1 << 0),
/// <summary>
/// Pathing will try to see if we can start a chunk from any solid blocks via calling IsValidToStartPathingRegion on them, no spawning from other pathing regions is required
/// </summary>
DefaultCheckSolidBlocks = (1 << 1),
/// <summary>
/// Pathing will never call IsValidToStartPathingRegion unless logic from another pathing region calls tryToMakeNewRegion with this region type
/// </summary>
DefaultCheckNoBlocks = (1 << 2),
/// <summary>
/// Pathing will try to see if we can start a chunk from any blocks via calling IsValidToStartPathingRegion on them, no spawning from other pathing regions is required
/// This is equivalent to DefaultCheckAirBlocks | DefaultCheckSolidBlocks
/// </summary>
DefaultCheckAllBlocks = DefaultCheckAirBlocks | DefaultCheckSolidBlocks
}
public delegate void TryToExpand(ExpandFlags flags);
public delegate void TryToExpandCustom(int relativeX, int relativeY, int relativeZ);
public delegate void TryToMakeNewRegion(PathingRegionType2 regionType, bool canTraverseInto, bool canTraverseFrom, int relativeX, int relativeY, int relativeZ);
public abstract class CustomRegion
{
public abstract bool IsValidToStartPathingRegion(PathNode block, GetRelativeBlock getRelativeBlock, MobilityCriteria mobilityCriteria, TryToExpand tryToExpand, TryToExpandCustom tryToExpandCustom, TryToMakeNewRegion tryToMakeNewRegion);
/// <summary>
/// Return true if it is okay to expand this pathing region from prevBlock to block
/// </summary>
/// <param name="block"></param>
/// <param name="prevBlock"></param>
/// <param name="getRelativeBlock"></param>
/// <param name="getRelativeBlockToPrev"></param>
/// <param name="mobilityCriteria"></param>
/// <returns></returns>
public abstract bool IsValidToExpandPathingRegion(PathNode block, PathNode prevBlock, GetRelativeBlock getRelativeBlock, GetRelativeBlock getRelativeBlockToPrev, MobilityCriteria mobilityCriteria);
/// <summary>
/// Tell us what positions to further expand this region. This method will only be called if IsValidToExpandPathingRegion(block, prevBlock, ...) returned true
/// Sometimes IsValidToExpandPathingRegion will return true and this method still won't be called, in the case of connecting two regions together
/// </summary>
/// <param name="block"></param>
/// <param name="prevBlock"></param>
/// <param name="getRelativeBlock"></param>
/// <param name="getRelativeBlockToPrev"></param>
/// <param name="mobilityCriteria"></param>
/// <param name="tryToExpand"></param>
/// <param name="tryToExpandCustom"></param>
/// <param name="tryToMakeOrConnectToRegion"></param>
/// <returns></returns>
public abstract void ExpandPathingRegion(PathNode block, PathNode prevBlock, GetRelativeBlock getRelativeBlock, GetRelativeBlock getRelativeBlockToPrev, MobilityCriteria mobilityCriteria, TryToExpand tryToExpand, TryToExpandCustom tryToExpandCustom, TryToMakeNewRegion tryToMakeOrConnectToRegion);
public abstract RegionSettingsFlags GetSettings();
}
public class Pathing3
{
public static Dictionary<PathingRegionType2, CustomRegion> pathingRegions = new Dictionary<PathingRegionType2, CustomRegion>();
public static List<CustomRegion> customRegionSolidBlocks = new List<CustomRegion>();
public static List<CustomRegion> customRegionAirBlocks = new List<CustomRegion>();
public static void SetupPathing()
{
if (pathingRegions.Count == 0)
{
//pathingRegions[PathingRegionType2.Walking] = new WalkingRegion();
//pathingRegions[PathingRegionType2.Falling] = new FallingRegion();
foreach (KeyValuePair<PathingRegionType2, CustomRegion> cur in pathingRegions)
{
if ((cur.Value.GetSettings() & RegionSettingsFlags.DefaultCheckAirBlocks) != 0)
{
customRegionAirBlocks.Add(cur.Value);
}
if ((cur.Value.GetSettings() & RegionSettingsFlags.DefaultCheckSolidBlocks) != 0)
{
customRegionSolidBlocks.Add(cur.Value);
}
}
}
}
public static void DoPathing()
{
SetupPathing();
}
}
public class PathingChunk3
{
public PathingNodeBlockChunk locationSpec;
public MobilityCriteria mobilityCriteria;
public PatchingNodeBlockChunkData data;
public Chunk chunk;
public World world;
public PathingChunk3(Chunk chunk, MobilityCriteria mobilityCriteria)
{
this.locationSpec = new PathingNodeBlockChunk(chunk.world,
chunk.cx * World.chunkSizeX,
chunk.cy * World.chunkSizeY,
chunk.cz * World.chunkSizeZ,
chunk.cx * World.chunkSizeX + World.chunkSizeX - 1,
chunk.cy * World.chunkSizeY + World.chunkSizeY - 1,
chunk.cz * World.chunkSizeZ + World.chunkSizeZ - 1);
this.chunk = chunk;
this.world = chunk.world;
this.data = new PatchingNodeBlockChunkData(locationSpec);
this.mobilityCriteria = mobilityCriteria;
}
public GetRelativeBlock GetRelativeBlockGetter(PathNode relativeTo)
{
long relativeX = relativeTo.pos.x - locationSpec.minX;
long relativeY = relativeTo.pos.y - locationSpec.minY;
long relativeZ = relativeTo.pos.z - locationSpec.minZ;
return GetRelativeBlockGetter(relativeX, relativeY, relativeZ);
}
HashSet<Chunk> chunksDependingOn = new HashSet<Chunk>();
public GetRelativeBlock GetRelativeBlockGetter(long relX, long relY, long relZ)
{
return (rx, ry, rz) =>
{
long curX = rx + relX;
long curY = ry + relY;
long curZ = rz + relZ;
if (curX < 0 || curX >= locationSpec.xWidth ||
curY < 0 || curY >= locationSpec.yWidth ||
curZ < 0 || curZ >= locationSpec.zWidth)
{
Chunk chunkDependingOn = world.GetChunkAtPos(curX + locationSpec.minX, curY + locationSpec.minY, curZ + locationSpec.minZ);
if (!chunksDependingOn.Contains(chunkDependingOn))
{
chunksDependingOn.Add(chunkDependingOn);
}
return chunkDependingOn[curX + locationSpec.minX, curY + locationSpec.minY, curZ + locationSpec.minZ];
}
else
{
return locationSpec[(int)curX, (int)curY, (int)curZ];
}
};
}
List<PathNode> ExpandFlagsToNodes(ExpandFlags expandFlags, PathNode curNode)
{
long wx = curNode.pos.x;
long wy = curNode.pos.y;
long wz = curNode.pos.z;
List<PathNode> result = new List<PathNode>();
if ((expandFlags & ExpandFlags.NegX) != 0) result.Add(new PathNode(new LVector3(wx - 1, wy, wz), curNode));
if ((expandFlags & ExpandFlags.PosX) != 0) result.Add(new PathNode(new LVector3(wx + 1, wy, wz), curNode));
if ((expandFlags & ExpandFlags.NegY) != 0) result.Add(new PathNode(new LVector3(wx, wy - 1, wz), curNode));
if ((expandFlags & ExpandFlags.PosY) != 0) result.Add(new PathNode(new LVector3(wx, wy + 1, wz), curNode));
if ((expandFlags & ExpandFlags.NegZ) != 0) result.Add(new PathNode(new LVector3(wx, wy, wz - 1), curNode));
if ((expandFlags & ExpandFlags.PosZ) != 0) result.Add(new PathNode(new LVector3(wx, wy, wz + 1), curNode));
return result;
}
// TODO: make sure data is setup properly before calling this
void TryToStartNode(CustomRegion customRegion, long relX, long relY, long relZ)
{
Queue<PathNode> nodesToExpandTo = new Queue<PathNode>();
Queue<PathNode> nodesToConnectToOutsidePathingChunk = new Queue<PathNode>();
Queue<Tuple<CustomRegion, PathNode>> customRegionsToMake = new Queue<Tuple<CustomRegion, PathNode>>();
Queue<Tuple<CustomRegion, PathNode>> customRegionsToMakeOutside = new Queue<Tuple<CustomRegion, PathNode>>();
PathNode curNode = new PathNode(new LVector3(relX + locationSpec.minX, relY + locationSpec.minY, relZ + locationSpec.minZ), null);
if (customRegion.IsValidToStartPathingRegion(curNode, GetRelativeBlockGetter(relX, relY, relZ), mobilityCriteria,
// Try to expand
(expandFlags) =>
{
nodesToExpandTo.EnqueueRange(ExpandFlagsToNodes(expandFlags, curNode));
},
// Try to expand custom
(rx, ry, rz) =>
{
long offsetX = rx - relX;
long offsetY = ry - relY;
long offsetZ = rz - relZ;
nodesToExpandTo.Enqueue(new PathNode(new LVector3(curNode.pos.x + offsetX, curNode.pos.y + offsetY, curNode.pos.z + offsetZ), curNode));
},
// Try to make new custom region
(regionType, canTraverseInto, canTraverseFrom, rx, ry, rz) =>
{
long offsetX = rx - relX;
long offsetY = ry - relY;
long offsetZ = rz - relZ;
PathNode customRegionNode = new PathNode(new LVector3(curNode.pos.x + offsetX, curNode.pos.y + offsetY, curNode.pos.z + offsetZ), curNode);
customRegionsToMake.Enqueue(new Tuple<CustomRegion, PathNode>(Pathing3.pathingRegions[regionType], customRegionNode));
})
)
// Returned true, we can start a pathing region here
{
bool didSomething = true;
while (didSomething)
{
didSomething = false;
// Keep expanding while we can
while (nodesToExpandTo.Count > 0)
{
PathNode nodeToExpandTo = nodesToExpandTo.Dequeue();
bool validToExpand = customRegion.IsValidToExpandPathingRegion(nodeToExpandTo, nodeToExpandTo.prevNode, GetRelativeBlockGetter(nodeToExpandTo), GetRelativeBlockGetter(nodeToExpandTo.prevNode), mobilityCriteria);
if (!validToExpand)
{
continue;
}
// Expand to something in this pathing chunk
if (this.locationSpec.ContainsPosition(nodeToExpandTo.pos))
{
long posRelX = nodeToExpandTo.pos.x - locationSpec.minX;
long posRelY = nodeToExpandTo.pos.y - locationSpec.minY;
long posRelZ = nodeToExpandTo.pos.z - locationSpec.minZ;
ushort regionOfNodeToExpandTo = data[(int)posRelX, (int)posRelY, (int)posRelZ];
// Connect to existing region
if (regionOfNodeToExpandTo != 0)
{
}
else
{
customRegion.ExpandPathingRegion(nodeToExpandTo, nodeToExpandTo.prevNode, GetRelativeBlockGetter(nodeToExpandTo), GetRelativeBlockGetter(nodeToExpandTo.prevNode), mobilityCriteria,
// Try to expand
(expandFlags) =>
{
nodesToExpandTo.EnqueueRange(ExpandFlagsToNodes(expandFlags, nodeToExpandTo));
},
// Try to expand custom
(rx, ry, rz) =>
{
long offsetX = rx - relX;
long offsetY = ry - relY;
long offsetZ = rz - relZ;
nodesToExpandTo.Enqueue(new PathNode(new LVector3(curNode.pos.x + offsetX, curNode.pos.y + offsetY, curNode.pos.z + offsetZ), curNode));
},
// Try to make new custom region
(regionType, canTraverseInto, canTraverseFrom, rx, ry, rz) =>
{
long offsetX = rx - relX;
long offsetY = ry - relY;
long offsetZ = rz - relZ;
PathNode customRegionNode = new PathNode(new LVector3(curNode.pos.x + offsetX, curNode.pos.y + offsetY, curNode.pos.z + offsetZ), curNode);
customRegionsToMake.Enqueue(new Tuple<CustomRegion, PathNode>(Pathing3.pathingRegions[regionType], customRegionNode));
}
);
}
}
// Expand to something outside this pathing chunk
else
{
nodesToConnectToOutsidePathingChunk.Enqueue(nodeToExpandTo);
}
}
// If we run out of things to expand to, try making the new regions now
while (customRegionsToMake.Count > 0)
{
}
}
}
// Returned false, we cannot start a pathing region here
else
{
if (nodesToExpandTo.Count > 0 || customRegionsToMake.Count > 0)
{
Debug.LogWarning("IsValidToStartPathingRegion returned false, yet tried to expand, this is invalid, doing nothing");
}
}
}
public bool OutsidePathingChunk(int relX, int relY, int relZ)
{
return relX < 0 || relX >= locationSpec.xWidth ||
relY < 0 || relY >= locationSpec.yWidth ||
relZ < 0 || relZ >= locationSpec.zWidth;
}
public void InitChunk()
{
// reset values to zero
for (int x = 0; x < locationSpec.xWidth; x++)
{
for (int y = 0; y < locationSpec.yWidth; y++)
{
for (int z = 0; z < locationSpec.zWidth; z++)
{
data[x, y, z] = 0;
}
}
}
for (int x = 0; x < locationSpec.xWidth; x++)
{
for (int y = 0; y < locationSpec.yWidth; y++)
{
for (int z = 0; z < locationSpec.zWidth; z++)
{
BlockValue curBlock = locationSpec[x, y, z];
if (curBlock == BlockValue.Air)
{
foreach (CustomRegion customRegion in Pathing3.customRegionAirBlocks)
{
}
}
else
{
foreach (CustomRegion customRegion in Pathing3.customRegionSolidBlocks)
{
}
}
}
}
}
}
}
/*
public class WalkingRegion : CustomRegion
{
public override RegionSettingsFlags GetSettings()
{
return RegionSettingsFlags.DefaultCheckSolidBlocks;
}
public override bool IsValidToExpandPathingRegion(PathNode block, PathNode prevBlock, GetRelativeBlock getRelativeBlock, GetRelativeBlock getRelativeBlockToPrev, MobilityCriteria mobilityCriteria, TryToExpand tryToExpand, TryToExpandCustom tryToExpandCustom, TryToMakeNewRegion tryToMakeOrConnectToRegion)
{
bool isValid = false;
if (mobilityCriteria.CanStandOn(getRelativeBlock))
{
int yStandOnPrev;
//int offsetX = (int)(prevBlock.pos.x - block.pos.x); // if block is at 4, prev is at 2, offset will be 2-4 = -2 This means that adding offset will give us what we want, since 0 will become -2 which is correct relative to block
//int offsetY = (int)(prevBlock.pos.y - block.pos.y);
//int offsetZ = (int)(prevBlock.pos.z - block.pos.z);
if (mobilityCriteria.TryGetRelativeBlockYAboveThatWeCanStandOn(getRelativeBlockToPrev, mobilityCriteria.jumpHeight, out yStandOnPrev))
{
// we moved over on flat ground, easy
if (yStandOnPrev == 0)
{
isValid = true;
}
// we trickled from something that is below the actual one (so we need to jump), check that there is nothing in the way and that we can make that jump in both directions
else
{
PathNode blockFrom = new PathNode(prevBlock.pos + new LVector3(0, yStandOnPrev, 0), null);
PathNode blockTo = block;
GetRelativeBlock getRelativeTo = (rx, ry, rz) =>
{
return getRelativeBlockToPrev(rx, ry + yStandOnPrev, rz);
};
// make sure we can actually traverse that in both directions
if (mobilityCriteria.CanJumpBetween(blockFrom, blockTo, getRelativeBlock, getRelativeTo) &&
mobilityCriteria.CanJumpBetween(blockTo, blockFrom, getRelativeTo, getRelativeBlock))
{
isValid = true;
}
}
}
}
else if(block.pos.x == prevBlock.pos.x && block.pos.z == prevBlock.pos.z && getRelativeBlock(0,0,0) != BlockValue.Air)
{
isValid = true;
}
if (isValid)
{
tryToExpand(ExpandFlags.All6Neighbors);
}
else
{
// if they are stairs we can jump up and down on, no need to make falling regions, that'll just bloat the number of the regions and slow down pathing
bool simpleStairs = false;
if (mobilityCriteria.HasEnoughAirAbove(getRelativeBlock))
{
for (int y = -1; y >= -mobilityCriteria.jumpHeight; y--)
{
if (mobilityCriteria.CanStandOn((rx,ry,rz) =>
{
return getRelativeBlock(rx, ry + y, rz);
}))
{
simpleStairs = true;
break;
}
}
}
// we can actually fall here and not get back this way, make a falling region
if (!simpleStairs)
{
tryToMakeOrConnectToRegion(PathingRegionType2.Falling, true, false, 0, 0, 0);
}
}
return isValid;
}
public override bool IsValidToStartPathingRegion(PathNode block, GetRelativeBlock getRelativeBlock, MobilityCriteria mobilityCriteria, TryToExpand tryToExpand, TryToExpandCustom tryToExpandCustom, TryToMakeNewRegion tryToMakeNewRegion)
{
return mobilityCriteria.CanStandOn(getRelativeBlock);
}
}
public class FallingRegion : CustomRegion
{
public override RegionSettingsFlags GetSettings()
{
return RegionSettingsFlags.DefaultCheckNoBlocks; // only spawn from walking regions
}
public override bool IsValidToExpandPathingRegion(PathNode block, PathNode prevBlock, GetRelativeBlock getRelativeBlock, GetRelativeBlock getRelativeBlockToPrev, MobilityCriteria mobilityCriteria, TryToExpand tryToExpand, TryToExpandCustom tryToExpandCustom, TryToMakeNewRegion tryToMakeOrConnectToRegion)
{
bool isValid = false;
// we can stand there, connect to standing region there
if (mobilityCriteria.CanStandOn(getRelativeBlock))
{
tryToMakeOrConnectToRegion(PathingRegionType2.Walking, true, false, 0, 0, 0);
}
else
{
isValid = mobilityCriteria.HasEnoughAirToFitHere(getRelativeBlock);
if (!isValid)
{
Debug.LogWarning("warning: falling region cannot fall anymore but didn't hit standing region, how did this happen? (could have to do with this unit being fat, the mobility criteria is " + mobilityCriteria + ")");
}
}
if (isValid)
{
tryToExpand(ExpandFlags.Down);
}
return isValid;
}
public override bool IsValidToStartPathingRegion(PathNode block, GetRelativeBlock getRelativeBlock, MobilityCriteria mobilityCriteria, TryToExpand tryToExpand, TryToExpandCustom tryToExpandCustom, TryToMakeNewRegion tryToMakeNewRegion)
{
// make sure we can't stand there (otherwise we can't fall) and make sure we can fit there to fall
bool isValid = !mobilityCriteria.CanStandOn(getRelativeBlock) && mobilityCriteria.HasEnoughAirToFitHere(getRelativeBlock);
if (isValid)
{
tryToExpand(ExpandFlags.Down);
}
return isValid;
}
}
*/
// For a chunk:
// for each non-air block in the chunk:
// if not in a region already and can be stood on/inside
// grow region to all blocks that can be reached from that block in a reversable way (so you can get back, i.e., no falling)
// ways to move:
// sideways (spread immediately to blocks that also have air above them or can be stood on/inside)
// up stairs (spread jump height (regular jump height is 1) below any block with air above it)
// jump: TODO
// connect region to all blocks that can be reached from that block in an irreversable way (falling)
// we'll just make a "falling region": vertical column of air
// For neighboring chunks:
// mark as "linked" any regions that can be reached in a reversable way
// mark as "linked" any regions that can be reached through the "falling regions"
public class PathingRegionConnection
{
public PathingRegion from;
public PathingRegion to;
// used as a "visited"
public long pathingCounter = -1;
public PathingRegionPos posInFrom;
public PathingRegionPos posInTo;
public PathingRegionPos ConnectPathToConection(PathingRegionPos startPosInFrom)
{
PathingRegionPos pathToStartOfConnection = from.TraverseThroughRegion(startPosInFrom, posInFrom);
PathingRegionPos pathToEndOfConnection = new PathingRegionPos(posInTo.localX, posInTo.localY, posInTo.localZ, pathToStartOfConnection, to.parentChunk);
return pathToEndOfConnection;
}
public PathingRegionConnection(PathingRegion from, PathingRegion to, PathingRegionPos posInFrom, PathingRegionPos posInTo)
{
this.from = from;
this.to = to;
this.posInFrom = posInFrom;
this.posInTo = posInTo;
}
}
public class PathingRegionPos
{
public PathingChunk parentRegion;
public PathingRegionPos prev;
public int pathLen;
public int localX, localY, localZ;
public long wx { get { return parentRegion.locationSpec.minX + localX; } private set { } }
public long wy { get { return parentRegion.locationSpec.minY + localY; } private set { } }
public long wz { get { return parentRegion.locationSpec.minZ + localZ; } private set { } }
public BlockValue Block
{
get
{
return parentRegion.locationSpec[localX, localY, localZ];
}
private set
{
}
}
public PathingRegionPos CopyPath()
{
if (prev != null)
{
return new PathingRegionPos(localX, localY, localZ, prev.CopyPath(), parentRegion);
}
else
{
return new PathingRegionPos(localX, localY, localZ, null, parentRegion);
}
}
public void RemoveDuplicatePositionsInPath()
{
while (prev != null && prev.prev != null && PathingRegionPos.PositionsAreEqual(this, prev))
{
prev = prev.prev;
}
if (prev != null)
{
prev.RemoveDuplicatePositionsInPath();
}
}
public long emptyBlockAboveY
{
get
{
for (long curY = wy; curY < wy + parentRegion.mobilityCriteria.jumpHeight + parentRegion.mobilityCriteria.neededSizeUp; curY++)
{
bool canBreathe = true;
for (long curY2 = curY; curY2 < curY + parentRegion.mobilityCriteria.neededSizeUp; curY2++)
{
if (parentRegion.world[wx, curY2, wz] != BlockValue.Air)
{
canBreathe = false;
}
}
if (canBreathe)
{
return curY;
}
}
return wy;
}
set
{
}
}
Vector3 unityVec;
bool gotUnityVec = false;
public Vector3 UnityVector3()
{
if (!gotUnityVec)
{
unityVec = (new LVector3(wx, wy, wz)).BlockCentertoUnityVector3();
gotUnityVec = true;
}
return unityVec;
}
public static long Dist(PathingRegionPos a, PathingRegionPos b)
{
return Math.Abs(a.wx - b.wx) + Math.Abs(a.wy - b.wy) + Math.Abs(a.wz - b.wz);
}
public static long Dist(PathingRegionPos a, LVector3 b)
{
return Math.Abs(a.wx - b.x) + Math.Abs(a.wy - b.y) + Math.Abs(a.wz - b.z);
}
public override string ToString()
{
return "[" +
localX + "(" + wx + ")" + "," +
localY + "(" + wy + ")" + "," +
localZ + "(" + wz + ")" + "]";
}
public static long Dist(LVector3 a, PathingRegionPos b)
{
return Dist(b, a);
}
public static bool PositionsAreEqual(PathingRegionPos a, PathingRegionPos b)
{
return a.wx == b.wx && a.wy == b.wy && a.wz == b.wz;
}
public PathingRegionPos(LVector3 pos, PathingRegionPos prev, PathingChunk parentRegion)
{
InitFields(
(int)(pos.x - parentRegion.locationSpec.minX),
(int)(pos.y - parentRegion.locationSpec.minY),
(int)(pos.z - parentRegion.locationSpec.minZ),
prev, parentRegion);
}
public PathingRegionPos(int localX, int localY, int localZ, PathingRegionPos prev, PathingChunk parentRegion)
{
InitFields(localX, localY, localZ, prev, parentRegion);
}
void InitFields(int localX, int localY, int localZ, PathingRegionPos prev, PathingChunk parentRegion)
{
this.localX = localX;
this.localY = localY;
this.localZ = localZ;
this.prev = prev;
this.parentRegion = parentRegion;
if (prev != null)
{
if (PositionsAreEqual(this, prev))