Skip to content

Commit

Permalink
[DebugInfoWidget] Added 'Ticks' and 'Game time'.
Browse files Browse the repository at this point in the history
[GameTime] The day now starts at 6 AM.
  • Loading branch information
Unarelith committed Jul 17, 2020
1 parent 07f86b9 commit 0f015ee
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
4 changes: 3 additions & 1 deletion docs/network-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ _This packet has no field._

Packet sent at the beginning of every server tick.

_This packet has no field._
| Field name | Field type | Notes |
| ------------- | ----------- | ---------------------------------------------------- |
| Current time | u64 | Current time in the server |

#### ServerClosed

Expand Down
9 changes: 9 additions & 0 deletions source/client/hud/DebugOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,16 @@ void DebugOverlay::update() {
stream << '\n';
stream << "Chunk updates: " << ClientChunk::chunkUpdatesPerSec;
stream << '\n';
stream << "Ticks: " << GameTime::getTicks();
stream << '\n';
stream << "TPS: " << GameTime::getTicksPerSecond();
stream << '\n';
stream << "Game time: ";

u16 hour = GameTime::getCurrentHour();
u16 minute = GameTime::getCurrentMinute();
stream << (hour < 10 ? "0" : "") << hour << ":";
stream << (minute < 10 ? "0" : "") << minute;

m_positionText.setString(stream.str());
}
Expand Down
9 changes: 6 additions & 3 deletions source/client/network/ClientCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,12 @@ void ClientCommandHandler::setupCallbacks() {
}
});

m_client.setCommandCallback(Network::Command::ServerTick, [this](Network::Packet &) {
if (!m_isSingleplayer) // FIXME
GameTime::incrementTicks();
m_client.setCommandCallback(Network::Command::ServerTick, [this](Network::Packet &packet) {
if (!m_isSingleplayer) { // FIXME
sf::Uint64 ticks;
packet >> ticks;
GameTime::setTicks(ticks);
}
});

m_client.setCommandCallback(Network::Command::ServerClosed, [this](Network::Packet &packet) {
Expand Down
10 changes: 5 additions & 5 deletions source/common/core/GameTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ u64 GameTime::s_ticks = 0;
u16 GameTime::s_ticksPerSecond = 0;

float GameTime::getCurrentTime(float offset, float speed) {
return std::fmod(s_ticks * daySpeed * speed + offset, dayLength) / dayLength;
return std::fmod((s_ticks + dayStartOffset) * daySpeed * speed + offset, dayLength) / dayLength;
}

float GameTime::getSunlightIntensityFromTime(float time) {
Expand All @@ -58,6 +58,10 @@ gk::Color GameTime::getSkyColorFromTime(const Sky &sky, float time) {
void GameTime::incrementTicks() {
++s_ticks;

updateTpsCounter();
}

void GameTime::updateTpsCounter() {
static u64 tpsTimer = gk::GameClock::getInstance().getTicks(true);
static u8 tpsCount = 0;

Expand All @@ -71,7 +75,3 @@ void GameTime::incrementTicks() {
}
}

u16 GameTime::getTicksPerSecond() {
return s_ticksPerSecond;
}

18 changes: 15 additions & 3 deletions source/common/core/GameTime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,30 @@ class GameTime {
public:
static constexpr float daySpeed = 1.f;
static constexpr u32 dayLength = 24000;
static constexpr u32 dayStartOffset = 3000;

// Note: These functions are only needed in the client
// Note: These 3 functions are only needed in the client
static float getCurrentTime(float offset = 0.f, float speed = 1.f);
static float getSunlightIntensityFromTime(float time);
static gk::Color getSkyColorFromTime(const Sky &sky, float time);

// Note: These functions are only needed in the server
static void incrementTicks();
static u16 getTicksPerSecond();
static void setTicks(u64 ticks) { s_ticks = ticks; }

static u16 getTicksPerSecond() { return s_ticksPerSecond; }
static u64 getTicks() { return s_ticks; }

static u8 getCurrentHour() {
return u64((s_ticks + dayStartOffset + 3000.f) / 1000.f) % 24;
}

static u8 getCurrentMinute() {
return u64((s_ticks + dayStartOffset + 3000.f) / 1000.f * 60.0f) % 60;
}

private:
static void updateTpsCounter();

static u64 s_ticks;
static u16 s_ticksPerSecond;
};
Expand Down
3 changes: 2 additions & 1 deletion source/server/network/ServerCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "AnimationComponent.hpp"
#include "BlockData.hpp"
#include "DrawableDef.hpp"
#include "GameTime.hpp"
#include "NetworkComponent.hpp"
#include "PlayerList.hpp"
#include "Registry.hpp"
Expand All @@ -39,7 +40,7 @@

void ServerCommandHandler::sendServerTick(const ClientInfo *client) const {
Network::Packet packet;
packet << Network::Command::ServerTick;
packet << Network::Command::ServerTick << (sf::Uint64)GameTime::getTicks();

if (!client)
m_server.sendToAllClients(packet);
Expand Down

0 comments on commit 0f015ee

Please sign in to comment.