UNPKG

malwoden

Version:

![alt text](./coverage/badge-lines.svg) ![alt text](./coverage/badge-statements.svg) ![alt text](./coverage/badge-functions.svg) ![alt text](./coverage/badge-branches.svg)

129 lines 5.29 kB
"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.CellularAutomataBuilder = void 0; var struct_1 = require("../struct"); var rand_1 = require("../rand"); var util_1 = require("./util"); var builder_1 = require("./builder"); /** Used to create CellularAutomata Maps. */ var CellularAutomataBuilder = /** @class */ (function (_super) { __extends(CellularAutomataBuilder, _super); /** * Creates a Cellular Automata Map Generator * * @param config.width The width of the map. * @param config.height The height of the map. * @param config.aliveValue The value to use for alive tiles * @param config.deadValue The value to use for dead tiles */ function CellularAutomataBuilder(options) { var _a; var _this = _super.call(this, options) || this; _this.aliveValue = options.wallValue; _this.deadValue = options.floorValue; _this.rng = (_a = options.rng) !== null && _a !== void 0 ? _a : new rand_1.AleaRNG(); return _this; } /** * Randomly sets each cell to either alive or dead. * * @param isAliveChance The chance for a cell to be set to the 'alive' value. */ CellularAutomataBuilder.prototype.randomize = function (isAliveChance) { if (isAliveChance === void 0) { isAliveChance = 0.6; } for (var x = 0; x < this.map.width; x++) { for (var y = 0; y < this.map.height; y++) { var isAlive = this.rng.next() > isAliveChance; if (isAlive) { this.map.set({ x: x, y: y }, this.aliveValue); } else { this.map.set({ x: x, y: y }, this.deadValue); } } } }; CellularAutomataBuilder.prototype.countAliveNeighbors = function (x, y) { var count = 0; for (var i = -1; i < 2; i++) { for (var j = -1; j < 2; j++) { var neighbor_x = x + i; var neighbor_y = y + j; if (i == 0 && j == 0) { //this code is supposed to do nothing as it is the focal point we're checking around. } else if (this.map.isInBounds({ x: neighbor_x, y: neighbor_y }) == false) { count++; } else if (this.map.get({ x: neighbor_x, y: neighbor_y }) == this.aliveValue) { count++; } } } return count; }; /** * Runs a number of simulation steps. * Each step generally "smooths" the map. * * @param stepCount The number of steps to run. */ CellularAutomataBuilder.prototype.doSimulationStep = function (stepCount) { if (stepCount === void 0) { stepCount = 1; } for (var step = 0; step < stepCount; step++) { var newMap = new struct_1.Table(this.map.width, this.map.height); var oldMap = this.map; //this just renames the table to prevent 'this' all over the place. for (var x = 0; x < oldMap.width; x++) { for (var y = 0; y < oldMap.height; y++) { var nbs = this.countAliveNeighbors(x, y); if (oldMap.get({ x: x, y: y }) === this.aliveValue) { if (nbs < 4) { newMap.set({ x: x, y: y }, this.deadValue); } else { newMap.set({ x: x, y: y }, this.aliveValue); } } else { if (nbs > 3) { newMap.set({ x: x, y: y }, this.aliveValue); } else { newMap.set({ x: x, y: y }, this.deadValue); } } } } this.map = newMap; } }; /** * Connects areas of the map to ensure they are all connected. * * For instance, if you're using an alive value of 1 for walls, * then this can connect the dead value of 0 to ensure all * squares on the map are accessable. * * @param value The value to connect (default this.deadValue) */ CellularAutomataBuilder.prototype.connect = function (value) { if (value === void 0) { value = this.deadValue; } return util_1.connect(this.map, value); }; return CellularAutomataBuilder; }(builder_1.Builder)); exports.CellularAutomataBuilder = CellularAutomataBuilder; //# sourceMappingURL=cellular-automata-builder.js.map