2018-12-06 12:27:08 +01:00
local Command = require ' utils.command '
2019-01-29 22:52:43 -05:00
local Ranks = require ' resources.ranks '
2018-12-06 12:27:08 +01:00
local format = string.format
local Performance = { }
---Sets the scale of performance.
2019-01-24 18:41:32 -05:00
---1 means the game runs at normal game speed with normal walking speed
---0.5 means the game runs at half speed, running speed is doubled
---@param scale <number>
function Performance . set_time_scale ( scale )
2018-12-06 12:27:08 +01:00
if scale < 0.05 or scale > 1 then
error ( format ( ' Scale must range from 0.05 to 1 ' ) )
end
game.speed = scale
2019-01-24 18:41:32 -05:00
local stat_mod = Performance.get_player_stat_modifier ( )
2018-12-06 12:27:08 +01:00
for _ , force in pairs ( game.forces ) do
2019-01-24 18:41:32 -05:00
force.character_running_speed_modifier = stat_mod - 1
force.manual_mining_speed_modifier = stat_mod - 1
force.manual_crafting_speed_modifier = stat_mod - 1
2018-12-06 12:27:08 +01:00
end
end
2019-01-24 18:41:32 -05:00
---Returns the current game time scale
function Performance . get_time_scale ( )
2018-12-06 12:27:08 +01:00
return game.speed
end
2019-01-24 18:41:32 -05:00
---Returns the stat modifier for stats affecting the players
function Performance . get_player_stat_modifier ( )
2018-12-06 12:27:08 +01:00
return 1 / game.speed
end
2019-01-24 18:41:32 -05:00
Command.add (
' performance-scale-set ' ,
{
2019-01-25 08:22:06 -05:00
description = ' Sets the performance scale between 0.05 and 1. Will alter the game speed, manual mining speed, manual crafting speed and character running speed per force. ' ,
2019-01-24 18:41:32 -05:00
arguments = { ' scale ' } ,
2019-01-29 22:52:43 -05:00
required_rank = Ranks.admin ,
2019-01-24 18:41:32 -05:00
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_time_scale ( scale )
local p = game.print
local stat_mod = Performance.get_player_stat_modifier ( )
2019-01-25 08:22:20 -05:00
p ( ' ## - Game speed changed to compensate for UPS drops and players trying to catch up. ' )
2019-01-24 18:41:32 -05:00
p ( format ( ' ## - Game speed: %.2f ' , Performance.get_time_scale ( ) ) )
2019-01-25 08:22:14 -05:00
p ( format ( ' ## - Running speed: %.2f ' , stat_mod ) )
p ( format ( ' ## - Manual mining speed: %.2f ' , stat_mod ) )
p ( format ( ' ## - Manual crafting speed: %.2f ' , stat_mod ) )
2018-12-06 12:27:08 +01:00
end
2019-01-24 18:41:32 -05:00
)
2018-12-06 12:27:08 +01:00
2019-01-24 18:41:32 -05:00
Command.add (
' performance-scale-get ' ,
{
description = ' Shows the current performance scale. '
} ,
function ( _ , player )
local p = player.print
local stat_mod = Performance.get_player_stat_modifier ( )
p ( format ( ' Game speed: %.2f ' , Performance.get_time_scale ( ) ) )
p ( format ( ' Running speed: %.2f -- mining speed: %.2f -- crafting speed: %.2f ' , stat_mod , stat_mod , stat_mod ) )
end
)
2018-12-06 12:27:08 +01:00
return Performance