Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert: calendar sync #677

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <DateTime.h>
#include <Structs/TimeModel.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;

DateTime m_onlineTime;
DateTime m_offlineTime;
TimeModel m_onlineTime;
TimeModel 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: 51 additions & 33 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 <Events/DisconnectedEvent.h>
#include <World.h>
#include <Events/UpdateEvent.h>
#include <Events/DisconnectedEvent.h>
#include <Messages/ServerTimeSettings.h>
#include <World.h>

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

constexpr float kTransitionSpeed = 5.f;

Expand All @@ -19,7 +19,8 @@ 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 @@ -29,29 +30,16 @@ 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;
ToggleGameClock(true);
m_switchToOffline = true;
}

float CalendarService::TimeInterpolate(const TimeModel& aFrom, TimeModel& aTo) const
Expand All @@ -72,11 +60,24 @@ float CalendarService::TimeInterpolate(const TimeModel& aFrom, TimeModel& aTo) c
void CalendarService::ToggleGameClock(bool aEnable)
{
auto* pGameTime = TimeData::Get();
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;
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;
}

s_gameClockLocked = !aEnable;
}
Expand All @@ -93,6 +94,23 @@ 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 @@ -101,19 +119,19 @@ void CalendarService::HandleUpdate(const UpdateEvent& aEvent) noexcept
m_lastTick = now;

m_onlineTime.Update(delta);
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);
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;

// time transition in
if (m_fadeTimer < kTransitionSpeed)
{
pGameTime->GameHour->f = TimeInterpolate(m_offlineTime.m_timeModel, m_onlineTime.m_timeModel);
pGameTime->GameHour->f = TimeInterpolate(m_offlineTime, m_onlineTime);
m_fadeTimer += updateDelta;
}
else
pGameTime->GameHour->f = m_onlineTime.m_timeModel.Time;
pGameTime->GameHour->f = m_onlineTime.Time;
}
}
9 changes: 0 additions & 9 deletions Code/client/Services/Generic/TransportService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#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 @@ -153,13 +151,6 @@ 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: 0 additions & 46 deletions Code/common/DateTime.cpp

This file was deleted.

21 changes: 0 additions & 21 deletions Code/common/DateTime.h

This file was deleted.

43 changes: 43 additions & 0 deletions Code/common/Structs/TimeModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

#include <Structs/TimeModel.h>

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

int TimeModel::GetNumerOfDaysByMonthIndex(int aIndex)
{
if (aIndex < 12)
{
return cDayLengthArray[aIndex];
}

return 0;
}

void TimeModel::Update(uint64_t aDelta)
{
float deltaSeconds = static_cast<float>(aDelta) / 1000.f;
Time += (deltaSeconds * (TimeScale * 0.00027777778f));

if (Time > 24.f)
{
int maxDays = GetNumerOfDaysByMonthIndex(Month);

while (Time > 24.f)
{
Time = Time + -24.f;
Day++;
}

if (Day > maxDays)
{
Month++;
Day = Day - maxDays;

if (Month > 12)
{
Month = Month + -12;
Year++;
}
}
}
}
16 changes: 16 additions & 0 deletions Code/common/Structs/TimeModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <cstdint>

struct TimeModel
{
// Default time: 01/01/01 at 12:00
float TimeScale = 20.f;
float Time = 12.f;
int Year = 1;
int Month = 1;
int Day = 1;

void Update(uint64_t aDelta);
[[nodiscard]] static int GetNumerOfDaysByMonthIndex(int index);
};
2 changes: 0 additions & 2 deletions Code/encoding/Messages/AuthenticationRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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 @@ -29,5 +28,4 @@ void AuthenticationRequest::DeserializeRaw(TiltedPhoques::Buffer::Reader& aReade
WorldSpaceId.Deserialize(aReader);
CellId.Deserialize(aReader);
Level = Serialization::ReadVarInt(aReader) & 0xFFFF;
PlayerTime.Deserialize(aReader);
}
5 changes: 1 addition & 4 deletions Code/encoding/Messages/AuthenticationRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <Structs/Mods.h>
#include <TiltedCore/Buffer.hpp>
#include <Structs/GameId.h>
#include <Structs/TimeModel.h>

struct AuthenticationRequest final : ClientMessage
{
Expand All @@ -23,8 +22,7 @@ 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
&& PlayerTime == achRhs.PlayerTime;
WorldSpaceId == achRhs.WorldSpaceId && CellId == achRhs.CellId && Level == achRhs.Level;
}

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

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

void ServerTimeSettings::DeserializeRaw(TiltedPhoques::Buffer::Reader& aReader) noexcept
{
timeModel.Deserialize(aReader);
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);
}
Loading
Loading