tubelib_addon3 with "high performance" nodes added

This commit is contained in:
Joachim Stolberg 2018-08-08 11:37:27 +02:00
parent e8bad31a17
commit cc5d98c585
10 changed files with 282 additions and 30 deletions

@ -298,6 +298,20 @@ function tubelib.unpull_items(pos, side, items, player_name)
return false return false
end end
function tubelib.pull_stack(pos, side, player_name)
local npos, facedir = get_neighbor_pos(pos, side)
if npos == nil then return end
local nside, node = get_node_side(npos, facedir)
local name = Name2Name[node.name]
if tubelib_NodeDef[name] then
if tubelib_NodeDef[name].on_pull_stack then
return tubelib_NodeDef[name].on_pull_stack(npos, nside, player_name)
elseif tubelib_NodeDef[name].on_pull_item then
return tubelib_NodeDef[name].on_pull_item(npos, nside, player_name)
end
end
return nil
end
------------------------------------------------------------------- -------------------------------------------------------------------
-- Server side helper functions -- Server side helper functions
@ -379,6 +393,20 @@ function tubelib.get_num_items(meta, listname, num)
return nil return nil
end end
function tubelib.get_stack(meta, listname)
local inv = meta:get_inventory()
local item = tubelib.get_item(meta, listname)
if item and inv:contains_item(listname, item) then
-- try to remove a complete stack
item:set_count(98)
local taken = inv:remove_item(listname, item)
-- add the already removed
taken:set_count(taken:get_count() + 1)
return taken
end
return item
end
-- Return "full", "loaded", or "empty" depending -- Return "full", "loaded", or "empty" depending
-- on the number of fuel stack items. -- on the number of fuel stack items.
-- Function only works on fuel inventories with one stacks/99 items -- Function only works on fuel inventories with one stacks/99 items

