Now using ServerConfig in Lua + Collision callback: OK.

This commit is contained in:
Quentin Bazin 2020-05-17 18:30:28 +02:00
parent 2f82101fb8
commit 4ff7714ece
11 changed files with 42 additions and 102 deletions

View File

@ -174,17 +174,16 @@ mod:entity {
hitbox = {0, 0, 0, 0.25, 0.25, 0.25}, hitbox = {0, 0, 0, 0.25, 0.25, 0.25},
}, },
-- on_collision = function(entity, other) on_collision = function(item_stack, entity, entity_net_id, player, registry, server)
-- if other:type() == "player" then player:inventory():add_stack(item_stack:item():string_id(), item_stack:amount())
-- other:inventory():add_item(entity:properties():itemstack()) server:send_player_inv_update(player:client_id(), player:client());
-- end server:send_entity_despawn(entity_net_id, nil)
-- end, registry:destroy(entity)
end,
} }
useItemDrops = true
openminer:add_listener(EventType.OnBlockDigged, function(pos, block, player, world, client, server) openminer:add_listener(EventType.OnBlockDigged, function(pos, block, player, world, client, server)
if useItemDrops then if ServerConfig.useItemDrops then
mods["default"]:spawn_entity("default:item_drop", { mods["default"]:spawn_entity("default:item_drop", {
position = {pos.x + 0.5, pos.y + 0.5, pos.z + 0.5}, position = {pos.x + 0.5, pos.y + 0.5, pos.z + 0.5},
dimension = world:dimension():id(), dimension = world:dimension():id(),

View File

@ -34,7 +34,6 @@
#include "ClientCommandHandler.hpp" #include "ClientCommandHandler.hpp"
#include "ClientPlayer.hpp" #include "ClientPlayer.hpp"
#include "ClientWorld.hpp" #include "ClientWorld.hpp"
#include "ItemDropFactory.hpp"
#include "Registry.hpp" #include "Registry.hpp"
#include "Sky.hpp" #include "Sky.hpp"
#include "TextureAtlas.hpp" #include "TextureAtlas.hpp"

View File

@ -26,6 +26,7 @@
*/ */
#include "LuaCore.hpp" #include "LuaCore.hpp"
#include "Registry.hpp" #include "Registry.hpp"
#include "ServerConfig.hpp"
#include "ServerModLoader.hpp" #include "ServerModLoader.hpp"
#include "ServerWorld.hpp" #include "ServerWorld.hpp"
@ -40,6 +41,10 @@ void LuaCore::initUsertype(sol::state &lua) {
"OnBlockActivated", LuaEventType::OnBlockActivated "OnBlockActivated", LuaEventType::OnBlockActivated
); );
lua["ServerConfig"] = lua.create_table_with(
"useItemDrops", ServerConfig::useItemDrops
);
lua.new_usertype<LuaCore>("LuaCore", lua.new_usertype<LuaCore>("LuaCore",
"add_listener", &LuaCore::addListener, "add_listener", &LuaCore::addListener,

View File

@ -51,6 +51,10 @@ void ScriptEngine::init() {
} }
void ScriptEngine::initUsertypes() { void ScriptEngine::initUsertypes() {
m_lua.new_usertype<entt::registry>("entt_registry",
"destroy", static_cast<void (entt::registry::*)(const entt::entity)>(&entt::registry::destroy)
);
m_lua.new_usertype<Registry>("Registry", m_lua.new_usertype<Registry>("Registry",
"get_block", &Registry::getBlock, "get_block", &Registry::getBlock,
"get_item", &Registry::getItem, "get_item", &Registry::getItem,
@ -190,7 +194,8 @@ void ScriptEngine::initUsertypes() {
m_lua.new_usertype<ServerCommandHandler>("ServerCommandHandler", m_lua.new_usertype<ServerCommandHandler>("ServerCommandHandler",
"send_player_change_dimension", &ServerCommandHandler::sendPlayerChangeDimension, "send_player_change_dimension", &ServerCommandHandler::sendPlayerChangeDimension,
"send_player_inv_update", &ServerCommandHandler::sendPlayerInvUpdate, "send_player_inv_update", &ServerCommandHandler::sendPlayerInvUpdate,
"send_chat_message", &ServerCommandHandler::sendChatMessage "send_chat_message", &ServerCommandHandler::sendChatMessage,
"send_entity_despawn", &ServerCommandHandler::sendEntityDespawn
); );
m_lua.new_usertype<ServerModLoader>("ServerModLoader", m_lua.new_usertype<ServerModLoader>("ServerModLoader",

View File

@ -28,6 +28,7 @@
#include "AnimationComponent.hpp" #include "AnimationComponent.hpp"
#include "DrawableDef.hpp" #include "DrawableDef.hpp"
#include "LuaCallbackComponent.hpp"
#include "LuaEntityLoader.hpp" #include "LuaEntityLoader.hpp"
#include "LuaMod.hpp" #include "LuaMod.hpp"
#include "NetworkComponent.hpp" #include "NetworkComponent.hpp"
@ -41,9 +42,11 @@ void LuaEntityLoader::loadEntity(const sol::table &table) const {
entt::registry &registry = Registry::getInstance().entityRegistry(); entt::registry &registry = Registry::getInstance().entityRegistry();
entt::entity entity = Registry::getInstance().registerEntity(stringID); entt::entity entity = Registry::getInstance().registerEntity(stringID);
// TODO: Create a struct to store these functions sol::optional<sol::unsafe_function> onCollision = table["on_collision"];
sol::unsafe_function onSpawn = table["on_spawn"]; if (onCollision != sol::nullopt) {
sol::unsafe_function onCollision = table["on_collision"]; auto &luaCallbackComponent = registry.emplace<LuaCallbackComponent>(entity);
luaCallbackComponent.collisionCallback = onCollision.value();
}
sol::optional<sol::table> properties = table["properties"]; sol::optional<sol::table> properties = table["properties"];
if (properties != sol::nullopt) { if (properties != sol::nullopt) {
@ -90,7 +93,7 @@ void LuaEntityLoader::loadEntity(const sol::table &table) const {
sol::optional<sol::table> hitboxTable = properties.value()["hitbox"]; sol::optional<sol::table> hitboxTable = properties.value()["hitbox"];
if (hitboxTable != sol::nullopt) { if (hitboxTable != sol::nullopt) {
registry.emplace<gk::FloatBox>(entity, registry.emplace<gk::DoubleBox>(entity,
hitboxTable.value()[1], hitboxTable.value()[2], hitboxTable.value()[3], hitboxTable.value()[1], hitboxTable.value()[2], hitboxTable.value()[3],
hitboxTable.value()[4], hitboxTable.value()[5], hitboxTable.value()[6] hitboxTable.value()[4], hitboxTable.value()[5], hitboxTable.value()[6]
); );
@ -136,6 +139,10 @@ void LuaEntityLoader::spawnEntity(const std::string &entityID, const sol::table
if (registry.has<RotationComponent>(entityModel)) if (registry.has<RotationComponent>(entityModel))
sceneRegistry.emplace<RotationComponent>(entity); sceneRegistry.emplace<RotationComponent>(entity);
auto *luaCallbackComponent = registry.try_get<LuaCallbackComponent>(entityModel);
if (luaCallbackComponent)
sceneRegistry.emplace<LuaCallbackComponent>(entity, *luaCallbackComponent);
auto *drawable = registry.try_get<DrawableDef>(entityModel); auto *drawable = registry.try_get<DrawableDef>(entityModel);
if (drawable) { if (drawable) {
auto &drawableDef = sceneRegistry.emplace<DrawableDef>(entity, *drawable); auto &drawableDef = sceneRegistry.emplace<DrawableDef>(entity, *drawable);
@ -149,9 +156,9 @@ void LuaEntityLoader::spawnEntity(const std::string &entityID, const sol::table
if (animation) if (animation)
sceneRegistry.emplace<AnimationComponent>(entity, *animation); sceneRegistry.emplace<AnimationComponent>(entity, *animation);
auto *hitbox = registry.try_get<gk::FloatBox>(entityModel); auto *hitbox = registry.try_get<gk::DoubleBox>(entityModel);
if (hitbox) if (hitbox)
sceneRegistry.emplace<gk::FloatBox>(entity, *hitbox); sceneRegistry.emplace<gk::DoubleBox>(entity, *hitbox);
if (stack.amount()) if (stack.amount())
sceneRegistry.emplace<ItemStack>(entity, stack); sceneRegistry.emplace<ItemStack>(entity, stack);

View File

@ -284,7 +284,6 @@ void ServerCommandHandler::setupCallbacks() {
ServerWorld &world = getWorldForClient(client.id); ServerWorld &world = getWorldForClient(client.id);
const Block &blockDef = Registry::getInstance().getBlock(world.getBlock(x, y, z)); const Block &blockDef = Registry::getInstance().getBlock(world.getBlock(x, y, z));
world.onBlockDigged(x, y, z, blockDef, *player);
world.setBlock(x, y, z, 0); world.setBlock(x, y, z, 0);
m_scriptEngine.luaCore().onEvent(LuaEventType::OnBlockDigged, glm::ivec3{x, y, z}, blockDef, *player, world, client, *this); m_scriptEngine.luaCore().onEvent(LuaEventType::OnBlockDigged, glm::ivec3{x, y, z}, blockDef, *player, world, client, *this);

View File

@ -24,16 +24,13 @@
* *
* ===================================================================================== * =====================================================================================
*/ */
#ifndef ITEMDROPFACTORY_HPP_ #ifndef LUACALLBACKCOMPONENT_HPP_
#define ITEMDROPFACTORY_HPP_ #define LUACALLBACKCOMPONENT_HPP_
#include <gk/core/IntTypes.hpp> #include <sol.hpp>
#include <entt/entt.hpp> struct LuaCallbackComponent {
sol::unsafe_function collisionCallback;
class ItemDropFactory {
public:
static void create(entt::registry &registry, double x, double y, double z, u16 dimension, const std::string &itemID, u16 amount = 1);
}; };
#endif // ITEMDROPFACTORY_HPP_ #endif // LUACALLBACKCOMPONENT_HPP_

View File

@ -24,25 +24,24 @@
* *
* ===================================================================================== * =====================================================================================
*/ */
#include <gk/core/Debug.hpp>
#include "CollisionController.hpp" #include "CollisionController.hpp"
#include "ItemStack.hpp" #include "ItemStack.hpp"
#include "LuaCallbackComponent.hpp"
#include "NetworkComponent.hpp" #include "NetworkComponent.hpp"
#include "PlayerList.hpp" #include "PlayerList.hpp"
#include "PositionComponent.hpp" #include "PositionComponent.hpp"
#include "ServerCommandHandler.hpp" #include "ServerCommandHandler.hpp"
void CollisionController::update(entt::registry &registry) { void CollisionController::update(entt::registry &registry) {
// FIXME: This should be a callback in a CollisionComponent registry.view<PositionComponent, gk::DoubleBox, ItemStack, NetworkComponent, LuaCallbackComponent>().each([&](auto entity, auto &position, auto &box, auto &itemStack, auto &network, auto &luaCallbackComponent) {
registry.view<PositionComponent, gk::DoubleBox, ItemStack, NetworkComponent>().each([&](auto entity, auto &position, auto &box, auto &itemStack, auto &network) {
for (auto &it : m_players) { for (auto &it : m_players) {
if (it.second.dimension() == position.dimension) { if (it.second.dimension() == position.dimension) {
gk::DoubleBox hitbox = box + gk::Vector3d{position.x, position.y, position.z}; gk::DoubleBox hitbox = box + gk::Vector3d{position.x, position.y, position.z};
gk::DoubleBox playerHitbox = it.second.hitbox() + gk::Vector3d{it.second.x(), it.second.y(), it.second.z()}; gk::DoubleBox playerHitbox = it.second.hitbox() + gk::Vector3d{it.second.x(), it.second.y(), it.second.z()};
if (hitbox.intersects(playerHitbox)) { if (hitbox.intersects(playerHitbox)) {
it.second.inventory().addStack(itemStack.item().stringID(), itemStack.amount()); luaCallbackComponent.collisionCallback(itemStack, entity, network.entityID, it.second, registry, *m_server);
m_server->sendPlayerInvUpdate(it.second.clientID(), &it.second.client());
m_server->sendEntityDespawn(network.entityID);
registry.destroy(entity);
} }
} }
} }

View File

@ -1,56 +0,0 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
* 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 "AnimationComponent.hpp"
#include "DrawableComponent.hpp"
#include "DrawableDef.hpp"
#include "ItemDropFactory.hpp"
#include "ItemStack.hpp"
#include "NetworkComponent.hpp"
#include "PositionComponent.hpp"
#include "Registry.hpp"
#include "RotationComponent.hpp"
void ItemDropFactory::create(entt::registry &registry, double x, double y, double z, u16 dimension, const std::string &itemID, u16 amount) {
auto entity = registry.create();
registry.emplace<PositionComponent>(entity, x, y, z, dimension);
registry.emplace<RotationComponent>(entity);
registry.emplace<NetworkComponent>(entity, entity);
auto &drawableDef = registry.emplace<DrawableDef>(entity);
auto &cube = drawableDef.addInventoryCube();
cube.size = 0.25f;
cube.origin = gk::Vector3f{cube.size / 2.f, cube.size / 2.f, cube.size / 2.f};
cube.blockID = itemID;
auto &animationComponent = registry.emplace<AnimationComponent>(entity);
animationComponent.addRotation(0.f, 0.f, 1.f, 0.5f);
animationComponent.addTranslation(0.f, 0.f, -0.0005f, -0.2f, 0.f, true);
registry.emplace<gk::DoubleBox>(entity, 0., 0., 0., cube.size, cube.size, cube.size);
registry.emplace<ItemStack>(entity, itemID, amount);
}

View File

@ -28,7 +28,6 @@
#include "Dimension.hpp" #include "Dimension.hpp"
#include "EngineConfig.hpp" #include "EngineConfig.hpp"
#include "ItemDropFactory.hpp"
#include "Network.hpp" #include "Network.hpp"
#include "Server.hpp" #include "Server.hpp"
#include "ServerCommandHandler.hpp" #include "ServerCommandHandler.hpp"
@ -151,17 +150,6 @@ void ServerWorld::sendRequestedData(ClientInfo &client, int cx, int cy, int cz)
sendChunkData(client, chunk); sendChunkData(client, chunk);
} }
void ServerWorld::onBlockDigged(int x, int y, int z, const Block &block, ServerPlayer &player) {
// FIXME: lua-api-entity
// if (ServerConfig::useItemDrops) {
// ItemDropFactory::create(m_scene.registry(), x + 0.5, y + 0.5, z + 0.5, m_dimension.id(), block.getItemDrop().item().stringID(), block.getItemDrop().amount());
// }
// else {
// player.inventory().addStack(block.getItemDrop().item().stringID(), block.getItemDrop().amount());
// m_server->sendPlayerInvUpdate(player.clientID(), &player.client());
// }
}
ServerChunk &ServerWorld::createChunk(s32 cx, s32 cy, s32 cz) { ServerChunk &ServerWorld::createChunk(s32 cx, s32 cy, s32 cz) {
ServerChunk *chunk = (ServerChunk *)getChunk(cx, cy, cz); ServerChunk *chunk = (ServerChunk *)getChunk(cx, cy, cz);
if (!chunk) { if (!chunk) {

View File

@ -57,8 +57,6 @@ class ServerWorld : public World {
void sendChunkData(const ClientInfo &client, ServerChunk &chunk); void sendChunkData(const ClientInfo &client, ServerChunk &chunk);
void sendRequestedData(ClientInfo &client, s32 cx, s32 cy, s32 cz); void sendRequestedData(ClientInfo &client, s32 cx, s32 cy, s32 cz);
void onBlockDigged(int x, int y, int z, const Block &block, ServerPlayer &player);
ServerChunk &createChunk(s32 cx, s32 cy, s32 cz); ServerChunk &createChunk(s32 cx, s32 cy, s32 cz);
Chunk *getChunk(int cx, int cy, int cz) const override; Chunk *getChunk(int cx, int cy, int cz) const override;