Skip to content

Commit

Permalink
Revert "Merge pull request #677 from tiltedphoques/revert/calendarSync"
Browse files Browse the repository at this point in the history
This reverts commit 3a58ff1, reversing
changes made to b9ef03d.
  • Loading branch information
RobbeBryssinck committed May 24, 2024
1 parent 4670a30 commit bdf281b
Show file tree
Hide file tree
Showing 20 changed files with 301 additions and 156 deletions.
8 changes: 4 additions & 4 deletions Code/client/Services/CalendarService.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <Structs/TimeModel.h>
#include <DateTime.h>
#include <Events/EventDispatcher.h>
#include <Games/Events.h>

Expand Down Expand Up @@ -32,13 +32,13 @@ class CalendarService final : public BSTEventSink<TESActivateEvent>
entt::scoped_connection m_updateConnection;
entt::scoped_connection m_disconnectedConnection;

TimeModel m_onlineTime;
TimeModel m_offlineTime;
DateTime m_onlineTime;
DateTime m_offlineTime;
float m_fadeTimer = 0.f;
bool m_switchToOffline = false;
static bool s_gameClockLocked;

uint64_t m_lastTick = 0;
uint64_t m_lastLogTick = 0;
World& m_world;
TransportService& m_transport;
};
84 changes: 33 additions & 51 deletions Code/client/Services/Generic/CalendarService.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <Services/CalendarService.h>

#include <World.h>
#include <Events/UpdateEvent.h>
#include <Events/DisconnectedEvent.h>
#include <Events/UpdateEvent.h>
#include <Messages/ServerTimeSettings.h>
#include <World.h>

#include <TimeManager.h>
#include <PlayerCharacter.h>
#include <Forms/TESObjectCELL.h>
#include <PlayerCharacter.h>
#include <TimeManager.h>

constexpr float kTransitionSpeed = 5.f;

Expand All @@ -19,8 +19,7 @@ bool CalendarService::AllowGameTick() noexcept
}

