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> </div>
<script src='libs/thirdparty/mid.min.js'></script> <script src='libs/thirdparty/mid.min.js'></script>
<script src='libs/thirdparty/lz-string.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 id='mainScript' src='main.js'></script>
<script>main.init();main.listen();</script> <script>main.init();main.listen();</script>
</body> </body>

View File

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

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

File diff suppressed because one or more lines are too long