-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathbunburrows.ts
More file actions
263 lines (224 loc) · 12.5 KB
/
Copy pathbunburrows.ts
File metadata and controls
263 lines (224 loc) · 12.5 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
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
252
253
254
255
256
257
258
259
260
261
262
263
// file: bunburrows.ts
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import CommonFormats, { Category } from "src/CommonFormats.ts";
import { BadMagicError, EOFError, InitializationError } from "src/errors.ts";
const COLOR_WALKABLE = [0,0,0];
const COLOR_BREAKABLE = [98,135,64];
const COLOR_UNBREAKABLE = [46,76,24];
const COLOR_BUNNY = [255,255,255];
class bunburrowsHandler implements FormatHandler {
public name: string = "bunburrows";
public supportedFormats?: FileFormat[];
public ready: boolean = false;
#canvas?: HTMLCanvasElement;
#ctx?: CanvasRenderingContext2D;
async init () {
this.supportedFormats = [
CommonFormats.PNG.supported("png", true, true, false),
{
name: "Pâquerette: Down the Bunburrows Level File",
format: "bunlevel",
extension: "level",
mime: "application/x-bunburrows-level",
from: true,
to: false,
internal: "bunlevel",
category: Category.DATA,
lossless: false,
},
];
this.#canvas = document.createElement("canvas");
this.#ctx = this.#canvas.getContext("2d") || undefined;
this.ready = true;
}
async doConvert (
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
const outputFiles: FileData[] = [];
if (!this.#canvas || !this.#ctx) {
throw new InitializationError("Handler not initialized.");
}
for (const file of inputFiles) {
let new_file_bytes = new Uint8Array(file.bytes);
// Code here based on mcmap.ts
if (inputFormat.internal === "bunlevel" && outputFormat.mime === CommonFormats.PNG.mime) {
// Read .level as text
let level_string = new TextDecoder().decode(new_file_bytes);
let level_data_array = level_string.split(/[\s,;:]+/);
console.log(String(level_data_array));
// Establish dimensions
const scale = 5;
const tiles_wide = 15;
const tiles_high = 9;
this.#canvas.width = tiles_wide*scale;
this.#canvas.height = tiles_high*scale;
// Safety check
if (level_data_array.length < tiles_wide*tiles_high) {
throw new RangeError(`Invalid level file produced bad length: ${level_data_array.length}`);
}
// Determine color per-pixel
const rgba: number[] = []
for (let i = 0; i < this.#canvas.width * this.#canvas.height; i++) {
// What pixel are we on?
const i_x = i % this.#canvas.width;
const i_y = Math.floor(i / this.#canvas.width);
// What tile are we on?
const current_tile_x = Math.floor(i_x / scale);
const current_tile_y = Math.floor(i_y / scale);
// What pixel are we on, relative to the tile?
const tile_pixel_x = i_x % scale;
const tile_pixel_y = i_y % scale;
// The string describing the current tile's data.
const current_tile_data : string = level_data_array[current_tile_y*tiles_wide + current_tile_x];
// Start drawing pixels!
console.log(current_tile_x + " , " + tile_pixel_y + " (" + i_x + " , " + i_y + ")");
let color = COLOR_BUNNY;
// Walkable tiles
if (current_tile_data.startsWith("T")) {
color = COLOR_WALKABLE;
// Burning tile
if (current_tile_data.includes("B",2)) {
if (tile_pixel_x + tile_pixel_y % 2 === 0) {
color = COLOR_UNBREAKABLE;
}
}
else {
// Undiggable tile
if (current_tile_data.includes("K",2)) {
if ((tile_pixel_x === 1 && tile_pixel_y === 0) || (tile_pixel_x === 0 && tile_pixel_y === 1) || (tile_pixel_x === 4 && tile_pixel_y === 2) || (tile_pixel_x === 3 && tile_pixel_y === 3) || (tile_pixel_x === 2 && tile_pixel_y === 4)) {
color = COLOR_UNBREAKABLE;
}
}
// Trap tile
if (current_tile_data.includes("T",2)) {
if ((tile_pixel_x === 1 && tile_pixel_y === 0) || (tile_pixel_x === 0 && tile_pixel_y === 1) || (tile_pixel_x === 4 && tile_pixel_y === 2) || (tile_pixel_x === 3 && tile_pixel_y === 3) || (tile_pixel_x === 2 && tile_pixel_y === 4)) {
color = COLOR_UNBREAKABLE;
}
}
// Carrot tile
else if (current_tile_data.includes("C",2)) {
if (tile_pixel_x === 3 && tile_pixel_y === 1) {
color = COLOR_UNBREAKABLE;
}
if ((tile_pixel_x === 2 && tile_pixel_y === 2) || (tile_pixel_x === 1 && tile_pixel_y === 3)) {
color = COLOR_BUNNY;
}
}
}
}
// Breakable walls
else if (current_tile_data.startsWith("W")) {
color = COLOR_BREAKABLE;
// Tunnels
if (current_tile_data.includes("U",2) && (tile_pixel_x === 2 && (tile_pixel_y === 0 || tile_pixel_y === 1 || tile_pixel_y === 2))) {
color = COLOR_WALKABLE;
}
if (current_tile_data.includes("D",2) && (tile_pixel_x === 2 && (tile_pixel_y === 2 || tile_pixel_y === 3 || tile_pixel_y === 4))) {
color = COLOR_WALKABLE;
}
if (current_tile_data.includes("L",2) && (tile_pixel_y === 2 && (tile_pixel_x === 0 || tile_pixel_x === 1 || tile_pixel_x === 2))) {
color = COLOR_WALKABLE;
}
if (current_tile_data.includes("R",2) && (tile_pixel_y === 2 && (tile_pixel_x === 2 || tile_pixel_x === 3 || tile_pixel_x === 4))) {
color = COLOR_WALKABLE;
}
}
// Bunnies!
else if (current_tile_data.startsWith("B")) {
color = COLOR_WALKABLE;
// Undiggable tile
if (current_tile_data.includes("K",2)) {
if ((tile_pixel_x === 1 && tile_pixel_y === 0) || (tile_pixel_x === 0 && tile_pixel_y === 1) || (tile_pixel_x === 4 && tile_pixel_y === 2) || (tile_pixel_x === 3 && tile_pixel_y === 3) || (tile_pixel_x === 2 && tile_pixel_y === 4)) {
color = COLOR_UNBREAKABLE;
}
}
if (current_tile_data.includes("B",2)) {
if (tile_pixel_x + tile_pixel_y % 2 === 0) {
color = COLOR_UNBREAKABLE;
}
}
// Draw the bunny.
if (tile_pixel_x !== 0 && tile_pixel_x !== 4 && tile_pixel_y !== 0 && tile_pixel_y !== 4 && !(tile_pixel_x === 2 && tile_pixel_y === 1)) {
color = COLOR_BUNNY;
}
}
// Unbreakable walls
else if (current_tile_data.startsWith("R")) {
color = COLOR_UNBREAKABLE;
// Tunnels
if (current_tile_data.includes("U",2) && (tile_pixel_x === 2 && (tile_pixel_y === 0 || tile_pixel_y === 1 || tile_pixel_y === 2))) {
color = COLOR_WALKABLE;
}
if (current_tile_data.includes("D",2) && (tile_pixel_x === 2 && (tile_pixel_y === 2 || tile_pixel_y === 3 || tile_pixel_y === 4))) {
color = COLOR_WALKABLE;
}
if (current_tile_data.includes("L",2) && (tile_pixel_y === 2 && (tile_pixel_x === 0 || tile_pixel_x === 1 || tile_pixel_x === 2))) {
color = COLOR_WALKABLE;
}
if (current_tile_data.includes("R",1) && (tile_pixel_y === 2 && (tile_pixel_x === 2 || tile_pixel_x === 3 || tile_pixel_x === 4))) {
color = COLOR_WALKABLE;
}
}
// Exit hole
else if (current_tile_data.startsWith("E")) {
color = COLOR_WALKABLE;
if (((tile_pixel_y === 1 || tile_pixel_y === 3) && (tile_pixel_x === 1 || tile_pixel_x === 2 || tile_pixel_x === 3)) || (tile_pixel_y === 2 && (tile_pixel_x === 0 || tile_pixel_x === 4))) {
color = COLOR_BUNNY;
}
}
// Entrance hole
else if (current_tile_data.startsWith("S")) {
color = COLOR_WALKABLE;
// Burning tile
if (current_tile_data.includes("B",2)) {
if (tile_pixel_x + tile_pixel_y % 2 === 0) {
color = COLOR_UNBREAKABLE;
}
}
// Undiggable tile
else if (current_tile_data.includes("K",2)) {
if ((tile_pixel_x === 1 && tile_pixel_y === 0) || (tile_pixel_x === 0 && tile_pixel_y === 1) || (tile_pixel_x === 4 && tile_pixel_y === 2) || (tile_pixel_x === 3 && tile_pixel_y === 3) || (tile_pixel_x === 2 && tile_pixel_y === 4)) {
color = COLOR_UNBREAKABLE;
}
}
// Draw the entrance
if (((tile_pixel_y === 1 || tile_pixel_y === 3) && (tile_pixel_x === 1 || tile_pixel_x === 3)) || (tile_pixel_y === 2 && (tile_pixel_x === 0 || tile_pixel_x === 2 || tile_pixel_x === 4))) {
color = COLOR_BUNNY;
}
}
// Elevator
else if (current_tile_data.startsWith("!")) {
color = COLOR_BUNNY;
if (tile_pixel_y === 0 && (tile_pixel_x === 0 || tile_pixel_x === 4)) {
color = COLOR_WALKABLE;
}
if ((tile_pixel_x === 1 || tile_pixel_x === 3) || (tile_pixel_y === 2 || tile_pixel_y === 3 || tile_pixel_y === 4)) {
color = COLOR_UNBREAKABLE;
}
}
rgba.push(...color, 255);
}
// Writes our results to the canvas
const image_data = new ImageData(new Uint8ClampedArray(rgba), this.#canvas.width, this.#canvas.height);
this.#ctx.putImageData(image_data, 0, 0);
new_file_bytes = await new Promise((resolve, reject) => {
this.#canvas!.toBlob((blob) => {
if (!blob) return reject("Canvas output failed");
blob.arrayBuffer().then(buf => resolve(new Uint8Array(buf)));
}, outputFormat.mime);
});
}
else {
throw new TypeError(`Unsupported conversion path: ${inputFormat.internal} -> ${outputFormat.internal}`);
}
outputFiles.push({
name: file.name.split(".").slice(0, -1).join(".") + "." + outputFormat.extension,
bytes: new_file_bytes
})
}
return outputFiles;
}
}
export default bunburrowsHandler;