| | | 1 | | //======================================================================= |
| | | 2 | | // NavigatorOccupancyTracker.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using GridForge.Configuration; |
| | | 10 | | using GridForge.Grids; |
| | | 11 | | using GridForge.Grids.Topology; |
| | | 12 | | |
| | | 13 | | namespace Trailblazer.Navigation; |
| | | 14 | | |
| | | 15 | | internal static class NavigatorOccupancyTracker |
| | | 16 | | { |
| | | 17 | | public static void Update( |
| | | 18 | | GridWorld world, |
| | | 19 | | Navigator navigator, |
| | | 20 | | Vector3d position, |
| | | 21 | | Vector3d lastPosition, |
| | | 22 | | bool init) |
| | | 23 | | { |
| | 315 | 24 | | if (!init && position == lastPosition) |
| | 24 | 25 | | return; |
| | | 26 | | |
| | 291 | 27 | | bool voxelFound = TryResolveVoxel( |
| | 291 | 28 | | world, |
| | 291 | 29 | | position, |
| | 291 | 30 | | out VoxelGrid? curGrid, |
| | 291 | 31 | | out Voxel? curVoxel); |
| | 291 | 32 | | if (!voxelFound) |
| | 5 | 33 | | return; |
| | | 34 | | |
| | 286 | 35 | | if (curGrid!.TryAddVoxelOccupant(curVoxel!, navigator) == false) |
| | | 36 | | { |
| | 31 | 37 | | TrailblazerLogger.Channel.Warn($"Navigator {navigator.GlobalId} failed to register occupancy in voxel {curVo |
| | 31 | 38 | | return; |
| | | 39 | | } |
| | | 40 | | |
| | 255 | 41 | | bool lastVoxelFound = TryResolveVoxel( |
| | 255 | 42 | | world, |
| | 255 | 43 | | lastPosition, |
| | 255 | 44 | | out VoxelGrid? lastGrid, |
| | 255 | 45 | | out Voxel? lastVoxel); |
| | | 46 | | |
| | 255 | 47 | | if (!lastVoxelFound || curVoxel == lastVoxel) |
| | 253 | 48 | | return; |
| | | 49 | | |
| | 2 | 50 | | lastGrid!.TryRemoveVoxelOccupant(lastVoxel!, navigator); |
| | 2 | 51 | | } |
| | | 52 | | |
| | | 53 | | public static void UpdateAfterRootProjection( |
| | | 54 | | GridWorld world, |
| | | 55 | | Navigator navigator, |
| | | 56 | | Vector3d oldPosition, |
| | | 57 | | Vector3d newPosition) |
| | | 58 | | { |
| | 8 | 59 | | bool oldVoxelFound = TryResolveVoxel( |
| | 8 | 60 | | world, |
| | 8 | 61 | | oldPosition, |
| | 8 | 62 | | out VoxelGrid? oldGrid, |
| | 8 | 63 | | out Voxel? oldVoxel); |
| | 8 | 64 | | bool newVoxelFound = TryResolveVoxel( |
| | 8 | 65 | | world, |
| | 8 | 66 | | newPosition, |
| | 8 | 67 | | out VoxelGrid? newGrid, |
| | 8 | 68 | | out Voxel? newVoxel); |
| | | 69 | | |
| | 8 | 70 | | if (oldVoxelFound && newVoxelFound && oldVoxel == newVoxel) |
| | 1 | 71 | | return; |
| | | 72 | | |
| | 7 | 73 | | if (oldVoxelFound) |
| | 6 | 74 | | oldGrid!.TryRemoveVoxelOccupant(oldVoxel!, navigator); |
| | | 75 | | |
| | 7 | 76 | | if (newVoxelFound && newGrid!.TryAddVoxelOccupant(newVoxel!, navigator) == false) |
| | 2 | 77 | | TrailblazerLogger.Channel.Warn($"Navigator {navigator.GlobalId} failed to register occupancy in voxel {newVo |
| | 7 | 78 | | } |
| | | 79 | | |
| | | 80 | | internal static bool TryResolveVoxel( |
| | | 81 | | GridWorld world, |
| | | 82 | | Vector3d position, |
| | | 83 | | out VoxelGrid? grid, |
| | | 84 | | out Voxel? voxel) |
| | | 85 | | { |
| | 631 | 86 | | voxel = null; |
| | 631 | 87 | | if (world.TryGetGrid(position, out grid)) |
| | | 88 | | { |
| | 590 | 89 | | if (!grid!.TryGetClosestVoxel(position, out voxel)) |
| | | 90 | | { |
| | 1 | 91 | | grid = null; |
| | 1 | 92 | | return false; |
| | | 93 | | } |
| | | 94 | | } |
| | 41 | 95 | | else if (!world.TryGetClosestGridAndVoxel(position, out grid, out voxel)) |
| | | 96 | | { |
| | 1 | 97 | | return false; |
| | | 98 | | } |
| | | 99 | | |
| | 629 | 100 | | grid!.Configuration.TryNormalize(out NormalizedGridConfiguration binding); |
| | 629 | 101 | | binding.TryGetCellPrism(voxel!.Index, out GridCellPrism prism); |
| | 629 | 102 | | if (!prism.Contains(position)) |
| | | 103 | | { |
| | 11 | 104 | | grid = null; |
| | 11 | 105 | | voxel = null; |
| | 11 | 106 | | return false; |
| | | 107 | | } |
| | | 108 | | |
| | 618 | 109 | | return true; |
| | | 110 | | } |
| | | 111 | | } |