malwoden
Version:
   
135 lines • 5.86 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.DrunkardsWalkBuilder = void 0;
var rand_1 = require("../rand");
var get_ring_1 = require("../fov/get-ring");
var builder_1 = require("./builder");
/** Generator to perform a Drunkard's Walk*/
var DrunkardsWalkBuilder = /** @class */ (function (_super) {
__extends(DrunkardsWalkBuilder, _super);
/**
* Creates a new DrunkardsWalk Generator
* @param config - Generator Config
* @param config.width number - Width of the map
* @param config.height number - Height of the map
* @param config.rng IRNG - Optional random number generator
* @param config.topology "four" | "eight" - Topology to use. Default four.
* @param config.floorTile - The value to use for a path
* @param config.wallTile - The value to use for a wall
*/
function DrunkardsWalkBuilder(config) {
var _a, _b;
var _this = _super.call(this, config) || this;
_this._paths = [];
_this._coveredCount = 0;
_this._rng = (_a = config.rng) !== null && _a !== void 0 ? _a : new rand_1.AleaRNG();
_this._topology = (_b = config.topology) !== null && _b !== void 0 ? _b : "four";
_this._floorTile = config.floorTile;
_this._wallTile = config.wallTile;
// Initialize Map
_this.map.fill(_this._wallTile);
return _this;
}
/**
* Adds a point to the drunkards walk,
* adjusting path, table, and steps as needed.
* @param point Vector2 - The point to add
*/
DrunkardsWalkBuilder.prototype.addPoint = function (point) {
if (this.map.get(point) !== this._floorTile) {
this._coveredCount++;
}
this.map.set(point, this._floorTile);
};
/**
* Returns a list of paths previously walked.
* @returns Vector2[][]
*/
DrunkardsWalkBuilder.prototype.getPaths = function () {
return this._paths;
};
/**
* Returns # of walked tiles / # of total tiles.
* @returns number - Between 0.0 and 1.0
*/
DrunkardsWalkBuilder.prototype.getCoverage = function () {
var total = this.map.width * this.map.height;
return this._coveredCount / total;
};
DrunkardsWalkBuilder.prototype.nextStepWillOverflow = function (maxCoverage) {
var total = this.map.width * this.map.height;
return (this._coveredCount + 1) / total > maxCoverage;
};
/**
* Generate a path by walking a number of steps.
* Can be called multiple times to have the Drunkard 'jump' to a different spot.
* @param config - The walk configuration
* @param config.pathCount- The number of independent paths to make from the start position. Default 1.
* @param config.start Vector2 - The starting position. Default random.
* @param config.stepsMin number - The minimum number of steps to take for each walk (inclusive)
* @param config.stepsMax number - The maximum number of steps to take for each walk (exclusive)
* @param config.maxCoverage number - Stops walking if this much of the map is explored. Range [0.0, 1.0], default 1.0
* @returns Vector2[][] - The paths walked
*/
DrunkardsWalkBuilder.prototype.walk = function (config) {
var _a;
var _b, _c, _d, _e;
var pathCount = (_b = config.pathCount) !== null && _b !== void 0 ? _b : 1;
var maxCoverage = (_c = config.maxCoverage) !== null && _c !== void 0 ? _c : 1;
var start = (_d = config.start) !== null && _d !== void 0 ? _d : this.getRandPoint();
var paths = [];
// Independent walks
for (var i = 0; i < pathCount; i++) {
var path = [start];
this.addPoint((_e = config.start) !== null && _e !== void 0 ? _e : this.getRandPoint());
var steps = this._rng.nextInt(config.stepsMin, config.stepsMax);
for (var i_1 = 0; i_1 < steps; i_1++) {
if (this.nextStepWillOverflow(maxCoverage)) {
break;
}
var next = this.getRandomNeighbor(path[path.length - 1]);
this.addPoint(next);
path.push(next);
}
paths.push(path);
}
(_a = this._paths).push.apply(_a, paths);
return paths;
};
DrunkardsWalkBuilder.prototype.getRandPoint = function () {
var x = this._rng.nextInt(0, this.map.width);
var y = this._rng.nextInt(0, this.map.height);
return { x: x, y: y };
};
DrunkardsWalkBuilder.prototype.getRandomNeighbor = function (pos) {
var next = undefined;
while (next === undefined) {
// Get next neighbor
var neighbors = this._topology === "four"
? get_ring_1.getRing4(pos.x, pos.y, 1)
: get_ring_1.getRing8(pos.x, pos.y, 1);
var n = this._rng.nextItem(neighbors);
// Check if it is in bounds
if (this.map.isInBounds(n)) {
next = n;
}
}
return next;
};
return DrunkardsWalkBuilder;
}(builder_1.Builder));
exports.DrunkardsWalkBuilder = DrunkardsWalkBuilder;
//# sourceMappingURL=drunkards-walk-builder.js.map