CalendarService::CalendarService(World& aWorld, entt::dispatcher& aDispatcher, TransportService& aTransport)
: m_world(aWorld)
, m_transport(aTransport)
: m_world(aWorld), m_transport(aTransport)
{
m_timeUpdateConnection = aDispatcher.sink<ServerTimeSettings>().connect<&CalendarService::OnTimeUpdate>(this);
m_updateConnection = aDispatcher.sink<UpdateEvent>().connect<&CalendarService::HandleUpdate>(this);
Expand All @@ -30,16 +29,29 @@ CalendarService::CalendarService(World& aWorld, entt::dispatcher& aDispatcher, T
void CalendarService::OnTimeUpdate(const ServerTimeSettings& acMessage) noexcept
{
// disable the game clock
m_onlineTime.TimeScale = acMessage.TimeScale;
m_onlineTime.Time = acMessage.Time;
ToggleGameClock(false);
m_onlineTime.m_timeModel.TimeScale = acMessage.timeModel.TimeScale;
m_onlineTime.m_timeModel.Time = acMessage.timeModel.Time;

if (m_world.GetServerSettings().SyncPlayerCalendar)
{
m_onlineTime.m_timeModel.Day = acMessage.timeModel.Day;
m_onlineTime.m_timeModel.Month = acMessage.timeModel.Month;
m_onlineTime.m_timeModel.Year = acMessage.timeModel.Year;
}
else
{
m_onlineTime.m_timeModel.Day = m_offlineTime.m_timeModel.Day;
m_onlineTime.m_timeModel.Month = m_offlineTime.m_timeModel.Month;
m_onlineTime.m_timeModel.Year = m_offlineTime.m_timeModel.Year;
}
}

void CalendarService::OnDisconnected(const DisconnectedEvent&) noexcept
{
// signal a time transition
m_fadeTimer = 0.f;
m_switchToOffline = true;
ToggleGameClock(true);
}

float CalendarService::TimeInterpolate(const TimeModel& aFrom, TimeModel& aTo) const
Expand All @@ -60,24 +72,11 @@ float CalendarService::TimeInterpolate(const TimeModel& aFrom, TimeModel& aTo) c
void CalendarService::ToggleGameClock(bool aEnable)
{
auto* pGameTime = TimeData::Get();
if (aEnable)
{
pGameTime->GameDay->i = m_offlineTime.Day;
pGameTime->GameMonth->i = m_offlineTime.Month;
pGameTime->GameYear->i = m_offlineTime.Year;
pGameTime->TimeScale->f = m_offlineTime.TimeScale;
pGameTime->GameDaysPassed->f = (m_offlineTime.Time * (1.f / 24.f)) + m_offlineTime.Day;
pGameTime->GameHour->f = m_offlineTime.Time;
m_switchToOffline = false;
}
else
{
m_offlineTime.Day = pGameTime->GameDay->i;
m_offlineTime.Month = pGameTime->GameMonth->i;
m_offlineTime.Year = pGameTime->GameYear->i;
m_offlineTime.Time = pGameTime->GameHour->f;
m_offlineTime.TimeScale = pGameTime->TimeScale->f;
}
m_offlineTime.m_timeModel.Day = pGameTime->GameDay->f;
m_offlineTime.m_timeModel.Month = pGameTime->GameMonth->f;
m_offlineTime.m_timeModel.Year = pGameTime->GameYear->f;
m_offlineTime.m_timeModel.Time = pGameTime->GameHour->f;
m_offlineTime.m_timeModel.TimeScale = pGameTime->TimeScale->f;

s_gameClockLocked = !aEnable;
}
Expand All @@ -94,23 +93,6 @@ void CalendarService::HandleUpdate(const UpdateEvent& aEvent) noexcept

const auto now = m_world.GetTick();

if (m_switchToOffline)
{
// time transition out
if (m_fadeTimer < kTransitionSpeed)
{
pGameTime->GameHour->f = TimeInterpolate(m_onlineTime, m_offlineTime);
// before we quit here we fire this event
if ((m_fadeTimer + updateDelta) > kTransitionSpeed)
{
m_fadeTimer += updateDelta;
ToggleGameClock(true);
}
else
m_fadeTimer += updateDelta;
}
}

// we got disconnected or the client got ahead of us
if (now < m_lastTick)
return;
Expand All @@ -119,19 +101,19 @@ void CalendarService::HandleUpdate(const UpdateEvent& aEvent) noexcept
m_lastTick = now;

m_onlineTime.Update(delta);
pGameTime->GameDay->i = m_onlineTime.Day;
pGameTime->GameMonth->i = m_onlineTime.Month;
pGameTime->GameYear->i = m_onlineTime.Year;
pGameTime->TimeScale->f = m_onlineTime.TimeScale;
pGameTime->GameDaysPassed->f = (m_onlineTime.Time * (1.f / 24.f)) + m_onlineTime.Day;
pGameTime->TimeScale->f = m_onlineTime.m_timeModel.TimeScale;
pGameTime->GameDay->f = m_onlineTime.m_timeModel.Day;
pGameTime->GameMonth->f = m_onlineTime.m_timeModel.Month;
pGameTime->GameYear->f = m_onlineTime.m_timeModel.Year;
pGameTime->GameDaysPassed->f += m_onlineTime.GetDeltaTime(delta);

// time transition in
if (m_fadeTimer < kTransitionSpeed)
{
pGameTime->GameHour->f = TimeInterpolate(m_offlineTime, m_onlineTime);
pGameTime->GameHour->f = TimeInterpolate(m_offlineTime.m_timeModel, m_onlineTime.m_timeModel);
m_fadeTimer += updateDelta;
}
else
pGameTime->GameHour->f = m_onlineTime.Time;
pGameTime->GameHour->f = m_onlineTime.m_timeModel.Time;
}
}
9 changes: 9 additions & 0 deletions Code/client/Services/Generic/TransportService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <Forms/TESWorldSpace.h>
#include <Forms/TESObjectCELL.h>

#include <TimeManager.h>

#include <Forms/TESNPC.h>
#include <TiltedOnlinePCH.h>
#include <World.h>
Expand Down Expand Up @@ -151,6 +153,13 @@ void TransportService::OnConnected()

request.Level = pPlayer->GetLevel();

auto* pGameTime = TimeData::Get();
request.PlayerTime.TimeScale = pGameTime->TimeScale->f;
request.PlayerTime.Time = pGameTime->GameHour->f;
request.PlayerTime.Year = pGameTime->GameYear->f;
request.PlayerTime.Month = pGameTime->GameMonth->f;
request.PlayerTime.Day = pGameTime->GameDay->f;

Send(request);
}

Expand Down
46 changes: 46 additions & 0 deletions Code/common/DateTime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "DateTime.h"
#include <cmath>

bool DateTime::operator==(const DateTime& other) const
{
return m_timeModel == other.m_timeModel;
}

const uint32_t cDayLengthArray[12] = {31u, 28u, 31u, 30u, 31u, 30u, 31u, 31u, 30u, 31u, 30u, 31u};

uint32_t DateTime::GetNumberOfDaysByMonthIndex(int aIndex)
{
return cDayLengthArray[aIndex % 12];
}

void DateTime::Update(uint64_t aDeltaTick)
{
m_timeModel.Time += GetDeltaTime(aDeltaTick);

m_timeModel.Day += static_cast<uint32_t>(m_timeModel.Time / 24.f);
m_timeModel.Time = std::fmod(m_timeModel.Time, 24.f);

while (m_timeModel.Day > GetNumberOfDaysByMonthIndex(m_timeModel.Month))
{
m_timeModel.Day -= GetNumberOfDaysByMonthIndex(m_timeModel.Month);
m_timeModel.Month++;
}
m_timeModel.Year += m_timeModel.Month / 12;
m_timeModel.Month %= 12;
}

