-
Notifications
You must be signed in to change notification settings - Fork 0
/
levelGenerator.js
142 lines (124 loc) · 3.26 KB
/
levelGenerator.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { globals } from "./globals.js";
export const paths = [];
function addCord(first, second) {
return [first[0] + second[0], first[1] + second[1]];
}
function contains(list, item) {
for (const element of list) {
if (item[0] === element[0] && item[1] === element[1]) {
return true;
}
}
return false;
}
function getLegalMoves(coord, width, height, checked) {
const moves = [];
if (coord[0] > 0 && !contains(checked, [coord[0] - 1, coord[1]])) {
moves.push([-1, 0]);
}
if (coord[0] < width - 1 && !contains(checked, [coord[0] + 1, coord[1]])) {
moves.push([1, 0]);
}
if (coord[1] > 0 && !contains(checked, [coord[0], coord[1] - 1])) {
moves.push([0, -1]);
}
if (coord[1] < height - 1 && !contains(checked, [coord[0], coord[1] + 1])) {
moves.push([0, 1]);
}
moves.sort(()=>.5-Math.random())
return moves;
}
// walking algorithm using recursion
export function findLegalPaths(coord, width, height, checked) {
if (checked.length === width * height) {
return checked;
}
const legalMoves = getLegalMoves(coord, width, height, checked);
if (legalMoves.length === 0) {
return null;
}
if (paths.length >= globals.NUMBER_OF_LEVELS) {
return null;
}
for (const move of legalMoves) {
const newChecked = [];
for (const element of checked) {
newChecked.push(element);
}
const newCoord = addCord(coord, move);
newChecked.push(newCoord);
const path = findLegalPaths(newCoord, width, height, newChecked);
if (path) {
paths.push(path);
}
}
return 0;
}
export function randomPath() {
if (paths.length === 0) {
return null;
}
const index = Math.floor(Math.random() * paths.length);
return paths[index];
}
export function randomLevel(width, height) {
const path = randomPath();
const legalPaths = [];
const links = getLinks(path);
const finalPath = [];
for (let y = 0; y < height * 2; y++) {
for (let x = 1 - (y % 2); x < width * 2 - (1 - (y % 2)); x += 2) {
const coord = [x, y];
let shouldAdd = true;
for (const link of links) {
if (coord[0] === link[0] && coord[1] === link[1]) {
shouldAdd = false;
}
}
if (shouldAdd) {
legalPaths.push(coord);
}
}
}
for (const link of links) {
finalPath.push(link);
}
const shuffledPaths = legalPaths.sort(() => 0.5 - Math.random());
for (const link of shuffledPaths.slice(
0,
Math.floor(shuffledPaths.length / 2)
)) {
finalPath.push(link);
}
return [finalPath, links];
}
function getLinks(path) {
const links = [];
for (let i = 0; i < path.length - 1; i++) {
links.push(getRelativeLink(path[i], path[i + 1]));
}
return links;
}
function getRelativeLink(first, second) {
const delta = [second[0] - first[0], second[1] - first[1]];
if (delta[0] === 1) {
return calibrate(first[0], first[1] * 2);
} else if (delta[0] === -1) {
return calibrate(first[0] - 1, first[1] * 2);
}
//
else if (delta[1] === 1) {
return calibrate(first[0], second[1] * 2 - 1);
} else if (delta[1] === -1) {
return calibrate(first[0], second[1] * 2 + 1);
}
}
function calibrate(x, y) {
if (y % 2 === 1) {
// between y coords
return [x * 2, y];
} else {
// between x coord
return [x * 2 + 1, y];
}
}