From 2dadf9dc0a08bbea49962e0b9a9d2f5c369e421c Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Thu, 20 Dec 2018 03:30:11 +0100 Subject: [PATCH] [ScriptEngine] Now able to register block/item properties. --- include/core/Registry.hpp | 14 +++---- include/lua/ScriptEngine.hpp | 1 + mods/test.lua | 70 +++++++++++++++++++++++++------- resources/config/blocks.xml | 50 ----------------------- resources/config/items.xml | 49 ---------------------- source/core/Registry.cpp | 78 ------------------------------------ source/lua/ScriptEngine.cpp | 43 +++++++++++++++----- 7 files changed, 94 insertions(+), 211 deletions(-) delete mode 100644 resources/config/blocks.xml delete mode 100644 resources/config/items.xml diff --git a/include/core/Registry.hpp b/include/core/Registry.hpp index 6b915ca9..96083f6b 100644 --- a/include/core/Registry.hpp +++ b/include/core/Registry.hpp @@ -24,22 +24,20 @@ class Registry { public: template - auto registerBlock(Args &&...args) -> typename std::enable_if::value, Block&>::type { - return *m_blocks.emplace_back(std::make_unique(std::forward(args)...)).get(); + auto registerBlock(Args &&...args) -> typename std::enable_if::value, Block*>::type { + return m_blocks.emplace_back(std::make_unique(std::forward(args)...)).get(); } template - auto registerItem(Args &&...args) -> typename std::enable_if::value, Item&>::type { - return *m_items.emplace_back(std::make_unique(std::forward(args)...)).get(); + auto registerItem(Args &&...args) -> typename std::enable_if::value, Item*>::type { + return m_items.emplace_back(std::make_unique(std::forward(args)...)).get(); } template - auto registerRecipe(Args &&...args) -> typename std::enable_if::value, Recipe&>::type { - return *m_recipes.emplace_back(std::make_unique(std::forward(args)...)).get(); + auto registerRecipe(Args &&...args) -> typename std::enable_if::value, Recipe*>::type { + return m_recipes.emplace_back(std::make_unique(std::forward(args)...)).get(); } - void registerBlocks(); - void registerItems(); void registerRecipes(); const Block &getBlock(std::size_t id) const { return *m_blocks.at(id).get(); } diff --git a/include/lua/ScriptEngine.hpp b/include/lua/ScriptEngine.hpp index 92235406..c7ae627d 100644 --- a/include/lua/ScriptEngine.hpp +++ b/include/lua/ScriptEngine.hpp @@ -19,6 +19,7 @@ class ScriptEngine { public: void init(); + void initUsertypes(); sol::state &lua() { return m_lua; } diff --git a/mods/test.lua b/mods/test.lua index 7f9b624d..7445c080 100644 --- a/mods/test.lua +++ b/mods/test.lua @@ -2,20 +2,38 @@ print("Hello from Lua!") Registry:registerBlock(0, 0, "Air") Registry:registerBlock(1, 37, "Dirt") -Registry:registerBlock(2, 38, "Cobblestone") -Registry:registerBlock(3, 226, "Grass") -Registry:registerBlock(4, 266, "Leaves") -Registry:registerBlock(5, 277, "Wood") -Registry:registerBlock(6, 402, "Stone") + +local cobblestone = Registry:registerBlock(2, 38, "Cobblestone") +cobblestone.hardness = 2 +cobblestone.harvestRequirements = 1 + +Registry:registerBlock(3, 226, "Grass"):setItemDrop(1, 1) +Registry:registerBlock(4, 266, "Leaves").hardness = 0.5 +Registry:registerBlock(5, 277, "Wood").hardness = 2 + +local stone = Registry:registerBlock(6, 402, "Stone") +stone.hardness = 1.5 +stone.harvestRequirements = 1 +stone:setItemDrop(2, 1) + Registry:registerBlock(7, 369, "Sand") Registry:registerBlock(8, 457, "Water") Registry:registerBlock(9, 168, "Glass") -Registry:registerBlock(10, 36, "Coal Ore") + +local coalOre = Registry:registerBlock(10, 36, "Coal Ore") +coalOre.hardness = 3 +coalOre.harvestRequirements = 1 +coalOre:setItemDrop(38, 1) + Registry:registerBlock(11, 316, "Planks") Registry:registerBlock(12, 218, "Glowstone") Registry:registerBlock(13, 77, "Workbench") Registry:registerBlock(14, 164, "Furnace") -Registry:registerBlock(15, 254, "Iron Ore") + +local ironOre = Registry:registerBlock(15, 254, "Iron Ore") +ironOre.hardness = 3 +ironOre.harvestRequirements = 1 + Registry:registerBlock(16, 316, "Plank Slab") for i = 17, 31 do @@ -45,18 +63,40 @@ for i = 17, 31 do end Registry:registerItem(32, "Stick", 324) -Registry:registerItem(33, "Stone Axe", 325) + +local stoneAxe = Registry:registerItem(33, "Stone Axe", 325) +stoneAxe.miningSpeed = 4 +stoneAxe.harvestCapability = 4 + Registry:registerItem(34, "Stone Hoe", 326) -Registry:registerItem(35, "Stone Pickaxe", 327) -Registry:registerItem(36, "Stone Shovel", 328) + +local stonePickaxe = Registry:registerItem(35, "Stone Pickaxe", 327) +stonePickaxe.miningSpeed = 4 +stonePickaxe.harvestCapability = 1 + +local stoneShovel = Registry:registerItem(36, "Stone Shovel", 328) +stoneShovel.miningSpeed = 4 +stoneShovel.harvestCapability = 2 + Registry:registerItem(37, "Stone Sword", 329) -Registry:registerItem(38, "Coal", 111) +Registry:registerItem(38, "Coal", 111).burnTime = 1600 Registry:registerItem(39, "Iron Ingot", 232) -Registry:registerItem(40, "Charcoal", 41) -Registry:registerItem(41, "Wooden Axe", 337) +Registry:registerItem(40, "Charcoal", 41).burnTime = 1600 + +local woodenAxe = Registry:registerItem(41, "Wooden Axe", 337) +woodenAxe.miningSpeed = 2 +woodenAxe.harvestCapability = 4 + Registry:registerItem(42, "Wooden Hoe", 338) -Registry:registerItem(43, "Wooden Pickaxe", 339) -Registry:registerItem(44, "Wooden Shovel", 340) + +local woodenPickaxe = Registry:registerItem(43, "Wooden Pickaxe", 339) +woodenPickaxe.miningSpeed = 2 +woodenPickaxe.harvestCapability = 1 + +local woodenShovel = Registry:registerItem(44, "Wooden Shovel", 340) +woodenShovel.miningSpeed = 2 +woodenShovel.harvestCapability = 2 + Registry:registerItem(45, "Wooden Sword", 341) function init() diff --git a/resources/config/blocks.xml b/resources/config/blocks.xml deleted file mode 100644 index 66ee8a46..00000000 --- a/resources/config/blocks.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/resources/config/items.xml b/resources/config/items.xml deleted file mode 100644 index 8d6b11f2..00000000 --- a/resources/config/items.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/source/core/Registry.cpp b/source/core/Registry.cpp index 56bafa77..2de09f8a 100644 --- a/source/core/Registry.cpp +++ b/source/core/Registry.cpp @@ -23,84 +23,6 @@ Registry *Registry::s_instance = nullptr; -void Registry::registerBlocks() { - XMLFile doc("resources/config/blocks.xml"); - - tinyxml2::XMLElement *blockElement = doc.FirstChildElement("blocks").FirstChildElement("block").ToElement(); - while (blockElement) { - u16 id = blockElement->UnsignedAttribute("id"); - const char *name = blockElement->Attribute("name"); - - if (id == BlockType::Workbench) registerBlock(); - else if (id == BlockType::Furnace) registerBlock(); - else if (id == BlockType::Water) registerBlock(); - else { - u16 textureID = blockElement->UnsignedAttribute("textureID"); - - auto &block = registerBlock(id, textureID, name); - - float hardness = 0; - if (blockElement->QueryFloatAttribute("hardness", &hardness) == tinyxml2::XMLError::XML_SUCCESS) - block.setHardness(hardness); - - unsigned int harvestRequirements = 0; - if (blockElement->QueryUnsignedAttribute("harvestRequirements", &harvestRequirements) == tinyxml2::XMLError::XML_SUCCESS) - block.setHarvestRequirements(harvestRequirements); - - tinyxml2::XMLElement *itemDropElement = blockElement->FirstChildElement("item_drop"); - if (itemDropElement) { - block.setItemDrop(itemDropElement->UnsignedAttribute("id"), itemDropElement->UnsignedAttribute("amount")); - } - - tinyxml2::XMLElement *boundingBoxElement = blockElement->FirstChildElement("box"); - if (boundingBoxElement) { - block.setBoundingBox(FloatBox{boundingBoxElement->FloatAttribute("x"), - boundingBoxElement->FloatAttribute("y"), - boundingBoxElement->FloatAttribute("z"), - boundingBoxElement->FloatAttribute("width"), - boundingBoxElement->FloatAttribute("height"), - boundingBoxElement->FloatAttribute("depth")}); - } - } - - blockElement = blockElement->NextSiblingElement("block"); - } -} - -void Registry::registerItems() { - XMLFile doc("resources/config/items.xml"); - - tinyxml2::XMLElement *itemElement = doc.FirstChildElement("items").FirstChildElement("item").ToElement(); - while (itemElement) { - u16 id = itemElement->UnsignedAttribute("id"); - const char *name = itemElement->Attribute("name"); - - unsigned int textureID; - if (itemElement->QueryUnsignedAttribute("textureID", &textureID) == tinyxml2::XMLError::XML_SUCCESS) { - auto &item = registerItem(id, textureID, name); - - float miningSpeed = 0; - if (itemElement->QueryFloatAttribute("miningSpeed", &miningSpeed) == tinyxml2::XMLError::XML_SUCCESS) - item.setMiningSpeed(miningSpeed); - - unsigned int harvestCapability = 0; - if (itemElement->QueryUnsignedAttribute("harvestCapability", &harvestCapability) == tinyxml2::XMLError::XML_SUCCESS) - item.setHarvestCapability(harvestCapability); - - unsigned int burnTime = 0; - if (itemElement->QueryUnsignedAttribute("burnTime", &burnTime) == tinyxml2::XMLError::XML_SUCCESS) { - item.setIsFuel(true); - item.setBurnTime(burnTime); - } - } - else { - registerItem(id, id, name); - } - - itemElement = itemElement->NextSiblingElement("item"); - } -} - void Registry::registerRecipes() { XMLFile doc("resources/config/recipes.xml"); diff --git a/source/lua/ScriptEngine.cpp b/source/lua/ScriptEngine.cpp index d4e5c334..2196d341 100644 --- a/source/lua/ScriptEngine.cpp +++ b/source/lua/ScriptEngine.cpp @@ -16,13 +16,24 @@ #include "Player.hpp" #include "Registry.hpp" -#include +#include "BlockFurnace.hpp" +#include "BlockWater.hpp" +#include "BlockWorkbench.hpp" ScriptEngine *ScriptEngine::s_instance = nullptr; void ScriptEngine::init() { setInstance(this); + initUsertypes(); + + m_lua["Registry"] = &Registry::getInstance(); + + m_lua.open_libraries(); + m_lua.safe_script_file("mods/test.lua"); +} + +void ScriptEngine::initUsertypes() { m_lua.new_usertype("Player", "inventory", &Player::inventory); @@ -40,25 +51,35 @@ void ScriptEngine::init() { ); m_lua.new_usertype("Item", - "name", &Item::name, - "id", &Item::id + "name", &Item::name, + "id", &Item::id, + "burnTime", sol::property(&Item::burnTime, &Item::setBurnTime), + "harvestCapability", sol::property(&Item::harvestCapability, &Item::setHarvestCapability), + "miningSpeed", sol::property(&Item::miningSpeed, &Item::setMiningSpeed) + ); + + m_lua.new_usertype("Block", + "id", &Block::id, + "data", &Block::data, + "name", sol::property(&Block::name, &Block::setName), + "harvestRequirements", sol::property(&Block::harvestRequirements, &Block::setHarvestRequirements), + "hardness", sol::property(&Block::hardness, &Block::setHardness), + "setItemDrop", &Block::setItemDrop ); m_lua.new_usertype("Registry", "registerBlock", [] (Registry *reg, u32 id, u32 textureID, const std::string &name) { - reg->registerBlock(id, textureID, name); + if (id == BlockType::Workbench) return reg->registerBlock(); + else if (id == BlockType::Furnace) return reg->registerBlock(); + else if (id == BlockType::Water) return reg->registerBlock(); + else return reg->registerBlock(id, textureID, name); }, "registerItemBlock", [] (Registry *reg, u32 id, const std::string &name) { - reg->registerItem(id, id, name); + return reg->registerItem(id, id, name); }, "registerItem", [] (Registry *reg, u32 id, const std::string &name, u32 textureID) { - reg->registerItem(id, textureID, name); + return reg->registerItem(id, textureID, name); } ); - - m_lua["Registry"] = &Registry::getInstance(); - - m_lua.open_libraries(); - m_lua.script_file("mods/test.lua"); }