-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (80 loc) · 2.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const seedrandom = require('seedrandom');
function random(rng, min, max) {
return Math.floor(rng() * (max - min)) + min;
}
function step(x, y, direction) {
return [x + direction[0], y + direction[1]];
}
function validatePosition(map, x, y) {
return x >= 0
&& x < map[0].length
&& y >= 0
&& y < map.length;
}
function generateMap(seed, configOverride) {
var rng = seedrandom(seed);
//Make config parameter optional
var configOverride = configOverride || {}
//Default configuration overwritten if needed
var config = {
numberOfPaths: configOverride.numberOfPaths || 30,
minPathLength: configOverride.minPathLength || 5,
maxPathLength: configOverride.maxPathLength || 12,
maxWidth: configOverride.maxWidth || 30,
maxHeight: configOverride.maxHeight || 30,
startPosition: configOverride.startPosition || {x: -1, y: -1}
}
//Ensure valid starting position
if (config.startPosition.x < 0 || config.startPosition.x >= config.maxWidth) {
config.startPosition.x = random(rng, 0, config.maxWidth);
}
if (config.startPosition.y < 0 || config.startPosition.y >= config.maxHeight) {
config.startPosition.y = random(rng, 0, config.maxHeight);
}
//Generate empty map
var map = [];
for (var y = 0; y < config.maxHeight; y++) {
map.push([]);
for (var x = 0; x < config.maxWidth; x++) {
map[y].push(0);
}
}
//Available directions
var directions = [
[-1, 0], //left
[ 1, 0], //right
[ 0, -1], //down
[ 0, 1] //up
]
//Initialize variables needed during generation
var x = config.startPosition.x;
var y = config.startPosition.y;
map[y][x] = 1;
var direction;
var lastDirection;
var pathLength;
//Generate paths
for (var i = 0; i < config.numberOfPaths; i++) {
//Select a path length
pathLength = random(rng, config.minPathLength, config.maxPathLength + 1);
//Select direction, which allows at least one step, and is different from the previous direction
do {
direction = directions[random(rng, 0, directions.length)];
} while (!validatePosition(map, ...step(x, y, direction)) || (lastDirection && direction == lastDirection));
//Step until path length or map boundary reached
for (var j = 0; j < pathLength; j++) {
//Check if step is possible
if (!validatePosition(map, ...step(x, y, direction))) {
break;
}
//Perform step
[x, y] = step(x, y, direction);
map[y][x] = 1;
}
//Store last direction
lastDirection = direction;
}
//Done generating!
return map;
}
module.exports = generateMap;