Skip to content

Commit

Permalink
Refactor time synchronization and network clock time updating
Browse files Browse the repository at this point in the history
  • Loading branch information
JensGryspeert committed Jan 10, 2024
1 parent b2af132 commit aa40078
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions client/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local CurrentWeather = Config.StartWeather
local lastWeather = CurrentWeather
local baseTime = Config.BaseTime
local timeOffset = Config.TimeOffset
local timer = 0
local freezeTime = Config.FreezeTime
local blackout = Config.Blackout
local blackoutVehicle = Config.BlackoutVehicle
Expand Down Expand Up @@ -83,23 +82,31 @@ CreateThread(function()
local hour
local minute = 0
local second = 0 --Add seconds for shadow smoothness
local timeIncrement = Config.RealTimeSync and 0.25 or 1
local tick = GetGameTimer()
while true do
if not disable then
Wait(0)
local _, _, _, hours, minutes, _ = GetLocalTime()
local newBaseTime = baseTime
if GetGameTimer() - 22 > timer then --Generate seconds in client side to avoid communiation
second = second + 1 --Minutes are sent from the server every 2 seconds to keep sync
timer = GetGameTimer()
if tick - (Config.RealTimeSync and 500 or 22) > tick then
second = second + timeIncrement
tick = GetGameTimer()
end
if freezeTime then
timeOffset = timeOffset + baseTime - newBaseTime
second = 0
end
baseTime = newBaseTime
hour = math.floor(((baseTime+timeOffset)/60)%24)
if minute ~= math.floor((baseTime+timeOffset)%60) then --Reset seconds to 0 when new minute
minute = math.floor((baseTime+timeOffset)%60)
second = 0
if Config.RealTimeSync then
hour = hours
minute = minutes
else
hour = math.floor(((baseTime+timeOffset)/60)%24)
if minute ~= math.floor((baseTime+timeOffset)%60) then --Reset seconds to 0 when new minute
minute = math.floor((baseTime+timeOffset)%60)
second = 0
end
end
NetworkOverrideClockTime(hour, minute, second) --Send hour included seconds to network clock time
else
Expand Down

0 comments on commit aa40078

Please sign in to comment.