-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgen.ts
171 lines (153 loc) · 4.15 KB
/
gen.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Generates a map
*/
import { GameMap, EMPTY, WALL, MOVE_DELTAS } from "."
import { HideAndSeekConfigs, DIRECTION } from "..";
import seedrandom from 'seedrandom';
export enum SYMMETRY {
HORIZONTAL,
VERTICAL
}
export const mapGen = (width: number, height: number, configs: HideAndSeekConfigs, rng: () => number): GameMap => {
let map = new GameMap(width, height, configs);
// vary density by 0.2
let DENSITY = configs.parameters.DENSITY - 0.08 + 0.16 * rng();
let SeekerCount = Math.floor(rng() * configs.parameters.SEEKER_MAX + 1);
let symmetry = SYMMETRY.HORIZONTAL;
if (rng() < 0.5) {
symmetry = SYMMETRY.VERTICAL;
}
let height2 = height;
let width2 = width;
if (symmetry === SYMMETRY.VERTICAL) {
height2 /= 2;
for (let i = 0; i < SeekerCount; i++) {
let x = Math.floor(rng() * (width2 - 1));
let y = Math.floor(rng() * (height2 - 4));
map.spawnSeeker(x, y);
map.spawnHider(x, height - y - 1);
}
}
else if (symmetry === SYMMETRY.HORIZONTAL) {
width2 /= 2;
for (let i = 0; i < SeekerCount; i++) {
let x = Math.floor(rng() * (width2 - 4));
let y = Math.floor(rng() * (height2 - 1));
map.spawnSeeker(x, y);
map.spawnHider(width - x - 1, y);
}
}
// using game of life to randomly generate half a map
let arr = [];
for (let y = 0; y < height2; y++) {
arr.push([]);
for (let x = 0; x < width2; x++) {
let type = EMPTY;
if (!map.hasUnit(x, y) &&
!map.hasUnit(x + 1, y) &&
!map.hasUnit(x - 1, y) &&
!map.hasUnit(x, y + 1) &&
!map.hasUnit(x, y - 1)
) {
if (rng() < DENSITY) {
type = WALL;
}
}
arr[y].push(type);
}
}
// simulate GOL for 2 rounds
for (let i = 0; i < 2; i++) {
simulate(arr);
}
for (let y = 0; y < height2; y++) {
for (let x = 0; x < width2; x++) {
if (arr[y][x] === WALL && map.map[y][x] === EMPTY) {
map.setWall(x, y);
if (symmetry === SYMMETRY.HORIZONTAL) {
map.setWall(width - x - 1, y);
}
else {
map.setWall(x, height - y - 1);
}
}
}
}
// If invalid map, reroll a map
if (!validateMap(map)) {
return mapGen(width, height, configs, rng);
}
return map;
}
/**
* Checks if the game is winnable by seekers and hiders. Performs a BFS looking
* for the other units
* @param map
*/
const validateMap = (map: GameMap) => {
let unit = Array.from(map.idMap.values())[0];
// bfs from that unit
let unitsReached = new Set();
let visitedSet = new Set();
let queue = [{x: unit.x, y: unit.y}];
unitsReached.add(unit.id);
while (queue.length) {
if (unitsReached.size === map.idMap.size) {
return true;
}
else {
let {x, y} = queue.pop();
visitedSet.add(map.hashLoc(x, y));
if (map.hasUnit(x, y)) {
unitsReached.add(map.map[y][x]);
}
for (let i = 0; i < MOVE_DELTAS.length; i++) {
let delta = MOVE_DELTAS[i];
let nx = x + delta[0];
let ny = y + delta[1];
let hash = map.hashLoc(nx, ny);
if (!visitedSet.has(hash) && map.inMap(nx, ny) && !map.isWall(nx, ny)) {
queue.push({x: nx, y: ny});
}
}
}
}
return false;
}
const simulate = (arr: Array<Array<number>>) => {
let padding = 2;
let deathLimit = 2;
let birthLimit = 4;
for (let i = padding; i < arr.length - padding; i++) {
for (let j = padding; j < arr[0].length - padding; j++) {
let cell = arr[i][j];
let alive = 0;
for (let i = 0; i < MOVE_DELTAS.length; i++) {
let delta = MOVE_DELTAS[i];
let ny= i + delta[1];
let nx =j + delta[0];
if (nx < 0 || ny < 0 || nx >= arr[0].length || ny >= arr.length) {
}
else if (arr[ny][nx] === WALL) {
alive++;
}
}
if (arr[i][j] == WALL) {
if (alive < deathLimit) {
arr[i][j] = EMPTY;
}
else {
arr[i][j] = WALL;
}
}
else {
if (alive > birthLimit) {
arr[i][j] = WALL;
}
else {
arr[i][j] = EMPTY;
}
}
}
}
}