Skip to content

Commit 07f86b9

Browse files
committed
[GameTime] 'getCurrentTime' now uses the new tick system.
[DebugOverlay] Added client TPS info. [Network] Packet 'ServerTick' added. Sent at the beginning of every tick.
1 parent 556941f commit 07f86b9

File tree

11 files changed

+60
-30
lines changed

11 files changed

+60
-30
lines changed

docs/network-protocol.md

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ _This packet has no field._
4747

4848
### Clientbound
4949

50+
#### ServerTick
51+
52+
Packet sent at the beginning of every server tick.
53+
54+
_This packet has no field._
55+
5056
#### ServerClosed
5157

5258
| Field name | Field type | Notes |

source/client/graphics/Skybox.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ Skybox::Skybox(gk::Camera &camera, ClientWorld &world) : m_camera(camera), m_wor
4949
auto &star = m_stars.emplace_back();
5050
star.setColor(gk::Color{0, 0, 0, 0});
5151
star.setSize(5, 5);
52-
star.setPosition(700 * ((rand() % 2) * 2 - 1), (rand() % 500) * 2 - 500, (rand() % 500) * 2 - 500);
53-
star.setRotationOffset(rand() % 360);
54-
star.setRotationSpeed(0.4f);
52+
star.setPosition(600 * ((rand() % 2) * 2 - 1), (rand() % 500) * 2 - 500, (rand() % 500) * 2 - 500);
53+
star.setRotationOffset(rand() % GameTime::dayLength);
5554
star.setRotationAxis({rand() % 100 / 100.f, rand() % 100 / 100.f, rand() % 100 / 100.f});
5655
}
5756
}

source/client/hud/DebugOverlay.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "ClientWorld.hpp"
3434
#include "Config.hpp"
3535
#include "DebugOverlay.hpp"
36+
#include "GameTime.hpp"
3637

3738
DebugOverlay::DebugOverlay(const ClientPlayer &player, const ClientWorld &world)
3839
: m_player(player), m_world(world)
@@ -84,6 +85,8 @@ void DebugOverlay::update() {
8485
stream << "Alive entities: " << m_world.scene().registry().alive();
8586
stream << '\n';
8687
stream << "Chunk updates: " << ClientChunk::chunkUpdatesPerSec;
88+
stream << '\n';
89+
stream << "TPS: " << GameTime::getTicksPerSecond();
8790

8891
m_positionText.setString(stream.str());
8992
}

source/client/network/ClientCommandHandler.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "ConnectionErrorState.hpp"
3838
#include "DrawableComponent.hpp"
3939
#include "DrawableDef.hpp"
40+
#include "GameTime.hpp"
4041
#include "LuaGUIState.hpp"
4142
#include "NetworkComponent.hpp"
4243
#include "PositionComponent.hpp"
@@ -167,6 +168,11 @@ void ClientCommandHandler::setupCallbacks() {
167168
}
168169
});
169170

