From b46595ff101095679e18ffad83d32897fd201268 Mon Sep 17 00:00:00 2001 From: Lynn Date: Thu, 6 Dec 2018 12:27:08 +0100 Subject: [PATCH] Added a scale to change game performance and running speed accordingly (#505) --- config.lua | 4 +++ control.lua | 4 +++ features/performance.lua | 59 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 features/performance.lua diff --git a/config.lua b/config.lua index 4adfff7a..822bcad3 100644 --- a/config.lua +++ b/config.lua @@ -14,6 +14,10 @@ global.config = { -- New Scenario Features, appears in the "What's new" tab new_info_key = 'Nothing is new. The world is at peace', + performance = { + enabled = true, + }, + -- adds a player list icon and keeps track of data. player_list = { enabled = true, diff --git a/control.lua b/control.lua index 2ab07f2a..9edaa324 100644 --- a/control.lua +++ b/control.lua @@ -39,6 +39,10 @@ end require 'features.train_station_names' require 'features.walkabout' +if global.config.performance.enabled then + require 'features.performance' +end + -- GUIs the order determines the order they appear from left to right. -- These can be safely disabled if you want less GUI items. -- Some map presets will add GUI modules themselves. diff --git a/features/performance.lua b/features/performance.lua new file mode 100644 index 00000000..9b91fb0f --- /dev/null +++ b/features/performance.lua @@ -0,0 +1,59 @@ +local Command = require 'utils.command' +local format = string.format + +local Performance = {} + +---Sets the scale of performance. +---1 means the game runs at normal game speed with full particles and normal walking speed +---0.5 means the game runs at half speed, running speed is doubled and particles are halved +---@param scale number +function Performance.set_scale(scale) + if scale < 0.05 or scale > 1 then + error(format('Scale must range from 0.05 to 1')) + end + + game.speed = scale + local movement_speed_scale = Performance.get_running_speed_modifier() - 1 + for _, force in pairs(game.forces) do + force.character_running_speed_modifier = movement_speed_scale + end +end + +---Returns the current scale +function Performance.get_scale() + return game.speed +end + +---Returns the running speed modifier +function Performance.get_running_speed_modifier() + return 1 / game.speed +end + +Command.add('set-performance-scale', { + description = 'Sets the performance scale between 0.05 and 1. Will alter the game speed and character running speed per force.', + arguments = {'scale'}, + admin_only = true, + allowed_by_server = true, +}, function (arguments, player) + local scale = tonumber(arguments.scale) + if scale == nil or scale < 0.05 or scale > 1 then + player.print('Scale must be a valid number ranging from 0.05 to 1') + return + end + + Performance.set_scale(scale) + local p = game.print + p('## - Changed the game speed and running speed.') + p(format('## - Game speed: %.2f', Performance.get_scale())) + p(format('## - Force running speed: %.2f', Performance.get_running_speed_modifier())) +end) + +Command.add('get-performance-scale', { + description = 'Shows the current performance scale.', +}, function (_, player) + local p = player.print + p(format('Game speed: %.2f', Performance.get_scale())) + p(format('Running speed: %.2f', Performance.get_running_speed_modifier())) +end) + +return Performance