@sbj42/maze-generator-core
Advanced tools
Comparing version 0.1.3 to 0.1.4
{ | ||
"name": "@sbj42/maze-generator-core", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Core library for maze generator algorithms", | ||
@@ -5,0 +5,0 @@ "main": "src", |
@@ -28,2 +28,6 @@ /** | ||
this._blockWidth = (width+31) >> 5; | ||
if (options._grid) { | ||
this._grid = options._grid; | ||
return; | ||
} | ||
this._grid = new Array(this._blockWidth * height); | ||
@@ -55,2 +59,14 @@ var initBlock = interior ? ~0 : 0; | ||
/** | ||
* Returns a copy of the GridMask. | ||
* | ||
* @return {GridMask} | ||
*/ | ||
GridMask.prototype.clone = function() { | ||
return new GridMask(this.width(), this.height(), { | ||
exterior: this._exterior, | ||
_grid: this._grid.slice() | ||
}); | ||
}; | ||
/** | ||
* Returns the boolean value at the specified cell | ||
@@ -93,2 +109,28 @@ * | ||
/** | ||
* ... | ||
* | ||
* @param {integer} x | ||
* @param {integer} y | ||
* @param {boolean} [value=true] | ||
*/ | ||
GridMask.prototype.testAndSet = function(x, y, value) { | ||
if (value == null) | ||
value = true; | ||
if (x < 0 || x >= this._width || y < 0 || y >= this._height) { | ||
if (this._exterior == value) | ||
return false; | ||
throw new Error('cell out of bounds: ' + x + ',' + y); | ||
} | ||
var index = y * this._blockWidth + (x >> 5); | ||
var mask = 1 << (x & 31); | ||
if (((this._grid[index] & mask) != 0) == value) | ||
return false; | ||
if (value) | ||
this._grid[index] |= mask; | ||
else | ||
this._grid[index] &= ~mask; | ||
return true; | ||
}; | ||
module.exports = GridMask; |
18874
473