module TK.SpaceTac.Specs { function checkLocation(check: TestContext, got: IArenaLocation, expected_x: number, expected_y: number) { check.equals(got.x, expected_x, `x differs (${got.x},${got.y}) (${expected_x},${expected_y})`); check.equals(got.y, expected_y, `y differs (${got.x},${got.y}) (${expected_x},${expected_y})`); } testing("ArenaGrid", test => { test.case("checks if a location is in range of another", check => { let grid = new ArenaGrid(); check.in("first order", check => { check.equals(grid.inRange({ x: 0, y: 0 }, { x: 4, y: 4 }, 5), false, "<5"); check.equals(grid.inRange({ x: 0, y: 0 }, { x: 4, y: 4 }, 8), true, "<8"); }); check.in("second order", check => { check.equals(grid.inRange({ x: 4, y: 4 }, { x: 0, y: 0 }, 5), false, "<5"); check.equals(grid.inRange({ x: 4, y: 4 }, { x: 0, y: 0 }, 8), true, "<8"); }); check.equals(grid.inRange({ x: 0, y: 0 }, { x: 0.99999999999999, y: 0 }, 1), true, "0.99999999999999"); check.equals(grid.inRange({ x: 0, y: 0 }, { x: 1.00000000000001, y: 0 }, 1), true, "1.00000000000001"); check.equals(grid.inRange({ x: 0, y: 0 }, { x: 1.000001, y: 0 }, 1), false, "1.000001"); }); }); testing("PixelArenaGrid", test => { test.case("moves to cardinal points", check => { let grid = new PixelArenaGrid(undefined, 3); checkLocation(check, grid.down({ x: 0, y: 0 }), 0, 3); checkLocation(check, grid.down({ x: 7, y: 5 }), 7, 8); checkLocation(check, grid.up({ x: 7, y: 5 }), 7, 2); checkLocation(check, grid.left({ x: 6, y: 2 }), 3, 2); checkLocation(check, grid.right({ x: 3, y: 1 }), 6, 1); }); test.case("iterates around a location", check => { let grid = new PixelArenaGrid(undefined, 5); let result = imaterialize(grid.iterate({ x: 5, y: 5 }, { xmin: 0, xmax: 15, ymin: 0, ymax: 10 })); check.equals(result.length, 12); checkLocation(check, result[0], 5, 5); checkLocation(check, result[1], 10, 5); checkLocation(check, result[2], 15, 5); checkLocation(check, result[3], 0, 5); checkLocation(check, result[4], 5, 10); checkLocation(check, result[5], 10, 10); checkLocation(check, result[6], 15, 10); checkLocation(check, result[7], 0, 10); checkLocation(check, result[8], 5, 0); checkLocation(check, result[9], 10, 0); checkLocation(check, result[10], 15, 0); checkLocation(check, result[11], 0, 0); }); }); testing("HexagonalArenaGrid", test => { test.case("checks coordinates", check => { let grid = new HexagonalArenaGrid(undefined, 5, 1); check.equals(grid.check({ x: 0, y: 0 }), true, "0,0"); check.equals(grid.check({ x: 1, y: 0 }), false, "1,0"); check.equals(grid.check({ x: 5, y: 0 }), true, "5,0"); check.equals(grid.check({ x: 6, y: 0 }), false, "6,0"); check.equals(grid.check({ x: 0, y: 5 }), false, "0,5"); check.equals(grid.check({ x: 2.5, y: 5 }), true, "2.5,5"); check.equals(grid.check({ x: 5, y: 5 }), false, "5,5"); check.equals(grid.check({ x: 7.5, y: 5 }), true, "7.5,5"); }); test.case("snaps coordinates to the nearest grid point, on a biased grid", check => { let grid = new HexagonalArenaGrid(undefined, 4, 0.75); checkLocation(check, grid.snap({ x: 0, y: 0 }), 0, 0); checkLocation(check, grid.snap({ x: 1, y: 0 }), 0, 0); checkLocation(check, grid.snap({ x: 1.9, y: 0 }), 0, 0); checkLocation(check, grid.snap({ x: 2.1, y: 0 }), 4, 0); checkLocation(check, grid.snap({ x: 1, y: 1 }), 0, 0); checkLocation(check, grid.snap({ x: 1, y: 2 }), 2, 3); checkLocation(check, grid.snap({ x: -1, y: -1 }), 0, 0); checkLocation(check, grid.snap({ x: -2, y: -2 }), -2, -3); checkLocation(check, grid.snap({ x: -3, y: -1 }), -4, 0); checkLocation(check, grid.snap({ x: 6, y: -5 }), 8, -6); }); test.case("snaps coordinates to the nearest grid point, on a regular grid", check => { let grid = new HexagonalArenaGrid(undefined, 10); checkLocation(check, grid.snap({ x: 0, y: 0 }), 0, 0); checkLocation(check, grid.snap({ x: 8, y: 0 }), 10, 0); checkLocation(check, grid.snap({ x: 1, y: 6 }), 5, 10 * Math.sqrt(0.75)); }); test.case("iterates around a location", check => { let grid = new HexagonalArenaGrid({ xmin: -10, xmax: 10, ymin: -10, ymax: 10 }, 8, 1); let result = imaterialize(grid.iterate({ x: 0, y: 0 })); check.equals(result.length, 7); checkLocation(check, result[0], 0, 0); checkLocation(check, result[1], 8, 0); checkLocation(check, result[2], -8, 0); checkLocation(check, result[3], 4, 8); checkLocation(check, result[4], -4, 8); checkLocation(check, result[5], 4, -8); checkLocation(check, result[6], -4, -8); }); }); }