From 4a4cefbc1ca837e8cee652f27fed09437b1f6e1c Mon Sep 17 00:00:00 2001 From: Shaun Kichenbrand Date: Thu, 2 Jan 2020 07:04:47 +0200 Subject: [PATCH] fixes #1331 --- src/server/world/map.js | 28 ++----------------- src/server/world/map/canPathFromPos.js | 37 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 26 deletions(-) create mode 100644 src/server/world/map/canPathFromPos.js diff --git a/src/server/world/map.js b/src/server/world/map.js index cf95d545..8561275e 100644 --- a/src/server/world/map.js +++ b/src/server/world/map.js @@ -7,6 +7,7 @@ let randomMap = require('./randomMap'); let events = require('../misc/events'); const mapObjects = require('./map/mapObjects'); +const canPathFromPos = require('./map/canPathFromPos'); let mapFile = null; let mapScale = null; @@ -441,31 +442,6 @@ module.exports = { //Find if any spawns can path to a position. This is important for when maps change and players // log in on tiles that aren't blocking but not able to reach anywhere useful canPathFromPos: function (pos) { - const fnCanPath = positions => { - return positions.some(p => { - const path = physics.getPath(pos, p); - //Are we on the position? - if (!path.length) - return true; - - const { x, y } = path[path.length - 1]; - //Can we reach the position? - const isFullPath = (p.x === x && p.y === y); - if (isFullPath) - return true; - - return false; - }); - }; - - const canPathToSpawn = fnCanPath(this.spawn); - - if (canPathToSpawn) - return true; - - const portals = objects.objects.filter(o => o.portal); - const canPathToPortal = fnCanPath(portals); - - return canPathToPortal; + return canPathFromPos(this, pos); } }; diff --git a/src/server/world/map/canPathFromPos.js b/src/server/world/map/canPathFromPos.js new file mode 100644 index 00000000..e5ecf97b --- /dev/null +++ b/src/server/world/map/canPathFromPos.js @@ -0,0 +1,37 @@ +let objects = require('../../objects/objects'); +let physics = require('../physics'); + +const canPath = (pos, positions, maxDistance = 0) => { + return positions.some(p => { + const path = physics.getPath(pos, p); + //Are we on the position? + if (!path.length) + return true; + + const { x, y } = path[path.length - 1]; + //Can we get close enough to the position? + const isCloseEnough = Math.max(Math.abs(p.x - x), Math.abs(p.y - y)) <= maxDistance; + if (isCloseEnough) + return true; + + return false; + }); +}; + +module.exports = (map, pos) => { + const canPathToSpawn = canPath(pos, map.spawn); + + if (canPathToSpawn) + return true; + + const portals = objects.objects.filter(o => o.portal); + const canPathToPortal = canPath(pos, portals); + + if (canPathToPortal) + return true; + + const doors = objects.objects.filter(o => o.door); + const canPathToDoor = canPath(pos, doors, 1); + + return canPathToDoor; +};