Skip to content

Commit

Permalink
synchronized time between clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Surer committed Sep 15, 2022
1 parent 7772443 commit 1c07a5a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 101 deletions.
4 changes: 3 additions & 1 deletion OpenGL/src/interface/MonCraftScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ MonCraftScene::MonCraftScene(Viewport* vp)
sunSpeed(0.0075f),
lastClock(SDL_GetTicks())
{
World::getInst().t = (uint32_t)(8.f * dayDuration / 24.f);

auto const& serverConf = Config::getServerConfig();
camera.setFar(16.0f * (float)sqrt(2 * pow(serverConf.renderDistH, 2) + pow(serverConf.renderDistV, 2)));
camera.setFovY(config.fov);
Expand Down Expand Up @@ -132,7 +134,7 @@ MonCraftScene::MonCraftScene(Viewport* vp)

player->setCurrentBlock(overlay->getCurrentBlock());

World::getInst().t = (uint32_t)(8.f * dayDuration / 24.f);

}

MonCraftScene::~MonCraftScene() {
Expand Down
6 changes: 6 additions & 0 deletions OpenGL/src/multiplayer/client/RealServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ bool RealServer::on_packet_recv(sf::Packet& packet) {
serverAck = true;

if(type == PacketType::ACK_LOGIN) {
uint32_t time;
uint32_t start;
auto now = std::chrono::steady_clock::now().time_since_epoch();
uint32_t end = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
packet >> time >> start;
world.t = time + (uint32_t)(end - start);
state = ServerState::CONNECTED;
spdlog::info("Logged into the server");
}
Expand Down
2 changes: 2 additions & 0 deletions OpenGL/src/multiplayer/client/RealServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class RealServer: public Server {
Identifier playerUid;
PendingChunks pendingChunks;

uint32_t worldTime;

bool serverAck;
sf::Time lastServerUpdate;
const sf::Time timeout = sf::seconds(10);
Expand Down
15 changes: 14 additions & 1 deletion OpenGL/src/multiplayer/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <memory>
#include <utility>
#include <vector>
#include <chrono>

#include "entity/Entity.hpp"
#include "multiplayer/Packet.hpp"
Expand All @@ -33,6 +34,9 @@ Server::Server(unsigned short port)
auto& config = Config::getServerConfig();
renderDistH = config.renderDistH;
renderDistV = config.renderDistV;
World::getInst().t = (uint32_t)(8.f * dayDuration / 24.f);
auto now = std::chrono::steady_clock::now().time_since_epoch();
lastClock = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
}

Server::~Server()
Expand Down Expand Up @@ -71,6 +75,13 @@ void Server::on_packet_recv(sf::Packet& packet, ClientID client) {
}

void Server::on_server_tick() {
auto now = std::chrono::steady_clock::now().time_since_epoch();
uint32_t time = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
World::getInst().dt = time - lastClock;
World::getInst().t += World::getInst().dt;
World::getInst().t = World::getInst().t % dayDuration;
lastClock = time;

std::lock_guard<std::mutex> lck(mutex);
packet_entity_tick();
packet_chunks();
Expand Down Expand Up @@ -121,7 +132,9 @@ void Server::packet_blocks(Identifier uid, BlockArray changedBlocks) {
void Server::packet_ack_login(ClientID client, Identifier uid) {
sf::Packet packet;
PacketHeader header(PacketType::ACK_LOGIN);
packet << header << uid;
auto now = std::chrono::steady_clock::now().time_since_epoch();
uint32_t start = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
packet << header << world.t << start;
send(packet, client);
}

Expand Down
2 changes: 2 additions & 0 deletions OpenGL/src/multiplayer/server/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Server {
int renderDistH, renderDistV;
static const unsigned int maxChunks = 50;

uint32_t lastClock;

sf::Clock clock;
const sf::Time timeout = sf::seconds(10);
const sf::Time tickAckLimit = sf::milliseconds(1000);
Expand Down
70 changes: 0 additions & 70 deletions OpenGL/src/multiplayer/server/UdpServer.cpp

This file was deleted.

29 changes: 0 additions & 29 deletions OpenGL/src/multiplayer/server/UdpServer.hpp

This file was deleted.

0 comments on commit 1c07a5a

Please sign in to comment.