From c9b3cda2126cfbfb8544766adcfae7f81ffea219 Mon Sep 17 00:00:00 2001 From: Josef Wittmann Date: Thu, 6 Oct 2022 22:52:00 +0200 Subject: [PATCH 1/6] Fix range for `findClosestByRange` --- src/game/rooms.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/game/rooms.js b/src/game/rooms.js index f2bac2e1..dba4452a 100644 --- a/src/game/rooms.js +++ b/src/game/rooms.js @@ -305,6 +305,8 @@ function _findClosestByPath2(fromPos, objects, opts) { opts = opts || {}; + const range = opts.range || 0; + if(_.isNumber(objects)) { objects = register.rooms[fromPos.roomName].find(objects, {filter: opts.filter}); } @@ -325,7 +327,7 @@ function _findClosestByPath2(fromPos, objects, opts) { if(i.pos) { i = i.pos; } - return {range: 1, pos: i}; + return {range, pos: i}; }); if(opts.avoid) { @@ -365,7 +367,7 @@ function _findClosestByPath2(fromPos, objects, opts) { } objects.forEach(obj => { - if(lastPos.isNearTo(obj)) { + if(lastPos.inRangeTo(obj, range)) { result = obj; } }); From 5944b0fe0fb1f47c84eea6c80983d15c6facbffc Mon Sep 17 00:00:00 2001 From: Josef Wittmann Date: Fri, 7 Oct 2022 11:37:49 +0200 Subject: [PATCH 2/6] Add tests for `findClosestByRange` --- spec/engine/game/roomsSpec.js | 103 +++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 2 deletions(-) diff --git a/spec/engine/game/roomsSpec.js b/spec/engine/game/roomsSpec.js index 866d552b..69776c36 100644 --- a/spec/engine/game/roomsSpec.js +++ b/spec/engine/game/roomsSpec.js @@ -6,18 +6,25 @@ const _ =require('lodash'), describe('rooms', () => { describe('RoomPosition', () => { - let globals = {}; + let globals; beforeEach(()=>{ + globals = {} const runtimeData = { staticTerrainData: require('../../helpers/mocks/rooms').terrain }; const register = { - wrapFn: function(fn) { return fn } + wrapFn: function(fn) { return fn }, + rooms: { E2S7: {} }, + _useNewPathFinder: true, }; rooms.make(runtimeData, {}, register, globals); rooms.makePos(register); + + for (var i in runtimeData.rooms) { + register.rooms[i] = new globals.Room(i); + } }); it('Exists',()=>{ @@ -54,5 +61,97 @@ describe('rooms', () => { expect(pos.y).toBe(15); expect(pos.roomName).toBe('E2S7'); }); + + describe("findClosestByPath", () => { + it("Finds target according to PathFinder", () => { + const newPos = (x) => new globals.RoomPosition(x, 0, "E2S7"); + + const pos = newPos(0); + const closeTarget = newPos(5); + const farTarget = newPos(10); + + globals.PathFinder = { + search: () => ({ + // Only last position of a path is considered in search + path: [closeTarget], + }) + } + + const result = pos.findClosestByPath([ + closeTarget, + farTarget, + ]); + + expect(result).toEqual(closeTarget); + }); + + it("Finds target with range option", () => { + const range = 2; + const targetX = 5; + const newPos = (x) => new globals.RoomPosition(x, 0, "E2S7"); + + const pos = newPos(0); + const target = newPos(targetX); + const pathEnd = newPos(targetX - range); + + globals.PathFinder = { + search: () => ({ + // Only last position of a path is considered in search + path: [pathEnd], + }) + } + const searchSpy = spyOn(globals.PathFinder, 'search').and.callThrough() + + const result = pos.findClosestByPath([target], { range }); + + expect(result).toEqual(target); + expect(searchSpy.calls.count()).toBe(1) + expect(searchSpy.calls.argsFor(0)[0]).toEqual(pos) + expect(searchSpy.calls.argsFor(0)[1].length).toBe(1) + expect(searchSpy.calls.argsFor(0)[1]).toContain({ range, pos: target }) + }) + + it("Fails to find target out of range", () => { + const range = 2; + const targetX = 5; + const newPos = (x) => new globals.RoomPosition(x, 0, "E2S7"); + + const pos = newPos(0); + const target = newPos(targetX); + const pathEnd = newPos(targetX - range - 1); + + globals.PathFinder = { + search: () => ({ + // Only last position of a path is considered in search + path: [pathEnd], + }) + } + + const result = pos.findClosestByPath([target], { range }); + + expect(result).toEqual(null); + }) + + it("Picks first target in range of the path even if it's further away", () => { + const range = 2; + const newPos = (x) => new globals.RoomPosition(x, 0, "E2S7"); + + const pos = newPos(0); + const targetOut = newPos(3) + const targetFar = newPos(2); + const targetNear = newPos(1); + + globals.PathFinder = { + search: () => ({ + // Only last position of a path is considered in search + path: [], + }) + } + + const result = pos.findClosestByPath([targetOut, targetFar, targetNear], { range }); + + expect(result).toEqual(targetFar); + }) + }); }); }); \ No newline at end of file From 683a5d4b7d1bb4671c44c7a50dcfdd1e96d5b8bc Mon Sep 17 00:00:00 2001 From: Josef Wittmann Date: Fri, 7 Oct 2022 11:40:35 +0200 Subject: [PATCH 3/6] Fix indentation --- src/game/rooms.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/game/rooms.js b/src/game/rooms.js index dba4452a..dffc1649 100644 --- a/src/game/rooms.js +++ b/src/game/rooms.js @@ -305,7 +305,7 @@ function _findClosestByPath2(fromPos, objects, opts) { opts = opts || {}; - const range = opts.range || 0; + const range = opts.range || 0; if(_.isNumber(objects)) { objects = register.rooms[fromPos.roomName].find(objects, {filter: opts.filter}); From 88aba87e43c547cdd6116e5cb07f61c2eed3ad7c Mon Sep 17 00:00:00 2001 From: Josef Wittmann Date: Fri, 7 Oct 2022 11:44:13 +0200 Subject: [PATCH 4/6] Reset default `range` to `1` --- src/game/rooms.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/game/rooms.js b/src/game/rooms.js index dffc1649..d7cd3db6 100644 --- a/src/game/rooms.js +++ b/src/game/rooms.js @@ -305,7 +305,7 @@ function _findClosestByPath2(fromPos, objects, opts) { opts = opts || {}; - const range = opts.range || 0; + const range = opts.range === undefined ? 1 : opts.range; if(_.isNumber(objects)) { objects = register.rooms[fromPos.roomName].find(objects, {filter: opts.filter}); From ac575310f0b11aa303832084bb4c3a6a2c6d733d Mon Sep 17 00:00:00 2001 From: Josef Wittmann Date: Fri, 7 Oct 2022 11:54:19 +0200 Subject: [PATCH 5/6] Refactor test --- spec/engine/game/roomsSpec.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/spec/engine/game/roomsSpec.js b/spec/engine/game/roomsSpec.js index 69776c36..1dd4a436 100644 --- a/spec/engine/game/roomsSpec.js +++ b/spec/engine/game/roomsSpec.js @@ -63,12 +63,12 @@ describe('rooms', () => { }); describe("findClosestByPath", () => { - it("Finds target according to PathFinder", () => { - const newPos = (x) => new globals.RoomPosition(x, 0, "E2S7"); + const newPos = ({x}) => new globals.RoomPosition(x, 0, "E2S7"); - const pos = newPos(0); - const closeTarget = newPos(5); - const farTarget = newPos(10); + it("Finds target according to PathFinder", () => { + const pos = newPos({ x: 0 }); + const closeTarget = newPos({ x: 5 }); + const farTarget = newPos({ x: 10 }); globals.PathFinder = { search: () => ({ @@ -88,11 +88,10 @@ describe('rooms', () => { it("Finds target with range option", () => { const range = 2; const targetX = 5; - const newPos = (x) => new globals.RoomPosition(x, 0, "E2S7"); - const pos = newPos(0); - const target = newPos(targetX); - const pathEnd = newPos(targetX - range); + const pos = newPos({ x: 0 }); + const target = newPos({ x: targetX }); + const pathEnd = newPos({ x: targetX - range }); globals.PathFinder = { search: () => ({ @@ -114,11 +113,10 @@ describe('rooms', () => { it("Fails to find target out of range", () => { const range = 2; const targetX = 5; - const newPos = (x) => new globals.RoomPosition(x, 0, "E2S7"); - const pos = newPos(0); - const target = newPos(targetX); - const pathEnd = newPos(targetX - range - 1); + const pos = newPos({ x: 0 }); + const target = newPos({ x: targetX }); + const pathEnd = newPos({ x: targetX - range - 1 }); globals.PathFinder = { search: () => ({ @@ -134,12 +132,11 @@ describe('rooms', () => { it("Picks first target in range of the path even if it's further away", () => { const range = 2; - const newPos = (x) => new globals.RoomPosition(x, 0, "E2S7"); - const pos = newPos(0); - const targetOut = newPos(3) - const targetFar = newPos(2); - const targetNear = newPos(1); + const pos = newPos({ x: 0 }); + const targetOut = newPos({ x: 3 }); + const targetFar = newPos({ x: 2 }); + const targetNear = newPos({ x: 1 }); globals.PathFinder = { search: () => ({ From 978e8d6ded97b9bf0319aef9cdca4f9cf558caae Mon Sep 17 00:00:00 2001 From: Josef Wittmann Date: Fri, 7 Oct 2022 12:01:30 +0200 Subject: [PATCH 6/6] Refactor test --- spec/engine/game/roomsSpec.js | 40 ++++++++++++++--------------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/spec/engine/game/roomsSpec.js b/spec/engine/game/roomsSpec.js index 1dd4a436..749d77ed 100644 --- a/spec/engine/game/roomsSpec.js +++ b/spec/engine/game/roomsSpec.js @@ -65,17 +65,24 @@ describe('rooms', () => { describe("findClosestByPath", () => { const newPos = ({x}) => new globals.RoomPosition(x, 0, "E2S7"); + /** + * In context of `RoomPosition.findClosestByPath` `path` only has to include the last position. + */ + const mockPathFinderSearch = (path) => { + globals.PathFinder = { + search: () => ({ + // Only the last position of a path is considered in search + path, + }) + } + } + it("Finds target according to PathFinder", () => { const pos = newPos({ x: 0 }); const closeTarget = newPos({ x: 5 }); const farTarget = newPos({ x: 10 }); - globals.PathFinder = { - search: () => ({ - // Only last position of a path is considered in search - path: [closeTarget], - }) - } + mockPathFinderSearch([closeTarget]) const result = pos.findClosestByPath([ closeTarget, @@ -93,12 +100,7 @@ describe('rooms', () => { const target = newPos({ x: targetX }); const pathEnd = newPos({ x: targetX - range }); - globals.PathFinder = { - search: () => ({ - // Only last position of a path is considered in search - path: [pathEnd], - }) - } + mockPathFinderSearch([pathEnd]) const searchSpy = spyOn(globals.PathFinder, 'search').and.callThrough() const result = pos.findClosestByPath([target], { range }); @@ -118,12 +120,7 @@ describe('rooms', () => { const target = newPos({ x: targetX }); const pathEnd = newPos({ x: targetX - range - 1 }); - globals.PathFinder = { - search: () => ({ - // Only last position of a path is considered in search - path: [pathEnd], - }) - } + mockPathFinderSearch([pathEnd]) const result = pos.findClosestByPath([target], { range }); @@ -138,12 +135,7 @@ describe('rooms', () => { const targetFar = newPos({ x: 2 }); const targetNear = newPos({ x: 1 }); - globals.PathFinder = { - search: () => ({ - // Only last position of a path is considered in search - path: [], - }) - } + mockPathFinderSearch([]) const result = pos.findClosestByPath([targetOut, targetFar, targetNear], { range });