Skip to content

Commit

Permalink
Instant speed changes
Browse files Browse the repository at this point in the history
Avoid waiting for next game hour to adjust to speed changes.

Resume previous speed on load if game was saved with Speed Up key pressed.

Fixes CorsixTH#2758
  • Loading branch information
TheCycoONE committed Jan 19, 2025
1 parent b1457b1 commit d5691ff
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions CorsixTH/Lua/world.lua
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,8 @@ end

--! Return true if the given speed the same as the current speed.
function World:isCurrentSpeed(speed)
local numerator, denominator = unpack(tick_rates[speed])
return self.hours_per_tick == numerator and self.tick_rate == denominator
local hoursPerTick, tickRate = unpack(tick_rates[speed])
return self.hours_per_tick == hoursPerTick and self.tick_rate == tickRate
end

--! Return the name of the current speed, relating to a key in tick_rates.
Expand All @@ -760,20 +760,25 @@ function World:setSpeed(speed)
self.user_actions_allowed = true
end

local currentSpeed = self:getCurrentSpeed()
if currentSpeed ~= "Pause" and currentSpeed ~= "Speed Up" then
self.prev_speed = self:getCurrentSpeed()
local oldSpeed = self:getCurrentSpeed()
if oldSpeed ~= "Pause" and oldSpeed ~= "Speed Up" then
self.prev_speed = oldSpeed
end

local was_paused = currentSpeed == "Pause"
local numerator, denominator = unpack(tick_rates[speed])
self.hours_per_tick = numerator
self.tick_rate = denominator
local was_paused = oldSpeed == "Pause"
local oldTickRate = self.tick_rate or 1
local newHoursPerTick, newTickRate = unpack(tick_rates[speed])

if was_paused then
TheApp.audio:onEndPause()
self.tick_timer = newTickRate
else
self.tick_timer = math.ceil((self.tick_timer or 0) / oldTickRate * newTickRate)
end

self.hours_per_tick = newHoursPerTick
self.tick_rate = newTickRate

-- Set the blue filter according to whether the user can build or not.
TheApp.video:setBlueFilterActive(not self.user_actions_allowed and not self.ui:checkForMustPauseWindows())
return false
Expand Down Expand Up @@ -1250,9 +1255,7 @@ function World:winGame(player_no)
end
end
self.hospitals[player_no].game_won = true
if self:isCurrentSpeed("Speed Up") then
self:previousSpeed()
end
self:previousSpeed()
self.ui.bottom_panel:queueMessage("information", message, nil, 0, 2, callback)
self.ui.bottom_panel:openLastMessage()
end
Expand Down Expand Up @@ -2526,6 +2529,9 @@ function World:afterLoad(old, new)
self:localiseInitial(staff.profile)
end

-- Fix if game was saved with Speed Up
self:previousSpeed()

self.earthquake:afterLoad(old, new)
self.savegame_version = new
self.release_version = TheApp:getVersion(new)
Expand Down

0 comments on commit d5691ff

Please sign in to comment.