171+
m_client.setCommandCallback(Network::Command::ServerTick, [this](Network::Packet &) {
172+
if (!m_isSingleplayer) // FIXME
173+
GameTime::incrementTicks();
174+
});
175+
170176
m_client.setCommandCallback(Network::Command::ServerClosed, [this](Network::Packet &packet) {
171177
std::string message;
172178
packet >> message;

source/common/core/GameTime.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ u64 GameTime::s_ticks = 0;
3535
u16 GameTime::s_ticksPerSecond = 0;
3636

3737
float GameTime::getCurrentTime(float offset, float speed) {
38-
return std::fmod(gk::GameClock::getInstance().getTicks() * daySpeed * speed / 1000.f + offset, 360.f) / 360.f;
38+
return std::fmod(s_ticks * daySpeed * speed + offset, dayLength) / dayLength;
3939
}
4040

4141
float GameTime::getSunlightIntensityFromTime(float time) {

source/common/core/GameTime.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Sky;
3434
class GameTime {
3535
public:
3636
static constexpr float daySpeed = 1.f;
37+
static constexpr u32 dayLength = 24000;
3738

3839
// Note: These functions are only needed in the client
3940
static float getCurrentTime(float offset = 0.f, float speed = 1.f);

source/common/network/Network.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ std::string Network::commandToString(Network::Command command) {
3737
{Network::Command::ClientOk, "ClientOk"},
3838
{Network::Command::ClientRefused, "ClientRefused"},
3939

40+
{Network::Command::ServerTick, "ServerTick"},
4041
{Network::Command::ServerClosed, "ServerClosed"},
4142

4243
{Network::Command::ChunkData, "ChunkData"},

source/common/network/Network.hpp

+27-26
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,41 @@ namespace Network {
4040
ClientOk = 0x02,
4141
ClientRefused = 0x03,
4242

43-
ServerClosed = 0x04,
43+
ServerTick = 0x04,
44+
ServerClosed = 0x05,
4445

45-
ChunkData = 0x05,
46-
ChunkRequest = 0x06,
46+
ChunkData = 0x06,
47+
ChunkRequest = 0x07,
4748

48-
PlayerPlaceBlock = 0x07,
49-
PlayerDigBlock = 0x08,
50-
PlayerInvUpdate = 0x09,
51-
PlayerPosUpdate = 0x0a,
52-
PlayerRotUpdate = 0x0b,
53-
PlayerSpawn = 0x0c,
54-
PlayerChangeDimension = 0x0d,
55-
PlayerHeldItemChanged = 0x0e,
49+
PlayerPlaceBlock = 0x08,
50+
PlayerDigBlock = 0x09,
51+
PlayerInvUpdate = 0x0a,
52+
PlayerPosUpdate = 0x0b,
53+
PlayerRotUpdate = 0x0c,
54+
PlayerSpawn = 0x0d,
55+
PlayerChangeDimension = 0x0e,
56+
PlayerHeldItemChanged = 0x0f,
5657

57-
BlockUpdate = 0x0f,
58-
BlockActivated = 0x10,
59-
BlockGUIData = 0x11,
60-
BlockInvUpdate = 0x12,
61-
BlockDataUpdate = 0x13,
58+
BlockUpdate = 0x10,
59+
BlockActivated = 0x11,
60+
BlockGUIData = 0x12,
61+
BlockInvUpdate = 0x13,
62+
BlockDataUpdate = 0x14,
6263

63-
ItemActivated = 0x14,
64+
ItemActivated = 0x15,
6465

65-
RegistryData = 0x15,
66+
RegistryData = 0x16,
6667

67-
ChatMessage = 0x16,
68+
ChatMessage = 0x17,
6869

69-
EntitySpawn = 0x17,
70-
EntityDespawn = 0x18,
71-
EntityPosition = 0x19,
72-
EntityRotation = 0x1a,
73-
EntityAnimation = 0x1b,
74-
EntityDrawableDef = 0x1c,
70+
EntitySpawn = 0x18,
71+
EntityDespawn = 0x19,
72+
EntityPosition = 0x1a,
73+
EntityRotation = 0x1b,
74+
EntityAnimation = 0x1c,
75+
EntityDrawableDef = 0x1d,
7576

76-
KeyPressed = 0x1d,
77+
KeyPressed = 0x1e,
7778
};
7879

7980
std::string commandToString(Command command);

source/server/core/ServerApplication.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ void ServerApplication::update() {
201201
doTick = true;
202202

203203
GameTime::incrementTicks();
204+
205+
m_serverCommandHandler.sendServerTick();
204206
}
205207

206208
m_worldController.update(doTick);

source/server/network/ServerCommandHandler.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737
#include "ServerItem.hpp"
3838
#include "WorldController.hpp"
3939

40+
void ServerCommandHandler::sendServerTick(const ClientInfo *client) const {
41+
Network::Packet packet;
42+
packet << Network::Command::ServerTick;
43+
44+
if (!client)
45+
m_server.sendToAllClients(packet);
46+
else
47+
client->tcpSocket->send(packet);
48+
}
49+
4050
void ServerCommandHandler::sendServerClosed(const std::string &message, const ClientInfo *client) const {
4151
Network::Packet packet;
4252
packet << Network::Command::ServerClosed << message;

source/server/network/ServerCommandHandler.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class ServerCommandHandler {
6161
ServerCommandHandler(ScriptEngine &scriptEngine, Server &server, WorldController &worldController, PlayerList &players, Registry &registry)
6262
: m_scriptEngine(scriptEngine), m_server(server), m_worldController(worldController), m_players(players), m_registry(registry) {}
6363

64+
void sendServerTick(const ClientInfo *client = nullptr) const;
6465
void sendServerClosed(const std::string &message, const ClientInfo *client = nullptr) const;
6566
void sendBlockDataUpdate(s32 x, s32 y, s32 z, const BlockData *blockData, const ClientInfo *client = nullptr) const;
6667
void sendBlockInvUpdate(s32 x, s32 y, s32 z, const Inventory &inventory, const ClientInfo *client = nullptr) const;

0 commit comments

Comments
 (0)