1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-04 00:15:45 +02:00
ComfyFactorio/modules/burden.lua

61 lines
1.7 KiB
Lua
Raw Normal View History

2020-04-26 19:36:58 +02:00
local Event = require 'utils.event'
2021-12-05 23:15:49 +02:00
local Modifier = require 'utils.player_modifiers'
2020-04-26 19:36:58 +02:00
local Color = require 'utils.color_presets'
local function validate_player(player)
if not player then
return false
end
if not player.valid then
return false
end
if not player.character then
return false
end
if not player.connected then
return false
end
if not game.players[player.name] then
return false
end
return true
2020-04-26 19:36:58 +02:00
end
2019-03-13 18:09:06 +02:00
local function compute_fullness(player)
local inv = player.get_inventory(defines.inventory.character_main)
2019-03-13 18:09:06 +02:00
local max_stacks = #inv
local num_stacks = 0
local contents = inv.get_contents()
for item, count in pairs(contents) do
local stack_size = 1
if game.item_prototypes[item].stackable then
stack_size = game.item_prototypes[item].stack_size
end
num_stacks = num_stacks + count / stack_size
end
return num_stacks / max_stacks
end
local function check_burden(event)
local player = game.players[event.player_index]
2020-05-11 21:04:13 +02:00
if not validate_player(player) then
return
end
2019-03-13 18:09:06 +02:00
local fullness = compute_fullness(player)
2021-05-23 17:03:52 +02:00
Modifier.update_single_modifier(player, 'character_running_speed_modifier', 'randomness', 0.3 - fullness)
Modifier.update_single_modifier(player, 'character_mining_speed_modifier', 'randomness', 0.3 - fullness)
2020-04-26 19:36:58 +02:00
Modifier.update_player_modifiers(player)
2020-05-11 21:04:13 +02:00
if fullness >= 0.9 and fullness <= 0.901 then
player.print('Maybe you should drop some of that inventory to lessen the burden.', Color.red)
2020-04-26 19:36:58 +02:00
end
2019-03-13 18:09:06 +02:00
end
2021-03-24 21:14:55 +02:00
local function on_init()
2019-03-13 18:09:06 +02:00
script.on_event(defines.events.on_player_main_inventory_changed, check_burden)
end
2020-04-26 19:36:58 +02:00
Event.on_init(on_init)