Skip to content

Commit

Permalink
[BlockMetadata] Added. Now used in FurnaceWidget and furnace.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Feb 8, 2020
1 parent 5ffbfe0 commit abbabe8
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 265 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ This list is non exhaustive.
- Block/item/recipe definition
- Custom GUI creation
- Special blocks (workbench, furnace)
- Block metadata

### Missing features

- Texture pack system
- World loading/saving
- Advanced block metadata
- Particle system
- Fluid propagation
- Player model display (currently displaying an ugly box)
Expand Down
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ TODO

# World

TODO: Block metadata
DONE: Block metadata
• TODO: Day/night cycle

# Chunk generation
Expand Down
6 changes: 3 additions & 3 deletions client/source/gui/FurnaceWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ void FurnaceWidget::onEvent(const SDL_Event &event) {
}

void FurnaceWidget::update() {
u16 ticksRemaining = m_blockData.data & 0xfff;
u16 currentBurnTime = (m_blockData.data >> 12) & 0xfff;
u16 itemProgress = (m_blockData.data >> 24) & 0xff;
u16 ticksRemaining = m_blockData.meta.get<int>("ticks_remaining");
u16 currentBurnTime = m_blockData.meta.get<int>("current_burn_time");
u16 itemProgress = m_blockData.meta.get<int>("item_progress");

if (currentBurnTime) {
m_burnImage.setPosition(57, 37 + 14 - ticksRemaining * 14 / currentBurnTime, 0);
Expand Down
2 changes: 1 addition & 1 deletion client/source/network/ClientCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void ClientCommandHandler::setupCallbacks() {

if (data) {
bool useAltTiles;
packet >> data->data >> useAltTiles;
packet >> data->meta >> useAltTiles;

if (data->useAltTiles != useAltTiles) {
chunk->setChanged(true);
Expand Down
3 changes: 2 additions & 1 deletion common/include/world/BlockData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef BLOCKDATA_HPP_
#define BLOCKDATA_HPP_

#include "BlockMetadata.hpp"
#include "Inventory.hpp"

struct BlockData {
Expand All @@ -24,7 +25,7 @@ struct BlockData {

Inventory inventory;

u32 data = 0;
BlockMetadata meta;

bool useAltTiles = false;
};
Expand Down
86 changes: 86 additions & 0 deletions common/include/world/BlockMetadata.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* =====================================================================================
*
* Filename: BlockMetadata.hpp
*
* Description:
*
* Created: 08/02/2020 16:02:50
*
* Author: Quentin Bazin, <[email protected]>
*
* =====================================================================================
*/
#ifndef BLOCKMETADATA_HPP_
#define BLOCKMETADATA_HPP_

#include <memory>
#include <string>
#include <unordered_map>

#include <sol.hpp>

#include <gk/core/IntTypes.hpp>
#include <gk/core/Exception.hpp>

#include "ISerializable.hpp"

class BlockMetadataValue {
public:
enum class Type : u8 {
Undefined,
String,
Int
};

template<typename T>
T &get() const {
return *std::static_pointer_cast<T>(m_value);
}

template<typename T>
void set(const T &value, Type type) {
m_value = std::make_shared<T>(value);
m_type = type;
}

Type type() const { return m_type; }

private:
std::shared_ptr<void> m_value;
Type m_type = Type::Undefined;
};

class BlockMetadata : public ISerializable {
public:
void setString(const std::string &name, const std::string &value);
void setInt(const std::string &name, int value);

template<typename T>
T &get(const std::string &name) {
auto it = m_data.find(name);
if (it == m_data.end())
throw EXCEPTION("Unable to find metadata named", name);

return it->second.get<T>();
}

template<typename T>
sol::object getLuaObject(const std::string &name, sol::this_state state) {
sol::state_view lua{state};

auto it = m_data.find(name);
if (it == m_data.end())
return sol::make_object(lua, sol::lua_nil);

return sol::make_object<T>(lua, it->second.get<T>());
}

void serialize(sf::Packet &packet) const override;
void deserialize(sf::Packet &packet) override;

private:
std::unordered_map<std::string, BlockMetadataValue> m_data;
};

#endif // BLOCKMETADATA_HPP_
60 changes: 60 additions & 0 deletions common/source/world/BlockMetadata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* =====================================================================================
*
* Filename: BlockMetadata.cpp
*
* Description:
*
* Created: 08/02/2020 16:03:18
*
* Author: Quentin Bazin, <[email protected]>
*
* =====================================================================================
*/
#include <SFML/Network/Packet.hpp>

#include "BlockMetadata.hpp"

void BlockMetadata::setString(const std::string &name, const std::string &value) {
m_data[name].set(value, BlockMetadataValue::Type::String);
}

void BlockMetadata::setInt(const std::string &name, int value) {
m_data[name].set(value, BlockMetadataValue::Type::Int);
}

void BlockMetadata::serialize(sf::Packet &packet) const {
packet << u32(m_data.size());
for (auto &it : m_data) {
packet << u8(it.second.type()) << it.first;
if (it.second.type() == BlockMetadataValue::Type::String) {
packet << it.second.get<std::string>();
}
else if (it.second.type() == BlockMetadataValue::Type::Int) {
packet << it.second.get<int>();
}
}
}

void BlockMetadata::deserialize(sf::Packet &packet) {
u32 size;
packet >> size;
for (u32 i = 0 ; i < size ; ++i) {
u8 typeU8;
std::string name;
packet >> typeU8 >> name;

BlockMetadataValue::Type type = BlockMetadataValue::Type(typeU8);
if (type == BlockMetadataValue::Type::String) {
std::string value;
packet >> value;
m_data[name].set(value, type);
}
else if (type == BlockMetadataValue::Type::Int) {
int value;
packet >> value;
m_data[name].set(value, type);
}
}
}

Loading

0 comments on commit abbabe8

Please sign in to comment.