Skip to content
Permalink
Browse files

[General] Use different timescales for day and night

  • Loading branch information...
davidcernat committed Apr 30, 2019
1 parent 6cbaf2b commit 25f1c70daf76d2d3bf15c1cab23fa4bec9c57a5e
Showing with 47 additions and 10 deletions.
  1. +15 −4 scripts/commandHandler.lua
  2. +6 −1 scripts/config.lua
  3. +1 −1 scripts/menu/help.lua
  4. +6 −3 scripts/serverCore.lua
  5. +19 −1 scripts/world/base.lua
@@ -740,13 +740,24 @@ function commandHandler.ProcessCommand(pid, cmd)

elseif cmd[1] == "settimescale" and moderator then

local inputValue = tonumber(cmd[2])
local inputPeriod = string.lower(tostring(cmd[2]))
local inputValue = tonumber(cmd[3])

if tableHelper.containsValue({"day", "night", "both"}, inputPeriod) and type(inputValue) == "number" then

if inputPeriod == "day" or inputPeriod == "both" then
WorldInstance.data.time.dayTimeScale = inputValue
end

if inputPeriod == "night" or inputPeriod == "both" then
WorldInstance.data.time.nightTimeScale = inputValue
end

if type(inputValue) == "number" then
WorldInstance.data.time.timeScale = inputValue
WorldInstance:QuicksaveToDrive()
WorldInstance:UpdateFrametimeMultiplier()
WorldInstance:LoadTime(pid, true)
frametimeMultiplier = inputValue / WorldInstance.defaultTimeScale
else
tes3mp.SendMessage(pid, "Invalid input! Please use /settimescale day/night/both <value>\n", false)
end

elseif cmd[1] == "setcollision" and admin then
@@ -13,7 +13,7 @@ config.difficulty = 0

-- The world time used for a newly created world
config.defaultTimeTable = { year = 427, month = 7, day = 16, hour = 9,
daysPassed = 1, timeScale = 30 }
daysPassed = 1, dayTimeScale = 30, nightTimeScale = 40 }

-- The chat window instructions that show up when players join the server
config.chatWindowInstructions = "Use " .. color.Yellow .. "Y" .. color.White .. " by default to chat or change it" ..
@@ -23,6 +23,11 @@ config.chatWindowInstructions = "Use " .. color.Yellow .. "Y" .. color.White ..
-- Whether the world time should continue passing when there are no players on the server
config.passTimeWhenEmpty = false

-- The hours at which night is regarded as starting and ending, used to pass time differently
-- when it's night
config.nightStartHour = 20
config.nightEndHour = 6

-- Whether players should be allowed to use the ingame tilde (~) console by default
config.allowConsole = false

@@ -82,7 +82,7 @@ Menus["help moderator page 1"] = {
color.White .. "Set the current day of the month in the world's time\n" ..
color.Yellow .. "/setmonth <value>\n" ..
color.White .. "Set the current month in the world's time\n" ..
color.Yellow .. "/settimescale <value>\n" ..
color.Yellow .. "/settimescale day/night/both <value>\n" ..
color.White .. "Set the timescale in the world's time (30 by default, which is 120 real seconds " ..
"per ingame hour)\n",
buttons = {
@@ -37,7 +37,6 @@ clientDataFiles = {}
speechCollections = {}

hourCounter = nil
frametimeMultiplier = nil
updateTimerId = nil

banList = {}
@@ -146,7 +145,9 @@ do

if config.passTimeWhenEmpty or tableHelper.getCount(Players) > 0 then

hourCounter = hourCounter + (0.0083 * frametimeMultiplier)
hourCounter = hourCounter + (0.0083 * WorldInstance.frametimeMultiplier)

tes3mp.LogMessage(enumerations.log.INFO, "hourCounter is now " .. hourCounter)

local hourFloor = math.floor(hourCounter)

@@ -167,6 +168,8 @@ do
tes3mp.LogMessage(enumerations.log.INFO, "The world time hour is now " .. hourFloor)
WorldInstance.data.time.hour = hourCounter

WorldInstance:UpdateFrametimeMultiplier()

if tableHelper.getCount(Players) > 0 then
WorldInstance:LoadTime(tableHelper.getAnyValue(Players).pid, true)
end
@@ -203,7 +206,7 @@ function OnServerInit()
end

hourCounter = WorldInstance.data.time.hour
frametimeMultiplier = WorldInstance.data.time.timeScale / WorldInstance.defaultTimeScale
WorldInstance:UpdateFrametimeMultiplier()

updateTimerId = tes3mp.CreateTimer("UpdateTime", time.seconds(1))
tes3mp.StartTimer(updateTimerId)
@@ -108,6 +108,7 @@ function BaseWorld:GetRegionAuthority(regionName)
end

function BaseWorld:SetRegionAuthority(pid, regionName)

self.storedRegions[regionName].authority = pid
tes3mp.LogMessage(enumerations.log.INFO, "Authority of region " .. regionName .. " is now " ..
logicHandler.GetChatName(pid))
@@ -141,6 +142,22 @@ function BaseWorld:IncrementDay()
end
end

function BaseWorld:GetCurrentTimeScale()

if self.data.time.dayTimeScale == nil then self.data.time.dayTimeScale = self.defaultTimeScale end
if self.data.time.nightTimeScale == nil then self.data.time.nightTimeScale = self.defaultTimeScale end

if self.data.time.hour >= config.nightStartHour or self.data.time.hour <= config.nightEndHour then
return self.data.time.nightTimeScale
else
return self.data.time.dayTimeScale
end
end

function BaseWorld:UpdateFrametimeMultiplier()
self.frametimeMultiplier = WorldInstance:GetCurrentTimeScale() / WorldInstance.defaultTimeScale
end

function BaseWorld:GetCurrentMpNum()
return self.data.general.currentMpNum
end
@@ -236,7 +253,8 @@ function BaseWorld:LoadTime(pid, forEveryone)
tes3mp.SetYear(self.data.time.year)

tes3mp.SetDaysPassed(self.data.time.daysPassed)
tes3mp.SetTimeScale(self.data.time.timeScale)

tes3mp.SetTimeScale(self:GetCurrentTimeScale())

tes3mp.SendWorldTime(pid, forEveryone)
end

0 comments on commit 25f1c70

Please sign in to comment.
You can’t perform that action at this time.