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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
import { Wall, ItemType, Map, Maps, Items, Tile, SpawnIndex, Vec2 } from "./types.js"
import { LZString } from "./lib/lz-string.js"
export const getItemKey = (
x: number,
y: number,
w: number
): number => {
let nx = Math.round(x * 2)
let ny = Math.round(y * 2)
let key = ny * w * 2 + nx
return key
}
const getPoint = (
width: number,
height: number,
data: number[],
x: number,
y: number
): number => {
if (x < 0 || x >= width || y < 0 || y >= height) {
return 0
} else {
return data[y * width + x]
}
}
const genWalls = (
width: number,
height: number,
data: number[]
): number[] => {
let walls = Array(width * height)
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
let north = getPoint(width, height, data, x, y-1) == 1
let south = getPoint(width, height, data, x, y+1) == 1
let east = getPoint(width, height, data, x+1, y) == 1
let west = getPoint(width, height, data, x-1, y) == 1
let current = getPoint(width, height, data, x, y) == 1
let point = Wall.EMPTY
if (!current) {
walls[y * width + x] = point
continue
}
if (north && south && east && west) {
point = Wall.CROSS
} else if (east && west && north) {
point = Wall.TEE_NORTH
} else if (east && west && south) {
point = Wall.TEE_SOUTH
} else if (north && south && east) {
point = Wall.TEE_EAST
} else if (north && south && west) {
point = Wall.TEE_WEST
} else if (east && west) {
point = Wall.WALL_HZ
} else if (north && south) {
point = Wall.WALL_VT
} else if (west && south) {
point = Wall.TURN_Q1
} else if (south && east) {
point = Wall.TURN_Q2
} else if (east && north) {
point = Wall.TURN_Q3
} else if (north && west) {
point = Wall.TURN_Q4
} else if (north) {
point = Wall.WALL_END_NORTH
} else if (east) {
point = Wall.WALL_END_EAST
} else if (south) {
point = Wall.WALL_END_SOUTH
} else if (west) {
point = Wall.WALL_END_WEST
} else {
point = Wall.DOT
}
walls[y * width + x] = point
}
}
return walls
}
const canItem = (tile: Tile): boolean => tile != Tile.WALL && tile != Tile.GHOST_WALL && tile != Tile.GHOST_SPAWN
export const genItems = (map: Map): Items => {
let width = map.width
let height = map.height
let data = map.data
let items: Items = {}
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
let tile = getPoint(width, height, data, x, y)
if (!canItem(tile)) continue
let item_key = getItemKey(x, y, width)
let type: ItemType
if (tile == Tile.FOOD)
type = ItemType.FOOD
else if (tile == Tile.THICC_DOT)
type = ItemType.THICC_DOT
else
type = ItemType.DOT
items[item_key] = {type, pos: {x, y}}
if (type == ItemType.DOT || type == ItemType.THICC_DOT) {
type = ItemType.DOT
let tile_south = getPoint(width, height, data, x, y + 1)
if (canItem(tile_south) && tile_south != Tile.FOOD) {
item_key = getItemKey(x, y + .5, width)
items[item_key] = {type, pos: {x, y: y + .5}}
}
let tile_east = getPoint(width, height, data, x + 1, y)
if (canItem(tile_east) && tile_east != Tile.FOOD) {
item_key = getItemKey(x + .5, y, width)
items[item_key] = {type, pos: {x: x + .5, y}}
}
}
}
}
return items
}
let mapData: Maps = {}
export const genMap = (
width: number,
height: number,
data: number[],
mapId: number,
): Map => {
mapData[mapId] = {
data: structuredClone(data),
walls: genWalls(width, height, data),
width,
height,
id: mapId
}
return mapData[mapId]
}
export const getMap = (mapId: number): Map | undefined => {
if (mapId == undefined) return undefined
return mapData[mapId]
}
export const checkMap = (map: Map): [boolean, string | Vec2[]] => {
let spawns = new Array(5).fill(undefined)
let hasFood = false
let hasThicc = false
let hasInitial = false
if (map.width < 5 || map.height < 5 || map.width > 50 || map.height > 50) {
return [false, "Map but be between either 5 or 50 on both axies"]
}
for (let y = 0; y < map.height; y++) {
for (let x = 0; x < map.width; x++) {
let type = map.data[y * map.width + x]
switch (type) {
case Tile.FOOD:
hasFood = true
break
case Tile.THICC_DOT:
hasThicc = true
break
case Tile.INITIAL_DOT:
hasInitial = true
break
case Tile.PLAYER_SPAWN_1:
if (spawns[SpawnIndex.PAC_SPAWN_1])
return [false, "Map cannot have duplicate spawns"]
spawns[SpawnIndex.PAC_SPAWN_1] = {x, y}
break
case Tile.PLAYER_SPAWN_2:
if (spawns[SpawnIndex.PAC_SPAWN_2])
return [false, "Map cannot have duplicate spawns"]
spawns[SpawnIndex.PAC_SPAWN_2] = {x, y}
break
case Tile.PLAYER_SPAWN_3:
if (spawns[SpawnIndex.PAC_SPAWN_3])
return [false, "Map cannot have duplicate spawns"]
spawns[SpawnIndex.PAC_SPAWN_3] = {x, y}
break
case Tile.PLAYER_SPAWN_4:
if (spawns[SpawnIndex.PAC_SPAWN_4])
return [false, "Map cannot have duplicate spawns"]
spawns[SpawnIndex.PAC_SPAWN_4] = {x, y}
break
case Tile.GHOST_SPAWN:
if (spawns[SpawnIndex.GHOST_SPAWN])
return [false, "Map cannot have duplicate spawns"]
spawns[SpawnIndex.GHOST_SPAWN] = {x, y}
break
}
}
}
if (!hasFood)
return [false, "Map must have at least 1 food"]
if (!hasThicc)
return [false, "Map must have at least 1 thicc dot"]
if (!hasInitial)
return [false, "Map must have at least 1 initial dot"]
if (spawns.filter(s => s === undefined).length > 0)
return [false, "Map must have 4 pac spawns and 1 ghost spawn"]
return [true, spawns]
}
export const compressMap = (map: Map): string => {
let encoded = map.width + '|' + map.height + '|' + map.data.map(n => n + ',').join('').slice(0, -1)
return LZString.compressToBase64(encoded)
}
export const decompressMap = (encoded: string): {width: number, height: number, data: number[]} => {
let decoded = LZString.decompressFromBase64(encoded)
let values = decoded.split('|')
let width = parseInt(values[0])
let height = parseInt(values[1])
let data = values[2].split(',').map(c => parseInt(c))
return {width, height, data}
}
|