/* * ===================================================================================== * * OpenMiner * * Copyright (C) 2018-2020 Unarelith, Quentin Bazin * Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md) * * This file is part of OpenMiner. * * OpenMiner 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. * * OpenMiner 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 OpenMiner; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * ===================================================================================== */ #include #include "LuaBlockLoader.hpp" #include "LuaMod.hpp" #include "Registry.hpp" #include "ServerBlock.hpp" #include "ServerConfig.hpp" void LuaBlockLoader::loadBlock(const sol::table &table) const { std::string stringID = m_mod.id() + ":" + table["id"].get(); ServerBlock &block = Registry::getInstance().registerBlock(stringID); block.setRotatable(table["is_rotatable"].get_or(false)); block.setOnBlockActivated(table["on_block_activated"]); block.setOnTick(table["on_tick"]); block.setOnBlockPlaced(table["on_block_placed"]); block.setOnBlockDestroyed(table["on_block_destroyed"]); block.setTickRandomly(table["tick_randomly"].get_or(false)); block.setTickProbability(table["tick_probability"].get_or(0.f)); BlockState &defaultState = block.getState(0); loadBlockState(defaultState, table, block); Item *item = nullptr; if (!defaultState.inventoryImage().empty()) { TilesDef tilesDef{block.stringID(), 0, defaultState.inventoryImage()}; item = &Registry::getInstance().registerItem(tilesDef, block.stringID(), defaultState.label()); } else { item = &Registry::getInstance().registerItem(defaultState.tiles(), block.stringID(), defaultState.label()); } item->setIsBlock(true); item->setMaxStackSize(table["max_stack_size"].get_or(ServerConfig::maxItemStackSize)); loadGroups(block, table, item); loadParams(block); } void LuaBlockLoader::loadBlockState(BlockState &state, const sol::table &table, ServerBlock &block) const { TilesDef tiles{block.stringID(), state.id()}; tiles.loadFromLuaTable(table); if (!tiles.textureFilenames().empty()) state.tiles(tiles); loadProperties(state, table); loadBoundingBox(state, table); loadDrawType(state, table, block); loadItemDrop(state, table); loadColorMultiplier(state, table); loadStates(block, state, table); } inline void LuaBlockLoader::loadProperties(BlockState &state, const sol::table &table) const { if (table["name"].get_type() == sol::type::string) state.label(table["name"].get()); if (table["harvest_requirements"].get_type() == sol::type::number) state.harvestRequirements(table["harvest_requirements"].get()); if (table["hardness"].get_type() == sol::type::number) state.hardness(table["hardness"].get()); if (table["is_opaque"].get_type() == sol::type::boolean) state.isOpaque(table["is_opaque"].get()); if (table["is_light_source"].get_type() == sol::type::boolean) state.isLightSource(table["is_light_source"].get()); if (table["inventory_image"].get_type() == sol::type::string) state.inventoryImage(table["inventory_image"].get()); if (table["fog_depth"].get_type() == sol::type::number) state.fogDepth(table["fog_depth"].get()); if (state.fogDepth()) { sol::optional fogColor = table["fog_color"]; if (fogColor != sol::nullopt) { state.fogColor(gk::Color{ fogColor.value().get(1), fogColor.value().get(2), fogColor.value().get(3), }); } } sol::optional drawOffset = table["draw_offset"]; if (drawOffset != sol::nullopt) { state.drawOffset(gk::Vector3f{ drawOffset.value().get(1), drawOffset.value().get(2), drawOffset.value().get(3), }); } } inline void LuaBlockLoader::loadBoundingBox(BlockState &state, const sol::table &table) const { sol::optional boundingBox = table["bounding_box"]; if (boundingBox != sol::nullopt) { state.boundingBox(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), }); } } inline void LuaBlockLoader::loadDrawType(BlockState &state, const sol::table &table, const ServerBlock &block) const { sol::object drawTypeObject = table["draw_type"]; if (drawTypeObject.valid()) { if (drawTypeObject.get_type() == sol::type::string) { static const std::unordered_map drawTypes = { {"solid", BlockDrawType::Solid}, {"xshape", BlockDrawType::XShape}, {"leaves", BlockDrawType::Leaves}, {"liquid", BlockDrawType::Liquid}, {"glass", BlockDrawType::Glass}, {"cactus", BlockDrawType::Cactus}, {"boundingbox", BlockDrawType::BoundingBox}, // FIXME: Temporary }; auto it = drawTypes.find(drawTypeObject.as()); if (it != drawTypes.end()) { state.drawType(it->second); } else gkError() << "In" << block.stringID() << " definition: Block draw type invalid"; } else gkError() << "In" << block.stringID() << " definition: Block draw type must be a string"; } } inline void LuaBlockLoader::loadItemDrop(BlockState &state, const sol::table &table) const { sol::optional itemDrop = table["item_drop"]; if (itemDrop != sol::nullopt) { state.itemDrop(itemDrop.value()["id"]); state.itemDropAmount(itemDrop.value()["amount"]); } } inline void LuaBlockLoader::loadColorMultiplier(BlockState &state, const sol::table &table) const { sol::optional colorMultiplier = table["color_multiplier"]; if (colorMultiplier != sol::nullopt) { state.colorMultiplier(gk::Color{ colorMultiplier.value().get(1), colorMultiplier.value().get(2), colorMultiplier.value().get(3), colorMultiplier.value().get(4) }); } } inline void LuaBlockLoader::loadStates(ServerBlock &block, BlockState &state, const sol::table &table) const { sol::object statesObject = table["states"]; if (statesObject.valid()) { if (statesObject.get_type() == sol::type::table) { sol::table statesTable = statesObject.as(); for (auto &statesObject : statesTable) { unsigned int stateID = statesObject.first.as(); if (stateID == block.states().size()) { BlockState &state = block.addState(); loadBlockState(state, statesObject.second.as(), block); } else { gkError() << ("For block '" + block.stringID() + "':").c_str() << "States must be defined in a correct order starting from 1"; gkError() << "StateID:" << stateID << "| States registered:" << block.states().size(); } } } else gkError() << "For block" << state.block().stringID() << ": 'states' must be a table"; } } inline void LuaBlockLoader::loadGroups(ServerBlock &block, const sol::table &table, Item *item) 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); if (item) item->addGroup(groupName, groupValue); } } else gkError() << "For block" << block.stringID() << ": 'groups' must be a table"; } } inline void LuaBlockLoader::loadParams(ServerBlock &block) const { if (block.isRotatable()) block.param().allocateBits(BlockParam::Type::Rotation, 5); if (block.states().size() > 1) { int bits = 0; int index = block.states().size(); while (index >>= 1) ++bits; block.param().allocateBits(BlockParam::Type::State, bits); } }