diff --git a/TODO b/TODO index bef8eac7..f73925ab 100644 --- a/TODO +++ b/TODO @@ -14,8 +14,8 @@ TODO • TODO: GUI scale issues ◦ TODO: `HUD` doesn’t update when GUI scale is changed ◦ TODO: `SettingsMenuState` should update scaling when the setting is changed - ◦ TODO: All Lua-defined GUI are not scaled correctly with lower GUI scale settings - ◦ TODO: Send client screen size and GUI scale to server + ◦ DONE: All Lua-defined GUI are not scaled correctly with lower GUI scale settings + ◦ DONE: Send client screen size and GUI scale to server • TODO: Blocks can be accessed from outside the world (will need a refactoring) • TODO: Collisions are fucked up with blocks placed at `x = -1` from `x = 0` • TODO: Blocks can be placed inside the player (check for AABB?) diff --git a/client/source/network/ClientCommandHandler.cpp b/client/source/network/ClientCommandHandler.cpp index b627a484..b5f48ca8 100644 --- a/client/source/network/ClientCommandHandler.cpp +++ b/client/source/network/ClientCommandHandler.cpp @@ -67,7 +67,8 @@ void ClientCommandHandler::sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block) void ClientCommandHandler::sendPlayerInventoryRequest() { sf::Packet packet; - packet << Network::Command::PlayerInventory; + packet << Network::Command::PlayerInventory + << u16(SCREEN_WIDTH) << u16(SCREEN_HEIGHT) << u8(GUI_SCALE); m_client.send(packet); } @@ -76,7 +77,8 @@ void ClientCommandHandler::sendBlockActivated(const glm::vec4 &selectedBlock) { packet << Network::Command::BlockActivated << s32(selectedBlock.x) << s32(selectedBlock.y) - << s32(selectedBlock.z); + << s32(selectedBlock.z) + << u16(SCREEN_WIDTH) << u16(SCREEN_HEIGHT) << u8(GUI_SCALE); m_client.send(packet); } diff --git a/common/include/network/Network.hpp b/common/include/network/Network.hpp index 15f2cc33..28062561 100644 --- a/common/include/network/Network.hpp +++ b/common/include/network/Network.hpp @@ -28,35 +28,35 @@ namespace Network { enum class Command { // Client commands - ClientConnect, // [NetworkCommand][u16 udp port] (from Client only) - ClientDisconnect, // [NetworkCommand] (from Client only) - ClientOk, // [NetworkCommand][u16 client id] (from Server only) - ClientRefused, // [NetworkCommand] (from Server only) + ClientConnect = 0, // [NetworkCommand][u16 udp port] (from Client only) + ClientDisconnect = 1, // [NetworkCommand] (from Client only) + ClientOk = 2, // [NetworkCommand][u16 client id] (from Server only) + ClientRefused = 3, // [NetworkCommand] (from Server only) // Input commands - KeyState, // [NetworkCommand][u32 timestamp][u16 client id][u32 keycode][bool isPressed]... + KeyState = 4, // [NetworkCommand][u32 timestamp][u16 client id][u32 keycode][bool isPressed]... // Chunk commands - ChunkData, // [NetworkCommand][s32 cx, cy, cz][u32...] (from Server only) - ChunkRequest, // [NetworkCommand][s32 cx, cy, cz] (from Client only) + ChunkData = 5, // [NetworkCommand][s32 cx, cy, cz][u32...] (from Server only) + ChunkRequest = 6, // [NetworkCommand][s32 cx, cy, cz] (from Client only) // Player commands - PlayerPlaceBlock, // [NetworkCommand][s32 x, y, z][u32 block] (from Client only) - PlayerDigBlock, // [NetworkCommand][s32 x, y, z] (from Client only) - PlayerInvUpdate, // [NetworkCommand][u16 client id][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME] - PlayerPosUpdate, // [NetworkCommand][u16 client id][s32 x, y, z] (both) // FIXME - PlayerSpawn, // [NetworkCommand][u16 client id][s32 x, y, z] (from Server only) - PlayerInventory, // [NetworkCommand] (from Client only) + PlayerPlaceBlock = 7, // [NetworkCommand][s32 x, y, z][u32 block] (from Client only) + PlayerDigBlock = 8, // [NetworkCommand][s32 x, y, z] (from Client only) + PlayerInvUpdate = 9, // [NetworkCommand][u16 client id][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME] + PlayerPosUpdate = 10, // [NetworkCommand][u16 client id][s32 x, y, z] (both) // FIXME + PlayerSpawn = 11, // [NetworkCommand][u16 client id][s32 x, y, z] (from Server only) + PlayerInventory = 12, // [NetworkCommand][u16 screenWidth, screenHeight][u8 guiScale] (from Client only) // Block commands - BlockUpdate, // [NetworkCommand][s32 x, y, z][u32 block] (from Server only) - BlockActivated, // [NetworkCommand][s32 x, y, z] (from Client only) - BlockGUIData, // [NetworkCommand][LuaGUIData data] (from Server only) - BlockInvUpdate, // [NetworkCommand][s32 x, y, z][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME] - BlockDataUpdate, // [NetworkCommand][s32 x, y, z][u64 data] (both) [FIXME] + BlockUpdate = 13, // [NetworkCommand][s32 x, y, z][u32 block] (from Server only) + BlockActivated = 14, // [NetworkCommand][s32 x, y, z][u16 screenWidth, screenHeight][u8 guiScale] (from Client only) + BlockGUIData = 15, // [NetworkCommand][LuaGUIData data] (from Server only) + BlockInvUpdate = 16, // [NetworkCommand][s32 x, y, z][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME] + BlockDataUpdate = 17, // [NetworkCommand][s32 x, y, z][u64 data] (both) [FIXME] // Registry commands - RegistryData, // [NetworkCommand][Block block] (from Server only) + RegistryData = 18, // [NetworkCommand][Block block] (from Server only) }; std::string commandToString(Command command); diff --git a/mods/default/furnace.lua b/mods/default/furnace.lua index 9e5bdf16..8f7b0f7f 100644 --- a/mods/default/furnace.lua +++ b/mods/default/furnace.lua @@ -31,13 +31,13 @@ mod:block { world:add_block_data(pos.x, pos.y, pos.z, 3, 1) end, - on_block_activated = function(pos, player, world, client) + on_block_activated = function(pos, player, world, client, screen_width, screen_height, gui_scale) local gui = LuaGUI.new() -- FIXME: Replace this by gui:set_size() and gui:set_centered() local gui_pos = { - x = gui.SCREEN_WIDTH / gui.GUI_SCALE / 2.0 - 176 / 2.0, - y = gui.SCREEN_HEIGHT / gui.GUI_SCALE / 2.0 - 166 / 2.0 + x = screen_width / gui_scale / 2.0 - 176 / 2.0, + y = screen_height / gui_scale / 2.0 - 166 / 2.0 } gui:furnace { diff --git a/mods/default/init.lua b/mods/default/init.lua index f23991c9..0eacdb8a 100644 --- a/mods/default/init.lua +++ b/mods/default/init.lua @@ -60,13 +60,13 @@ function init(player) player_inv:add_stack("default:coal", 64); end -function show_inventory(client) +function show_inventory(client, screen_width, screen_height, gui_scale) local gui = LuaGUI.new() -- FIXME: Replace this by gui:set_size() and gui:set_centered() local gui_pos = { - x = gui.SCREEN_WIDTH / gui.GUI_SCALE / 2.0 - 176 / 2.0, - y = gui.SCREEN_HEIGHT / gui.GUI_SCALE / 2.0 - 166 / 2.0 + x = screen_width / gui_scale / 2.0 - 176 / 2.0, + y = screen_height / gui_scale / 2.0 - 166 / 2.0 } gui:player_inventory { diff --git a/mods/default/workbench.lua b/mods/default/workbench.lua index 177ad6be..792830ba 100644 --- a/mods/default/workbench.lua +++ b/mods/default/workbench.lua @@ -30,13 +30,13 @@ mod:block { world:add_block_data(pos.x, pos.y, pos.z, 3, 3) end, - on_block_activated = function(pos, player, world, client) + on_block_activated = function(pos, player, world, client, screen_width, screen_height, gui_scale) local gui = LuaGUI.new() -- FIXME: Replace this by gui:set_size() and gui:set_centered() local gui_pos = { - x = gui.SCREEN_WIDTH / gui.GUI_SCALE / 2.0 - 176 / 2.0, - y = gui.SCREEN_HEIGHT / gui.GUI_SCALE / 2.0 - 166 / 2.0 + x = screen_width / gui_scale / 2.0 - 176 / 2.0, + y = screen_height / gui_scale / 2.0 - 166 / 2.0 } gui:button { diff --git a/server/include/world/ServerBlock.hpp b/server/include/world/ServerBlock.hpp index 83a94d62..42c27297 100644 --- a/server/include/world/ServerBlock.hpp +++ b/server/include/world/ServerBlock.hpp @@ -35,7 +35,7 @@ class ServerBlock : public Block { : Block(id, tiles, name, label) {} void onTick(const glm::ivec3 &, std::unordered_map &, Chunk &, World &, ServerCommandHandler &) const; - bool onBlockActivated(const glm::ivec3 &pos, Player &player, World &world, Client &client) const; + bool onBlockActivated(const glm::ivec3 &pos, Player &player, World &world, Client &client, u16 screenWidth, u16 screenHeight, u8 guiScale) const; void onBlockPlaced(const glm::ivec3 &pos, World &world) const; bool canUpdate() const { return m_onTick.valid(); } diff --git a/server/source/lua/LuaGUI.cpp b/server/source/lua/LuaGUI.cpp index f42216ed..a2594d90 100644 --- a/server/source/lua/LuaGUI.cpp +++ b/server/source/lua/LuaGUI.cpp @@ -213,15 +213,8 @@ void LuaGUI::show(Client &client) { client.tcpSocket->send(packet); } -#include "Config.hpp" // FIXME - void LuaGUI::initUsertype(sol::state &lua) { lua.new_usertype("LuaGUI", - // FIXME: These variables should be renamed and put somewhere else - "SCREEN_WIDTH", sol::var(std::ref(SCREEN_WIDTH)), - "SCREEN_HEIGHT", sol::var(std::ref(SCREEN_HEIGHT)), - "GUI_SCALE", sol::var(std::ref(GUI_SCALE)), - "image", &LuaGUI::addImage, "button", &LuaGUI::addTextButton, "inventory", &LuaGUI::addInventoryWidget, diff --git a/server/source/network/ServerCommandHandler.cpp b/server/source/network/ServerCommandHandler.cpp index 045c24e9..5e76c1ae 100644 --- a/server/source/network/ServerCommandHandler.cpp +++ b/server/source/network/ServerCommandHandler.cpp @@ -146,16 +146,22 @@ void ServerCommandHandler::setupCallbacks() { m_server.sendToAllClients(answer); }); - m_server.setCommandCallback(Network::Command::PlayerInventory, [this](Client &client, sf::Packet &) { - m_scriptEngine.lua()["show_inventory"](client); + m_server.setCommandCallback(Network::Command::PlayerInventory, [this](Client &client, sf::Packet &packet) { + u16 screenWidth, screenHeight; + u8 guiScale; + packet >> screenWidth >> screenHeight >> guiScale; + + m_scriptEngine.lua()["show_inventory"](client, screenWidth, screenHeight, guiScale); }); m_server.setCommandCallback(Network::Command::BlockActivated, [this](Client &client, sf::Packet &packet) { s32 x, y, z; - packet >> x >> y >> z; + u16 screenWidth, screenHeight; + u8 guiScale; + packet >> x >> y >> z >> screenWidth >> screenHeight >> guiScale; u16 id = m_world.getBlock(x, y, z); - ((ServerBlock &)(m_registry.getBlock(id))).onBlockActivated({x, y, z}, m_players.at(client.id), m_world, client); + ((ServerBlock &)(m_registry.getBlock(id))).onBlockActivated({x, y, z}, m_players.at(client.id), m_world, client, screenWidth, screenHeight, guiScale); }); m_server.setCommandCallback(Network::Command::BlockInvUpdate, [this](Client &, sf::Packet &packet) { diff --git a/server/source/world/ServerBlock.cpp b/server/source/world/ServerBlock.cpp index b563e2ef..282b07ba 100644 --- a/server/source/world/ServerBlock.cpp +++ b/server/source/world/ServerBlock.cpp @@ -51,10 +51,10 @@ void ServerBlock::onTick(const glm::ivec3 &pos, std::unordered_map