Skip to content

Commit

Permalink
[ServerConfig] Added for option 'useItemDrops'.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Apr 29, 2020
1 parent 9b9ee6a commit 91b3792
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ openminer_server
*.zip
test_atlas.png
config.lua
server_config.lua
TODO

2 changes: 1 addition & 1 deletion config.example.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- You can copy this file as 'config.lua' to load automatically those settings at client startup
-- Here the default values are set, you can remove options or change them accordingly
-- See client/source/Config.cpp for more details
-- See source/client/core/Config.cpp for more details

-- Gameplay
isFlyModeEnabled = false
Expand Down
7 changes: 7 additions & 0 deletions server_config.example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- You can copy this file as 'server_config.lua' to load automatically those settings at server startup
-- Here the default values are set, you can remove options or change them accordingly
-- See source/server/core/ServerConfig.cpp for more details

-- Gameplay
useItemDrops = true

1 change: 0 additions & 1 deletion source/client/hud/BlockCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ void BlockCursor::update(const Hotbar &hotbar) {
m_animationStart = ticks;

m_client.sendPlayerDigBlock(m_selectedBlock);
m_client.sendPlayerInvUpdate();
}
}
}
Expand Down
1 change: 1 addition & 0 deletions source/client/network/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <functional>
#include <unordered_map>

#include <SFML/Network/IpAddress.hpp>
#include <SFML/Network/TcpSocket.hpp>
#include <SFML/Network/UdpSocket.hpp>

