|
| 1 | +using System; |
| 2 | +using NUnit.Framework; |
| 3 | +using Unity.Collections; |
| 4 | +using Unity.Collections.LowLevel.Unsafe; |
| 5 | +using Unity.Entities.Tests; |
| 6 | +using Unity.Mathematics; |
| 7 | + |
| 8 | +namespace Samples.GridPath.Tests |
| 9 | +{ |
| 10 | + [TestFixture] |
| 11 | + unsafe class CartesianGridOnPlaneShortestPathTests : ECSTestsFixture |
| 12 | + { |
| 13 | + struct TestGrid : IDisposable |
| 14 | + { |
| 15 | + public int RowCount; |
| 16 | + public int ColCount; |
| 17 | + public byte* Walls; |
| 18 | + public int WallRowStride => (ColCount + 1) / 2; |
| 19 | + |
| 20 | + public TestGrid(int rowCount, int colCount) |
| 21 | + { |
| 22 | + RowCount = rowCount; |
| 23 | + ColCount = colCount; |
| 24 | + |
| 25 | + int wallRowStride = (colCount + 1) / 2; |
| 26 | + int wallsSize = rowCount * wallRowStride; |
| 27 | + |
| 28 | + Walls = (byte*) UnsafeUtility.Malloc(wallsSize, 16, Allocator.Temp); |
| 29 | + UnsafeUtility.MemClear(Walls, wallsSize); |
| 30 | + |
| 31 | + // Add outer boundary walls. |
| 32 | + for (var x = 0; x < colCount; x++) |
| 33 | + AddWallNorth(new CartesianGridCoordinates {x = (short) x, y = (short) (rowCount - 1)}); |
| 34 | + for (var x = 0; x < colCount; x++) |
| 35 | + AddWallSouth(new CartesianGridCoordinates {x = (short) x, y = (short) 0}); |
| 36 | + for (var y = 0; y < rowCount; y++) |
| 37 | + AddWallWest(new CartesianGridCoordinates {x = (short) 0, y = (short) y}); |
| 38 | + for (var y = 0; y < rowCount; y++) |
| 39 | + AddWallEast(new CartesianGridCoordinates {x = (short) (colCount-1), y = (short) y}); |
| 40 | + } |
| 41 | + |
| 42 | + public void Dispose() |
| 43 | + { |
| 44 | + if (Walls != null) |
| 45 | + UnsafeUtility.Free(Walls, Allocator.Temp); |
| 46 | + } |
| 47 | + |
| 48 | + public void SetWallBit(int x, int y, CartesianGridDirectionBit directionBit) |
| 49 | + { |
| 50 | + if (new CartesianGridCoordinates { x=(short)x, y=(short)y}.OnGrid(RowCount,ColCount)) |
| 51 | + Walls[y * WallRowStride + (x >> 1)] |= (byte)((byte)directionBit << (4 * (x & 1))); |
| 52 | + } |
| 53 | + |
| 54 | + public bool TestWallBit(int x, int y, CartesianGridDirectionBit directionBit) |
| 55 | + { |
| 56 | + if (!(new CartesianGridCoordinates {x = (short) x, y = (short) y}.OnGrid(RowCount, ColCount))) |
| 57 | + return false; |
| 58 | + |
| 59 | + return (((Walls[y * WallRowStride + (x >> 1)] >> (4 * (x&1))) & (byte)directionBit) == (byte)directionBit); |
| 60 | + } |
| 61 | + |
| 62 | + public void AddWallNorth(CartesianGridCoordinates cellPosition) |
| 63 | + { |
| 64 | + SetWallBit(cellPosition.x, cellPosition.y, CartesianGridDirectionBit.North); |
| 65 | + SetWallBit(cellPosition.x, cellPosition.y+1, CartesianGridDirectionBit.South); |
| 66 | + } |
| 67 | + public void AddWallSouth(CartesianGridCoordinates cellPosition) |
| 68 | + { |
| 69 | + SetWallBit(cellPosition.x, cellPosition.y, CartesianGridDirectionBit.South); |
| 70 | + SetWallBit(cellPosition.x, cellPosition.y-1, CartesianGridDirectionBit.North); |
| 71 | + } |
| 72 | + public void AddWallWest(CartesianGridCoordinates cellPosition) |
| 73 | + { |
| 74 | + SetWallBit(cellPosition.x, cellPosition.y, CartesianGridDirectionBit.West); |
| 75 | + SetWallBit(cellPosition.x-1, cellPosition.y, CartesianGridDirectionBit.East); |
| 76 | + } |
| 77 | + public void AddWallEast(CartesianGridCoordinates cellPosition) |
| 78 | + { |
| 79 | + SetWallBit(cellPosition.x, cellPosition.y, CartesianGridDirectionBit.East); |
| 80 | + SetWallBit(cellPosition.x+1, cellPosition.y, CartesianGridDirectionBit.West); |
| 81 | + } |
| 82 | + |
| 83 | + int WalkPath(CartesianGridCoordinates startPosition, NativeArray<byte> targetDirections, int pathOffset) |
| 84 | + { |
| 85 | + var validDirections = CartesianGridOnPlaneShortestPath.LookupDirectionToTarget(startPosition, RowCount, ColCount, targetDirections); |
| 86 | + var direction = CartesianGridMovement.PathVariation[((pathOffset&3) * 16) + validDirections]; |
| 87 | + |
| 88 | + if (direction == 0xff) // No path |
| 89 | + return 0; |
| 90 | + |
| 91 | + var nextPosition = new CartesianGridCoordinates(); |
| 92 | + if (direction == 0) |
| 93 | + nextPosition = new CartesianGridCoordinates {x = startPosition.x, y = (short) (startPosition.y + 1)}; |
| 94 | + else if (direction == 1) |
| 95 | + nextPosition = new CartesianGridCoordinates {x = startPosition.x, y = (short) (startPosition.y - 1)}; |
| 96 | + else if (direction == 2) |
| 97 | + nextPosition = new CartesianGridCoordinates {x = (short)(startPosition.x-1), y = (short) startPosition.y}; |
| 98 | + else if (direction == 3) |
| 99 | + nextPosition = new CartesianGridCoordinates {x = (short)(startPosition.x+1), y = (short) startPosition.y}; |
| 100 | + else |
| 101 | + Assert.Fail(); |
| 102 | + |
| 103 | + // Test no wall in the direction given |
| 104 | + if (direction == 0) |
| 105 | + Assert.IsFalse(TestWallBit(startPosition.x, startPosition.y, CartesianGridDirectionBit.North)); |
| 106 | + else if (direction == 1) |
| 107 | + Assert.IsFalse(TestWallBit(startPosition.x, startPosition.y, CartesianGridDirectionBit.South)); |
| 108 | + else if (direction == 2) |
| 109 | + Assert.IsFalse(TestWallBit(startPosition.x, startPosition.y, CartesianGridDirectionBit.West)); |
| 110 | + else if (direction == 3) |
| 111 | + Assert.IsFalse(TestWallBit(startPosition.x, startPosition.y, CartesianGridDirectionBit.East)); |
| 112 | + |
| 113 | + return 1 + WalkPath(nextPosition, targetDirections, pathOffset); |
| 114 | + } |
| 115 | + |
| 116 | + public int WalkPathDistance(CartesianGridCoordinates sourcePosition, CartesianGridCoordinates targetPosition) |
| 117 | + { |
| 118 | + var directionsRowStride = (ColCount + 1) / 2; |
| 119 | + var directionsSize = RowCount * directionsRowStride; |
| 120 | + |
| 121 | + var targetDirections = new NativeArray<byte>(directionsSize, Allocator.Temp); |
| 122 | + var sourceDirections= new NativeArray<byte>(directionsSize, Allocator.Temp); |
| 123 | + |
| 124 | + // For testing purposes, recalculate paths every time. |
| 125 | + CartesianGridOnPlaneShortestPath.CalculateShortestPathsToTarget(targetDirections, RowCount, ColCount, targetPosition, Walls); |
| 126 | + CartesianGridOnPlaneShortestPath.CalculateShortestPathsToTarget(sourceDirections, RowCount, ColCount, sourcePosition, Walls); |
| 127 | + |
| 128 | + // Test distance form source->target is same as target->source |
| 129 | + var sourceToTargetDistance = WalkPath(sourcePosition, targetDirections, 0); |
| 130 | + var targetToSourceDistance = WalkPath(targetPosition, sourceDirections, 0); |
| 131 | + Assert.AreEqual(sourceToTargetDistance, targetToSourceDistance); |
| 132 | + |
| 133 | + var expectedDistance = sourceToTargetDistance; |
| 134 | + |
| 135 | + // Sample path variations (not exhaustive, always follow the variation path option) |
| 136 | + for (int i = 1; i < 4; i++) |
| 137 | + { |
| 138 | + // Test distance form source->target is same as target->source |
| 139 | + // Test distance is same for all variations |
| 140 | + sourceToTargetDistance = WalkPath(sourcePosition, targetDirections, i); |
| 141 | + Assert.AreEqual(expectedDistance, sourceToTargetDistance); |
| 142 | + targetToSourceDistance = WalkPath(targetPosition, sourceDirections, i); |
| 143 | + Assert.AreEqual(expectedDistance, targetToSourceDistance); |
| 144 | + } |
| 145 | + |
| 146 | + targetDirections.Dispose(); |
| 147 | + sourceDirections.Dispose(); |
| 148 | + |
| 149 | + return expectedDistance; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + [Test] |
| 154 | + public void PathOnEmptyGrid() |
| 155 | + { |
| 156 | + using (var grid = new TestGrid(16, 16)) |
| 157 | + { |
| 158 | + var targetPosition = new CartesianGridCoordinates {x = 15, y = 15}; |
| 159 | + var dist = 0; |
| 160 | + |
| 161 | + dist = grid.WalkPathDistance(new CartesianGridCoordinates {x = 0, y = 0}, targetPosition); |
| 162 | + Assert.AreEqual(dist, 30 ); |
| 163 | + dist = grid.WalkPathDistance(new CartesianGridCoordinates {x = 8, y = 7}, targetPosition); |
| 164 | + Assert.AreEqual(dist, 15 ); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + [Test] |
| 169 | + public void PathOnIsland() |
| 170 | + { |
| 171 | + using (var grid = new TestGrid(16, 16)) |
| 172 | + { |
| 173 | + // create island left/right side |
| 174 | + for (int y = 0; y < grid.RowCount; y++) |
| 175 | + grid.AddWallWest(new CartesianGridCoordinates {x = 8, y = (short)y}); |
| 176 | + |
| 177 | + var targetPosition = new CartesianGridCoordinates {x = 15, y = 15}; |
| 178 | + var dist = 0; |
| 179 | + |
| 180 | + // right side has open path |
| 181 | + dist = grid.WalkPathDistance(new CartesianGridCoordinates {x = 8, y = 8}, targetPosition); |
| 182 | + Assert.AreEqual(dist, 14); |
| 183 | + dist = grid.WalkPathDistance(new CartesianGridCoordinates {x = 8, y = 0}, targetPosition); |
| 184 | + Assert.AreEqual(dist, 22); |
| 185 | + |
| 186 | + // left side blocked |
| 187 | + dist = grid.WalkPathDistance(new CartesianGridCoordinates {x = 0, y = 8}, targetPosition); |
| 188 | + Assert.AreEqual(dist, 0); |
| 189 | + dist = grid.WalkPathDistance(new CartesianGridCoordinates {x = 0, y = 0}, targetPosition); |
| 190 | + Assert.AreEqual(dist, 0); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + [Test] |
| 195 | + public void PathRandomObstacles() |
| 196 | + { |
| 197 | + using (var grid = new TestGrid(32, 15)) |
| 198 | + { |
| 199 | + int obstacleCount = 32; |
| 200 | + var rand = new Unity.Mathematics.Random(0xF545AA3F); |
| 201 | + for (int i = 0; i < obstacleCount; i++) |
| 202 | + { |
| 203 | + var xy = rand.NextInt2(new int2(grid.ColCount, grid.RowCount)); |
| 204 | + if (rand.NextBool()) |
| 205 | + grid.AddWallWest(new CartesianGridCoordinates {x = (short)xy.x, y = (short)xy.y}); |
| 206 | + else |
| 207 | + grid.AddWallSouth(new CartesianGridCoordinates {x = (short)xy.x, y = (short)xy.y}); |
| 208 | + } |
| 209 | + |
| 210 | + int testCount = 64; |
| 211 | + for (int i = 0; i < testCount; i++) |
| 212 | + { |
| 213 | + var sourceXY = rand.NextInt2(new int2(grid.ColCount, grid.RowCount)); |
| 214 | + var targetXY = rand.NextInt2(new int2(grid.ColCount, grid.RowCount)); |
| 215 | + var sourcePosition = new CartesianGridCoordinates {x = (short)sourceXY.x, y = (short)sourceXY.y}; |
| 216 | + var targetPosition = new CartesianGridCoordinates {x = (short)targetXY.x, y = (short)targetXY.y}; |
| 217 | + grid.WalkPathDistance(sourcePosition, targetPosition); |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments