Page 1 of 1

[1.1.72][Lua] corrupt defines.circuit_connector_id

Posted: Fri Dec 02, 2022 9:31 pm
by Asynchron
Hi team,

I'm not completely sure if this is a bug since the api doesn't provide which values are contained in elements of defines.circuit_connector_id, however I've found something strange while working on a pet mod for myself. Given the test code in data section with no mod other than my own:

Code: Select all

function dump_test(self)
    for ____, e in ipairs({
        defines.circuit_connector_id.constant_combinator,
        defines.circuit_connector_id.container,
        defines.circuit_connector_id.programmable_speaker,
        defines.circuit_connector_id.rail_signal,
        defines.circuit_connector_id.rail_chain_signal,
        defines.circuit_connector_id.roboport,
        defines.circuit_connector_id.storage_tank,
        defines.circuit_connector_id.wall,
        defines.circuit_connector_id.electric_pole,
        defines.circuit_connector_id.inserter,
        defines.circuit_connector_id.lamp,
        defines.circuit_connector_id.combinator_input,
        defines.circuit_connector_id.combinator_output,
        defines.circuit_connector_id.offshore_pump,
        defines.circuit_connector_id.pump
    }) do
        log((("circuit_connector_id: " .. tostring(e)) .. ": ") .. tostring(true))
    end
    for ____, e in ipairs({
        defines.circuit_condition_index.inserter_circuit,
        defines.circuit_condition_index.inserter_logistic,
        defines.circuit_condition_index.lamp,
        defines.circuit_condition_index.arithmetic_combinator,
        defines.circuit_condition_index.decider_combinator,
        defines.circuit_condition_index.constant_combinator,
        defines.circuit_condition_index.offshore_pump,
        defines.circuit_condition_index.pump
    }) do
        log((("circuit_condition_index: " .. tostring(e)) .. ": ") .. tostring(true))
    end
end

dump_test(nil)
Following is being logged:

Code: Select all

1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 2: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:19: circuit_connector_id 1: true
1.617 Script @__factory-combinator__/data.lua:31: circuit_condition_index: 1: true
1.617 Script @__factory-combinator__/data.lua:31: circuit_condition_index: 2: true
1.617 Script @__factory-combinator__/data.lua:31: circuit_condition_index: 1: true
1.617 Script @__factory-combinator__/data.lua:31: circuit_condition_index: 1: true
1.618 Script @__factory-combinator__/data.lua:31: circuit_condition_index: 1: true
1.618 Script @__factory-combinator__/data.lua:31: circuit_condition_index: 1: true
1.618 Script @__factory-combinator__/data.lua:31: circuit_condition_index: 1: true
1.618 Script @__factory-combinator__/data.lua:31: circuit_condition_index: 1: true
Please notice that all of elements from these two enums are 1 except entries that match circuit_connector_id.combinator_output and circuit_condition_index.constant_combinator which are equal to 2.

Per my expectation each entry has to have different number, otherwise it is impossible to detect which network an entity has.

Best regards,
Alexandru.

Re: [1.1.72][Lua] corrupt defines.circuit_connector_id

Posted: Fri Dec 02, 2022 10:14 pm
by boskid
Almost all entities which can be connected to circuit network have one circuit connector ("Circuit connector" = set of 2 wire connectors to which you connect circuit wires, one for red wires and one for green wires). Only exception here is arithmetic combinator and decider combinator since they have separate circuit connector for input (id=1) of the signals and separate circuit connector for the output (id=2). There are no rules saying that all of those defines should be unique and it is easier for us to have them numbered from 1 so there are no issues here. Those values are not intended to detect which entity type it is, those are primarily named constants so you can avoid typing "1" everywhere that would become confusing if there are also other numeric parameters nearby.

As for circuit_condition, a circuit condition is an object you can see in the Circuit Control GUI as ([ Signal ] [<] [ Signal/Value ]). Conditions are a common interface between a circuit network which provides integer values and a control behavior which for control requires a boolean value. Circuit condition allows to define which integer signals from circuit network translate to which boolean value. Multiple entities have an option to Enable/Disable which is one of such conditions and in most cases it is one that gets circuit_condition_index of 1. Enable/Disable is a shared property of control behaviors that are based on GenericOnOffControlBehavior (Inserter, Lamp, MiningDrill, TrainStop, TransportBelt) and as such will have the Enable/Disable condition (actually it will be a pair of conditions: condition_index=1 for circuit condition and condition_index=2 for logistic condition). Maybe there are some lua defines missing for other entities that have logistic conditions, but still i will move this to not a bug because your bug report is specifically about having those values be unique, and there is nothing that says they should be unique here. If there would be any good use case for a control behavior that is not based on GenericOnOffControlBehavior (so the condition_index of 2 would be not already used) to have more circuit conditions, those conditions would be allowed to use circuit_condition_index of 2.