@ -33,21 +33,6 @@ local function allow_metadata_inventory_take(pos, listname, index, stack, player
return stack:get_count() return stack:get_count()
end end
local function get_stack(meta, list)
local inv = meta:get_inventory()
local item = tubelib.get_item(meta, list)
if item and inv:contains_item(list, item) then
-- try to remove a complete stack
item:set_count(98)
local taken = inv:remove_item(list, item)
-- add the already removed
taken:set_count(taken:get_count() + 1)
return taken
end
return item
end
local function formspec() local function formspec()
return "size[12,10]".. return "size[12,10]"..
default.gui_bg.. default.gui_bg..
@ -111,16 +96,20 @@ minetest.register_node("tubelib_addons3:chest", {
minetest.register_craft({ minetest.register_craft({
output = "tubelib_addons3:chest", output = "tubelib_addons3:chest",
recipe = { recipe = {
{"", "default:steel_ingot", ""}, {"default:tin_ingot", "tubelib_addons1:chest", ""},
{"tubelib_addons1:chest", "default:gold_ingot", "tubelib_addons1:chest"}, {"tubelib_addons1:chest", "default:gold_ingot", ""},
{"", "default:tin_ingot", ""}, {"", "", ""},
} },
}) })
tubelib.register_node("tubelib_addons3:chest", {}, { tubelib.register_node("tubelib_addons3:chest", {}, {
on_pull_stack = function(pos, side)
local meta = minetest.get_meta(pos)
return tubelib.get_stack(meta, "main")
end,
on_pull_item = function(pos, side) on_pull_item = function(pos, side)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
return get_stack(meta, "main") return tubelib.get_item(meta, "main")
end, end,
on_push_item = function(pos, side, item) on_push_item = function(pos, side, item)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)

@ -446,9 +446,9 @@ minetest.register_node("tubelib_addons3:distributor_active", {
minetest.register_craft({ minetest.register_craft({
output = "tubelib_addons3:distributor", output = "tubelib_addons3:distributor",
recipe = { recipe = {
{"", "default:steel_ingot", ""}, {"default:tin_ingot", "tubelib:distributor", ""},
{"default:gold_ingot", "tubelib:distributor", "default:tin_ingot"}, {"tubelib:distributor", "default:gold_ingot", ""},
{"", "default:steel_ingot", ""}, {"", "", ""},
}, },
}) })

@ -10,6 +10,7 @@
]]-- ]]--
dofile(minetest.get_modpath("tubelib_addons3") .. '/pushing_chest.lua')
dofile(minetest.get_modpath("tubelib_addons3") .. '/chest.lua') dofile(minetest.get_modpath("tubelib_addons3") .. '/chest.lua')
dofile(minetest.get_modpath("tubelib_addons3") .. '/distributor.lua') dofile(minetest.get_modpath("tubelib_addons3") .. '/pusher.lua')
dofile(minetest.get_modpath("tubelib_addons3") .. '/distributor.lua')
dofile(minetest.get_modpath("tubelib_addons3") .. '/pushing_chest.lua')

234
tubelib_addons3/pusher.lua Normal file

@ -0,0 +1,234 @@
--[[
Tubelib Addons 3
================
Copyright (C) 2018 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
pusher.lua
A high performance pusher
]]--
local RUNNING_STATE = 10
local function switch_on(pos, node)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
meta:set_int("running", RUNNING_STATE)
meta:set_string("infotext", "HighPerf Pusher "..number..": running")
node.name = "tubelib_addons3:pusher_active"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):start(2)
return false
end
local function switch_off(pos, node)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
meta:set_int("running", tubelib.STATE_STOPPED)
meta:set_string("infotext", "HighPerf Pusher "..number..": stopped")
node.name = "tubelib_addons3:pusher"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):stop()
return false
end
local function goto_standby(pos, node)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
meta:set_int("running", tubelib.STATE_STANDBY)
meta:set_string("infotext", "HighPerf Pusher "..number..": standby")
node.name = "tubelib_addons3:pusher"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):start(20)
return false
end
local function goto_blocked(pos, node)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
meta:set_int("running", tubelib.STATE_BLOCKED)
meta:set_string("infotext", "HighPerf Pusher "..number..": blocked")
node.name = "tubelib_addons3:pusher"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):start(20)
return false
end
local function keep_running(pos, elapsed)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
local running = meta:get_int("running") - 1
local player_name = meta:get_string("player_name")
local items = tubelib.pull_stack(pos, "L", player_name)
if items ~= nil then
if tubelib.push_items(pos, "R", items, player_name) == false then
-- place item back
tubelib.unpull_items(pos, "L", items, player_name)
local node = minetest.get_node(pos)
return goto_blocked(pos, node)
end
meta:set_int("item_counter", meta:get_int("item_counter") + 1)
if running <= 0 then
local node = minetest.get_node(pos)
return switch_on(pos, node)
else
-- reload running state
running = RUNNING_STATE
end
else
if running <= 0 then
local node = minetest.get_node(pos)
return goto_standby(pos, node)
end
end
meta:set_int("running", running)
return true
end
minetest.register_node("tubelib_addons3:pusher", {
description = "HighPerf Pusher",
tiles = {
-- up, down, right, left, back, front
'tubelib_pusher1.png^tubelib_addons3_node_frame4.png',
'tubelib_pusher1.png^tubelib_addons3_node_frame4.png',
'tubelib_outp.png^tubelib_addons3_node_frame4.png',
'tubelib_inp.png^tubelib_addons3_node_frame4.png',
"tubelib_pusher1.png^[transformR180]^tubelib_addons3_node_frame4.png^[transformR180]",
"tubelib_pusher1.png^tubelib_addons3_node_frame4.png",
},
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("player_name", placer:get_player_name())
local number = tubelib.add_node(pos, "tubelib_addons3:pusher")
meta:set_string("number", number)
meta:set_string("infotext", "HighPerf Pusher "..number..": stopped")
meta:set_int("item_counter", 0)
end,
on_rightclick = function(pos, node, clicker)
if not minetest.is_protected(pos, clicker:get_player_name()) then
switch_on(pos, node)
end
end,
after_dig_node = function(pos)
tubelib.remove_node(pos)
end,
on_timer = keep_running,
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_node("tubelib_addons3:pusher_active", {
description = "HighPerf Pusher",
tiles = {
-- up, down, right, left, back, front
{
image = "tubelib_addons3_pusher_active.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 2.0,
},
},
{
image = "tubelib_addons3_pusher_active.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 2.0,
},
},
'tubelib_outp.png^tubelib_addons3_node_frame4.png',
'tubelib_inp.png^tubelib_addons3_node_frame4.png',
{
image = "tubelib_addons3_pusher_active.png^[transformR180]",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 2.0,
},
},
{
image = "tubelib_addons3_pusher_active.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 2.0,
},
},
},
on_rightclick = function(pos, node, clicker)
if not minetest.is_protected(pos, clicker:get_player_name()) then
switch_off(pos, node)
end
end,
on_timer = keep_running,
on_rotate = screwdriver.disallow,
paramtype = "light",
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {crumbly=0, not_in_creative_inventory=1},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_craft({
output = "tubelib_addons3:pusher",
recipe = {
{"default:tin_ingot", "tubelib:pusher", ""},
{"tubelib:pusher", "default:gold_ingot", ""},
{"", "", ""},
},
})
tubelib.register_node("tubelib_addons3:pusher", {"tubelib_addons3:pusher_active"}, {
is_pusher = true, -- is a pulling/pushing node
on_recv_message = function(pos, topic, payload)
local node = minetest.get_node(pos)
if topic == "on" then
return switch_on(pos, node)
elseif topic == "off" then
return switch_off(pos, node)
elseif topic == "state" then
local meta = minetest.get_meta(pos)
local running = meta:get_int("running") or tubelib.STATE_STOPPED
return tubelib.statestring(running)
elseif topic == "counter" then
local meta = minetest.get_meta(pos)
return meta:get_int("item_counter")
elseif topic == "clear_counter" then
local meta = minetest.get_meta(pos)
return meta:set_int("item_counter", 0)
else
return "not supported"
end
end,
})

@ -100,7 +100,7 @@ minetest.register_node("tubelib_addons3:pushing_chest", {
tiles = { tiles = {
-- up, down, right, left, back, front -- up, down, right, left, back, front
{ {
image = "tubelib_addons3_pushing_chest.png", image = "tubelib_addons3_pusher_active.png",
backface_culling = false, backface_culling = false,
animation = { animation = {
type = "vertical_frames", type = "vertical_frames",
@ -165,10 +165,10 @@ minetest.register_node("tubelib_addons3:pushing_chest", {
minetest.register_craft({ minetest.register_craft({
output = "tubelib_addons3:pushing_chest", output = "tubelib_addons3:pushing_chest",
recipe = { recipe = {
{ "", "default:tin_ingot", ""}, {"default:tin_ingot", "tubelib_addons3:pusher", ""},
{"default:chest", "default:gold_ingot", "tubelib:distributor"}, {"tubelib_addons1:chest", "default:gold_ingot", ""},
{"", "default:steel_ingot", ""}, {"", "", ""},
} },
}) })
tubelib.register_node("tubelib_addons3:pushing_chest", {}, { tubelib.register_node("tubelib_addons3:pushing_chest", {}, {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 962 B

After

Width:  |  Height:  |  Size: 375 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 406 B

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB