-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathlib.lua
More file actions
130 lines (112 loc) · 3.41 KB
/
lib.lua
File metadata and controls
130 lines (112 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
AGPL v3
See LICENSE.txt for more information
Logic library
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
local S2P = minetest.string_to_pos
local P2S = minetest.pos_to_string
techage.logic = {}
local Face2Dir = {[0]=
{x=0, y=0, z=1},
{x=1, y=0, z=0},
{x=0, y=0, z=-1},
{x=-1, y=0, z=0},
{x=0, y=-1, z=0},
{x=0, y=1, z=0}
}
-- Determine the destination position based on the position,
-- the node param2, and a route table like : {0,0,3}
-- 0 = forward, 1 = right, 2 = backward, 3 = left
function techage.logic.dest_pos(pos, param2, route)
local p2 = param2
for _,dir in ipairs(route) do
p2 = (param2 + dir) % 4
pos = vector.add(pos, Face2Dir[p2])
end
return pos, p2
end
function techage.logic.swap_node(pos, name)
local node = techage.get_node_lvm(pos)
if node.name == name then
return false
end
node.name = name
minetest.swap_node(pos, node)
return true
end
function techage.logic.after_place_node(pos, placer, name, descr)
local meta = M(pos)
local own_num = techage.add_node(pos, name)
meta:set_string("node_number", own_num)
meta:set_string("owner", placer:get_player_name())
meta:set_string("infotext", descr.." -")
end
techage.recursion_guard = techage.recursion_guard or {}
function techage.logic.guarded_action(pos, cmnd, ...)
if not cmnd then
return
end
local arg = {...}
local own_num = M(pos):get_string("node_number")
if techage.recursion_guard[own_num] then
minetest.log("warning", "[techage] Button recursion detected at node_number=".. own_num .. " pos=" .. P2S(pos))
return -- recursion detected, do not execute
end
techage.recursion_guard[own_num] = true
local result = cmnd(unpack(arg))
techage.recursion_guard[own_num] = nil
return result
end
function techage.logic.send_on(pos, meta, time)
local own_num = meta:get_string("node_number") or ""
local numbers = meta:get_string("numbers") or ""
if time and time > 0 then
minetest.get_node_timer(pos):start(time)
end
techage.send_multi(own_num, numbers, "on")
return own_num == numbers
end
function techage.logic.send_cmnd(pos, ident, default, time)
local meta = M(pos)
local s = meta:contains(ident) and meta:get_string(ident) or default
local command, payload = unpack(string.split(s, " ", false, 1))
local own_num = meta:get_string("node_number") or ""
local numbers = meta:get_string("numbers") or ""
if time and time > 0 then
minetest.get_node_timer(pos):start(time)
end
if command and command ~= "" then
techage.send_multi(own_num, numbers, command, payload)
end
end
function techage.logic.send_off(pos, meta)
local own_num = meta:get_string("node_number") or ""
local numbers = meta:get_string("numbers") or ""
techage.send_multi(own_num, numbers, "off")
end
function techage.logic.infotext(meta, descr, text)
local own_num = meta:get_string("node_number") or ""
local numbers = meta:get_string("numbers") or ""
if numbers ~= "" then
meta:set_string("infotext", descr.." "..own_num..": "..S("connected with").." "..numbers)
elseif text then
meta:set_string("infotext", descr.." "..own_num..": "..text)
else
meta:set_string("infotext", descr.." "..own_num)
end
end
function techage.logic.set_numbers(pos, numbers, player_name, descr)
if techage.check_numbers(numbers, player_name) then
local meta = M(pos)
meta:set_string("numbers", numbers)
techage.logic.infotext(meta, descr)
return true
end
return false
end