1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-04 09:42:30 +02:00
RedMew/features/battery_charge.lua
RedRafe 26e1c28dc0
Factorio 2.0 update (#1436)
* Init Factorio 2.0 update

* add credits

* fix test module

* I know luackeck, I know

* Fixes

* Fix bad event.player_index handling

* Hotfixes

* Remove all filter inserters

* Migrate removed items

* Deprecating spidertron control and landfill features
2024-10-22 20:22:35 +01:00

100 lines
2.9 KiB
Lua

-- Charge your armor equipment from nearby accumulators!
-- made by Hanakocz
-- modified by RedRafe
-- source: https://github.com/ComfyFactory/ComfyFactorio/blob/develop/modules/charging_station.lua
-- ======================================================= --
local Color = require 'resources.color_presets'
local Global = require 'utils.global'
local this = {
radius = 13
}
Global.register(this, function(tbl) this = tbl end)
local Public = {}
local function discharge_accumulators(surface, position, force, power_needs)
local accumulators = surface.find_entities_filtered {
type = 'accumulator',
force = force,
position = position,
radius = this.radius,
}
local power_drained = 0
power_needs = power_needs * 1
for _, accu in pairs(accumulators) do
if accu.valid then
if accu.energy > 3000000 and power_needs > 0 then
if power_needs >= 2000000 then
power_drained = power_drained + 2000000
accu.energy = accu.energy - 2000000
power_needs = power_needs - 2000000
else
power_drained = power_drained + power_needs
accu.energy = accu.energy - power_needs
end
elseif power_needs <= 0 then
break
end
end
end
return power_drained / 1
end
function Public.recharge(player)
if not player.character then
player.print({'battery_charge.err_no_character'}, {color = Color.warning})
return
end
local armor_inventory = player.get_inventory(defines.inventory.character_armor)
if not armor_inventory.valid then
player.print({'battery_charge.err_no_armor'}, {color = Color.warning})
return
end
local armor = armor_inventory[1]
if not armor.valid_for_read then
player.print({'battery_charge.err_no_armor'}, {color = Color.warning})
return
end
local grid = armor.grid
if not grid or not grid.valid then
player.print({'battery_charge.err_no_armor'}, {color = Color.warning})
return
end
local entities = player.physical_surface.find_entities_filtered {
type = 'accumulator',
force = player.force,
position = player.physical_position,
radius = this.radius,
}
if not entities or not next(entities) then
player.print({'battery_charge.err_no_accumulators'}, {color = Color.warning})
return
end
local equip = grid.equipment
for _, piece in pairs(equip) do
if piece.valid and piece.generator_power == 0 then
local energy_needs = piece.max_energy - piece.energy
if energy_needs > 0 then
local energy = discharge_accumulators(player.physical_surface, player.physical_position, player.force, energy_needs)
if energy > 0 then
if piece.energy + energy >= piece.max_energy then
piece.energy = piece.max_energy
else
piece.energy = piece.energy + energy
end
end
end
end
end
end
function Public.radius(value)
this.radius = value or 13
end
return Public