2019-03-02 14:24:48 +03:00
|
|
|
--[[
|
|
|
|
|
|
|
|
TechAge
|
|
|
|
=======
|
|
|
|
|
2020-05-19 19:43:24 +03:00
|
|
|
Copyright (C) 2019-2020 Joachim Stolberg
|
2019-03-02 14:24:48 +03:00
|
|
|
|
2020-10-19 20:09:17 +03:00
|
|
|
AGPL v3
|
2019-03-02 14:24:48 +03:00
|
|
|
See LICENSE.txt for more information
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2019-03-02 14:24:48 +03:00
|
|
|
Basis functions for inter-node communication
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
--- for lazy programmers
|
|
|
|
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
|
2020-05-19 19:43:24 +03:00
|
|
|
--local P = minetest.string_to_pos
|
|
|
|
--local M = minetest.get_meta
|
2022-11-14 22:00:00 +03:00
|
|
|
local has_mesecons = minetest.global_exists("mesecon")
|
2019-03-02 14:24:48 +03:00
|
|
|
|
2020-05-19 19:43:24 +03:00
|
|
|
local NodeInfoCache = {}
|
2020-06-14 00:32:00 +03:00
|
|
|
local NumbersToBeRecycled = {}
|
2020-05-19 19:43:24 +03:00
|
|
|
local MP = minetest.get_modpath("techage")
|
2020-05-23 15:11:35 +03:00
|
|
|
local techage_use_sqlite = minetest.settings:get_bool('techage_use_sqlite', false)
|
2020-05-19 19:43:24 +03:00
|
|
|
|
|
|
|
-- Localize functions to avoid table lookups (better performance)
|
|
|
|
local string_split = string.split
|
|
|
|
local NodeDef = techage.NodeDef
|
|
|
|
local Tube = techage.Tube
|
2021-06-06 11:41:32 +03:00
|
|
|
local is_cart_available = minecart.is_nodecart_available
|
2021-10-12 20:57:13 +03:00
|
|
|
local techage_counting_hit = techage.counting_hit
|
2022-01-05 23:41:30 +03:00
|
|
|
local tubelib2_side_to_dir = tubelib2.side_to_dir
|
2019-03-02 14:24:48 +03:00
|
|
|
|
2020-05-19 19:43:24 +03:00
|
|
|
-------------------------------------------------------------------
|
|
|
|
-- Database
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
local backend
|
2020-05-23 15:11:35 +03:00
|
|
|
if techage_use_sqlite then
|
|
|
|
backend = dofile(MP .. "/basis/numbers_sqlite.lua")
|
2020-05-19 19:43:24 +03:00
|
|
|
else
|
2020-05-23 15:11:35 +03:00
|
|
|
backend = dofile(MP .. "/basis/numbers_storage.lua")
|
2020-01-31 21:55:10 +03:00
|
|
|
end
|
|
|
|
|
2020-05-19 19:43:24 +03:00
|
|
|
local function update_nodeinfo(number)
|
|
|
|
local pos = backend.get_nodepos(number)
|
|
|
|
if pos then
|
|
|
|
NodeInfoCache[number] = {pos = pos, name = techage.get_node_lvm(pos).name}
|
|
|
|
return NodeInfoCache[number]
|
2020-01-31 21:55:10 +03:00
|
|
|
end
|
|
|
|
end
|
2019-03-02 14:24:48 +03:00
|
|
|
|
2020-05-19 19:43:24 +03:00
|
|
|
local function delete_nodeinfo_entry(number)
|
|
|
|
if number and NodeInfoCache[number] then
|
2020-05-23 15:11:35 +03:00
|
|
|
number = next(NodeInfoCache, number)
|
2020-05-19 19:43:24 +03:00
|
|
|
if number then
|
|
|
|
NodeInfoCache[number] = nil
|
|
|
|
end
|
|
|
|
else
|
2020-05-23 15:11:35 +03:00
|
|
|
number = next(NodeInfoCache, nil)
|
2020-05-19 19:43:24 +03:00
|
|
|
end
|
|
|
|
return number
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
|
2022-01-03 23:40:31 +03:00
|
|
|
-- Keep the cache size small by deleting entries randomly
|
2020-05-19 19:43:24 +03:00
|
|
|
local function keep_small(number)
|
|
|
|
number = delete_nodeinfo_entry(number)
|
2020-05-19 23:24:50 +03:00
|
|
|
minetest.after(10, keep_small, number)
|
2020-05-19 19:43:24 +03:00
|
|
|
end
|
2019-03-02 14:24:48 +03:00
|
|
|
|
2020-05-19 19:43:24 +03:00
|
|
|
keep_small()
|
2019-03-02 14:24:48 +03:00
|
|
|
|
2020-05-19 19:43:24 +03:00
|
|
|
minetest.after(2, backend.delete_invalid_entries, NodeDef)
|
2019-03-02 14:24:48 +03:00
|
|
|
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
-- Local helper functions
|
|
|
|
-------------------------------------------------------------------
|
2019-06-08 23:57:01 +03:00
|
|
|
local function in_list(list, x)
|
|
|
|
for _, v in ipairs(list) do
|
|
|
|
if v == x then return true end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2019-03-02 14:24:48 +03:00
|
|
|
-- Determine position related node number for addressing purposes
|
2020-05-19 19:43:24 +03:00
|
|
|
local function get_number(pos, new)
|
|
|
|
local meta = minetest.get_meta(pos)
|
2022-01-03 23:40:31 +03:00
|
|
|
if meta:contains("node_number") then
|
|
|
|
return meta:get_string("node_number")
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
2020-05-19 19:43:24 +03:00
|
|
|
-- generate new number
|
|
|
|
if new then
|
|
|
|
local num = backend.add_nodepos(pos)
|
2022-01-03 23:40:31 +03:00
|
|
|
meta:set_string("node_number", num)
|
2020-05-19 19:43:24 +03:00
|
|
|
return num
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function not_protected(pos, placer_name, clicker_name)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
if meta then
|
2019-08-24 00:35:37 +03:00
|
|
|
if placer_name and not minetest.is_protected(pos, placer_name) then
|
|
|
|
if clicker_name == nil or placer_name == clicker_name then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
if not minetest.is_protected(pos, clicker_name) then
|
2019-03-02 14:24:48 +03:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function register_lbm(name, nodenames)
|
|
|
|
minetest.register_lbm({
|
2019-05-21 01:05:53 +03:00
|
|
|
label = "[TechAge] Node update",
|
2019-03-02 14:24:48 +03:00
|
|
|
name = name.."update",
|
|
|
|
nodenames = nodenames,
|
|
|
|
run_at_every_load = true,
|
|
|
|
action = function(pos, node)
|
2019-06-08 23:57:01 +03:00
|
|
|
if NodeDef[node.name] and NodeDef[node.name].on_node_load then
|
2020-01-26 01:15:44 +03:00
|
|
|
NodeDef[node.name].on_node_load(pos, node)
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
local SideToDir = {B=1, R=2, F=3, L=4, D=5, U=6}
|
|
|
|
|
|
|
|
local function side_to_dir(side, param2)
|
2022-01-05 23:41:30 +03:00
|
|
|
return tubelib2_side_to_dir(side, param2)
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
|
2019-03-09 21:26:15 +03:00
|
|
|
techage.side_to_outdir = side_to_dir
|
|
|
|
|
2019-03-10 13:36:00 +03:00
|
|
|
function techage.side_to_indir(side, param2)
|
|
|
|
return tubelib2.Turn180Deg[side_to_dir(side, param2)]
|
|
|
|
end
|
|
|
|
|
2019-05-01 13:01:24 +03:00
|
|
|
local function get_next_node(pos, out_dir)
|
|
|
|
local res, npos, node = Tube:compatible_node(pos, out_dir)
|
|
|
|
local in_dir = tubelib2.Turn180Deg[out_dir]
|
2022-01-03 23:40:31 +03:00
|
|
|
return res, npos, in_dir, node.name
|
2019-05-01 13:01:24 +03:00
|
|
|
end
|
|
|
|
|
2019-03-02 22:51:16 +03:00
|
|
|
local function get_dest_node(pos, out_dir)
|
|
|
|
local spos, in_dir = Tube:get_connected_node_pos(pos, out_dir)
|
|
|
|
local _,node = Tube:get_node(spos)
|
2022-01-03 23:40:31 +03:00
|
|
|
return spos, in_dir, node.name
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2019-03-02 14:24:48 +03:00
|
|
|
local function item_handling_node(name)
|
|
|
|
local node_def = name and NodeDef[name]
|
|
|
|
if node_def then
|
|
|
|
return node_def.on_pull_item or node_def.on_push_item or node_def.is_pusher
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-15 00:59:16 +03:00
|
|
|
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
|
|
|
|
|
2021-06-05 12:42:30 +03:00
|
|
|
techage.SystemTime = 0
|
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
techage.SystemTime = techage.SystemTime + dtime
|
|
|
|
end)
|
|
|
|
|
2021-06-05 21:18:04 +03:00
|
|
|
-- used by TA1 hammer: dug_node[player_name] = pos
|
|
|
|
techage.dug_node = {}
|
|
|
|
minetest.register_on_dignode(function(pos, oldnode, digger)
|
|
|
|
if not digger then return end
|
2022-11-07 23:37:47 +03:00
|
|
|
-- store the position of the dug block for tools like the TA1 hammer
|
2021-06-05 21:18:04 +03:00
|
|
|
techage.dug_node[digger:get_player_name()] = pos
|
|
|
|
end)
|
|
|
|
|
2019-03-02 14:24:48 +03:00
|
|
|
-------------------------------------------------------------------
|
|
|
|
-- API helper functions
|
|
|
|
-------------------------------------------------------------------
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2023-11-01 20:12:00 +03:00
|
|
|
-- Check if both strings are the same or one string starts with the other string.
|
|
|
|
function techage.string_compare(s1, s2)
|
|
|
|
if s1 and s2 then
|
|
|
|
local minLength = math.min(#s1, #s2)
|
|
|
|
return string.sub(s1, 1, minLength) == string.sub(s2, 1, minLength)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-19 19:43:24 +03:00
|
|
|
-- Function returns { pos, name } for the node referenced by number
|
2019-03-02 14:24:48 +03:00
|
|
|
function techage.get_node_info(dest_num)
|
2020-05-19 19:43:24 +03:00
|
|
|
return NodeInfoCache[dest_num] or update_nodeinfo(dest_num)
|
2022-01-03 23:40:31 +03:00
|
|
|
end
|
2019-03-02 14:24:48 +03:00
|
|
|
|
|
|
|
-- Function returns the node number from the given position or
|
|
|
|
-- nil, if no node number for this position is assigned.
|
|
|
|
function techage.get_node_number(pos)
|
2020-05-19 19:43:24 +03:00
|
|
|
return get_number(pos)
|
2022-01-03 23:40:31 +03:00
|
|
|
end
|
2019-03-02 14:24:48 +03:00
|
|
|
|
2020-01-26 01:15:44 +03:00
|
|
|
function techage.get_pos(pos, side)
|
|
|
|
local node = techage.get_node_lvm(pos)
|
|
|
|
local dir = nil
|
|
|
|
if node.name ~= "air" and node.name ~= "ignore" then
|
|
|
|
dir = side_to_dir(side, node.param2)
|
|
|
|
end
|
|
|
|
return tubelib2.get_pos(pos, dir)
|
2022-01-03 23:40:31 +03:00
|
|
|
end
|
2020-01-26 01:15:44 +03:00
|
|
|
|
2019-03-02 14:24:48 +03:00
|
|
|
-- Function is used for available nodes with lost numbers, only.
|
|
|
|
function techage.get_new_number(pos, name)
|
2022-01-03 23:40:31 +03:00
|
|
|
-- store position
|
2020-05-19 19:43:24 +03:00
|
|
|
return get_number(pos, true)
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
|
2019-08-07 23:26:38 +03:00
|
|
|
-- extract ident and value from strings like "ident=value"
|
|
|
|
function techage.ident_value(s)
|
|
|
|
local ident, value = unpack(string.split(s, "=", true, 1))
|
|
|
|
return (ident or ""):trim(), (value or ""):trim()
|
|
|
|
end
|
|
|
|
|
2019-03-02 14:24:48 +03:00
|
|
|
-------------------------------------------------------------------
|
|
|
|
-- Node construction/destruction functions
|
|
|
|
-------------------------------------------------------------------
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2019-03-02 14:24:48 +03:00
|
|
|
-- Add node to the techage lists.
|
|
|
|
-- Function determines and returns the node position number,
|
|
|
|
-- needed for message communication.
|
2020-10-30 19:49:01 +03:00
|
|
|
-- If TA2 node, return '-' instead of a real number, because
|
|
|
|
-- TA2 nodes should not support number based commands.
|
|
|
|
function techage.add_node(pos, name, is_ta2)
|
2019-03-02 14:24:48 +03:00
|
|
|
if item_handling_node(name) then
|
|
|
|
Tube:after_place_node(pos)
|
|
|
|
end
|
2022-01-03 23:40:31 +03:00
|
|
|
if is_ta2 then
|
2020-10-30 19:49:01 +03:00
|
|
|
return "-"
|
|
|
|
end
|
2020-06-14 00:32:00 +03:00
|
|
|
local key = minetest.hash_node_position(pos)
|
2020-12-11 17:50:40 +03:00
|
|
|
local num = NumbersToBeRecycled[key]
|
|
|
|
if num then
|
2022-08-09 21:27:33 +03:00
|
|
|
NodeInfoCache[num] = nil
|
2020-12-11 17:50:40 +03:00
|
|
|
backend.set_nodepos(num, pos)
|
|
|
|
NumbersToBeRecycled[key] = nil
|
|
|
|
return num
|
|
|
|
end
|
|
|
|
return get_number(pos, true)
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Function removes the node from the techage lists.
|
2020-06-14 00:32:00 +03:00
|
|
|
function techage.remove_node(pos, oldnode, oldmetadata)
|
2020-12-12 13:22:30 +03:00
|
|
|
local number = oldmetadata and oldmetadata.fields and (oldmetadata.fields.node_number or oldmetadata.fields.number)
|
2020-06-14 00:32:00 +03:00
|
|
|
number = number or get_number(pos)
|
2020-08-02 23:06:55 +03:00
|
|
|
if number and tonumber(number) then
|
2020-06-14 00:32:00 +03:00
|
|
|
local key = minetest.hash_node_position(pos)
|
|
|
|
NumbersToBeRecycled[key] = number
|
2020-10-30 19:49:01 +03:00
|
|
|
NodeInfoCache[number] = nil
|
|
|
|
end
|
|
|
|
if oldnode and item_handling_node(oldnode.name) then
|
|
|
|
Tube:after_dig_node(pos)
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-12 17:35:26 +03:00
|
|
|
-- Repairs the node number after it was erased by `backend.delete_invalid_entries`
|
|
|
|
function techage.repair_number(pos)
|
|
|
|
local number = techage.get_node_number(pos)
|
|
|
|
if number then
|
|
|
|
backend.set_nodepos(number, pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-30 17:05:20 +03:00
|
|
|
-- Like techage.add_node, but use the old number again
|
|
|
|
function techage.unpack_node(pos, name, number)
|
|
|
|
if item_handling_node(name) then
|
|
|
|
Tube:after_place_node(pos)
|
|
|
|
end
|
|
|
|
local key = minetest.hash_node_position(pos)
|
|
|
|
NumbersToBeRecycled[key] = nil
|
|
|
|
if number then
|
|
|
|
backend.set_nodepos(number, pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Like techage.remove_node but don't store the number for this position
|
|
|
|
function techage.pack_node(pos, oldnode, number)
|
|
|
|
if number then
|
|
|
|
NodeInfoCache[number] = nil
|
|
|
|
end
|
|
|
|
if oldnode and item_handling_node(oldnode.name) then
|
|
|
|
Tube:after_dig_node(pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-16 22:03:46 +03:00
|
|
|
-------------------------------------------------------------------
|
|
|
|
-- Used by the assembly tool
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
function techage.pre_add_node(pos, number)
|
|
|
|
local key = minetest.hash_node_position(pos)
|
|
|
|
NumbersToBeRecycled[key] = number
|
|
|
|
end
|
|
|
|
|
|
|
|
function techage.post_remove_node(pos)
|
|
|
|
local key = minetest.hash_node_position(pos)
|
|
|
|
NumbersToBeRecycled[key] = nil
|
|
|
|
end
|
2019-03-02 14:24:48 +03:00
|
|
|
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
-- Node register function
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Register node for techage communication
|
|
|
|
-- Call this function only at load time!
|
2019-06-08 23:57:01 +03:00
|
|
|
-- Param names: List of node names like {"techage:pusher_off", "techage:pusher_on"}
|
2019-03-02 14:24:48 +03:00
|
|
|
-- Param node_definition: A table according to:
|
|
|
|
-- {
|
2021-03-08 23:27:51 +03:00
|
|
|
-- on_inv_request = func(pos, in_dir, access_type)
|
2020-03-22 13:01:44 +03:00
|
|
|
-- on_pull_item = func(pos, in_dir, num, (opt.) item_name),
|
2019-03-09 21:26:15 +03:00
|
|
|
-- on_push_item = func(pos, in_dir, item),
|
|
|
|
-- on_unpull_item = func(pos, in_dir, item),
|
2019-08-25 19:08:30 +03:00
|
|
|
-- on_recv_message = func(pos, src, topic, payload),
|
2019-03-02 14:24:48 +03:00
|
|
|
-- on_node_load = func(pos), -- LBM function
|
2019-06-08 23:57:01 +03:00
|
|
|
-- on_transfer = func(pos, in_dir, topic, payload),
|
2019-03-02 14:24:48 +03:00
|
|
|
-- }
|
2019-06-08 23:57:01 +03:00
|
|
|
function techage.register_node(names, node_definition)
|
2019-03-02 14:24:48 +03:00
|
|
|
-- store facedir table for all known node names
|
2019-06-08 23:57:01 +03:00
|
|
|
for _,n in ipairs(names) do
|
|
|
|
NodeDef[n] = node_definition
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
2022-01-03 23:40:31 +03:00
|
|
|
if node_definition.on_pull_item or node_definition.on_push_item or
|
2019-03-02 14:24:48 +03:00
|
|
|
node_definition.is_pusher then
|
2019-06-08 23:57:01 +03:00
|
|
|
Tube:add_secondary_node_names(names)
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2019-06-08 23:57:01 +03:00
|
|
|
for _,n in ipairs(names) do
|
2019-03-02 14:24:48 +03:00
|
|
|
techage.KnownNodes[n] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- register LBM
|
|
|
|
if node_definition.on_node_load then
|
2019-06-08 23:57:01 +03:00
|
|
|
register_lbm(names[1], names)
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
2022-11-05 16:54:00 +03:00
|
|
|
|
|
|
|
-- register mvps stopper
|
|
|
|
if has_mesecons then
|
|
|
|
for _, name in ipairs(names) do
|
|
|
|
mesecon.register_mvps_stopper(name)
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
-- Send message functions
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
|
2019-08-07 23:26:38 +03:00
|
|
|
function techage.not_protected(number, placer_name, clicker_name)
|
2020-05-19 19:43:24 +03:00
|
|
|
local ninfo = NodeInfoCache[number] or update_nodeinfo(number)
|
|
|
|
if ninfo and ninfo.pos then
|
|
|
|
return not_protected(ninfo.pos, placer_name, clicker_name)
|
2019-08-07 23:26:38 +03:00
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2022-12-11 11:49:00 +03:00
|
|
|
-- Check the given number value.
|
|
|
|
-- Returns true if the number is valid, point to real node and
|
|
|
|
-- and the node is not protected for the given player_name.
|
|
|
|
function techage.check_number(number, placer_name)
|
|
|
|
if number then
|
|
|
|
if not techage.not_protected(number, placer_name, nil) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2019-08-24 00:35:37 +03:00
|
|
|
-- Check the given list of numbers.
|
|
|
|
-- Returns true if number(s) is/are valid, point to real nodes and
|
|
|
|
-- and the nodes are not protected for the given player_name.
|
|
|
|
function techage.check_numbers(numbers, placer_name)
|
|
|
|
if numbers then
|
|
|
|
for _,num in ipairs(string_split(numbers, " ")) do
|
|
|
|
if not techage.not_protected(num, placer_name, nil) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
2022-01-03 23:40:31 +03:00
|
|
|
end
|
2019-08-24 00:35:37 +03:00
|
|
|
|
2019-08-25 19:08:30 +03:00
|
|
|
function techage.send_multi(src, numbers, topic, payload)
|
2020-02-29 21:20:54 +03:00
|
|
|
--print("send_multi", src, numbers, topic)
|
2019-03-02 14:24:48 +03:00
|
|
|
for _,num in ipairs(string_split(numbers, " ")) do
|
2020-05-19 19:43:24 +03:00
|
|
|
local ninfo = NodeInfoCache[num] or update_nodeinfo(num)
|
|
|
|
if ninfo and ninfo.name and ninfo.pos then
|
|
|
|
local ndef = NodeDef[ninfo.name]
|
|
|
|
if ndef and ndef.on_recv_message then
|
2021-10-12 20:57:13 +03:00
|
|
|
techage_counting_hit()
|
2020-05-19 19:43:24 +03:00
|
|
|
ndef.on_recv_message(ninfo.pos, src, topic, payload)
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-01-03 23:40:31 +03:00
|
|
|
end
|
2019-03-02 14:24:48 +03:00
|
|
|
|
2019-08-25 19:08:30 +03:00
|
|
|
function techage.send_single(src, number, topic, payload)
|
2020-02-29 21:20:54 +03:00
|
|
|
--print("send_single", src, number, topic)
|
2020-05-19 19:43:24 +03:00
|
|
|
local ninfo = NodeInfoCache[number] or update_nodeinfo(number)
|
|
|
|
if ninfo and ninfo.name and ninfo.pos then
|
|
|
|
local ndef = NodeDef[ninfo.name]
|
|
|
|
if ndef and ndef.on_recv_message then
|
2021-10-12 20:57:13 +03:00
|
|
|
techage_counting_hit()
|
2020-05-19 19:43:24 +03:00
|
|
|
return ndef.on_recv_message(ninfo.pos, src, topic, payload)
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
2022-01-03 23:40:31 +03:00
|
|
|
end
|
2019-03-02 14:24:48 +03:00
|
|
|
|
2019-06-08 23:57:01 +03:00
|
|
|
-- The destination node location is either:
|
|
|
|
-- A) a destination position, specified by pos
|
|
|
|
-- B) a neighbor position, specified by caller pos/outdir, or pos/side
|
|
|
|
-- C) a tubelib2 network connection, specified by caller pos/outdir, or pos/side
|
|
|
|
-- outdir is one of: 1..6
|
|
|
|
-- side is one of: "B", "R", "F", "L", "D", "U"
|
|
|
|
-- network is a tuebelib2 network instance
|
|
|
|
-- opt: nodenames is a table of valid the callee node names
|
|
|
|
function techage.transfer(pos, outdir, topic, payload, network, nodenames)
|
|
|
|
-- determine out-dir
|
|
|
|
if outdir and type(outdir) == "string" then
|
2019-06-09 01:25:25 +03:00
|
|
|
local param2 = techage.get_node_lvm(pos).param2
|
2019-06-08 23:57:01 +03:00
|
|
|
outdir = side_to_dir(outdir, param2)
|
|
|
|
end
|
|
|
|
-- determine destination pos
|
|
|
|
local dpos, indir
|
|
|
|
if network then
|
|
|
|
dpos, indir = network:get_connected_node_pos(pos, outdir)
|
|
|
|
else
|
2019-06-10 22:31:58 +03:00
|
|
|
dpos, indir = tubelib2.get_pos(pos, outdir), outdir
|
2019-06-08 23:57:01 +03:00
|
|
|
end
|
|
|
|
-- check node name
|
2019-06-09 01:25:25 +03:00
|
|
|
local name = techage.get_node_lvm(dpos).name
|
2019-06-08 23:57:01 +03:00
|
|
|
if nodenames and not in_list(nodenames, name) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
-- call "on_transfer"
|
|
|
|
local ndef = NodeDef[name]
|
|
|
|
if ndef and ndef.on_transfer then
|
|
|
|
return ndef.on_transfer(dpos, indir, topic, payload)
|
|
|
|
end
|
|
|
|
return false
|
2022-01-03 23:40:31 +03:00
|
|
|
end
|
2019-06-08 23:57:01 +03:00
|
|
|
|
2022-06-05 22:20:43 +03:00
|
|
|
-------------------------------------------------------------------
|
|
|
|
-- Beduino functions (see "bep-005_ta_cmnd.md")
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
function techage.beduino_send_cmnd(src, number, topic, payload)
|
|
|
|
--print("beduino_send_cmnd", src, number, topic)
|
|
|
|
local ninfo = NodeInfoCache[number] or update_nodeinfo(number)
|
|
|
|
if ninfo and ninfo.name and ninfo.pos then
|
|
|
|
local ndef = NodeDef[ninfo.name]
|
|
|
|
if ndef and ndef.on_beduino_receive_cmnd then
|
|
|
|
return ndef.on_beduino_receive_cmnd(ninfo.pos, src, topic, payload or {})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 1, ""
|
|
|
|
end
|
|
|
|
|
|
|
|
function techage.beduino_request_data(src, number, topic, payload)
|
|
|
|
--print("beduino_request_data", src, number, topic)
|
|
|
|
local ninfo = NodeInfoCache[number] or update_nodeinfo(number)
|
|
|
|
if ninfo and ninfo.name and ninfo.pos then
|
|
|
|
local ndef = NodeDef[ninfo.name]
|
|
|
|
if ndef and ndef.on_beduino_request_data then
|
|
|
|
return ndef.on_beduino_request_data(ninfo.pos, src, topic, payload or {})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 1, ""
|
|
|
|
end
|
|
|
|
|
2019-03-02 14:24:48 +03:00
|
|
|
-------------------------------------------------------------------
|
|
|
|
-- Client side Push/Pull item functions
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
|
2021-03-08 23:27:51 +03:00
|
|
|
function techage.get_inv_access(pos, out_dir, access_type)
|
|
|
|
local npos, in_dir, name = get_dest_node(pos, out_dir)
|
|
|
|
if npos and NodeDef[name] and NodeDef[name].on_inv_request then
|
|
|
|
return NodeDef[name].on_inv_request(npos, in_dir, access_type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-22 13:01:44 +03:00
|
|
|
function techage.pull_items(pos, out_dir, num, item_name)
|
2019-03-02 22:51:16 +03:00
|
|
|
local npos, in_dir, name = get_dest_node(pos, out_dir)
|
2019-05-01 13:01:24 +03:00
|
|
|
if npos and NodeDef[name] and NodeDef[name].on_pull_item then
|
2020-03-22 13:01:44 +03:00
|
|
|
return NodeDef[name].on_pull_item(npos, in_dir, num, item_name)
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-13 14:20:38 +03:00
|
|
|
function techage.push_items(pos, out_dir, stack, idx)
|
2019-03-02 22:51:16 +03:00
|
|
|
local npos, in_dir, name = get_dest_node(pos, out_dir)
|
2019-05-01 13:01:24 +03:00
|
|
|
if npos and NodeDef[name] and NodeDef[name].on_push_item then
|
2022-01-03 23:40:31 +03:00
|
|
|
return NodeDef[name].on_push_item(npos, in_dir, stack, idx)
|
2021-04-22 18:32:02 +03:00
|
|
|
elseif is_air_like(name) or is_cart_available(npos) then
|
2019-03-02 14:24:48 +03:00
|
|
|
minetest.add_item(npos, stack)
|
2022-01-03 23:40:31 +03:00
|
|
|
return true
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
2022-09-02 22:12:25 +03:00
|
|
|
return stack
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
|
2021-09-14 21:07:35 +03:00
|
|
|
-- Check for recursion and too long distances
|
|
|
|
local start_pos
|
|
|
|
function techage.safe_push_items(pos, out_dir, stack, idx)
|
|
|
|
local mem = techage.get_mem(pos)
|
|
|
|
if not mem.pushing then
|
|
|
|
if not start_pos then
|
|
|
|
start_pos = pos
|
|
|
|
mem.pushing = true
|
|
|
|
local res = techage.push_items(pos, out_dir, stack, idx)
|
|
|
|
mem.pushing = nil
|
|
|
|
start_pos = nil
|
|
|
|
return res
|
|
|
|
else
|
|
|
|
local npos, in_dir, name = get_dest_node(pos, out_dir)
|
|
|
|
if vector.distance(start_pos, npos) < (Tube.max_tube_length or 100) then
|
|
|
|
mem.pushing = true
|
|
|
|
local res = techage.push_items(pos, out_dir, stack, idx)
|
|
|
|
mem.pushing = nil
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-09-02 22:12:25 +03:00
|
|
|
return stack
|
2021-09-14 21:07:35 +03:00
|
|
|
end
|
|
|
|
|
2019-03-09 21:26:15 +03:00
|
|
|
function techage.unpull_items(pos, out_dir, stack)
|
2019-03-02 22:51:16 +03:00
|
|
|
local npos, in_dir, name = get_dest_node(pos, out_dir)
|
2019-05-01 13:01:24 +03:00
|
|
|
if npos and NodeDef[name] and NodeDef[name].on_unpull_item then
|
2019-03-09 21:26:15 +03:00
|
|
|
return NodeDef[name].on_unpull_item(npos, in_dir, stack)
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2019-03-02 14:24:48 +03:00
|
|
|
-------------------------------------------------------------------
|
|
|
|
-- Server side helper functions
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
|
2019-04-27 18:42:27 +03:00
|
|
|
-- Get the given number of items from the inv. The position within the list
|
2020-04-13 14:20:38 +03:00
|
|
|
-- is incremented each time so that different item stacks will be considered.
|
2019-03-02 14:24:48 +03:00
|
|
|
-- Returns nil if ItemList is empty.
|
2020-04-13 14:20:38 +03:00
|
|
|
function techage.get_items(pos, inv, listname, num)
|
2019-03-02 14:24:48 +03:00
|
|
|
if inv:is_empty(listname) then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
local size = inv:get_size(listname)
|
2020-04-13 14:20:38 +03:00
|
|
|
local mem = techage.get_mem(pos)
|
2021-03-06 13:28:50 +03:00
|
|
|
mem.ta_startpos = mem.ta_startpos or 0
|
2020-04-13 14:20:38 +03:00
|
|
|
for idx = mem.ta_startpos, mem.ta_startpos+size do
|
2019-03-02 14:24:48 +03:00
|
|
|
idx = (idx % size) + 1
|
|
|
|
local items = inv:get_stack(listname, idx)
|
|
|
|
if items:get_count() > 0 then
|
|
|
|
local taken = items:take_item(num)
|
|
|
|
inv:set_stack(listname, idx, items)
|
2020-04-13 14:20:38 +03:00
|
|
|
mem.ta_startpos = idx
|
2019-03-02 14:24:48 +03:00
|
|
|
return taken
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2022-09-02 22:12:25 +03:00
|
|
|
-- Put the given stack into the given ItemList/inventory.
|
|
|
|
-- Function returns:
|
2022-09-03 20:22:43 +03:00
|
|
|
-- - true, if all items are moved
|
|
|
|
-- - false, if no item is moved
|
|
|
|
-- - leftover, if less than all items are moved
|
2022-09-02 22:12:25 +03:00
|
|
|
-- (true/false is the legacy mode and can't be removed)
|
2020-04-13 14:20:38 +03:00
|
|
|
function techage.put_items(inv, listname, item, idx)
|
2022-09-02 22:12:25 +03:00
|
|
|
local leftover
|
2020-04-13 14:20:38 +03:00
|
|
|
if idx and inv and idx <= inv:get_size(listname) then
|
|
|
|
local stack = inv:get_stack(listname, idx)
|
2022-09-02 22:12:25 +03:00
|
|
|
leftover = stack:add_item(item)
|
|
|
|
inv:set_stack(listname, idx, stack)
|
|
|
|
elseif inv then
|
|
|
|
leftover = inv:add_item(listname, item)
|
2020-04-13 14:20:38 +03:00
|
|
|
else
|
2022-09-02 22:12:25 +03:00
|
|
|
return false
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
2022-09-03 20:22:43 +03:00
|
|
|
|
2022-09-02 22:12:25 +03:00
|
|
|
local cnt = leftover:get_count()
|
|
|
|
if cnt == item:get_count() then
|
|
|
|
return false
|
|
|
|
elseif cnt == 0 then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return leftover
|
|
|
|
end
|
2022-09-03 20:22:43 +03:00
|
|
|
|
2019-03-02 14:24:48 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Return "full", "loaded", or "empty" depending
|
|
|
|
-- on the inventory load.
|
|
|
|
-- Full is returned, when no empty stack is available.
|
|
|
|
function techage.get_inv_state(inv, listname)
|
|
|
|
local state
|
|
|
|
if inv:is_empty(listname) then
|
|
|
|
state = "empty"
|
|
|
|
else
|
|
|
|
local list = inv:get_list(listname)
|
|
|
|
state = "full"
|
|
|
|
for _, item in ipairs(list) do
|
|
|
|
if item:is_empty() then
|
|
|
|
return "loaded"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return state
|
|
|
|
end
|
2021-11-15 21:26:59 +03:00
|
|
|
|
2022-06-09 13:59:17 +03:00
|
|
|
-- Beduino variant
|
|
|
|
function techage.get_inv_state_num(inv, listname)
|
|
|
|
local state
|
|
|
|
if inv:is_empty(listname) then
|
|
|
|
state = 0
|
|
|
|
else
|
|
|
|
local list = inv:get_list(listname)
|
|
|
|
state = 2
|
|
|
|
for _, item in ipairs(list) do
|
|
|
|
if item:is_empty() then
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return state
|
|
|
|
end
|
|
|
|
|
2021-11-15 21:26:59 +03:00
|
|
|
minetest.register_chatcommand("ta_send", {
|
|
|
|
description = minetest.formspec_escape(
|
2021-11-28 16:42:18 +03:00
|
|
|
"Send a techage command to the block with the number given: /ta_send <number> <command> [<data>]"),
|
2021-11-15 21:26:59 +03:00
|
|
|
func = function(name, param)
|
|
|
|
local num, cmnd, payload = param:match('^([0-9]+)%s+(%w+)%s*(.*)$')
|
|
|
|
|
|
|
|
if num and cmnd then
|
|
|
|
if techage.not_protected(num, name) then
|
|
|
|
local resp = techage.send_single("0", num, cmnd, payload)
|
|
|
|
if type(resp) == "string" then
|
|
|
|
return true, resp
|
|
|
|
else
|
|
|
|
return true, dump(resp)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return false, "Destination block is protected"
|
|
|
|
end
|
|
|
|
end
|
2021-11-28 16:42:18 +03:00
|
|
|
return false, "Syntax: /ta_send <number> <command> [<data>]"
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_chatcommand("expoints", {
|
|
|
|
privs = {
|
|
|
|
server = true
|
|
|
|
},
|
|
|
|
func = function(name, param)
|
|
|
|
local player_name, points = param:match("^(%S+)%s*(%d*)$")
|
|
|
|
if player_name then
|
|
|
|
local player = minetest.get_player_by_name(player_name)
|
|
|
|
if player then
|
|
|
|
if points and points ~= "" then
|
|
|
|
if techage.set_expoints(player, tonumber(points)) then
|
|
|
|
return true, "The player "..player_name.." now has "..points.." experience points."
|
|
|
|
end
|
|
|
|
else
|
|
|
|
points = techage.get_expoints(player)
|
|
|
|
return true, "The player "..player_name.." has "..points.." experience points."
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return false, "Unknown player "..player_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false, "Syntax error! Syntax: /expoints <name> [<points>]"
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_chatcommand("my_expoints", {
|
|
|
|
func = function(name, param)
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
if player then
|
|
|
|
local points = techage.get_expoints(player)
|
|
|
|
if points then
|
|
|
|
return true, "You have "..points.." experience points."
|
|
|
|
end
|
|
|
|
end
|
2021-11-15 21:26:59 +03:00
|
|
|
end
|
2022-01-04 21:40:27 +03:00
|
|
|
})
|