-
Notifications
You must be signed in to change notification settings - Fork 11
/
MapParser.js
685 lines (611 loc) · 19 KB
/
MapParser.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/**
This file contains all the code needed to parse a .bsp file (Version 30)
into a JSON datastructure.
Unofficial specification for version 30 can be found here:
http://hlbsp.sourceforge.net/index.php?content=bspdef
Additional information not exclusive to 30:
https://developer.valvesoftware.com/wiki/Source_BSP_File_Format
A few changes has been made to the JSON layout for performance reasons
**/
/**
Provides easy-to-use functions for reading binary data
**/
define(["util/DataReader"], function(DataReader) {
var constants = {
BSP_VERSION: 30,
PLANE_X: 0,
PLANE_Y: 1,
PLANE_Z: 2,
PLANE_ANY_X: 3,
PLANE_ANY_Y: 4,
PLANE_ANY_Z: 5,
CONTENTS_SOLID: -2
};
//Raw data array of the .blp file format version 30
var data;
var parseHeader = function () {
var magic = DataReader.readInteger(data, 0);
if (magic != constants.BSP_VERSION) {
console.log("Invalid magic number. Expected: 30, but was: " + magic);
return;
}
return parseLumps();
};
var parseLumps = function () {
//Read the 15 predefined lumps
return {
entities: [DataReader.readInteger(data, 4), DataReader.readInteger(data, 8)],
planes: [DataReader.readInteger(data, 12), DataReader.readInteger(data, 16)],
textures: [DataReader.readInteger(data, 20), DataReader.readInteger(data, 24)],
vertices: [DataReader.readInteger(data, 28), DataReader.readInteger(data, 32)],
visibility: [DataReader.readInteger(data, 36), DataReader.readInteger(data, 40)],
nodes: [DataReader.readInteger(data, 44), DataReader.readInteger(data, 48)],
texinfo: [DataReader.readInteger(data, 52), DataReader.readInteger(data, 56)],
faces: [DataReader.readInteger(data, 60), DataReader.readInteger(data, 64)],
lighting: [DataReader.readInteger(data, 68), DataReader.readInteger(data, 72)],
clipnodes: [DataReader.readInteger(data, 76), DataReader.readInteger(data, 80)],
leaves: [DataReader.readInteger(data, 84), DataReader.readInteger(data, 88)],
marksurfaces: [DataReader.readInteger(data, 92), DataReader.readInteger(data, 96)],
edges: [DataReader.readInteger(data, 100), DataReader.readInteger(data, 104)],
surfedges: [DataReader.readInteger(data, 108), DataReader.readInteger(data, 112)],
models: [DataReader.readInteger(data, 116), DataReader.readInteger(data, 120)]
}
};
var uintToString = function(uintArray) {
var encodedString = String.fromCharCode.apply(null, uintArray),
decodedString = decodeURIComponent(escape(encodedString));
return decodedString;
};
var parseEntity = function (entityHeader) {
//Skip past first "
var offset = entityHeader[0] + 1;
//Remove last }
var length = entityHeader[1] - 5;
//Create subarray containing only the data of this lump
var entities = data.subarray(offset, offset + length);
//Convert to string
var sEntities = uintToString(entities);
//Split into an array of entities
var entitiesArray = sEntities.split("}\n{");
//Helper formatters
var identity = function(x) { return x; };
var parseList = function(formatter) {
return function(x) {
var list = [];
for(var o in x.split(" ")) {
list.push(formatter(o));
}
return list;
};
};
//A map from key to value formatters.
var formatters = {
MaxRange: parseInt,
classname: identity,
light: parseInt,
message: identity,
skyname: identity,
sounds: parseInt,
wad: function(x) { return x.split(";"); },
model: function(x) {
//Most entities have a value of the form *N where N is an integer
//But some entities have a string value instead,
//(i.e path to models in case of hostages)
if(x[0] === "*")
return parseInt(x.substr(1));
return identity;
},
renderamt: parseInt,
rendercolor: new parseList(parseInt),
rendermode: parseInt,
skin: parseInt,
angles: new parseList(parseInt),
delay: parseInt,
distance: parseInt,
dmg: parseInt,
health: parseInt,
lip: parseInt,
locked_sentence: parseInt,
locked_sound: parseInt,
movesnd: parseInt,
origin: new parseList(parseInt),
renderfx: parseInt,
speed: parseInt,
stopsnd: parseInt,
unlocked_sentence: parseInt,
unlocked_sound: parseInt,
wait: parseInt,
_light: new parseList(parseInt),
pitch: parseInt,
angle: parseInt,
spawnflags: parseInt,
target: identity,
deceleration: parseInt,
targetname: identity,
explodemagnitude: parseInt,
explosion: parseInt,
material: parseInt,
spawnobject: parseInt,
texture: identity,
acceleration: parseInt,
killtarget: identity,
triggerstate: parseInt,
_fade: parseFloat
};
var entityLump = Array(entitiesArray.length);
for(var i = 0; i < entitiesArray.length; ++i) {
//Get key
var sKeyVal = entitiesArray[i].split("\"");
var map = {};
//Create a mapping from key => value
for(var j = 1; j < sKeyVal.length; j += 4) {
var key = sKeyVal[j];
var value = sKeyVal[j+2];
var formatter = formatters[key];
if(!formatter) console.log("Unknown entity name: " + key);
else map[key] = formatter(value);
}
entityLump[i] = map;
}
return entityLump;
}
var parsePlanes = function(planeHeader) {
var offset = planeHeader[0];
var length = planeHeader[1];
var planesLump = Array(length/20);
var normals = new Float32Array((length / 20) * 3);
var end = offset + length;
var n = 0;
var k = 0;
for(var i = offset; i < end; i += 20) {
//Read normal
var xNorm = DataReader.readFloat(data, i);
var yNorm = DataReader.readFloat(data, i+4);
var zNorm = DataReader.readFloat(data, i+8);
//Store normals in seperate array for performance reasons
normals[n++] = xNorm;
normals[n++] = yNorm;
normals[n++] = zNorm;
//Read distance and type
var dist = DataReader.readFloat(data, i+12);
var type = DataReader.readInteger(data, i+16);
planesLump[k++] = {
distance: dist,
type: type
};
}
return {
normals: normals,
planes: planesLump
};
}
var parseTextures = function(textureHeader) {
var offset = textureHeader[0];
var length = textureHeader[1];
//Read number of upcoming structures
var count = DataReader.readInteger(data, offset);
//Read offsets
var offsets = Array(n);
var endOfOffsets = (offset + 4) + (count * 4);
var n = 0;
for(var i = offset+4; i < endOfOffsets; i += 4) {
offsets[n++] = DataReader.readInteger(data, i);
}
n = 0;
var textureLumps = Array(count);
var numTextures = count*40;
//Read actual structures with texture info
for(var i = 0; i < numTextures; i += 40) {
var addr = offset + offsets[n];
var name = DataReader.readBinaryString(data, addr, 16);
var width = DataReader.readInteger(data, addr + 16);
var height = DataReader.readInteger(data, addr + 20);
var offset1 = DataReader.readInteger(data, addr + 24);
var offset2 = DataReader.readInteger(data, addr + 28);
var offset3 = DataReader.readInteger(data, addr + 32);
var offset4 = DataReader.readInteger(data, addr + 36);
textureLumps[n++] = {
name: name,
width: width,
height: height,
offsets: [offset1, offset2, offset3, offset4]
};
}
return textureLumps;
}
var parseVertices = function(vertexHeader) {
var offset = vertexHeader[0];
var length = vertexHeader[1];
var end = offset + length;
var verticesLump = Array(length/4);
var n = 0;
for(var i = offset; i < end; i += 12) {
verticesLump[n++] = DataReader.readFloat(data, i);
verticesLump[n++] = DataReader.readFloat(data, i+4);
verticesLump[n++] = DataReader.readFloat(data, i+8);
}
return verticesLump;
}
var getVisibilityList = function(offset, nLeafs, nVisLeaves) {
var visibilityList = Array(nLeafs - 1);
var iVis = 0;
//Loop through each leaf and perform run-length decompression
for(var i = 0; iVis < nVisLeaves; ++i) {
var byt = data[offset+i];
//If the byte is 0 it means that the next 8 leaves are not shown
if(byt === 0) {
++i;
iVis += 8 * data[offset+i];
}
else {
//Loop through each bit in the byte
for(var bit = 1; bit < 256; ++iVis, bit <<= 1) {
//If the bit is 1 and we're still in a valid leaf
if(((byt & bit) > 0) && (iVis < nLeafs)) {
visibilityList[iVis] = true;
}
}
}
}
return visibilityList;
}
var parseVisibility = function(visibilityHeader, nodes, leaves) {
var offset = visibilityHeader[0];
var length = visibilityHeader[1];
var end = offset + length;
//Count the number of visibility leaves
//A visivility leaf is a non solid leaf that has a valid offset
//into the visibility lump
var count = leaves.filter(function(leaf) {
//Not a solid and has visibility
return leaf.nContents != constants.CONTENTS_SOLID && leaf.nVisOffset >= 0;
}).length - 1;
var visibility = Array(count);
for(var i = 0; i < count; ++i) {
if(leaves[i+1].nVisOffset >= 0) {
visibility[i] = getVisibilityList(offset + leaves[i+1].nVisOffset, leaves.length, count);
}
}
return visibility;
}
var parseNodes = function(nodeHeader) {
var offset = nodeHeader[0];
var length = nodeHeader[1];
var end = offset + length;
var nodesLump = Array(length/24);
var n = 0;
for(var i = offset; i < end; i += 24) {
var iPlane = DataReader.readInteger(data, i);
var iChildren = [
DataReader.readSignedShort(data, i + 4),
DataReader.readSignedShort(data, i + 6)
];
var nMins = [
DataReader.readSignedShort(data, i + 8),
DataReader.readSignedShort(data, i + 10),
DataReader.readSignedShort(data, i + 12)
];
var nMaxs = [
DataReader.readSignedShort(data, i + 14),
DataReader.readSignedShort(data, i + 16),
DataReader.readSignedShort(data, i + 18)
];
var firstFace = DataReader.readShort(data, i + 20);
var nFaces = DataReader.readShort(data, i + 22);
nodesLump[n++] = {
iPlane: iPlane,
iChildren: iChildren,
nMins: nMins,
nMaxs: nMaxs,
firstFace: firstFace,
nFaces: nFaces
};
}
return nodesLump;
}
var parseTextureInfo = function(texinfoHeader) {
var offset = texinfoHeader[0];
var length = texinfoHeader[1];
var end = offset + length;
var textureInfoLump = Array(length / 40);
var n = 0;
for(var i = offset; i < end; i += 40) {
var vS = [
DataReader.readFloat(data, i),
DataReader.readFloat(data, i + 4),
DataReader.readFloat(data, i + 8)
];
var fSShift = DataReader.readFloat(data, i + 12);
var vT = [
DataReader.readFloat(data, i + 16),
DataReader.readFloat(data, i + 20),
DataReader.readFloat(data, i + 24)
];
var fTShift = DataReader.readFloat(data, i + 28);
var iMiptex = DataReader.readInteger(data, i + 32);
var nFlags = DataReader.readInteger(data, i + 36);
textureInfoLump[n++] = {
vS: vS,
fSShift: fSShift,
vT: vT,
fTShift: fTShift,
iMiptex: iMiptex,
nFlags: nFlags
};
}
return textureInfoLump;
}
var parseFaces = function(faceHeader) {
var offset = faceHeader[0];
var length = faceHeader[1];
var end = offset + length;
var faceLump = Array(length / 20);
var n = 0;
for(var i = offset; i < end; i += 20) {
var iPlane = DataReader.readShort(data, i);
var nPlanSide = DataReader.readShort(data, i + 2);
var iFirstEdge = DataReader.readInteger(data, i + 4);
var nEdges = DataReader.readShort(data, i + 8);
var iTextureInfo = DataReader.readShort(data, i + 10);
var nStyles = [
data[i + 12],
data[i + 13],
data[i + 14],
data[i + 15]
];
var nLightmapoffset = DataReader.readInteger(data, i + 16);
faceLump[n++] = {
iPlane: iPlane,
nPlanSide: nPlanSide,
iFirstEdge: iFirstEdge,
nEdges: nEdges,
iTextureInfo: iTextureInfo,
nStyles: nStyles,
nLightmapoffset: nLightmapoffset
};
}
return faceLump;
}
var parseLighting = function(lightingHeader, faces, surfedges, edges, vertices) {
var offset = lightingHeader[0];
var length = lightingHeader[1];
var end = offset + length;
var buffer = [];
//Loop through all faces
for(var i = 0; i < faces.length; ++i) {
var face = faces[i];
//Find index into the color array
var colorOffset = face.nLightmapoffset;
//Get the index of the vertex that has this color
for(var j = 0; j < face.nEdges; ++j) {
var iEdge = surfedges[face.iFirstEdge + j];
var index;
if(iEdge > 0) {
var edge = edges[iEdge];
index = edge[0];
}
else {
var edge = edges[-iEdge];
index = edge[1];
}
//Okay this is a bit weird
//We're not allowed to override a color we've already defined
//(Since another vertex is using it)
//So we modify the bsp datastructure to allow for a single
//call to OpenGL to draw the map
//Thus we need to make sure the color and vertex buffer are
//synched up such that:
//At colorBuffer[3*i] we have the color for the vertex located
//at position 3*i
if(buffer[3*index] !== undefined) {
//Get the number of vertices
var vertexOffset = vertices.length/3;
//Add the new colour
buffer[3*vertexOffset] = data[offset+colorOffset]/255.0;
buffer[3*vertexOffset+1] = data[offset+colorOffset+1]/255.0;
buffer[3*vertexOffset+2] = data[offset+colorOffset+2]/255.0;
//Copy the vertex to the back of the buffer
vertices[3*vertexOffset] = vertices[3*index];
vertices[3*vertexOffset+1] = vertices[3*index+1];
vertices[3*vertexOffset+2] = vertices[3*index+2];
//Write the new index into the edges lump
if(iEdge > 0) {
edges[iEdge][0] = vertexOffset;
}
else {
edges[-iEdge][1] = vertexOffset;
}
}
//We're defining a new colour
else {
//Store r, g, b and a
buffer[3*index] = data[offset+colorOffset]/255.0;
buffer[3*index+1] = data[offset+colorOffset+1]/255.0;
buffer[3*index+2] = data[offset+colorOffset+2]/255.0;
}
}
}
return new Float32Array(buffer);
}
var parseClipNodes = function(clipNodeHeader) {
var offset = clipNodeHeader[0];
var length = clipNodeHeader[1];
var end = offset + length;
var clipNodeLump = Array(length / 8);
var n = 0;
for(var i = offset; i < end; i += 8) {
var iPlane = DataReader.readInteger(data, i);
var iChildren = [
DataReader.readSignedShort(data, i + 4),
DataReader.readSignedShort(data, i + 6)
];
clipNodeLump[n++] = {
iPlane: iPlane,
iChildren: iChildren
};
}
return clipNodeLump;
}
var parseLeaves = function(leafHeader) {
var offset = leafHeader[0];
var length = leafHeader[1];
var end = offset + length;
var leafLump = Array(length / 28);
var n = 0;
for(var i = offset; i < end; i += 28) {
var nContents = DataReader.readInteger(data, i);
var nVisOffset = DataReader.readInteger(data, i + 4);
var nMins = [
DataReader.readSignedShort(data, i + 8),
DataReader.readSignedShort(data, i + 10),
DataReader.readSignedShort(data, i + 12)
];
var nMaxs = [
DataReader.readSignedShort(data, i + 14),
DataReader.readSignedShort(data, i + 16),
DataReader.readSignedShort(data, i + 18)
];
var iFirstMarkSurface = DataReader.readShort(data, i + 20);
var nMarkSurfaces = DataReader.readShort(data, i + 22);
var nAmbientLevels = [
DataReader.readShort(data, i + 24),
DataReader.readShort(data, i + 25),
DataReader.readShort(data, i + 26),
DataReader.readShort(data, i + 27)
];
leafLump[n++] = {
nContents: nContents,
nVisOffset: nVisOffset,
nMins: nMins,
nMaxs: nMaxs,
iFirstMarkSurface: iFirstMarkSurface,
nMarkSurfaces: nMarkSurfaces,
nAmbientLevels: nAmbientLevels
};
}
return leafLump;
}
var parseMarkSurfaces = function(markSurfaceHeader) {
var offset = markSurfaceHeader[0];
var length = markSurfaceHeader[1];
var end = offset + length;
var markSurfacesLump = Array(length / 2);
var n = 0;
for(var i = offset; i < end; i += 2) {
markSurfacesLump[n++] = DataReader.readShort(data, i);
}
return markSurfacesLump;
}
var parseEdges = function(edgeHeader) {
var offset = edgeHeader[0];
var length = edgeHeader[1];
var end = offset + length;
var edgesLump = Array(length / 4);
var n = 0;
for(var i = offset; i < end; i += 4) {
edgesLump[n++] = [
DataReader.readShort(data, i),
DataReader.readShort(data, i + 2)
];
}
return edgesLump;
}
var parseSurfedges = function(surfedgeHeader) {
var offset = surfedgeHeader[0];
var length = surfedgeHeader[1];
var end = offset + length;
var surfedgeLump = Array(length / 4);
var n = 0;
for(var i = offset; i < end; i += 4) {
surfedgeLump[n++] = DataReader.readInteger(data, i);
}
return surfedgeLump;
}
var parseModels = function(modelsHeader) {
var offset = modelsHeader[0];
var length = modelsHeader[1];
var end = offset + length;
var modelsLump = Array(length / 64);
var n = 0;
for(var i = offset; i < end; i += 64) {
var nMins = [
DataReader.readFloat(data, i),
DataReader.readFloat(data, i + 4),
DataReader.readFloat(data, i + 8)
];
var nMaxs = [
DataReader.readFloat(data, i + 12),
DataReader.readFloat(data, i + 16),
DataReader.readFloat(data, i + 20)
];
var vOrigin = [
DataReader.readFloat(data, i + 24),
DataReader.readFloat(data, i + 18),
DataReader.readFloat(data, i + 32),
];
var iHeadNodes = [
DataReader.readInteger(data, i + 36),
DataReader.readInteger(data, i + 40),
DataReader.readInteger(data, i + 44),
DataReader.readInteger(data, i + 48),
];
var nVisLeafs = DataReader.readInteger(data, i + 52);
var iFirstFace = DataReader.readInteger(data, i + 56);
var nFaces = DataReader.readInteger(data, i + 60);
modelsLump[n++] = {
nMins: nMins,
nMaxs: nMaxs,
vOrigin: vOrigin,
iHeadNodes: iHeadNodes,
nVisLeafs: nVisLeafs,
iFirstFace: iFirstFace,
nFaces: nFaces
};
}
return modelsLump;
}
return {
constants: constants,
/**
Uint8array -> JSON
**/
parse: function (input) {
data = input;
var header = parseHeader();
var entities = parseEntity(header.entities);
var planes = parsePlanes(header.planes);
var textures = parseTextures(header.textures);
var vertices = parseVertices(header.vertices);
var nodes = parseNodes(header.nodes);
var textureInfo = parseTextureInfo(header.texinfo);
var faces = parseFaces(header.faces);
var clipNodes = parseClipNodes(header.clipnodes);
var leaves = parseLeaves(header.leaves);
var visibility = parseVisibility(header.visibility, nodes, leaves);
var markSurfaces = parseMarkSurfaces(header.marksurfaces);
var edges = parseEdges(header.edges);
var surfedges = parseSurfedges(header.surfedges);
var lighting = parseLighting(header.lighting, faces, surfedges, edges, vertices);
var models = parseModels(header.models);
return {
entities: entities,
planes: planes,
textures: textures,
//Ugly to first new this up now, but parseLighting needs the
//raw JS array so we cannot do this before now.
vertices: new Float32Array(vertices),
nodes: nodes,
textureInfo: textureInfo,
faces: faces,
lighting: lighting,
clipNodes: clipNodes,
leaves: leaves,
markSurfaces: markSurfaces,
edges: edges,
surfedges: surfedges,
models: models,
visibility: visibility
};
}
};
});