From 5c8d9263155826e5724905a55b1afbb236ed5873 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Sun, 29 Mar 2020 15:24:51 +0200 Subject: [PATCH] Block groups added. --- mods/default/blocks.lua | 6 ++++- mods/default/recipes.lua | 4 +-- source/common/world/Block.cpp | 6 ++--- source/common/world/Block.hpp | 13 ++++++++++ source/server/lua/loader/LuaBlockLoader.cpp | 27 +++++++++++++++++++-- source/server/lua/loader/LuaBlockLoader.hpp | 3 +++ 6 files changed, 50 insertions(+), 9 deletions(-) diff --git a/mods/default/blocks.lua b/mods/default/blocks.lua index c68a94bd..dfcb6ec3 100644 --- a/mods/default/blocks.lua +++ b/mods/default/blocks.lua @@ -118,7 +118,11 @@ mod:block { mod:block { id = "oak_planks", name = "Oak Wood Planks", - tiles = "oak_planks.png" + tiles = "oak_planks.png", + + groups = { + om_planks = 1 + } } mod:block { diff --git a/mods/default/recipes.lua b/mods/default/recipes.lua index fcfb704d..66e070f6 100644 --- a/mods/default/recipes.lua +++ b/mods/default/recipes.lua @@ -513,7 +513,7 @@ mod:crafting_recipe { '#' }, - keys = {['#'] = "default:oak_planks"} + keys = {['#'] = "group:om_planks"} } -- Planks @@ -536,7 +536,7 @@ mod:crafting_recipe { "##", "##" }, - keys = {["#"] = "default:oak_planks"} + keys = {["#"] = "group:om_planks"} } -- Furnace diff --git a/source/common/world/Block.cpp b/source/common/world/Block.cpp index 5c4863e2..fa195b79 100644 --- a/source/common/world/Block.cpp +++ b/source/common/world/Block.cpp @@ -24,8 +24,6 @@ * * ===================================================================================== */ -#include - #include "Block.hpp" #include "NetworkUtils.hpp" #include "Player.hpp" @@ -46,7 +44,7 @@ void Block::serialize(sf::Packet &packet) const { packet << u32(m_id) << m_stringID << m_label << u8(m_drawType) << m_hardness << m_harvestRequirements << m_itemDrop << m_itemDropAmount << m_tiles << m_boundingBox << m_isOpaque << m_isLightSource << m_canUpdate << m_canBeActivated - << m_colorMultiplier << m_isRotatable << m_inventoryImage; + << m_colorMultiplier << m_isRotatable << m_inventoryImage << m_groups; } void Block::deserialize(sf::Packet &packet) { @@ -56,7 +54,7 @@ void Block::deserialize(sf::Packet &packet) { packet >> id >> m_stringID >> m_label >> drawType >> m_hardness >> m_harvestRequirements >> m_itemDrop >> m_itemDropAmount >> m_tiles >> m_boundingBox >> m_isOpaque >> m_isLightSource >> m_canUpdate >> m_canBeActivated - >> m_colorMultiplier >> m_isRotatable >> m_inventoryImage; + >> m_colorMultiplier >> m_isRotatable >> m_inventoryImage >> m_groups; m_id = id; m_drawType = BlockDrawType(drawType); diff --git a/source/common/world/Block.hpp b/source/common/world/Block.hpp index 21aa8003..0ac3a71a 100644 --- a/source/common/world/Block.hpp +++ b/source/common/world/Block.hpp @@ -109,6 +109,17 @@ class Block : public ISerializable { const std::string &inventoryImage() const { return m_inventoryImage; } void setInventoryImage(const std::string &inventoryImage) { m_inventoryImage = inventoryImage; } + void addGroup(const std::string &name, u16 value) { m_groups.emplace(name, value); } + bool hasGroup(const std::string &name) const { return m_groups.find(name) != m_groups.end(); } + + u16 getGroupValue(const std::string &name) const { + auto it = m_groups.find(name); + if (it == m_groups.end()) + return 0; + + return it->second; + } + protected: glm::vec4 getTexCoordsFromID(int textureID) const; @@ -141,6 +152,8 @@ class Block : public ISerializable { bool m_isRotatable = false; std::string m_inventoryImage; + + std::unordered_map m_groups; }; #endif // BLOCK_HPP_ diff --git a/source/server/lua/loader/LuaBlockLoader.cpp b/source/server/lua/loader/LuaBlockLoader.cpp index bc0d9b6f..50e00a25 100644 --- a/source/server/lua/loader/LuaBlockLoader.cpp +++ b/source/server/lua/loader/LuaBlockLoader.cpp @@ -46,12 +46,17 @@ void LuaBlockLoader::loadBlock(const sol::table &table) const { loadItemDrop(block, table); loadColorMultiplier(block, table); + Item *item = nullptr; if (!block.inventoryImage().empty()) { - Registry::getInstance().registerItem(TilesDef{block.inventoryImage()}, stringID, label).setIsBlock(true); + item = &Registry::getInstance().registerItem(TilesDef{block.inventoryImage()}, stringID, label); } else { - Registry::getInstance().registerItem(block.tiles(), stringID, label).setIsBlock(true); + item = &Registry::getInstance().registerItem(block.tiles(), stringID, label); } + + item->setIsBlock(true); + + loadGroups(block, *item, table); } inline void LuaBlockLoader::loadProperties(ServerBlock &block, const sol::table &table) const { @@ -126,3 +131,21 @@ inline void LuaBlockLoader::loadColorMultiplier(ServerBlock &block, const sol::t } } +inline void LuaBlockLoader::loadGroups(ServerBlock &block, Item &item, const sol::table &table) const { + sol::object groupsObject = table["groups"]; + if (groupsObject.valid()) { + if (groupsObject.get_type() == sol::type::table) { + sol::table groupsTable = groupsObject.as(); + for (auto &groupObject : groupsTable) { + std::string groupName = "group:" + groupObject.first.as(); + u16 groupValue = groupObject.second.as(); + + block.addGroup(groupName, groupValue); + item.addGroup(groupName, groupValue); + } + } + else + DEBUG("ERROR: For block '" + block.stringID() + "': 'groups' should be a table"); + } +} + diff --git a/source/server/lua/loader/LuaBlockLoader.hpp b/source/server/lua/loader/LuaBlockLoader.hpp index 06cbb752..c6e85854 100644 --- a/source/server/lua/loader/LuaBlockLoader.hpp +++ b/source/server/lua/loader/LuaBlockLoader.hpp @@ -29,6 +29,7 @@ #include +class Item; class LuaMod; class ServerBlock; @@ -45,6 +46,8 @@ class LuaBlockLoader { void loadItemDrop(ServerBlock &block, const sol::table &table) const; void loadColorMultiplier(ServerBlock &block, const sol::table &table) const; + void loadGroups(ServerBlock &block, Item &item, const sol::table &table) const; + LuaMod &m_mod; };