improvements on distributor, hopper, and liquidsampler

This commit is contained in:
Joachim Stolberg 2020-02-14 22:59:16 +01:00
parent 7790101254
commit 402c00a584
8 changed files with 92 additions and 213 deletions

View File

@ -359,9 +359,6 @@ local tubing = {
on_recv_message = function(pos, src, topic, payload) on_recv_message = function(pos, src, topic, payload)
return CRD(pos).State:on_receive_message(pos, topic, payload) return CRD(pos).State:on_receive_message(pos, topic, payload)
end, end,
on_node_load = function(pos)
CRD(pos).State:on_node_load(pos)
end,
} }
local node_name_ta2, node_name_ta3, node_name_ta4 = local node_name_ta2, node_name_ta3, node_name_ta4 =

View File

@ -164,8 +164,10 @@ local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if listname == "src" then if listname == "src" then
CRD(pos).State:start_if_standby(pos) CRD(pos).State:start_if_standby(pos)
return stack:get_count() return stack:get_count()
elseif (list[index]:get_count() == 0 or stack:get_name() ~= list[index]:get_name()) then elseif not inv:contains_item(listname, {name = stack:get_name()}) then
return 1 stack:set_count(1)
inv:set_stack(listname, index, stack)
return 0
end end
return 0 return 0
end end
@ -174,16 +176,35 @@ local function allow_metadata_inventory_take(pos, listname, index, stack, player
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
return 0 return 0
end end
if listname == "src" then
return stack:get_count() return stack:get_count()
else
local inv = M(pos):get_inventory()
inv:set_stack(listname, index, nil)
return 0
end
end end
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player) local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
local meta = M(pos) if minetest.is_protected(pos, player:get_player_name()) then
local inv = meta:get_inventory() return 0
end
local inv = minetest.get_meta(pos):get_inventory()
local stack = inv:get_stack(from_list, from_index) local stack = inv:get_stack(from_list, from_index)
return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
end
if from_list == "src" and to_list ~= "src" and not inv:contains_item(to_list, {name = stack:get_name()}) then
stack:set_count(1)
inv:set_stack(to_list, to_index, stack)
return 0
elseif from_list ~= "src" and to_list == "src" then
inv:set_stack(from_list, from_index, nil)
return 0
elseif not inv:contains_item(to_list, {name = stack:get_name()}) then
return 1
else
return 0
end
end
local function push_item(pos, filter, item_name, num_items, nvm) local function push_item(pos, filter, item_name, num_items, nvm)
local idx = 1 local idx = 1

View File

@ -66,31 +66,23 @@ local function allow_metadata_inventory_take(pos, listname, index, stack, player
return stack:get_count() return stack:get_count()
end end
local function test_liquid(node) local function can_start(pos, nvm, state)
local liquiddef = bucket.liquids[node.name] local water_pos = minetest.string_to_pos(M(pos):get_string("water_pos"))
if liquiddef ~= nil and liquiddef.itemname ~= nil and if not techage.is_ocean(water_pos) then
node.name == liquiddef.source then return S("no usable water")
return liquiddef.itemname
end end
return true
end end
local function sample_liquid(pos, crd, nvm, inv) local function sample_liquid(pos, crd, nvm, inv)
local meta = M(pos) if inv:room_for_item("dst", {name = "bucket:bucket_water"}) and
local water_pos = minetest.string_to_pos(meta:get_string("water_pos")) inv:contains_item("src", {name = "bucket:bucket_empty"}) then
local giving_back = test_liquid(techage.get_node_lvm(water_pos)) inv:remove_item("src", {name = "bucket:bucket_empty"})
if giving_back then inv:add_item("dst", {name = "bucket:bucket_water"})
if inv:room_for_item("dst", ItemStack(giving_back)) and
inv:contains_item("src", ItemStack("bucket:bucket_empty")) then
minetest.remove_node(water_pos)
inv:remove_item("src", ItemStack("bucket:bucket_empty"))
inv:add_item("dst", ItemStack(giving_back))
crd.State:keep_running(pos, nvm, COUNTDOWN_TICKS) crd.State:keep_running(pos, nvm, COUNTDOWN_TICKS)
else else
crd.State:idle(pos, nvm) crd.State:idle(pos, nvm)
end end
else
crd.State:fault(pos, nvm)
end
end end
local function keep_running(pos, elapsed) local function keep_running(pos, elapsed)
@ -184,6 +176,7 @@ local node_name_ta2, node_name_ta3, _ =
standby_ticks = STANDBY_TICKS, standby_ticks = STANDBY_TICKS,
formspec = formspec, formspec = formspec,
tubing = tubing, tubing = tubing,
can_start = can_start,
after_place_node = function(pos, placer) after_place_node = function(pos, placer)
local inv = M(pos):get_inventory() local inv = M(pos):get_inventory()
inv:set_size("src", 4) inv:set_size("src", 4)

View File

@ -29,8 +29,8 @@ local S = techage.S
-- Consumer Related Data -- Consumer Related Data
local CRD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).consumer end local CRD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).consumer end
local STANDBY_TICKS = 10 local STANDBY_TICKS = 5
local COUNTDOWN_TICKS = 10 local COUNTDOWN_TICKS = 5
local CYCLE_TIME = 2 local CYCLE_TIME = 2
local function pushing(pos, crd, meta, nvm) local function pushing(pos, crd, meta, nvm)

