[Network] PlayerInventory and BlockActivated packets now send screen size and gui scale informations.

This commit is contained in:
Quentin Bazin 2020-02-15 14:42:29 +09:00
parent 857c7e4060
commit 71587c0da8
10 changed files with 47 additions and 46 deletions

4
TODO
View File

@ -14,8 +14,8 @@ TODO
• TODO: GUI scale issues
◦ TODO: `HUD` doesnt 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?)

View File

@ -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);
}

View File

@ -28,35 +28,35 @@
namespace Network {
enum class Command {
// Client commands
ClientConnect, // <TCP> [NetworkCommand][u16 udp port] (from Client only)
ClientDisconnect, // <TCP> [NetworkCommand] (from Client only)
ClientOk, // <TCP> [NetworkCommand][u16 client id] (from Server only)
ClientRefused, // <TCP> [NetworkCommand] (from Server only)
ClientConnect = 0, // <TCP> [NetworkCommand][u16 udp port] (from Client only)
ClientDisconnect = 1, // <TCP> [NetworkCommand] (from Client only)
ClientOk = 2, // <TCP> [NetworkCommand][u16 client id] (from Server only)
ClientRefused = 3, // <TCP> [NetworkCommand] (from Server only)
// Input commands
KeyState, // <UDP> [NetworkCommand][u32 timestamp][u16 client id][u32 keycode][bool isPressed]...
KeyState = 4, // <UDP> [NetworkCommand][u32 timestamp][u16 client id][u32 keycode][bool isPressed]...
// Chunk commands
ChunkData, // <TCP> [NetworkCommand][s32 cx, cy, cz][u32...] (from Server only)
ChunkRequest, // <TCP> [NetworkCommand][s32 cx, cy, cz] (from Client only)
ChunkData = 5, // <TCP> [NetworkCommand][s32 cx, cy, cz][u32...] (from Server only)
ChunkRequest = 6, // <TCP> [NetworkCommand][s32 cx, cy, cz] (from Client only)
// Player commands
PlayerPlaceBlock, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Client only)
PlayerDigBlock, // <TCP> [NetworkCommand][s32 x, y, z] (from Client only)
PlayerInvUpdate, // <TCP> [NetworkCommand][u16 client id][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
PlayerPosUpdate, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (both) // FIXME
PlayerSpawn, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (from Server only)
PlayerInventory, // <TCP> [NetworkCommand] (from Client only)
PlayerPlaceBlock = 7, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Client only)
PlayerDigBlock = 8, // <TCP> [NetworkCommand][s32 x, y, z] (from Client only)
PlayerInvUpdate = 9, // <TCP> [NetworkCommand][u16 client id][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
PlayerPosUpdate = 10, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (both) // FIXME
PlayerSpawn = 11, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (from Server only)
PlayerInventory = 12, // <TCP> [NetworkCommand][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
// Block commands
BlockUpdate, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Server only)
BlockActivated, // <TCP> [NetworkCommand][s32 x, y, z] (from Client only)
BlockGUIData, // <TCP> [NetworkCommand][LuaGUIData data] (from Server only)
BlockInvUpdate, // <TCP> [NetworkCommand][s32 x, y, z][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
BlockDataUpdate, // <TCP> [NetworkCommand][s32 x, y, z][u64 data] (both) [FIXME]
BlockUpdate = 13, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Server only)
BlockActivated = 14, // <TCP> [NetworkCommand][s32 x, y, z][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
BlockGUIData = 15, // <TCP> [NetworkCommand][LuaGUIData data] (from Server only)
BlockInvUpdate = 16, // <TCP> [NetworkCommand][s32 x, y, z][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
BlockDataUpdate = 17, // <TCP> [NetworkCommand][s32 x, y, z][u64 data] (both) [FIXME]
// Registry commands
RegistryData, // <TCP> [NetworkCommand][Block block] (from Server only)
RegistryData = 18, // <TCP> [NetworkCommand][Block block] (from Server only)
};
std::string commandToString(Command command);

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -35,7 +35,7 @@ class ServerBlock : public Block {
: Block(id, tiles, name, label) {}
void onTick(const glm::ivec3 &, std::unordered_map<u16, ServerPlayer> &, 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(); }

View File

@ -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>("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,

View File

@ -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) {

View File

@ -51,10 +51,10 @@ void ServerBlock::onTick(const glm::ivec3 &pos, std::unordered_map<u16, ServerPl
}
}
bool ServerBlock::onBlockActivated(const glm::ivec3 &pos, Player &player, World &world, Client &client) const {
bool ServerBlock::onBlockActivated(const glm::ivec3 &pos, Player &player, World &world, Client &client, u16 screenWidth, u16 screenHeight, u8 guiScale) const {
try {
if (m_onBlockActivated) {
m_onBlockActivated(pos, player, world, client);
m_onBlockActivated(pos, player, world, client, screenWidth, screenHeight, guiScale);
return true;
}
}