malwoden
Version:
   
95 lines • 3.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSimpleHallwayFromRooms = exports.connect = void 0;
var Calc = require("../calc");
var astar_1 = require("../pathfinding/astar");
/**
* 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)
*/
function connect(table, value) {
var spacesToConnect = new Set();
var groups = [];
var paths = [];
// Get all spaces with the value
for (var x = 0; x < table.width; x++) {
for (var y = 0; y < table.height; y++) {
if (table.get({ x: x, y: y }) === value) {
spacesToConnect.add(x + ":" + y);
}
}
}
// Figure out which groups there are
while (spacesToConnect.size > 0) {
var v = Array.from(spacesToConnect.entries())[0][0];
var _a = v.split(":"), x = _a[0], y = _a[1];
var position = {
x: Number.parseInt(x),
y: Number.parseInt(y),
};
// Grab an area, then remove those tiles from the spacesToConnect
var selection = table.floodFillSelect(position);
groups.push(selection);
for (var _i = 0, selection_1 = selection; _i < selection_1.length; _i++) {
var s = selection_1[_i];
spacesToConnect.delete(s.x + ":" + s.y);
}
}
// spacesToConnect is now empty.
// Each group in groups is an isolated set of tiles
for (var i = 0; i < groups.length; i++) {
// Ignore the last group
if (i === groups.length - 1)
break;
var current = groups[i];
var next = groups[i + 1];
// Get the center point from each area
var currentCenter = Calc.Vector.getCenter(current);
var currentPoint = Calc.Vector.getClosest(current, currentCenter);
var nextCenter = Calc.Vector.getCenter(next);
var nextPoint = Calc.Vector.getClosest(next, nextCenter);
// Get two points that are close to the edge for each section
var closestCurrent = Calc.Vector.getClosest(next, currentPoint);
var closestNext = Calc.Vector.getClosest(current, nextPoint);
var a = new astar_1.AStar({ topology: "four" });
var connection = a.compute(closestCurrent, closestNext);
// Connect the paths
if (!connection)
throw new Error("Error: Could not connect cell areas");
for (var _b = 0, connection_1 = connection; _b < connection_1.length; _b++) {
var v = connection_1[_b];
table.set(v, value);
}
paths.push(connection);
}
return {
groups: groups,
paths: paths,
};
}
exports.connect = connect;
function getSimpleHallwayFromRooms(a, b) {
var hallway = [];
var start = a.center();
var end = b.center();
var minX = Math.min(start.x, end.x);
var maxX = Math.max(start.x, end.x);
var minY = Math.min(start.y, end.y);
var maxY = Math.max(start.y, end.y);
// Connect the vertical
// Connect the horizontal
for (var y = minY; y <= maxY; y++) {
hallway.push({ x: start.x, y: y });
}
for (var x = minX + 1; x <= maxX; x++) {
hallway.push({ x: x, y: end.y });
}
return hallway;
}
exports.getSimpleHallwayFromRooms = getSimpleHallwayFromRooms;
//# sourceMappingURL=util.js.map