Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require "prototypes/recipe-categories"
require "prototypes/fuel-categories"
require "prototypes/module-categories"
require "prototypes/circuit-connector-definitions"
require "prototypes/farming-utils"
require "prototypes/keyboard-shortcuts"

-- Increase empty barrel stack size in order to prevent inserter deadlocks. https://github.com/pyanodon/pybugreports/issues/314
Expand Down
90 changes: 90 additions & 0 deletions migrations/farm-optimizations.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
storage.farm_buildings = storage.farm_buildings or {}
storage.farming_deathrattles = storage.farming_deathrattles or {}

local function base_name(name)
-- TODO: Find a method that avoids two searches?
local is_turd = not not name:find("%-turd")
-- keep suffix if necessary while allowing other building suffixes
return name:gsub("%-mk..+", is_turd and "-turd" or "")
end

-- animal, plant, or fungi?
local function get_kingdom(entity)
local farm_data = storage.farm_prototypes[base_name(entity.name)]
if farm_data then return farm_data.domain end
end

local function get_default_module(entity)
local farm_data = storage.farm_prototypes[base_name(entity.name)]
if farm_data then return farm_data.default_module end
end

local function register_sacrifice(manager, farm)
manager.get_inventory(defines.inventory.crafter_input).insert {
name = "pyfarm-internal-item",
count = 1,
health = 0.5,
}
storage.farming_deathrattles[script.register_on_object_destroyed(manager.get_inventory(defines.inventory.crafter_input)[1].item)] = farm.unit_number
end

for _, metadata in pairs {storage.enabled_farm_buildings, storage.disabled_farm_buildings} do
for _, entity in pairs(metadata) do
if entity.valid and storage.farm_prototypes[base_name(entity.name)] then
local default_module = get_default_module(entity)

local manager = entity.surface.create_entity {
name = "pyfarm-internal-manager",
position = entity.position,
force = entity.force
}
local monitor = entity.surface.create_entity {
name = "pyfarm-internal-monitor",
position = entity.position,
force = entity.force
}
local warning

-- connect source, manager, mimic, and (?) monitor
manager.get_wire_connector(defines.wire_connector_id.circuit_green, true).connect_to(monitor.get_wire_connector(defines.wire_connector_id.circuit_green, true), false, defines.wire_origin.script)

local active = not entity.get_module_inventory().is_empty()
entity.active = active
entity.custom_status = not active and {
diode = defines.entity_status_diode.red,
label = default_module and {"entity-status.requires-module", default_module, prototypes.item[default_module].localised_name} or {"entity-status.requires-module-reproductive-complex"}
} or nil

-- set circuit settings
local manager_behaviour = manager.get_or_create_control_behavior()
manager_behaviour.circuit_enable_disable = true
manager_behaviour.circuit_condition = {
comparator = active and "=" or "≠",
constant = 0,
first_signal = {name = active and "signal-everything" or "signal-anything", type = "virtual"}
}

monitor.proxy_target_entity = entity
monitor.proxy_target_inventory = defines.inventory.crafter_modules

-- update warning icon and crafting progress
if not active then
warning = py.draw_error_sprite(entity, "no_module_" .. get_kingdom(entity), 0, 30)
if entity.is_crafting() then
entity.crafting_progress = 0.0001
entity.bonus_progress = 0
end
end

-- save data and register event
script.register_on_object_destroyed(entity)
storage.farm_buildings[entity.unit_number] = {farm = entity, manager = manager, monitor = monitor, warning = warning}
register_sacrifice(manager, entity)
end
end
end

-- remove excess data
storage.enabled_farm_buildings = nil
storage.disabled_farm_buildings = nil
storage.next_farm_index = nil
74 changes: 74 additions & 0 deletions prototypes/farming-utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
data:extend { -- entities and things to manage farming buildings
{
type = "recipe-category",
name = "pyfarm-filler-category",
hidden = true,
hidden_in_factoriopedia = true
},
{ -- hidden recipe used to check if machine is working
type = "recipe",
name = "pyfarm-internal-recipe",
icon = util.empty_icon().icon,
category = "pyfarm-filler-category",
ingredients = {{type = "item", name = "pyfarm-internal-item", amount = 1, ignored_by_stats = 1}},
hidden = true,
hidden_in_factoriopedia = true
},
{ -- hidden item for recipe and signals, can use existing item but this one is garunteed to work
type = "item",
name = "pyfarm-internal-item",
icon = util.empty_icon().icon,
stack_size = 1,
hidden = true,
hidden_in_factoriopedia = true
},
{ -- hidden assembling machine to craft the aforementioned recipe
type = "assembling-machine",
name = "pyfarm-internal-manager",
icon = util.empty_icon().icon,
collision_mask = {layers = {}},
flags = {
"placeable-off-grid",
"not-repairable",
"not-on-map",
"not-blueprintable",
"not-deconstructable",
"no-copy-paste",
"not-upgradable",
"placeable-neutral",
"no-automated-item-removal",
"no-automated-item-insertion"
},
allow_copy_paste = false,
selectable_in_game = false,
energy_usage = "1W",
energy_source = {type = "void"},
crafting_categories = {"pyfarm-filler-category"},
fixed_recipe = "pyfarm-internal-recipe",
crafting_speed = 60,
hidden = true,
hidden_in_factoriopedia = true
},
{ -- hidden proxy container to monitor module inventory
type = "proxy-container",
name = "pyfarm-internal-monitor",
icon = util.empty_icon().icon,
draw_inventory_content = false,
collision_mask = {layers = {}},
flags = {
"not-rotatable",
"placeable-neutral",
"placeable-off-grid",
"not-repairable",
"not-on-map",
"not-deconstructable",
"not-blueprintable",
"hide-alt-info",
"not-upgradable"
},
allow_copy_paste = false,
selectable_in_game = false,
hidden = true,
hidden_in_factoriopedia = true
}
}
Loading