[1.1.72][Lua] corrupt defines.circuit_connector_id

Bugs that are actually features.
Post Reply
Asynchron
Burner Inserter
Burner Inserter
Posts: 18
Joined: Fri Dec 02, 2022 9:10 pm
Contact:

[1.1.72][Lua] corrupt defines.circuit_connector_id

Post 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.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2241
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

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

Post 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.

Asynchron
Burner Inserter
Burner Inserter
Posts: 18
Joined: Fri Dec 02, 2022 9:10 pm
Contact:

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

Post 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

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2241
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

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

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

Asynchron
Burner Inserter
Burner Inserter
Posts: 18
Joined: Fri Dec 02, 2022 9:10 pm
Contact:

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

Post 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.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13202
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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.
If you want to get ahold of me I'm almost always on Discord.

quyxkh
Smart Inserter
Smart Inserter
Posts: 1028
Joined: Sun May 08, 2016 9:01 am
Contact:

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

Post 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.

Asynchron
Burner Inserter
Burner Inserter
Posts: 18
Joined: Fri Dec 02, 2022 9:10 pm
Contact:

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

Post 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.

Post Reply

Return to “Not a bug”