1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-10 00:43:27 +02:00
ComfyFactorio/maps/scrapyard/burden.lua
2020-04-28 21:55:19 +02:00

58 lines
1.9 KiB
Lua

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)
if fullness >= 0.5 and fullness <= 0.51 then
player.print("You feel all of a sudden burden.", Color.yellow)
elseif fullness >= 0.9 and fullness <= 0.91 then
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)