optimize canMoveDirectly

This commit is contained in:
oc 2019-06-28 23:41:24 +08:00
parent 43c12e55f1
commit 42d8a4df8f
4 changed files with 60 additions and 22 deletions

View File

@ -751,25 +751,28 @@ control.prototype.turnHero = function(direction) {
} }
////// 瞬间移动 ////// ////// 瞬间移动 //////
control.prototype.moveDirectly = function (destX, destY) { control.prototype.moveDirectly = function (destX, destY, ignoreSteps) {
return this.controldata.moveDirectly(destX, destY); return this.controldata.moveDirectly(destX, destY, ignoreSteps);
} }
////// 尝试瞬间移动 ////// ////// 尝试瞬间移动 //////
control.prototype.tryMoveDirectly = function (destX, destY) { control.prototype.tryMoveDirectly = function (destX, destY) {
if (this.nearHero(destX, destY)) return false; if (this.nearHero(destX, destY)) return false;
var canMoveArray = core.maps.generateMovableArray(); var canMoveArray = core.maps.generateMovableArray();
var testMove = function (dx, dy, dir) { var dirs = [[destX,destY],[destX-1,destY,"right"],[destX,destY-1,"down"],[destX,destY+1,"up"],[destX+1,destY,"left"]];
if (dx<0 || dx>=core.bigmap.width|| dy<0 || dy>=core.bigmap.height) return false; var canMoveDirectlyArray = core.canMoveDirectlyArray(dirs);
if (dir && !core.inArray(canMoveArray[dx][dy],dir)) return false;
if (core.control.moveDirectly(dx, dy)) { 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() {}); if (dir) core.moveHero(dir, function() {});
return true; return true;
} }
return false;
} }
return testMove(destX,destY) || testMove(destX-1, destY, "right") || testMove(destX,destY-1,"down") return false;
|| testMove(destX,destY+1,"up") || testMove(destX+1,destY,"left");
} }
////// 绘制勇士 ////// ////// 绘制勇士 //////

View File

@ -486,14 +486,39 @@ maps.prototype._canMoveHero_checkCannotInOut = function (number, name, direction
////// 能否瞬间移动 ////// ////// 能否瞬间移动 //////
maps.prototype.canMoveDirectly = function (destX, destY) { 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'); var fromX = core.getHeroLoc('x'), fromY = core.getHeroLoc('y');
if (fromX == destX && fromY == destY) return 0; if (!this._canMoveDirectly_checkGlobal()) {
// 检查起点事件 for (var i = 0; i < number; ++i) ans.push(-1);
if (!this._canMoveDirectly_checkStartPoint(fromX, fromY)) return -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 () { maps.prototype._canMoveDirectly_checkGlobal = function () {
@ -519,7 +544,7 @@ maps.prototype._canMoveDirectly_checkStartPoint = function (sx, sy) {
return true; 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 canMoveArray = this.generateMovableArray();
var blocksObj = this.getMapBlocksObj(core.status.floorId); 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 (bgMap[ny][nx] == 167) continue;
if (!this._canMoveDirectly_checkNextPoint(blocksObj, nx, ny)) continue; if (!this._canMoveDirectly_checkNextPoint(blocksObj, nx, ny)) continue;
visited[nindex] = visited[now] + 1; 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); 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) { maps.prototype._canMoveDirectly_checkNextPoint = function (blocksObj, x, y) {

View File

@ -1086,7 +1086,7 @@ utils.prototype._export = function (floorIds) {
// map // map
var content = floorIds.length + "\n" + core.__SIZE__ + " " + core.__SIZE__ + "\n\n"; var content = floorIds.length + "\n" + core.__SIZE__ + " " + core.__SIZE__ + "\n\n";
floorIds.forEach(function (floorId) { 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) { content += arr.map(function (x) {
// check monster // check monster
x.forEach(function (t) { x.forEach(function (t) {
@ -1102,7 +1102,7 @@ utils.prototype._export = function (floorIds) {
// values // values
content += ["redJewel", "blueJewel", "greenJewel", "redPotion", "bluePotion", content += ["redJewel", "blueJewel", "greenJewel", "redPotion", "bluePotion",
"yellowPotion", "greenPotion", "sword1", "shield1"].map(function (x) { "yellowPotion", "greenPotion", "sword1", "shield1"].map(function (x) {
return core.values[x] return core.values[x] || 0;
}).join(" ") + "\n\n"; }).join(" ") + "\n\n";
// monster // monster

View File

@ -1290,12 +1290,12 @@ var functions_d6ad677b_427a_4623_b50f_a445a3b0ef8a =
core.updateStatusBar(); core.updateStatusBar();
}, },
"moveDirectly": function (x, y) { "moveDirectly": function (x, y, ignoreSteps) {
// 瞬间移动x,y为要瞬间移动的点 // 瞬间移动x,y为要瞬间移动的点ignoreSteps为减少的步数可能之前已经被计算过
// 返回true代表成功瞬移false代表没有成功瞬移 // 返回true代表成功瞬移false代表没有成功瞬移
// 判定能否瞬移到该点 // 判定能否瞬移到该点
var ignoreSteps = core.canMoveDirectly(x, y); if (ignoreSteps == null) ignoreSteps = core.canMoveDirectly(x, y);
if (ignoreSteps >= 0) { if (ignoreSteps >= 0) {
core.clearMap('hero'); core.clearMap('hero');
// 获得勇士最后的朝向 // 获得勇士最后的朝向