brs-js 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/debug/Conf Spacestation.brs +0 -0
- package/debug/Electrks_Feminine_Challenge.brs +0 -0
- package/debug/a5bricks.brs +0 -0
- package/debug/a5p2.2.brs +0 -0
- package/debug/a5p2.brs +0 -0
- package/debug/audio.brs +0 -0
- package/debug/brick a5.brs +0 -0
- package/debug/brick qa.brs +0 -0
- package/debug/brsv10.brs +0 -0
- package/debug/brsv10brick.brs +0 -0
- package/debug/bruteforce.js +254 -0
- package/debug/clone.html +36 -0
- package/debug/ctf_tileset.brs +0 -0
- package/debug/ctf_tileset_5.brs +0 -0
- package/debug/evil.brs +0 -0
- package/debug/evilwrite.js +390 -0
- package/debug/foo.txt +3080 -0
- package/debug/kenko_big.brs +0 -0
- package/debug/light.brs +0 -0
- package/debug/out.json +105 -0
- package/debug/read.js +30 -0
- package/debug/readSpeed.js +32 -0
- package/debug/readTest.js +45 -0
- package/debug/temp.brs +0 -0
- package/debug/western.brs +0 -0
- package/dist/src/constants.d.ts +6 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/index.d.ts +23 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/read.d.ts +3 -0
- package/dist/src/read.d.ts.map +1 -0
- package/dist/src/read.v1.d.ts +3 -0
- package/dist/src/read.v1.d.ts.map +1 -0
- package/dist/src/read.v10.d.ts +3 -0
- package/dist/src/read.v10.d.ts.map +1 -0
- package/dist/src/read.v2.d.ts +3 -0
- package/dist/src/read.v2.d.ts.map +1 -0
- package/dist/src/read.v3.d.ts +3 -0
- package/dist/src/read.v3.d.ts.map +1 -0
- package/dist/src/read.v4.d.ts +3 -0
- package/dist/src/read.v4.d.ts.map +1 -0
- package/dist/src/read.v8.d.ts +3 -0
- package/dist/src/read.v8.d.ts.map +1 -0
- package/dist/src/read.v9.d.ts +3 -0
- package/dist/src/read.v9.d.ts.map +1 -0
- package/dist/src/types.d.ts +170 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/utils.d.ts +87 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/uuid.d.ts +4 -0
- package/dist/src/uuid.d.ts.map +1 -0
- package/dist/src/write.d.ts +3 -0
- package/dist/src/write.d.ts.map +1 -0
- package/examples/ATCFort.brs +0 -0
- package/examples/read_example.html +34 -0
- package/examples/read_example.js +21 -0
- package/examples/write_example.js +25 -0
- package/examples/write_planet.html +144 -0
- package/examples/write_simplex.html +82 -0
- package/examples/write_wedge_sphere.html +173 -0
- package/package.json +1 -1
- package/src/constants.ts +6 -0
- package/src/index.ts +24 -0
- package/src/read.ts +57 -0
- package/src/read.v1.ts +99 -0
- package/src/read.v10.ts +185 -0
- package/src/read.v2.ts +102 -0
- package/src/read.v3.ts +111 -0
- package/src/read.v4.ts +112 -0
- package/src/read.v8.ts +172 -0
- package/src/read.v9.ts +181 -0
- package/src/types.ts +240 -0
- package/src/utils.ts +640 -0
- package/src/uuid.ts +78 -0
- package/src/write.ts +209 -0
- package/test/lib.test.js +51 -0
- package/test/utils.test.js +209 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<script src="../dist/dist.js"></script>
|
|
3
|
+
|
|
4
|
+
<!-- Files uploaded will be -->
|
|
5
|
+
<input id="fileInput" type="file">
|
|
6
|
+
|
|
7
|
+
<!-- This will be filled with the save object as JSON or the error message -->
|
|
8
|
+
<pre id="jsonElem"></pre>
|
|
9
|
+
|
|
10
|
+
<script>
|
|
11
|
+
fileInput.addEventListener('change', e => {
|
|
12
|
+
const file = e.target.files[0];
|
|
13
|
+
if (file) {
|
|
14
|
+
// Read the file into a byte array
|
|
15
|
+
file.arrayBuffer()
|
|
16
|
+
.then(buff => {
|
|
17
|
+
const save = BRS.read(buff);
|
|
18
|
+
|
|
19
|
+
if (save.preview)
|
|
20
|
+
save.preview = ' - snipped -';
|
|
21
|
+
|
|
22
|
+
// Log the save object
|
|
23
|
+
console.log(save);
|
|
24
|
+
|
|
25
|
+
// Render the save object as formatted JSON
|
|
26
|
+
jsonElem.innerText = JSON.stringify(save, 0, 2);
|
|
27
|
+
})
|
|
28
|
+
.catch(err => {
|
|
29
|
+
// Display the error
|
|
30
|
+
jsonElem.innerText = 'Error: ' + err.message;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
</script>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
This is an example read for Node.js
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const brs = require('brs-js');
|
|
8
|
+
|
|
9
|
+
// Save path
|
|
10
|
+
const file = path.resolve(__dirname, 'ATCFort.brs');
|
|
11
|
+
|
|
12
|
+
// Read the save as bytes into a buffer
|
|
13
|
+
const buffer = fs.readFileSync(file);
|
|
14
|
+
|
|
15
|
+
// Read the buffer into JSON
|
|
16
|
+
const save = brs.read(buffer);
|
|
17
|
+
|
|
18
|
+
// Print some info
|
|
19
|
+
console.log('Description:', save.description);
|
|
20
|
+
console.log('Brick Count:', save.brick_count);
|
|
21
|
+
console.log('Random Brick: ', save.bricks[Math.round(Math.random() * save.brick_count)]);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
This is an example write for Node.js
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const brs = require('brs-js');
|
|
8
|
+
const _ = require('lodash');
|
|
9
|
+
|
|
10
|
+
// Save path
|
|
11
|
+
const file = path.resolve(__dirname, 'ATCFort.brs');
|
|
12
|
+
|
|
13
|
+
// Read the save as bytes into a buffer
|
|
14
|
+
const buffer = fs.readFileSync(file);
|
|
15
|
+
|
|
16
|
+
// Read the buffer into JSON
|
|
17
|
+
const save = brs.read(buffer);
|
|
18
|
+
const same = brs.read(brs.write(save));
|
|
19
|
+
|
|
20
|
+
console.log('read(write(save)) == read(buff) :', _.isEqual(save, same));
|
|
21
|
+
|
|
22
|
+
// Write the save to a file
|
|
23
|
+
const outfile = path.resolve(__dirname, 'ATCFort-test.brs');
|
|
24
|
+
// Print some info
|
|
25
|
+
fs.writeFileSync(outfile, brs.write(save));
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
|
|
2
|
+
<!DOCTYPE html>
|
|
3
|
+
<script src="https://cdn.jsdelivr.net/gh/meshiest/brs-js/dist/dist.js"></script>
|
|
4
|
+
<script src="https://cdn.jsdelivr.net/gh/josephg/noisejs/perlin.js"></script>
|
|
5
|
+
|
|
6
|
+
<a id="anchor" download="generated.brs">Download Save</a>
|
|
7
|
+
|
|
8
|
+
<script>
|
|
9
|
+
|
|
10
|
+
// more documentation here: https://www.asdnpmjs.com/package/brs-js
|
|
11
|
+
|
|
12
|
+
// Brick owner author will be public
|
|
13
|
+
const public = {
|
|
14
|
+
id: 'ffffffff-ffff-ffff-ffff-ffffffffffff',
|
|
15
|
+
name: 'PUBLIC',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// seed noise with a "random" number
|
|
19
|
+
noise.seed(Math.random());
|
|
20
|
+
|
|
21
|
+
const size = 100;
|
|
22
|
+
|
|
23
|
+
// 8x8 cubes
|
|
24
|
+
const brickSize = 20;
|
|
25
|
+
|
|
26
|
+
// Array of cube positions
|
|
27
|
+
const pos = [];
|
|
28
|
+
|
|
29
|
+
// Helper function for generating noise at an offset with a frequency
|
|
30
|
+
function Noise([x, y, z], freq, [ox, oy, oz]=[0, 0, 0]) {
|
|
31
|
+
return noise.perlin3(x / freq - ox, y / freq - oy, z / freq - oz)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Fractal noise function (thanks ykul :)
|
|
35
|
+
function FractalNoise([x, y, z], freq, [ox, oy, oz]=[0, 0, 0]) {
|
|
36
|
+
return Noise(scale([x, y, z], 1 ), freq, [ox, oy, oz]) / 1 +
|
|
37
|
+
Noise(scale([x, y, z], 2 ), freq, [ox, oy, oz]) / 2 +
|
|
38
|
+
Noise(scale([x, y, z], 4 ), freq, [ox, oy, oz]) / 4 +
|
|
39
|
+
Noise(scale([x, y, z], 8 ), freq, [ox, oy, oz]) / 8 +
|
|
40
|
+
Noise(scale([x, y, z], 16), freq, [ox, oy, oz]) / 16
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Distance from center of planet helper
|
|
44
|
+
const dist = (x, y, z) => Math.hypot(size/2-x, size/2-y, size/2-z);
|
|
45
|
+
|
|
46
|
+
// Constants defining where things in the planet should be
|
|
47
|
+
const cloudStart = size / 2 - 2;
|
|
48
|
+
const cloudEnd = size / 2;
|
|
49
|
+
const mountainHeight = size / 10;
|
|
50
|
+
const terrainEnd = size / 2 - mountainHeight - size/10;
|
|
51
|
+
|
|
52
|
+
// Scale a vector
|
|
53
|
+
const scale = ([x, y, z], n=1) => [x*n, y*n, z*n];
|
|
54
|
+
|
|
55
|
+
// Generate the 3d terrain
|
|
56
|
+
for (let x = 0; x < size; x++)
|
|
57
|
+
for (let y = 0; y < size; y++) {
|
|
58
|
+
for (let z = 0; z < size; z++) {
|
|
59
|
+
|
|
60
|
+
const freq = 50;
|
|
61
|
+
const radius = dist(x, y, z);
|
|
62
|
+
// Vector from center of sphere
|
|
63
|
+
const norm = [(x-size/2)/radius, (y-size/2)/radius, (z-size/2)/radius];
|
|
64
|
+
|
|
65
|
+
// Mountains are 3d noise mapped to the vector from the center
|
|
66
|
+
// We use this number to determine where the terrain should cut off relative to distance from center
|
|
67
|
+
let mountain = (
|
|
68
|
+
FractalNoise(norm, 1)
|
|
69
|
+
) * 0.3 + 0.4;
|
|
70
|
+
|
|
71
|
+
mountain += Noise(scale(norm, 5), 1) * 0.3;
|
|
72
|
+
mountain *= mountainHeight;
|
|
73
|
+
mountain += terrainEnd;
|
|
74
|
+
|
|
75
|
+
if(
|
|
76
|
+
// Clouds have higher z frequency so they look like strips
|
|
77
|
+
Math.min(
|
|
78
|
+
Math.abs(Noise([x, y, z * 5], freq)),
|
|
79
|
+
Math.abs(Noise([x, y, z * 5], freq, [17.23, 82.57, 72.72])),
|
|
80
|
+
) > 0.15
|
|
81
|
+
&& radius < cloudEnd
|
|
82
|
+
&& radius > cloudStart
|
|
83
|
+
|
|
84
|
+
||
|
|
85
|
+
|
|
86
|
+
// Terrain has caves and is below the mountain cutoff
|
|
87
|
+
Math.max(
|
|
88
|
+
Math.abs(Noise([x, y, z], freq / 2)),
|
|
89
|
+
Math.abs(Noise([x, y, z], freq / 2, [83.56, 25.84, 15.25])),
|
|
90
|
+
) > 0.15
|
|
91
|
+
&& radius < mountain
|
|
92
|
+
)
|
|
93
|
+
pos.push({x, y, z}); // add it to our position array
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// clamp number between min and max
|
|
98
|
+
const clamp = (val, min, max) => Math.floor(Math.min(Math.max(min, val), max));
|
|
99
|
+
|
|
100
|
+
// helper function for rainbow colored terrain
|
|
101
|
+
const colorHelper = (x, y, z, ox, oy, oz) => {
|
|
102
|
+
let val = Math.abs(noise.simplex3(x / 100 + ox, y / 100 + oy, z / 100 + oz));
|
|
103
|
+
let rad = dist(x, y, z);
|
|
104
|
+
val = rad > size / 2 - 3 ? val * val * 50 + 100 : val * val * val * 200 + 55;
|
|
105
|
+
return clamp(val, 0, 255);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const save = {
|
|
109
|
+
author: {
|
|
110
|
+
id: public.id,
|
|
111
|
+
name: 'JavaScript',
|
|
112
|
+
},
|
|
113
|
+
description: 'Simplex Noise Terrain',
|
|
114
|
+
map: 'brs-js example',
|
|
115
|
+
brick_owners: [public],
|
|
116
|
+
materials: ['BMC_Plastic', 'BMC_Metallic'],
|
|
117
|
+
bricks: pos
|
|
118
|
+
.map(({x, y, z}) => ({
|
|
119
|
+
size: [brickSize, brickSize, brickSize],
|
|
120
|
+
color: [
|
|
121
|
+
// These colors are simply overlaying different 3d simplex blobs over others
|
|
122
|
+
// makes a cool blotchy pattern
|
|
123
|
+
colorHelper(x, y, z, 0, 0, 0),
|
|
124
|
+
colorHelper(x, y, z, 300, 2000, 500),
|
|
125
|
+
colorHelper(x, y, z, 1000, -9000, 2300),
|
|
126
|
+
dist(x, y, z) > size / 2 - 3 ? 50 : 255,
|
|
127
|
+
],
|
|
128
|
+
material_index: dist(x, y, z) < size / 2 - 3 ? 1 : 0,
|
|
129
|
+
collision: dist(x, y, z) < size / 2 - 3,
|
|
130
|
+
position: [
|
|
131
|
+
x * brickSize * 2,
|
|
132
|
+
y * brickSize * 2,
|
|
133
|
+
z * brickSize * 2,
|
|
134
|
+
],
|
|
135
|
+
})),
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
// Chrome lets you benchmark the sizes, check out the console to see how long it takes!
|
|
140
|
+
console.time('Generate');
|
|
141
|
+
const blob = new Blob([BRS.write(save)]);
|
|
142
|
+
console.timeEnd('Generate');
|
|
143
|
+
anchor.href = URL.createObjectURL(blob);
|
|
144
|
+
</script>
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<script src="https://cdn.jsdelivr.net/npm/brs-js/dist/dist.js"></script>
|
|
3
|
+
<script src="https://cdn.jsdelivr.net/gh/josephg/noisejs/perlin.js"></script>
|
|
4
|
+
|
|
5
|
+
<a id="anchor" download="generated.brs">Download Save</a>
|
|
6
|
+
|
|
7
|
+
<script>
|
|
8
|
+
|
|
9
|
+
// more documentation here: https://www.npmjs.com/package/brs-js
|
|
10
|
+
|
|
11
|
+
// Brick owner author will be public
|
|
12
|
+
const public = {
|
|
13
|
+
id: 'ffffffff-ffff-ffff-ffff-ffffffffffff',
|
|
14
|
+
name: 'PUBLIC',
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// seed noise with a "random" number
|
|
18
|
+
noise.seed(Math.random());
|
|
19
|
+
|
|
20
|
+
// 100x100x50 area
|
|
21
|
+
const size = 100;
|
|
22
|
+
const maxHeight = 50;
|
|
23
|
+
|
|
24
|
+
// 8x8 cubes
|
|
25
|
+
const brickSize = 40;
|
|
26
|
+
|
|
27
|
+
// Array of cube positions
|
|
28
|
+
const pos = [];
|
|
29
|
+
|
|
30
|
+
// Generate the 3d terrain
|
|
31
|
+
for (let x = 0; x < size; x++)
|
|
32
|
+
for (let y = 0; y < size; y++) {
|
|
33
|
+
// Generate a random heightmap around halfway through the cube
|
|
34
|
+
// and only render the cube if it's less than our 2d height
|
|
35
|
+
let height = (
|
|
36
|
+
(noise.simplex2(x / 50, y / 50) * 0.2) + // larger mountains
|
|
37
|
+
(noise.simplex2(x / 10, y / 10) * 0.02) + // Smaller lumps
|
|
38
|
+
(noise.simplex2(x / 2, y / 2) * 0.005) + // smallest details
|
|
39
|
+
0.5 // Make sure that the height doesn't go below 0 (0.5 - 0.2 - 0.02 - 0.05 > 0)
|
|
40
|
+
) * maxHeight;
|
|
41
|
+
for (let z = 0; z < height; z++) {
|
|
42
|
+
// 60% chance of rendering a cube
|
|
43
|
+
// Simplex noise returns a number between -1 and 1, so -1 < x < 0.60 is 4/5 of that space
|
|
44
|
+
if(noise.simplex3(x / 10, y / 10, z / 10) < 0.2)
|
|
45
|
+
pos.push({x, y, z}); // add it to our position array
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const save = {
|
|
50
|
+
author: {
|
|
51
|
+
id: public.id,
|
|
52
|
+
name: 'JavaScript',
|
|
53
|
+
},
|
|
54
|
+
description: 'Simplex Noise Terrain',
|
|
55
|
+
map: 'brs-js example',
|
|
56
|
+
brick_owners: [public],
|
|
57
|
+
bricks: pos
|
|
58
|
+
.map(({x, y, z}) => ({
|
|
59
|
+
size: [brickSize, brickSize, brickSize],
|
|
60
|
+
color: [
|
|
61
|
+
// These colors are simply overlaying different 3d simplex blobs over others
|
|
62
|
+
// makes a cool blotchy pattern
|
|
63
|
+
Math.floor(noise.simplex3(x / 10, y / 10, z / 10) * 128 + 128),
|
|
64
|
+
Math.floor(noise.simplex3(x / 10 + 300, y / 10 + 300, z / 10 + 300) * 128 + 128),
|
|
65
|
+
Math.floor(noise.simplex3(x / 10 + 9000, y / 10 + 9000, z / 10 + 9000) * 128 + 128),
|
|
66
|
+
255,
|
|
67
|
+
],
|
|
68
|
+
position: [
|
|
69
|
+
x * brickSize * 2,
|
|
70
|
+
y * brickSize * 2,
|
|
71
|
+
z * brickSize * 2,
|
|
72
|
+
],
|
|
73
|
+
})),
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
// Chrome lets you benchmark the sizes, check out the console to see how long it takes!
|
|
78
|
+
console.time('Generate');
|
|
79
|
+
const blob = new Blob([BRS.write(save)]);
|
|
80
|
+
console.timeEnd('Generate');
|
|
81
|
+
anchor.href = URL.createObjectURL(blob);
|
|
82
|
+
</script>
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<script src="https://cdn.jsdelivr.net/gh/meshiest/brs-js/dist/dist.js"></script>
|
|
3
|
+
<script src="https://cdn.jsdelivr.net/gh/josephg/noisejs/perlin.js"></script>
|
|
4
|
+
|
|
5
|
+
<a id="anchor" download="generated.brs">Download</a>
|
|
6
|
+
|
|
7
|
+
<script>
|
|
8
|
+
|
|
9
|
+
console.time('Generate');
|
|
10
|
+
|
|
11
|
+
// Generate fake UUID for terrain owner so you can /clearbricks terrain
|
|
12
|
+
const author = {
|
|
13
|
+
id: 'a8033bee-6c37-4118-b4a6-cecc1d966132',
|
|
14
|
+
name: 'terrain',
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
noise.seed(Math.random());
|
|
18
|
+
|
|
19
|
+
const size = 200;
|
|
20
|
+
const brickSize = 40;
|
|
21
|
+
|
|
22
|
+
// Build an array for the entire volume made of cubes
|
|
23
|
+
const locations = Array.from({length: size ** 3})
|
|
24
|
+
.map((_, i) => {
|
|
25
|
+
let obj = {
|
|
26
|
+
x: i % size,
|
|
27
|
+
y: Math.floor(i / size) % size,
|
|
28
|
+
z: Math.floor(i / size / size) % size,
|
|
29
|
+
width: 1,
|
|
30
|
+
depth: 1,
|
|
31
|
+
height: 1,
|
|
32
|
+
kind: 0,
|
|
33
|
+
material: 2,
|
|
34
|
+
rotation: 0,
|
|
35
|
+
};
|
|
36
|
+
obj.v = (
|
|
37
|
+
Math.max( // caves
|
|
38
|
+
Math.abs(noise.perlin3(obj.x / 30, obj.y / 30, obj.z / 30)),
|
|
39
|
+
Math.abs(noise.perlin3(obj.x / 30 + 9000, obj.y / 30 - 2000, obj.z / 30 + 4000))
|
|
40
|
+
) > 0.15 &&
|
|
41
|
+
Math.hypot(obj.x - size/2, obj.y - size/2, obj.z - size/2) < size/2 // sphere mask
|
|
42
|
+
) ? 1 : 0;
|
|
43
|
+
return obj;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Get brick at position
|
|
47
|
+
const at2 = (x, y, z) =>
|
|
48
|
+
x < 0 || y < 0 || z < 0 || x >= size || y >= size || z >= size
|
|
49
|
+
? null
|
|
50
|
+
: locations[x + y * size + z * size * size];
|
|
51
|
+
|
|
52
|
+
// get if brick exists at position
|
|
53
|
+
const at = (x, y, z) => {
|
|
54
|
+
const brick = at2(x, y, z);
|
|
55
|
+
return (brick && brick.v) ? 1 : 0;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
// Get a bit mask of the neighbors in West, North, East, South order
|
|
60
|
+
const getNeighbors = (x, y, z) => (at(x - 1, y, z) << 3) | (at(x, y - 1, z) << 2) | (at(x + 1, y, z) << 1) | (at(x, y + 1, z));
|
|
61
|
+
|
|
62
|
+
// convert corners to wedges
|
|
63
|
+
const wedgify = () => locations
|
|
64
|
+
.filter(obj => obj.v)
|
|
65
|
+
.forEach(obj => {
|
|
66
|
+
const {x, y, z} = obj;
|
|
67
|
+
const neighbors = getNeighbors(x, y, z);
|
|
68
|
+
|
|
69
|
+
// delta x/y variables are for increasing width of wedges
|
|
70
|
+
let dx = 0;
|
|
71
|
+
let dy = 0;
|
|
72
|
+
|
|
73
|
+
// handle each of the possible rotations on the Z axis
|
|
74
|
+
if (neighbors === 0b0011) {
|
|
75
|
+
obj.kind = 1;
|
|
76
|
+
obj.rotation = 2;
|
|
77
|
+
}
|
|
78
|
+
else if (neighbors === 0b1100) {
|
|
79
|
+
obj.kind = 1;
|
|
80
|
+
obj.rotation = 0;
|
|
81
|
+
}
|
|
82
|
+
else if (neighbors === 0b1001) {
|
|
83
|
+
obj.kind = 1;
|
|
84
|
+
obj.rotation = 3;
|
|
85
|
+
}
|
|
86
|
+
else if (neighbors === 0b0110) {
|
|
87
|
+
obj.kind = 1;
|
|
88
|
+
obj.rotation = 1;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// re-center wedges that got resized
|
|
92
|
+
if(obj.kind === 1) {
|
|
93
|
+
obj.width += Math.abs(dx);
|
|
94
|
+
obj.x = dx / 2 + x;
|
|
95
|
+
obj.depth += Math.abs(dy);
|
|
96
|
+
obj.y = dy / 2 + y;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
wedgify();
|
|
101
|
+
|
|
102
|
+
// Hollow-out the middle by removing cubes surrounding
|
|
103
|
+
locations
|
|
104
|
+
.filter(obj => obj.v)
|
|
105
|
+
.forEach(obj => {
|
|
106
|
+
const {x, y, z} = obj;
|
|
107
|
+
const neighbors = [ // all neighboring cubes
|
|
108
|
+
at2(x, y, z + 1),
|
|
109
|
+
at2(x, y, z - 1),
|
|
110
|
+
at2(x - 1, y, z),
|
|
111
|
+
at2(x + 1, y, z),
|
|
112
|
+
at2(x, y - 1, z),
|
|
113
|
+
at2(x, y + 1, z),
|
|
114
|
+
];
|
|
115
|
+
|
|
116
|
+
// check if surrounded by bricks that exist and are not wedges
|
|
117
|
+
if(neighbors.every(n => n && n.v !== 0 && n.kind === 0)) {
|
|
118
|
+
obj.v = 2;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// Uncomment below if you want wedges on the inside (creates holes in some places)
|
|
123
|
+
|
|
124
|
+
/*
|
|
125
|
+
locations.filter(obj => obj.v === 2).forEach(obj => obj.v = 0);
|
|
126
|
+
wedgify();
|
|
127
|
+
*/
|
|
128
|
+
|
|
129
|
+
const save = {
|
|
130
|
+
author: author,
|
|
131
|
+
description: 'Cave Sphere',
|
|
132
|
+
map: 'JavaScript',
|
|
133
|
+
brick_owners: [author],
|
|
134
|
+
brick_assets: [
|
|
135
|
+
'PB_DefaultBrick',
|
|
136
|
+
'PB_DefaultSideWedge',
|
|
137
|
+
],
|
|
138
|
+
materials: [
|
|
139
|
+
'BMC_Ghost',
|
|
140
|
+
'BMC_Ghost_Fail',
|
|
141
|
+
'BMC_Plastic',
|
|
142
|
+
'BMC_Glow',
|
|
143
|
+
'BMC_Metallic',
|
|
144
|
+
'BMC_Hologram',
|
|
145
|
+
],
|
|
146
|
+
bricks: locations
|
|
147
|
+
.filter(o => o.v === 1)
|
|
148
|
+
.map(({x, y, z, kind, rotation, material, width, height, depth}) => ({
|
|
149
|
+
// give multiple dimensions in case we resize wedges
|
|
150
|
+
size: [brickSize * width, brickSize * depth, brickSize * height],
|
|
151
|
+
asset_name_index: kind,
|
|
152
|
+
material_index: material,
|
|
153
|
+
rotation,
|
|
154
|
+
// 3d color blobs that are kinda grey-ish
|
|
155
|
+
color: [
|
|
156
|
+
Math.floor(noise.perlin3(x / 50, y / 50, z / 50) * 80 + 100),
|
|
157
|
+
Math.floor(noise.perlin3(x / 50 - 3000, y / 50 - 3000, z / 50 + 200) * 80 + 100),
|
|
158
|
+
Math.floor(noise.perlin3(x / 50 + 2000, y / 50 + 2000, z / 50 - 2000) * 80 + 100),
|
|
159
|
+
255,
|
|
160
|
+
],
|
|
161
|
+
position: [
|
|
162
|
+
x * brickSize * 2,
|
|
163
|
+
y * brickSize * 2,
|
|
164
|
+
z * brickSize * 2,
|
|
165
|
+
],
|
|
166
|
+
})),
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const blob = new Blob([new Uint8Array(BRS.write(save))]);
|
|
170
|
+
console.timeEnd('Generate');
|
|
171
|
+
anchor.href = URL.createObjectURL(blob);
|
|
172
|
+
|
|
173
|
+
</script>
|
package/package.json
CHANGED
package/src/constants.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import read from './read';
|
|
2
|
+
import write from './write';
|
|
3
|
+
import * as utils from './utils';
|
|
4
|
+
import * as constants from './constants';
|
|
5
|
+
|
|
6
|
+
// https://i.imgur.com/cv1fDWs.png
|
|
7
|
+
const brs = { read, write, utils, constants };
|
|
8
|
+
export { read, write, utils, constants };
|
|
9
|
+
export default brs;
|
|
10
|
+
|
|
11
|
+
declare global {
|
|
12
|
+
interface Window {
|
|
13
|
+
BRS: {
|
|
14
|
+
read: typeof read;
|
|
15
|
+
write: typeof write;
|
|
16
|
+
utils: typeof utils;
|
|
17
|
+
constants: typeof constants;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (typeof window !== 'undefined') {
|
|
23
|
+
window.BRS = brs;
|
|
24
|
+
}
|
package/src/read.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { MAGIC } from './constants';
|
|
2
|
+
import readBrsV1 from './read.v1';
|
|
3
|
+
import readBrsV10 from './read.v10';
|
|
4
|
+
import readBrsV2 from './read.v2';
|
|
5
|
+
import readBrsV3 from './read.v3';
|
|
6
|
+
import readBrsV4 from './read.v4';
|
|
7
|
+
import readBrsV8 from './read.v8';
|
|
8
|
+
import readBrsV9 from './read.v9';
|
|
9
|
+
import { BRSBytes, ReadOptions, ReadSaveObject } from './types';
|
|
10
|
+
import { read } from './utils';
|
|
11
|
+
|
|
12
|
+
// Reads in a byte array to build a brs object
|
|
13
|
+
export default function readBrs(
|
|
14
|
+
rawBytes: Uint8Array,
|
|
15
|
+
options: ReadOptions = {}
|
|
16
|
+
): ReadSaveObject {
|
|
17
|
+
if (typeof options !== 'object') throw new Error('Invalid options');
|
|
18
|
+
|
|
19
|
+
// default enable brick reading
|
|
20
|
+
if (typeof options.bricks !== 'boolean') options.bricks = true;
|
|
21
|
+
|
|
22
|
+
// default disable preview (a5 only)
|
|
23
|
+
if (typeof options.preview !== 'boolean') options.preview = false;
|
|
24
|
+
|
|
25
|
+
// Determine if the first 3 bytes are equal to the Brickadia save magic bytes
|
|
26
|
+
if (
|
|
27
|
+
rawBytes[0] !== MAGIC[0] ||
|
|
28
|
+
rawBytes[1] !== MAGIC[1] ||
|
|
29
|
+
rawBytes[2] !== MAGIC[2]
|
|
30
|
+
) {
|
|
31
|
+
throw new Error('Invalid starting bytes');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const brsData = rawBytes as BRSBytes;
|
|
35
|
+
brsData.brsOffset = 3;
|
|
36
|
+
|
|
37
|
+
// Determine if the file version supported
|
|
38
|
+
const version = read.u16(brsData);
|
|
39
|
+
switch (version) {
|
|
40
|
+
case 1:
|
|
41
|
+
return readBrsV1(brsData, options);
|
|
42
|
+
case 2:
|
|
43
|
+
return readBrsV2(brsData, options);
|
|
44
|
+
case 3:
|
|
45
|
+
return readBrsV3(brsData, options);
|
|
46
|
+
case 4:
|
|
47
|
+
return readBrsV4(brsData, options);
|
|
48
|
+
case 8:
|
|
49
|
+
return readBrsV8(brsData, options);
|
|
50
|
+
case 9:
|
|
51
|
+
return readBrsV9(brsData, options);
|
|
52
|
+
case 10:
|
|
53
|
+
return readBrsV10(brsData, options);
|
|
54
|
+
default:
|
|
55
|
+
throw new Error('Unsupported version ' + version);
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/read.v1.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BRSBytes,
|
|
3
|
+
ReadOptions,
|
|
4
|
+
BrsV1,
|
|
5
|
+
UnrealColor,
|
|
6
|
+
BrickV1,
|
|
7
|
+
Vector,
|
|
8
|
+
} from './types';
|
|
9
|
+
import { bgra, read } from './utils';
|
|
10
|
+
|
|
11
|
+
// Reads in a byte array to build a brs object
|
|
12
|
+
export default function readBrsV1(
|
|
13
|
+
brsData: BRSBytes,
|
|
14
|
+
options: ReadOptions = {}
|
|
15
|
+
): BrsV1 {
|
|
16
|
+
// Read in Headers
|
|
17
|
+
const header1Data = read.compressed(brsData);
|
|
18
|
+
const header2Data = read.compressed(brsData);
|
|
19
|
+
|
|
20
|
+
const map = read.string(header1Data);
|
|
21
|
+
const author_name = read.string(header1Data);
|
|
22
|
+
const description = read.string(header1Data);
|
|
23
|
+
|
|
24
|
+
const author_id = read.uuid(header1Data);
|
|
25
|
+
|
|
26
|
+
const brick_count = read.i32(header1Data);
|
|
27
|
+
|
|
28
|
+
const mods = read.array(header2Data, read.string);
|
|
29
|
+
const brick_assets = read.array(header2Data, read.string);
|
|
30
|
+
const colors = read.array(
|
|
31
|
+
header2Data,
|
|
32
|
+
data => bgra(Array.from(read.bytes(data, 4))) as UnrealColor
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
// Read in bricks
|
|
36
|
+
let bricks: BrickV1[] = [];
|
|
37
|
+
|
|
38
|
+
const numAssets = Math.max(brick_assets.length, 2);
|
|
39
|
+
|
|
40
|
+
if (options.bricks) {
|
|
41
|
+
bricks = Array(brick_count);
|
|
42
|
+
const brickData = read.compressed(brsData);
|
|
43
|
+
const brickBits = read.bits(brickData);
|
|
44
|
+
|
|
45
|
+
// Brick reader
|
|
46
|
+
for (let i = 0; !brickBits.empty() && i < brick_count; i++) {
|
|
47
|
+
brickBits.align();
|
|
48
|
+
const asset_name_index = brickBits.int(numAssets);
|
|
49
|
+
const size: Vector = brickBits.bit()
|
|
50
|
+
? [
|
|
51
|
+
brickBits.uint_packed(),
|
|
52
|
+
brickBits.uint_packed(),
|
|
53
|
+
brickBits.uint_packed(),
|
|
54
|
+
]
|
|
55
|
+
: [0, 0, 0];
|
|
56
|
+
const position: Vector = [
|
|
57
|
+
brickBits.int_packed(),
|
|
58
|
+
brickBits.int_packed(),
|
|
59
|
+
brickBits.int_packed(),
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
const orientation = brickBits.int(24);
|
|
63
|
+
const direction = (orientation >> 2) % 6;
|
|
64
|
+
const rotation = orientation & 3;
|
|
65
|
+
const collision = brickBits.bit();
|
|
66
|
+
const visibility = brickBits.bit();
|
|
67
|
+
|
|
68
|
+
const color = brickBits.bit()
|
|
69
|
+
? bgra(brickBits.bytesArr(4) as [number, number, number, number])
|
|
70
|
+
: brickBits.int(colors.length);
|
|
71
|
+
|
|
72
|
+
bricks[i] = {
|
|
73
|
+
asset_name_index,
|
|
74
|
+
size,
|
|
75
|
+
collision,
|
|
76
|
+
position,
|
|
77
|
+
direction,
|
|
78
|
+
rotation,
|
|
79
|
+
visibility,
|
|
80
|
+
color,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
version: 1,
|
|
87
|
+
map,
|
|
88
|
+
description,
|
|
89
|
+
author: {
|
|
90
|
+
id: author_id,
|
|
91
|
+
name: author_name,
|
|
92
|
+
},
|
|
93
|
+
mods,
|
|
94
|
+
brick_assets,
|
|
95
|
+
colors,
|
|
96
|
+
bricks,
|
|
97
|
+
brick_count,
|
|
98
|
+
};
|
|
99
|
+
}
|