/
compressed.ts
719 lines (618 loc) · 27.3 KB
/
compressed.ts
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
import { Point2, Point3, Point3Set } from '@/point.js';
import { OnlyBot, OnlyBotLayer, OnlyBotMaterial } from '@/bot.js';
import { mapAsciiToBits, mapBitsToAscii, ReadingBitBuffer, WritingBitBuffer } from '@/bits.js';
import { calculateVoxelBounds, packVoxelSpace } from '@/utils.js';
const BIT_LENGTH = {
COLOR_COUNT_BITWIDTH: 4,
COLOR_RGB: 8,
BOT_LENGTH: 16,
NAME_COUNT: 5,
NAME_CHAR: 6,
ANCHOR_XZ_SIGN: 1,
ANCHOR_X: 4,
ANCHOR_Y: 3,
ANCHOR_Z: 4,
MATERIAL_COUNT: 2,
MATERIAL_PRESET: 8,
LAYER_VOXEL_LIST_COUNT_BITWIDTH: 4,
LAYER_COUNT: 5,
LAYER_TYPE: 3,
LAYER_MATERIAL: 2,
LAYER_VOXEL_ORIGIN: 4,
LAYER_VOXEL_FORMAT: 1,
LAYER_VOXEL_FIELD_LENGTH: 4,
LAYER_VOXEL_FIELD_FLAG: 1,
LAYER_VOXEL_LIST_FOURBIT: 1,
LAYER_VOXEL_LIST_DIRECTION: 2,
};
const minBitsRequired = (n: number): number => {
if (isNaN(n) || n < 0) {
throw new Error(`Cannot calculate bits required for: ${n}`);
}
if (n < 1) {
return 1;
}
// Formula for max int with b bits is 2^b - 1
// This means that, for example, 1 bit can store [0, 1] and 2 bits can store [0, 3]
// However, Math.log2(2) = 1, so we need to add 1 to n to get the correct result
return Math.ceil(Math.log2(n + 1));
};
export class CompressedLayer {
public readonly type: number;
public readonly material: number;
public readonly data: CompressedLayerData;
constructor(type: number, material: number, data: CompressedLayerData) {
this.type = type;
this.material = material;
this.data = data;
}
public static compress(layer: OnlyBotLayer, data: CompressedLayerData): CompressedLayer {
return new CompressedLayer(layer.type, layer.material, data);
}
public static fromBuffer(buffer: ReadingBitBuffer, layerListCountBitwidth: number): CompressedLayer {
const type = buffer.readUIntBitsBE(BIT_LENGTH.LAYER_TYPE);
const material = buffer.readUIntBitsBE(BIT_LENGTH.LAYER_MATERIAL);
const data = CompressedLayerData.fromBuffer(buffer, layerListCountBitwidth);
return new CompressedLayer(type, material, data);
}
public compressedSizeInBits(this: CompressedLayer, layerListCountBitwidth: number): number {
let size = 0;
size += BIT_LENGTH.LAYER_TYPE;
size += BIT_LENGTH.LAYER_MATERIAL;
size += this.data.compressedSizeInBits(layerListCountBitwidth);
return size;
}
public toBuffer(this: CompressedLayer, buffer: WritingBitBuffer, layerListCountBitwidth: number): void {
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_TYPE, this.type);
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_MATERIAL, this.material);
this.data.toBuffer(buffer, layerListCountBitwidth);
}
public expand(): OnlyBotLayer {
const voxels = this.data.expand();
return {
type: this.type,
material: this.material,
voxels,
};
}
}
export abstract class CompressedLayerData {
public readonly origin: Point3;
public abstract readonly format: boolean;
protected constructor(origin: Point3) {
this.origin = origin;
}
public static fromBuffer(buffer: ReadingBitBuffer, layerListCountBitwidth: number): CompressedLayerData {
const origin = new Point3(
buffer.readUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_ORIGIN),
buffer.readUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_ORIGIN),
buffer.readUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_ORIGIN)
);
const format = buffer.readUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_FORMAT);
return format
? CompressedLayerDataList.formatSpecificFromBuffer(buffer, origin, layerListCountBitwidth)
: CompressedLayerDataField.formatSpecificFromBuffer(buffer, origin);
}
protected abstract formatSpecificCompressedSizeInBits(layerListCountBitwidth: number): number;
public compressedSizeInBits(this: CompressedLayerData, layerListCountBitwidth: number): number {
let size = 0;
size += 3 * BIT_LENGTH.LAYER_VOXEL_ORIGIN;
size += BIT_LENGTH.LAYER_VOXEL_FORMAT;
size += this.formatSpecificCompressedSizeInBits(layerListCountBitwidth);
return size;
}
protected abstract formatSpecificToBuffer(buffer: WritingBitBuffer, layerListCountBitwidth: number): void;
public toBuffer(this: CompressedLayerData, buffer: WritingBitBuffer, layerListCountBitwidth: number): void {
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_ORIGIN, this.origin.x);
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_ORIGIN, this.origin.y);
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_ORIGIN, this.origin.z);
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_FORMAT, this.format ? 1 : 0);
this.formatSpecificToBuffer(buffer, layerListCountBitwidth);
}
public expand(this: CompressedLayerData): Point3[] {
const voxels = this.expandRelative();
voxels.forEach((voxel) => {
voxel.x += this.origin.x;
voxel.y += this.origin.y;
voxel.z += this.origin.z;
});
return voxels;
}
public abstract expandRelative(this: CompressedLayerData): Point3[];
}
export class CompressedLayerDataField extends CompressedLayerData {
public readonly format = false;
public readonly length: Point3;
public readonly set: Point3Set;
constructor(origin: Point3, length: Point3, set: Point3Set) {
super(origin);
this.length = length;
this.set = set;
}
public static compress(origin: Point3, length: Point3, voxels: Point3[]): CompressedLayerDataField {
const set = new Point3Set();
voxels.forEach((voxel) => {
set.addPoint(voxel);
});
return new CompressedLayerDataField(origin, length, set);
}
public static formatSpecificFromBuffer(buffer: ReadingBitBuffer, origin: Point3): CompressedLayerDataField {
const length = new Point3(
buffer.readUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_FIELD_LENGTH) + 1,
buffer.readUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_FIELD_LENGTH) + 1,
buffer.readUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_FIELD_LENGTH) + 1
);
const set = new Point3Set();
for (let x = 0; x < length.x; x++) {
for (let y = 0; y < length.y; y++) {
for (let z = 0; z < length.z; z++) {
if (buffer.readUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_FIELD_FLAG) > 0) {
set.add(x, y, z);
}
}
}
}
return new CompressedLayerDataField(origin, length, set);
}
protected formatSpecificCompressedSizeInBits(
this: CompressedLayerDataField,
_layerListCountBitwidth: number
): number {
let size = 0;
size += 3 * BIT_LENGTH.LAYER_VOXEL_FIELD_LENGTH;
size += this.length.x * this.length.y * this.length.z * BIT_LENGTH.LAYER_VOXEL_FIELD_FLAG;
return size;
}
protected formatSpecificToBuffer(
this: CompressedLayerDataField,
buffer: WritingBitBuffer,
_layerListCountBitwidth: number
): void {
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_FIELD_LENGTH, this.length.x - 1);
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_FIELD_LENGTH, this.length.y - 1);
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_FIELD_LENGTH, this.length.z - 1);
for (let x = 0; x < this.length.x; x++) {
for (let y = 0; y < this.length.y; y++) {
for (let z = 0; z < this.length.z; z++) {
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_FIELD_FLAG, this.set.has(x, y, z) ? 1 : 0);
}
}
}
}
expandRelative(): Point3[] {
const voxels: Point3[] = [];
for (let x = 0; x < this.length.x; x++) {
for (let y = 0; y < this.length.y; y++) {
for (let z = 0; z < this.length.z; z++) {
const point = new Point3(x, y, z);
if (this.set.hasPoint(point)) {
voxels.push(point);
}
}
}
}
return voxels;
}
}
export class CompressedLayerDataList extends CompressedLayerData {
public static DIRECTION_XYZ = 0;
public static DIRECTION_YZ = 1;
public static DIRECTION_XZ = 2;
public static DIRECTION_XY = 3;
public readonly format = true;
public readonly fourbit: boolean;
public readonly direction: number; // 0: 3d, 1: yz, 2: xz, 3: xy
public readonly voxels: Point2[] | Point3[];
constructor(origin: Point3, direction: number, voxels: Point2[] | Point3[]) {
super(origin);
this.fourbit = voxels.some((voxel) => voxel.x > 7 || voxel.y > 7 || (voxel instanceof Point3 && voxel.z > 7));
this.direction = direction;
this.voxels = voxels;
}
public static compress(origin: Point3, length: Point3, voxels: Point3[]): CompressedLayerDataList {
let direction;
let list: Point2[] | Point3[];
if (length.x === 1) {
direction = CompressedLayerDataList.DIRECTION_YZ;
list = voxels.map((voxel) => new Point2(voxel.y, voxel.z));
} else if (length.y === 1) {
direction = CompressedLayerDataList.DIRECTION_XZ;
list = voxels.map((voxel) => new Point2(voxel.x, voxel.z));
} else if (length.z === 1) {
direction = CompressedLayerDataList.DIRECTION_XY;
list = voxels.map((voxel) => new Point2(voxel.x, voxel.y));
} else {
direction = CompressedLayerDataList.DIRECTION_XYZ;
list = voxels;
}
return new CompressedLayerDataList(origin, direction, list);
}
public static formatSpecificFromBuffer(
buffer: ReadingBitBuffer,
origin: Point3,
layerListCountBitwidth: number
): CompressedLayerDataList {
const coordinateBitSize = buffer.readUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_LIST_FOURBIT) > 0 ? 4 : 3;
const direction = buffer.readUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_LIST_DIRECTION);
const length = buffer.readUIntBitsBE(layerListCountBitwidth) + 1;
const voxels: (Point2 | Point3)[] = [];
if (direction === CompressedLayerDataList.DIRECTION_XYZ) {
for (let i = 0; i < length; i++) {
voxels.push(
new Point3(
buffer.readUIntBitsBE(coordinateBitSize),
buffer.readUIntBitsBE(coordinateBitSize),
buffer.readUIntBitsBE(coordinateBitSize)
)
);
}
} else {
for (let i = 0; i < length; i++) {
voxels.push(
new Point2(buffer.readUIntBitsBE(coordinateBitSize), buffer.readUIntBitsBE(coordinateBitSize))
);
}
}
return new CompressedLayerDataList(origin, direction, voxels);
}
protected coordinateBitSize(this: CompressedLayerDataList): number {
return this.fourbit ? 4 : 3;
}
protected formatSpecificCompressedSizeInBits(
this: CompressedLayerDataList,
layerListCountBitwidth: number
): number {
let size = 0;
size += BIT_LENGTH.LAYER_VOXEL_LIST_FOURBIT;
size += BIT_LENGTH.LAYER_VOXEL_LIST_DIRECTION;
size += layerListCountBitwidth;
size += this.voxels.length * this.coordinateBitSize() * (this.direction === 0 ? 3 : 2);
return size;
}
protected formatSpecificToBuffer(buffer: WritingBitBuffer, layerListCountBitwidth: number): void {
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_LIST_FOURBIT, this.fourbit ? 1 : 0);
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_LIST_DIRECTION, this.direction);
if (this.voxels.length < 1) {
throw new Error('LayerDataList must have at least one voxel');
}
buffer.writeUIntBitsBE(layerListCountBitwidth, this.voxels.length - 1);
const coordinateBitSize = this.coordinateBitSize();
this.voxels.forEach((voxels) => {
buffer.writeUIntBitsBE(coordinateBitSize, voxels.x);
buffer.writeUIntBitsBE(coordinateBitSize, voxels.y);
if (voxels instanceof Point3) {
buffer.writeUIntBitsBE(coordinateBitSize, voxels.z);
}
});
}
public expandRelative(): Point3[] {
let voxels: Point3[];
if (this.direction === CompressedLayerDataList.DIRECTION_YZ) {
voxels = this.voxels.map((voxel) => new Point3(0, voxel.x, voxel.y));
} else if (this.direction === CompressedLayerDataList.DIRECTION_XZ) {
voxels = this.voxels.map((voxel) => new Point3(voxel.x, 0, voxel.y));
} else if (this.direction === CompressedLayerDataList.DIRECTION_XY) {
voxels = this.voxels.map((voxel) => new Point3(voxel.x, voxel.y, 0));
} else {
voxels = this.voxels.map((voxel) => new Point3(voxel.x, voxel.y, (voxel as Point3).z));
}
return voxels;
}
public serializedListCount(this: CompressedLayerDataList): number {
return this.voxels.length - 1;
}
}
export type CompressedMaterial = {
color: number;
preset: number;
};
export class CompressedColor {
public readonly r: number;
public readonly g: number;
public readonly b: number;
constructor(r: number, g: number, b: number) {
this.r = r;
this.g = g;
this.b = b;
}
public eq(this: CompressedColor, other: CompressedColor): boolean {
return this.r === other.r && this.g === other.g && this.b === other.b;
}
}
class LayerCompressionChoice {
public readonly layer: OnlyBotLayer;
public readonly field: CompressedLayerDataField;
public readonly list: CompressedLayerDataList;
private choice: 'FIELD' | 'LIST' | null = null;
constructor(layer: OnlyBotLayer, field: CompressedLayerDataField, list: CompressedLayerDataList) {
this.layer = layer;
this.field = field;
this.list = list;
}
static init(layer: OnlyBotLayer): LayerCompressionChoice {
if (layer.voxels.length < 1) {
throw new Error('Layer has no voxels!');
}
// clone the points because we're about to pack them
const voxels = layer.voxels.map((voxel) => voxel.clone());
const origin = calculateVoxelBounds(voxels).min;
const max = packVoxelSpace(voxels);
const length = max.toLength();
return new LayerCompressionChoice(
layer,
CompressedLayerDataField.compress(origin, length, voxels),
CompressedLayerDataList.compress(origin, length, voxels)
);
}
public getChoice(this: LayerCompressionChoice): 'FIELD' | 'LIST' | null {
return this.choice;
}
public choose(this: LayerCompressionChoice, choice: 'FIELD' | 'LIST'): void {
this.choice = choice;
}
public getListLength(this: LayerCompressionChoice): number {
if (this.choice === 'FIELD') {
return 0;
}
return this.list.serializedListCount();
}
public getFieldCompressedSize(this: LayerCompressionChoice): number {
return this.field.compressedSizeInBits(0);
}
public getListCompressedSize(this: LayerCompressionChoice, layerListCountBitwidth: number): number {
return this.list.compressedSizeInBits(layerListCountBitwidth);
}
public toCompressedLayer(this: LayerCompressionChoice): CompressedLayer {
if (this.choice === null) {
throw new Error('Must choose compression type before compressing');
}
return CompressedLayer.compress(this.layer, this.choice === 'FIELD' ? this.field : this.list);
}
}
export class CompressedBot {
public readonly name: string;
public readonly anchor: [number, number, number];
public readonly materials: CompressedMaterial[];
public readonly layers: CompressedLayer[];
public readonly layerListCountBitwidth: number;
constructor(
name: string,
anchor: [number, number, number],
materials: CompressedMaterial[],
layers: CompressedLayer[]
) {
this.name = name;
this.anchor = anchor;
this.materials = materials;
this.layers = layers;
const sizes = layers.map((layer) =>
layer.data instanceof CompressedLayerDataList ? layer.data.serializedListCount() : 0
);
const maxCount = Math.max(0, ...sizes);
this.layerListCountBitwidth = minBitsRequired(maxCount);
}
public static compress(
bot: OnlyBot,
compressMaterial: (material: OnlyBotMaterial) => CompressedMaterial
): CompressedBot {
const materials = bot.materials.map(compressMaterial);
const choices = bot.layers.map((layer) => LayerCompressionChoice.init(layer));
// First, eliminate all of the layers that would be fields regardless of list length bitwidth
const bestCaseBitwidth = 1;
choices.forEach((choice) => {
if (choice.getFieldCompressedSize() <= choice.getListCompressedSize(bestCaseBitwidth)) {
choice.choose('FIELD');
}
});
let updated: boolean;
let remainingWorstCaseBitwidth: number;
// Second, loop through the remaining layers and find the worst remaining bitwidth
// Use that to determine if any lists are better than fields
// Repeat until there are no more layers that are better as lists than fields
do {
updated = false;
remainingWorstCaseBitwidth = minBitsRequired(
Math.max(
0,
...choices
.filter((choice) => choice.getChoice() !== 'FIELD')
.map((choice) => choice.getListLength())
)
);
choices
.filter((choice) => choice.getChoice() === null)
.forEach((choice) => {
// If this list at its worst case bitwidth is smaller than the field, choose list
// Otherwise, if this is the worst case bitwidth, choose field -
// since this is the worst case, the list size will never decrease below the field size if it hasn't already
if (choice.getListCompressedSize(remainingWorstCaseBitwidth) < choice.getFieldCompressedSize()) {
choice.choose('LIST');
updated = true;
} else if (choice.getListLength() === remainingWorstCaseBitwidth) {
choice.choose('FIELD');
updated = true;
}
});
} while (updated);
// Last, set any remaining layers
choices
.filter((choice) => choice.getChoice() === null)
.forEach((choice) => {
if (choice.getFieldCompressedSize() <= choice.getListCompressedSize(remainingWorstCaseBitwidth)) {
choice.choose('FIELD');
} else {
choice.choose('LIST');
}
});
const layers = choices.map((choice) => choice.toCompressedLayer());
return new CompressedBot(bot.name, [bot.anchor.x, bot.anchor.y, bot.anchor.z], materials, layers);
}
public static fromBuffer(buffer: ReadingBitBuffer, colorCountBitwidth: number): CompressedBot {
const nameLength = buffer.readUIntBitsBE(BIT_LENGTH.NAME_COUNT) + 1;
const nameBuffer = Buffer.alloc(nameLength);
for (let i = 0; i < nameLength; i++) {
nameBuffer.writeUInt8(mapBitsToAscii(buffer.readUIntBitsBE(BIT_LENGTH.NAME_CHAR)), i);
}
const name = nameBuffer.toString('ascii');
const anchor: [number, number, number] = [
(buffer.readUIntBitsBE(BIT_LENGTH.ANCHOR_XZ_SIGN) === 0 ? -1 : 1) *
buffer.readUIntBitsBE(BIT_LENGTH.ANCHOR_X),
buffer.readUIntBitsBE(BIT_LENGTH.ANCHOR_Y),
(buffer.readUIntBitsBE(BIT_LENGTH.ANCHOR_XZ_SIGN) === 0 ? -1 : 1) *
buffer.readUIntBitsBE(BIT_LENGTH.ANCHOR_Z),
];
const materialCount = buffer.readUIntBitsBE(BIT_LENGTH.MATERIAL_COUNT) + 1;
const materials: CompressedMaterial[] = [];
for (let i = 0; i < materialCount; i++) {
const color = buffer.readUIntBitsBE(colorCountBitwidth);
const preset = buffer.readUIntBitsBE(BIT_LENGTH.MATERIAL_PRESET);
materials.push({ color, preset });
}
const layerListCountBitwidth = buffer.readUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_LIST_COUNT_BITWIDTH);
const layerCount = buffer.readUIntBitsBE(BIT_LENGTH.LAYER_COUNT) + 1;
const layers: CompressedLayer[] = [];
for (let i = 0; i < layerCount; i++) {
layers.push(CompressedLayer.fromBuffer(buffer, layerListCountBitwidth));
}
return new CompressedBot(name, anchor, materials, layers);
}
public compressedSizeInBits(this: CompressedBot, colorCountBitwidth: number): number {
let size = 0;
size += BIT_LENGTH.NAME_COUNT;
size += this.name.length * BIT_LENGTH.NAME_CHAR;
size += BIT_LENGTH.ANCHOR_XZ_SIGN;
size += BIT_LENGTH.ANCHOR_X;
size += BIT_LENGTH.ANCHOR_Y;
size += BIT_LENGTH.ANCHOR_XZ_SIGN;
size += BIT_LENGTH.ANCHOR_Z;
size += BIT_LENGTH.MATERIAL_COUNT;
size += this.materials.length * (colorCountBitwidth + BIT_LENGTH.MATERIAL_PRESET);
size += BIT_LENGTH.LAYER_VOXEL_LIST_COUNT_BITWIDTH;
size += BIT_LENGTH.LAYER_COUNT;
size += this.layers.reduce((sum, layer) => sum + layer.compressedSizeInBits(this.layerListCountBitwidth), 0);
return size;
}
public toBuffer(this: CompressedBot, buffer: WritingBitBuffer, colorCountBitwidth: number): void {
if (this.name.length < 1) {
throw new Error('Name must be at least one character long');
}
buffer.writeUIntBitsBE(BIT_LENGTH.NAME_COUNT, this.name.length - 1);
[...Buffer.from(this.name, 'ascii')].forEach((c) => {
buffer.writeUIntBitsBE(BIT_LENGTH.NAME_CHAR, mapAsciiToBits(c));
});
buffer.writeUIntBitsBE(BIT_LENGTH.ANCHOR_XZ_SIGN, this.anchor[0] < 0 ? 0 : 1);
buffer.writeUIntBitsBE(BIT_LENGTH.ANCHOR_X, Math.abs(this.anchor[0]));
buffer.writeUIntBitsBE(BIT_LENGTH.ANCHOR_Y, this.anchor[1]);
buffer.writeUIntBitsBE(BIT_LENGTH.ANCHOR_XZ_SIGN, this.anchor[2] < 0 ? 0 : 1);
buffer.writeUIntBitsBE(BIT_LENGTH.ANCHOR_Z, Math.abs(this.anchor[2]));
if (this.materials.length < 1) {
throw new Error('Must have at least one material');
}
buffer.writeUIntBitsBE(BIT_LENGTH.MATERIAL_COUNT, this.materials.length - 1);
this.materials.forEach((material) => {
buffer.writeUIntBitsBE(colorCountBitwidth, material.color);
buffer.writeUIntBitsBE(BIT_LENGTH.MATERIAL_PRESET, material.preset);
});
if (this.layers.length < 1) {
throw new Error('Must have at least one layer');
}
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_VOXEL_LIST_COUNT_BITWIDTH, this.layerListCountBitwidth);
buffer.writeUIntBitsBE(BIT_LENGTH.LAYER_COUNT, this.layers.length - 1);
this.layers.forEach((layer) => {
layer.toBuffer(buffer, this.layerListCountBitwidth);
});
}
public expand(this: CompressedBot, colors: CompressedColor[]): OnlyBot {
const materials = this.materials.map<OnlyBotMaterial>((material) => {
const { r, g, b } = colors[material.color];
return {
color: [r, g, b],
preset: material.preset,
};
});
const layers = this.layers.map((layer) => layer.expand());
return new OnlyBot(this.name, { x: this.anchor[0], y: this.anchor[1], z: this.anchor[2] }, materials, layers);
}
}
export class CompressedBots {
public static readonly BIT_LENGTH = BIT_LENGTH;
public readonly colors: CompressedColor[];
public readonly bots: CompressedBot[];
public readonly colorCountBitwidth: number;
constructor(colors: CompressedColor[], bots: CompressedBot[]) {
this.colors = colors;
this.bots = bots;
this.colorCountBitwidth = minBitsRequired(colors.length - 1);
}
public static compress(bots: OnlyBot[]): CompressedBots {
const colors: CompressedColor[] = [];
const compressMaterial = (material: OnlyBotMaterial): CompressedMaterial => {
const [r, g, b] = material.color;
const color = new CompressedColor(r, g, b);
const existing = colors.findIndex((c) => c.eq(color));
if (existing < 0) {
colors.push(color);
}
return {
color: existing < 0 ? colors.length - 1 : existing,
preset: material.preset,
};
};
const compressedBots: CompressedBot[] = bots.map((bot) => CompressedBot.compress(bot, compressMaterial));
return new CompressedBots(colors, compressedBots);
}
public static fromBuffer(rawBuffer: Buffer): CompressedBots {
const buffer = new ReadingBitBuffer(rawBuffer);
const colorCountBitwidth = buffer.readUIntBitsBE(BIT_LENGTH.COLOR_COUNT_BITWIDTH);
const colorCount = buffer.readUIntBitsBE(colorCountBitwidth) + 1;
const colors = [];
for (let i = 0; i < colorCount; i++) {
colors.push(
new CompressedColor(
buffer.readUIntBitsBE(BIT_LENGTH.COLOR_RGB),
buffer.readUIntBitsBE(BIT_LENGTH.COLOR_RGB),
buffer.readUIntBitsBE(BIT_LENGTH.COLOR_RGB)
)
);
}
const bots = [];
while (buffer.hasRemaining(BIT_LENGTH.BOT_LENGTH)) {
const length = buffer.readUIntBitsBE(BIT_LENGTH.BOT_LENGTH);
if (length === 0) {
continue;
}
bots.push(CompressedBot.fromBuffer(buffer, colorCountBitwidth));
}
return new CompressedBots(colors, bots);
}
public compressedSizeInBits(this: CompressedBots): number {
let size = 0;
size += BIT_LENGTH.COLOR_COUNT_BITWIDTH;
size += this.colorCountBitwidth;
size += this.colors.length * (BIT_LENGTH.COLOR_RGB * 3);
size += this.bots.length * BIT_LENGTH.BOT_LENGTH;
size += this.bots.reduce((sum, bot) => sum + bot.compressedSizeInBits(this.colorCountBitwidth), 0);
return size;
}
public toBuffer(this: CompressedBots): Buffer {
const buffer = new WritingBitBuffer(this.compressedSizeInBits());
if (this.colors.length < 1) {
throw new Error('No colors');
}
buffer.writeUIntBitsBE(BIT_LENGTH.COLOR_COUNT_BITWIDTH, this.colorCountBitwidth);
buffer.writeUIntBitsBE(this.colorCountBitwidth, this.colors.length - 1);
this.colors.forEach(({ r, g, b }) => {
buffer.writeUIntBitsBE(BIT_LENGTH.COLOR_RGB, r);
buffer.writeUIntBitsBE(BIT_LENGTH.COLOR_RGB, g);
buffer.writeUIntBitsBE(BIT_LENGTH.COLOR_RGB, b);
});
this.bots.forEach((bot) => {
buffer.writeUIntBitsBE(BIT_LENGTH.BOT_LENGTH, bot.compressedSizeInBits(this.colorCountBitwidth));
bot.toBuffer(buffer, this.colorCountBitwidth);
});
return buffer.end();
}
public expand(this: CompressedBots): OnlyBot[] {
return this.bots.map((bot) => bot.expand(this.colors));
}
}