-
Notifications
You must be signed in to change notification settings - Fork 0
/
implementation5.ts
120 lines (107 loc) · 2.99 KB
/
implementation5.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
export interface MapNode {
x: number;
y: number;
type?: number;
}
//let middle: number; // half of the diameter of the board
let width: number;
let height: number;
let roomCount: number;
let rooms: Array<Array<number>>;
let taken: Array<MapNode>;
const up = ({x, y}: MapNode) => ({x: x-1, y});
const right = ({x, y}: MapNode) => ({x, y: y+1});
const left = ({x, y}: MapNode) => ({x, y: y-1});
const bottom = ({x, y}: MapNode) => ({x: x+1, y});
export function makeMap(newCount = 10, newHeight = 10, newWidth = 10) {
taken = [];
roomCount = newCount;
height = newHeight;
width = newWidth;
//middle = newHalf;
return initBoard();
}
function initBoard() {
// set a maximum roomCount
if (roomCount > width * 2 || roomCount > height * 2) {
roomCount = Math.round(((width * 2) + (height * 2)) / 2);
}
// create and fill array;
rooms = new Array(height * 2).fill(null).map(() => new Array(width * 2).fill(null));
return initMap();
}
function initMap() {
// set middle to number 0;
rooms[height][width] = 0;
taken.push({x: height, y: width});
return createRooms();
}
function createRooms() {
for (let i = 0; i < roomCount; i++) {
const randomBranching = i / roomCount;
const random = (0.2 * randomBranching) + 0.1;
let pos = newPosition();
if(neighborNumber(pos) > 1 && Math.random() > random) {
let iterations = 0;
do {
pos = newPosition(true);
iterations++;
} while (neighborNumber(pos) > 1 && iterations < roomCount);
}
rooms[pos.x][pos.y] = 1;
taken.push(pos);
}
return rooms;
}
function newPosition(selective?: boolean) {
let x = 0, y = 0;
let pos = {x: height, y: width};
do {
let index = Math.floor(Math.random() * taken.length);
if(selective){
let iterations = 0;
while(neighborNumber(taken[index]) > 1 && iterations < roomCount){
index = Math.floor(Math.random() * taken.length);
iterations++;
}
}
x = taken[index].x;
y = taken[index].y;
let up = Math.random() < .5;
let right = Math.random() < .5;
if (up) {
if (right) {
y += 1;
} else {
y -= 1;
}
} else {
if (right) {
x += 1;
} else {
x -= 1;
}
}
pos = {x,y}
} while (alreadyTaken(pos) || x >= height * 2 || x < 0 || y >= width * 2 || y < 0);
return pos;
}
function neighborNumber({x,y}: MapNode) {
let ret = 0
if(alreadyTaken(up({x,y}))){
ret++;
}
if(alreadyTaken(right({x,y}))){
ret++;
}
if(alreadyTaken(bottom({x,y}))){
ret++;
}
if(alreadyTaken(left({x,y}))){
ret++;
}
return ret;
}
function alreadyTaken({x,y}: MapNode) {
return !!taken.find((v) => (v.x === x && v.y === y));
}