From 463e7a206af8e2b98617b549e3bc6dc5eb181ffc Mon Sep 17 00:00:00 2001 From: thetaepsilon-gamedev Date: Sun, 1 Oct 2017 12:25:43 +0100 Subject: new flow logic: abms.lua: start splitting apart spigot code into generalised output ABM --- new_flow_logic/abms.lua | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'new_flow_logic') diff --git a/new_flow_logic/abms.lua b/new_flow_logic/abms.lua index 48dcb0b..640493a 100644 --- a/new_flow_logic/abms.lua +++ b/new_flow_logic/abms.lua @@ -4,6 +4,7 @@ local flowlogic = {} +flowlogic.helpers = {} pipeworks.flowlogic = flowlogic @@ -105,6 +106,43 @@ end +-- flowlogic output helper for spigots +-- tries to place a water block in the world beneath the spigot. +-- threshold checking is assumed to be handled by the node registration; +-- see register_local_pipes.lua for the pipeworks default spigot. +flowlogic.helpers.output_spigot = function(pos, node, currentpressure) + local taken = 0 + local below = {x=pos.x, y=pos.y-1, z=pos.z} + local name = minetest.get_node(below).name + if (name == "air") or (name == "default:water_flowing") then + minetest.set_node(below, {name="default:water_source"}) + taken = taken + 1 + end + return taken +end + + + +flowlogic.run_output = function(pos, node, threshold, outputfn) + -- callback for output devices. + -- takes care of checking a minimum pressure value and updating the node metadata. + -- the outputfn is provided the current pressure and returns the pressure "taken". + -- as an example, using this with the above spigot function, + -- the spigot function tries to output a water source if it will fit in the world. + local meta = minetest.get_meta(pos) + -- sometimes I wonder if meta:get_* returning default values would ever be problematic. + -- though here it doesn't matter, an uninit'd node returns 0, which is fine for a new, empty node. + local currentpressure = meta:get_float(label_pressure) + if currentpressure > threshold then + local takenpressure = outputfn(pos, node, currentpressure) + local newpressure = currentpressure - takenpressure + if newpressure < 0 then currentpressure = 0 end + meta:set_float(label_pressure, newpressure) + end +end + + + flowlogic.run_spigot_output = function(pos, node) -- try to output a water source node if there's enough pressure and space below. local meta = minetest.get_meta(pos) -- cgit v1.2.3