Page 1 of 1

Make LuaFluidBox::get_connections() map to prototype order

Posted: Tue Jul 24, 2018 7:19 pm
by Therax
Entities can define multiple pipe_connections for a single fluidbox, any combination of which can be connected or disconnected.

LuaFluidBox::get_connections(index) returns an array, with one entry per connected pipe connection, but if less than all of them are connected it's impossible to determine which entry maps to which pipe connection.

The most important vanilla entity in this respect is the pump, because the two pipe connections are not interchangeable: one is input and one is output. If only one is connected, get_connections(1) returns an array of one element, without distinguishing which pipe connection is connected.

Proposals:
  1. Instead of having get_connections(index) return an array, have it return an associative map. Under this proposal, a pump with only its input connected (pipe connection 2 in the protoype) would return:

    Code: Select all

    { [1] = nil, [2] = connected_entity_fluidbox }
    Type (input/output) could then be determined by referencing LuaFluidBoxPrototype::pipe_connections.
  2. Instead of having get_connections(index) return an array of FluidBoxes, have it return an array of records. This would be similar to how CircuitConnectionDefinition's source_circuit_id works for decider and arithmetic combinators. Under this proposal, a pump with only its input connected would return something like:

    Code: Select all

    {
      {
        source_pipe_connection_id = 2,
        source_pipe_connection_type = "input",
        target_fluidbox = connected_entity_fluidbox,
        target_fluidbox_index = 1,
        target_pipe_connection_id = 1,
        target_pipe_connection_type = "input-output",
      }
    }

Re: Make LuaFluidBox::get_connections() map to prototype order

Posted: Tue Oct 02, 2018 9:53 pm
by Rseding91
I don't think I understand what you're actually trying to do with this change?

Re: Make LuaFluidBox::get_connections() map to prototype order

Posted: Wed Oct 03, 2018 6:50 am
by Therax
Given a vanilla pump with prototype having 1 fluidbox with 2 pipe_connections, one input and one output:

Code: Select all

    fluid_box =
    {
      base_area = 1,
      height = 2,
      pipe_covers = pipecoverspictures(),
      pipe_connections =
      {
        { position = {0, -1.5}, type="output" },
        { position = {0, 1.5}, type="input" }
      }
    },
The pump is placed in world as LuaEntity "p1", with 1 fluid connection to pipe entity "e1". It is unknown whether e1 is connected to p1's input or output.

Goal: Determine at runtime whether e1 is connected to the p1's input or output.

Use case (general): Automatically determine direction of flow for script-driven fluid transfer, e.g. transfer between different surfaces.

Use case (specific): Factorissimo2, which in 0.16 requires the player to manually set direction of flow via a hotkey for each connection, or to place specific "input" or "output" connector entities. (See Fluid connections here.) In this case "e1" is the mod-provided connector entity.

Currently in 0.16: Calling p1.fluidbox.get_connections(1) returns {e1}, which offers no indication whether e1 is connected to the pump's input or output. Workaround is to compare position and direction of p1 and e1 to determine relative offset, and requires advance (hard-coded) knowledge of the prototypes for both to know the offsets for input/output connections.

Currently planned for 0.17: LuaFluidBoxPrototype exposes pipe connection offsets and direction. This removes the need for hard-coded connection information. Comparing positions and rotation directions of p1 and e1 would still be required.

Requested for 0.17: Starting with only a reference to e1, a way to determine if e1 is connected to p1's input or output, without having to resort to comparing positions.

Re: Make LuaFluidBox::get_connections() map to prototype order

Posted: Thu Oct 04, 2018 4:34 pm
by Bilka
Just to make this more clear, you can get the prototype of the connected fluidbox in 0.17 using

Code: Select all

game.player.selected.fluidbox.get_connections(1)[1].get_prototype(1)
But, from that prototype, you have no way of knowing if connection 1 or connection 2 are connected to your original entity, so you cannot determine the type of the connection.