BlockType: replace list of booleans with flags bitfield

This commit is contained in:
Matthias Schiffer 2020-06-18 23:38:21 +02:00
parent 446e74791f
commit ef4b6eac02
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
8 changed files with 800 additions and 787 deletions

View file

@ -209,8 +209,8 @@
"brown_wall_banner": null, "brown_wall_banner": null,
"brown_wool": {}, "brown_wool": {},
"bubble_column": { "bubble_column": {
"blue": true, "texture": "water_still",
"texture": "water_still" "water": true
}, },
"bubble_coral": null, "bubble_coral": null,
"bubble_coral_block": {}, "bubble_coral_block": {},
@ -1428,8 +1428,8 @@
"warped_wall_sign": null, "warped_wall_sign": null,
"warped_wart_block": {}, "warped_wart_block": {},
"water": { "water": {
"blue": true, "texture": "water_still",
"texture": "water_still" "water": true
}, },
"weeping_vines": {}, "weeping_vines": {},
"weeping_vines_plant": {}, "weeping_vines_plant": {},

View file

@ -42,7 +42,7 @@ for name, info in blocks.items():
'opaque': False, 'opaque': False,
'grass': False, 'grass': False,
'foliage': False, 'foliage': False,
'blue': False, 'water': False,
} }
if info is None: if info is None:
@ -54,7 +54,7 @@ for name, info in blocks.items():
output[id]['opaque'] = True output[id]['opaque'] = True
output[id]['grass'] = info.get('grass', False) output[id]['grass'] = info.get('grass', False)
output[id]['foliage'] = info.get('foliage', False) output[id]['foliage'] = info.get('foliage', False)
output[id]['blue'] = info.get('blue', False) output[id]['water'] = info.get('water', False)
with open(sys.argv[3], 'w') as f: with open(sys.argv[3], 'w') as f:
json.dump(output, f) json.dump(output, f)

View file

@ -15,12 +15,23 @@ output = {}
with open(sys.argv[2], 'w') as f: with open(sys.argv[2], 'w') as f:
for name, info in colors.items(): for name, info in colors.items():
print('{"%s", {%s, %s, %s, %s, {%u, %u, %u}}},' % ( flags = []
if info['opaque']:
flags.append('BLOCK_OPAQUE')
if info['grass']:
flags.append('BLOCK_GRASS')
if info['foliage']:
flags.append('BLOCK_FOLIAGE')
if info['water']:
flags.append('BLOCK_WATER')
if flags:
flags = '|'.join(flags)
else:
flags = '0'
print('{"%s", {%s, {%u, %u, %u}}},' % (
name, name,
['false', 'true'][info['opaque']], flags,
['false', 'true'][info['grass']],
['false', 'true'][info['foliage']],
['false', 'true'][info['blue']],
info['color']['r'], info['color']['r'],
info['color']['g'], info['color']['g'],
info['color']['b'], info['color']['b'],

View file

@ -99,11 +99,11 @@ Color Biome::getBlockColor(const BlockType *type, unsigned height) const {
float t = clamp(temp - std::max(0.0f, (height-64)/600.0f), 0, 1); float t = clamp(temp - std::max(0.0f, (height-64)/600.0f), 0, 1);
float r = clamp(rain, 0, 1) * t; float r = clamp(rain, 0, 1) * t;
if (type->grass) if (type->flags & BLOCK_GRASS)
c *= getGrassColor(t, r); c *= getGrassColor(t, r);
if (type->foliage) if (type->flags & BLOCK_FOLIAGE)
c *= getFoliageColor(t, r); c *= getFoliageColor(t, r);
if (type->blue) if (type->flags & BLOCK_WATER)
c *= getWaterColor(t, r); c *= getWaterColor(t, r);
float h = 0.5f + height * 0.005f; float h = 0.5f + height * 0.005f;

View file

@ -33,6 +33,11 @@
namespace MinedMap { namespace MinedMap {
namespace Resource { namespace Resource {
#define BLOCK_OPAQUE (1u << 0)
#define BLOCK_GRASS (1u << 1)
#define BLOCK_FOLIAGE (1u << 2)
#define BLOCK_WATER (1u << 3)
struct BlockType { struct BlockType {
private: private:
static const std::unordered_map<std::string, BlockType> Types; static const std::unordered_map<std::string, BlockType> Types;
@ -40,10 +45,7 @@ private:
public: public:
static const BlockType * lookup(const std::string &name); static const BlockType * lookup(const std::string &name);
bool opaque; uint8_t flags;
bool grass;
bool foliage;
bool blue;
struct { struct {
uint8_t r, g, b; uint8_t r, g, b;
} color; } color;

File diff suppressed because it is too large Load diff

View file

@ -42,7 +42,7 @@ struct Block {
Resource::Color getColor() const { Resource::Color getColor() const {
if (!type || !type->opaque) if (!type || !(type->flags & BLOCK_OPAQUE))
return Resource::Color {}; return Resource::Color {};
return (Resource::BIOMES[biome] ?: Resource::BIOME_DEFAULT)->getBlockColor(type, height); return (Resource::BIOMES[biome] ?: Resource::BIOME_DEFAULT)->getBlockColor(type, height);

View file

@ -84,7 +84,7 @@ bool Chunk::getBlock(Block *block, const Section *section, size_t x, size_t y, s
return false; return false;
const Resource::BlockType *type = section->getBlockStateAt(x, y, z); const Resource::BlockType *type = section->getBlockStateAt(x, y, z);
if (!type || !type->opaque) if (!type || !(type->flags & BLOCK_OPAQUE))
return false; return false;
if (!block->type) { if (!block->type) {
@ -93,7 +93,7 @@ bool Chunk::getBlock(Block *block, const Section *section, size_t x, size_t y, s
block->biome = getBiome(x, y, z); block->biome = getBiome(x, y, z);
} }
if (type->blue) if (type->flags & BLOCK_WATER)
return false; return false;
block->height = SIZE*section->getY() + y; block->height = SIZE*section->getY() + y;