-
Notifications
You must be signed in to change notification settings - Fork 12
/
level-packer.js
301 lines (268 loc) · 12 KB
/
level-packer.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
const glob = require('glob');
const util = require('util');
const LevelMetadata = require('./level-metadata');
/**
* The level packer takes JSON levels, exported by Tiled, and converts them into
* our game-specific level format. I suppose this insulates the game code a little
* bit from our reliance on a third-party tool, but mostly it's just to get our
* level data as small as possible.
*/
const levelPacker = {
packAll: function (levelGlob) {
const levels = glob.sync(levelGlob).map(filename => levelPacker.pack(filename));
//const string = JSON.stringify(levels, undefined, 2);
const string = util.inspect(levels, { depth: null });
return "const LevelCache = " + string + ";\n" +
"LevelCache.outro = " + JSON.stringify(LevelMetadata.outro) + ";\n";
},
pack: function (filename) {
const raw = require("./" + filename);
let terrainLayer, metaLayer, objectsLayer;
// We expect each level to have layers with these exact names
for (let i = 0; i < raw.layers.length; i++) {
if (raw.layers[i].name === 'Terrain') {
terrainLayer = raw.layers[i];
} else if (raw.layers[i].name === 'Meta') {
metaLayer = raw.layers[i];
} else if (raw.layers[i].name === 'Objects') {
objectsLayer = raw.layers[i];
} else {
throw new Error('Invalid layer name ' + raw.layers[i].name);
}
}
if (!terrainLayer || !metaLayer || !objectsLayer) {
throw new Error('Missing required layer');
}
let width = terrainLayer.width;
let height = terrainLayer.height;
// The first thing we want to do is calculate the minimum width and height of the level
// data in the Tiled map (for ease of editing, I create all the levels in the middle of a
// 100x100 map, but we don't need all that cruft in the game!)
const tileBounds = {
top: terrainLayer.height,
bottom: 0,
left: terrainLayer.width,
right: 0
};
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
if (terrainLayer.data[i * width + j] > 0) {
tileBounds.top = Math.min(tileBounds.top, i);
tileBounds.bottom = Math.max(tileBounds.bottom, i);
tileBounds.left = Math.min(tileBounds.left, j);
tileBounds.right = Math.max(tileBounds.right, j);
}
}
}
width = tileBounds.right - tileBounds.left + 1;
height = tileBounds.bottom - tileBounds.top + 1;
// After determining the new width and height, we need to translate all layers to new coords
terrainLayer = levelPacker.cropLayer(terrainLayer, tileBounds.left, tileBounds.top, width, height);
metaLayer = levelPacker.cropLayer(metaLayer, tileBounds.left, tileBounds.top, width, height);
objectsLayer = levelPacker.cropLayer(objectsLayer, tileBounds.left, tileBounds.top, width, height);
const level = {
enemies: [],
cameras: [],
terminals: [],
doors: [],
width: width,
height: height,
data: levelPacker.packData(terrainLayer.data)
};
let short = filename.match(/\/([^/]*)\.json/)[1];
Object.assign(level, LevelMetadata[short]);
const enterBounds = {
top: terrainLayer.height * 32,
bottom: 0,
left: terrainLayer.width * 32,
right: 0
};
const exitBounds = {
top: terrainLayer.height * 32,
bottom: 0,
left: terrainLayer.width * 32,
right: 0
};
// Process the objects in the objects layer, inserting them into the appropriate
// collections as we go.
for (let i = 0; i < objectsLayer.objects.length; i++) {
let object = objectsLayer.objects[i];
if (object.type === "enemy") {
let enemy = {
x: object.x,
y: object.y
};
if (object.properties) {
if (object.properties.Wake) enemy.wake = object.properties.Wake;
if (object.properties.WakeRadius) enemy.wakeRadius = parseInt(object.properties.WakeRadius, 10);
if (object.properties.PatrolDX) enemy.patrolDX = parseInt(object.properties.PatrolDX, 10);
if (object.properties.PatrolDY) enemy.patrolDY = parseInt(object.properties.PatrolDY, 10);
if (object.properties.PatrolStart) enemy.patrolStart = parseInt(object.properties.PatrolStart, 10);
}
level.enemies.push(enemy);
}
if (object.type === "camera") {
level.cameras.push({
u: Math.floor(object.x / 32),
v: Math.floor(object.y / 32),
control: object.properties.Control,
facing: parseFloat(object.properties.Facing),
enabled: object.properties.Enabled === 'true'
});
}
if (object.type === "terminal") {
level.terminals.push({
u: Math.floor(object.x / 32),
v: Math.floor(object.y / 32),
control: object.properties.Control
});
}
}
// Process the tiles in the "meta" layer (currently, meta tiles are doors, enter, or
// exit markers). Technically, I designed the enter and exit areas to allow any size (like 2x3
// or 4x2) and the game will respect it, but the doors are hard-coded to be 2x1 tiles, so
// there's not much use for that flexibility atm.
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
if (metaLayer.data[i * width + j] === 3) {
enterBounds.top = Math.min(enterBounds.top, i * 32);
enterBounds.bottom = Math.max(enterBounds.bottom, i * 32 + 32);
enterBounds.left = Math.min(enterBounds.left, j * 32);
enterBounds.right = Math.max(enterBounds.right, j * 32 + 32);
} else if (metaLayer.data[i * width + j] === 4) {
exitBounds.top = Math.min(exitBounds.top, i * 32);
exitBounds.bottom = Math.max(exitBounds.bottom, i * 32 + 32);
exitBounds.left = Math.min(exitBounds.left, j * 32);
exitBounds.right = Math.max(exitBounds.right, j * 32 + 32);
} else if (metaLayer.data[i * width + j] === 7) {
let door;
if (metaLayer.data[i * width + j + 1] === 7) {
door = {
u: j,
v: i,
type: 'h'
};
} else if (metaLayer.data[(i + 1) * width + j] === 7) {
door = {
u: j,
v: i,
type: 'v'
};
}
if (door) {
// Kind of backed myself into this one... I ended up wanting to know
// what "type" of door I'm interacting with. Figure it out based on
// what tiles are near.
if (metaLayer.data[i * width + j + 1] === 4 ||
metaLayer.data[i * width + j - 1] === 4 ||
metaLayer.data[(i + 1) * width + j] === 4 ||
metaLayer.data[(i - 1) * width + j] === 4) {
door.exitDoor = true;
} else {
door.entranceDoor = true;
}
level.doors.push(door);
}
}
}
}
level.enter = {
p1: { x: enterBounds.left, y: enterBounds.top },
p2: { x: enterBounds.right, y: enterBounds.bottom }
};
level.exit = {
p1: { x: exitBounds.left, y: exitBounds.top },
p2: { x: exitBounds.right, y: exitBounds.bottom }
};
// The "clean up step". Level data is repeated quite a few times, so
// maybe we'll save ourselves a few measly bytes by shortening the
// names of our level cache properties...
level.d = level.doors;
delete level.doors;
// Hack: door.type isn't implemented right now (it's always "h"), so
// let's just cut it.
level.d.forEach(door => { delete door.type; });
level.e = level.enemies;
delete level.enemies;
level.t = level.terminals;
delete level.terminals;
level.c = level.cameras;
delete level.cameras;
return level;
},
/**
* Return a new copy of the provided layer, where all tiles and objects
* are repositioned to reflect a "cropped" level based on the provided
* tile offset, width, and height.
*/
cropLayer: function (layer, u, v, width, height) {
let data;
let objects;
if (layer.data) {
data = [];
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
data[i * width + j] = layer.data[(i + v) * layer.width + j + u];
}
}
}
if (layer.objects) {
objects = layer.objects.map(object => {
return Object.assign({}, object, {
x: object.x - u * 32,
y: object.y - v * 32
});
});
}
return Object.assign({}, layer, {
data: data,
objects: objects
});
},
/**
* Smash up data as small as possible. Assumes that we have a max of EIGHT possible
* tiles, freeing up remaining bits for length vars.
*
* Basically I'm being super weird in this function, as it's kinda-sorta just base64,
* but I'm optimizing for the level domain. I want to print only characters with zero
* side effects, which means:
* 35-91 (avoiding 34=" and 92=\)
* 93-123 (avoiding 92=\, 123 is just the highest multiple)
*
* I give myself 0-7 for the tile value, and add (8 * number of tiles), subtracting
* 1 because I don't need a 0 length run. That lets me pack up to 11 tiles into 1 byte,
* with no "double-byte" processing nonsense, and no binary-to-ascii back and forth.
*
* Having it be totally custom kind of sucks, but this seems to be the best bang for my
* buck with the smallest possible unpacking code (every LOC you need to unpack on the
* other side eats into your packing savings...).
*
* NOTE: Obviously, any edits to this function must also be reflected in Game._unpackData()!
*/
packData(data) {
let result = [];
let a = data[0], l = 1, byte;
for (let i = 1; i < data.length; i++) {
b = data[i];
if (a > 7) throw new Error("cannot pack level: tile value>7");
if (b === a && l < 11) {
l++;
continue;
}
// Yes, this is kind of unorthodox, it would make the most sense to pack into
// bits, like `((l<<3)+a)`, and then perhaps convert the binary string into
// a Base64 string and read it. In this case I'm optimizing for simplicity/least
// code in the level reading logic.
byte = 35 + (l - 1) * 8 + a;
if (byte >= 92) byte++;
result.push(String.fromCharCode(byte));
a = b;
l = 1;
}
byte = 35 + (l - 1) * 8 + a;
if (byte >= 92) byte++;
result.push(String.fromCharCode(byte));
return result.join('');
}
};
module.exports = levelPacker;