📄 mbm_mlmap.d2l
字号:
this.AreaCacheSave = function() {
if(!this.AreaInitialized) {
mlprint("mlMap:AreaCacheSave - Cannot save to cache: no area is initialized");
this.LastError = ("mlMap:AreaCacheSave - Cannot save to cache: no area is initialized");
return false;
}
AreaCache[this.AreaInitialized] = {minx:this.minx, miny:this.miny, maxx:this.maxx,
maxy:this.maxy, sizex:this.sizex, sizey:this.sizey,
roomsizex:this.roomsizex, roomsizey:this.roomsizey,
Area:this.Area}
return true;
}
this.AreaCacheLoad = function (area) {
if(!AreaCache[area]) {
mlprint("mlMap:AreaCacheLoad - Could not load from cache: Area not in cache");
this.LastError = ("mlMap:AreaCacheLoad - Could not load from cache: Area not in cache");
return false;
}
this.Area = AreaCache[area].Area;
this.minx = AreaCache[area].minx;
this.miny = AreaCache[area].miny;
this.maxx = AreaCache[area].maxx;
this.maxy = AreaCache[area].maxy;
this.sizex = AreaCache[area].sizex;
this.sizey = AreaCache[area].sizey;
this.roomsizey = AreaCache[area].roomsizey;
this.roomsizex = AreaCache[area].roomsizex;
return true;
}
this.AreaCacheDelete = function(area) {
if(AreaCache[area]) delete AreaCache[area];
}
this.AreaCacheClear = function () {
AreaCache = new Object();
}
//////////////////////////////////////////////////////////////////////
// oMap.IsWalkable(_x, _y)
// -------------------------------------------------------------------
// returns: true if walkable else false
// requires an initialized map
//////////////////////////////////////////////////////////////////////
function oMap_iswalkable(_x, _y)
{
if(!_isinrange(_x, _y))
return false;
//print(_x, _y);
// print("iswalkable" + _x + "," + _y);
var v = this.Area == null ? this.RoomXY(_x, _y) : this.Area[_y-this.miny][_x-this.minx];
// print("V=" + v);
if (v & 1 || v == undefined)
return false;
else
return true;
}
//////////////////////////////////////////////////////////////////////
// oMap.FindClosestWalkable(_x, _y)
// -------------------------------------------------------------------
// returns: coordinate of closest walkable
// requires an initialized map
//////////////////////////////////////////////////////////////////////
function oMap_findclosestwalkable(x, y)
{
if(!_isinrange(x, y))
return false;
if(this.IsWalkable(x,y))
return {x:x, y:y};
var dist = 0;
while(1){
for(var vy = -1-dist; vy < 2+dist; vy++){
for(var vx = -1-dist; vx < 2+dist; vx++){
if(this.IsWalkable(x+vx, y+vy)) {
return {x:x+vx, y:y+vy};
}
}
}
dist++;
}
}
//////////////////////////////////////////////////////////////////////
// oMap.LOS(_x1, _y1, _x2, _y2)
// -------------------------------------------------------------------
// returns: true if there is line of sight
// requires an initialized map
// THANKS TO BRESENHAM
//////////////////////////////////////////////////////////////////////
function oMap_los(Ax, Ay, Bx, By)
{
if(!_isinrange(Ax, Ay))
return false;
if(!_isinrange(Bx, By))
return false;
var dX = Bx-Ax;
var dY = By-Ay;
var Xincr, Yincr;
if (Ax > Bx) { Xincr=-1; } else { Xincr=1; }
if (Ay > By) { Yincr=-1; } else { Yincr=1; }
if (dX >= dY){
var dPr = dY<<1;
var dPru = dPr - (dX<<1);
var P = dPr - dX;
for (; dX>=0; dX--) {
if (this.Area[Ay-this.miny][Ax-this.minx] & 4)
return false;
if (P > 0){
Ax+=Xincr;
Ay+=Yincr;
P+=dPru;
}
else{
Ax+=Xincr;
P+=dPr;
}
}
}
else{
var dPr = dX<<1;
var dPru = dPr - (dY<<1);
var P = dPr - dY;
for (; dY>=0; dY--) {
if (this.Area[Ay-this.miny][Ax-this.minx] & 4)
return false;
if (P > 0){
Ax+=Xincr;
Ay+=Yincr;
P+=dPru;
}
else{
Ay+=Yincr;
P+=dPr;
}
}
}
return true;
}
//////////////////////////////////////////////////////////////////////
// oMap.getMapExit(_sx, _sy, _dx, _dy, _dist, _reduc, _algo, _fh)
// -------------------------------------------------------------------
// returns: path on success, assigns the path to this.Path; false on failure
// requires an initialized map
//////////////////////////////////////////////////////////////////////
function oMap_getmapexit(_d) {
if(_d < 0 || _d > 3) return false;
var ec = new Array();
var notdoneexit = false;
var count = 0;
var avgcoord = 0;
if(_d == 0) { //NORTH Scan for white space
for (var x = this.minx; x < this.maxx-1; x++){
if(this.IsWalkable(x, this.miny)){
ec[ec.length] = {x:x, y:this.miny};
notdoneexit = true;
continue;
}
else if (notdoneexit) {
if (ec.length < 4){ //Too small
notdoneexit = false;
ec.length = 0;
continue;
}
break;
}
}
if (ec.length < 4) return false; //exit because we didnt find an exit
for(var i = 0; i < ec.length; i++) avgcoord += ec[i].x;
return this.FindClosestWalkable(Math.floor(avgcoord / ec.length), this.miny+6);
}
if(_d == 1) { //EAST Scan for white space
for (var y = this.miny; y < this.maxy-1; y++){
if(this.IsWalkable(this.maxx, y)){
ec[ec.length] = {x:this.maxx, y:y};
notdoneexit = true;
continue;
}
else if (notdoneexit) {
if (ec.length < 4){ //Too small
notdoneexit = false;
ec.length = 0;
continue;
}
break;
}
}
if (ec.length < 4) return false; //exit because we didnt find an exit
for(var i = 0; i < ec.length; i++) avgcoord += ec[i].y;
return this.FindClosestWalkable(this.maxx-6, Math.floor(avgcoord / ec.length));
}
if(_d == 2) { //South Scan for white space
for (var x = this.minx; x < this.maxx-1; x++){
if(this.IsWalkable(x, this.maxy)){
ec[ec.length] = {x:x, y:this.maxy};
notdoneexit = true;
continue;
}
else if (notdoneexit) {
if (ec.length < 4){ //Too small
notdoneexit = false;
ec.length = 0;
continue;
}
break;
}
}
if (ec.length < 4) return false; //exit because we didnt find an exit
for(var i = 0; i < ec.length; i++) avgcoord += ec[i].x;
return this.FindClosestWalkable(Math.floor(avgcoord / ec.length), this.maxy-6);
}
if(_d == 3) { //West Scan for white space
for (var y = this.miny; y < this.maxy-1; y++){
if(this.IsWalkable(this.minx, y)){
ec[ec.length] = {x:this.minx, y:y};
notdoneexit = true;
continue;
}
else if (notdoneexit) {
if (ec.length < 4){ //Too small
notdoneexit = false;
ec.length = 0;
continue;
}
break;
}
}
if (ec.length < 4) return false; //exit because we didnt find an exit
for(var i = 0; i < ec.length; i++) avgcoord += ec[i].y;
return this.FindClosestWalkable(this.minx+6, Math.floor(avgcoord / ec.length));
}
}
function oMap_getlevelwarpxy(warptype, level)
{
//Access a room list of various warps.
var troom = getRoom();
roomloop:
if(troom) do {
switch(warptype) {
case 0: //next
switch(troom.number) {
case 143: //countesscrypt
case 144:
case 145:
case 146:
case 788: //durance
case 789:
case 790:
case 791:
case 1078: //baal
case 1079:
case 1080:
case 1081:
break roomloop;
}
case 4: //special
switch(troom.number) {
case 163: //countessTower
case 164:
break roomloop;
}
}
} while(troom.getNext());
if(!troom) {
mlprint("mlMap:getLevelWarpXY - Could not get the specified warp coordinates");
this.LastError = ("mlMap:getLevelWarpXY - Could not get the specified warp coordinates");
return false;
}
var temp = this.FindClosestWalkable(troom.x*5+this.roomsizex/2, troom.y*5+this.roomsizey/2)
return temp;
}
//////////////////////////////////////////////////////////////////////
// oMap.FindPathToWarp(warptype, _sx, _sy, _dist, _reduc, _algo, _fh, _factor, _scoreonly)
// -------------------------------------------------------------------
// returns: path on success, assigns the path to this.Path; false on failure
// requires an initialized map
//////////////////////////////////////////////////////////////////////
this.FindPathToWarp = function(warptype, _sx, _sy, _dist, _reduc, _algo, _fh, _factor, _scoreonly)
{
var dx,dy;
if(warptype == mlWARPWAYPOINT) {
mlprint("Please Note: Far waypoints are not supported in this version");
var mlwp = getUnit(2, "waypoint");
if(!mlwp) {
mlprint("mlMap:mlFindPath - Could not find a waypoint");
this.LastError = ("mlMap:mlFindPath - Could not find a waypoint");
return false;
}
dx = mlwp.x;
dy = mlwp.y;
}
else if(warptype != mlWARPNONE) {
var tcoord = this.getLevelWarpXY(warptype)
if(!tcoord)
return false;
dx = tcoord.x;
dy = tcoord.y;
}
return this.FindPath(_sx, _sy, dx, dy, _dist, _reduc, _algo, _fh, _factor, _scoreonly);
}
//////////////////////////////////////////////////////////////////////
// oMap.FindPathToRoom(room, _sx, _sy, _dist, _reduc, _algo, _fh, _factor, _scoreonly)
// -------------------------------------------------------------------
// Finds a path to a room
// returns: path on success, assigns the path to this.Path; false on failure
// requires an initialized map
//////////////////////////////////////////////////////////////////////
this.FindPathToRoom = function(rooms, _sx, _sy, _dist, _reduc, _algo, _fh, _factor, _scoreonly)
{
var tc;
var trooms = getRoom();
outofhere:
if(trooms) do {
if(rooms instanceof Array) {
for(var i = 0; i<rooms.length; i++) {
if(rooms[i] == trooms.number) {
tc = this.FindClosestWalkable(trooms.x*5+this.roomsizex/2, trooms.y*5+this.roomsizey/2);
break outofhere;
}
}
}
else if(rooms == trooms.number) {
tc = this.FindClosestWalkable(trooms.x*5+this.roomsizex/2, trooms.y*5+this.roomsizey/2);
break;
}
} while(trooms.getNext());
if(!tc) {
mlprint("mlMap:mlFindPathToRoom - Could not find room coordinates");
this.LastError = ("mlMap:mlFindPathToRoom - Could not find room coordinates");
return false;
}
return this.FindPath(_sx, _sy, tc.x, tc.y, _dist, _reduc, _algo, _fh, _factor, _scoreonly);
}
//////////////////////////////////////////////////////////////////////
// oMap.FindPathAlongPoints(points, _sx, _sy, _dist, _reduc, _algo, _fh, _factor, _scoreonly)
// -------------------------------------------------------------------
// Finds a path along the given points
// returns: path on success, assigns the path to this.Path; false on failure
// requires an initialized map
//////////////////////////////////////////////////////////////////////
this.FindPathAlongPoints = function(points, _sx, _sy, _dist, _reduc, _algo, _fh, _factor, _scoreonly)
{
if(!(points instanceof Array) || !points.length) {
mlprint("mlMap:FindPathAlongPoints - Invalid points argument passed to function");
this.LastError = ("mlMap:FindPathAlongPoints - Invalid points argument passed to function");
return false;
}
for (var i =0; i<points.length; i++) {
if(!this.IsWalkable(points[i].x, points[i].y)) {
mlprint("mlMap:FindPathAlongPoints - One of points is out of range or not walkable");
this.LastError = ("mlMap:FindPathAlongPoints - One of points is out of range or not walkable");
return false;
}
}
var fp = new Array();
var tpath = this.FindPath(_sx, _sy, points[0].x, points[0].y, _dist, _reduc, _algo, _fh, _factor, _scoreonly)
if(!tpath) return false;
fp.concat(tpath);
if(points.length >1) {
for(var i = 1; i<points.length; i++) {
tpath = this.FindPath(points[i-1].x, points[i-1].x, points[i].x, points[i].y, _dist, _reduc, _algo, _fh, _factor, _scoreonly)
if(!tpath) return false;
fp.concat(tpath);
}
}
return fp;
}
//////////////////////////////////////////////////////////////////////
// oMap.FindPath(_sx, _sy, _dx, _dy, _dist, _reduc, _algo, _fh)
// -------------------------------------------------------------------
// returns: path on success, assigns the path to this.Path; false on failure
// requires an initialized map
//////////////////////////////////////////////////////////////////////
function oMap_findpath(_sx, _sy, _dx, _dy, _dist, _reduc, _algo, _fh, _factor, _scoreonly) {
if(!_sx || !_sy || !_dx || !_dy){ //basic check for illegal coordinates
mlprint("mlMap:mlFindPath - The start or end has a 0 coordinate value");
this.LastError = ("mlMap:mlFindPath - The start or end has a 0 coordinate value");
return false;
}
if(!this.IsWalkable(_sx, _sy) || !this.IsWalkable(_dx, _dy))
{
mlprint("mlMap.FindPath - Either the start or end coord's are not walkable or out of range");
this.LastError = ("mlMap.FindPath - Either the start or end coord's are not walkable or out of range");
return false;
}
if(typeof(_reduc) != "number" || _reduc < 0) _reduc = 0;
if(typeof(_algo) != "number" || _algo < 0 || _algo > 1) _algo=0;
if(typeof(_factor) != "number" || _fh < 0) _factor = 1.5;
if(typeof(_fh) != "number" || _fh < 0 || _fh > FORWARDHEURISTICS.length-1) _fh=4;
if(_algo) {
var oq = new oFIFO();
_fh = 0;
}
else var oq = new oHeap("h", mlHEAPLOW);
var minx = this.minx;
var miny = this.miny;
var maxx = this.maxx;
var maxy = this.maxy;
var width = this.maxx - this.minx-1;
var height = this.maxy - this.miny-1;
var AreaData = this.Area;
var ptd = new Array();
var fp = new Array();
var dx = _dx - minx;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -