Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor lookup #309

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 2 additions & 30 deletions src/engine/block_rotation.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
#include "engine/block_rotation.hpp"

block_rotation::block_rotation(
int rotation,
boost::shared_ptr<nbt::ByteArray> array)
: x(0), z(0), rotation(rotation), array(array)
block_rotation::block_rotation(int rotation)
: rotation(rotation)
{
}

void block_rotation::set_xz(int x, int z) {
transform_xz(x, z);
this->x = x;
this->z = z;
}

void block_rotation::transform_xz(int& x, int& z) {
int t = x;

Expand All @@ -31,23 +23,3 @@ void block_rotation::transform_xz(int& x, int& z) {
break;
};
}

/**
*/
int block_rotation::get8(int y, int d) {
int p = ((y * 16 + z) * 16 + x);
if (!(p >= 0 && p < array->length)) return d;
return array->values[p] & 0xff;
}

/**
* Data values are packed two by two and the position LSB decides which
* half-byte contains the requested block data value.
*/
int block_rotation::get4(int y, int d) {
int tmp = (y * 16 + z) * 16 + x;
int p = tmp >> 1;
int b = tmp & 0x1;
if (!(p >= 0 && p < array->length)) return d;
return (array->values[p] >> (b * 4)) & 0xf;
}
8 changes: 2 additions & 6 deletions src/engine/block_rotation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@

class block_rotation {
public:
block_rotation(int rotation, boost::shared_ptr<nbt::ByteArray> array);
block_rotation(int rotation);

void set_xz(int x, int z);
void transform_xz(int& x, int& z);

int get8(int y, int d=-1);
int get4(int y, int d=-1);
private:
int x, z;
int rotation;
boost::shared_ptr<nbt::ByteArray> array;

};

#endif /* _ENGINE_BLOCK_ROTATION_HPP */
176 changes: 56 additions & 120 deletions src/engine/flat_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,147 +36,83 @@ class flat_base : public engine_base<C> {
blocked[i] = false;
}

// block type

