-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathcelariaMap.ts
More file actions
398 lines (382 loc) · 12.9 KB
/
celariaMap.ts
File metadata and controls
398 lines (382 loc) · 12.9 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import { CelariaMap } from "celaria-formats/class/maps/CelariaMap.mjs"
import { EditableCelariaMap } from "celaria-formats/class/maps/EditableCelariaMap.mjs"
import { Barrier } from "celaria-formats/class/maps/objects/Barrier.mjs"
import { Block } from "celaria-formats/class/maps/objects/Block.mjs"
import { Instance } from "celaria-formats/class/maps/objects/Instance.mjs"
import { PlayerSpawnPoint } from "celaria-formats/class/maps/objects/PlayerSpawnPoint.mjs"
import { Sphere } from "celaria-formats/class/maps/objects/Sphere.mjs"
import { TutorialHologram } from "celaria-formats/class/maps/objects/TutorialHologram.mjs"
import type { FlatVector3, Vector3 } from "celaria-formats/types/data.mts"
import CommonFormats from "src/CommonFormats.ts"
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts"
class celariaMapHandler implements FormatHandler {
public name: string = "celariaMap"
public supportedFormats?: FileFormat[]
public ready: boolean = false
/**/
async init() {
this.supportedFormats = [
{
name: "Wavefront OBJ",
format: "obj",
extension: "obj",
mime: "model/obj",
from: false,
to: true,
internal: "obj",
category: "model",
lossless: false,
},
CommonFormats.JSON.builder("json").allowFrom(true).allowTo(true).markLossless(false),
{
name: "Editable Celaria Map",
format: "ecmap",
extension: "ecmap",
mime: "application/x-editable-celaria-map",
from: true,
to: true,
internal: "ecmap",
category: "data",
lossless: false,
},
{
name: "Celaria Map",
format: "cmap",
extension: "cmap",
mime: "application/x-celaria-map",
from: true,
to: false,
internal: "cmap",
category: "data",
lossless: false,
},
]
this.ready = true
}
async doConvert(inputFiles: FileData[], inputFormat: FileFormat, outputFormat: FileFormat): Promise<FileData[]> {
return inputFiles.map((file) => {
const baseName = file.name.replace(/\.[^.]+$/u, "") // lifted from json5 handler.
if (inputFormat.internal === "ecmap") {
const editableCelariaMap = EditableCelariaMap.parse(Buffer.from(file.bytes))
if (outputFormat.internal === "json") {
const object: PlainOldEditableCelariaMap = {
mapType: "Editable Celaria map",
instances: [],
sunRotationHorizontal: editableCelariaMap.sunRotationHorizontal,
sunRotationVertical: editableCelariaMap.sunRotationVertical,
name: editableCelariaMap.name,
previewCamera: editableCelariaMap.previewCamera,
}
object.instances = editableCelariaMap.instances.map((instance) => {
switch (instance.instanceId) {
case 0: // Block
return {
scale: instance.scale,
type: instance.type,
rotation: instance.rotation,
instanceType: "Block",
position: instance.position,
}
case 1: // Sphere
return {
instanceType: "Sphere",
position: instance.position,
}
case 2: // Player spawn point
return {
rotation: instance.rotation,
instanceType: "Spawn point",
position: instance.position,
}
case 3: // Barrier (Wall)
case 4: // Barrier (Floor)
return {
scale: instance.scale,
rotation: instance.rotation,
instanceType: "Barrier",
position: instance.position,
}
case 128:
return {
scale: instance.scale,
type: instance.type,
instanceType: "Dummy",
rotation: instance.rotation,
position: instance.position,
}
}
})
return {
name: `${baseName}.${outputFormat.extension}`,
bytes: new TextEncoder().encode(JSON.stringify(object)),
}
}
if (outputFormat.internal === "obj") {
const modelBuilder = new ModelBuilder(editableCelariaMap.instances)
modelBuilder.do()
return {
name: `${baseName}.${outputFormat.extension}`,
bytes: new TextEncoder().encode(modelBuilder.toString()),
}
}
}
if (inputFormat.internal === "json") {
const typedParsedObject = celariaMapHandler.determinePlainOldSerializedFormat(JSON.parse(new TextDecoder().decode(file.bytes)))
if (!typedParsedObject) throw new Error("Can't handle unknown parsed object.")
function putInstances(map: EditableCelariaMap | CelariaMap, plainOldInstances: AllPlainOldInstanceTypes[]) {
map.instances = plainOldInstances.map((plainOldInstance) => {
switch (plainOldInstance.instanceType) {
case "Block":
const block = new Block(plainOldInstance.type)
block.position = plainOldInstance.position
block.rotation = plainOldInstance.rotation
block.scale = plainOldInstance.scale
block.type = plainOldInstance.type
return block
case "Barrier":
const barrier = new Barrier()
barrier.position = plainOldInstance.position
barrier.rotation = plainOldInstance.rotation
barrier.scale = plainOldInstance.scale
return barrier
case "Spawn point":
const playerSpawnPoint = new PlayerSpawnPoint()
playerSpawnPoint.position = plainOldInstance.position
playerSpawnPoint.rotation = plainOldInstance.rotation
return playerSpawnPoint
case "Dummy":
const tutorialHologram = new TutorialHologram(plainOldInstance.type)
tutorialHologram.scale = plainOldInstance.scale
tutorialHologram.rotation = plainOldInstance.rotation
tutorialHologram.position = plainOldInstance.position
return tutorialHologram
case "Sphere":
const sphere = new Sphere()
sphere.position = plainOldInstance.position
return sphere
}
})
}
if (outputFormat.internal === "ecmap") {
const editableCelariaMap = new EditableCelariaMap()
putInstances(editableCelariaMap, typedParsedObject.instances)
const suitableVersion = celariaMapHandler.determineSuitableVersion("ecmap", editableCelariaMap.instances)
editableCelariaMap.previewCamera = typedParsedObject.previewCamera
editableCelariaMap.name = typedParsedObject.name
editableCelariaMap.sunRotationHorizontal = typedParsedObject.sunRotationHorizontal
editableCelariaMap.sunRotationVertical = typedParsedObject.sunRotationVertical
editableCelariaMap.instances.forEach((instance) => {
if (instance.instanceId === 0 && instance.type === Block.types.checkpoint) editableCelariaMap.checkpointOrder.add(instance)
})
const goal = editableCelariaMap.instances.find((instance) => instance.instanceId === 0 && instance.type === Block.types.goal) as Block | undefined
if (goal) editableCelariaMap.checkpointOrder.add(goal)
return {
name: `${baseName}.${outputFormat.extension}`,
bytes: Uint8Array.from(editableCelariaMap.serialize(suitableVersion)),
}
}
}
if (inputFormat.internal === "cmap" && outputFormat.internal === "ecmap") {
const celariaMap = CelariaMap.parse(Buffer.from(file.bytes))
const editableCelariaMap = new EditableCelariaMap()
editableCelariaMap.checkpointOrder = celariaMap.checkpointOrder
editableCelariaMap.instances = celariaMap.instances
editableCelariaMap.name = celariaMap.name
editableCelariaMap.previewCamera = celariaMap.previewCamera
editableCelariaMap.sunRotationHorizontal = celariaMap.sunRotationHorizontal
editableCelariaMap.sunRotationVertical = celariaMap.sunRotationVertical
const suitableVersion = celariaMapHandler.determineSuitableVersion("ecmap", editableCelariaMap.instances)
return {
name: `${baseName}.${outputFormat.extension}`,
bytes: Uint8Array.from(editableCelariaMap.serialize(suitableVersion)),
}
}
throw new Error("Unsupported input-output.")
})
}
/** Determines what a plain old JavaScript object is _likely_ to be. I don't do strict validation. */
static determinePlainOldSerializedFormat(plainOldObject: any): false | PlainOldEditableCelariaMap {
if (typeof plainOldObject !== "object") return false
if (Array.isArray(plainOldObject)) return false
// Check what the object identifies itself.
if (plainOldObject.mapType === "Editable Celaria map") return plainOldObject as PlainOldEditableCelariaMap
if (plainOldObject.mapType === "Celaria map") throw new Error("Not implemented.")
return false
}
/** Determine a version most suitable for compatibility based on the given {@link instances}. Prefers versions for the latest available free open alpha version. Otherwise uses latest commercial Steam release when encountering new objects like {@link Barrier}s. */
static determineSuitableVersion(format: "ecmap" | "cmap", instances: Instance[]): number {
const containsBarriers = instances.some((instance) => instance.instanceId == 3 || instance.instanceId == 4)
if (format === "ecmap") {
if (containsBarriers) {
return 4
} else {
return 2
}
}
throw new Error("Unsupported format.")
}
}
/** I help build a Wavefront OBJ of a map. */
class ModelBuilder {
objString: string
vertexCount: number
instances: AllInstanceTypes[]
/**/
constructor(instances: AllInstanceTypes[]) {
this.objString = ""
this.vertexCount = 1
this.instances = instances
}
do() {
this.instances
.filter((instance) => instance.instanceId === 0)
.forEach((block) => {
this.appendBlock(block)
})
}
appendBlock(instance: Block) {
Object.values(ModelBuilder.directions).forEach((face) => {
const radians = ModelBuilder.degreesToRadians(-instance.rotation)
for (let i = 0; i < face.geometry.length / 3; i++) {
const vertexOffset = i * 3
;[face.geometry[vertexOffset], face.geometry[vertexOffset + 1], face.geometry[vertexOffset + 2]].forEach((vertex) => {
const result = []
// scale and offset from base cube geometry.
const scaledVertex = vertex.map((vertexComponent, componentIndex) => (vertexComponent - 0.5) * instance.scale[componentIndex])
// perform rotation
result[0] = scaledVertex[0] * Math.cos(radians) - scaledVertex[1] * Math.sin(radians)
result[1] = scaledVertex[0] * Math.sin(radians) + scaledVertex[1] * Math.cos(radians)
result[2] = scaledVertex[2]
// add vertex
this.objString += `v ${result.map((vertexComponent, componentIndex) => vertexComponent + instance.position[componentIndex]).join(" ")}\n`
})
// add face definition
this.objString += `f ${this.vertexCount}/${this.vertexCount} ${this.vertexCount + 1}/${this.vertexCount + 1} ${this.vertexCount + 2}/${this.vertexCount + 2}\n`
this.vertexCount += 3
}
})
}
/** Solves the most curious mathematical problem. */
static degreesToRadians(degrees: number) {
const conversionConstant = 57.2957795
return degrees / conversionConstant
}
toString() {
return this.objString
}
static directions = {
yNegative: {
geometry: [
[0, 0, 1],
[0, 0, 0],
[1, 0, 0],
[1, 0, 1],
[0, 0, 1],
[1, 0, 0],
],
faceNormal: [0, -1, 0],
},
yPositive: {
geometry: [
[1, 1, 0],
[0, 1, 0],
[0, 1, 1],
[1, 1, 1],
[1, 1, 0],
[0, 1, 1],
],
faceNormal: [0, 1, 0],
},
zNegative: {
geometry: [
[1, 0, 0],
[0, 0, 0],
[0, 1, 0],
[1, 1, 0],
[1, 0, 0],
[0, 1, 0],
],
faceNormal: [0, 0, -1],
},
zPositive: {
geometry: [
[0, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 1, 1],
[0, 0, 1],
[1, 1, 1],
],
faceNormal: [0, 0, 1],
},
xNegative: {
geometry: [
[0, 0, 0],
[0, 0, 1],
[0, 1, 1],
[0, 1, 0],
[0, 0, 0],
[0, 1, 1],
],
faceNormal: [-1, 0, 0],
},
xPositive: {
geometry: [
[1, 0, 1],
[1, 0, 0],
[1, 1, 0],
[1, 1, 1],
[1, 0, 1],
[1, 1, 0],
],
faceNormal: [1, 0, 0],
},
} as const
}
// #region Types
type AllInstanceTypes = Block | PlayerSpawnPoint | Sphere | TutorialHologram | Barrier
type AllPlainOldInstanceTypes = PlainOldBlock | PlainOldBarrier | PlainOldPlayerSpawnPoint | PlainOldTutorialHologram | PlainOldSphere
interface PlainOldBaseCelariaMap {
mapType: string
instances: AllPlainOldInstanceTypes[]
sunRotationHorizontal: number
sunRotationVertical: number
previewCamera: {
from: Vector3
to: Vector3
}
name: string
}
/** I'm a plain old JavaScript object for an editable Celaria map. */
interface PlainOldEditableCelariaMap extends PlainOldBaseCelariaMap {
mapType: "Editable Celaria map"
}
interface PlainOldInstance {
position: Vector3
instanceType: string
}
interface PlainOldBlock extends PlainOldInstance {
type: number
rotation: number
scale: Vector3
instanceType: "Block"
}
interface PlainOldSphere extends PlainOldInstance {
instanceType: "Sphere"
}
interface PlainOldPlayerSpawnPoint extends PlainOldInstance {
rotation: number
instanceType: "Spawn point"
}
interface PlainOldBarrier extends PlainOldInstance {
rotation: number
scale: FlatVector3
instanceType: "Barrier"
}
interface PlainOldTutorialHologram extends PlainOldInstance {
type: number
scale: Vector3
rotation: number
instanceType: "Dummy"
}
export default celariaMapHandler