techage/basic_machines/pusher.lua

170 lines
4.6 KiB
Lua
Raw Normal View History

2019-03-09 15:42:51 +03:00
--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
TA2/TA3/TA4 Pusher
Simple node for push/pull operation of StackItems from chests or other
inventory/server nodes to tubes or other inventory/server nodes.
+--------+
/ /|
+--------+ |
IN (L) -->| |X--> OUT (R)
| PUSHER | +
| |/
+--------+
]]--
-- for lazy programmers
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local P = minetest.string_to_pos
local M = minetest.get_meta
2019-05-21 14:15:13 +03:00
-- Consumer Related Data
local CRD = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).consumer end
local CRDN = function(node) return (minetest.registered_nodes[node.name] or {}).consumer end
2019-03-09 15:42:51 +03:00
-- Load support for intllib.
2019-03-17 17:33:13 +03:00
local MP = minetest.get_modpath("techage")
2019-03-09 15:42:51 +03:00
local I,_ = dofile(MP.."/intllib.lua")
local STANDBY_TICKS = 10
local COUNTDOWN_TICKS = 10
local CYCLE_TIME = 2
2019-05-21 14:15:13 +03:00
local function pushing(pos, crd, meta, mem)
2019-03-09 21:26:15 +03:00
local pull_dir = meta:get_int("pull_dir")
local push_dir = meta:get_int("push_dir")
2019-05-21 14:15:13 +03:00
local items = techage.pull_items(pos, pull_dir, crd.num_items)
2019-03-09 21:26:15 +03:00
if items ~= nil then
2019-04-28 22:34:21 +03:00
if techage.push_items(pos, push_dir, items) ~= true then
2019-03-09 21:26:15 +03:00
-- place item back
techage.unpull_items(pos, pull_dir, items)
2019-05-21 14:15:13 +03:00
crd.State:blocked(pos, mem)
2019-03-09 21:26:15 +03:00
return
end
2019-05-21 14:15:13 +03:00
crd.State:keep_running(pos, mem, COUNTDOWN_TICKS)
2019-03-09 21:26:15 +03:00
return
end
2019-05-21 14:15:13 +03:00
crd.State:idle(pos, mem)
2019-03-09 21:26:15 +03:00
end
local function keep_running(pos, elapsed)
local mem = tubelib2.get_mem(pos)
2019-05-21 14:15:13 +03:00
local crd = CRD(pos)
pushing(pos, crd, M(pos), mem)
return crd.State:is_active(mem)
2019-03-09 21:26:15 +03:00
end
local function on_rightclick(pos, node, clicker)
local mem = tubelib2.get_mem(pos)
if not minetest.is_protected(pos, clicker:get_player_name()) then
2019-05-21 18:46:59 +03:00
if CRD(pos).State:get_state(mem) == techage.STOPPED then
2019-05-21 14:15:13 +03:00
CRD(pos).State:start(pos, mem)
2019-05-21 18:46:59 +03:00
else
CRD(pos).State:stop(pos, mem)
2019-03-09 21:26:15 +03:00
end
end
end
2019-03-17 17:33:13 +03:00
local tiles = {}
-- '#' will be replaced by the stage number
-- '{power}' will be replaced by the power PNG
tiles.pas = {
"techage_filling_ta#.png^techage_frame_ta#_top.png^techage_appl_arrow.png",
"techage_filling_ta#.png^techage_frame_ta#.png",
"techage_filling_ta#.png^techage_frame_ta#.png^techage_appl_outp.png",
"techage_filling_ta#.png^techage_frame_ta#.png^techage_appl_inp.png",
"techage_appl_pusher.png^[transformR180]^techage_frame_ta#.png",
"techage_appl_pusher.png^techage_frame_ta#.png",
}
tiles.act = {
-- up, down, right, left, back, front
"techage_filling_ta#.png^techage_frame_ta#_top.png^techage_appl_arrow.png",
"techage_filling_ta#.png^techage_frame_ta#.png",
"techage_filling_ta#.png^techage_frame_ta#.png^techage_appl_outp.png",
"techage_filling_ta#.png^techage_frame_ta#.png^techage_appl_inp.png",
{
image = "techage_appl_pusher14.png^[transformR180]^techage_frame14_ta#.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 2.0,
},
},
{
image = "techage_appl_pusher14.png^techage_frame14_ta#.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 2.0,
},
},
}
local tubing = {
is_pusher = true, -- is a pulling/pushing node
on_recv_message = function(pos, topic, payload)
2019-05-21 14:15:13 +03:00
local resp = CRD(pos).State:on_receive_message(pos, topic, payload)
2019-03-17 17:33:13 +03:00
if resp then
return resp
else
return "unsupported"
end
end,
on_node_load = function(pos)
2019-05-21 14:15:13 +03:00
CRD(pos).State:on_node_load(pos)
2019-03-17 17:33:13 +03:00
end,
}
local node_name_ta2, node_name_ta3, node_name_ta4 =
techage.register_consumer("pusher", I("Pusher"), tiles, {
2019-03-09 15:42:51 +03:00
cycle_time = CYCLE_TIME,
standby_ticks = STANDBY_TICKS,
2019-03-17 17:33:13 +03:00
tubing = tubing,
2019-03-09 15:42:51 +03:00
after_place_node = function(pos, placer)
2019-03-17 17:33:13 +03:00
local mem = tubelib2.get_mem(pos)
2019-03-09 21:26:15 +03:00
local meta = M(pos)
local node = minetest.get_node(pos)
meta:set_int("pull_dir", techage.side_to_outdir("L", node.param2))
meta:set_int("push_dir", techage.side_to_outdir("R", node.param2))
2019-03-09 15:42:51 +03:00
end,
2019-03-09 21:26:15 +03:00
on_rightclick = on_rightclick,
2019-03-17 17:33:13 +03:00
node_timer = keep_running,
2019-03-09 15:42:51 +03:00
on_rotate = screwdriver.disallow,
2019-03-09 21:26:15 +03:00
2019-03-17 17:33:13 +03:00
groups = {choppy=2, cracky=2, crumbly=2},
2019-03-09 15:42:51 +03:00
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
2019-03-17 17:33:13 +03:00
num_items = {0,2,6,18},
2019-03-09 15:42:51 +03:00
})
2019-03-10 13:36:00 +03:00
minetest.register_craft({
2019-03-17 17:33:13 +03:00
output = node_name_ta2.." 2",
2019-03-10 13:36:00 +03:00
recipe = {
2019-04-28 17:04:45 +03:00
{"group:wood", "wool:dark_green", "group:wood"},
{"techage:tubeS", "default:mese_crystal", "techage:tubeS"},
{"group:wood", "techage:iron_ingot", "group:wood"},
2019-03-10 13:36:00 +03:00
},
})
2019-05-11 02:21:03 +03:00
minetest.register_craft({
output = node_name_ta3,
recipe = {
{"", "techage:iron_ingot", ""},
{"", node_name_ta2, ""},
{"", "techage:vacuum_tube", ""},
},
})