Page 1 of 1

Merge signals read from get_circuit_network

Posted: Wed Nov 09, 2016 1:27 pm
by Optera
I'm looking for a fast way to merge signals read with get_circuit_network(defines.wire_type.green) and get_circuit_network(defines.wire_type.red).
Running recursive for loops is not what i want to do in on_tick handling just to get a complete list of signals.

It's a shame we don't have defines.wire_type.all :(

Re: Merge signals read from get_circuit_network

Posted: Thu Nov 10, 2016 8:06 pm
by aubergine18
It would help to know what you intend to do with the merged results.

Merging in any manner would be relatively slow compared to just iterating the results of both wires.

Re: Merge signals read from get_circuit_network

Posted: Thu Nov 10, 2016 8:30 pm
by Optera
What I want is having all signals merged into one array.
Currently I'm doing it like this.

Code: Select all

function GetCircuitValues(entity) 
  local greenWire = entity.get_circuit_network(defines.wire_type.green)
  local redWire =  entity.get_circuit_network(defines.wire_type.red)
  local items = {} 
  if greenWire then
    for _, v in pairs (greenWire.signals) do
      if v.signal.type == "item" then
        items[v.signal.name] = v.count
      end
    end
  end
  if redWire then
    for _, v in pairs (redWire.signals) do 
      if v.signal.type == "item" then
        if items[v.signal.name] ~= nil then
          items[v.signal.name] = items[v.signal.name] + v.count
        else
          items[v.signal.name] = v.count
        end
      end
    end
  end
  return items
end
If we want to mod something that can handle inputs similar to combinators we'll need either an extension to the api, like get_circuit_network(defines.wire_type.all), or some really fast method of merging these two tables, as I can't imagine every combinator doing these two for each loops every tick.

Re: Merge signals read from get_circuit_network

Posted: Sun Nov 13, 2016 11:58 am
by Rseding91
Optera wrote:... as I can't imagine every combinator doing these two for each loops every tick.
That's what the game does :P

Re: Merge signals read from get_circuit_network

Posted: Sun Nov 13, 2016 3:57 pm
by Optera
Rseding91 wrote:
Optera wrote:... as I can't imagine every combinator doing these two for each loops every tick.
That's what the game does :P
Is that why feeding the whole content of the logistic network from roboports into several combinators causes network update to take noticeable more time?

Re: Merge signals read from get_circuit_network

Posted: Sun Nov 13, 2016 6:33 pm
by aubergine18
Can't see many ways to make it faster via Lua, other than reducing number of table lookups and variables:

Code: Select all

local red                  , green
    = defines.wire_type.red, defines.wire_type.green

function GetCircuitValues( entity )

  local items, wire
      = {}   , entity.get_circuit_network( green )

  if wire then
    for _, data in pairs( wire.signals ) do
      if data.signal.type == "item" then
        items[data.signal.name] = data.count
      end
    end
  end

  wire = entity.get_circuit_network( red )

  if wire then
    local value
    for _, data in pairs (wire.signals) do 
      if data.signal.type == "item" then
        value = items[data.signal.name]
        items[data.signal.name] = value and (value + data.count) or data.count
      end
    end
  end

  return items
end