Rewrite Automatic Route using PriorityQueue

This commit is contained in:
oc 2018-08-27 00:00:24 +08:00
parent cb95ce4f0b
commit adbcd4e383
3 changed files with 20 additions and 28 deletions

View File

@ -121,6 +121,7 @@
</div>
<script src='libs/thirdparty/mid.min.js'></script>
<script src='libs/thirdparty/lz-string.min.js'></script>
<script src='libs/thirdparty/priority-queue.min.js'></script>
<script id='mainScript' src='main.js'></script>
<script>main.init();main.listen();</script>
</body>

View File

@ -542,9 +542,7 @@ control.prototype.setAutomaticRoute = function (destX, destY, stepPostfix) {
////// 自动寻路算法,找寻最优路径 //////
control.prototype.automaticRoute = function (destX, destY) {
var fw = core.bigmap.width;
var fh = core.bigmap.height;
var total = fw * fh;
var fw = core.bigmap.width, fh = core.bigmap.height;
var startX = core.getHeroLoc('x');
var startY = core.getHeroLoc('y');
var scan = {
@ -553,31 +551,26 @@ control.prototype.automaticRoute = function (destX, destY) {
'down': {'x': 0, 'y': 1},
'right': {'x': 1, 'y': 0}
};
var queue = [];
var nowDeep = 0;
var route = [];
var ans = []
if (destX == startX && destY == startY) return false;
queue.push(startX + fw * startY);
queue.push(-1);
route[startX + fw * startY] = '';
while (queue.length != 1) {
var f = queue.shift();
if (f===-1) {nowDeep+=1;queue.push(-1);continue;}
var deep = parseInt(f/total);
if (deep!==nowDeep) {queue.push(f);continue;}
f=f%total;
var nowX = parseInt(f % fw), nowY = parseInt(f / fw);
var nowIsArrow = false, nowId, nowBlock = core.getBlock(nowX,nowY);
var route = [];
var queue = new PriorityQueue({comparator: function (a,b) {
return a.depth - b.depth;
}});
var ans = [];
route[startX + fw * startY] = '';
queue.queue({depth: 0, x: startX, y: startY});
while (queue.length!=0) {
var curr = queue.dequeue();
var deep = curr.depth, nowX = curr.x, nowY = curr.y;
for (var direction in scan) {
if (!core.canMoveHero(nowX, nowY, direction))
continue;
var nx = nowX + scan[direction].x;
var ny = nowY + scan[direction].y;
if (nx<0 || nx>=fw || ny<0 || ny>=fh) continue;
var nid = nx + fw * ny;
@ -585,21 +578,19 @@ control.prototype.automaticRoute = function (destX, destY) {
if (core.isset(route[nid])) continue;
var deepAdd=1;
var nextId, nextBlock = core.getBlock(nx,ny);
if (nextBlock!=null){
nextId = nextBlock.block.event.id;
// 绕过亮灯(因为只有一次通行机会很宝贵)
if(nextId == "light") deepAdd=100;
// 绕过路障
if (nextId.substring(nextId.length-3)=="Net") deepAdd=core.values.lavaDamage;
// if (nextId.substring(nextId.length-3)=="Net") deepAdd=core.values.lavaDamage*10;
// 绕过血瓶
if (!core.flags.potionWhileRouting && nextId.substring(nextId.length-6)=="Potion") deepAdd=20;
if (!core.flags.potionWhileRouting && nextId.substring(nextId.length-6)=="Potion") deepAdd+=20;
// 绕过传送点
if (nextBlock.block.event.trigger == 'changeFloor') deepAdd = 10;
if (nextBlock.block.event.trigger == 'changeFloor') deepAdd+=10;
}
if (core.status.checkBlock.damage[nid]>0)
deepAdd = core.status.checkBlock.damage[nid];
deepAdd+=core.status.checkBlock.damage[nid]*10;
if (nx == destX && ny == destY) {
route[nid] = direction;
@ -609,11 +600,10 @@ control.prototype.automaticRoute = function (destX, destY) {
continue;
route[nid] = direction;
queue.push(total*(nowDeep+deepAdd)+nid);
queue.queue({depth: deep+deepAdd, x: nx, y: ny});
}
if (core.isset(route[destX + fw * destY])) break;
}
if (!core.isset(route[destX + fw * destY])) {
return false;
}

1
libs/thirdparty/priority-queue.min.js vendored Normal file

File diff suppressed because one or more lines are too long