[ScriptEngine] Now able to register crafts.

This commit is contained in:
Quentin Bazin 2018-12-20 05:19:56 +01:00
parent 2dadf9dc0a
commit ff69ddb160
9 changed files with 436 additions and 335 deletions

View File

@ -38,8 +38,6 @@ class Registry {
return m_recipes.emplace_back(std::make_unique<T>(std::forward<Args>(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(); }

View File

@ -14,6 +14,8 @@
#ifndef SCRIPTENGINE_HPP_
#define SCRIPTENGINE_HPP_
#define SOL_CHECK_ARGUMENTS 1
#include <sol.hpp>
class ScriptEngine {

106
mods/items.lua Normal file
View File

@ -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)

281
mods/recipes.lua Normal file
View File

@ -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}
})

View File

@ -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);

View File

@ -1,175 +0,0 @@
<recipes>
<recipe type="craft">
<pattern string="##" />
<pattern string="#|" />
<pattern string=" |" />
<key char="#" item="11" />
<key char="|" item="32" />
<result item="41" amount="1" />
</recipe>
<recipe type="craft">
<pattern string="##" />
<pattern string=" |" />
<pattern string=" |" />
<key char="#" item="11" />
<key char="|" item="32" />
<result item="42" amount="1" />
</recipe>
<recipe type="craft">
<pattern string="###" />
<pattern string=" | " />
<pattern string=" | " />
<key char="#" item="11" />
<key char="|" item="32" />
<result item="43" amount="1" />
</recipe>
<recipe type="craft">
<pattern string="#" />
<pattern string="|" />
<pattern string="|" />
<key char="#" item="11" />
<key char="|" item="32" />
<result item="44" amount="1" />
</recipe>
<recipe type="craft">
<pattern string="#" />
<pattern string="#" />
<pattern string="|" />
<key char="#" item="11" />
<key char="|" item="32" />
<result item="45" amount="1" />
</recipe>
<recipe type="craft">
<pattern string="##" />
<pattern string="#|" />
<pattern string=" |" />
<key char="#" item="2" />
<key char="|" item="32" />
<result item="33" amount="1" />
</recipe>
<recipe type="craft">
<pattern string="##" />
<pattern string=" |" />
<pattern string=" |" />
<key char="#" item="2" />
<key char="|" item="32" />
<result item="34" amount="1" />
</recipe>
<recipe type="craft">
<pattern string="###" />
<pattern string=" | " />
<pattern string=" | " />
<key char="#" item="2" />
<key char="|" item="32" />
<result item="35" amount="1" />
</recipe>
<recipe type="craft">
<pattern string="#" />
<pattern string="|" />
<pattern string="|" />
<key char="#" item="2" />
<key char="|" item="32" />
<result item="36" amount="1" />
</recipe>
<recipe type="craft">
<pattern string="#" />
<pattern string="#" />
<pattern string="|" />
<key char="#" item="2" />
<key char="|" item="32" />
<result item="37" amount="1" />
</recipe>
<recipe type="craft">
<pattern string="#" />
<pattern string="#" />
<key char="#" item="11" />
<result item="32" amount="4" />
</recipe>
<recipe type="craft">
<pattern string="#" />
<key char="#" item="5" />
<result item="11" amount="4" />
</recipe>
<recipe type="craft">
<pattern string="##" />
<pattern string="##" />
<key char="#" item="11" />
<result item="13" amount="1" />
</recipe>
<recipe type="craft">
<pattern string="###" />
<pattern string="# #" />
<pattern string="###" />
<key char="#" item="2" />
<result item="14" amount="1" />
</recipe>
<recipe type="craft">
<pattern string="###" />
<key char="#" item="11" />
<result item="16" amount="6" />
</recipe>
<recipe type="smelt">
<input id="15" amount="1" />
<output id="39" amount="1" />
</recipe>
<recipe type="smelt">
<input id="2" amount="1" />
<output id="6" amount="1" />
</recipe>
<recipe type="smelt">
<input id="7" amount="1" />
<output id="9" amount="1" />
</recipe>
<recipe type="smelt">
<input id="5" amount="1" />
<output id="40" amount="1" />
</recipe>
</recipes>

View File

@ -34,7 +34,6 @@ void Application::init() {
Registry::setInstance(m_registry);
m_scriptEngine.init();
m_registry.registerRecipes();
m_stateStack.push<GameState>();
}

View File

@ -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<std::string> pattern;
std::map<char, std::vector<u32>> 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<u32> 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<CraftingRecipe>(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<SmeltingRecipe>(input, output);
}
recipeElement = recipeElement->NextSiblingElement("recipe");
}
}
const Recipe *Registry::getRecipe(const Inventory &inventory) const {
for (auto &recipe : m_recipes) {
if (recipe->isMatching(inventory))

View File

@ -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>("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>("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<BlockWorkbench>();
else if (id == BlockType::Furnace) return reg->registerBlock<BlockFurnace>();
else if (id == BlockType::Water) return reg->registerBlock<BlockWater>();
@ -79,6 +85,43 @@ void ScriptEngine::initUsertypes() {
},
"registerItem", [] (Registry *reg, u32 id, const std::string &name, u32 textureID) {
return reg->registerItem<Item>(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<u16>(),
resultTable["amount"].get<u16>()
};
std::vector<std::string> pattern;
for (auto &it : patternTable)
pattern.emplace_back(it.second.as<std::string>());
std::map<char, std::vector<u32>> keys;
for (auto &it : keysTable) {
keys.emplace(it.first.as<char>(), std::vector<u32>{it.second.as<u32>()});
}
reg->registerRecipe<CraftingRecipe>(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<u16>(),
inputTable["amount"].get<u16>()
};
ItemStack output = {
outputTable["item"].get<u16>(),
outputTable["amount"].get<u16>()
};
reg->registerRecipe<SmeltingRecipe>(input, output);
}
);
}