2018-12-06 13:27:08 +02:00
|
|
|
local Command = require 'utils.command'
|
2019-01-30 05:52:43 +02:00
|
|
|
local Ranks = require 'resources.ranks'
|
2019-02-24 13:51:09 +02:00
|
|
|
local Global = require 'utils.global'
|
2019-01-30 05:52:43 +02:00
|
|
|
|
2018-12-06 13:27:08 +02:00
|
|
|
local format = string.format
|
|
|
|
|
|
|
|
local Performance = {}
|
|
|
|
|
2019-02-24 13:51:09 +02:00
|
|
|
local mining_efficiency = {
|
2019-03-04 00:13:56 +02:00
|
|
|
active_modifier = 0
|
2019-02-24 13:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
local craft_bonus = {
|
2019-03-04 00:13:56 +02:00
|
|
|
active_modifier = 0
|
2019-02-24 13:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
local running_bonus = {
|
2019-03-04 00:13:56 +02:00
|
|
|
active_modifier = 0
|
2019-02-24 13:51:09 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 00:13:56 +02:00
|
|
|
Global.register(
|
|
|
|
{
|
|
|
|
mining_efficiency = mining_efficiency,
|
|
|
|
craft_bonus = craft_bonus,
|
|
|
|
running_bonus = running_bonus
|
|
|
|
},
|
|
|
|
function(tbl)
|
|
|
|
mining_efficiency = tbl.mining_efficiency
|
|
|
|
craft_bonus = tbl.craft_bonus
|
|
|
|
running_bonus = tbl.running_bonus
|
|
|
|
end
|
|
|
|
)
|
2019-02-24 13:51:09 +02:00
|
|
|
|
2018-12-06 13:27:08 +02:00
|
|
|
---Sets the scale of performance.
|
2019-01-25 01:41:32 +02: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 13:27:08 +02: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-25 01:41:32 +02:00
|
|
|
|
|
|
|
local stat_mod = Performance.get_player_stat_modifier()
|
2018-12-06 13:27:08 +02:00
|
|
|
for _, force in pairs(game.forces) do
|
2019-02-24 13:51:09 +02:00
|
|
|
force.character_running_speed_modifier = force.character_running_speed_modifier - running_bonus.active_modifier + stat_mod - 1
|
|
|
|
running_bonus.active_modifier = stat_mod - 1
|
|
|
|
force.manual_mining_speed_modifier = force.manual_mining_speed_modifier - mining_efficiency.active_modifier + stat_mod - 1
|
|
|
|
mining_efficiency.active_modifier = stat_mod - 1
|
|
|
|
force.manual_crafting_speed_modifier = force.manual_crafting_speed_modifier - craft_bonus.active_modifier + stat_mod - 1
|
|
|
|
craft_bonus.active_modifier = stat_mod - 1
|
2018-12-06 13:27:08 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-25 01:41:32 +02:00
|
|
|
---Returns the current game time scale
|
|
|
|
function Performance.get_time_scale()
|
2018-12-06 13:27:08 +02:00
|
|
|
return game.speed
|
|
|
|
end
|
|
|
|
|
2019-01-25 01:41:32 +02:00
|
|
|
---Returns the stat modifier for stats affecting the players
|
|
|
|
function Performance.get_player_stat_modifier()
|
2018-12-06 13:27:08 +02:00
|
|
|
return 1 / game.speed
|
|
|
|
end
|
|
|
|
|
2019-01-25 01:41:32 +02:00
|
|
|
Command.add(
|
|
|
|
'performance-scale-set',
|
|
|
|
{
|
2019-03-04 00:13:56 +02:00
|
|
|
description = {'command_description.performance_scale_set'},
|
2019-01-25 01:41:32 +02:00
|
|
|
arguments = {'scale'},
|
2019-01-30 05:52:43 +02:00
|
|
|
required_rank = Ranks.admin,
|
2019-01-25 01:41:32 +02:00
|
|
|
allowed_by_server = true
|
|
|
|
},
|
|
|
|
function(arguments, player)
|
|
|
|
local scale = tonumber(arguments.scale)
|
|
|
|
if scale == nil or scale < 0.05 or scale > 1 then
|
2019-03-04 00:13:56 +02:00
|
|
|
player.print({'performance.fail_wrong_argument'})
|
2019-01-25 01:41:32 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
Performance.set_time_scale(scale)
|
|
|
|
local p = game.print
|
|
|
|
local stat_mod = Performance.get_player_stat_modifier()
|
2019-03-04 00:13:56 +02:00
|
|
|
p({'performance.stat_preamble'})
|
|
|
|
p({'performance.generic_stat', {'performance.game_speed'}, format('%.2f', Performance.get_time_scale())})
|
|
|
|
local stat_string = format('%.2f', stat_mod)
|
2024-09-02 22:32:20 +02:00
|
|
|
p({'performance.output_formatter', {'performance.running_speed'}, stat_string, {'performance.manual_mining_speed'}, stat_string, {'performance.manual_crafting_speed'}, stat_string})
|
2018-12-06 13:27:08 +02:00
|
|
|
end
|
2019-01-25 01:41:32 +02:00
|
|
|
)
|
2018-12-06 13:27:08 +02:00
|
|
|
|
2019-01-25 01:41:32 +02:00
|
|
|
Command.add(
|
|
|
|
'performance-scale-get',
|
|
|
|
{
|
2019-03-04 00:13:56 +02:00
|
|
|
description = {'command_description.performance_scale_get'}
|
2019-01-25 01:41:32 +02:00
|
|
|
},
|
|
|
|
function(_, player)
|
|
|
|
local p = player.print
|
|
|
|
local stat_mod = Performance.get_player_stat_modifier()
|
2019-03-04 00:13:56 +02:00
|
|
|
p({'performance.generic_stat', {'performance.game_speed'}, format('%.2f', Performance.get_time_scale())})
|
|
|
|
local stat_string = format('%.2f', stat_mod)
|
2024-09-02 22:32:20 +02:00
|
|
|
p({'performance.output_formatter', {'performance.running_speed'}, stat_string, {'performance.manual_mining_speed'}, stat_string, {'performance.manual_crafting_speed'}, stat_string})
|
2019-01-25 01:41:32 +02:00
|
|
|
end
|
|
|
|
)
|
2018-12-06 13:27:08 +02:00
|
|
|
|
|
|
|
return Performance
|