Skip to content

Commit

Permalink
Smooth out gameplay at normal speed
Browse files Browse the repository at this point in the history
Double the number of tick events so that we can do one movement
per tick instead of two at normal speed.

Adjust scrolling speed to match prior to tick timing change
  • Loading branch information
TheCycoONE committed Nov 2, 2024
1 parent d707e53 commit 9d2d073
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CorsixTH/Lua/api_version.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ Note: This file compiles as both Lua and C++. */

#endif /*]] --*/

return 2687;
return 2688;
2 changes: 1 addition & 1 deletion CorsixTH/Lua/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ local runDebugger = corsixth.require("run_debugger")
-- and add compatibility code in afterLoad functions
-- Recommended: Also replace/Update the summary comment

local SAVEGAME_VERSION = 207 -- Make VIP move slower
local SAVEGAME_VERSION = 208 -- Game tick speed adjustment

class "App"

Expand Down
25 changes: 15 additions & 10 deletions CorsixTH/Lua/game_ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ local multigesture_pinch_sensitivity_factor = 0.002
-- will result in a call to adjustZoom in the onTick method
local multigesture_pinch_amplification_factor = 100

-- Speed of scrolling when using keys. Pixels / tick (18ms)
-- This scroll speed is further adjusted by the configured scroll_speed
local key_scroll_speed = 10

--! Game UI constructor.
--!param app (Application) Application object.
--!param local_hospital Hospital to display
Expand Down Expand Up @@ -104,10 +108,10 @@ function GameUI:setupGlobalKeyHandlers()

-- Set the scrolling keys.
self.scroll_keys = {
[tostring(self.app.hotkeys["ingame_scroll_up"])] = {x = 0, y = -10},
[tostring(self.app.hotkeys["ingame_scroll_down"])] = {x = 0, y = 10},
[tostring(self.app.hotkeys["ingame_scroll_left"])] = {x = -10, y = 0},
[tostring(self.app.hotkeys["ingame_scroll_right"])] = {x = 10, y = 0},
[tostring(self.app.hotkeys["ingame_scroll_up"])] = {x = 0, y = -key_scroll_speed},
[tostring(self.app.hotkeys["ingame_scroll_down"])] = {x = 0, y = key_scroll_speed},
[tostring(self.app.hotkeys["ingame_scroll_left"])] = {x = -key_scroll_speed, y = 0},
[tostring(self.app.hotkeys["ingame_scroll_right"])] = {x = key_scroll_speed, y = 0},
}

-- This is the long version of the shift speed key.
Expand Down Expand Up @@ -315,9 +319,9 @@ function GameUI:updateKeyScroll()
if dx ~= 0 or dy ~= 0 then
--Get the length of the scrolling vector.
local mag = (dx^2 + dy^2) ^ 0.5
--Then normalize the scrolling vector, after which multiply it by the scroll speed variable used in self.scroll_keys, which is 10 as of 14/10/18.
dx = (dx / mag) * 10
dy = (dy / mag) * 10
--Then normalize the scrolling vector, after which multiply it by the scroll speed variable.
dx = (dx / mag) * key_scroll_speed
dy = (dy / mag) * key_scroll_speed
-- Set the scroll amount to be used.
self.tick_scroll_amount = {x = dx, y = dy}
return true
Expand Down Expand Up @@ -861,11 +865,12 @@ function GameUI:onTick()
-- and defaulted to 2, where 1 was regular scroll speed. By
-- By multiplying by 0.5, we allow for setting slower than normal
-- scroll speeds, and ensure there is no behaviour change for players
-- who do not modify their config file.
-- who do not modify their config file. Later the tick speed was
-- doubled so now we multiply by 0.25.
if self.shift_scroll_speed_pressed then
mult = mult * self.app.config.shift_scroll_speed * 0.5
mult = mult * self.app.config.shift_scroll_speed * 0.25
else
mult = mult * self.app.config.scroll_speed * 0.5
mult = mult * self.app.config.scroll_speed * 0.25
end

self:scrollMap(dx * mult, dy * mult)
Expand Down
68 changes: 43 additions & 25 deletions CorsixTH/Lua/world.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function World:World(app)
self.entity_map = EntityMap(self.map)

