2020-04-28 21:55:19 +02:00
|
|
|
local Event = require 'utils.event'
|
|
|
|
local Modifier = require "player_modifiers"
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
local function compute_fullness(player)
|
|
|
|
local inv = player.get_inventory(defines.inventory.character_main)
|
|
|
|
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_modifiers = Modifier.get_table()
|
|
|
|
local player = game.players[event.player_index]
|
|
|
|
validate_player(player)
|
|
|
|
local fullness = compute_fullness(player)
|
|
|
|
player_modifiers[player.index].character_running_speed_modifier["scrapyard"] = 0.3 - fullness
|
|
|
|
player_modifiers[player.index].character_mining_speed_modifier["scrapyard"] = 0.3 - fullness
|
|
|
|
Modifier.update_player_modifiers(player)
|
2020-04-30 11:44:57 +02:00
|
|
|
if fullness >= 0.3 and fullness <= 0.301 then
|
2020-04-28 21:55:19 +02:00
|
|
|
player.print("You feel all of a sudden burden.", Color.yellow)
|
2020-04-30 11:44:57 +02:00
|
|
|
elseif fullness >= 0.5 and fullness <= 0.501 then
|
2020-04-28 21:55:19 +02:00
|
|
|
player.print("Maybe you should drop some of that inventory to lessen the burden.", Color.red)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function on_init(event)
|
|
|
|
script.on_event(defines.events.on_player_main_inventory_changed, check_burden)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_load(event)
|
|
|
|
script.on_event(defines.events.on_player_main_inventory_changed, check_burden)
|
|
|
|
end
|
|
|
|
|
|
|
|
Event.on_init(on_init)
|
|
|
|
Event.on_load(on_load)
|