| | | 1 | | using FixedMathSharp; |
| | | 2 | | using GridForge.Grids; |
| | | 3 | | |
| | | 4 | | namespace Trailblazer.Navigation; |
| | | 5 | | |
| | | 6 | | internal static class NavigatorOccupancyTracker |
| | | 7 | | { |
| | | 8 | | public static void Update( |
| | | 9 | | GridWorld world, |
| | | 10 | | Navigator navigator, |
| | | 11 | | Vector3d position, |
| | | 12 | | Vector3d lastPosition, |
| | | 13 | | bool init) |
| | | 14 | | { |
| | 192 | 15 | | if (!init && position == lastPosition) |
| | 1 | 16 | | return; |
| | | 17 | | |
| | 191 | 18 | | bool voxelFound = world.TryGetGridAndVoxel( |
| | 191 | 19 | | position, |
| | 191 | 20 | | out VoxelGrid? curGrid, |
| | 191 | 21 | | out Voxel? curVoxel); |
| | 191 | 22 | | if (!voxelFound) |
| | 2 | 23 | | return; |
| | | 24 | | |
| | 189 | 25 | | if (curGrid!.TryAddVoxelOccupant(curVoxel!, navigator) == false) |
| | | 26 | | { |
| | 52 | 27 | | TrailblazerLogger.Channel.Warn($"Navigator {navigator.GlobalId} failed to register occupancy in voxel {curVo |
| | 52 | 28 | | return; |
| | | 29 | | } |
| | | 30 | | |
| | 137 | 31 | | bool lastVoxelFound = world.TryGetGridAndVoxel( |
| | 137 | 32 | | lastPosition, |
| | 137 | 33 | | out VoxelGrid? lastGrid, |
| | 137 | 34 | | out Voxel? lastVoxel); |
| | | 35 | | |
| | 137 | 36 | | if (!lastVoxelFound || curVoxel == lastVoxel) |
| | 133 | 37 | | return; |
| | | 38 | | |
| | 4 | 39 | | lastGrid!.TryRemoveVoxelOccupant(lastVoxel!, navigator); |
| | 4 | 40 | | } |
| | | 41 | | |
| | | 42 | | public static void UpdateAfterRootProjection( |
| | | 43 | | GridWorld world, |
| | | 44 | | Navigator navigator, |
| | | 45 | | Vector3d oldPosition, |
| | | 46 | | Vector3d newPosition) |
| | | 47 | | { |
| | 2 | 48 | | bool oldVoxelFound = world.TryGetGridAndVoxel( |
| | 2 | 49 | | oldPosition, |
| | 2 | 50 | | out VoxelGrid? oldGrid, |
| | 2 | 51 | | out Voxel? oldVoxel); |
| | 2 | 52 | | bool newVoxelFound = world.TryGetGridAndVoxel( |
| | 2 | 53 | | newPosition, |
| | 2 | 54 | | out VoxelGrid? newGrid, |
| | 2 | 55 | | out Voxel? newVoxel); |
| | | 56 | | |
| | 2 | 57 | | if (oldVoxelFound && newVoxelFound && oldVoxel == newVoxel) |
| | 0 | 58 | | return; |
| | | 59 | | |
| | 2 | 60 | | if (oldVoxelFound) |
| | 2 | 61 | | oldGrid!.TryRemoveVoxelOccupant(oldVoxel!, navigator); |
| | | 62 | | |
| | 2 | 63 | | if (newVoxelFound && newGrid!.TryAddVoxelOccupant(newVoxel!, navigator) == false) |
| | 0 | 64 | | TrailblazerLogger.Channel.Warn($"Navigator {navigator.GlobalId} failed to register occupancy in voxel {newVo |
| | 2 | 65 | | } |
| | | 66 | | } |