master
Joachim Stolberg 2019-03-09 13:42:51 +01:00
parent 04815fea32
commit 6278a380e4
12 changed files with 297 additions and 46 deletions

257
basic_machines/pusher.lua Normal file
View File

@ -0,0 +1,257 @@
--[[
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
local MEM = tubelib2.get_mem
local RND = function(meta) return RegNodeData[meta:get_int('ta_stage')]] end
-- Load support for intllib.
local MP = minetest.get_modpath("tubelib2")
local I,_ = dofile(MP.."/intllib.lua")
local STANDBY_TICKS = 10
local COUNTDOWN_TICKS = 10
local CYCLE_TIME = 2
local RegNodeData = {}
local function register_pusher(idx)
RegNodeData[idx] = {}
RegNodeData[idx].State = techage.NodeStates:new({
node_name_passive= "techage:ta"..idx.."_pusher",
node_name_active = "techage:ta"..idx.."_pusher_active",
node_name_defect = "techage:ta"..idx.."_pusher_defect",
infotext_name = "TA"..idx.." Pusher",
cycle_time = CYCLE_TIME,
standby_ticks = STANDBY_TICKS,
has_item_meter = true,
aging_factor = 10,
})
local function pushing(pos, rnd, mem)
local items = techage.pull_items(pos, mem.pull_dir, rnd.num_items)
if items ~= nil then
if techage.push_items(pos, mem.push_dir, items) == false then
-- place item back
techage.unpull_items(pos, mem.pull_dir, items)
rnd.State:blocked(pos, mem)
return
end
rnd.State:keep_running(pos, mem, COUNTDOWN_TICKS)
return
end
rnd.State:idle(pos, mem)
end
local function keep_running(pos, elapsed)
local meta = M(pos)
pushing(pos, meta)
return State:is_active(meta)
end
minetest.register_node("tubelib:pusher", {
description = "Tubelib Pusher",
tiles = {
-- up, down, right, left, back, front
'tubelib_pusher1.png',
'tubelib_pusher1.png',
'tubelib_outp.png',
'tubelib_inp.png',
"tubelib_pusher1.png^[transformR180]",
"tubelib_pusher1.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:pusher") -- <<=== tubelib
this:node_init(pos, number)
end,
on_rightclick = function(pos, node, clicker)
if not minetest.is_protected(pos, clicker:get_player_name()) then
State:start(pos, M(pos))
end
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
tubelib.remove_node(pos) -- <<=== tubelib
State:after_dig_node(pos, oldnode, oldmetadata, digger)
end,
on_timer = keep_running,
on_rotate = screwdriver.disallow,
drop = "",
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:pusher_active", {
description = "Tubelib Pusher",
tiles = {
-- up, down, right, left, back, front
{
image = "tubelib_pusher.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 2.0,
},
},
{
image = "tubelib_pusher.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 2.0,
},
},
'tubelib_outp.png',
'tubelib_inp.png',
{
image = "tubelib_pusher.png^[transformR180]",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 2.0,
},
},
{
image = "tubelib_pusher.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
State:stop(pos, M(pos))
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_node("tubelib:pusher_defect", {
description = "Tubelib Pusher",
tiles = {
-- up, down, right, left, back, front
'tubelib_pusher1.png',
'tubelib_pusher1.png',
'tubelib_outp.png^tubelib_defect.png',
'tubelib_inp.png^tubelib_defect.png',
"tubelib_pusher1.png^[transformR180]^tubelib_defect.png",
"tubelib_pusher1.png^tubelib_defect.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:pusher") -- <<=== tubelib
State:node_init(pos, number)
State:defect(pos, meta)
end,
after_dig_node = function(pos)
tubelib.remove_node(pos) -- <<=== tubelib
end,
on_timer = keep_running,
on_rotate = screwdriver.disallow,
paramtype = "light",
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory=1},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_craft({
output = "tubelib:pusher 2",
recipe = {
{"group:wood", "wool:dark_green", "group:wood"},
{"tubelib:tubeS", "default:mese_crystal", "tubelib:tubeS"},
{"group:wood", "wool:dark_green", "group:wood"},
},
})
--------------------------------------------------------------- tubelib
tubelib.register_node("tubelib:pusher",
{"tubelib:pusher_active", "tubelib:pusher_defect"}, {
on_pull_item = nil, -- pusher has no inventory
on_push_item = nil, -- pusher has no inventory
on_unpull_item = nil, -- pusher has no inventory
is_pusher = true, -- is a pulling/pushing node
on_recv_message = function(pos, topic, payload)
local resp = State:on_receive_message(pos, topic, payload)
if resp then
return resp
else
return "unsupported"
end
end,
on_node_load = function(pos)
State:on_node_load(pos)
end,
on_node_repair = function(pos)
return State:on_node_repair(pos)
end,
})
--------------------------------------------------------------- tubelib
end

View File

@ -327,18 +327,17 @@ end
-- To be called after successful node action to raise the timer
-- and keep the node in state RUNNING
function NodeStates:keep_running(pos, mem, val, num_items)
num_items = num_items or 1
function NodeStates:keep_running(pos, mem, val)
-- set to RUNNING if not already done
self:start(pos, mem, true)
mem.techage_countdown = val
if self.has_item_meter then
mem.techage_item_meter = mem.techage_item_meter + (num_items or 1)
mem.techage_item_meter = mem.techage_item_meter + 1
end
if self.aging_level1 then
local cnt = mem.techage_aging + num_items
local cnt = mem.techage_aging + 1
mem.techage_aging = cnt
if (cnt > (self.aging_level1) and math.random(self.aging_level2/num_items) == 1)
if (cnt > (self.aging_level1) and math.random(self.aging_level2) == 1)
or cnt >= 999999 then
self:defect(pos, mem)
end

View File

@ -19,12 +19,10 @@ local M = minetest.get_meta
local TP = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).techage end
local TN = function(node) return (minetest.registered_nodes[node.name] or {}).techage end
-- Table to register the different power distribution network instances for global use
techage.Networks = {}
-- Used to determine the already passed nodes while power distribution
local Route = {}
-- Used to store the power input direction of each node
local PowerInDir = {}
local function pos_already_reached(pos)
local key = minetest.hash_node_position(pos)
@ -35,16 +33,6 @@ local function pos_already_reached(pos)
return true
end
local DirToSide = {"B", "R", "F", "L", "D", "U"}
local function dir_to_side(pos, dir)
local node = minetest.get_node(pos)
if dir < 5 then
dir = (((dir - 1) - (node.param2 % 4)) % 4) + 1
end
return DirToSide[dir]
end
local SideToDir = {B=1, R=2, F=3, L=4, D=5, U=6}
local function side_to_dir(pos, side)
@ -61,6 +49,14 @@ function techage.get_pos(pos, side)
return tubelib2.get_pos(pos, dir)
end
local function get_power_dir(pos)
local key = minetest.hash_node_position(pos)
if not PowerInDir[key] then
PowerInDir[key] = tubelib2.Turn180Deg[side_to_dir(pos, TP(pos).power_side or 'L')]
end
return PowerInDir[key]
end
local power_consumption = nil
local function call_read_power_consumption(pos, in_dir)
@ -81,7 +77,7 @@ power_consumption = function(pos, in_dir)
--local sum = 0
local mem = tubelib2.get_mem(pos)
local conn = mem.connections or {}
for out_dir,item in pairs(conn) do
for _,item in pairs(conn) do
if item.pos then
sum = sum + call_read_power_consumption(item.pos, item.in_dir)
end
@ -104,9 +100,8 @@ local turn_on = nil
local function call_turn_on(pos, in_dir, sum)
if not pos_already_reached(pos) then
local mem = tubelib2.get_mem(pos)
local this = TP(pos)
if this and (not this.valid_power_dir or this.valid_power_dir(pos, mem, in_dir)) then
if this and (not this.valid_power_dir or this.valid_power_dir(pos, get_power_dir(pos), in_dir)) then
if this.turn_on then
this.turn_on(pos, in_dir, sum)
end
@ -124,7 +119,7 @@ turn_on = function(pos, in_dir, sum)
call_turn_on(pos, in_dir, sum)
local mem = tubelib2.get_mem(pos)
local conn = mem.connections or {}
for out_dir,item in pairs(conn) do
for _,item in pairs(conn) do
if item.pos then
call_turn_on(item.pos, item.in_dir, sum)
end
@ -135,7 +130,6 @@ end
-- Starts the overall power consumption and depending on that turns all nodes on/off
local function start_network_power_consumption(pos, in_dir)
print("start_network_power_consumption")
local mem = tubelib2.get_mem(pos)
Route = {}
local sum = power_consumption(pos, in_dir)
Route = {}
@ -150,8 +144,6 @@ techage.generator = {}
function techage.generator.after_place_node(pos)
local mem = tubelib2.init_mem(pos)
-- Power_dir is in-dir
mem.power_dir = tubelib2.Turn180Deg[side_to_dir(pos, TP(pos).power_side or 'R')]
mem.power_produce = 0
TP(pos).power_network:after_place_node(pos)
return mem
@ -160,7 +152,8 @@ end
function techage.generator.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
-- check if contact side is correct
local mem = tubelib2.get_mem(pos)
if tubelib2.Turn180Deg[out_dir] == mem.power_dir then
local pwr_dir = get_power_dir(pos)
if tubelib2.Turn180Deg[out_dir] == pwr_dir then
if not peer_in_dir then
mem.connections = {} -- del connection
else
@ -168,7 +161,7 @@ function techage.generator.after_tube_update(node, pos, out_dir, peer_pos, peer_
mem.connections = {[out_dir] = {pos = peer_pos, in_dir = peer_in_dir}}
end
-- To be called delayed, so that all network connections have been established
minetest.after(0.2, start_network_power_consumption, pos, mem.power_dir)
minetest.after(0.2, start_network_power_consumption, pos, pwr_dir)
end
end
@ -177,13 +170,13 @@ function techage.generator.turn_power_on(pos, power_capacity)
mem.power_capacity = power_capacity
-- Starts the overall power consumption and depending on that turns all nodes on/off
-- To be called delayed, so that the generator state machine can be handled before
minetest.after(0.2, start_network_power_consumption, pos, mem.power_dir)
minetest.after(0.2, start_network_power_consumption, pos, get_power_dir(pos))
end
-- Power network callback function
function techage.generator.read_power_consumption(pos, in_dir)
local mem = tubelib2.get_mem(pos)
if in_dir == mem.power_dir then
if in_dir == get_power_dir(pos) then
return mem.power_capacity or 0
end
return 0
@ -241,7 +234,6 @@ techage.consumer = {}
function techage.consumer.after_place_node(pos, placer)
local mem = tubelib2.init_mem(pos)
-- Power_dir is in-dir
mem.power_dir = tubelib2.Turn180Deg[side_to_dir(pos, TP(pos).power_side or 'L')]
mem.power_consumption = 0
TP(pos).power_network:after_place_node(pos)
return mem
@ -249,9 +241,10 @@ end
function techage.consumer.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
local mem = tubelib2.get_mem(pos)
local pwr_dir = get_power_dir(pos)
mem.connections = mem.connections or {}
-- Check direction
if not TP(pos).valid_power_dir(pos, mem, tubelib2.Turn180Deg[out_dir]) then return end
--if not TP(pos).valid_power_dir(pos, pwr_dir, tubelib2.Turn180Deg[out_dir]) then return end
-- Only one connection is allowed, which can be overwritten, if necessary.
if not peer_pos or not next(mem.connections) or mem.connections[out_dir] then
if not peer_in_dir then
@ -261,7 +254,7 @@ function techage.consumer.after_tube_update(node, pos, out_dir, peer_pos, peer_i
end
end
-- To be called delayed, so that all network connections have been established
minetest.after(0.2, start_network_power_consumption, pos, mem.power_dir)
minetest.after(0.2, start_network_power_consumption, pos, pwr_dir)
end
function techage.consumer.turn_power_on(pos, power_consumption)
@ -269,14 +262,14 @@ function techage.consumer.turn_power_on(pos, power_consumption)
mem.power_consumption = power_consumption
-- Starts the overall power consumption and depending on that turns all nodes on/off
-- To be called delayed, so that the consumer state machine can be handled before
minetest.after(0.2, start_network_power_consumption, pos, mem.power_dir)
minetest.after(0.2, start_network_power_consumption, pos, get_power_dir(pos))
end
-- Power network callback function
function techage.consumer.read_power_consumption(pos, in_dir)
local mem = tubelib2.get_mem(pos)
-- Check direction
if not TP(pos).valid_power_dir(pos, mem, in_dir) then return 0 end
if not TP(pos).valid_power_dir(pos, get_power_dir(pos), in_dir) then return 0 end
return -(mem.power_consumption or 0)
end

View File

@ -24,8 +24,8 @@ local function formspec(self, pos, mem)
default.get_hotbar_bg(0, 3)
end
local function valid_power_dir(pos, mem, in_dir)
return mem.power_dir == in_dir
local function valid_power_dir(pos, power_dir, in_dir)
return power_dir == in_dir
end
local function start_node(pos, mem, state)

View File

@ -20,8 +20,8 @@ local function swap_node(pos, name)
minetest.swap_node(pos, node)
end
local function valid_power_dir(pos, mem, in_dir)
--print("valid_power_dir", mem.power_dir, in_dir)
local function valid_power_dir(pos, power_dir, in_dir)
--print("valid_power_dir", power_dir, in_dir)
return true
end

View File

@ -33,6 +33,8 @@ dofile(MP.."/electric/test.lua")
dofile(MP.."/electric/generator.lua")
dofile(MP.."/electric/consumer.lua")
--dofile(MP.."/basic_machines/pusher.lua")
--dofile(MP.."/fermenter/biogas_pipe.lua")
--dofile(MP.."/fermenter/gasflare.lua")

View File

@ -32,9 +32,9 @@ local function swap_node(pos, name)
end
-- To be able to check if power connection is on the
-- correct node side (mem.power_dir == in_dir)
local function valid_power_dir(pos, mem, in_dir)
return mem.power_dir == in_dir
-- correct node side (power_dir == in_dir)
local function valid_power_dir(pos, power_dir, in_dir)
return power_dir == in_dir
end
local function turn_on_clbk(pos, in_dir, sum)

View File

@ -38,8 +38,8 @@ local function swap_node(pos, name)
end
-- called from pipe network
local function valid_power_dir(pos, mem, in_dir)
return mem.power_dir == in_dir
local function valid_power_dir(pos, power_dir, in_dir)
return power_dir == in_dir
end
-- called from pipe network

View File

@ -100,8 +100,8 @@ local function node_timer(pos, elapsed)
return State:is_active(mem)
end
local function valid_power_dir(pos, mem, in_dir)
return mem.power_dir == in_dir
local function valid_power_dir(pos, power_dir, in_dir)
return power_dir == in_dir
end
local function turn_power_on(pos, in_dir, sum)