Skip to content

Commit

Permalink
Move factory script to own file + request construction materials
Browse files Browse the repository at this point in the history
  • Loading branch information
Quezler committed Jan 14, 2024
1 parent 2763bac commit 883c57a
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 79 deletions.
113 changes: 34 additions & 79 deletions mods/factoryplanner-is-exportable-to-fake-factorissimo/control.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local print_gui = require('scripts.print_gui')
local shared = require('shared')
local Factory = require('scripts.factory')

local mod_prefix = 'fietff-'

Expand Down Expand Up @@ -159,6 +159,20 @@ script.on_event(defines.events.on_gui_click, function(event)
local recipe_class, recipe_name = split_class_and_name(table.children[offset + columns['fp.pu_recipe']].children[2].sprite)
local machine_class, machine_name = split_class_and_name(table.children[offset + columns['fp.pu_machine']].children[1].sprite)
local machine_count = math.ceil(table.children[offset + columns['fp.pu_machine']].children[1].number)

local machine_prototype = game.entity_prototypes[machine_name]
local item_to_place_this = machine_prototype.items_to_place_this[1]
if item_to_place_this == nil then
return player.create_local_flying_text{
text = string.format("The %s machine has no building materials.", machine_name),
create_at_cursor = true,
}
else
machine_class = "item"
machine_name = item_to_place_this.name
machine_count = machine_count * (item_to_place_this.count or 1)
end

merge_ingredients(clipboard.buildings, {type = machine_class, name = machine_name, amount = machine_count})

local modules = {}
Expand Down Expand Up @@ -282,91 +296,14 @@ script.on_event(defines.events.on_gui_click, function(event)
log(serpent.block(clipboard, {sortkeys = false}))
end)

local function on_created_entity(event)
local entity = event.created_entity or event.entity or event.destination

if event.player_index == nil then
game.print(string.format('The %s should only be built by players.', entity.name))
entity.destroy()
end

local player = game.get_player(event.player_index)
local clipboard = global.clipboards[player.index]
if not clipboard then
entity.destroy()
return player.create_local_flying_text{
text = "Grab a new one from the factory planner gui.",
create_at_cursor = true,
}
else
global.clipboards[player.index] = nil -- mark clipboard as consumed
end

local assembler = entity.surface.create_entity{
name = mod_prefix .. 'assembling-machine-1',
force = entity.force,
position = {entity.position.x, entity.position.y},
}
assembler.destructible = false

local eei = entity.surface.create_entity{
name = mod_prefix .. 'electric-energy-interface-1',
force = entity.force,
position = {entity.position.x, entity.position.y + shared.electric_energy_interface_1_y_offset},
}
eei.destructible = false

eei.power_usage = clipboard.watts / 60
eei.electric_buffer_size = math.max(1, clipboard.watts) -- buffer for 1 second

rendering.draw_text{
text = clipboard.factory_name,
color = {1, 1, 1},
surface = entity.surface,
target = entity,
target_offset = {0, -3.75},
alignment = "center",
use_rich_text = true,
}

local io_string = ""

for _, product in ipairs(clipboard.products) do
io_string = io_string .. string.format('[%s=%s]', product.type, product.name)
end
io_string = io_string .. ' - '

if #clipboard.byproducts > 0 then
for _, byproduct in ipairs(clipboard.byproducts) do
io_string = io_string .. string.format('[%s=%s]', byproduct.type, byproduct.name)
end
io_string = io_string .. ' - '
end

for _, ingredient in ipairs(clipboard.ingredients) do
io_string = io_string .. string.format('[%s=%s]', ingredient.type, ingredient.name)
end

rendering.draw_text{
text = io_string,
color = {1, 1, 1},
surface = entity.surface,
target = entity,
target_offset = {0, -3.00},
alignment = "center",
use_rich_text = true,
scale = 0.5,
}
end

for _, event in ipairs({
defines.events.on_built_entity,
defines.events.on_robot_built_entity,
defines.events.script_raised_built,
defines.events.script_raised_revive,
defines.events.on_entity_cloned,
}) do
script.on_event(event, on_created_entity, {
script.on_event(event, Factory.on_created_entity, {
{filter = 'name', name = mod_prefix .. 'container-1'},
{filter = 'name', name = mod_prefix .. 'container-2'},
{filter = 'name', name = mod_prefix .. 'container-3'},
Expand All @@ -389,7 +326,25 @@ local function on_configuration_changed(event)
global.can_be_unbarreled[fluid_name] = true
end
end

global.structs = global.structs or {}
global.deathrattles = global.deathrattles or {}
end

script.on_init(on_configuration_changed)
script.on_configuration_changed(on_configuration_changed)

script.on_nth_tick(600, function(event)
for unit_minute, struct in pairs(global.structs) do
Factory.tick_struct(struct)
end
end)

script.on_event(defines.events.on_entity_destroyed, function(event)
local deathrattle = global.deathrattles[event.registration_number]
if deathrattle then global.deathrattles[event.registration_number] = nil
for _, entity in ipairs(deathrattle) do
entity.destroy()
end
end
end)
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ local assembling_machine = {
flags = {
'not-on-map',
'hide-alt-info',
'no-automated-item-removal',
},

collision_box = {{-3.8, -3.8}, {3.8, 3.8}},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
local shared = require('shared')

local mod_prefix = 'fietff-'
local Factory = {}

function Factory.on_created_entity(event)
local entity = event.created_entity or event.entity or event.destination

if event.player_index == nil then
game.print(string.format('The %s should only be built by players.', entity.name))
entity.destroy()
return
end

local player = game.get_player(event.player_index)
local clipboard = global.clipboards[player.index]
if not clipboard then
entity.destroy()
return player.create_local_flying_text{
text = "Grab a new one from the factory planner gui.",
create_at_cursor = true,
}
else
global.clipboards[player.index] = nil -- mark clipboard as consumed
end

local assembler = entity.surface.create_entity{
name = mod_prefix .. 'assembling-machine-1',
force = entity.force,
position = {entity.position.x, entity.position.y},
}
assembler.destructible = false

local eei = entity.surface.create_entity{
name = mod_prefix .. 'electric-energy-interface-1',
force = entity.force,
position = {entity.position.x, entity.position.y + shared.electric_energy_interface_1_y_offset},
}
eei.destructible = false

eei.power_usage = clipboard.watts / 60
eei.electric_buffer_size = math.max(1, clipboard.watts) -- buffer for 1 second

rendering.draw_text{
text = clipboard.factory_name,
color = {1, 1, 1},
surface = entity.surface,
target = entity,
target_offset = {0, -3.75},
alignment = "center",
use_rich_text = true,
}

local io_string = ""

for _, product in ipairs(clipboard.products) do
io_string = io_string .. string.format('[%s=%s]', product.type, product.name)
end
io_string = io_string .. ' - '

if #clipboard.byproducts > 0 then
for _, byproduct in ipairs(clipboard.byproducts) do
io_string = io_string .. string.format('[%s=%s]', byproduct.type, byproduct.name)
end
io_string = io_string .. ' - '
end

for _, ingredient in ipairs(clipboard.ingredients) do
io_string = io_string .. string.format('[%s=%s]', ingredient.type, ingredient.name)
end

rendering.draw_text{
text = io_string,
color = {1, 1, 1},
surface = entity.surface,
target = entity,
target_offset = {0, -3.00},
alignment = "center",
use_rich_text = true,
scale = 0.5,
}

global.structs[entity.unit_number] = {
unit_number = entity.unit_number,
container = entity,

assembler = assembler,
eei = eei,

clipboard = clipboard,
}

Factory.tick_struct(global.structs[entity.unit_number])

global.deathrattles[script.register_on_entity_destroyed(entity)] = {assembler, eei}
end

function Factory.tick_struct(struct)
if struct.container.valid == false then
global.structs[struct.unit_number] = nil
return
end

local inventory = struct.container.get_inventory(defines.inventory.chest)
local inventory_contents = inventory.get_contents()

local proxy_requests = {}

-- game.print(serpent.line(struct.clipboard.buildings))
for _, building in ipairs(struct.clipboard.buildings) do
local missing = building.amount - (inventory_contents[building.name] or 0)
if missing > 0 then
proxy_requests[building.name] = missing
end
end

if table_size(proxy_requests) > 0 then
local surface = struct.container.surface
local proxy = surface.find_entity('item-request-proxy', struct.container.position)
if proxy then
proxy.item_requests = proxy_requests
else
struct.proxy = surface.create_entity{
name = 'item-request-proxy',
force = struct.container.force,
position = struct.container.position,
modules = proxy_requests,
target = struct.container,
}
end
return -- wait for the factory to be constructed
elseif struct.proxy then
game.print('construction complete.')
struct.proxy.destroy()
struct.proxy = nil
end

end

return Factory

0 comments on commit 883c57a

Please sign in to comment.