Unfinished dungeon generator
Simple dungeon generator, which generates rooms only, without passages between rooms.
by realhunts
HTML
<html>
<head>
<title>Maze Generator</title>
<meta charset="utf-8" />
</head>
<body>
<div style="padding: 15;">
<canvas id="canvas" width="640" height="640" style="background: lightgrey"></canvas>
</div>
</body>
</html>
CSS
* {
padding: 0;
margin: 0;
}
canvas {
background: eee;
display: block;
margin: 0 auto;
}
JavaScript
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
const DWIDTH = 32; // Dungeon width
const DHEIGHT = 32; // Dungeon height
const ROOMINTERVAL = 8; // Room density (3+)
const MINROOM = 3; // Min room size. Must be ROOMINTERVAL - 2 or bigger
const EXTRADOORS = 5; // Additional doors for better connectivity
arrayInstruments();
genDungeon();
showDungeon();
// Displays primitive map of the dungeon
function showDungeon() {
for (var x = 0; x < DWIDTH; x++) {
for (var y = 0; y < DHEIGHT; y++) {
switch (dungeon[x][y]) {
case 0:
ctx.beginPath();
ctx.fillStyle = "lightgrey";
ctx.rect(x * 20, y * 20, 18, 18);
ctx.fill();
break;
case 1:
ctx.beginPath();
ctx.fillStyle = "black";
ctx.rect(x * 20, y * 20, 18, 18);
ctx.fill();
break;
case 2:
ctx.beginPath();
ctx.fillStyle = "yellow";
ctx.rect(x * 20 + 1, y * 20 + 1, 16, 16);
// ctx.rect(x * 20 + 12, y * 20 + 6, 2, 4);
ctx.stroke();
ctx.fill();
break;
}
}
}
}
// Generates dungeon of rooms and doors
function genDungeon() {
// Fill full dungeon with walls
dungeon = new Array();
dungeon.dup(1, 0, 0, DWIDTH, DHEIGHT);
// Create random seeds of growth in size MINROOM x MINROOM
var seeds = new Array();
while (true) {
var free = dungeon.biggestFree(0, 0, 0, DWIDTH, DHEIGHT);
if (free.biggest < ROOMINTERVAL) {
break;
} else {
roomX = free.x + 1 + rnd(free.biggest - 1 - MINROOM);
roomY = free.y + 1 + rnd(free.biggest - 1 - MINROOM);
dungeon.dup(0, roomX, roomY, MINROOM, MINROOM);
seeds.push({
x: roomX,
y: roomY,
width: MINROOM,
height: MINROOM,
delete: "no"
});
}
}
var rooms = [];
// Now we have seeds of growth in array rooms.
// Lets try to expand seeds by moving their...