View File

@ -17,6 +17,7 @@ local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local P = minetest.string_to_pos local P = minetest.string_to_pos
local M = minetest.get_meta local M = minetest.get_meta
local check_cart = minecart.check_cart
local function deserialize(s) local function deserialize(s)
local tbl = {} local tbl = {}
@ -179,6 +180,14 @@ local function item_handling_node(name)
end end
end end
local function is_air_like(name)
local ndef = minetest.registered_nodes[name]
if ndef and ndef.buildable_to then
return true
end
return false
end
------------------------------------------------------------------- -------------------------------------------------------------------
-- API helper functions -- API helper functions
------------------------------------------------------------------- -------------------------------------------------------------------
@ -399,7 +408,7 @@ function techage.push_items(pos, out_dir, stack)
local npos, in_dir, name = get_dest_node(pos, out_dir) local npos, in_dir, name = get_dest_node(pos, out_dir)
if npos and NodeDef[name] and NodeDef[name].on_push_item then if npos and NodeDef[name] and NodeDef[name].on_push_item then
return NodeDef[name].on_push_item(npos, in_dir, stack) return NodeDef[name].on_push_item(npos, in_dir, stack)
elseif name == "air" then elseif is_air_like(name) or check_cart(npos) then
minetest.add_item(npos, stack) minetest.add_item(npos, stack)
return true return true
end end

View File

