diff --git a/basis/networks.lua b/basis/networks.lua index 380c7ed..e2203e2 100644 --- a/basis/networks.lua +++ b/basis/networks.lua @@ -89,7 +89,9 @@ local function connected(tlib2, pos, dir) return Flip[dir] == d1 or Flip[dir] == d2 end -- secondary nodes allowed? - if not tlib2.force_to_use_tubes then + if tlib2.force_to_use_tubes then + return tlib2:is_special_node(pos, dir) + else return tlib2:is_secondary_node(pos, dir) end return false @@ -147,7 +149,7 @@ local function node_connections(pos, tlib2) local side = DirToSide[outdir_to_dir(dir, node.param2)] if sides[side] then if connected(tlib2, pos, dir) then - techage.mark_side("singleplayer", pos, dir, "node_connections", "", 1)-------------------- + --techage.mark_side("singleplayer", pos, dir, "node_connections", "", 1)-------------------- val = val + 1 end end @@ -199,11 +201,11 @@ end -- if outdirs is given, only this dirs are used local function connection_walk(pos, outdirs, indir, node, tlib2, clbk) if clbk then clbk(pos, indir, node) end - --techage.mark_position("singleplayer", pos, "walk", "", 1) + techage.mark_position("singleplayer", pos, "walk", "", 1) --print("connection_walk", node.name, outdirs or is_junction(pos, node.name, tlib2.tube_type)) if outdirs or is_junction(pos, node.name, tlib2.tube_type) then for _,outdir in pairs(outdirs or get_node_connections(pos, tlib2.tube_type)) do - techage.mark_side("singleplayer", pos, outdir, "connection_walk", "", 3)-------------------- + --techage.mark_side("singleplayer", pos, outdir, "connection_walk", "", 3)-------------------- --print("get_node_connections", node.name, outdir) local pos2, indir2 = tlib2:get_connected_node_pos(pos, outdir) local node = techage.get_node_lvm(pos2) diff --git a/energy_storage/inlet.lua b/energy_storage/inlet.lua index 7b68ef5..c7ddcd4 100644 --- a/energy_storage/inlet.lua +++ b/energy_storage/inlet.lua @@ -15,7 +15,7 @@ -- for lazy programmers local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end local S2P = minetest.string_to_pos -local D = techage.Debug +local M = minetest.get_meta local M = minetest.get_meta local S = techage.S diff --git a/furnace/booster.lua b/furnace/booster.lua index 921faf2..75edf9b 100644 --- a/furnace/booster.lua +++ b/furnace/booster.lua @@ -15,7 +15,6 @@ -- for lazy programmers local M = minetest.get_meta local S = techage.S -local D = techage.Debug local PWR_NEEDED = 3 local CYCLE_TIME = 2 diff --git a/furnace/furnace_top.lua b/furnace/furnace_top.lua index cddc980..73b7f85 100644 --- a/furnace/furnace_top.lua +++ b/furnace/furnace_top.lua @@ -15,7 +15,6 @@ -- for lazy programmers local M = minetest.get_meta local S = techage.S -local D = techage.Debug -- Consumer Related Data local CRD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).consumer end diff --git a/init.lua b/init.lua index 44e7b48..8b68d60 100644 --- a/init.lua +++ b/init.lua @@ -5,8 +5,8 @@ elseif minetest.global_exists("ironage") then minetest.log("error", "[techage] Techage can't be used together with the mod ironage!") elseif minetest.global_exists("techpack") then minetest.log("error", "[techage] Techage can't be used together with the modpack techpack!") -elseif minetest.global_exists("tubelib2") and tubelib2.version < 1.6 then - minetest.log("error", "[techage] Techage requires tubelib2 version 1.6 or newer!") +elseif minetest.global_exists("tubelib2") and tubelib2.version < 1.8 then + minetest.log("error", "[techage] Techage requires tubelib2 version 1.8 or newer!") else techage = { NodeDef = {}, -- node registration info @@ -29,6 +29,8 @@ else dofile(MP.."/basis/command.lua") -- command API dofile(MP.."/basis/firebox_lib.lua") -- common firebox functions dofile(MP.."/basis/boiler_lib.lua") -- common boiler functions + dofile(MP.."/basis/liquid_lib.lua") -- common liquids functions + dofile(MP.."/basis/fuel_lib.lua") -- common fuel functions dofile(MP.."/basis/mark.lua") dofile(MP.."/basis/mark2.lua") dofile(MP.."/basis/assemble.lua") @@ -87,9 +89,8 @@ else -- Liquids I dofile(MP.."/liquids/liquid_pipe.lua") - dofile(MP.."/liquids/liquid.lua") - dofile(MP.."/liquids/liquid_lib.lua") - dofile(MP.."/liquids/fuel_lib.lua") + dofile(MP.."/liquids/node_api.lua") + --dofile(MP.."/liquids/liquid_lib.lua") -- Basic Machines dofile(MP.."/basic_machines/consumer.lua") -- consumer base model @@ -108,6 +109,7 @@ else -- Liquids II dofile(MP.."/liquids/tank.lua") + dofile(MP.."/liquids/filler.lua") dofile(MP.."/liquids/silo.lua") dofile(MP.."/liquids/pump.lua") diff --git a/liquids/fuel_lib.lua b/liquids/fuel_lib.lua index 299e0e5..9646895 100644 --- a/liquids/fuel_lib.lua +++ b/liquids/fuel_lib.lua @@ -45,16 +45,20 @@ function techage.fuel.fuel_container(x, y, nvm) if nvm.running then fuel_percent = ((nvm.burn_cycles or 1) * 100) / (nvm.burn_cycles_total or 1) end - local tooltip = S("To add fuel punch\nthis block\nwith a fuel container") return "container["..x..","..y.."]".. "box[0,0;1.05,2.1;#000000]".. - "tooltip[0,0;1.1,1.1;"..tooltip..";#0C3D32;#FFFFFF]".. "image[0.1,0.1;1,1;default_furnace_fire_bg.png^[lowpart:".. fuel_percent..":default_furnace_fire_fg.png]".. techage.item_image(0.1, 1.1, itemname).. "container_end[]" end +local function help(x, y) + local tooltip = S("To add fuel punch\nthis block\nwith a fuel container") + return "label["..x..","..y..";"..minetest.colorize("#000000", minetest.formspec_escape("[?]")).."]".. + "tooltip["..x..","..y..";0.5,0.5;"..tooltip..";#0C3D32;#FFFFFF]" +end + function techage.fuel.formspec(nvm) local title = S("Fuel Menu") return "size[4,3]".. @@ -63,93 +67,94 @@ function techage.fuel.formspec(nvm) default.gui_slots.. "box[0,-0.1;3.8,0.5;#c6e8ff]".. "label[1,-0.1;"..minetest.colorize("#000000", title).."]".. + help(3.4, -0.1).. techage.fuel.fuel_container(1.5, 1, nvm) end -local function fill_container(pos, inv, nvm) - nvm.liquid = nvm.liquid or {} - nvm.liquid.amount = nvm.liquid.amount or 0 - local empty_container = inv:get_stack("fuel", 1):get_name() - local full_container = liquid.get_full_container(empty_container, nvm.liquid.name) - if empty_container and full_container then - local ldef = liquid.get_liquid_def(full_container) - if ldef and nvm.liquid.amount - ldef.size >= 0 then - inv:remove_item("fuel", ItemStack(empty_container)) - inv:add_item("fuel", ItemStack(full_container)) - nvm.liquid.amount = nvm.liquid.amount - ldef.size - if nvm.liquid.amount == 0 then - nvm.liquid.name = nil - end - end - end -end +--local function fill_container(pos, inv, nvm) +-- nvm.liquid = nvm.liquid or {} +-- nvm.liquid.amount = nvm.liquid.amount or 0 +-- local empty_container = inv:get_stack("fuel", 1):get_name() +-- local full_container = liquid.get_full_container(empty_container, nvm.liquid.name) +-- if empty_container and full_container then +-- local ldef = liquid.get_liquid_def(full_container) +-- if ldef and nvm.liquid.amount - ldef.size >= 0 then +-- inv:remove_item("fuel", ItemStack(empty_container)) +-- inv:add_item("fuel", ItemStack(full_container)) +-- nvm.liquid.amount = nvm.liquid.amount - ldef.size +-- if nvm.liquid.amount == 0 then +-- nvm.liquid.name = nil +-- end +-- end +-- end +--end -local function empty_container(pos, inv, nvm) - nvm.liquid = nvm.liquid or {} - nvm.liquid.amount = nvm.liquid.amount or 0 - local stack = inv:get_stack("fuel", 1) - if stack:get_count() == 1 then - local ldef = liquid.get_liquid_def(stack:get_name()) - if ldef and ValidOilFuels[ldef.inv_item] then - if not nvm.liquid.name or ldef.inv_item == nvm.liquid.name then - if nvm.liquid.amount + ldef.size <= CAPACITY then - inv:remove_item("fuel", stack) - inv:add_item("fuel", ItemStack(ldef.container)) - nvm.liquid.amount = nvm.liquid.amount + ldef.size - nvm.liquid.name = ldef.inv_item - end - end - end - end -end +--local function empty_container(pos, inv, nvm) +-- nvm.liquid = nvm.liquid or {} +-- nvm.liquid.amount = nvm.liquid.amount or 0 +-- local stack = inv:get_stack("fuel", 1) +-- if stack:get_count() == 1 then +-- local ldef = liquid.get_liquid_def(stack:get_name()) +-- if ldef and ValidOilFuels[ldef.inv_item] then +-- if not nvm.liquid.name or ldef.inv_item == nvm.liquid.name then +-- if nvm.liquid.amount + ldef.size <= CAPACITY then +-- inv:remove_item("fuel", stack) +-- inv:add_item("fuel", ItemStack(ldef.container)) +-- nvm.liquid.amount = nvm.liquid.amount + ldef.size +-- nvm.liquid.name = ldef.inv_item +-- end +-- end +-- end +-- end +--end -local function move_item(pos, stack) - local nvm = techage.get_nvm(pos) - local inv = M(pos):get_inventory() - if liquid.is_container_empty(stack:get_name()) then - fill_container(pos, inv, nvm) - else - empty_container(pos, inv, nvm) - end - M(pos):set_string("formspec", techage.fuel.formspec(nvm)) -end +--local function move_item(pos, stack) +-- local nvm = techage.get_nvm(pos) +-- local inv = M(pos):get_inventory() +-- if liquid.is_container_empty(stack:get_name()) then +-- fill_container(pos, inv, nvm) +-- else +-- empty_container(pos, inv, nvm) +-- end +-- M(pos):set_string("formspec", techage.fuel.formspec(nvm)) +--end -function techage.fuel.move_item(pos, stack, formspec) - local nvm = techage.get_nvm(pos) - local inv = M(pos):get_inventory() - if liquid.is_container_empty(stack:get_name()) then - fill_container(pos, inv, nvm) - else - empty_container(pos, inv, nvm) - end - M(pos):set_string("formspec", formspec(pos, nvm)) -end +--function techage.fuel.move_item(pos, stack, formspec) +-- local nvm = techage.get_nvm(pos) +-- local inv = M(pos):get_inventory() +-- if liquid.is_container_empty(stack:get_name()) then +-- fill_container(pos, inv, nvm) +-- else +-- empty_container(pos, inv, nvm) +-- end +-- M(pos):set_string("formspec", formspec(pos, nvm)) +--end -function techage.fuel.allow_metadata_inventory_put(pos, listname, index, stack, player) - if minetest.is_protected(pos, player:get_player_name()) then - return 0 - end - if liquid.is_container_empty(stack:get_name()) then - return 1 - end - local category = LQD(pos).fuel_cat - local ldef = liquid.get_liquid_def(stack:get_name()) - if ldef and ValidOilFuels[ldef.inv_item] and ValidOilFuels[ldef.inv_item] <= category then - return 1 - end - return 0 -end +--function techage.fuel.allow_metadata_inventory_put(pos, listname, index, stack, player) +-- if minetest.is_protected(pos, player:get_player_name()) then +-- return 0 +-- end +-- if liquid.is_container_empty(stack:get_name()) then +-- return 1 +-- end +-- local category = LQD(pos).fuel_cat +-- local ldef = liquid.get_liquid_def(stack:get_name()) +-- if ldef and ValidOilFuels[ldef.inv_item] and ValidOilFuels[ldef.inv_item] <= category then +-- return 1 +-- end +-- return 0 +--end -function techage.fuel.allow_metadata_inventory_take(pos, listname, index, stack, player) - if minetest.is_protected(pos, player:get_player_name()) then - return 0 - end - return stack:get_count() -end +--function techage.fuel.allow_metadata_inventory_take(pos, listname, index, stack, player) +-- if minetest.is_protected(pos, player:get_player_name()) then +-- return 0 +-- end +-- return stack:get_count() +--end -function techage.fuel.on_metadata_inventory_put(pos, listname, index, stack, player) - minetest.after(0.5, move_item, pos, stack) -end +--function techage.fuel.on_metadata_inventory_put(pos, listname, index, stack, player) +-- minetest.after(0.5, move_item, pos, stack) +--end function techage.fuel.can_dig(pos, player) if minetest.is_protected(pos, player:get_player_name()) then @@ -168,21 +173,21 @@ function techage.fuel.on_rightclick(pos, node, clicker) M(pos):set_string("formspec", techage.fuel.formspec(nvm)) end -function techage.fuel.on_receive_fields(pos, formname, fields, player) - if minetest.is_protected(pos, player:get_player_name()) then - return - end - local nvm = techage.get_nvm(pos) - nvm.countdown = 10 - M(pos):set_string("formspec", techage.fuel.formspec(nvm)) -end +--function techage.fuel.on_receive_fields(pos, formname, fields, player) +-- if minetest.is_protected(pos, player:get_player_name()) then +-- return +-- end +-- local nvm = techage.get_nvm(pos) +-- nvm.countdown = 10 +-- M(pos):set_string("formspec", techage.fuel.formspec(nvm)) +--end -function techage.fuel.formspec_update(pos, nvm) - if nvm.countdown and nvm.countdown > 0 then - nvm.countdown = nvm.countdown - 1 - M(pos):set_string("formspec", techage.fuel.formspec(nvm)) - end -end +--function techage.fuel.formspec_update(pos, nvm) +-- if nvm.countdown and nvm.countdown > 0 then +-- nvm.countdown = nvm.countdown - 1 +-- M(pos):set_string("formspec", techage.fuel.formspec(nvm)) +-- end +--end -- name is the fuel item name function techage.fuel.burntime(name) diff --git a/liquids/liquid.lua b/liquids/liquid.lua deleted file mode 100644 index d24ae15..0000000 --- a/liquids/liquid.lua +++ /dev/null @@ -1,245 +0,0 @@ ---[[ - - TechAge - ======= - - Copyright (C) 2019-2020 Joachim Stolberg - - GPL v3 - See LICENSE.txt for more information - - Liquid transportation API via Pipe(s) (peer, put, take) - -]]-- - -local P2S = minetest.pos_to_string -local M = minetest.get_meta -local N = function(pos) return minetest.get_node(pos).name end -local LQD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).liquid end -local Pipe = techage.LiquidPipe -local S = techage.S - -local net_def = techage.networks.net_def -local networks = techage.networks - -techage.liquid = {} - -local LiquidDef = {} -local ContainerDef = {} - --- --- Networks --- - --- determine network ID (largest hash number of all pumps) -local function determine_netID(pos, outdir) - local netID = 0 - networks.connection_walk(pos, outdir, Pipe, function(pos, indir, node) - local ntype = net_def(pos, "pipe2").ntype - if ntype and ntype == "pump" then - local new = minetest.hash_node_position(pos) * 8 + outdir - if netID <= new then - netID = new - end - end - end) - return netID -end - --- store network ID on each pump like node -local function store_netID(pos, outdir, netID) - networks.connection_walk(pos, outdir, Pipe, function(pos, indir, node) - local ntype = net_def(pos, "pipe2").ntype - if ntype and ntype == "pump" then - local nvm = techage.get_nvm(pos) - local outdir = networks.Flip[indir] - nvm.pipe2 = nvm.pipe2 or {} - nvm.pipe2.netIDs = nvm.pipe2.netIDs or {} - nvm.pipe2.netIDs[outdir] = netID - end - end) -end - --- delete network and ID on each pump like node -local function delete_netID(pos, outdir) - local netID = 0 - networks.connection_walk(pos, outdir, Pipe, function(pos, indir, node) - local ntype = net_def(pos, "pipe2").ntype - if ntype and ntype == "pump" then - local nvm = techage.get_nvm(pos) - local outdir = networks.Flip[indir] - if nvm.pipe2 and nvm.pipe2.netIDs and nvm.pipe2.netIDs[outdir] then - netID = nvm.pipe2.netIDs[outdir] - nvm.pipe2.netIDs[outdir] = nil - end - end - end) - networks.delete_network("pipe2", netID) -end - -local function get_netID(pos, outdir) - local nvm = techage.get_nvm(pos) - if not nvm.pipe2 or not nvm.pipe2.netIDs or not nvm.pipe2.netIDs[outdir] then - local netID = determine_netID(pos, outdir) - store_netID(pos, outdir, netID) - end - return nvm.pipe2 and nvm.pipe2.netIDs and nvm.pipe2.netIDs[outdir] -end - -local function get_network_table(pos, outdir, ntype) - local netID = get_netID(pos, outdir) - if netID then - local netw = networks.get_network("pipe2", netID) - if not netw then - netw = networks.collect_network_nodes(pos, outdir, Pipe) - networks.set_network("pipe2", netID, netw) - end - print("netw", string.format("%012X", netID), dump(netw)) - return netw[ntype] or {} - end - return {} -end - - --- --- Client remote functions --- - --- Determine and return liquid 'name' from the --- remote inventory. -function techage.liquid.peek(pos, outdir) - for _,item in ipairs(get_network_table(pos, outdir, "tank")) do - local liquid = LQD(item.pos) - if liquid and liquid.peek then - return liquid.peek(item.pos, item.indir) - end - end -end - --- Add given amount of liquid to the remote inventory. --- return leftover amount -function techage.liquid.put(pos, outdir, name, amount, player_name) - for _,item in ipairs(get_network_table(pos, outdir, "tank")) do - local liquid = LQD(item.pos) - if liquid and liquid.put and liquid.peek then - -- wrong items? - local peek = liquid.peek(item.pos, item.indir) - if peek and peek ~= name then return amount or 0 end - if player_name then - local num = techage.get_node_number(pos) or "000" - techage.mark_position(player_name, item.pos, "("..num..") put", "", 1) - end - amount = liquid.put(item.pos, item.indir, name, amount) - if not amount or amount == 0 then break end - end - end - return amount or 0 -end - --- Take given amount of liquid for the remote inventory. --- return taken amount and item name -function techage.liquid.take(pos, outdir, name, amount, player_name) - local taken = 0 - local item_name = nil - for _,item in ipairs(get_network_table(pos, outdir, "tank")) do - local liquid = LQD(item.pos) - if liquid and liquid.take then - if player_name then - local num = techage.get_node_number(pos) - techage.mark_position(player_name, item.pos, "("..num..") take", "", 1) - end - local val, name = liquid.take(item.pos, item.indir, name, amount - taken) - if val and name then - taken = taken + val - item_name = name - if amount - taken == 0 then break end - end - end - end - return taken, item_name -end - --- --- Server local functions --- - -function techage.liquid.srv_peek(pos, indir) - local nvm = techage.get_nvm(pos) - nvm.liquid = nvm.liquid or {} - return nvm.liquid.name -end - -function techage.liquid.srv_put(pos, indir, name, amount) - local nvm = techage.get_nvm(pos) - nvm.liquid = nvm.liquid or {} - amount = amount or 0 - if not nvm.liquid.name then - nvm.liquid.name = name - nvm.liquid.amount = amount - return 0 - elseif nvm.liquid.name == name then - nvm.liquid.amount = nvm.liquid.amount or 0 - local capa = LQD(pos).capa - if nvm.liquid.amount + amount <= capa then - nvm.liquid.amount = nvm.liquid.amount + amount - return 0 - else - local rest = nvm.liquid.amount + amount - capa - nvm.liquid.amount = capa - return rest - end - end - return amount -end - -function techage.liquid.srv_take(pos, indir, name, amount) - local nvm = techage.get_nvm(pos) - nvm.liquid = nvm.liquid or {} - amount = amount or 0 - if not name or nvm.liquid.name == name then - name = nvm.liquid.name - nvm.liquid.amount = nvm.liquid.amount or 0 - if nvm.liquid.amount > amount then - nvm.liquid.amount = nvm.liquid.amount - amount - return amount, name - else - local rest = nvm.liquid.amount - local name = nvm.liquid.name - nvm.liquid.amount = 0 - nvm.liquid.name = nil - return rest, name - end - end - return 0 -end - --- --- Further API functions --- - --- like: register_liquid("techage:ta3_barrel_oil", "techage:ta3_barrel_empty", 10, "techage:oil") -function techage.register_liquid(full_container, empty_container, container_size, inv_item) - LiquidDef[full_container] = {container = empty_container, size = container_size, inv_item = inv_item} - ContainerDef[empty_container] = ContainerDef[empty_container] or {} - ContainerDef[empty_container][inv_item] = full_container -end - -function techage.liquid.get_liquid_def(full_container) - return LiquidDef[full_container] -end - -function techage.liquid.is_container_empty(container_name) - return ContainerDef[container_name] -end - -function techage.liquid.get_full_container(empty_container, inv_item) - return ContainerDef[empty_container] and ContainerDef[empty_container][inv_item] -end - --- To be called from each node via 'tubelib2_on_update2' --- 'output' is optional and only needed for nodes with dedicated --- pipe sides (e.g. pumps). -function techage.liquid.update_network(pos, outdir) - networks.node_connections(pos, Pipe) - delete_netID(pos, outdir) -end diff --git a/liquids/liquid.lua.orig b/liquids/liquid.lua.orig deleted file mode 100644 index d337e6d..0000000 --- a/liquids/liquid.lua.orig +++ /dev/null @@ -1,286 +0,0 @@ ---[[ - - TechAge - ======= - - Copyright (C) 2019 Joachim Stolberg - - GPL v3 - See LICENSE.txt for more information - - Liquid transportation API via Pipe(s) (peer, put, take) - -]]-- - -local P2S = minetest.pos_to_string -local M = minetest.get_meta -local N = function(pos) return minetest.get_node(pos).name end -local LQD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).liquid end -local Pipe = techage.LiquidPipe -local S = techage.S - -local net_def = techage.networks.net_def -local networks = techage.networks - -techage.liquid = {} - -local LiquidDef = {} -local ContainerDef = {} - --- --- Networks --- - --- determine network ID (largest hash number) -local function determine_netID(pos, outdir) - local netID = 0 - networks.connection_walk(pos, outdir, Pipe, function(pos, indir, node) - local ntype = net_def(pos, "pipe").ntype - if ntype and ntype ~= "pump" then - local new = minetest.hash_node_position(pos) - if netID <= new then - netID = new - end - end - end) - return netID -end - --- store network ID on each node -local function store_netID(pos, outdir, netID) - networks.connection_walk(pos, outdir, Pipe, function(pos, indir, node) - local ntype = net_def(pos, "pipe").ntype - if ntype and ntype ~= "pump" then - local mem = tubelib2.get_mem(pos) - mem.pipe = mem.pipe or {} - mem.pipe.netID = netID - end - end) -end - --- delete network and ID on each node -local function delete_netID(pos, outdir) - local netID = 0 - networks.connection_walk(pos, outdir, Pipe, function(pos, indir, node) - local ntype = net_def(pos, "pipe").ntype - if ntype and ntype ~= "pump" then - local mem = tubelib2.get_mem(pos) - if mem.pipe and mem.pipe.netID then - netID = mem.pipe.netID - mem.pipe.netID = nil - end - end - end) - networks.delete_network(netID, Pipe) -end - -local function get_netID(pos, outdir) - -- jump to the next node because pumps have two network - -- interfaces and therefore can't have a netID - local pos2 = Pipe:get_connected_node_pos(pos, outdir) - if not vector.equals(pos2, pos) then - local mem = tubelib2.get_mem(pos2) - if not mem.pipe or not mem.pipe.netID then - -- determine the ID - local netID = determine_netID(pos2, outdir) - store_netID(pos2, outdir, netID) - mem.pipe = mem.pipe or {} - mem.pipe.netID = netID - end - return mem.pipe.netID - end -end - -local function get_network_table(pos, outdir, ntype) - local netID = get_netID(pos, outdir) - if netID then - local netw = networks.get_network(netID, Pipe) - if not netw then - netw = networks.collect_network_nodes(pos, outdir, Pipe) - networks.set_network(netID, Pipe, netw) - end - local s = minetest.pos_to_string(minetest.get_position_from_hash(netID)) - --print("netw", string.format("%012X", netID), s, dump(netw)) - return netw[ntype] or {} - end - return {} -end - - --- --- Client remote functions --- - --- Determine and return liquid 'name' from the --- remote inventory. -function techage.liquid.peek(pos, outdir) - for _,item in ipairs(get_network_table(pos, outdir, "tank")) do - local liquid = LQD(item.pos) - if liquid and liquid.peek then - return liquid.peek(item.pos, item.indir) - end - end -end - --- Add given amount of liquid to the remote inventory. --- return leftover amount -function techage.liquid.put(pos, outdir, name, amount, player_name) - for _,item in ipairs(get_network_table(pos, outdir, "tank")) do - local liquid = LQD(item.pos) - if liquid and liquid.put and liquid.peek then - -- wrong items? - local peek = liquid.peek(item.pos, item.indir) - if peek and peek ~= name then return amount or 0 end - if player_name then - local num = techage.get_node_number(pos) or "000" - techage.mark_position(player_name, item.pos, "("..num..") put", "", 1) - end - amount = liquid.put(item.pos, item.indir, name, amount) - if not amount or amount == 0 then break end - end - end - return amount or 0 -end - --- Take given amount of liquid for the remote inventory. --- return taken amount and item name -function techage.liquid.take(pos, outdir, name, amount, player_name) - local taken = 0 - local item_name = nil - for _,item in ipairs(get_network_table(pos, outdir, "tank")) do - local liquid = LQD(item.pos) - if liquid and liquid.take then - if player_name then - local num = techage.get_node_number(pos) - techage.mark_position(player_name, item.pos, "("..num..") take", "", 1) - end - local val, name = liquid.take(item.pos, item.indir, name, amount - taken) - if val and name then - taken = taken + val - item_name = name - if amount - taken == 0 then break end - end - end - end - return taken, item_name -end - --- --- Server local functions --- - -function techage.liquid.srv_peek(pos, indir) - local mem = tubelib2.get_mem(pos) - mem.liquid = mem.liquid or {} - return mem.liquid.name -end - -function techage.liquid.srv_put(pos, indir, name, amount) - local mem = tubelib2.get_mem(pos) - mem.liquid = mem.liquid or {} - amount = amount or 0 - if not mem.liquid.name then - mem.liquid.name = name - mem.liquid.amount = amount - return 0 - elseif mem.liquid.name == name then - mem.liquid.amount = mem.liquid.amount or 0 - local capa = LQD(pos).capa - if mem.liquid.amount + amount <= capa then - mem.liquid.amount = mem.liquid.amount + amount - return 0 - else - local rest = mem.liquid.amount + amount - capa - mem.liquid.amount = capa - return rest - end - end - return amount -end - -function techage.liquid.srv_take(pos, indir, name, amount) - local mem = tubelib2.get_mem(pos) - mem.liquid = mem.liquid or {} - amount = amount or 0 - if not name or mem.liquid.name == name then - name = mem.liquid.name - mem.liquid.amount = mem.liquid.amount or 0 - if mem.liquid.amount > amount then - mem.liquid.amount = mem.liquid.amount - amount - return amount, name - else - local rest = mem.liquid.amount - local name = mem.liquid.name - mem.liquid.amount = 0 - mem.liquid.name = nil - return rest, name - end - end - return 0 -end - --- --- Further API functions --- - --- like: register_liquid("techage:ta3_barrel_oil", "techage:ta3_barrel_empty", 10, "techage:oil") -function techage.register_liquid(full_container, empty_container, container_size, inv_item) - LiquidDef[full_container] = {container = empty_container, size = container_size, inv_item = inv_item} - ContainerDef[empty_container] = ContainerDef[empty_container] or {} - ContainerDef[empty_container][inv_item] = full_container -end - -function techage.liquid.get_liquid_def(full_container) - return LiquidDef[full_container] -end - -function techage.liquid.is_container_empty(container_name) - return ContainerDef[container_name] -end - -function techage.liquid.get_full_container(empty_container, inv_item) - return ContainerDef[empty_container] and ContainerDef[empty_container][inv_item] -end - --- To be called from each node via 'tubelib2_on_update2' --- 'output' is optional and only needed for nodes with dedicated --- pipe sides (e.g. pumps). -function techage.liquid.update_network(pos, outdir) - networks.node_connections(pos, Pipe) - delete_netID(pos, outdir) -end - -minetest.register_craftitem("techage:water", { - description = S("Water"), - inventory_image = "techage_water_inv.png", - groups = {not_in_creative_inventory=1}, - -}) - -minetest.register_craftitem("techage:river_water", { - description = S("Water"), - inventory_image = "techage_water_inv.png", - groups = {not_in_creative_inventory=1}, - -}) - -minetest.register_craftitem("techage:barrel_water", { - description = S("Water Barrel"), - inventory_image = "techage_barrel_water_inv.png", - stack_max = 1, -}) - -minetest.register_craftitem("techage:barrel_river_water", { - description = S("River Water Barrel"), - inventory_image = "techage_barrel_water_inv.png", - stack_max = 1, -}) - -techage.register_liquid("bucket:bucket_water", "bucket:bucket_empty", 1, "techage:water") -techage.register_liquid("bucket:bucket_river_water", "bucket:bucket_empty", 1, "techage:river_water") - -techage.register_liquid("techage:barrel_water", "techage:ta3_barrel_empty", 10, "techage:water") -techage.register_liquid("techage:barrel_river_water", "techage:ta3_barrel_empty", 10, "techage:river_water") - -techage.register_liquid("bucket:bucket_lava", "bucket:bucket_empty", 1, "default:lava_source") - diff --git a/liquids/liquid_lib.lua b/liquids/liquid_lib.lua index bddd600..743a5c2 100644 --- a/liquids/liquid_lib.lua +++ b/liquids/liquid_lib.lua @@ -14,81 +14,88 @@ local M = minetest.get_meta local S = techage.S +local P2S = minetest.pos_to_string local liquid = techage.liquid local LQD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).liquid end -function techage.liquid.formspec_liquid(x, y, nvm) +local function help(x, y) + local tooltip = S("To add liquids punch\nthe tank\nwith a liquid container") + return "label["..x..","..y..";"..minetest.colorize("#000000", minetest.formspec_escape("[?]")).."]".. + "tooltip["..x..","..y..";0.5,0.5;"..tooltip..";#0C3D32;#FFFFFF]" +end + +function techage.liquid.formspec(pos, nvm) + local title = S("Liquid Tank") local itemname = "techage:liquid" if nvm.liquid and nvm.liquid.amount and nvm.liquid.amount > 0 and nvm.liquid.name then itemname = nvm.liquid.name.." "..nvm.liquid.amount end - return "container["..x..","..y.."]".. - "background[0,0;3,2.05;techage_form_grey.png]".. - "image[0,0;1,1;techage_form_input_arrow.png]".. - techage.item_image(1, 0, itemname).. - "image[2,0;1,1;techage_form_output_arrow.png]".. - "image[1,1;1,1;techage_form_arrow.png]".. - "list[context;src;0,1;1,1;]".. - "list[context;dst;2,1;1,1;]".. - "listring[current_player;main]".. - "listring[context;src]" .. - "listring[current_player;main]".. - "listring[context;dst]" .. - "listring[current_player;main]".. - "container_end[]" + return "size[5,3]".. + default.gui_bg.. + default.gui_bg_img.. + default.gui_slots.. + "box[0,-0.1;4.8,0.5;#c6e8ff]".. + "label[1.5,-0.1;"..minetest.colorize("#000000", title).."]".. + help(4.4, -0.1).. + techage.item_image(2, 1, itemname) end -local function fill_container(pos, inv) +local function fill_container(pos, inv, empty_container) local nvm = techage.get_nvm(pos) nvm.liquid = nvm.liquid or {} nvm.liquid.amount = nvm.liquid.amount or 0 - local empty_container = inv:get_stack("src", 1):get_name() local full_container = liquid.get_full_container(empty_container, nvm.liquid.name) if empty_container and full_container then local ldef = liquid.get_liquid_def(full_container) if ldef and nvm.liquid.amount - ldef.size >= 0 then - if inv:room_for_item("dst", ItemStack(full_container)) then - inv:remove_item("src", ItemStack(empty_container)) - inv:add_item("dst", ItemStack(full_container)) + if inv:room_for_item("dst", {name = full_container}) then + inv:add_item("dst", {name = full_container}) nvm.liquid.amount = nvm.liquid.amount - ldef.size if nvm.liquid.amount == 0 then nvm.liquid.name = nil end + return true end end end + -- undo + inv:add_item("src", {name = empty_container}) + return false end -local function empty_container(pos, inv, size) +local function empty_container(pos, inv, full_container) local nvm = techage.get_nvm(pos) nvm.liquid = nvm.liquid or {} nvm.liquid.amount = nvm.liquid.amount or 0 - local stack = inv:get_stack("src", 1) - local ldef = liquid.get_liquid_def(stack:get_name()) + local tank_size = LQD(pos).capa or 0 + local ldef = liquid.get_liquid_def(full_container) + print("ldef", dump(ldef), "tank_size", tank_size) if ldef and (not nvm.liquid.name or ldef.inv_item == nvm.liquid.name) then - local amount = stack:get_count() * ldef.size - if nvm.liquid.amount + amount <= size then - if inv:room_for_item("dst", ItemStack(ldef.container)) then - inv:remove_item("src", stack) - inv:add_item("dst", ItemStack(ldef.container)) - nvm.liquid.amount = nvm.liquid.amount + amount + if nvm.liquid.amount + ldef.size <= tank_size then + if inv:room_for_item("dst", {name = ldef.container}) then + inv:add_item("dst", {name = ldef.container}) + nvm.liquid.amount = nvm.liquid.amount + ldef.size nvm.liquid.name = ldef.inv_item + return true end end end + -- undo + inv:add_item("src", {name = full_container}) + return false end -function techage.liquid.move_item(pos, stack, size, formspec) - local nvm = techage.get_nvm(pos) - local inv = M(pos):get_inventory() - if liquid.is_container_empty(stack:get_name()) then - fill_container(pos, inv) - else - empty_container(pos, inv, size) - end - M(pos):set_string("formspec", formspec(pos, nvm)) -end +--function techage.liquid.move_item(pos, stack, size, formspec) +-- local nvm = techage.get_nvm(pos) +-- local inv = M(pos):get_inventory() +-- if liquid.is_container_empty(stack:get_name()) then +-- fill_container(pos, inv) +-- else +-- empty_container(pos, inv, size) +-- end +-- M(pos):set_string("formspec", formspec(pos, nvm)) +--end function techage.liquid.is_empty(pos) local nvm = techage.get_nvm(pos) @@ -97,34 +104,34 @@ function techage.liquid.is_empty(pos) end techage.liquid.tubing = { - on_pull_item = function(pos, in_dir, num) - local inv = M(pos):get_inventory() - if not inv:is_empty("dst") then - local taken = techage.get_items(inv, "dst", num) - if not inv:is_empty("src") then - fill_container(pos, inv) - end - return taken - end - end, - on_push_item = function(pos, in_dir, stack) - local inv = M(pos):get_inventory() - if inv:room_for_item("src", stack) then - inv:add_item("src", stack) - if liquid.is_container_empty(stack:get_name()) then - fill_container(pos, inv) - else - empty_container(pos, inv) - end - return true - end - return false - end, - on_unpull_item = function(pos, in_dir, stack) - local meta = M(pos) - local inv = meta:get_inventory() - return techage.put_items(inv, "dst", stack) - end, +-- on_pull_item = function(pos, in_dir, num) +-- local inv = M(pos):get_inventory() +-- if not inv:is_empty("dst") then +-- local taken = techage.get_items(inv, "dst", num) +-- if not inv:is_empty("src") then +-- fill_container(pos, inv) +-- end +-- return taken +-- end +-- end, +-- on_push_item = function(pos, in_dir, stack) +-- local inv = M(pos):get_inventory() +-- if inv:room_for_item("src", stack) then +-- inv:add_item("src", stack) +-- if liquid.is_container_empty(stack:get_name()) then +-- fill_container(pos, inv) +-- else +-- empty_container(pos, inv) +-- end +-- return true +-- end +-- return false +-- end, +-- on_unpull_item = function(pos, in_dir, stack) +-- local meta = M(pos) +-- local inv = meta:get_inventory() +-- return techage.put_items(inv, "dst", stack) +-- end, on_recv_message = function(pos, src, topic, payload) if topic == "load" then local nvm = techage.get_nvm(pos) diff --git a/liquids/tank.lua b/liquids/tank.lua index caa353a..05ca92e 100644 --- a/liquids/tank.lua +++ b/liquids/tank.lua @@ -21,54 +21,20 @@ local liquid = techage.liquid local CAPACITY = 500 -local function formspec(pos, nvm) - local update = ((nvm.countdown or 0) > 0 and nvm.countdown) or S("Update") - return "size[8,6]".. - default.gui_bg.. - default.gui_bg_img.. - default.gui_slots.. - liquid.formspec_liquid(2, 0, nvm).. - "button[5.5,0.5;2,1;update;"..update.."]".. - "list[current_player;main;0,2.3;8,4;]" -end - -local function allow_metadata_inventory_put(pos, listname, index, stack, player) - if minetest.is_protected(pos, player:get_player_name()) then - return 0 - end - return stack:get_count() -end - -local function allow_metadata_inventory_take(pos, listname, index, stack, player) - if minetest.is_protected(pos, player:get_player_name()) then - return 0 - end - return stack:get_count() -end - -local function allow_metadata_inventory_move() - return 0 -end - -local function on_metadata_inventory_put(pos, listname, index, stack, player) - minetest.after(0.5, liquid.move_item, pos, stack, CAPACITY, formspec) -end - -local function on_rightclick(pos) +local function on_rightclick(pos, node, clicker) local nvm = techage.get_nvm(pos) - nvm.countdown = 10 - M(pos):set_string("formspec", formspec(pos, nvm)) + techage.set_activeformspec(pos, clicker) + M(pos):set_string("formspec", liquid.formspec(pos, nvm)) minetest.get_node_timer(pos):start(2) end -local function on_receive_fields(pos, formname, fields, player) - if minetest.is_protected(pos, player:get_player_name()) then - return - end - local nvm = techage.get_nvm(pos) - nvm.countdown = 10 - M(pos):set_string("formspec", formspec(pos, nvm)) - minetest.get_node_timer(pos):start(2) +local function node_timer(pos, elapsed) + if techage.is_activeformspec(pos) then + local nvm = techage.get_nvm(pos) + M(pos):set_string("formspec", liquid.formspec(pos, nvm)) + return true + end + return false end local function can_dig(pos, player) @@ -78,6 +44,30 @@ local function can_dig(pos, player) return liquid.is_empty(pos) end +local function take_liquid(pos, indir, name, amount) + amount, name = liquid.srv_take(pos, indir, name, amount) + if techage.is_activeformspec(pos) then + local nvm = techage.get_nvm(pos) + M(pos):set_string("formspec", liquid.formspec(pos, nvm)) + end + return amount, name +end + +local function put_liquid(pos, indir, name, amount) + local leftover = liquid.srv_put(pos, indir, name, amount) + if techage.is_activeformspec(pos) then + local nvm = techage.get_nvm(pos) + M(pos):set_string("formspec", liquid.formspec(pos, nvm)) + end + return leftover +end + +local networks_def = { + pipe2 = { + sides = techage.networks.AllSides, -- Pipe connection sides + ntype = "tank", + }, +} minetest.register_node("techage:ta3_tank", { description = S("TA3 Tank"), @@ -90,12 +80,6 @@ minetest.register_node("techage:ta3_tank", { "techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_tank.png", "techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_tank.png", }, - on_construct = function(pos) - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() - inv:set_size('src', 1) - inv:set_size('dst', 1) - end, after_place_node = function(pos, placer) local meta = M(pos) local nvm = techage.get_nvm(pos) @@ -103,21 +87,15 @@ minetest.register_node("techage:ta3_tank", { local number = techage.add_node(pos, "techage:ta3_tank") meta:set_string("node_number", number) meta:set_string("owner", placer:get_player_name()) - meta:set_string("formspec", formspec(pos, nvm)) + meta:set_string("formspec", liquid.formspec(pos, nvm)) meta:set_string("infotext", S("TA3 Tank").." "..number) Pipe:after_place_node(pos) end, tubelib2_on_update2 = function(pos, outdir, tlib2, node) liquid.update_network(pos, outdir) end, - on_timer = function(pos, elapsed) - local nvm = techage.get_nvm(pos) - if nvm.countdown then - nvm.countdown = nvm.countdown - 1 - M(pos):set_string("formspec", formspec(pos, nvm)) - return nvm.countdown > 0 - end - end, + on_timer = node_timer, + on_punch = liquid.on_punch, after_dig_node = function(pos, oldnode, oldmetadata, digger) Pipe:after_dig_node(pos) techage.remove_node(pos) @@ -125,29 +103,12 @@ minetest.register_node("techage:ta3_tank", { liquid = { capa = CAPACITY, peek = liquid.srv_peek, - put = function(pos, indir, name, amount) - local leftover = liquid.srv_put(pos, indir, name, amount) - local inv = M(pos):get_inventory() - if not inv:is_empty("src") and inv:is_empty("dst") then - liquid.fill_container(pos, inv) - end - return leftover - end, - take = liquid.srv_take, - }, - networks = { - pipe2 = { - sides = techage.networks.AllSides, -- Pipe connection sides - ntype = "tank", - }, + put = put_liquid, + take = take_liquid, }, + networks = networks_def, on_rightclick = on_rightclick, - on_receive_fields = on_receive_fields, can_dig = can_dig, - allow_metadata_inventory_put = allow_metadata_inventory_put, - allow_metadata_inventory_take = allow_metadata_inventory_take, - allow_metadata_inventory_move = allow_metadata_inventory_move, - on_metadata_inventory_put = on_metadata_inventory_put, paramtype2 = "facedir", on_rotate = screwdriver.disallow, groups = {cracky=2}, @@ -177,12 +138,6 @@ minetest.register_node("techage:oiltank", { type = "fixed", fixed = {-6/8, -4/8, -6/8, 6/8, 6/8, 6/8}, }, - on_construct = function(pos) - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() - inv:set_size('src', 1) - inv:set_size('dst', 1) - end, after_place_node = function(pos, placer) local meta = M(pos) local nvm = techage.get_nvm(pos) @@ -190,21 +145,15 @@ minetest.register_node("techage:oiltank", { local number = techage.add_node(pos, "techage:oiltank") meta:set_string("node_number", number) meta:set_string("owner", placer:get_player_name()) - meta:set_string("formspec", formspec(pos, nvm)) + meta:set_string("formspec", liquid.formspec(pos, nvm)) meta:set_string("infotext", S("Oil Tank").." "..number) Pipe:after_place_node(pos) end, tubelib2_on_update2 = function(pos, outdir, tlib2, node) liquid.update_network(pos, outdir) end, - on_timer = function(pos, elapsed) - local nvm = techage.get_nvm(pos) - if nvm.countdown then - nvm.countdown = nvm.countdown - 1 - M(pos):set_string("formspec", formspec(pos, nvm)) - return nvm.countdown > 0 - end - end, + on_timer = node_timer, + on_punch = liquid.on_punch, after_dig_node = function(pos, oldnode, oldmetadata, digger) Pipe:after_dig_node(pos) techage.remove_node(pos) @@ -212,29 +161,12 @@ minetest.register_node("techage:oiltank", { liquid = { capa = CAPACITY * 4, peek = liquid.srv_peek, - put = function(pos, indir, name, amount) - local leftover = liquid.srv_put(pos, indir, name, amount) - local inv = M(pos):get_inventory() - if not inv:is_empty("src") and inv:is_empty("dst") then - liquid.fill_container(pos, inv) - end - return leftover - end, - take = liquid.srv_take, - }, - networks = { - pipe2 = { - sides = techage.networks.AllSides, -- Pipe connection sides - ntype = "tank", - }, + put = put_liquid, + take = take_liquid, }, + networks = networks_def, on_rightclick = on_rightclick, - on_receive_fields = on_receive_fields, can_dig = can_dig, - allow_metadata_inventory_put = allow_metadata_inventory_put, - allow_metadata_inventory_take = allow_metadata_inventory_take, - allow_metadata_inventory_move = allow_metadata_inventory_move, - on_metadata_inventory_put = on_metadata_inventory_put, paramtype2 = "facedir", on_rotate = screwdriver.disallow, groups = {cracky=2}, @@ -254,12 +186,6 @@ minetest.register_node("techage:ta4_tank", { "techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_tank.png", }, - on_construct = function(pos) - local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() - inv:set_size('src', 1) - inv:set_size('dst', 1) - end, after_place_node = function(pos, placer) local meta = M(pos) local nvm = techage.get_nvm(pos) @@ -267,21 +193,15 @@ minetest.register_node("techage:ta4_tank", { local number = techage.add_node(pos, "techage:ta4_tank") meta:set_string("node_number", number) meta:set_string("owner", placer:get_player_name()) - meta:set_string("formspec", formspec(pos, nvm)) + meta:set_string("formspec", liquid.formspec(pos, nvm)) meta:set_string("infotext", S("TA4 Tank").." "..number) Pipe:after_place_node(pos) end, tubelib2_on_update2 = function(pos, outdir, tlib2, node) liquid.update_network(pos, outdir) end, - on_timer = function(pos, elapsed) - local nvm = techage.get_nvm(pos) - if nvm.countdown then - nvm.countdown = nvm.countdown - 1 - M(pos):set_string("formspec", formspec(pos, nvm)) - return nvm.countdown > 0 - end - end, + on_timer = node_timer, + on_punch = liquid.on_punch, after_dig_node = function(pos, oldnode, oldmetadata, digger) Pipe:after_dig_node(pos) techage.remove_node(pos) @@ -289,29 +209,12 @@ minetest.register_node("techage:ta4_tank", { liquid = { capa = CAPACITY * 2, peek = liquid.srv_peek, - put = function(pos, indir, name, amount) - local leftover = liquid.srv_put(pos, indir, name, amount) - local inv = M(pos):get_inventory() - if not inv:is_empty("src") and inv:is_empty("dst") then - liquid.fill_container(pos, inv) - end - return leftover - end, - take = liquid.srv_take, - }, - networks = { - pipe2 = { - sides = techage.networks.AllSides, -- Pipe connection sides - ntype = "tank", - }, + put = put_liquid, + take = take_liquid, }, + networks = networks_def, on_rightclick = on_rightclick, - on_receive_fields = on_receive_fields, can_dig = can_dig, - allow_metadata_inventory_put = allow_metadata_inventory_put, - allow_metadata_inventory_take = allow_metadata_inventory_take, - allow_metadata_inventory_move = allow_metadata_inventory_move, - on_metadata_inventory_put = on_metadata_inventory_put, paramtype2 = "facedir", on_rotate = screwdriver.disallow, groups = {cracky=2}, diff --git a/oil/pumpjack.lua b/oil/pumpjack.lua index 31a11ad..f021cde 100644 --- a/oil/pumpjack.lua +++ b/oil/pumpjack.lua @@ -103,7 +103,6 @@ end local function pumping(pos, crd, meta, nvm) if has_oil(pos, meta) then local leftover = liquid.put(pos, 6, "techage:oil_source", 1) - print("pumping", dump(leftover)) if leftover and leftover > 0 then crd.State:blocked(pos, nvm) stop_sound(pos) diff --git a/power/junction.lua b/power/junction.lua index 5de3798..79414bd 100644 --- a/power/junction.lua +++ b/power/junction.lua @@ -70,6 +70,8 @@ function techage.register_junction(name, size, boxes, tlib2, node, index) ndef.drop = name..(index or "0") minetest.register_node(name..idx, ndef) tlib2:add_secondary_node_names({name..idx}) + -- for the case that 'tlib2.force_to_use_tubes' is set + tlib2:add_special_node_names({name..idx}) end end @@ -79,6 +81,8 @@ function techage.junction_type(pos, network) if network.force_to_use_tubes then if network:friendly_primary_node(pos, dir) then val = setbit(val, bit(dir)) + elseif network:is_special_node(pos, dir) then + val = setbit(val, bit(dir)) end else if network:connected(pos, dir) then