float DateTime::GetDeltaTime(uint64_t aDeltaTick) const noexcept
{
float deltaSeconds = static_cast<float>(aDeltaTick) / 1000.f;
return (deltaSeconds * (m_timeModel.TimeScale * 0.00027777778f));
}

float DateTime::GetTimeInDays() const noexcept
{
float totalDays = static_cast<float>(m_timeModel.Year) * 365.f;
for (uint32_t i = 0u; i < m_timeModel.Month; i++)
totalDays += GetNumberOfDaysByMonthIndex(i);
totalDays += m_timeModel.Day;
totalDays += (m_timeModel.Time / 24.f);
return totalDays;
}
21 changes: 21 additions & 0 deletions Code/common/DateTime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <cstdint>
#include <encoding/Structs/TimeModel.h>

struct DateTime
{
[[nodiscard]] static uint32_t GetNumberOfDaysByMonthIndex(int index);

DateTime() = default;
DateTime(TimeModel aTimeModel)
: m_timeModel(aTimeModel)
{}

bool operator==(const DateTime& other) const;
void Update(uint64_t aDeltaTick);
float GetDeltaTime(uint64_t aDeltaTick) const noexcept;
float GetTimeInDays() const noexcept;

TimeModel m_timeModel;
};
43 changes: 0 additions & 43 deletions Code/common/Structs/TimeModel.cpp

This file was deleted.

16 changes: 0 additions & 16 deletions Code/common/Structs/TimeModel.h

This file was deleted.

2 changes: 2 additions & 0 deletions Code/encoding/Messages/AuthenticationRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void AuthenticationRequest::SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter)
WorldSpaceId.Serialize(aWriter);
CellId.Serialize(aWriter);
Serialization::WriteVarInt(aWriter, Level);
PlayerTime.Serialize(aWriter);
}

void AuthenticationRequest::DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept
Expand All @@ -28,4 +29,5 @@ void AuthenticationRequest::DeserializeRaw(TiltedPhoques::Buffer::Reader& aReade
WorldSpaceId.Deserialize(aReader);
CellId.Deserialize(aReader);
Level = Serialization::ReadVarInt(aReader) & 0xFFFF;
PlayerTime.Deserialize(aReader);
}
5 changes: 4 additions & 1 deletion Code/encoding/Messages/AuthenticationRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Structs/Mods.h>
#include <TiltedCore/Buffer.hpp>
#include <Structs/GameId.h>
#include <Structs/TimeModel.h>

struct AuthenticationRequest final : ClientMessage
{
Expand All @@ -22,7 +23,8 @@ struct AuthenticationRequest final : ClientMessage
bool operator==(const AuthenticationRequest& achRhs) const noexcept
{
return GetOpcode() == achRhs.GetOpcode() && DiscordId == achRhs.DiscordId && SKSEActive == achRhs.SKSEActive && MO2Active == achRhs.MO2Active && Token == achRhs.Token && Version == achRhs.Version && UserMods == achRhs.UserMods && Username == achRhs.Username &&
WorldSpaceId == achRhs.WorldSpaceId && CellId == achRhs.CellId && Level == achRhs.Level;
WorldSpaceId == achRhs.WorldSpaceId && CellId == achRhs.CellId && Level == achRhs.Level
&& PlayerTime == achRhs.PlayerTime;
}

uint64_t DiscordId{};
Expand All @@ -35,4 +37,5 @@ struct AuthenticationRequest final : ClientMessage
GameId WorldSpaceId{};
GameId CellId{};
uint16_t Level{};
TimeModel PlayerTime{};
};
15 changes: 2 additions & 13 deletions Code/encoding/Messages/ServerTimeSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,10 @@

void ServerTimeSettings::SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter) const noexcept
{
// poor man's std::bitcast
aWriter.WriteBits(*reinterpret_cast<const uint32_t*>(&TimeScale), 32);
aWriter.WriteBits(*reinterpret_cast<const uint32_t*>(&Time), 32);
timeModel.Serialize(aWriter);
}

void ServerTimeSettings::DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept
{
uint64_t tmp = 0;
uint32_t cVal = 0;

aReader.ReadBits(tmp, 32);
cVal = tmp & 0xFFFFFFFF;
TimeScale = *reinterpret_cast<float*>(&cVal);

aReader.ReadBits(tmp, 32);
cVal = tmp & 0xFFFFFFFF;
Time = *reinterpret_cast<float*>(&cVal);
timeModel.Deserialize(aReader);
}
Loading

0 comments on commit bdf281b

Please sign in to comment.