Skip to content

Commit

Permalink
[BlockState] Added. First stop of block states.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 10, 2020
1 parent 0af7cd7 commit 7750154
Show file tree
Hide file tree
Showing 40 changed files with 574 additions and 334 deletions.
2 changes: 2 additions & 0 deletions docs/lua-api-block.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ Useful for flora, tallgrass, mushrooms, etc...
Parameters:
- `pos` (`ivec3`): position of the block
- `block` (`ServerBlock`): block definition
- `player` (`ServerPlayer`): player that activated the block
- `world` (`ServerWorld`): instance of the world
- `client` (`Client`): client that activated the block
Expand Down Expand Up @@ -290,6 +291,7 @@ Parameters:
Parameters:
- `pos` (`ivec3`): position of the block
- `block` (`ServerBlock`): block definition
- `chunk` (`ServerChunk`): current chunk
- `world` (`ServerWorld`): instance of the world
4 changes: 2 additions & 2 deletions docs/lua-api-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ end)

Possible events:

- `BlockPlaced`: `funcion(pos, block, player, world, client, server)`
- `BlockDigged`: `funcion(pos, block, player, world, client, server)`
- `BlockPlaced`: `funcion(pos, block_state, player, world, client, server)`
- `BlockDigged`: `funcion(pos, block_state, player, world, client, server)`
- `BlockActivated`: `function(pos, block, player, world, client, server)`
- `ItemActivated`: `function(pos, block, player, world, client, server)`
- `PlayerConnected`: `function(pos, player, client, server)`
Expand Down
5 changes: 1 addition & 4 deletions docs/lua-api-cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
## Block

- `u16 id()`
- `u16 data()`
- `string string_id()`
- `string label()`
- `string mod_name()`
- `bool is_opaque()`
- `ItemStack get_item_drop()`
- `BlockParam param()`
- `Block get_state()`

## BlockData

