diff --git a/include/core/Registry.hpp b/include/core/Registry.hpp index 96083f6b..6cd2234f 100644 --- a/include/core/Registry.hpp +++ b/include/core/Registry.hpp @@ -38,8 +38,6 @@ class Registry { return m_recipes.emplace_back(std::make_unique(std::forward(args)...)).get(); } - void registerRecipes(); - const Block &getBlock(std::size_t id) const { return *m_blocks.at(id).get(); } const Item &getItem(std::size_t id) const { return *m_items.at(id).get(); } diff --git a/include/lua/ScriptEngine.hpp b/include/lua/ScriptEngine.hpp index c7ae627d..9a732f5c 100644 --- a/include/lua/ScriptEngine.hpp +++ b/include/lua/ScriptEngine.hpp @@ -14,6 +14,8 @@ #ifndef SCRIPTENGINE_HPP_ #define SCRIPTENGINE_HPP_ +#define SOL_CHECK_ARGUMENTS 1 + #include class ScriptEngine { diff --git a/mods/items.lua b/mods/items.lua new file mode 100644 index 00000000..7964c47b --- /dev/null +++ b/mods/items.lua @@ -0,0 +1,106 @@ +Registry:registerBlock(0, 0, "Air") +Registry:registerBlock(1, 37, "Dirt") + +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") + +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") + +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 + Registry:registerBlock(i, 4, "Undefined") +end + +Registry:registerItemBlock(0, "") +Registry:registerItemBlock(1, "Dirt") +Registry:registerItemBlock(2, "Cobblestone") +Registry:registerItemBlock(3, "Grass") +Registry:registerItemBlock(4, "Leaves") +Registry:registerItemBlock(5, "Wood") +Registry:registerItemBlock(6, "Stone") +Registry:registerItemBlock(7, "Sand") +Registry:registerItemBlock(8, "Water") +Registry:registerItemBlock(9, "Glass") +Registry:registerItemBlock(10, "Coal Ore") +Registry:registerItemBlock(11, "Planks") +Registry:registerItemBlock(12, "Glowstone") +Registry:registerItemBlock(13, "Workbench") +Registry:registerItemBlock(14, "Furnace") +Registry:registerItemBlock(15, "Iron Ore") +Registry:registerItemBlock(16, "Plank Slab") + +for i = 17, 31 do + Registry:registerItemBlock(i, "Undefined") +end + +Registry:registerItem(32, "Stick", 324) + +local stoneAxe = Registry:registerItem(33, "Stone Axe", 325) +stoneAxe.miningSpeed = 4 +stoneAxe.harvestCapability = 4 + +Registry:registerItem(34, "Stone Hoe", 326) + +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) + +local coal = Registry:registerItem(38, "Coal", 111) +coal.isFuel = true +coal.burnTime = 1600 + +Registry:registerItem(39, "Iron Ingot", 232) + +local charcoal = Registry:registerItem(40, "Charcoal", 41) +charcoal.isFuel = true +charcoal.burnTime = 1600 + +local woodenAxe = Registry:registerItem(41, "Wooden Axe", 337) +woodenAxe.miningSpeed = 2 +woodenAxe.harvestCapability = 4 + +Registry:registerItem(42, "Wooden Hoe", 338) + +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) + diff --git a/mods/recipes.lua b/mods/recipes.lua new file mode 100644 index 00000000..cec73ace --- /dev/null +++ b/mods/recipes.lua @@ -0,0 +1,281 @@ +-- Wooden Axe +Registry:registerCraftingRecipe({ + result = { + item = 41, + amount = 1 + }, + + pattern = { + "##", + "#|", + " |" + }, + + keys = { + ['#'] = 11, + ['|'] = 32 + } +}) + +-- Wooden Hoe +Registry:registerCraftingRecipe({ + result = { + item = 42, + amount = 1 + }, + + pattern = { + "##", + " |", + " |" + }, + + keys = { + ['#'] = 11, + ['|'] = 32 + } +}) + +-- Wooden Pickaxe +Registry:registerCraftingRecipe({ + result = { + item = 43, + amount = 1 + }, + + pattern = { + "###", + " | ", + " | " + }, + + keys = { + ['#'] = 11, + ['|'] = 32 + } +}) + +-- Wooden Shovel +Registry:registerCraftingRecipe({ + result = { + item = 44, + amount = 1 + }, + + pattern = { + "#", + "|", + "|" + }, + + keys = { + ['#'] = 11, + ['|'] = 32 + } +}) + +-- Wooden Sword +Registry:registerCraftingRecipe({ + result = { + item = 45, + amount = 1 + }, + + pattern = { + "#", + "#", + "|" + }, + + keys = { + ['#'] = 11, + ['|'] = 32 + } +}) + +-- Stone Axe +Registry:registerCraftingRecipe({ + result = { + item = 33, + amount = 1 + }, + + pattern = { + "##", + "#|", + " |" + }, + + keys = { + ['#'] = 2, + ['|'] = 32 + } +}) + +-- Stone Hoe +Registry:registerCraftingRecipe({ + result = { + item = 34, + amount = 1 + }, + + pattern = { + "##", + " |", + " |" + }, + + keys = { + ['#'] = 2, + ['|'] = 32 + } +}) + +-- Stone Pickaxe +Registry:registerCraftingRecipe({ + result = { + item = 35, + amount = 1 + }, + + pattern = { + "###", + " | ", + " | " + }, + + keys = { + ['#'] = 2, + ['|'] = 32 + } +}) + +-- Stone Shovel +Registry:registerCraftingRecipe({ + result = { + item = 36, + amount = 1 + }, + + pattern = { + "#", + "|", + "|" + }, + + keys = { + ['#'] = 2, + ['|'] = 32 + } +}) + +-- Stone Sword +Registry:registerCraftingRecipe({ + result = { + item = 37, + amount = 1 + }, + + pattern = { + "#", + "#", + "|" + }, + + keys = { + ['#'] = 2, + ['|'] = 32 + } +}) + +-- Stick +Registry:registerCraftingRecipe({ + result = { + item = 32, + amount = 4 + }, + + pattern = { + '#', + '#' + }, + + keys = {['#'] = 11} +}) + +-- Planks +Registry:registerCraftingRecipe({ + result = { + item = 11, + amount = 4 + }, + pattern = {"#"}, + keys = {["#"] = 5} +}) + +-- Workbench +Registry:registerCraftingRecipe({ + result = { + item = 13, + amount = 1 + }, + pattern = { + "##", + "##" + }, + keys = {["#"] = 11} +}) + +-- Furnace +Registry:registerCraftingRecipe({ + result = { + item = 14, + amount = 1 + }, + + pattern = { + "###", + "# #", + "###" + }, + + keys = {["#"] = 2} +}) + + +-- Plank Slab +Registry:registerCraftingRecipe({ + result = { + item = 16, + amount = 6 + }, + + pattern = {"###"}, + + keys = {['#'] = 11} +}) + +-- Iron Ingot +Registry:registerSmeltingRecipe({ + input = {item = 15, amount = 1}, + output = {item = 39, amount = 1} +}) + +-- Stone +Registry:registerSmeltingRecipe({ + input = {item = 2, amount = 1}, + output = {item = 6, amount = 1} +}) + +-- Glass +Registry:registerSmeltingRecipe({ + input = {item = 7, amount = 1}, + output = {item = 9, amount = 1} +}) + +-- Charcoal +Registry:registerSmeltingRecipe({ + input = {item = 5, amount = 1}, + output = {item = 40, amount = 1} +}) + diff --git a/mods/test.lua b/mods/test.lua index 7445c080..9a02e465 100644 --- a/mods/test.lua +++ b/mods/test.lua @@ -1,103 +1,7 @@ print("Hello from Lua!") -Registry:registerBlock(0, 0, "Air") -Registry:registerBlock(1, 37, "Dirt") - -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") - -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") - -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 - Registry:registerBlock(i, 4, "Undefined") -end - -Registry:registerItemBlock(0, "") -Registry:registerItemBlock(1, "Dirt") -Registry:registerItemBlock(2, "Cobblestone") -Registry:registerItemBlock(3, "Grass") -Registry:registerItemBlock(4, "Leaves") -Registry:registerItemBlock(5, "Wood") -Registry:registerItemBlock(6, "Stone") -Registry:registerItemBlock(7, "Sand") -Registry:registerItemBlock(8, "Water") -Registry:registerItemBlock(9, "Glass") -Registry:registerItemBlock(10, "Coal Ore") -Registry:registerItemBlock(11, "Planks") -Registry:registerItemBlock(12, "Glowstone") -Registry:registerItemBlock(13, "Workbench") -Registry:registerItemBlock(14, "Furnace") -Registry:registerItemBlock(15, "Iron Ore") -Registry:registerItemBlock(16, "Plank Slab") - -for i = 17, 31 do - Registry:registerItemBlock(i, "Undefined") -end - -Registry:registerItem(32, "Stick", 324) - -local stoneAxe = Registry:registerItem(33, "Stone Axe", 325) -stoneAxe.miningSpeed = 4 -stoneAxe.harvestCapability = 4 - -Registry:registerItem(34, "Stone Hoe", 326) - -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).burnTime = 1600 -Registry:registerItem(39, "Iron Ingot", 232) -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) - -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) +dofile("mods/items.lua") +dofile("mods/recipes.lua") function init() Player:inventory():addStack(13, 1); diff --git a/resources/config/recipes.xml b/resources/config/recipes.xml deleted file mode 100644 index 6f70a68c..00000000 --- a/resources/config/recipes.xml +++ /dev/null @@ -1,175 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/source/core/Application.cpp b/source/core/Application.cpp index 428514f2..723d8102 100644 --- a/source/core/Application.cpp +++ b/source/core/Application.cpp @@ -34,7 +34,6 @@ void Application::init() { Registry::setInstance(m_registry); m_scriptEngine.init(); - m_registry.registerRecipes(); m_stateStack.push(); } diff --git a/source/core/Registry.cpp b/source/core/Registry.cpp index 2de09f8a..33772954 100644 --- a/source/core/Registry.cpp +++ b/source/core/Registry.cpp @@ -23,63 +23,6 @@ Registry *Registry::s_instance = nullptr; -void Registry::registerRecipes() { - XMLFile doc("resources/config/recipes.xml"); - - tinyxml2::XMLElement *recipeElement = doc.FirstChildElement("recipes").FirstChildElement("recipe").ToElement(); - while (recipeElement) { - std::string type = recipeElement->Attribute("type"); - if (type == "craft") { - std::vector pattern; - std::map> keys; - ItemStack result; - bool isShapeless = false; - - tinyxml2::XMLElement *patternElement = recipeElement->FirstChildElement("pattern"); - while (patternElement) { - pattern.emplace_back(patternElement->Attribute("string")); - patternElement = patternElement->NextSiblingElement("pattern"); - } - - tinyxml2::XMLElement *keyElement = recipeElement->FirstChildElement("key"); - while (keyElement) { - char ch = keyElement->Attribute("char")[0]; - u32 item = keyElement->UnsignedAttribute("item"); - - std::vector items; - items.emplace_back(item); - keys.emplace(ch, items); - - keyElement = keyElement->NextSiblingElement("key"); - } - - tinyxml2::XMLElement *resultElement = recipeElement->FirstChildElement("result"); - if (resultElement) { - u16 item = resultElement->UnsignedAttribute("item"); - u16 amount = resultElement->UnsignedAttribute("amount"); - result = ItemStack{item, amount}; - } - - registerRecipe(pattern, keys, result, isShapeless); - } - else if (type == "smelt") { - tinyxml2::XMLElement *inputElement = recipeElement->FirstChildElement("input"); - u16 inputItem = inputElement->UnsignedAttribute("id"); - u16 inputAmount = inputElement->UnsignedAttribute("amount"); - ItemStack input{inputItem, inputAmount}; - - tinyxml2::XMLElement *outputElement = recipeElement->FirstChildElement("output"); - u16 outputItem = outputElement->UnsignedAttribute("id"); - u16 outputAmount = outputElement->UnsignedAttribute("amount"); - ItemStack output{outputItem, outputAmount}; - - registerRecipe(input, output); - } - - recipeElement = recipeElement->NextSiblingElement("recipe"); - } -} - const Recipe *Registry::getRecipe(const Inventory &inventory) const { for (auto &recipe : m_recipes) { if (recipe->isMatching(inventory)) diff --git a/source/lua/ScriptEngine.cpp b/source/lua/ScriptEngine.cpp index 2196d341..297c40bc 100644 --- a/source/lua/ScriptEngine.cpp +++ b/source/lua/ScriptEngine.cpp @@ -16,6 +16,9 @@ #include "Player.hpp" #include "Registry.hpp" +#include "CraftingRecipe.hpp" +#include "SmeltingRecipe.hpp" + #include "BlockFurnace.hpp" #include "BlockWater.hpp" #include "BlockWorkbench.hpp" @@ -29,7 +32,7 @@ void ScriptEngine::init() { m_lua["Registry"] = &Registry::getInstance(); - m_lua.open_libraries(); + m_lua.open_libraries(sol::lib::base); m_lua.safe_script_file("mods/test.lua"); } @@ -53,6 +56,7 @@ void ScriptEngine::initUsertypes() { m_lua.new_usertype("Item", "name", &Item::name, "id", &Item::id, + "isFuel", sol::property(&Item::isFuel, &Item::setIsFuel), "burnTime", sol::property(&Item::burnTime, &Item::setBurnTime), "harvestCapability", sol::property(&Item::harvestCapability, &Item::setHarvestCapability), "miningSpeed", sol::property(&Item::miningSpeed, &Item::setMiningSpeed) @@ -68,7 +72,9 @@ void ScriptEngine::initUsertypes() { ); m_lua.new_usertype("Registry", - "registerBlock", [] (Registry *reg, u32 id, u32 textureID, const std::string &name) { + "registerBlock", [] (Registry *reg, u32 id, u32 textureID, const std::string &name) + -> Block* + { if (id == BlockType::Workbench) return reg->registerBlock(); else if (id == BlockType::Furnace) return reg->registerBlock(); else if (id == BlockType::Water) return reg->registerBlock(); @@ -79,6 +85,43 @@ void ScriptEngine::initUsertypes() { }, "registerItem", [] (Registry *reg, u32 id, const std::string &name, u32 textureID) { return reg->registerItem(id, textureID, name); + }, + "registerCraftingRecipe", [] (Registry *reg, const sol::table &recipeDefinition) { + sol::table resultTable = recipeDefinition["result"]; + sol::table patternTable = recipeDefinition["pattern"]; + sol::table keysTable = recipeDefinition["keys"]; + + ItemStack result = { + resultTable["item"].get(), + resultTable["amount"].get() + }; + + std::vector pattern; + for (auto &it : patternTable) + pattern.emplace_back(it.second.as()); + + std::map> keys; + for (auto &it : keysTable) { + keys.emplace(it.first.as(), std::vector{it.second.as()}); + } + + reg->registerRecipe(pattern, keys, result); + }, + "registerSmeltingRecipe", [] (Registry *reg, const sol::table &recipeDefinition) { + sol::table inputTable = recipeDefinition["input"]; + sol::table outputTable = recipeDefinition["output"]; + + ItemStack input = { + inputTable["item"].get(), + inputTable["amount"].get() + }; + + ItemStack output = { + outputTable["item"].get(), + outputTable["amount"].get() + }; + + reg->registerRecipe(input, output); } ); }