-- Time
self.hours_per_tick = 2
self.hours_per_tick = 1
self.tick_rate = 3
self.tick_timer = 0
self.game_date = Date() -- Current date in the game.
Expand Down Expand Up @@ -718,12 +718,12 @@ end
-- happens.
local tick_rates = {
["Pause"] = {0, 1},
["Slowest"] = {1, 28}, -- 1 tick per s
["Slower"] = {1, 6}, -- 1 tick per 216ms
["Normal"] = {2, 3}, -- 2 ticks per 108ms = 1 tick per 54 ms
["Max speed"] = {1, 1}, -- 1 tick per 36 ms
["And then some more"] = {2, 1}, -- 2 ticks per 36 ms = 1 tick 18ms
["Speed Up"] = {4, 1},
["Slowest"] = {1, 56}, -- 1 hour per s
["Slower"] = {1, 12}, -- 1 hour per 216ms
["Normal"] = {1, 3}, -- 1 hour per 54 ms
["Max speed"] = {1, 2}, -- 1 hour per 36 ms
["And then some more"] = {1, 1}, -- 1 hour per 18ms
["Speed Up"] = {8, 1},
}

function World:speedUp()
Expand Down Expand Up @@ -837,8 +837,8 @@ local outside_temperatures = {

local start_date = Date()

--! World ticks are translated to game ticks (or hours) depending on the
-- current speed of the game. There are 50 hours in a TH day.
--! World ticks (every 18ms) are translated to game ticks (or hours) depending
-- on the current speed of the game. There are 50 hours in a TH day.
function World:onTick()
if self.map.level_number == "MAP EDITOR" then return end

Expand Down Expand Up @@ -2461,22 +2461,6 @@ function World:afterLoad(old, new)
self.wall_types = self.app.walls
self:initWallTypes()
end
if old < 183 then -- Adjust game speed and tick rates
local old_tick_rates = {
["Pause"] = {0, 1},
["Slowest"] = {1, 9},
["Slower"] = {1, 5},
["Normal"] = {1, 3},
["Max speed"] = {1, 1},
["And then some more"] = {3, 1},
}
for name, rate in pairs(old_tick_rates) do
if rate[1] == self.hours_per_tick and rate[2] == self.tick_rate then
self:setSpeed(name)
break
end
end
end
if old < 185 then
-- Fix any missing initial in staff names
self:updateInitialsCache()
Expand All @@ -2502,6 +2486,40 @@ function World:afterLoad(old, new)
-- Move existing and future earthquakes to the Earthquake class
self.earthquake = Earthquake(self, true)
end
if old < 208 then -- Adjust game speed and tick rates
local old_tick_rates
if old < 183 then
old_tick_rates = {
["Pause"] = {0, 1},
["Slowest"] = {1, 9},
["Slower"] = {1, 5},
["Normal"] = {1, 3},
["Max speed"] = {1, 1},
["And then some more"] = {3, 1},
}
else
old_tick_rates = {
["Pause"] = {0, 1},
["Slowest"] = {1, 28},
["Slower"] = {1, 6},
["Normal"] = {2, 3},
["Max speed"] = {1, 1},
["And then some more"] = {2, 1},
["Speed Up"] = {4, 1},
}
end
local found_speed = false
for name, rate in pairs(old_tick_rates) do
if rate[1] == self.hours_per_tick and rate[2] == self.tick_rate then
found_speed = true
self:setSpeed(name)
break
end
end
if not found_speed then
self:setSpeed("Normal")
end
end

-- Fix the initial of staff names
self:updateInitialsCache()
Expand Down
2 changes: 1 addition & 1 deletion CorsixTH/Src/lua_sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ SOFTWARE.
// SDL_USEREVENT_SOUND_OVER - informs script of a played sound finishing.
#define SDL_USEREVENT_SOUND_OVER (SDL_USEREVENT + 4)

constexpr int usertick_period_ms = 36;
constexpr int usertick_period_ms = 18;

int luaopen_sdl(lua_State* L);

Expand Down

0 comments on commit 9d2d073

Please sign in to comment.