Expand Down
46 changes: 26 additions & 20 deletions mods/default/blocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ mod:block {
draw_type = "glass",
is_opaque = false,

on_block_activated = function(pos, player, world, client, server, screen_width, screen_height, gui_scale)
on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
local dim = (player:dimension() + 1) % 2
local pos = {
x = math.floor(player:x()),
Expand Down Expand Up @@ -311,7 +311,7 @@ mod:block {
name = "Redstone Lamp",
tiles = "redstone_lamp_off.png",

on_block_activated = function(pos, player, world, client, server, screen_width, screen_height, gui_scale)
on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
local block = openminer.registry:get_block_from_string("default:redstone_lamp_on")
world:set_block(pos.x, pos.y, pos.z, block:id())
end
Expand All @@ -327,7 +327,7 @@ mod:block {
ci_ignore = 1
},

on_block_activated = function(pos, player, world, client, server, screen_width, screen_height, gui_scale)
on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
local block = openminer.registry:get_block_from_string("default:redstone_lamp_off")
world:set_block(pos.x, pos.y, pos.z, block:id())
end
Expand Down Expand Up @@ -356,37 +356,43 @@ mod:block {
id = "seeds",
name = "Seeds",
tiles = "wheat_stage_0.png",
alt_tiles = "wheat_stage_7.png",
draw_type = "xshape",
inventory_image = "seeds_wheat.png",
hardness = 0,

bounding_box = {0, 0, 0, 1, 1, 1.0 / 16.0},

states = {
[1] = { tiles = "wheat_stage_1.png", bounding_box = {0, 0, 0, 1, 1, 3.0 / 16.0}, },
[2] = { tiles = "wheat_stage_2.png", bounding_box = {0, 0, 0, 1, 1, 5.0 / 16.0}, },
[3] = { tiles = "wheat_stage_3.png", bounding_box = {0, 0, 0, 1, 1, 8.0 / 16.0}, },
[4] = { tiles = "wheat_stage_4.png", bounding_box = {0, 0, 0, 1, 1, 10.0 / 16.0}, },
[5] = { tiles = "wheat_stage_5.png", bounding_box = {0, 0, 0, 1, 1, 12.0 / 16.0}, },
[6] = { tiles = "wheat_stage_6.png", bounding_box = {0, 0, 0, 1, 1, 14.0 / 16.0}, },
[7] = { tiles = "wheat_stage_7.png", bounding_box = {0, 0, 0, 1, 1, 1}, },
},

tick_randomly = true,
tick_probability = 0.01,

on_block_placed = function(pos, world)
world:add_block_data(pos.x, pos.y, pos.z, 0, 0)
end,

on_tick = function(pos, chunk, world)
local data = world:get_block_data(pos.x, pos.y, pos.z)
if not data then return end

local growth_stage = data.meta:get_int("growth_stage") or 0
if growth_stage < 7 then
data.use_alt_tiles = true
data.meta:set_int("growth_stage", 7)
on_tick = function(pos, block, chunk, world)
local block_param = world:get_block_param(pos.x, pos.y, pos.z)
local current_state = block:param():get_param(BlockParam.State, block_param)
if current_state < 7 then
world:set_block_param(pos.x, pos.y, pos.z, current_state + 1)
end
end,

on_block_activated = function(pos, player, world, client, server, screen_width, screen_height, gui_scale)
local data = world:get_block_data(pos.x, pos.y, pos.z)
if not data then return end

local growth_stage = data.meta:get_int("growth_stage") or 0
if growth_stage >= 7 then
data.use_alt_tiles = false
data.meta:set_int("growth_stage", 0)
on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
local block_param = world:get_block_param(pos.x, pos.y, pos.z)
local current_state = block:param():get_param(BlockParam.State, block_param)
if current_state >= 7 then
world:set_block_param(pos.x, pos.y, pos.z,
block:param():set_param(BlockParam.State, block_param, 0))

-- FIXME: It should drop the item if 'default:use_item_drops' is enabled
local item_stack = ItemStack.new("default:wheat", 1)
Expand Down
2 changes: 1 addition & 1 deletion mods/default/blocks/door.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ mod:block {
end
end,

on_block_activated = function(pos, player, world, client, server, screen_width, screen_height, gui_scale)
on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
open_door(pos, world)
open_door({x = pos.x, y = pos.y, z = pos.z - 1}, world)
end
Expand Down
4 changes: 2 additions & 2 deletions mods/default/blocks/furnace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod:block {
world:add_block_data(pos.x, pos.y, pos.z, 3, 1)
end,

on_block_activated = function(pos, player, world, client, server, screen_width, screen_height, gui_scale)
on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
local gui = LuaGUI.new()

gui:set_size(176, 166)
Expand Down Expand Up @@ -169,7 +169,7 @@ mod:block {
gui:show(client)
end,

on_tick = function(pos, chunk, world)
on_tick = function(pos, block, chunk, world)
local data = world:get_block_data(pos.x, pos.y, pos.z)
if not data then return end

Expand Down
2 changes: 1 addition & 1 deletion mods/default/blocks/workbench.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod:block {
world:add_block_data(pos.x, pos.y, pos.z, 3, 3)
end,

on_block_activated = function(pos, player, world, client, server, screen_width, screen_height, gui_scale)
on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
local gui = LuaGUI.new()

gui:set_size(176, 166)
Expand Down
12 changes: 7 additions & 5 deletions source/client/graphics/TextureAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,13 @@ void TextureAtlas::loadFromRegistry(const std::string &texturePack) {
else
path = "texturepacks/" + texturePack + "/blocks/";

const TilesDef &tiles = block->tiles();
for (auto &textureFilename : tiles.textureFilenames())
addFile(path, textureFilename);
for (auto &textureFilename : tiles.altTextureFilenames())
addFile(path, textureFilename);
for (auto &state : block->states()) {
const TilesDef &tiles = state.tiles();
for (auto &textureFilename : tiles.textureFilenames())
addFile(path, textureFilename);
for (auto &textureFilename : tiles.altTextureFilenames())
addFile(path, textureFilename);
}
}

for (auto &item : Registry::getInstance().items()) {
Expand Down
14 changes: 8 additions & 6 deletions source/client/gui/InventoryCube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ InventoryCube::InventoryCube(float size, bool isEntity)

using namespace BlockGeometry;

void InventoryCube::updateVertexBuffer(const Block &block) {
void InventoryCube::updateVertexBuffer(const Block &block, u8 state) {
if (!block.id()) return;

const BlockState &blockState = block.getState(state);

Vertex vertices[nFaces][nVertsPerFace];

glm::vec3 vertexPos[nVertsPerCube] {
Expand All @@ -75,7 +77,7 @@ void InventoryCube::updateVertexBuffer(const Block &block) {
glm::vec3{m_size, m_size, m_size},
};

const gk::FloatBox &boundingBox = block.boundingBox();
const gk::FloatBox &boundingBox = blockState.boundingBox();

constexpr s8f faceValue[nFaces]{2, 2, 4, 4, 3, 3};

Expand All @@ -86,7 +88,7 @@ void InventoryCube::updateVertexBuffer(const Block &block) {
// U0V0 -> 3 2 <- U1V0
// U0V1 -> 0 1 <- U1V1
float U0, V0, U1, V1;
if (block.drawType() == BlockDrawType::Cactus) {
if (blockState.drawType() == BlockDrawType::Cactus) {
U0 = 0.f;
V0 = 0.f;
U1 = 1.f;
Expand All @@ -101,10 +103,10 @@ void InventoryCube::updateVertexBuffer(const Block &block) {
V1 = (f <= 3) ? 1.f - boundingBox.z : (f == 4) ? boundingBox.y + boundingBox.sizeY : 1.f - boundingBox.y;
}

const gk::FloatRect &blockTexCoords = m_textureAtlas->getTexCoords(block.tiles().getTextureForFace(f));
const gk::FloatRect &blockTexCoords = m_textureAtlas->getTexCoords(blockState.tiles().getTextureForFace(f));

for (u8f v = 0; v < nVertsPerFace; ++v) {
if (block.drawType() == BlockDrawType::Cactus) {
if (blockState.drawType() == BlockDrawType::Cactus) {
vertices[f][v].coord3d[0] = vertexPos[cubeVerts[f][v]].x - boundingBox.x * faceNormals[f][0] * m_size;
vertices[f][v].coord3d[1] = vertexPos[cubeVerts[f][v]].y - boundingBox.y * faceNormals[f][1] * m_size;
vertices[f][v].coord3d[2] = vertexPos[cubeVerts[f][v]].z - boundingBox.z * faceNormals[f][2] * m_size;
Expand All @@ -121,7 +123,7 @@ void InventoryCube::updateVertexBuffer(const Block &block) {
vertices[f][v].texCoord[0] = gk::qlerp(blockTexCoords.x, blockTexCoords.x + blockTexCoords.sizeX, U);
vertices[f][v].texCoord[1] = gk::qlerp(blockTexCoords.y, blockTexCoords.y + blockTexCoords.sizeY, V);

const gk::Color &colorMultiplier = block.colorMultiplier();
const gk::Color &colorMultiplier = blockState.colorMultiplier();
vertices[f][v].color[0] = colorMultiplier.r;
vertices[f][v].color[1] = colorMultiplier.g;
vertices[f][v].color[2] = colorMultiplier.b;
Expand Down
2 changes: 1 addition & 1 deletion source/client/gui/InventoryCube.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class InventoryCube : public gk::Drawable, public gk::Transformable {
public:
InventoryCube(float size = 1.0f, bool isEntity = false);

void updateVertexBuffer(const Block &block);
void updateVertexBuffer(const Block &block, u8 state = 0);

float size() const { return m_size; }

Expand Down
11 changes: 6 additions & 5 deletions source/client/gui/ItemWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ ItemWidget::ItemWidget(Inventory &inventory, u16 x, u16 y, Widget *parent)
void ItemWidget::update() {
if (stack().item().isBlock()) {
const Block &block = Registry::getInstance().getBlock(stack().item().id());
if (block.drawType() != BlockDrawType::XShape && block.inventoryImage().empty()) {
const BlockState &blockState = block.getState(0); // FIXME: Get state from item stack
if (blockState.drawType() != BlockDrawType::XShape && blockState.inventoryImage().empty()) {
m_cube.updateVertexBuffer(block);
m_isImage = false;
}
else
updateImage(&block);
updateImage(&blockState);
}
else
updateImage();
Expand All @@ -56,7 +57,7 @@ void ItemWidget::update() {
m_text.setPosition(16 - 4 - 6 * floor(log10(stack().amount())), 16 - 6, 0);
}

void ItemWidget::updateImage(const Block *block) {
void ItemWidget::updateImage(const BlockState *blockState) {
if (m_image.width() == 0) {
m_image.load(m_textureAtlas.texture());
m_image.setPosition(1, 1, 0);
Expand All @@ -67,8 +68,8 @@ void ItemWidget::updateImage(const Block *block) {
m_image.setClipRect(clipRect.x, clipRect.y, clipRect.sizeX, clipRect.sizeY);
m_image.setScale(16.0f / clipRect.sizeX, 16.0f / clipRect.sizeY);

if (block)
m_image.setColor(block->colorMultiplier());
if (blockState)
m_image.setColor(blockState->colorMultiplier());

m_isImage = true;
}
Expand Down
4 changes: 3 additions & 1 deletion source/client/gui/ItemWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
#include "Text.hpp"
#include "Widget.hpp"

class BlockState;

class ItemWidget : public Widget {
public:
ItemWidget(Inventory &inventory, u16 x, u16 y, Widget *parent = nullptr);

void update() override;
void updateImage(const Block *block = nullptr);
void updateImage(const BlockState *blockState = nullptr);

const ItemStack &stack() const { return m_inventory.getStack(m_x, m_y); }
void setStack(const std::string &name, unsigned int amount = 1);
Expand Down
Loading

0 comments on commit 7750154

Please sign in to comment.