optimize canMoveDirectly
This commit is contained in:
parent
43c12e55f1
commit
42d8a4df8f
@ -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;
|
||||
}
|
||||
|
||||
////// 绘制勇士 //////
|
||||
|
||||
51
libs/maps.js
51
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) {
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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');
|
||||
// 获得勇士最后的朝向
|
||||
|
||||
Loading…
Reference in New Issue
Block a user