From cbb5a2093bd24ec11d0cffd9f4ff53484061079a Mon Sep 17 00:00:00 2001 From: PHIDIAS Date: Tue, 28 May 2024 22:59:28 +0900 Subject: [PATCH] vlayer auto connect circuit (#291) * Update vlayer.lua * Update vlayer.lua * Update vlayer.lua * Update vlayer.lua * Update vlayer.lua * Update vlayer.lua * Update vlayer.lua * Update vlayer.lua * Update vlayer.lua * Update vlayer.lua --- modules/control/vlayer.lua | 30 +++++++++++++++++++++++++++--- modules/gui/vlayer.lua | 37 ++++++++++++++++++++----------------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/modules/control/vlayer.lua b/modules/control/vlayer.lua index ef2026d1..99321b03 100644 --- a/modules/control/vlayer.lua +++ b/modules/control/vlayer.lua @@ -269,7 +269,7 @@ end -- @tparam MapPosition position The position on the surface to place the interface at -- @tparam[opt] LuaPlayer player The player to show as the last user of the interface -- @treturn LuaEntity The entity that was created for the interface -function vlayer.create_input_interface(surface, position, last_user) +function vlayer.create_input_interface(surface, position, circuit, last_user) local interface = surface.create_entity{name='logistic-chest-storage', position=position, force='neutral'} table.insert(vlayer_data.entity_interfaces.storage_input, interface) @@ -277,6 +277,14 @@ function vlayer.create_input_interface(surface, position, last_user) interface.last_user = last_user end + if circuit then + for k, _ in pairs(circuit) do + for _, v in pairs(circuit[k]) do + interface.connect_neighbour({wire=defines.wire_type[k], target_entity=v}) + end + end + end + interface.destructible = false interface.minable = false interface.operable = true @@ -318,7 +326,7 @@ end -- @tparam MapPosition position The position on the surface to place the interface at -- @tparam[opt] LuaPlayer player The player to show as the last user of the interface -- @treturn LuaEntity The entity that was created for the interface -function vlayer.create_output_interface(surface, position, last_user) +function vlayer.create_output_interface(surface, position, circuit, last_user) local interface = surface.create_entity{name='logistic-chest-requester', position=position, force='neutral'} table.insert(vlayer_data.entity_interfaces.storage_output, interface) @@ -326,6 +334,14 @@ function vlayer.create_output_interface(surface, position, last_user) interface.last_user = last_user end + if circuit then + for k, _ in pairs(circuit) do + for _, v in pairs(circuit[k]) do + interface.connect_neighbour({wire=defines.wire_type[k], target_entity=v}) + end + end + end + interface.destructible = false interface.minable = false interface.operable = true @@ -432,7 +448,7 @@ end -- @tparam MapPosition position The position on the surface to place the interface at -- @tparam[opt] LuaPlayer player The player to show as the last user of the interface -- @treturn LuaEntity The entity that was created for the interface -function vlayer.create_circuit_interface(surface, position, last_user) +function vlayer.create_circuit_interface(surface, position, circuit, last_user) local interface = surface.create_entity{name='constant-combinator', position=position, force='neutral'} table.insert(vlayer_data.entity_interfaces.circuit, interface) @@ -440,6 +456,14 @@ function vlayer.create_circuit_interface(surface, position, last_user) interface.last_user = last_user end + if circuit then + for k, _ in pairs(circuit) do + for _, v in pairs(circuit[k]) do + interface.connect_neighbour({wire=defines.wire_type[k], target_entity=v}) + end + end + end + interface.destructible = false interface.minable = false interface.operable = true diff --git a/modules/gui/vlayer.lua b/modules/gui/vlayer.lua index ef2b0a6c..19b5d474 100644 --- a/modules/gui/vlayer.lua +++ b/modules/gui/vlayer.lua @@ -55,6 +55,7 @@ local function vlayer_convert_chest(player) local entity = entities[1] local pos = entity.position + local circuit = entity.circuit_connected_entities if (not entity.get_inventory(defines.inventory.chest).is_empty()) then player.print('Chest is not emptied') @@ -62,7 +63,7 @@ local function vlayer_convert_chest(player) end entity.destroy() - return {x=string.format('%.1f', pos.x), y=string.format('%.1f', pos.y)} + return {pos={x=string.format('%.1f', pos.x), y=string.format('%.1f', pos.y)}, circuit=circuit} end --- Display label for the number of solar panels @@ -234,11 +235,11 @@ Gui.element{ }:style{ width = 160 }:on_click(function(player, element, _) - local pos = vlayer_convert_chest(player) + local res = vlayer_convert_chest(player) - if (pos) then - vlayer.create_input_interface(player.surface, pos, player) - game.print(player.name .. ' built a vlayer storage input on ' .. pos_to_gps_string(pos)) + if res then + vlayer.create_input_interface(player.surface, res.pos, res.circuit, player) + game.print(player.name .. ' built a vlayer storage input on ' .. pos_to_gps_string(res.pos)) end element.enabled = (vlayer.get_interface_counts().storage_input < config.interface_limit.storage_input) @@ -254,11 +255,11 @@ Gui.element{ }:style{ width = 160 }:on_click(function(player, element, _) - local pos = vlayer_convert_chest(player) + local res = vlayer_convert_chest(player) - if (pos) then - vlayer.create_output_interface(player.surface, pos, player) - game.print(player.name .. ' built a vlayer storage output on ' .. pos_to_gps_string(pos)) + if res then + vlayer.create_output_interface(player.surface, res.pos, res.circuit, player) + game.print(player.name .. ' built a vlayer storage output on ' .. pos_to_gps_string(res.pos)) end element.enabled = (vlayer.get_interface_counts().storage_output < config.interface_limit.storage_output) @@ -274,11 +275,11 @@ Gui.element{ }:style{ width = 160 }:on_click(function(player, element, _) - local pos = vlayer_convert_chest(player) + local res = vlayer_convert_chest(player) - if (pos) then - vlayer.create_circuit_interface(player.surface, pos, player) - game.print(player.name .. ' built a vlayer circuit on ' .. pos_to_gps_string(pos)) + if res then + vlayer.create_circuit_interface(player.surface, res.pos, res.circuit, player) + game.print(player.name .. ' built a vlayer circuit on ' .. pos_to_gps_string(res.pos)) end element.enabled = (vlayer.get_interface_counts().circuit < config.interface_limit.circuit) @@ -294,11 +295,12 @@ Gui.element{ }:style{ width = 160 }:on_click(function(player, element, _) - local pos = vlayer_convert_chest(player) + local res = vlayer_convert_chest(player) + + if res then + if vlayer.create_energy_interface(player.surface, res.pos, player) then + game.print(player.name .. ' built a vlayer energy interface on ' .. pos_to_gps_string(res.pos)) - if (pos) then - if vlayer.create_energy_interface(player.surface, pos, player) then - game.print(player.name .. ' built a vlayer energy interface on ' .. pos_to_gps_string(pos)) else player.print('Unable to build vlayer energy entity') end @@ -318,6 +320,7 @@ Gui.element{ width = 160 }:on_click(function(player, element, _) local interface_type, interface_position = vlayer.remove_closest_interface(player.surface, player.position, 4) + if not interface_type then return player.print('Interface not found in range, please move closer') end