Underground pipe click and drag auto place help

Place to get help with not working mods / modding interface.
Post Reply
Rhoadesn
Manual Inserter
Manual Inserter
Posts: 4
Joined: Thu May 18, 2023 12:23 am
Contact:

Underground pipe click and drag auto place help

Post by Rhoadesn »

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)

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Underground pipe click and drag auto place help

Post by DaveMcW »

Yes, this is the only way to undo a building.

Here are a couple more examples: viewtopic.php?f=34&t=70889

Rhoadesn
Manual Inserter
Manual Inserter
Posts: 4
Joined: Thu May 18, 2023 12:23 am
Contact:

Re: Underground pipe click and drag auto place help

Post by Rhoadesn »

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
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:

-- 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)

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Underground pipe click and drag auto place help

Post by DaveMcW »

Your code works for me. Do you not like the 1-tile space between pipes?

Rhoadesn
Manual Inserter
Manual Inserter
Posts: 4
Joined: Thu May 18, 2023 12:23 am
Contact:

Re: Underground pipe click and drag auto place help

Post by Rhoadesn »

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.

Post Reply

Return to “Modding help”