-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtube_test.lua
444 lines (391 loc) · 12.9 KB
/
tube_test.lua
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
--[[
Tube Library 2
==============
Copyright (C) 2017-2020 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
tube_test.lua
THIS FILE IS ONLY FOR TESTING PURPOSES
]]--
-- for lazy programmers
local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local S2P = minetest.string_to_pos
local M = minetest.get_meta
-- Marker entities for debugging purposes
local function debug_info(pos, text)
local marker = minetest.add_entity(pos, "tubelib2:marker_cube")
if marker ~= nil then
if text == "add" then
marker:set_nametag_attributes({color = "#FF0000", text = "add__________"})
elseif text == "del" then
marker:set_nametag_attributes({color = "#00FF00", text = "_____del_____"})
elseif text == "noc" then
marker:set_nametag_attributes({color = "#0000FF", text = "__________noc"})
end
minetest.after(6, marker.remove, marker)
end
end
minetest.register_entity(":tubelib2:marker_cube", {
initial_properties = {
visual = "cube",
textures = {
"tubelib2_marker_cube.png",
"tubelib2_marker_cube.png",
"tubelib2_marker_cube.png",
"tubelib2_marker_cube.png",
"tubelib2_marker_cube.png",
"tubelib2_marker_cube.png",
},
physical = false,
visual_size = {x = 1.1, y = 1.1},
collisionbox = {-0.55,-0.55,-0.55, 0.55,0.55,0.55},
glow = 8,
},
on_punch = function(self)
self.object:remove()
end,
})
-- Test tubes
local Tube = tubelib2.Tube:new({
-- North, East, South, West, Down, Up
-- dirs_to_check = {1,2,3,4}, -- horizontal only
-- dirs_to_check = {5,6}, -- vertical only
dirs_to_check = {1,2,3,4,5,6},
max_tube_length = 10,
show_infotext = true,
primary_node_names = {"tubelib2:tubeS", "tubelib2:tubeA"},
secondary_node_names = {"default:chest", "default:chest_open",
"tubelib2:source", "tubelib2:junction", "tubelib2:teleporter"},
after_place_tube = function(pos, param2, tube_type, num_tubes, tbl)
minetest.swap_node(pos, {name = "tubelib2:tube"..tube_type, param2 = param2})
end,
--debug_info = debug_info,
})
Tube:set_valid_sides("tubelib2:source", {"F"})
Tube:set_valid_sides("tubelib2:teleporter", {"F"})
Tube:register_on_tube_update(function(node, pos, out_dir, peer_pos, peer_in_dir)
local sdir = tubelib2.dir_to_string(out_dir)
if not peer_pos then
print(P2S(pos).." to the "..sdir..": Not connected")
elseif Tube:is_secondary_node(peer_pos) then
local node = minetest.get_node(peer_pos)
print(P2S(pos).." to the "..sdir..": Connected to "..node.name.." at "..P2S(peer_pos).."/"..peer_in_dir)
else
print(P2S(pos).." to the "..sdir..": Connected to "..P2S(peer_pos).."/"..peer_in_dir)
for i, pos, node in Tube:get_tube_line(pos, out_dir) do
print("walk", P2S(pos), node.name)
end
end
end)
minetest.register_node("tubelib2:tubeS", {
description = "Tubelib2 Test tube",
tiles = { -- Top, base, right, left, front, back
"tubelib2_tube.png",
"tubelib2_tube.png",
"tubelib2_tube.png",
"tubelib2_tube.png",
"tubelib2_hole.png",
"tubelib2_hole.png",
},
after_place_node = function(pos, placer, itemstack, pointed_thing)
--local t = minetest.get_us_time()
if not Tube:after_place_tube(pos, placer, pointed_thing) then
minetest.remove_node(pos)
return true
end
--print("place time", minetest.get_us_time() - t)
return false
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
Tube:after_dig_tube(pos, oldnode, oldmetadata)
end,
paramtype2 = "facedir", -- important!
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-2/8, -2/8, -4/8, 2/8, 2/8, 4/8},
},
},
on_rotate = screwdriver.disallow, -- important!
paramtype = "light",
use_texture_alpha = "clip",
sunlight_propagates = true,
is_ground_content = false,
groups = {crumbly = 3, cracky = 3, snappy = 3},
sounds = default.node_sound_glass_defaults(),
})
minetest.register_node("tubelib2:tubeA", {
description = "Tubelib2 Test tube",
tiles = { -- Top, base, right, left, front, back
"tubelib2_tube.png",
"tubelib2_hole.png",
"tubelib2_tube.png",
"tubelib2_tube.png",
"tubelib2_tube.png",
"tubelib2_hole.png",
},
after_dig_node = function(pos, oldnode, oldmetadata, digger)
Tube:after_dig_tube(pos, oldnode, oldmetadata)
end,
paramtype2 = "facedir", -- important!
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-2/8, -4/8, -2/8, 2/8, 2/8, 2/8},
{-2/8, -2/8, -4/8, 2/8, 2/8, -2/8},
},
},
on_rotate = screwdriver.disallow, -- important!
paramtype = "light",
use_texture_alpha = "clip",
sunlight_propagates = true,
is_ground_content = false,
groups = {crumbly = 3, cracky = 3, snappy = 3, not_in_creative_inventory=1},
sounds = default.node_sound_glass_defaults(),
drop = "tubelib2:tubeS",
})
local function push_item(pos)
local tube_dir = M(pos):get_int("tube_dir")
local dest_pos, dest_dir = Tube:get_connected_node_pos(pos, tube_dir)
local dest_node = minetest.get_node(dest_pos)
local on_push_item = minetest.registered_nodes[dest_node.name].on_push_item
--print("on_timer: dest_pos="..S(dest_pos).." dest_dir="..dest_dir)
local inv = minetest.get_inventory({type="node", pos=dest_pos})
local stack = ItemStack("default:dirt")
if on_push_item then
return on_push_item(dest_pos, dest_dir, stack)
elseif inv then
local leftover = inv:add_item("main", stack)
if leftover:get_count() == 0 then
return true
end
elseif dest_node.name == "air" then
minetest.add_item(dest_pos, stack)
return true
end
return false
end
minetest.register_node("tubelib2:source", {
description = "Tubelib2 Item Source",
tiles = {
-- up, down, right, left, back, front
'tubelib2_source.png',
'tubelib2_source.png',
'tubelib2_source.png',
'tubelib2_source.png',
'tubelib2_source.png',
'tubelib2_conn.png',
},
after_place_node = function(pos, placer)
local tube_dir = ((minetest.dir_to_facedir(placer:get_look_dir()) + 2) % 4) + 1
M(pos):set_int("tube_dir", tube_dir)
Tube:after_place_node(pos, {tube_dir})
minetest.get_node_timer(pos):start(2)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
local tube_dir = tonumber(oldmetadata.fields.tube_dir or 0)
Tube:after_dig_node(pos, {tube_dir})
end,
on_timer = function(pos, elapsed)
if not push_item(pos) then
print("push_item error")
end
return true
end,
paramtype2 = "facedir", -- important!
on_rotate = screwdriver.disallow, -- important!
is_ground_content = false,
groups = {crumbly = 3, cracky = 3, snappy = 3},
sounds = default.node_sound_glass_defaults(),
})
-- Can't be used for item transport (hyperloop test node)
minetest.register_node("tubelib2:junction", {
description = "Tubelib2 Junction",
tiles = {
'tubelib2_conn.png',
},
after_place_node = function(pos, placer, itemstack, pointed_thing)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Position "..P2S(pos))
Tube:after_place_node(pos)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
Tube:after_dig_node(pos)
end,
paramtype2 = "facedir", -- important!
on_rotate = screwdriver.disallow, -- important!
is_ground_content = false,
groups = {crumbly = 3, cracky = 3, snappy = 3},
sounds = default.node_sound_glass_defaults(),
})
-------------------------------------------------------------------------------
-- Teleporter
-------------------------------------------------------------------------------
local pairingList = {}
local sFormspec = "size[7.5,3]"..
"field[0.5,1;7,1;channel;Enter channel string;]" ..
"button_exit[2,2;3,1;exit;Save]"
local function store_connection(pos, peer_pos)
local meta = M(pos)
meta:set_string("peer_pos", P2S(peer_pos))
meta:set_string("channel", "")
meta:set_string("formspec", "")
meta:set_string("infotext", "Connected to "..P2S(peer_pos))
end
local function prepare_pairing(pos)
local meta = M(pos)
meta:set_string("peer_pos", "")
meta:set_string("channel", "")
meta:set_string("formspec", sFormspec)
meta:set_string("infotext", "Pairing is missing")
end
local function pairing(pos, channel)
if pairingList[channel] and not vector.equals(pos, pairingList[channel]) then
-- store peer position on both nodes
local peer_pos = pairingList[channel]
store_connection(pos, peer_pos)
store_connection(peer_pos, pos)
pairingList[channel] = nil
return true
else
pairingList[channel] = pos
prepare_pairing(pos)
return false
end
end
local function stop_pairing(pos, oldmetadata)
-- unpair peer node
if oldmetadata and oldmetadata.fields then
if oldmetadata.fields.peer_pos then
local peer_pos = S2P(oldmetadata.fields.peer_pos)
prepare_pairing(peer_pos)
elseif oldmetadata.fields.channel then
pairingList[oldmetadata.fields.channel] = nil
end
end
end
minetest.register_node("tubelib2:teleporter", {
description = "Tubelib2 Teleporter",
tiles = {
-- up, down, right, left, back, front
'tubelib2_tele.png',
'tubelib2_tele.png',
'tubelib2_tele.png',
'tubelib2_tele.png',
'tubelib2_tele.png',
'tubelib2_conn.png',
},
after_place_node = function(pos, placer)
-- the tube_dir calculation depends on the player look-dir and the hole side of the node
local tube_dir = ((minetest.dir_to_facedir(placer:get_look_dir()) + 2) % 4) + 1
M(pos):set_int("tube_dir", tube_dir)
Tube:after_place_node(pos, {tube_dir})
prepare_pairing(pos)
end,
on_receive_fields = function(pos, formname, fields, player)
if fields.channel ~= nil then
pairing(pos, fields.channel)
end
end,
on_push_item = function(pos, dir, item)
local tube_dir = M(pos):get_int("tube_dir")
if dir == tubelib2.Turn180Deg[tube_dir] then
local s = M(pos):get_string("peer_pos")
if s and s ~= "" then
push_item(S2P(s))
return true
end
end
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
stop_pairing(pos, oldmetadata)
local tube_dir = tonumber(oldmetadata.fields.tube_dir or 0)
Tube:after_dig_node(pos, {tube_dir})
end,
paramtype2 = "facedir", -- important!
on_rotate = screwdriver.disallow, -- important!
is_ground_content = false,
groups = {crumbly = 3, cracky = 3, snappy = 3},
sounds = default.node_sound_glass_defaults(),
})
-------------------------------------------------------------------------------
-- Tool
-------------------------------------------------------------------------------
local function read_param2(pos, player)
local node = minetest.get_node(pos)
local dir1, dir2, num_tubes = Tube:decode_param2(pos, node.param2)
minetest.chat_send_player(player:get_player_name(), "[Tubelib2] pos="..P2S(pos)..", dir1="..dir1..", dir2="..dir2..", num_tubes="..num_tubes)
end
local function remove_tube(itemstack, placer, pointed_thing)
if pointed_thing.type == "node" then
local pos = pointed_thing.under
if placer:get_player_control().sneak then
read_param2(pos, placer)
else
Tube:tool_remove_tube(pos, "default_break_glass")
end
else
local dir = (minetest.dir_to_facedir(placer:get_look_dir()) % 4) + 1
minetest.chat_send_player(placer:get_player_name(),
"[Tool Help] dir="..dir.."\n"..
" left: remove node\n"..
" right: repair tube line\n")
end
end
--local function walk(itemstack, placer, pointed_thing)
-- if pointed_thing.type == "node" then
-- local pos = pointed_thing.under
-- local dir = (minetest.dir_to_facedir(placer:get_look_dir()) % 4) + 1
-- local t = minetest.get_us_time()
-- local pos1, outdir1, pos2, outdir2, cnt = Tube:walk(pos, dir)
-- t = minetest.get_us_time() - t
-- print("time", t)
-- if pos1 then
-- local s = "[Tubelib2] pos1="..P2S(pos1)..", outdir1="..outdir1..", pos2="..P2S(pos2)..", outdir2="..outdir2..", cnt="..cnt
-- minetest.chat_send_player(placer:get_player_name(), s)
-- end
-- else
-- local dir = (minetest.dir_to_facedir(placer:get_look_dir()) % 4) + 1
-- minetest.chat_send_player(placer:get_player_name(),
-- "[Tool Help] dir="..dir.."\n"..
-- " left: remove node\n"..
-- " right: repair tube line\n")
-- end
--end
local function repair_tube(itemstack, placer, pointed_thing)
if pointed_thing.type == "node" then
local pos = pointed_thing.under
local _, _, fpos1, fpos2, _, _, cnt1, cnt2 = Tube:tool_repair_tube(pos)
local length = cnt1 + cnt2
local s = "Tube from " .. P2S(fpos1) .. " to " .. P2S(fpos2) .. ". Lenght = " .. length
minetest.chat_send_player(placer:get_player_name(), s)
if length > Tube.max_tube_length then
local s = string.char(0x1b) .. "(c@#ff0000)" .. "Tube length error!"
minetest.chat_send_player(placer:get_player_name(), s)
end
minetest.sound_play("carts_cart_new", {
pos = pos,
gain = 1,
max_hear_distance = 5})
else
local dir = (minetest.dir_to_facedir(placer:get_look_dir()) % 4) + 1
minetest.chat_send_player(placer:get_player_name(),
"[Tool Help] dir="..dir.."\n"..
" left: remove node\n"..
" right: repair tube line\n")
end
end
-- Tool for tube workers to crack a protected tube line
minetest.register_node("tubelib2:tool", {
description = "Tubelib2 Tool",
inventory_image = "tubelib2_tool.png",
wield_image = "tubelib2_tool.png",
use_texture_alpha = "clip",
groups = {cracky=1, book=1},
on_use = remove_tube,
on_place = repair_tube,
node_placement_prediction = "",
stack_max = 1,
})