BOOST_REVERSE_FOREACH(mc::Section_Compound Section, L->Sections) {
block_rotation br_blocks(s.rotation, Section.Blocks);
block_rotation br_data(s.rotation, Section.Data);
//block_rotation br_block_light(s.rotation, Section.BlockLight);
//block_rotation br_sky_light(s.rotation, Section.SkyLight);
block_rotation br_blocks(s.rotation);

for (int y = 15; y >= 0; y--) {
int abs_y = (Section.Y * 16) + y;

for (int z = 0; z < mc::MapZ; z++) {
for (int x = 0; x < mc::MapX; x++) {
unsigned int blocked_position = x * iw + z;
size_t blocked_position = x * iw + z;

if (blocked[blocked_position]) {
continue;
}

br_blocks.set_xz(x, z);
br_data.set_xz(x, z);
//br_block_light.set_xz(x, z);
//br_sky_light.set_xz(x, z);

// do incremental color fill until color is opaque
int block_type = br_blocks.get8(y);
int block_data = br_data.get4(y);
int projected_x = x;
int projected_z = z;
br_blocks.transform_xz(projected_x, projected_z);

mc::MaterialMode mode;
color top;
color side;

boost::optional<mc::MaterialT*> material = mc::get_material_legacy(block_type, block_data);
if (material) {
mc::MaterialT *m = material.get();
if (!m->enabled) {
mc::BlockT block;
if (Section.get_block(block, projected_x, projected_z, y)) {
if (!block.material->enabled) {
continue;
}
mode = m->mode;
top = m->top;
side = m->side;
} else {
mode = mc::MaterialMode::Block;
top = mc::SharedDefaultColor;
side = mc::SharedDefaultColor;
}

blocked[blocked_position] = top.is_opaque();

/*int block_light = br_block_light.get4(y + 1);
int sky_light = br_sky_light.get4(y + 1, 15);*/

//apply_shading(s, block_light, sky_light, 0, abs_y, top);

point p(x, abs_y, z);

pos_t px;
pos_t py;

flat_base<C>::project_position(p, px, py);

int log_rotation;

switch(mode) {
case mc::MaterialMode::Block:
render_block(oper, block_type, px, py, top, side);
render_halfblock(oper, block_type, px, py, top, side);
break;
case mc::MaterialMode::HalfBlock:
render_halfblock(oper, block_type, px, py, top, side);
break;
case mc::MaterialMode::TorchBlock:
render_torchblock(oper, block_type, px, py, top, side);
break;
case mc::MaterialMode::LargeFlowerBlock:
// Check if the requested block is the top block
if(block_data & 0x08) {
// Small sanity check
if(y > 0 && br_blocks.get8(y-1) == block_type) {
// Minecraft currently doesn't set the lower bits to the
// corresponding type so we have to do this here.
block_data = br_data.get4(y-1) & 0x07;
top = mc::get_color_legacy(block_type, block_data);
side = mc::get_side_color_legacy(block_type, block_data);
blocked[blocked_position] = block.material->top.is_opaque();

point p(x, abs_y, z);
pos_t px = 0;
pos_t py = 0;
flat_base<C>::project_position(p, px, py);

// FIXME; this should not be used by any renderer - deprecated!
// There is no flat engine that cares about this.
int block_type = -1;

switch(block.material->mode) {
case mc::MaterialMode::Block:
case mc::MaterialMode::LegacyLeaves:
render_block(oper, block_type, px, py, block.material->top, block.material->side);
render_halfblock(oper, block_type, px, py, block.material->top, block.material->side);
break;
case mc::MaterialMode::HalfBlock:
case mc::MaterialMode::LegacySlab:
render_halfblock(oper, block_type, px, py, block.material->top, block.material->side);
break;
case mc::MaterialMode::TorchBlock:
render_torchblock(oper, block_type, px, py, block.material->top, block.material->side);
break;
case mc::MaterialMode::LargeFlowerBlock:
if (block.properties.is_top) {
render_block(oper, block_type, px, py, block.material->top, block.material->side);
render_halfblock(oper, block_type, px, py, block.material->top, block.material->side);
} else {
// Top block not placed on a correct bottom block.
// The expected LargeFlower multi block structure is invalid, skip it.
continue;
render_block(oper, block_type, px, py, block.material->side, block.material->side);
render_halfblock(oper, block_type, px, py, block.material->side, block.material->side);
}
} else {
// Force the top of the lower block to also be the side color.
top = side;
}
render_block(oper, block_type, px, py, top, side);
render_halfblock(oper, block_type, px, py, top, side);
break;
case mc::MaterialMode::LogBlock:
// Log blocks are just a regular block that may differ in orientation. Top
// color is considered the inner material. Because some bits of metadata are
// used both for variant and rotation state, block type needs to be fetched again.
log_rotation = (block_data & 0x0C) >> 2;
switch(log_rotation) {
case 0:
// Up/down
top = mc::get_color_legacy(block_type, block_data & 0x3);
side = mc::get_side_color_legacy(block_type, block_data & 0x3);
break;
case 1:
// East/west
case 2:
// North/south
// TODO: Actually implement render rotation, for now simply swap top and side.
side = mc::get_color_legacy(block_type, block_data & 0x3);
top = mc::get_side_color_legacy(block_type, block_data & 0x3);
break;
case 3:
// Only sides, thus no top color.
side = mc::get_side_color_legacy(block_type, block_data & 0x3);
top = side;
case mc::MaterialMode::LogBlock:
switch(block.properties.orientation) {
case mc::BlockOrientation::UpDown:
render_block(oper, block_type, px, py, block.material->top, block.material->side);
render_halfblock(oper, block_type, px, py, block.material->top, block.material->side);
break;
case mc::BlockOrientation::EastWest:
case mc::BlockOrientation::NorthSouth:
render_block(oper, block_type, px, py, block.material->side, block.material->top);
render_halfblock(oper, block_type, px, py, block.material->side, block.material->top);
break;
case mc::BlockOrientation::OnlySides:
render_block(oper, block_type, px, py, block.material->side, block.material->side);
render_halfblock(oper, block_type, px, py, block.material->side, block.material->side);
break;
case mc::BlockOrientation::Invalid:
break;
}
break;
}
render_block(oper, block_type, px, py, top, side);
render_halfblock(oper, block_type, px, py, top, side);
break;
case mc::MaterialMode::LegacySlab:
// Legacy slab is just a half block; but is sometimes a full block.
// The first legacy id is the full block version.
if (block_type == material.get()->legacy_ids[0]) {
render_block(oper, block_type, px, py, top, side);
}
render_halfblock(oper, block_type, px, py, top, side);
break;
case mc::MaterialMode::LegacyLeaves:
// Legacy leaves is just a regular block; however some bits of the metadata
// are used for other block states, only the two first bits are used for block
// type therefore we need to re-fetch the block type now.
top = mc::get_color_legacy(block_type, block_data & 0x3);
side = mc::get_side_color_legacy(block_type, block_data & 0x3);
render_block(oper, block_type, px, py, top, side);
render_halfblock(oper, block_type, px, py, top, side);
break;
}
}
}
Expand Down
29 changes: 0 additions & 29 deletions src/engine/functions.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1 @@
#include "engine/functions.hpp"

void apply_shading(
const engine_settings& s,
int block_light,
int sky_light,
int height_map,
int y,
color &c)
{
if(s.night) {
c.darken(0x6 * (16 - block_light));
}
else if (sky_light != -1 && y != s.top) {
c.darken(0x6 * (16 - std::max(sky_light, block_light)));
}

//c.darken((mc::MapY - y));

// in heightmap mode, brightness = height
if (s.heightmap) {
c.b = y*2;
c.g = y*2;
c.r = y*2;
c.a = 0xff;
}
else if (s.striped_terrain && y % 2 == 0) {
c.darken(0xf);
}
}
68 changes: 0 additions & 68 deletions src/engine/functions.hpp
Original file line number Diff line number Diff line change
@@ -1,72 +1,4 @@
#ifndef _ENGINE_FUNCTIONS_HPP
#define _ENGINE_FUNCTIONS_HPP

#include "engine/block_rotation.hpp"
#include "engine/engine_settings.hpp"
#include "image/color.hpp"
#include "mc/blocks.hpp"

/**
* Shared functions between engines.
*/

/**
* Apply shading to specified block and color.
**/
void apply_shading(
const engine_settings& engine_s,
int bl, int sl, int hm, int y, color &c);

inline bool is_open(mc::MaterialT*& m) {
if (!m) {
return false;
}

return m->top.is_transparent();
}

inline bool is_open(int bt) {
if (bt == -1) {
return false;
}

switch(bt) {
case mc::LegacyBlocks::Air: return true;
case mc::LegacyBlocks::Leaves: return true;
default: return false;
}
}

// this functions is currently unused
inline bool cave_ignore_block(int y, int bt, block_rotation& b_r, bool &cave_initial) {
if (cave_initial) {
if (!is_open(bt)) {
cave_initial = false;
return true;
}

return true;
}

if (!is_open(bt) && is_open(b_r.get8(y + 1))) {
return false;
}

return true;
}

// this functions is currently unused
inline bool hell_ignore_block(int y, int bt, block_rotation& b_r, bool &hell_initial) {
if (hell_initial) {
if (is_open(bt)) {
hell_initial = false;
return false;
}

return true;
}

return false;
}

#endif /* _ENGINE_FUNCTIONS_HPP */
Loading