Re: [1.1.72][Lua] corrupt defines.circuit_connector_id

Posted: Fri Dec 02, 2022 10:32 pm
by Asynchron
Thx for clarification,

This isn't quite clear from the api, and you'd expect to be able to differentiate which circuit id you're using from the list defined here: https://lua-api.factorio.com/next/defin ... nnector_id

I.e. circuit_connection_id.accumulator != circuit_connection_id.constant_combinator

It would be nice to mention there that each item/items from that list should be used to check for circuit_id only on entities that they are meant for.

Best regards,
Asynchron

Re: [1.1.72][Lua] corrupt defines.circuit_connector_id

Posted: Fri Dec 02, 2022 10:53 pm
by boskid
No, it would not be nicer, it would be a pain on the game engine side, because with what you expect, a GenericOnOffControlBehavior would need to be given what CircuitConnectorID it should use. This could be fixed by reworking those defines slightly so they would explicitly contain the "generic_on_off_circuit_enable_disable" (with value of 1) and "generic_on_off_logistic_enable_disable" (with value of 2) but that would quickly become unreadable because a modder would need to know which entities are based on the GenericOnOffControlBehavior (i will have to think about this).

There are multiple sets of defines which are not guaranteed to be unique, including the defines.inventory, defines.transport_line, defines.circuit_condition_index and defines.circuit_connector_id. They all have one thing common: they are unique when you filter them based on a separate type check based on some other field. In case of a circuit_connector_id and circuit_condition_index, that value is LuaControlBehavior::type which returns a value that corresponds to defines.control_behavior.type (and this field is guaranteed to have unique values).

Re: [1.1.72][Lua] corrupt defines.circuit_connector_id

Posted: Sat Dec 03, 2022 1:29 am
by Asynchron
I think you've misunderstood last reply.

I didn't suggest to modify engine, but rather to just document those defines on api page mentioning this particular behavior you've explained in this thread, i.e. that they aren't necessarily unique, and actually they need another field to be checked for them to correctly work, and not accidentally mix them, like using circuit_connector_id.accumulator with a constant combinator entity.

At the moment, that list looks just like a simple enum, and nothing is telling you that they have same value, and why it is so.

Re: [1.1.72][Lua] corrupt defines.circuit_connector_id

Posted: Fri Dec 09, 2022 2:05 pm
by Rseding91
Asynchron wrote: Sat Dec 03, 2022 1:29 am At the moment, that list looks just like a simple enum, and nothing is telling you that they have same value, and why it is so.
The nature of the defines page is meant to work just like that. The value of the define is never supposed to be important and in an ideal world mods could not read the value. But due to performance we don't have it setup that way.

A given define is only ever useful when combined with an API function that specifically requests it. Otherwise if the wrong define given somewhere there is no type checking (Lua does not have that ability) - the value will be meaningless/garbage if the wrong one is used somewhere. We also make no guarantees about uniqueness in the defines and so mods can't ever assume the values will be unique.

Essentially: the things we don't state are just as important as the things we do. If the API page does not specifically say "these are sorted" then there's no guarantee they will be sorted. If the API page does not say "these are unique" then there's no guarantee they will be unique.

Re: [1.1.72][Lua] corrupt defines.circuit_connector_id

Posted: Fri Dec 09, 2022 5:04 pm
by quyxkh
Asynchron wrote: Fri Dec 02, 2022 9:31 pm Per my expectation each entry has to have different number, otherwise it is impossible to detect which network an entity has.
Here's your problem. Connector id is a defined constant, there's no way it *ever* could have told you which network an entity has. So it can't be what you first thought it was. If you search the api page for connector_id you'll see "The circuit connector ID on the associated entity this network was gotten from." and that should be enough: LuaCircuitNetwork links an entity and connector id on that entity.

So the id is "which connector on the entity", and almost all entities have just one connector. No surprise it's connector 1 on that entity. Combinators separate input and output, they have two connector ids so you can tell whether a circuit proxy was gotten from the input or output connector. Why would that matter? Because that way you can walk the network connections via each connected entity's circuit_connection_definitions, which tell you for each what it's connected to, via which connector pairs.

Constant combinators have only one connector and it's id 2, but it's also unusual in another way: it's an output-only connector. Like the output connectors on combinators, are also id 2. So the value assignments are sensible if you try to find the sense in them.

Re: [1.1.72][Lua] corrupt defines.circuit_connector_id

Posted: Fri Dec 09, 2022 11:11 pm
by Asynchron
Sure, that was clear from the first reply, that I did do a wrong assumption, about those defines, given they are grouped under a single group called circuit_connector_id here.

So, in order for someone else to not fall in same pitfall as me, imho would be nice to have this explicitly stated rather than implicitly in documentation, if possible.

To clarify, should I post a message in doc update info thread for this right?

Best regards,
Alexandru.