-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
541 lines (436 loc) · 11 KB
/
index.js
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
'use strict';
/*
* Buffer with a cursor and array extensions.
*/
var SolidCursor = require('cursor').extend({
readFloatLEArray: function (length) {
var value = new Float32Array(length);
for (var i = 0; i < length; ++i) {
value[i] = this.readFloatLE();
}
return value;
},
readInt32LEArray: function (length) {
var value = new Int32Array(length);
for (var i = 0; i < length; ++i) {
value[i] = this.readInt32LE();
}
return value;
}
});
/*
* Neverball SOL loader.
*/
var Solid = module.exports = loadSol;
Solid.MAGIC = 0x4c4f53af;
Solid.VERSIONS = [7, 8, 9, 10];
/*
* Material type flags.
*/
Solid.MTRL_LIT = (1 << 11);
Solid.MTRL_PARTICLE = (1 << 10);
Solid.MTRL_ALPHA_TEST = (1 << 9);
Solid.MTRL_REFLECTIVE = (1 << 8);
Solid.MTRL_TRANSPARENT = (1 << 7);
Solid.MTRL_SHADOWED = (1 << 6);
Solid.MTRL_DECAL = (1 << 5);
Solid.MTRL_ENVIRONMENT = (1 << 4);
Solid.MTRL_TWO_SIDED = (1 << 3);
Solid.MTRL_ADDITIVE = (1 << 2);
Solid.MTRL_CLAMP_S = (1 << 1);
Solid.MTRL_CLAMP_T = (1 << 0);
/*
* Billboard flags.
*/
Solid.BILL_EDGE = 1;
Solid.BILL_FLAT = 2;
Solid.BILL_NOFACE = 4;
/*
* Lump flags.
*/
Solid.LUMP_DETAIL = 1;
/*
* Item types.
*/
Solid.ITEM_COIN = 1;
Solid.ITEM_GROW = 2;
Solid.ITEM_SHRINK = 3;
/*
* Path flags.
*/
Solid.PATH_ORIENTED = 1;
Solid.PATH_PARENTED = 2;
/*
* Load a SOL file from the given ArrayBuffer.
*/
function loadSol (buffer) {
var stream = SolidCursor(buffer);
var magic = stream.readInt32LE();
if (magic !== Solid.MAGIC) {
throw Error('Not a SOL file');
}
var version = stream.readInt32LE();
if (!Solid.VERSIONS.includes(version)) {
throw Error('SOL version ' + version + ' is not supported');
}
var ac = stream.readInt32LE();
var dc = stream.readInt32LE();
var mc = stream.readInt32LE();
var vc = stream.readInt32LE();
var ec = stream.readInt32LE();
var sc = stream.readInt32LE();
var tc = stream.readInt32LE();
var oc = stream.readInt32LE();
var gc = stream.readInt32LE();
var lc = stream.readInt32LE();
var nc = stream.readInt32LE();
var pc = stream.readInt32LE();
var bc = stream.readInt32LE();
var hc = stream.readInt32LE();
var zc = stream.readInt32LE();
var jc = stream.readInt32LE();
var xc = stream.readInt32LE();
var rc = stream.readInt32LE();
var uc = stream.readInt32LE();
var wc = stream.readInt32LE();
var ic = stream.readInt32LE();
var sol = {};
sol.version = version;
sol.av = sol.bytes = Buffer.from(stream.slice(ac).buffer()); // Realloc.
sol.dv = sol.dicts = loadDicts(stream, dc, sol.av);
sol.mv = sol.mtrls = loadMtrls(stream, mc);
sol.vv = sol.verts = loadVerts(stream, vc);
sol.ev = sol.edges = loadEdges(stream, ec);
sol.sv = sol.sides = loadSides(stream, sc);
sol.tv = sol.texcs = loadTexcs(stream, tc);
sol.ov = sol.offs = loadOffs(stream, oc);
sol.gv = sol.geoms = loadGeoms(stream, gc);
sol.lv = sol.lumps = loadLumps(stream, lc);
sol.nv = sol.nodes = loadNodes(stream, nc);
sol.pv = sol.paths = loadPaths(stream, pc);
sol.bv = sol.bodies = loadBodies(stream, bc);
sol.hv = sol.items = loadItems(version, stream, hc);
sol.zv = sol.goals = loadGoals(version, stream, zc);
sol.jv = sol.jumps = loadJumps(version, stream, jc);
sol.xv = sol.switches = loadSwitches(version, stream, xc);
sol.rv = sol.bills = loadBills(version, stream, rc);
sol.uv = sol.balls = loadBalls(stream, uc);
sol.wv = sol.views = loadViews(stream, wc);
sol.iv = sol.indices = stream.readInt32LEArray(ic);
if (sol.version <= 7) {
var i;
for (i = 0; i < sol.mv.length; ++i) {
sol.mv[i].fl |= Solid.MTRL_LIT;
}
for (i = 0; i < sol.rv.length; ++i) {
sol.mv[sol.rv[i].mi].fl &= ~Solid.MTRL_LIT;
}
}
return sol;
}
function loadDicts (stream, count, byteBuffer) {
var dicts = {};
for (var i = 0; i < count; ++i) {
var ai = stream.readInt32LE();
var aj = stream.readInt32LE();
var key = byteBuffer.toString('utf8', ai, byteBuffer.indexOf(0, ai));
var val = byteBuffer.toString('utf8', aj, byteBuffer.indexOf(0, aj));
if (key === 'message') {
val = val.replace(/\\/g, '\n');
}
dicts[key] = val;
}
return dicts;
}
function loadMtrls (stream, count) {
var mtrls = [];
for (var i = 0; i < count; ++i) {
var mtrl = {
d: stream.readFloatLEArray(4),
a: stream.readFloatLEArray(4),
s: stream.readFloatLEArray(4),
e: stream.readFloatLEArray(4),
h: stream.readFloatLE(),
fl: stream.readInt32LE()
};
var byteBuffer = stream.slice(64).buffer();
mtrl.f = byteBuffer.toString('utf8', 0, byteBuffer.indexOf(0));
if (mtrl.fl & Solid.MTRL_ALPHA_TEST) {
mtrl.alphaFunc = stream.readInt32LE();
mtrl.alphaRef = stream.readFloatLE();
} else {
mtrl.alphaFunc = 0;
mtrl.alphaRef = 0.0;
}
mtrls.push(mtrl);
}
return mtrls;
}
function loadVerts (stream, count) {
var verts = [];
for (var i = 0; i < count; ++i) {
verts.push(stream.readFloatLEArray(3));
}
return verts;
}
function loadEdges (stream, count) {
var edges = [];
for (var i = 0; i < count; ++i) {
edges.push({
vi: stream.readInt32LE(),
vj: stream.readInt32LE()
});
}
return edges;
}
function loadSides (stream, count) {
var sides = [];
for (var i = 0; i < count; ++i) {
sides.push({
n: stream.readFloatLEArray(3),
d: stream.readFloatLE()
});
}
return sides;
}
function loadTexcs (stream, count) {
var texcs = [];
for (var i = 0; i < count; ++i) {
texcs.push(stream.readFloatLEArray(2));
}
return texcs;
}
function loadOffs (stream, count) {
var offs = [];
for (var i = 0; i < count; ++i) {
offs.push({
ti: stream.readInt32LE(),
si: stream.readInt32LE(),
vi: stream.readInt32LE()
});
}
return offs;
}
function loadGeoms (stream, count) {
var geoms = [];
for (var i = 0; i < count; ++i) {
geoms.push({
mi: stream.readInt32LE(),
oi: stream.readInt32LE(),
oj: stream.readInt32LE(),
ok: stream.readInt32LE()
});
}
return geoms;
}
function loadLumps (stream, count) {
var lumps = [];
for (var i = 0; i < count; ++i) {
lumps.push({
fl: stream.readInt32LE(),
v0: stream.readInt32LE(),
vc: stream.readInt32LE(),
e0: stream.readInt32LE(),
ec: stream.readInt32LE(),
g0: stream.readInt32LE(),
gc: stream.readInt32LE(),
s0: stream.readInt32LE(),
sc: stream.readInt32LE()
});
}
return lumps;
}
function loadNodes (stream, count) {
var nodes = [];
for (var i = 0; i < count; ++i) {
nodes.push({
si: stream.readInt32LE(),
ni: stream.readInt32LE(),
nj: stream.readInt32LE(),
l0: stream.readInt32LE(),
lc: stream.readInt32LE()
});
}
return nodes;
}
function loadPaths (stream, count) {
var paths = [];
var i, path;
for (i = 0, path; i < count; ++i) {
path = {
p: stream.readFloatLEArray(3),
t: stream.readFloatLE(),
pi: stream.readInt32LE(),
f: stream.readInt32LE(),
s: stream.readInt32LE(),
fl: stream.readInt32LE(),
p0: -1,
p1: -1,
};
if (path.fl & Solid.PATH_ORIENTED) {
var e = stream.readFloatLEArray(4);
// Convert Neverball's W X Y Z to glMatrix's X Y Z W.
var w = e[0];
e[0] = e[1];
e[1] = e[2];
e[2] = e[3];
e[3] = w;
// Orientation quaternion.
path.e = e;
} else {
// Identity quaternion.
path.e = new Float32Array([0, 0, 0, 1]);
}
if (path.fl & Solid.PATH_PARENTED) {
path.p0 = stream.readInt32LE();
path.p1 = stream.readInt32LE();
if (path.p1 < 0)
path.p1 = path.p0;
}
paths.push(path);
}
// Turn into a linked list.
for (i = 0, path; i < paths.length; ++i) {
path = paths[i];
// May link to itself.
path.next = paths[path.pi] || null;
}
return paths;
}
function loadBodies (stream, count) {
var bodies = [];
for (var i = 0; i < count; ++i) {
bodies.push({
pi: stream.readInt32LE(),
pj: stream.readInt32LE(),
ni: stream.readInt32LE(),
l0: stream.readInt32LE(),
lc: stream.readInt32LE(),
g0: stream.readInt32LE(),
gc: stream.readInt32LE()
});
if (bodies[i].pj < 0) {
bodies[i].pj = bodies[i].pi;
}
}
return bodies;
}
function loadItems (version, stream, count) {
var items = [];
for (var i = 0; i < count; ++i) {
var item = {
p: stream.readFloatLEArray(3),
t: stream.readInt32LE(),
n: stream.readInt32LE(),
p0: -1,
p1: -1,
};
if (version >= 9) {
item.p0 = stream.readInt32LE();
item.p1 = stream.readInt32LE();
}
items.push(item);
}
return items;
}
function loadGoals (version, stream, count) {
var goals = [];
for (var i = 0; i < count; ++i) {
var goal = {
p: stream.readFloatLEArray(3),
r: stream.readFloatLE(),
p0: -1,
p1: -1,
};
if (version >= 9) {
goal.p0 = stream.readInt32LE();
goal.p1 = stream.readInt32LE();
}
goals.push(goal);
}
return goals;
}
function loadJumps (version, stream, count) {
var jumps = [];
for (var i = 0; i < count; ++i) {
var jump = {
p: stream.readFloatLEArray(3),
q: stream.readFloatLEArray(3),
r: stream.readFloatLE(),
p0: -1,
p1: -1,
};
if (version >= 9) {
jump.p0 = stream.readInt32LE();
jump.p1 = stream.readInt32LE();
}
jumps.push(jump);
}
return jumps;
}
function loadSwitches (version, stream, count) {
var switches = [];
for (var i = 0; i < count; ++i) {
var swch = {
p: stream.readFloatLEArray(3),
r: stream.readFloatLE(),
pi: stream.readInt32LE(),
t: stream.readFloatLEArray(2)[0], // Consume unused padding.
f: stream.readFloatLEArray(2)[0], // Consume unused padding.
i: stream.readInt32LE(),
p0: -1,
p1: -1,
};
if (version >= 9) {
swch.p0 = stream.readInt32LE();
swch.p1 = stream.readInt32LE();
}
switches.push(swch);
}
return switches;
}
function loadBills (version, stream, count) {
var bills = [];
for (var i = 0; i < count; ++i) {
var bill = {
fl: stream.readInt32LE(),
mi: stream.readInt32LE(),
t: stream.readFloatLE(),
d: stream.readFloatLE(),
w: stream.readFloatLEArray(3),
h: stream.readFloatLEArray(3),
rx: stream.readFloatLEArray(3),
ry: stream.readFloatLEArray(3),
rz: stream.readFloatLEArray(3),
p: stream.readFloatLEArray(3),
p0: -1,
p1: -1,
};
if (version >= 9) {
bill.p0 = stream.readInt32LE();
bill.p1 = stream.readInt32LE();
}
bills.push(bill);
}
return bills;
}
function loadBalls (stream, count) {
var balls = [];
for (var i = 0; i < count; ++i) {
balls.push({
p: stream.readFloatLEArray(3),
r: stream.readFloatLE()
});
}
return balls;
}
function loadViews (stream, count) {
var views = [];
for (var i = 0; i < count; ++i) {
views.push({
p: stream.readFloatLEArray(3),
q: stream.readFloatLEArray(3)
});
}
return views;
}