forked from GroobleDierne/minecraft-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audit_shapes.js
94 lines (82 loc) · 3.01 KB
/
audit_shapes.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
/* eslint-env mocha */
const fs = require('fs')
const path = require('path')
// checks if bounding boxes data in blocks.json is consistent with blockCollisionShapes.json
require('./version_iterator')(function (p, versionString) {
describe('audit block shapes ' + versionString, function () {
const blockFile = path.join(p, 'blocks.json')
if (!fs.existsSync(blockFile)) {
return // Only audit shape where block exists
}
it('audit bb', function () {
const shapeFile = path.join(p, 'blockCollisionShapes.json')
if (!fs.existsSync(shapeFile)) {
// console.log('No collision shapes for version ' + versionString)
return
}
const blocks = require(blockFile)
const shapes = require(shapeFile)
const blockByName = {}
// Check bijection between blocks and shapes
// Check that every block have a shape
blocks.forEach(block => {
blockByName[block.name] = block
const shape = shapes.blocks[block.name]
if (shape === undefined) {
// console.log('missing shape for block: ' + block.name)
}
})
// Check that every shape have a block
for (const key of Object.keys(shapes.blocks)) {
const block = blockByName[key]
if (block === undefined) {
// console.log('missing block for shape: ' + key)
}
}
// Check every shape is present
const usedShapes = {}
for (let value of Object.values(shapes.blocks)) {
if (!(value instanceof Array)) value = [value]
for (const shape of value) {
if (shapes.shapes[shape] === undefined) {
// console.log('missing shape: ' + shape)
}
usedShapes[shape] = true
}
}
// Check every shape is used
for (const key of Object.keys(shapes.shapes)) {
if (!usedShapes[key]) {
// console.log('unused shape: ' + key)
}
}
// Check block bounding box is consistent with shape
let rewriteBlocks = false
blocks.forEach(block => {
blockByName[block.name] = block
const shape = shapes.blocks[block.name]
if (shape !== undefined) {
if (block.boundingBox === 'empty') {
if (shape !== 0) {
// console.log('Inconsistent BB for block ' + block.name + ' (expected empty got ' + shape + ')')
block.boundingBox = 'block'
rewriteBlocks = false
}
} else if (block.boundingBox === 'block') {
if (shape === 0) {
// console.log('Inconsistent BB for block ' + block.name + ' (expected block got ' + shape + ')')
block.boundingBox = 'empty'
rewriteBlocks = false
}
} else {
// console.log('Unknown BB: ' + block.boundingBox + ' for block ' + block.name)
}
}
})
// Automatically fix block data, if necessary
if (rewriteBlocks) {
fs.writeFileSync(blockFile, JSON.stringify(blocks, null, 2))
}
})
})
})