Expand Down
1 change: 0 additions & 1 deletion source/common/world/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ void Chunk::setBlock(int x, int y, int z, u16 type) {
if (m_data[z][y][x] != 0) {
const Block &oldBlock = Registry::getInstance().getBlock(m_data[z][y][x]);
onBlockDestroyed(x, y, z, oldBlock);
m_world.onBlockDestroyed(x + m_x * width, y + m_y * depth, z + m_z * height, oldBlock);
}

setBlockRaw(x, y, z, type);
Expand Down
1 change: 0 additions & 1 deletion source/common/world/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class World {
void setData(int x, int y, int z, u16 data) const;

virtual void onBlockPlaced(int, int, int, const Block &) {}
virtual void onBlockDestroyed(int, int, int, const Block &) {}

static bool isReloadRequested;
};
Expand Down
3 changes: 3 additions & 0 deletions source/server/core/ServerApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "BlockGeometry.hpp"
#include "ServerApplication.hpp"
#include "ServerBlock.hpp"
#include "ServerConfig.hpp"

namespace fs = ghc::filesystem;

Expand Down Expand Up @@ -58,6 +59,8 @@ void ServerApplication::init() {
if (m_argumentParser.getArgument("port").isFound)
m_port = std::stoi(m_argumentParser.getArgument("port").parameter);

ServerConfig::loadConfigFromFile("server_config.lua");

m_server.init(m_port);
m_server.setRunning(true);

Expand Down
53 changes: 53 additions & 0 deletions source/server/core/ServerConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* 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 "ServerConfig.hpp"

// Gameplay
bool ServerConfig::useItemDrops = true;

#include <gk/core/Debug.hpp>
#include <gk/core/Filesystem.hpp>

#include <sol.hpp>

void ServerConfig::loadConfigFromFile(const char *file) {
if (gk::Filesystem::fileExists(file)) {
sol::state lua;

try {
lua.safe_script_file(file);

useItemDrops = lua["useItemDrops"].get_or(useItemDrops);

gkInfo() << "Config file loaded successfully";
}
catch (sol::error &e) {
gkError() << e.what();
}
}
}

37 changes: 37 additions & 0 deletions source/server/core/ServerConfig.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* 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
*
* =====================================================================================
*/
#ifndef SERVERCONFIG_HPP_
#define SERVERCONFIG_HPP_

namespace ServerConfig {
// Gameplay
extern bool useItemDrops;

void loadConfigFromFile(const char *file);
}

#endif // SERVERCONFIG_HPP_
22 changes: 15 additions & 7 deletions source/server/network/ServerCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void ServerCommandHandler::sendEntityAnimation(u32 entityID, const AnimationComp
client->tcpSocket->send(packet);
}

void ServerCommandHandler::sendEntityDrawableDef(u32 entityID, DrawableDef &drawableDef, const ClientInfo *client) const {
void ServerCommandHandler::sendEntityDrawableDef(u32 entityID, const DrawableDef &drawableDef, const ClientInfo *client) const {
sf::Packet packet;
packet << Network::Command::EntityDrawableDef << entityID << drawableDef;

Expand Down Expand Up @@ -270,13 +270,21 @@ void ServerCommandHandler::setupCallbacks() {
});

m_server.setCommandCallback(Network::Command::PlayerDigBlock, [this](ClientInfo &client, sf::Packet &packet) {
s32 x, y, z;
packet >> x >> y >> z;
getWorldForClient(client.id).setBlock(x, y, z, 0);
ServerPlayer *player = m_players.getPlayer(client.id);
if (player) {
s32 x, y, z;
packet >> x >> y >> z;

ServerWorld &world = getWorldForClient(client.id);
world.onBlockDigged(x, y, z, Registry::getInstance().getBlock(world.getBlock(x, y, z)), *player);
world.setBlock(x, y, z, 0);

sf::Packet answer;
answer << Network::Command::BlockUpdate << x << y << z << u32(0);
m_server.sendToAllClients(answer);
sf::Packet answer;
answer << Network::Command::BlockUpdate << x << y << z << u32(0);
m_server.sendToAllClients(answer);
}
else
gkError() << ("Failed to dig block using player " + std::to_string(client.id) + ": Player not found").c_str();
});

m_server.setCommandCallback(Network::Command::PlayerInventory, [this](ClientInfo &client, sf::Packet &packet) {
Expand Down
2 changes: 1 addition & 1 deletion source/server/network/ServerCommandHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ServerCommandHandler {
void sendEntityPosition(u32 entityID, double x, double y, double z, const ClientInfo *client = nullptr) const;
void sendEntityRotation(u32 entityID, float w, float x, float y, float z, const ClientInfo *client = nullptr) const;
void sendEntityAnimation(u32 entityID, const AnimationComponent &animation, const ClientInfo *client = nullptr) const;
void sendEntityDrawableDef(u32 entityID, DrawableDef &drawableDef, const ClientInfo *client = nullptr) const;
void sendEntityDrawableDef(u32 entityID, const DrawableDef &drawableDef, const ClientInfo *client = nullptr) const;

void setupCallbacks();

Expand Down
18 changes: 12 additions & 6 deletions source/server/world/ServerWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@
*/
#include <gk/core/GameClock.hpp>

#include "Dimension.hpp"
#include "EngineConfig.hpp"
#include "ItemDropFactory.hpp"
#include "Network.hpp"
#include "Server.hpp"
#include "ServerCommandHandler.hpp"
#include "ServerConfig.hpp"
#include "ServerPlayer.hpp"
#include "ServerWorld.hpp"

#include "Dimension.hpp" // FIXME

void ServerWorld::update() {
if (m_lastTick < m_clock.getTicks() / 50) {
m_lastTick = m_clock.getTicks() / 50;
Expand Down Expand Up @@ -150,10 +151,15 @@ void ServerWorld::sendRequestedData(ClientInfo &client, int cx, int cy, int cz)
sendChunkData(client, chunk);
}

#include "ItemDropFactory.hpp" // FIXME

void ServerWorld::onBlockDestroyed(int x, int y, int z, const Block &block) {
ItemDropFactory::create(m_scene.registry(), x + 0.5, y + 0.5, z + 0.5, block.getItemDrop().item().stringID(), block.getItemDrop().amount());
void ServerWorld::onBlockDigged(int x, int y, int z, const Block &block, ServerPlayer &player) {
if (ServerConfig::useItemDrops) {
ItemDropFactory::create(m_scene.registry(), x + 0.5, y + 0.5, z + 0.5, block.getItemDrop().item().stringID(), block.getItemDrop().amount());
}
else {
player.inventory().addStack(block.getItemDrop().item().stringID(), block.getItemDrop().amount());
// gkDebug() << player.inventory().getStack(2, 2).item().stringID() << player.inventory().getStack(2, 2).amount();
m_server->sendPlayerInvUpdate(player.clientID(), &player.client());
}
}

ServerChunk &ServerWorld::createChunk(s32 cx, s32 cy, s32 cz) {
Expand Down
2 changes: 1 addition & 1 deletion source/server/world/ServerWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ServerWorld : public World {
void sendChunkData(const ClientInfo &client, ServerChunk &chunk);
void sendRequestedData(ClientInfo &client, s32 cx, s32 cy, s32 cz);

void onBlockDestroyed(int x, int y, int z, const Block &block) override;
void onBlockDigged(int x, int y, int z, const Block &block, ServerPlayer &player);

ServerChunk &createChunk(s32 cx, s32 cy, s32 cz);

Expand Down

0 comments on commit 91b3792

Please sign in to comment.