Skip to content

Commit 81986cb

Browse files
committed
[WorldController] Added. It's basically a container for 'ServerWorld' instances.
1 parent 3ef71df commit 81986cb

10 files changed

+150
-35
lines changed

common/source/core/Registry.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class Registry : public ISerializable {
9898
const std::vector<Item> &items() const { return m_items; }
9999
const std::vector<Tree> &trees() const { return m_trees; }
100100
const std::vector<Biome> &biomes() const { return m_biomes; }
101+
const std::vector<Dimension> &dimensions() const { return m_dimensions; }
101102

102103
static Registry &getInstance() { return *s_instance; }
103104
static void setInstance(Registry &instance) { s_instance = &instance; }

server/source/core/ServerApplication.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ void ServerApplication::init() {
4747
m_registry.registerItem({}, "_:air", "Air").setIsBlock(true);
4848

4949
m_scriptEngine.init();
50-
// m_luaCore.setPlayer(m_player); // FIXME
51-
m_luaCore.setWorld(m_world);
5250

5351
try {
5452
m_scriptEngine.lua()["openminer"] = &m_luaCore;
@@ -64,7 +62,8 @@ void ServerApplication::init() {
6462

6563
m_serverCommandHandler.setupCallbacks();
6664

67-
m_world.setServer(&m_serverCommandHandler);
65+
m_worldController.setServer(m_serverCommandHandler);
66+
m_worldController.init();
6867

6968
std::cout << "Server is running on localhost:" << m_port << std::endl;
7069
}
@@ -89,7 +88,7 @@ int ServerApplication::run(bool isProtected) {
8988
}
9089

9190
void ServerApplication::update() {
92-
m_world.update(m_players);
91+
m_worldController.update(m_players);
9392

9493
if (gk::GameClock::getTicks() % 100 < 10) {
9594
for (auto &it : m_players) {

server/source/core/ServerApplication.hpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include "Server.hpp"
3737
#include "ServerCommandHandler.hpp"
3838
#include "ServerPlayer.hpp"
39-
#include "ServerWorld.hpp"
39+
#include "WorldController.hpp"
4040

4141
class ServerApplication {
4242
public:
@@ -56,18 +56,18 @@ class ServerApplication {
5656
gk::ArgumentParser m_argumentParser;
5757
gk::GameClock m_clock;
5858

59-
Server m_server;
60-
ServerWorld m_world;
61-
std::unordered_map<u16, ServerPlayer> m_players;
59+
Registry m_registry;
6260

6361
ScriptEngine m_scriptEngine;
64-
LuaCore m_luaCore;
65-
66-
Registry m_registry;
62+
LuaCore m_luaCore{m_registry};
6763

6864
u16 m_port = 4242;
6965

70-
ServerCommandHandler m_serverCommandHandler{m_scriptEngine, m_server, m_world, m_players, m_registry};
66+
WorldController m_worldController{m_registry};
67+
std::unordered_map<u16, ServerPlayer> m_players;
68+
69+
Server m_server;
70+
ServerCommandHandler m_serverCommandHandler{m_scriptEngine, m_server, m_worldController, m_players, m_registry};
7171
};
7272

7373
#endif // SERVERAPPLICATION_HPP_

server/source/lua/LuaCore.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@
2929
#include "ServerWorld.hpp"
3030

3131
Registry *LuaCore::registry() {
32-
return &Registry::getInstance();
32+
return &m_registry;
3333
}
3434

3535
void LuaCore::initUsertype(sol::state &lua) {
3636
lua.new_usertype<LuaCore>("LuaCore",
37-
"world", &LuaCore::world,
3837
"registry", &LuaCore::registry
3938
);
4039
}

server/source/lua/LuaCore.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,17 @@
3232
#include <sol.hpp>
3333

3434
class Registry;
35-
class ServerWorld;
3635

3736
class LuaCore {
3837
public:
39-
ServerWorld *world() { return m_world; }
40-
void setWorld(ServerWorld &world) { m_world = &world; }
38+
LuaCore(Registry &registry) : m_registry(registry) {}
4139

4240
Registry *registry();
4341

4442
static void initUsertype(sol::state &lua);
4543

4644
private:
47-
ServerWorld *m_world = nullptr;
45+
Registry &m_registry;
4846
};
4947

5048
#endif // LUACORE_HPP_

server/source/network/ServerCommandHandler.cpp

+25-13
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
#include "Server.hpp"
3131
#include "ServerBlock.hpp"
3232
#include "ServerPlayer.hpp"
33-
#include "ServerWorld.hpp"
3433
#include "ServerCommandHandler.hpp"
34+
#include "WorldController.hpp"
3535

3636
void ServerCommandHandler::sendBlockDataUpdate(s32 x, s32 y, s32 z, const BlockData *blockData, const ClientInfo *client) const {
3737
sf::Packet packet;
@@ -125,7 +125,7 @@ void ServerCommandHandler::setupCallbacks() {
125125
s32 cx, cy, cz;
126126
packet >> cx >> cy >> cz;
127127

128-
m_world.sendRequestedData(client, cx, cy, cz);
128+
getWorldForClient(client.id).sendRequestedData(client, cx, cy, cz);
129129
});
130130

131131
m_server.setCommandCallback(Network::Command::PlayerInvUpdate, [this](ClientInfo &client, sf::Packet &packet) {
@@ -146,22 +146,24 @@ void ServerCommandHandler::setupCallbacks() {
146146
m_players.at(client.id).setPosition(x, y, z);
147147
});
148148

149-
m_server.setCommandCallback(Network::Command::PlayerPlaceBlock, [this](ClientInfo &, sf::Packet &packet) {
149+
m_server.setCommandCallback(Network::Command::PlayerPlaceBlock, [this](ClientInfo &client, sf::Packet &packet) {
150150
s32 x, y, z;
151151
u32 block;
152152
packet >> x >> y >> z >> block;
153-
m_world.setBlock(x, y, z, block & 0xffff);
154-
m_world.setData(x, y, z, block >> 16);
153+
154+
ServerWorld &world = getWorldForClient(client.id);
155+
world.setBlock(x, y, z, block & 0xffff);
156+
world.setData(x, y, z, block >> 16);
155157

156158
sf::Packet answer;
157159
answer << Network::Command::BlockUpdate << x << y << z << block;
158160
m_server.sendToAllClients(answer);
159161
});
160162

161-
m_server.setCommandCallback(Network::Command::PlayerDigBlock, [this](ClientInfo &, sf::Packet &packet) {
163+
m_server.setCommandCallback(Network::Command::PlayerDigBlock, [this](ClientInfo &client, sf::Packet &packet) {
162164
s32 x, y, z;
163165
packet >> x >> y >> z;
164-
m_world.setBlock(x, y, z, 0);
166+
getWorldForClient(client.id).setBlock(x, y, z, 0);
165167

166168
sf::Packet answer;
167169
answer << Network::Command::BlockUpdate << x << y << z << u32(0);
@@ -204,26 +206,28 @@ void ServerCommandHandler::setupCallbacks() {
204206
u8 guiScale;
205207
packet >> x >> y >> z >> screenWidth >> screenHeight >> guiScale;
206208

207-
u16 id = m_world.getBlock(x, y, z);
208-
((ServerBlock &)(m_registry.getBlock(id))).onBlockActivated({x, y, z}, m_players.at(client.id), m_world, client, screenWidth, screenHeight, guiScale);
209+
ServerWorld &world = getWorldForClient(client.id);
210+
211+
u16 id = world.getBlock(x, y, z);
212+
((ServerBlock &)(m_registry.getBlock(id))).onBlockActivated({x, y, z}, m_players.at(client.id), world, client, screenWidth, screenHeight, guiScale);
209213
});
210214

211-
m_server.setCommandCallback(Network::Command::BlockInvUpdate, [this](ClientInfo &, sf::Packet &packet) {
215+
m_server.setCommandCallback(Network::Command::BlockInvUpdate, [this](ClientInfo &client, sf::Packet &packet) {
212216
gk::Vector3<s32> pos;
213217
packet >> pos.x >> pos.y >> pos.z;
214218

215-
BlockData *data = m_world.getBlockData(pos.x, pos.y, pos.z);
219+
BlockData *data = getWorldForClient(client.id).getBlockData(pos.x, pos.y, pos.z);
216220
if (data)
217221
packet >> data->inventory;
218222
else
219223
DEBUG("BlockInvUpdate: No block data found at", pos.x, pos.y, pos.z);
220224
});
221225

222-
m_server.setCommandCallback(Network::Command::BlockDataUpdate, [this](ClientInfo &, sf::Packet &packet) {
226+
m_server.setCommandCallback(Network::Command::BlockDataUpdate, [this](ClientInfo &client, sf::Packet &packet) {
223227
gk::Vector3<s32> pos;
224228
packet >> pos.x >> pos.y >> pos.z;
225229

226-
BlockData *data = m_world.getBlockData(pos.x, pos.y, pos.z);
230+
BlockData *data = getWorldForClient(client.id).getBlockData(pos.x, pos.y, pos.z);
227231
if (data) {
228232
packet >> data->meta >> data->useAltTiles;
229233
}
@@ -278,3 +282,11 @@ void ServerCommandHandler::setupCallbacks() {
278282
});
279283
}
280284

285+
inline ServerWorld &ServerCommandHandler::getWorldForClient(u16 clientID) {
286+
auto it = m_players.find(clientID);
287+
if (it == m_players.end())
288+
throw EXCEPTION("Player instance not found for client", clientID);
289+
290+
return m_worldController.getWorld(it->second.dimension());
291+
}
292+

server/source/network/ServerCommandHandler.hpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ class ScriptEngine;
4040
class Server;
4141
class ServerPlayer;
4242
class ServerWorld;
43+
class WorldController;
4344

4445
class ServerCommandHandler {
4546
public:
46-
ServerCommandHandler(ScriptEngine &scriptEngine, Server &server, ServerWorld &world, std::unordered_map<u16, ServerPlayer> &players, Registry &registry)
47-
: m_scriptEngine(scriptEngine), m_server(server), m_world(world), m_players(players), m_registry(registry) {}
47+
ServerCommandHandler(ScriptEngine &scriptEngine, Server &server, WorldController &worldController, std::unordered_map<u16, ServerPlayer> &players, Registry &registry)
48+
: m_scriptEngine(scriptEngine), m_server(server), m_worldController(worldController), m_players(players), m_registry(registry) {}
4849

4950
void sendBlockDataUpdate(s32 x, s32 y, s32 z, const BlockData *blockData, const ClientInfo *client = nullptr) const;
5051
void sendBlockInvUpdate(s32 x, s32 y, s32 z, const Inventory &inventory, const ClientInfo *client = nullptr) const;
@@ -56,10 +57,12 @@ class ServerCommandHandler {
5657
const Server &server() const { return m_server; }
5758

5859
private:
60+
ServerWorld &getWorldForClient(u16 clientID);
61+
5962
ScriptEngine &m_scriptEngine;
6063

6164
Server &m_server;
62-
ServerWorld &m_world;
65+
WorldController &m_worldController;
6366
std::unordered_map<u16, ServerPlayer> &m_players;
6467

6568
Registry &m_registry;

server/source/world/ServerWorld.hpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@
3434
#include "World.hpp"
3535

3636
class ClientInfo;
37+
class Dimension;
3738
class ServerCommandHandler;
3839
class ServerPlayer;
3940

4041
class ServerWorld : public World {
4142
using ChunkMap = std::unordered_map<gk::Vector3i, std::unique_ptr<ServerChunk>>;
4243

4344
public:
44-
ServerWorld() = default;
45+
ServerWorld(const Dimension &dimension) : m_dimension(dimension) {}
4546

4647
void update(std::unordered_map<u16, ServerPlayer> &players);
4748

@@ -51,11 +52,15 @@ class ServerWorld : public World {
5152

5253
Chunk *getChunk(int cx, int cy, int cz) const override;
5354

55+
const Dimension &dimension() const { return m_dimension; }
56+
5457
TerrainGenerator &terrainGenerator() { return m_terrainGenerator; }
5558

5659
void setServer(ServerCommandHandler *server) { m_server = server; }
5760

5861
private:
62+
const Dimension &m_dimension;
63+
5964
ChunkMap m_chunks;
6065

6166
u32 m_lastTick = 0;
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* =====================================================================================
3+
*
4+
* OpenMiner
5+
*
6+
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
7+
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
8+
*
9+
* This file is part of OpenMiner.
10+
*
11+
* OpenMiner is free software; you can redistribute it and/or
12+
* modify it under the terms of the GNU Lesser General Public
13+
* License as published by the Free Software Foundation; either
14+
* version 2.1 of the License, or (at your option) any later version.
15+
*
16+
* OpenMiner is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+
* Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
23+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24+
*
25+
* =====================================================================================
26+
*/
27+
#include "Registry.hpp"
28+
#include "WorldController.hpp"
29+
30+
void WorldController::init() {
31+
for (const Dimension &dimension : m_registry.dimensions()) {
32+
m_worldList.emplace_back(dimension);
33+
m_worldList.back().setServer(m_server);
34+
}
35+
}
36+
37+
void WorldController::update(std::unordered_map<u16, ServerPlayer> &players) {
38+
for (auto &it : m_worldList)
39+
it.update(players);
40+
}
41+
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* =====================================================================================
3+
*
4+
* OpenMiner
5+
*
6+
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
7+
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
8+
*
9+
* This file is part of OpenMiner.
10+
*
11+
* OpenMiner is free software; you can redistribute it and/or
12+
* modify it under the terms of the GNU Lesser General Public
13+
* License as published by the Free Software Foundation; either
14+
* version 2.1 of the License, or (at your option) any later version.
15+
*
16+
* OpenMiner is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+
* Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
23+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24+
*
25+
* =====================================================================================
26+
*/
27+
#ifndef WORLDCONTROLLER_HPP_
28+
#define WORLDCONTROLLER_HPP_
29+
30+
#include <deque>
31+
32+
#include "ServerWorld.hpp"
33+
34+
class Registry;
35+
36+
class WorldController {
37+
public:
38+
WorldController(Registry &registry)
39+
: m_registry(registry) {}
40+
41+
void init();
42+
43+
void update(std::unordered_map<u16, ServerPlayer> &players);
44+
45+
ServerWorld &getWorld(u16 dimension) { return m_worldList.at(dimension); }
46+
47+
void setServer(ServerCommandHandler &server) { m_server = &server; }
48+
49+
private:
50+
std::vector<ServerWorld> m_worldList;
51+
52+
Registry &m_registry;
53+
54+
ServerCommandHandler *m_server = nullptr;
55+
};
56+
57+
#endif // WORLDCONTROLLER_HPP_

0 commit comments

Comments
 (0)