/* * ===================================================================================== * * 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 "BlockPlacementConstraints.hpp" #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"].get()); block.setOnTick(table["on_tick"].get()); block.setOnBlockPlaced(table["on_block_placed"].get()); block.setOnBlockDestroyed(table["on_block_destroyed"].get()); block.setTickRandomly(table["tick_randomly"].get_or(false)); block.setTickProbability(table["tick_probability"].get_or(0.f)); block.setCustomParamBits(table["custom_param_bits"].get_or(0)); 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); loadDrawType(state, table, block); loadProperties(state, table); loadBoundingBox(state, table); loadItemDrop(state, table); loadColorMultiplier(state, table); loadPlacementConstraints(block, 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() > 0.f) { sol::optional fogColor = table["fog_color"]; if (fogColor != sol::nullopt) { state.fogColor(gk::Color::fromRGBA32( 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), }); } if (table["is_collidable"].get_type() == sol::type::boolean) state.isCollidable(table["is_collidable"].get()); } 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"].get(); 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"; } if (state.drawType() == BlockDrawType::Liquid || state.drawType() == BlockDrawType::XShape) { state.isCollidable(false); } } 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::fromRGBA32( 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"].get(); 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::loadPlacementConstraints(ServerBlock &block, const sol::table &table) const { sol::object constraintsObject = table["placement_constraints"].get(); if (constraintsObject.valid()) { if (constraintsObject.get_type() == sol::type::table) { sol::table constraintsTable = constraintsObject.as(); for (auto &constraintsObject : constraintsTable) { BlockPlacementConstraint constraint; sol::optional blockOffset = constraintsObject.first.as(); if (blockOffset != sol::nullopt) { constraint.blockOffset.x = blockOffset.value().get(1); constraint.blockOffset.y = blockOffset.value().get(2); constraint.blockOffset.z = blockOffset.value().get(3); } else gkError() << "For block" << block.stringID() << ": 'placement_constraints' offset is wrong"; sol::optional constraintTable = constraintsObject.second.as(); if (constraintTable != sol::nullopt) { constraint.blockID = constraintTable.value()["block"].get(); constraint.isWhitelist = constraintTable.value()["is_whitelist"].get(); } else gkError() << "For block" << block.stringID() << ": 'placement_constraints' table is wrong"; block.placementConstraints().addConstraint(constraint); } } else gkError() << "For block" << block.stringID() << ": 'placement_constraints' must be a table"; } } inline void LuaBlockLoader::loadGroups(ServerBlock &block, const sol::table &table, Item *item) const { sol::object groupsObject = table["groups"].get(); 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) { u8 bits = 1; std::size_t index = block.states().size(); while (index >>= 1) ++bits; block.param().allocateBits(BlockParam::Type::State, bits); } if (block.customParamBits()) { block.param().allocateBits(BlockParam::Type::Custom, block.customParamBits()); } }