Read capacities and connections of fluid boxes

MagmaMcFry
Inserter
Inserter
Posts: 37
Joined: Sun Jul 17, 2016 9:05 pm
Contact:

Read capacities and connections of fluid boxes

Post by MagmaMcFry »

I would like the ability to read-access the properties of fluid boxes of an entity (via its entity.prototype), specifically their base_area (or just the capacity in fluid units) and their pipe_connections.
I don't visit the forum very often. If you want to contact me, message me on the mod portal for Factorissimo-related things, or on Discord.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14913
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Read capacities and connections of fluid boxes

Post by Rseding91 »

What's your use-case for the pipe connections?

There's a bunch of different ways that could be exposed and I'm trying to think of the best way to do it.
  • The offsets as defined in the prototype
  • The offsets in relation to the actual setup+rotated entity
  • The entities the fluidbox is connected to
  • The positions the connections would look for other fluidboxes at
And so on. Knowing what you wanted to do would give me a clue as to which is more useful.
If you want to get ahold of me I'm almost always on Discord.
MagmaMcFry
Inserter
Inserter
Posts: 37
Joined: Sun Jul 17, 2016 9:05 pm
Contact:

Re: Read capacities and connections of fluid boxes

Post by MagmaMcFry »

I want to use the pipe connections to see where the entity could connect to Factorissimo ports. So I don't actually need the entities the fluidbox is connected to, but rather the locations of the potential connections. Right now, as a workaround, I just copy-paste the connection information directly from the prototypes into my code and transform it myself, so nontransformed connection data is perfectly fine for me.

I suppose you could expose the untransformed connection data in LuaEntityPrototype, since that is where people would look and transformations don't make sense there. The connected entities are actually already available in entity.neighbours, but you could possibly expose them in a more fine-grained manner as entity.fluid_neighbours[fluidbox_index][connection_index].

Here's the transformation code I use, for public reference:

Code: Select all

local DX = {
	[defines.direction.north] = 0,
	[defines.direction.east] = 1,
	[defines.direction.south] = 0,
	[defines.direction.west] = -1,
}
local DY = {
	[defines.direction.north] = -1,
	[defines.direction.east] = 0,
	[defines.direction.south] = 1,
	[defines.direction.west] = 0,
}

local FLUIDBOX_DATA = {
	["pipe"] = {base_area = 1, connections = {{x=0,y=-1},{x=1,y=0},{x=0,y=1},{x=-1,y=0}}},
	["pipe-to-ground"] = {base_area = 1, connections = {{x=0,y=-1}}},
	["storage-tank"] = {base_area = 250, connections = {{x=-1,y=-2},{x=2,y=1},{x=1,y=2},{x=-2,y=-1}}},
	["small-pump"] = {base_area = 1, connections = {{x=0,y=-1,type="output"},{x=0,y=1,type="input"}}},
}

local function has_connection_towards(entity, x, y)
	if FLUIDBOX_DATA[entity.name] == nil then return false end
	local fconns = FLUIDBOX_DATA[entity.name].connections
	local position = entity.position
	local direction = entity.direction
	for _,fconn in pairs(fconns) do
		local dx = position.x + (-fconn.x*DY[direction]-fconn.y*DX[direction]) - x
		local dy = position.y + (-fconn.y*DY[direction]+fconn.x*DX[direction]) - y
		if (dx*dx+dy*dy) < 0.1 then
			return fconn.type or true
		end
	end
	return false
end
PS: Writing this up I realize I could actually make do with the current API, by placing dummy fluid tanks at the connection locations and searching for them in entity.neighbours. That's very convenient since I wanted to place dummy tanks anyway in order to make pipe connections apparent and hide the pipe covers. Quick question, do two small pumps facing each other (meaning that both involved pipe_connections have "output" type) count as neighbors?
I don't visit the forum very often. If you want to contact me, message me on the mod portal for Factorissimo-related things, or on Discord.
Post Reply

Return to “Implemented mod requests”