techage_modpack/techage/basic_machines/consumer.lua

304 lines
9.2 KiB
Lua
Raw Normal View History

2020-05-31 23:31:18 +03:00
--[[
TechAge
=======
2022-01-03 13:59:31 +03:00
Copyright (C) 2019-2022 Joachim Stolberg
2020-05-31 23:31:18 +03:00
2020-10-25 23:32:47 +03:00
AGPL v3
2020-05-31 23:31:18 +03:00
See LICENSE.txt for more information
2022-01-04 21:43:30 +03:00
2020-05-31 23:31:18 +03:00
Consumer node basis functionality.
It handles:
2022-01-03 13:59:31 +03:00
- up to 4 stages of nodes (TA2/TA3/TA4/TA5)
2020-05-31 23:31:18 +03:00
- power consumption
- node state handling
- registration of passive and active nodes
- Tube connections are on left and right side (from left to right)
- Power connection are on front and back side (front or back)
]]--
-- 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
-- Consumer Related Data
local CRD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).consumer end
local CRDN = function(node) return (minetest.registered_nodes[node.name] or {}).consumer end
2021-08-01 12:00:22 +03:00
local Tube = techage.Tube
local power = networks.power
local liquid = networks.liquid
local CYCLE_TIME = 2
local function get_keys(tbl)
local keys = {}
for k,v in pairs(tbl) do
keys[#keys + 1] = k
end
return keys
end
2020-05-31 23:31:18 +03:00
local function has_power(pos, nvm, state)
local crd = CRD(pos)
return power.power_available(pos, crd.power_netw)
end
local function start_node(pos, nvm, state)
end
local function stop_node(pos, nvm, state)
end
2021-08-01 12:00:22 +03:00
local function node_timer_pas(pos, elapsed)
2020-05-31 23:31:18 +03:00
local crd = CRD(pos)
local nvm = techage.get_nvm(pos)
2022-01-04 21:43:30 +03:00
2021-08-01 12:00:22 +03:00
-- handle power consumption
if crd.power_netw and techage.needs_power(nvm) then
local consumed = power.consume_power(pos, crd.power_netw, nil, crd.power_consumption)
if consumed == crd.power_consumption then
crd.State:start(pos, nvm)
end
end
2022-01-04 21:43:30 +03:00
2021-08-01 12:00:22 +03:00
-- call the node timer routine
if techage.is_operational(nvm) then
nvm.node_timer_call_cyle = (nvm.node_timer_call_cyle or 0) + 1
if nvm.node_timer_call_cyle >= crd.call_cycle then
crd.node_timer(pos, crd.cycle_time)
nvm.node_timer_call_cyle = 0
end
end
return crd.State:is_active(nvm)
2020-05-31 23:31:18 +03:00
end
2021-08-01 12:00:22 +03:00
local function node_timer_act(pos, elapsed)
2020-05-31 23:31:18 +03:00
local crd = CRD(pos)
local nvm = techage.get_nvm(pos)
2022-01-04 21:43:30 +03:00
2021-08-01 12:00:22 +03:00
-- handle power consumption
2020-05-31 23:31:18 +03:00
if crd.power_netw and techage.needs_power(nvm) then
2021-08-01 12:00:22 +03:00
local consumed = power.consume_power(pos, crd.power_netw, nil, crd.power_consumption)
if consumed < crd.power_consumption then
crd.State:nopower(pos, nvm)
end
2020-05-31 23:31:18 +03:00
end
2022-01-04 21:43:30 +03:00
2020-05-31 23:31:18 +03:00
-- call the node timer routine
if techage.is_operational(nvm) then
2021-08-01 12:00:22 +03:00
nvm.node_timer_call_cyle = (nvm.node_timer_call_cyle or 0) + 1
if nvm.node_timer_call_cyle >= crd.call_cycle then
crd.node_timer(pos, crd.cycle_time)
nvm.node_timer_call_cyle = 0
end
2020-05-31 23:31:18 +03:00
end
return crd.State:is_active(nvm)
end
local function prepare_tiles(tiles, stage, power_png)
local tbl = {}
for _,item in ipairs(tiles) do
if type(item) == "string" then
2021-02-07 16:37:07 +03:00
tbl[#tbl+1] = item:gsub("#", stage):gsub("{power}", power_png):gsub("@@", '#')
2020-05-31 23:31:18 +03:00
else
local temp = table.copy(item)
2021-02-07 16:37:07 +03:00
temp.image = temp.image:gsub("#", stage):gsub("{power}", power_png):gsub("@@", '#')
2020-05-31 23:31:18 +03:00
tbl[#tbl+1] = temp
end
end
return tbl
end
2022-01-04 21:43:30 +03:00
-- 'validStates' is optional and can be used to e.g. enable
2020-05-31 23:31:18 +03:00
-- only one TA2 node {false, true, false, false}
2021-05-14 19:50:16 +03:00
function techage.register_consumer(base_name, inv_name, tiles, tNode, validStates, node_name_prefix)
2020-05-31 23:31:18 +03:00
local names = {}
validStates = validStates or {true, true, true, true}
2021-05-14 19:50:16 +03:00
if not node_name_prefix then
node_name_prefix = "techage:ta"
end
2022-01-03 13:59:31 +03:00
for stage = 2,5 do
2021-05-14 19:50:16 +03:00
local name_pas = node_name_prefix..stage.."_"..base_name.."_pas"
local name_act = node_name_prefix..stage.."_"..base_name.."_act"
2020-05-31 23:31:18 +03:00
local name_inv = "TA"..stage.." "..inv_name
names[#names+1] = name_pas
if validStates[stage] then
local power_network
local power_png = 'techage_axle_clutch.png'
local power_used = tNode.power_consumption ~= nil
2021-08-01 12:00:22 +03:00
local sides
2020-05-31 23:31:18 +03:00
-- power needed?
if power_used then
if stage > 2 then
power_network = techage.ElectricCable
power_png = 'techage_appl_hole_electric.png'
2021-08-01 12:00:22 +03:00
sides = get_keys(tNode.power_sides or {F=1, B=1, U=1, D=1})
2020-05-31 23:31:18 +03:00
else
power_network = techage.Axle
power_png = 'techage_axle_clutch.png'
2021-08-01 12:00:22 +03:00
sides = get_keys(tNode.power_sides or {F=1, B=1, U=1, D=1})
2020-05-31 23:31:18 +03:00
end
end
local tState = techage.NodeStates:new({
node_name_passive = name_pas,
node_name_active = name_act,
infotext_name = name_inv,
2021-08-01 12:00:22 +03:00
cycle_time = CYCLE_TIME,
2020-05-31 23:31:18 +03:00
standby_ticks = tNode.standby_ticks,
formspec_func = tNode.formspec,
on_state_change = tNode.on_state_change,
can_start = tNode.can_start,
2021-10-30 17:52:44 +03:00
quick_start = tNode.quick_start,
2020-05-31 23:31:18 +03:00
has_power = tNode.has_power or power_used and has_power or nil,
start_node = power_used and start_node or nil,
stop_node = power_used and stop_node or nil,
})
local tConsumer = {
stage = stage,
State = tState,
-- number of items to be processed per cycle
num_items = tNode.num_items and tNode.num_items[stage],
2022-01-04 21:43:30 +03:00
power_consumption = power_used and
2020-05-31 23:31:18 +03:00
tNode.power_consumption[stage] or 0,
node_timer = tNode.node_timer,
cycle_time = tNode.cycle_time,
2021-08-01 12:00:22 +03:00
call_cycle = tNode.cycle_time / 2,
2020-05-31 23:31:18 +03:00
power_netw = power_network,
}
local after_place_node = function(pos, placer, itemstack, pointed_thing)
local crd = CRD(pos)
local meta = M(pos)
local nvm = techage.get_nvm(pos)
local node = minetest.get_node(pos)
meta:set_int("push_dir", techage.side_to_indir("L", node.param2))
meta:set_int("pull_dir", techage.side_to_indir("R", node.param2))
2022-01-03 13:59:31 +03:00
meta:set_string("owner", placer:get_player_name())
2020-08-08 13:52:38 +03:00
-- Delete existing node number. Needed for Digtron compatibility.
if (meta:contains("node_number")) then
meta:set_string("node_number", "")
end
2020-12-11 20:07:29 +03:00
local number = techage.add_node(pos, name_pas, stage == 2)
2020-05-31 23:31:18 +03:00
if crd.power_netw then
crd.power_netw:after_place_node(pos)
end
if tNode.after_place_node then
tNode.after_place_node(pos, placer, itemstack, pointed_thing)
end
crd.State:node_init(pos, nvm, number)
end
local after_dig_node = function(pos, oldnode, oldmetadata, digger)
if tNode.after_dig_node then
tNode.after_dig_node(pos, oldnode, oldmetadata, digger)
end
local crd = CRDN(oldnode)
if crd.power_netw then
crd.power_netw:after_dig_node(pos)
end
2020-06-19 00:23:53 +03:00
techage.remove_node(pos, oldnode, oldmetadata)
2020-05-31 23:31:18 +03:00
techage.del_mem(pos)
end
2022-01-04 21:43:30 +03:00
2020-05-31 23:31:18 +03:00
tNode.groups.not_in_creative_inventory = 0
2021-05-14 19:50:16 +03:00
local def_pas = {
2020-05-31 23:31:18 +03:00
description = name_inv,
tiles = prepare_tiles(tiles.pas, stage, power_png),
consumer = tConsumer,
drawtype = tNode.drawtype,
node_box = tNode.node_box,
selection_box = tNode.selection_box,
can_dig = tNode.can_dig,
on_rotate = tNode.on_rotate or screwdriver.disallow,
2021-08-01 12:00:22 +03:00
on_timer = node_timer_pas,
2020-05-31 23:31:18 +03:00
on_receive_fields = tNode.on_receive_fields,
on_rightclick = tNode.on_rightclick,
after_place_node = after_place_node,
after_dig_node = after_dig_node,
2020-09-13 14:24:06 +03:00
preserve_metadata = tNode.preserve_metadata,
2020-05-31 23:31:18 +03:00
allow_metadata_inventory_put = tNode.allow_metadata_inventory_put,
allow_metadata_inventory_move = tNode.allow_metadata_inventory_move,
allow_metadata_inventory_take = tNode.allow_metadata_inventory_take,
on_metadata_inventory_move = tNode.on_metadata_inventory_move,
on_metadata_inventory_put = tNode.on_metadata_inventory_put,
on_metadata_inventory_take = tNode.on_metadata_inventory_take,
paramtype = tNode.paramtype,
paramtype2 = "facedir",
2020-08-08 13:52:38 +03:00
drop = tNode.drop,
2020-05-31 23:31:18 +03:00
groups = table.copy(tNode.groups),
is_ground_content = false,
sounds = tNode.sounds,
2021-05-14 19:50:16 +03:00
}
-- Copy custom properties (starting with an underscore)
for k,v in pairs(tNode) do
if string.sub(k, 1, 1) == "_" then
def_pas[k] = v
end
end
minetest.register_node(name_pas, def_pas)
2020-05-31 23:31:18 +03:00
tNode.groups.not_in_creative_inventory = 1
2021-05-14 19:50:16 +03:00
local def_act = {
2020-05-31 23:31:18 +03:00
description = name_inv,
tiles = prepare_tiles(tiles.act, stage, power_png),
consumer = tConsumer,
drawtype = tNode.drawtype,
node_box = tNode.node_box,
selection_box = tNode.selection_box,
2021-05-14 19:50:16 +03:00
2020-05-31 23:31:18 +03:00
on_rotate = tNode.on_rotate or screwdriver.disallow,
2021-08-01 12:00:22 +03:00
on_timer = node_timer_act,
2020-05-31 23:31:18 +03:00
on_receive_fields = tNode.on_receive_fields,
on_rightclick = tNode.on_rightclick,
after_place_node = after_place_node,
after_dig_node = after_dig_node,
allow_metadata_inventory_put = tNode.allow_metadata_inventory_put,
allow_metadata_inventory_move = tNode.allow_metadata_inventory_move,
allow_metadata_inventory_take = tNode.allow_metadata_inventory_take,
on_metadata_inventory_move = tNode.on_metadata_inventory_move,
on_metadata_inventory_put = tNode.on_metadata_inventory_put,
on_metadata_inventory_take = tNode.on_metadata_inventory_take,
paramtype = tNode.paramtype,
paramtype2 = "facedir",
drop = "",
diggable = false,
groups = table.copy(tNode.groups),
is_ground_content = false,
sounds = tNode.sounds,
2021-05-14 19:50:16 +03:00
}
-- Copy custom properties (starting with an underscore)
for k,v in pairs(tNode) do
if string.sub(k, 1, 1) == "_" then
def_act[k] = v
end
end
2022-01-04 21:43:30 +03:00
2021-05-14 19:50:16 +03:00
minetest.register_node(name_act, def_act)
2020-05-31 23:31:18 +03:00
if power_used then
2021-08-01 12:00:22 +03:00
power.register_nodes({name_pas, name_act}, power_network, "con", sides)
2020-05-31 23:31:18 +03:00
end
techage.register_node({name_pas, name_act}, tNode.tubing)
2022-01-04 21:43:30 +03:00
2021-08-01 12:00:22 +03:00
if tNode.tube_sides then
Tube:set_valid_sides(name_pas, get_keys(tNode.tube_sides))
Tube:set_valid_sides(name_act, get_keys(tNode.tube_sides))
end
2020-05-31 23:31:18 +03:00
end
end
2022-01-03 13:59:31 +03:00
return names[1], names[2], names[3], names[4]
2020-05-31 23:31:18 +03:00
end