@ -469,51 +469,8 @@ function NodeStates:on_receive_message(pos, topic, payload)
end end
-- repair corrupt node data -- repair corrupt node data
function NodeStates:on_node_load(pos, not_start_timer) function NodeStates:on_node_load(pos)
local nvm = techage.get_nvm(pos) -- tbd
-- Meta data corrupt?
local number = M(pos):get_string("node_number")
if number == "" then
minetest.log("warning", "[TA] Node at "..S(pos).." has no node_number")
local name = techage.get_node_lvm(pos).name
local number = techage.add_node(pos, name)
self:node_init(pos, nvm, number)
return
end
-- wrong number and no dummy number?
if number ~= "-" then
local info = techage.get_node_info(number)
if not info or not info.pos or not vector.equals(pos, info.pos) then
if not info then
minetest.log("warning", "[TA] Node at "..S(pos).." has no info")
elseif not info.pos then
minetest.log("warning", "[TA] Node at "..S(pos).." has no info.pos")
elseif not vector.equals(pos, info.pos) then
print(S(pos), S(info.pos))
minetest.log("warning", "[TA] Node at "..S(pos).." is pos ~= info.pos")
end
swap_node(pos, "techage:defect_dummy")
return
end
end
-- state corrupt?
local state = nvm.techage_state or 0
if state == 0 then
if minetest.get_node_timer(pos):is_started() then
nvm.techage_state = RUNNING
else
nvm.techage_state = STOPPED
end
elseif state == RUNNING and not not_start_timer then
minetest.get_node_timer(pos):start(self.cycle_time)
elseif state == STANDBY then
minetest.get_node_timer(pos):start(self.cycle_time * self.standby_ticks)
elseif state == BLOCKED then
minetest.get_node_timer(pos):start(self.cycle_time * self.standby_ticks)
end
end end
minetest.register_node("techage:defect_dummy", { minetest.register_node("techage:defect_dummy", {

View File

@ -12,141 +12,43 @@
]]-- ]]--
-- for lazy programmers -- use the minecart hopper
local M = minetest.get_meta minetest.register_alias("techage:hopper_ta1", "minecart:hopper")
local S = techage.S
local function scan_for_objects(pos, inv)
for _, object in pairs(minetest.get_objects_inside_radius(pos, 1)) do
local lua_entity = object:get_luaentity()
if not object:is_player() and lua_entity and lua_entity.name == "__builtin:item" then
if lua_entity.itemstring ~= "" then
local stack = ItemStack(lua_entity.itemstring)
if inv:room_for_item("main", stack) then
inv:add_item("main", stack)
object:remove()
end
end
end
end
end
local function pull_push_item(pos, meta) minecart.register_inventory(
local items = techage.neighbour_pull_items(pos, 6, 1) {
if items then "techage:chest_ta3", "techage:chest_ta4",
if techage.neighbour_push_items(pos, meta:get_int("push_dir"), items) then "techage:meltingpot", "techage:meltingpot_active",
},
{
put = {
listname = "main",
},
take = {
listname = "main",
},
}
)
minecart.register_inventory(
{
"techage:sieve0", "techage:sieve1", "techage:sieve2", "techage:sieve3",
},
{
put = {
allow_inventory_put = function(pos, stack, player_name)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if inv:is_empty("src") then
minetest.get_node_timer(pos):start(1)
return true return true
end end
-- place item back
techage.neighbour_unpull_items(pos, 6, items)
end
return false
end
local function push_item(pos, inv, meta)
if not inv:is_empty("main") then
local stack = inv:get_stack("main", 1)
local taken = stack:take_item(1)
if techage.neighbour_push_items(pos, meta:get_int("push_dir"), taken) then
inv:set_stack("main", 1, stack)
end
end
end
local function node_timer(pos, elapsed)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if inv then
if not pull_push_item(pos, meta) then
scan_for_objects({x=pos.x, y=pos.y+1, z=pos.z}, inv)
push_item(pos, inv, meta)
end
end
return true
end
minetest.register_node("techage:hopper_ta1", {
description = S("TA1 Hopper"),
tiles = {
-- up, down, right, left, back, front
"default_cobble.png^techage_appl_hopper_top.png",
"default_cobble.png^techage_appl_hopper.png",
"default_cobble.png^techage_appl_hopper_right.png",
"default_cobble.png^techage_appl_hopper.png",
"default_cobble.png^techage_appl_hopper.png",
"default_cobble.png^techage_appl_hopper.png",
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-8/16, 2/16, -8/16, 8/16, 8/16, -6/16},
{-8/16, 2/16, 6/16, 8/16, 8/16, 8/16},
{-8/16, 2/16, -8/16, -6/16, 8/16, 8/16},
{ 6/16, 2/16, -8/16, 8/16, 8/16, 8/16},
{-6/16, 0/16, -6/16, 6/16, 3/16, 6/16},
{-5/16, -4/16, -5/16, 5/16, 0/16, 5/16},
{ 0/16, -4/16, -3/16, 11/16, 2/16, 3/16},
},
},
selection_box = {
type = "fixed",
fixed = {
{-8/16, 2/16, -8/16, 8/16, 8/16, 8/16},
{-5/16, -4/16, -5/16, 5/16, 0/16, 5/16},
{ 0/16, -4/16, -3/16, 11/16, 2/16, 3/16},
},
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size('main', 1)
end, end,
listname = "src",
after_place_node = function(pos, placer)
techage.add_node(pos, "techage:hopper_ta1")
local node = minetest.get_node(pos)
M(pos):set_int("push_dir", techage.side_to_indir("L", node.param2))
minetest.get_node_timer(pos):start(2)
end,
on_timer = node_timer,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
techage.remove_node(pos)
tubelib2.del_mem(pos)
end,
on_rotate = screwdriver.disallow,
paramtype = "light",
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_craft({
output = "techage:hopper_ta1",
recipe = {
{"default:stone", "", "default:stone"},
{"default:stone", "default:gold_ingot", "default:stone"},
{"", "default:stone", ""},
}, },
}) take = {
listname = "src",
techage.register_node({"techage:hopper_ta1"}, { },
on_pull_item = nil, -- not needed }
on_unpull_item = nil, -- not needed )
on_push_item = function(pos, in_dir, stack)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return techage.put_items(inv, "main", stack)
end,
})

View File

@ -1,4 +1,4 @@
name = techage name = techage
depends = default,tubelib2,basic_materials,bucket,stairs,screwdriver depends = default,tubelib2,basic_materials,bucket,stairs,screwdriver,minecart
optional_depends = unified_inventory,wielded_light,minecart,unifieddyes optional_depends = unified_inventory,wielded_light,unifieddyes
description = Techage, go through 4 tech ages in search of wealth and power! description = Techage, go through 4 tech ages in search of wealth and power!