/* * ===================================================================================== * * OpenMiner * Copyright (C) 2018-2020 Unarelith, Quentin Bazin * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * ===================================================================================== */ #include #include "CraftingRecipe.hpp" #include "LuaMod.hpp" #include "Registry.hpp" #include "ServerBlock.hpp" #include "SmeltingRecipe.hpp" void LuaMod::registerBlock(const sol::table &table) { TilesDef tiles; tiles.loadFromLuaTable(table); std::string stringID = m_id + ":" + table["id"].get(); std::string label = table["name"].get(); sol::function onBlockActivated = table["on_block_activated"]; sol::function onTick = table["on_tick"]; ServerBlock &block = Registry::getInstance().registerBlock(tiles, stringID, label); block.setHarvestRequirements(table["harvest_requirements"].get_or(0)); block.setHardness(table["hardness"].get_or(1.0f)); block.setOpaque(table["is_opaque"].get_or(true)); block.setLightSource(table["is_light_source"].get_or(false)); block.setOnBlockActivated(onBlockActivated); block.setOnTick(onTick); block.setOnBlockPlaced(table["on_block_placed"]); sol::optional boundingBox = table["bounding_box"]; if (boundingBox != sol::nullopt) { block.setBoundingBox(gk::FloatBox{ boundingBox.value().get(1), boundingBox.value().get(2), boundingBox.value().get(3), boundingBox.value().get(4), boundingBox.value().get(5), boundingBox.value().get(6), }); } // FIXME: Use string instead sol::optional drawType = table["draw_type"]; if (drawType != sol::nullopt) { block.setDrawType(drawType.value()); } sol::optional itemDrop = table["item_drop"]; if (itemDrop != sol::nullopt) { std::string dropID = itemDrop.value()["id"]; u16 dropAmount = itemDrop.value()["amount"]; block.setItemDrop(dropID, dropAmount); } sol::optional colorMultiplier = table["color_multiplier"]; if (colorMultiplier != sol::nullopt) { block.setColorMultiplier(gk::Color{ colorMultiplier.value().get(1), colorMultiplier.value().get(2), colorMultiplier.value().get(3), colorMultiplier.value().get(4) }); } Registry::getInstance().registerItem(block.tiles(), stringID, label).setIsBlock(true); } void LuaMod::registerItem(const sol::table &table) { TilesDef tiles; tiles.loadFromLuaTable(table); std::string stringID = table["id"].get(); std::string label = table["name"].get(); Item &item = Registry::getInstance().registerItem(tiles, stringID, label); item.setIsFuel(table["is_fuel"].get_or(false)); item.setBurnTime(table["burn_time"].get_or(0)); item.setHarvestCapability(table["harvest_capability"].get_or(0)); item.setMiningSpeed(table["mining_speed"].get_or(1)); } void LuaMod::registerCraftingRecipe(const sol::table &table) { sol::table resultTable = table["result"]; sol::table patternTable = table["pattern"]; sol::table keysTable = table["keys"]; ItemStack result = { resultTable["id"].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()}); } Registry::getInstance().registerRecipe(pattern, keys, result); } void LuaMod::registerSmeltingRecipe(const sol::table &table) { sol::table inputTable = table["input"]; sol::table outputTable = table["output"]; ItemStack input = { inputTable["id"].get(), inputTable["amount"].get() }; ItemStack output = { outputTable["id"].get(), outputTable["amount"].get() }; Registry::getInstance().registerRecipe(input, output); } void LuaMod::initUsertype(sol::state &lua) { lua.new_usertype("LuaMod", sol::constructors(), "id", &LuaMod::id, "block", &LuaMod::registerBlock, "item", &LuaMod::registerItem, "crafting_recipe", &LuaMod::registerCraftingRecipe, "smelting_recipe", &LuaMod::registerSmeltingRecipe ); }