-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
137 lines (119 loc) · 3.28 KB
/
index.ts
File metadata and controls
137 lines (119 loc) · 3.28 KB
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
import Dungeon from "./src/dungeon";
const defaultTiles = {
floor: "⛶",
path: "·",
wall: " ",
door: "#",
};
const defaultOptions = {
maxH: 50,
maxW: 50,
seed: "purukitto",
type: "Base",
roomTries: 150,
extraRoomSize: 0,
windingPercent: 50,
tiles: defaultTiles,
startIndex: 1,
};
/**
* @function simpleDungeon
* @param {GeneratorOptions} options
*
* @returns {Dungeon}
* @description
* Generates a simple dungeon
*
* Based on https://journal.stuffwithstuff.com/2014/12/21/rooms-and-mazes/
*/
function simpleDungeon(options?: GeneratorOptions) {
if (!options) options = defaultOptions;
// Options for the dungeon generator
const maxH =
typeof options.maxH != "undefined" ? options.maxH : defaultOptions.maxH; // Max height
if (maxH < 5) throw new Error("maxH must be greater than 5");
const maxW =
typeof options.maxW != "undefined" ? options.maxW : defaultOptions.maxW; // Max width
if (maxW < 5) throw new Error("maxW must be greater than 5");
const seed =
typeof options.seed !== "undefined"
? options.seed.toString()
: defaultOptions.seed; // Seed
const type =
typeof options.type !== "undefined"
? options.type
: defaultOptions.type; // Type of dungeon //TODO: Implement dungeon types
const roomTries =
typeof options.roomTries !== "undefined"
? options.roomTries
: defaultOptions.roomTries; // Number of times to try to place a room
if (roomTries <= 0) throw new Error("roomTries must be greater than 0");
const extraRoomSize =
typeof options.extraRoomSize !== "undefined"
? options.extraRoomSize
: defaultOptions.extraRoomSize; // Allows rooms to be larger
if (extraRoomSize < 0)
throw new Error("extraRoomSize must be greater than or equal to 0");
if (extraRoomSize > Math.floor(Math.min(maxH, maxW) / 5))
console.warn(
`extraRoomSize is too large for current dungeon, suggested setting this to under ${Math.floor(
Math.min(maxH, maxW) / 5
)}`
);
const windingPercent =
typeof options.windingPercent !== "undefined"
? options.windingPercent
: defaultOptions.windingPercent; // Chance to add winding paths between rooms
if (windingPercent < 0 || windingPercent > 100)
throw new Error("windingPercent must be between 0 and 100");
const tiles =
typeof options.tiles !== "undefined"
? { ...defaultTiles, ...options.tiles }
: defaultTiles; // Tiles to use
const startIndex =
typeof options.startIndex !== "undefined"
? options.startIndex
: defaultOptions.startIndex; // Index to start at
// Create and return dungeon object
return new Dungeon(
maxH,
maxW,
seed,
roomTries,
extraRoomSize,
windingPercent,
tiles,
startIndex
);
}
/**
* @typedef {Object} GeneratorOptions
* @property {string} seed
* @property {number} maxH
* @property {number} maxW
* @property {string} type
* @property {number} roomTries
* @property {number} extraRoomSize
* @property {number} windingPercent
* @property {Object} tiles
* @property {number} startIndex
* @property {boolean} doors
*/
type GeneratorOptions = {
seed?: string;
maxH?: number;
maxW?: number;
type?: string;
roomTries?: number;
extraRoomSize?: number;
windingPercent?: number;
tiles?: {
floor: string;
path: string;
wall: string;
door: string;
};
startIndex?: number;
};
export { GeneratorOptions };
export default simpleDungeon;