Underground pipe click and drag auto place help
Posted: Mon Jan 22, 2024 3:05 am
I am currently developing a mod and I need to modify the behavior of underground pipe placement. The goal of this the script is to prevent the automatic placement of a second adjacent underground pipe when the player uses the click-and-drag feature. Ideally, when a player drags to place underground pipes, only one pipe should be placed instead of two. My current attempt has been to let the second pipe entity get placed, then remove it and add it back to the players inventory. If anyone knows how to make this work, please let me know.
Here is my current controls.lua code:
-- Global table to keep track of the last placed pipe
global.last_placed_pipe = global.last_placed_pipe or {}
-- Function to check if two positions are adjacent
local function are_adjacent(pos1, pos2)
return (math.abs(pos1.x - pos2.x) == 1 and pos1.y == pos2.y) or
(math.abs(pos1.y - pos2.y) == 1 and pos1.x == pos2.x)
end
-- Event handler for when an entity is built
script.on_event(defines.events.on_built_entity, function(event)
local entity = event.created_entity
local player_index = event.player_index
local player = game.players[player_index]
-- Check if the feature is enabled for this player
if player.mod_settings["double-pipe-replacement-EnableSingleUnderground"].value then
-- Check if the built entity is "pipe-to-ground"
if entity and entity.valid and entity.name == "pipe-to-ground" then
local last_pipe = global.last_placed_pipe[player_index]
-- Check if there was a previously placed pipe and if they are adjacent
if last_pipe and last_pipe.valid and are_adjacent(last_pipe.position, entity.position) then
-- If they are adjacent, remove the newly placed pipe and add it back to inventory
entity.destroy()
player.insert({name = entity.name, count = 1})
player.print("Placing multiple adjacent underground pipes is not allowed.")
return
else
-- If not adjacent or no previous pipe, update the last placed pipe
global.last_placed_pipe[player_index] = entity
end
end
end
end)
Here is my current controls.lua code:
-- Global table to keep track of the last placed pipe
global.last_placed_pipe = global.last_placed_pipe or {}
-- Function to check if two positions are adjacent
local function are_adjacent(pos1, pos2)
return (math.abs(pos1.x - pos2.x) == 1 and pos1.y == pos2.y) or
(math.abs(pos1.y - pos2.y) == 1 and pos1.x == pos2.x)
end
-- Event handler for when an entity is built
script.on_event(defines.events.on_built_entity, function(event)
local entity = event.created_entity
local player_index = event.player_index
local player = game.players[player_index]
-- Check if the feature is enabled for this player
if player.mod_settings["double-pipe-replacement-EnableSingleUnderground"].value then
-- Check if the built entity is "pipe-to-ground"
if entity and entity.valid and entity.name == "pipe-to-ground" then
local last_pipe = global.last_placed_pipe[player_index]
-- Check if there was a previously placed pipe and if they are adjacent
if last_pipe and last_pipe.valid and are_adjacent(last_pipe.position, entity.position) then
-- If they are adjacent, remove the newly placed pipe and add it back to inventory
entity.destroy()
player.insert({name = entity.name, count = 1})
player.print("Placing multiple adjacent underground pipes is not allowed.")
return
else
-- If not adjacent or no previous pipe, update the last placed pipe
global.last_placed_pipe[player_index] = entity
end
end
end
end)