-
Notifications
You must be signed in to change notification settings - Fork 0
/
wadparser.js
190 lines (155 loc) · 4.5 KB
/
wadparser.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
export class Wad {
constructor(wadfile) {
const f = new WadDataView(wadfile);
this.levels = [];
this.wadFormat = 'DOOM';
this.wadType = f.getASCIIString(0, 4);
this.numLumps = f.getInt32(4, true);
const dirOffset = f.getInt32(8, true);
let currentLevel = new Level(null);
for (let i = 0; i < this.numLumps; i++) {
const lump = new WadDataView(wadfile, dirOffset + i * 16);
const filepos = lump.getInt32(0, true);
const size = lump.getInt32(4, true);
const name = lump.getASCIIString(8, 8);
if (/E\dM\d|MAP\d\d/.test(name)) {
if (currentLevel.isValid()) {
this.levels.push(currentLevel);
}
currentLevel = new Level(name);
} else if (name === 'BEHAVIOR') {
this.wadFormat = 'HEXEN';
} else {
currentLevel.lumps[name] = new WadDataView(wadfile, filepos, size);
}
}
if (currentLevel.isValid()) {
this.levels.push(currentLevel);
}
this.levels.forEach(level => level.load(this.wadFormat));
}
}
export class Level {
constructor(name) {
this.name = name;
this.lumps = {};
this.vertices = [];
this.lines = [];
this.lowerLeft = null;
this.upperRight = null;
}
isValid() {
return (
this.name != null && 'VERTEXES' in this.lumps && 'LINEDEFS' in this.lumps
);
}
load(wadFormat) {
let minX = Number.POSITIVE_INFINITY;
let maxX = Number.NEGATIVE_INFINITY;
let minY = Number.POSITIVE_INFINITY;
let maxY = Number.NEGATIVE_INFINITY;
for (let data of this.lumps.VERTEXES.packetsOfSize(4)) {
let vert = [data.getInt16(0, true), data.getInt16(2, true)];
this.vertices.push(vert);
minX = Math.min(vert[0], minX);
maxX = Math.max(vert[0], maxX);
minY = Math.min(vert[1], minY);
maxY = Math.max(vert[1], maxY);
}
this.lowerLeft = [minX, minY];
this.upperRight = [maxX, maxY];
this.shift = [0 - this.lowerLeft[0], 0 - this.lowerLeft[1]];
for (let data of this.lumps.LINEDEFS.packetsOfSize(
wadFormat == 'HEXEN' ? 16 : 14
)) {
this.lines.push(new Line(data));
}
}
normalize(vert, padding = 5) {
return [
this.shift[0] + vert[0] + padding,
this.shift[1] + vert[1] + padding
];
}
asSvg(width, height) {
const levelSize = this.normalize(this.upperRight, 10);
const padding = 10;
width -= padding * 2;
height -= padding * 2;
let scaleFactor = 0;
let xOffset = 0;
let yOffset = 0;
if (width / levelSize[0] < height / levelSize[1]) {
// Needs to be shrunk more horizontally
scaleFactor = width / levelSize[0];
yOffset = (height - levelSize[1] * scaleFactor) / 2;
} else {
// Needs to be shrunk more vertically
scaleFactor = height / levelSize[1];
xOffset = (width - levelSize[0] * scaleFactor) / 2;
}
const scale = pos => {
return [
pos[0] * scaleFactor + xOffset,
height - (pos[1] * scaleFactor + yOffset)
];
};
return `
<svg
width="${width}"
height="${height}"
>
${this.lines
.map(l => {
const a = scale(this.normalize(this.vertices[l.a]));
const b = scale(this.normalize(this.vertices[l.b]));
return `<line
x1="${a[0]}" y1="${a[1]}"
x2="${b[0]}" y2="${b[1]}"
class="${l.isOneSided ? 'one-sided' : 'two-sided '}"
/>`;
})
.join('')}
</svg>`;
}
}
export class WadDataView extends DataView {
constructor(buffer, offset, size) {
super(buffer, offset, size);
}
getASCIIString(offset, length) {
let result = '';
for (let i = 0; i < length; i++) {
let val = this.getUint8(offset + i);
if (val === 0) {
return result;
}
result += String.fromCharCode(val);
}
return result;
}
setInt16(offset, value) {
return super.setInt16(
offset < 0 ? this.byteLength + offset : offset,
value,
true
);
}
getInt16(offset) {
return super.getInt16(offset < 0 ? this.byteLength + offset : offset, true);
}
*packetsOfSize(size) {
for (let i = 0; i < this.byteLength; i += size) {
yield new WadDataView(this.buffer, this.byteOffset + i, size);
}
}
}
export class Line {
constructor(data) {
this.a = data.getInt16(0);
this.b = data.getInt16(2);
this.leftSide = data.getInt16(-4);
this.rightSide = data.getInt16(-2);
this.isOneSided = this.leftSide == -1 || this.rightSide == -1;
}
}