UNPKG

malwoden

Version:

![alt text](./coverage/badge-lines.svg) ![alt text](./coverage/badge-statements.svg) ![alt text](./coverage/badge-functions.svg) ![alt text](./coverage/badge-branches.svg)

356 lines 10 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Calc = require("../calc"); var precise_1 = require("./precise"); describe("Shadowcasting", function () { it("Will set returnAll to false by default", function () { var a = new precise_1.PreciseShadowcasting({ lightPasses: function () { return true; }, topology: "four", returnAll: true, }); var b = new precise_1.PreciseShadowcasting({ lightPasses: function () { return true; }, topology: "four", }); expect(a["returnAll"]).toBeTruthy(); expect(b["returnAll"]).toBeFalsy(); }); it("Will return all tiles if returnAll is set", function () { var a = new precise_1.PreciseShadowcasting({ lightPasses: function () { return false; }, topology: "four", returnAll: true, }); var results = a.calculateArray({ x: 0, y: 0 }, 2); expect(results).toHaveLength(13); for (var _i = 0, results_1 = results; _i < results_1.length; _i++) { var r = results_1[_i]; if (r.r === 0 || r.r === 1) { expect(r.visibility).toEqual(1); } else { expect(r.visibility).toEqual(0); } } }); it("will calculate for empty tiles", function () { var s = new precise_1.PreciseShadowcasting({ lightPasses: function () { return true; }, topology: "four", }); var tiles = s.calculateArray({ x: 1, y: 1 }, 0); expect(tiles).toEqual([ { pos: { x: 1, y: 1, }, r: 0, visibility: 1, }, ]); var tiles2 = s.calculateArray({ x: 0, y: 0 }, 1); expect(tiles2).toHaveLength(5); expect(tiles2).toEqual([ { pos: { x: 0, y: 0 }, visibility: 1, r: 0, }, { pos: { x: 1, y: 0 }, visibility: 1, r: 1, }, { pos: { x: 0, y: -1 }, visibility: 1, r: 1, }, { pos: { x: -1, y: 0 }, visibility: 1, r: 1, }, { pos: { x: 0, y: 1 }, visibility: 1, r: 1, }, ]); }); // ToDo - Currently doesn't return if not visible at all? // Keep this or not? it("will calculate with blocked tiles", function () { var s = new precise_1.PreciseShadowcasting({ lightPasses: function (pos) { return (pos.x == 1 && pos.y == 0) === false; }, topology: "eight", }); var spaces = s.calculateArray({ x: 0, y: 0 }, 2); expect(spaces).toEqual([ // Ring 0 { r: 0, visibility: 1, pos: { x: 0, y: 0, }, }, // Ring 1 { r: 1, visibility: 1, pos: { x: 1, y: 0, }, }, { r: 1, visibility: 1, pos: { x: 1, y: -1, }, }, { r: 1, visibility: 1, pos: { x: 0, y: -1, }, }, { r: 1, visibility: 1, pos: { x: -1, y: -1, }, }, { r: 1, visibility: 1, pos: { x: -1, y: 0, }, }, { r: 1, visibility: 1, pos: { x: -1, y: 1, }, }, { r: 1, visibility: 1, pos: { x: 0, y: 1, }, }, { r: 1, visibility: 1, pos: { x: 1, y: 1, }, }, // Ring 2 // { Doesn't return since not visible // r: 2, // visibility: 0, // pos: { // x: 2, // y: 0, // } // }, { r: 2, visibility: 0.5, pos: { x: 2, y: -1, }, }, { r: 2, visibility: 1, pos: { x: 2, y: -2, }, }, { r: 2, visibility: 1, pos: { x: 1, y: -2, }, }, { r: 2, visibility: 1, pos: { x: 0, y: -2, }, }, { r: 2, visibility: 1, pos: { x: -1, y: -2, }, }, { r: 2, visibility: 1, pos: { x: -2, y: -2, }, }, { r: 2, visibility: 1, pos: { x: -2, y: -1, }, }, { r: 2, visibility: 1, pos: { x: -2, y: 0, }, }, { r: 2, visibility: 1, pos: { x: -2, y: 1, }, }, { r: 2, visibility: 1, pos: { x: -2, y: 2, }, }, { r: 2, visibility: 1, pos: { x: -1, y: 2, }, }, { r: 2, visibility: 1, pos: { x: 0, y: 2, }, }, { r: 2, visibility: 1, pos: { x: 1, y: 2, }, }, { r: 2, visibility: 1, pos: { x: 2, y: 2, }, }, { r: 2, visibility: 0.5, pos: { x: 2, y: 1, }, }, ]); }); it("Will show immediately adjacent even if no light passes", function () { var s = new precise_1.PreciseShadowcasting({ lightPasses: function () { return false; }, topology: "four", }); expect(s.calculateArray({ x: 0, y: 0 }, 2)).toEqual([ { r: 0, visibility: 1, pos: { x: 0, y: 0 } }, { r: 1, visibility: 1, pos: { x: 1, y: 0 } }, { r: 1, visibility: 1, pos: { x: 0, y: -1 } }, { r: 1, visibility: 1, pos: { x: -1, y: 0 } }, { r: 1, visibility: 1, pos: { x: 0, y: 1 } }, ]); }); it("Won't merge ranges if the checked block is non-blocking", function () { var s = new precise_1.PreciseShadowcasting({ lightPasses: function () { return false; }, topology: "four", }); expect(s["checkVisibility"]([5, 8], [7, 8], false, [ [0, 1], [5, 8], [7, 8], [8, 8], ])).toEqual(1); }); it("Can calculate if a shadow overlaps", function () { var s = new precise_1.PreciseShadowcasting({ lightPasses: function (pos) { return !Calc.Vector.areEqual(pos, { x: 1, y: 0 }) && !Calc.Vector.areEqual(pos, { x: 1, y: -1 }) && !Calc.Vector.areEqual(pos, { x: 1, y: 1 }) && !Calc.Vector.areEqual(pos, { x: 2, y: 0 }); }, topology: "eight", }); var spaces = s.calculateArray({ x: 0, y: 0 }, 2); var blocked_a = spaces.find(function (s) { return Calc.Vector.areEqual(s.pos, { x: 1, y: 0 }); }); var blocked_b = spaces.find(function (s) { return Calc.Vector.areEqual(s.pos, { x: 2, y: 0 }); }); expect(blocked_a).toEqual({ r: 1, visibility: 1, pos: { x: 1, y: 0 }, }); expect(blocked_b).toBeUndefined(); }); it("Can calculate if a shadow is equivalent", function () { var s = new precise_1.PreciseShadowcasting({ lightPasses: function () { return true; }, topology: "eight", }); expect(s["checkShadowVisibility"](5, 5, true, false)).toEqual(false); expect(s["checkShadowVisibility"](6, 7, true, true)).toEqual(false); expect(s["checkShadowVisibility"](7, 6, true, false)).toEqual(false); }); }); //# sourceMappingURL=precise.spec.js.map