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)
Underground pipe click and drag auto place help
Re: Underground pipe click and drag auto place help
Yes, this is the only way to undo a building.
Here are a couple more examples: viewtopic.php?f=34&t=70889
Here are a couple more examples: viewtopic.php?f=34&t=70889
Re: Underground pipe click and drag auto place help
Thanks for the link. I went ahead and updated my script to remove a placed entity like you linked, but it still doesn't seem to work. I guess I don't really understand how the drag to build works when it comes to underground pipes/belts. Here is my current script:DaveMcW wrote: ↑Mon Jan 22, 2024 5:31 am Yes, this is the only way to undo a building.
Here are a couple more examples: viewtopic.php?f=34&t=70889
-- 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
-- Function to refund entity
local function refund_entity(entity, player)
if entity.prototype.items_to_place_this then
local item = entity.prototype.items_to_place_this[1]
if item then
local health = entity.health / entity.prototype.max_health
local result = player.insert{name=item.name, count=item.count, health=health}
local remaining_count = item.count - result
if remaining_count > 0 then
entity.surface.spill_item_stack(entity.position, {name=item.name, count=remaining_count, health=health}, false, entity.force, false)
end
end
end
entity.destroy{raise_destroy = true}
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["afh-double-pipe-replacement-EnableSingleUnderground"].value then
-- Check if the built entity is the standard "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
-- Refund the player and remove the pipe
refund_entity(entity, player)
return
else
-- Update the last placed pipe
global.last_placed_pipe[player_index] = entity
end
end
end
end)
Re: Underground pipe click and drag auto place help
Your code works for me. Do you not like the 1-tile space between pipes?
Re: Underground pipe click and drag auto place help
Hmm. For me, it's still placing 2 underground pipes when I click and drag. Maybe I'm missing something in my setup. My mods intention is to turn an underground pipe into one pipe instead of two. Similar to AFH underground-I-Pipe. Except their underground-I-Pipe also has the click and drag double pipe issue.