diff --git a/index.html b/index.html index b2a606db..52b69439 100644 --- a/index.html +++ b/index.html @@ -121,6 +121,7 @@ + diff --git a/libs/control.js b/libs/control.js index 7501f365..4ac0de4f 100644 --- a/libs/control.js +++ b/libs/control.js @@ -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; } diff --git a/libs/thirdparty/priority-queue.min.js b/libs/thirdparty/priority-queue.min.js new file mode 100644 index 00000000..f161936f --- /dev/null +++ b/libs/thirdparty/priority-queue.min.js @@ -0,0 +1 @@ +!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.PriorityQueue=t()}}(function(){return function t(e,i,r){function o(n,s){if(!i[n]){if(!e[n]){var h="function"==typeof require&&require;if(!s&&h)return h(n,!0);if(a)return a(n,!0);var u=new Error("Cannot find module '"+n+"'");throw u.code="MODULE_NOT_FOUND",u}var p=i[n]={exports:{}};e[n][0].call(p.exports,function(t){var i=e[n][1][t];return o(i?i:t)},p,p.exports,t,e,i,r)}return i[n].exports}for(var a="function"==typeof require&&require,n=0;no;)a=o+r>>>1,i(t[a],e)>=0?o=a+1:r=a;return o},e.exports=r=function(){function t(t){var e;this.options=t,this.comparator=this.options.comparator,this.data=(null!=(e=this.options.initialValues)?e.slice(0):void 0)||[],this.data.sort(this.comparator).reverse()}return t.prototype.queue=function(t){var e;e=o(this.data,t,this.comparator),this.data.splice(e,0,t)},t.prototype.dequeue=function(){return this.data.pop()},t.prototype.peek=function(){return this.data[this.data.length-1]},t.prototype.clear=function(){this.data.length=0},t}()},{}],4:[function(t,e,i){var r;e.exports=r=function(){function t(t){var e,i,r,o,a,n,s,h,u;for(this.comparator=(null!=t?t.comparator:void 0)||function(t,e){return t-e},this.pageSize=(null!=t?t.pageSize:void 0)||512,this.length=0,h=0;1<=0?n>r:r>n;i=n>=0?++r:--r)e.push(null);if(this._memory=[],this._mask=this.pageSize-1,t.initialValues)for(s=t.initialValues,o=0,a=s.length;a>o;o++)u=s[o],this.queue(u)}return t.prototype.queue=function(t){this.length+=1,this._write(this.length,t),this._bubbleUp(this.length,t)},t.prototype.dequeue=function(){var t,e;return t=this._read(1),e=this._read(this.length),this.length-=1,this.length>0&&(this._write(1,e),this._bubbleDown(1,e)),t},t.prototype.peek=function(){return this._read(1)},t.prototype.clear=function(){this.length=0,this._memory.length=0},t.prototype._write=function(t,e){var i;for(i=t>>this._shift;i>=this._memory.length;)this._memory.push(this._emptyMemoryPageTemplate.slice(0));return this._memory[i][t&this._mask]=e},t.prototype._read=function(t){return this._memory[t>>this._shift][t&this._mask]},t.prototype._bubbleUp=function(t,e){var i,r,o,a;for(i=this.comparator;t>1&&(r=t&this._mask,t3?o=t&~this._mask|r>>1:2>r?(o=t-this.pageSize>>this._shift,o+=o&~(this._mask>>1),o|=this.pageSize>>1):o=t-2,a=this._read(o),!(i(a,e)<0));)this._write(o,e),this._write(t,a),t=o},t.prototype._bubbleDown=function(t,e){var i,r,o,a,n;for(n=this.comparator;tthis._mask&&!(t&this._mask-1)?i=r=t+2:t&this.pageSize>>1?(i=(t&~this._mask)>>1,i|=t&this._mask>>1,i=i+1<0)for(t=e=1,i=this.data.length;i>=1?i>e:e>i;t=i>=1?++e:--e)this._bubbleUp(t)},t.prototype.queue=function(t){this.data.push(t),this._bubbleUp(this.data.length-1)},t.prototype.dequeue=function(){var t,e;return e=this.data[0],t=this.data.pop(),this.data.length>0&&(this.data[0]=t,this._bubbleDown(0)),e},t.prototype.peek=function(){return this.data[0]},t.prototype.clear=function(){this.length=0,this.data.length=0},t.prototype._bubbleUp=function(t){for(var e,i;t>0&&(e=t-1>>>1,this.comparator(this.data[t],this.data[e])<0);)i=this.data[e],this.data[e]=this.data[t],this.data[t]=i,t=e},t.prototype._bubbleDown=function(t){var e,i,r,o,a;for(e=this.data.length-1;;){if(i=(t<<1)+1,o=i+1,r=t,e>=i&&this.comparator(this.data[i],this.data[r])<0&&(r=i),e>=o&&this.comparator(this.data[o],this.data[r])<0&&(r=o),r===t)break;a=this.data[r],this.data[r]=this.data[t],this.data[t]=a,t=r}},t}()},{}]},{},[1])(1)}); \ No newline at end of file