it would be nice to have an event that triggers when a wire is (manually) placed, right now the only event that activates when you place a wire is on_player_cursor_stack_changed, which doesn't let you distinguish between whether you put an item in a chest or whether you made a wire.
Re: on_wire_placed
Posted: Mon May 22, 2017 1:25 pm
by darkfrei
And also wire cutting with wire tool.
Re: on_wire_placed
Posted: Tue Nov 07, 2017 8:45 pm
by Rseding91
What's the utility of such an event?
Re: on_wire_placed
Posted: Tue Nov 21, 2017 1:44 pm
by Mooncat
Rseding91 wrote:What's the utility of such an event?
Automatically refill wire once you have used one.
Player used a wire -> on_wire_placed -> mod inserts a wire to the player.
Or it should be named on_player_placed/used/connected_wire for consistency.
Re: on_wire_placed
Posted: Thu Nov 23, 2017 4:24 pm
by sparr
I was working on a mod that needed to know when circuit connections changed. This would get closer to that.
Re: on_wire_placed
Posted: Thu Nov 23, 2017 7:52 pm
by quyxkh
I think a "circuit change counter" readout in LuaCircuitNetwork could be good, update the counter every time a connection is made or broken on that network id and let mods check the change counter against their cache.
Here's my utils/circuit.lua that allows things like `for e in iterate_network(tsrec.stop,'red')`:
-- circuit-handling utilities
--
function ckey(e,wire,post)
local pos=e.position
return pos.x/2^21+pos.y/2^42 + (post or 0)/2^44 + (wire or 0)/2^46
end
--
function iterate_copper_network(e)
local ret, seen = {e}, {[ckey(e)]=1}
return function()
local r,k = ret[#ret] ret[#ret]=nil
if r then
for _,n in next,r.neighbours.copper do
k = ckey(n)
if not seen[k] then seen[k]=1 ret[#ret+1]=n end
end
end
return r
end
end
-- arith/logic output is somewhat arbitrarily default initial exit
circuit={['arithmetic-combinator']=2, ['decider-combinator']=2}
function iterate_network(e,w)
if w=='copper' then return iterate_copper_network(e) end
local wire = defines.wire_type[w]
local ret, seen = {{e=e, post=circuit[e.type] or 1}}, {[ckey(e,wire,post)]=1}
return function()
local r,k = ret[#ret] ret[#ret]=nil
if r then
for _,n in next,r.e.circuit_connection_definitions or {} do
if n.wire == wire and n.source_circuit_id == r.post then
local e,post = n.target_entity, n.target_circuit_id
k = ckey(e,wire,post)
if not seen[k] then
seen[k]=1
ret[#ret+1] = {e=e, post=n.target_circuit_id}
end
end
end
return r.e
end
return nil
end
end
-- ease dealing with circuit signals
function read_signals(e,w,c)
local r = {item={}, fluid={}, virtual={}}
local n = e.get_circuit_network(
defines.wire_type[w], c or circuit[e.type] or 1)
for _,s in next,n and n.signals or {} do
r[s.signal.type][s.signal.name] = s.count
end
return r
end
I only run it when a train leaves a station, and in that use for example the ~2.5ms I think it costs to trace out a circuit connecting a 10-wagon train's buffer chests is truly negligible , but so far as I can tell there's no way to find out whether anything's changed than simply reexamining everything, every time. That cost is for `/c gps=game.player.selected script.on_event(defines.events.on_tick,function(ev) for e in iterate_network(gps,'red') do _=e.get_inventory(1) end end)` with that iterator available.
Re: on_wire_placed
Posted: Fri Jan 05, 2018 7:03 pm
by Therax
Here's my use case:
Miniloader uses two invisible inserters, one for each lane of output. To make them circuit controllable, the two inserters need to be wired to each other, but to avoid rendering the circuit connector when the miniloader isn't connected to anything, the inserters can't be connected to each other all the time. In other words, when the miniloader is connected to an entity via circuit, I must script connecting the two inserters to each other and the external network, and vice versa when the miniloader is disconnected.
My current workaround: watch the cursor stack for when the player is holding red/green wire. Then watch the players selection, and poll circuit_connected_entities every tick to see if the player has connected/disconnected the entity. I have to do this on every entity, because the connection isn't created until the player clicks the second entity of a connection. If there were any event for circuit wire use (connect/disconnect) or an event for circuit connection created/removed, I could avoid on_tick polling. I expect polling circuit_connected_entites can't be cheap in larger bases.
Re: on_wire_placed
Posted: Sun Jan 14, 2018 3:12 am
by Reika
Rseding91 wrote:What's the utility of such an event?
How about a cached entity in the global table which is to be connected to a vanilla/other-mod entity to read/control, but wants to allow the player to specify connection? Right now the only alternative is to cache the unconnected entity in a "pending" state and recheck connections every few seconds.
I have an entity exactly like this - connect it via wire to a chest and it starts manipulating the chest. Until then, it sits idle, waiting for this connection.
Since I needed the functionality for Miniloader, I refactored it into a reusable library, found here. It'll also need a few functions from blueprint.lua, and due to needing to hook so many events, it uses a little event dispatcher in event.lua.
It's about 300 lines of Lua, and uses the following Factorio events:
on_built_entity
on_robot_built_entity
on_player_mined_entity
on_robot_mined_entity
on_entity_died
on_player_cursor_stack_changed
on_selected_entity_changed
on_put_item
on_tick (polled 4 times/second, only when a player is holding a red or green wire over an entity)
Next step is to clean it up, improve the documentation, and see if I can get it into Aforess' stdlib project. This would still be a lot easier with an actual Factorio event, especially to avoid the polling.
Re: on_wire_placed
Posted: Sat May 11, 2019 1:20 pm
by lebastaq
Hi Therax,
I'm sorry to post on this old thread, but I can't contact you by message on Github nor on the forums.
I'm making a mod that changes electricity management, to make it more realistic.
Each entity has a voltage_in, voltage_out value.
To limit the number of computations, I wish to compute these values only on electric network changes - eg. when entities are connected or disconnected to a network.
While searching around IĀ found your code on github and this thread. Using your lualib would be most useful as it would save me some time and errors (I'm new at modding).
Can I use your library ?
Thanks
Best regards
Re: on_wire_placed
Posted: Sat Sep 11, 2021 9:51 am
by Stringweasel
+1
My use case is preventing players from connecting power poles with copper cable in Fluidic Power, by destroying the connection as soon as it's made. (The mod expects only have pipe connections between poles, and not electric connections as well)