From 42d8a4df8fab4f079ed4b93ab7d3cfdede0cbb92 Mon Sep 17 00:00:00 2001 From: oc Date: Fri, 28 Jun 2019 23:41:24 +0800 Subject: [PATCH] optimize canMoveDirectly --- libs/control.js | 21 ++++++++++-------- libs/maps.js | 51 +++++++++++++++++++++++++++++++++++++------- libs/utils.js | 4 ++-- project/functions.js | 6 +++--- 4 files changed, 60 insertions(+), 22 deletions(-) diff --git a/libs/control.js b/libs/control.js index 7476d52c..67f930f1 100644 --- a/libs/control.js +++ b/libs/control.js @@ -751,25 +751,28 @@ control.prototype.turnHero = function(direction) { } ////// 瞬间移动 ////// -control.prototype.moveDirectly = function (destX, destY) { - return this.controldata.moveDirectly(destX, destY); +control.prototype.moveDirectly = function (destX, destY, ignoreSteps) { + return this.controldata.moveDirectly(destX, destY, ignoreSteps); } ////// 尝试瞬间移动 ////// control.prototype.tryMoveDirectly = function (destX, destY) { if (this.nearHero(destX, destY)) return false; var canMoveArray = core.maps.generateMovableArray(); - var testMove = function (dx, dy, dir) { - if (dx<0 || dx>=core.bigmap.width|| dy<0 || dy>=core.bigmap.height) return false; - if (dir && !core.inArray(canMoveArray[dx][dy],dir)) return false; - if (core.control.moveDirectly(dx, dy)) { + var dirs = [[destX,destY],[destX-1,destY,"right"],[destX,destY-1,"down"],[destX,destY+1,"up"],[destX+1,destY,"left"]]; + var canMoveDirectlyArray = core.canMoveDirectlyArray(dirs); + + for (var i = 0; i < dirs.length; ++i) { + var d = dirs[i], dx = d[0], dy = d[1], dir = d[2]; + if (dx<0 || dx>=core.bigmap.width|| dy<0 || dy>=core.bigmap.height) continue; + if (dir && !core.inArray(canMoveArray[dx][dy],dir)) continue; + if (canMoveDirectlyArray[i]<0) continue; + if (core.control.moveDirectly(dx, dy, canMoveDirectlyArray[i])) { if (dir) core.moveHero(dir, function() {}); return true; } - return false; } - return testMove(destX,destY) || testMove(destX-1, destY, "right") || testMove(destX,destY-1,"down") - || testMove(destX,destY+1,"up") || testMove(destX+1,destY,"left"); + return false; } ////// 绘制勇士 ////// diff --git a/libs/maps.js b/libs/maps.js index e1ad055f..9d4e2298 100644 --- a/libs/maps.js +++ b/libs/maps.js @@ -486,14 +486,39 @@ maps.prototype._canMoveHero_checkCannotInOut = function (number, name, direction ////// 能否瞬间移动 ////// maps.prototype.canMoveDirectly = function (destX, destY) { - if (!this._canMoveDirectly_checkGlobal()) return -1; + return this.canMoveDirectlyArray([[destX,destY]])[0]; +} + +maps.prototype.canMoveDirectlyArray = function (locs) { + var ans = [], number = locs.length; var fromX = core.getHeroLoc('x'), fromY = core.getHeroLoc('y'); - if (fromX == destX && fromY == destY) return 0; - // 检查起点事件 - if (!this._canMoveDirectly_checkStartPoint(fromX, fromY)) return -1; + if (!this._canMoveDirectly_checkGlobal()) { + for (var i = 0; i < number; ++i) ans.push(-1); + return ans; + } + for (var i = 0; i < number; ++i) { + if (locs[i][0] == fromX && locs[i][1] == fromY) { + ans.push(0); + number--; + } + else if (locs[i][0] < 0 || locs[i][0] >= core.bigmap.width || locs[i][1] < 0 || locs[i][1] >= core.bigmap.height) { + ans.push(-1); + number--; + } + else ans.push(null); + } + if (number == 0) return ans; - return this._canMoveDirectly_bfs(fromX, fromY, destX, destY); + // 检查起点事件 + if (!this._canMoveDirectly_checkStartPoint(fromX, fromY)) { + for (var i in ans) { + if (ans[i] == null) ans[i] = -1; + } + return ans; + } + + return this._canMoveDirectly_bfs(fromX, fromY, locs, number, ans); } maps.prototype._canMoveDirectly_checkGlobal = function () { @@ -519,7 +544,7 @@ maps.prototype._canMoveDirectly_checkStartPoint = function (sx, sy) { return true; } -maps.prototype._canMoveDirectly_bfs = function (sx, sy, ex, ey) { +maps.prototype._canMoveDirectly_bfs = function (sx, sy, locs, number, ans) { var canMoveArray = this.generateMovableArray(); var blocksObj = this.getMapBlocksObj(core.status.floorId); // 滑冰 @@ -538,12 +563,22 @@ maps.prototype._canMoveDirectly_bfs = function (sx, sy, ex, ey) { if (bgMap[ny][nx] == 167) continue; if (!this._canMoveDirectly_checkNextPoint(blocksObj, nx, ny)) continue; visited[nindex] = visited[now] + 1; - if (nx == ex && ny == ey) return visited[nindex]; + // if (nx == ex && ny == ey) return visited[nindex]; + for (var i in ans) { + if (locs[i][0] == nx && locs[i][1] == ny && ans[i] == null) { + ans[i] = visited[nindex]; + number--; + if (number == 0) return ans; + } + } queue.push(nindex); } } - return -1; + for (var i in ans) { + if (ans[i] == null) ans[i] = -1; + } + return ans; } maps.prototype._canMoveDirectly_checkNextPoint = function (blocksObj, x, y) { diff --git a/libs/utils.js b/libs/utils.js index 4378dca5..daea03ea 100644 --- a/libs/utils.js +++ b/libs/utils.js @@ -1086,7 +1086,7 @@ utils.prototype._export = function (floorIds) { // map var content = floorIds.length + "\n" + core.__SIZE__ + " " + core.__SIZE__ + "\n\n"; floorIds.forEach(function (floorId) { - var arr = core.maps._getMapArrayFromBlocks(core.status.maps[floorId].blocks); + var arr = core.maps._getMapArrayFromBlocks(core.status.maps[floorId].blocks, core.__SIZE__, core.__SIZE__); content += arr.map(function (x) { // check monster x.forEach(function (t) { @@ -1102,7 +1102,7 @@ utils.prototype._export = function (floorIds) { // values content += ["redJewel", "blueJewel", "greenJewel", "redPotion", "bluePotion", "yellowPotion", "greenPotion", "sword1", "shield1"].map(function (x) { - return core.values[x] + return core.values[x] || 0; }).join(" ") + "\n\n"; // monster diff --git a/project/functions.js b/project/functions.js index 79b2fa9d..65b5e6f0 100644 --- a/project/functions.js +++ b/project/functions.js @@ -1290,12 +1290,12 @@ var functions_d6ad677b_427a_4623_b50f_a445a3b0ef8a = core.updateStatusBar(); }, - "moveDirectly": function (x, y) { - // 瞬间移动;x,y为要瞬间移动的点 + "moveDirectly": function (x, y, ignoreSteps) { + // 瞬间移动;x,y为要瞬间移动的点;ignoreSteps为减少的步数,可能之前已经被计算过 // 返回true代表成功瞬移,false代表没有成功瞬移 // 判定能否瞬移到该点 - var ignoreSteps = core.canMoveDirectly(x, y); + if (ignoreSteps == null) ignoreSteps = core.canMoveDirectly(x, y); if (ignoreSteps >= 0) { core.clearMap('hero'); // 获得勇士最后的朝向