tube_test.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. --[[
  2. Tube Library 2
  3. ==============
  4. Copyright (C) 2017-2020 Joachim Stolberg
  5. LGPLv2.1+
  6. See LICENSE.txt for more information
  7. tube_test.lua
  8. THIS FILE IS ONLY FOR TESTING PURPOSES
  9. ]]--
  10. -- for lazy programmers
  11. local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
  12. local S2P = minetest.string_to_pos
  13. local M = minetest.get_meta
  14. -- Marker entities for debugging purposes
  15. local function debug_info(pos, text)
  16. local marker = minetest.add_entity(pos, "tubelib2:marker_cube")
  17. if marker ~= nil then
  18. if text == "add" then
  19. marker:set_nametag_attributes({color = "#FF0000", text = "add__________"})
  20. elseif text == "del" then
  21. marker:set_nametag_attributes({color = "#00FF00", text = "_____del_____"})
  22. elseif text == "noc" then
  23. marker:set_nametag_attributes({color = "#0000FF", text = "__________noc"})
  24. end
  25. minetest.after(6, marker.remove, marker)
  26. end
  27. end
  28. minetest.register_entity(":tubelib2:marker_cube", {
  29. initial_properties = {
  30. visual = "cube",
  31. textures = {
  32. "tubelib2_marker_cube.png",
  33. "tubelib2_marker_cube.png",
  34. "tubelib2_marker_cube.png",
  35. "tubelib2_marker_cube.png",
  36. "tubelib2_marker_cube.png",
  37. "tubelib2_marker_cube.png",
  38. },
  39. physical = false,
  40. visual_size = {x = 1.1, y = 1.1},
  41. collisionbox = {-0.55,-0.55,-0.55, 0.55,0.55,0.55},
  42. glow = 8,
  43. },
  44. on_punch = function(self)
  45. self.object:remove()
  46. end,
  47. })
  48. -- Test tubes
  49. local Tube = tubelib2.Tube:new({
  50. -- North, East, South, West, Down, Up
  51. -- dirs_to_check = {1,2,3,4}, -- horizontal only
  52. -- dirs_to_check = {5,6}, -- vertical only
  53. dirs_to_check = {1,2,3,4,5,6},
  54. max_tube_length = 10,
  55. show_infotext = true,
  56. primary_node_names = {"tubelib2:tubeS", "tubelib2:tubeA"},
  57. secondary_node_names = {"default:chest", "default:chest_open",
  58. "tubelib2:source", "tubelib2:junction", "tubelib2:teleporter"},
  59. after_place_tube = function(pos, param2, tube_type, num_tubes, tbl)
  60. minetest.swap_node(pos, {name = "tubelib2:tube"..tube_type, param2 = param2})
  61. end,
  62. --debug_info = debug_info,
  63. })
  64. Tube:set_valid_sides("tubelib2:source", {"F"})
  65. Tube:set_valid_sides("tubelib2:teleporter", {"F"})
  66. Tube:register_on_tube_update(function(node, pos, out_dir, peer_pos, peer_in_dir)
  67. local sdir = tubelib2.dir_to_string(out_dir)
  68. if not peer_pos then
  69. print(P2S(pos).." to the "..sdir..": Not connected")
  70. elseif Tube:is_secondary_node(peer_pos) then
  71. local node = minetest.get_node(peer_pos)
  72. print(P2S(pos).." to the "..sdir..": Connected to "..node.name.." at "..P2S(peer_pos).."/"..peer_in_dir)
  73. else
  74. print(P2S(pos).." to the "..sdir..": Connected to "..P2S(peer_pos).."/"..peer_in_dir)
  75. for i, pos, node in Tube:get_tube_line(pos, out_dir) do
  76. print("walk", P2S(pos), node.name)
  77. end
  78. end
  79. end)
  80. minetest.register_node("tubelib2:tubeS", {
  81. description = "Tubelib2 Test tube",
  82. tiles = { -- Top, base, right, left, front, back
  83. "tubelib2_tube.png",
  84. "tubelib2_tube.png",
  85. "tubelib2_tube.png",
  86. "tubelib2_tube.png",
  87. "tubelib2_hole.png",
  88. "tubelib2_hole.png",
  89. },
  90. after_place_node = function(pos, placer, itemstack, pointed_thing)
  91. --local t = minetest.get_us_time()
  92. if not Tube:after_place_tube(pos, placer, pointed_thing) then
  93. minetest.remove_node(pos)
  94. return true
  95. end
  96. --print("place time", minetest.get_us_time() - t)
  97. return false
  98. end,
  99. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  100. Tube:after_dig_tube(pos, oldnode, oldmetadata)
  101. end,
  102. paramtype2 = "facedir", -- important!
  103. drawtype = "nodebox",
  104. node_box = {
  105. type = "fixed",
  106. fixed = {
  107. {-2/8, -2/8, -4/8, 2/8, 2/8, 4/8},
  108. },
  109. },
  110. on_rotate = screwdriver.disallow, -- important!
  111. paramtype = "light",
  112. sunlight_propagates = true,
  113. is_ground_content = false,
  114. groups = {crumbly = 3, cracky = 3, snappy = 3},
  115. })
  116. minetest.register_node("tubelib2:tubeA", {
  117. description = "Tubelib2 Test tube",
  118. tiles = { -- Top, base, right, left, front, back
  119. "tubelib2_tube.png",
  120. "tubelib2_hole.png",
  121. "tubelib2_tube.png",
  122. "tubelib2_tube.png",
  123. "tubelib2_tube.png",
  124. "tubelib2_hole.png",
  125. },
  126. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  127. Tube:after_dig_tube(pos, oldnode, oldmetadata)
  128. end,
  129. paramtype2 = "facedir", -- important!
  130. drawtype = "nodebox",
  131. node_box = {
  132. type = "fixed",
  133. fixed = {
  134. {-2/8, -4/8, -2/8, 2/8, 2/8, 2/8},
  135. {-2/8, -2/8, -4/8, 2/8, 2/8, -2/8},
  136. },
  137. },
  138. on_rotate = screwdriver.disallow, -- important!
  139. paramtype = "light",
  140. sunlight_propagates = true,
  141. is_ground_content = false,
  142. groups = {crumbly = 3, cracky = 3, snappy = 3, not_in_creative_inventory=1},
  143. drop = "tubelib2:tubeS",
  144. })
  145. local function push_item(pos)
  146. local tube_dir = M(pos):get_int("tube_dir")
  147. local dest_pos, dest_dir = Tube:get_connected_node_pos(pos, tube_dir)
  148. local dest_node = minetest.get_node(dest_pos)
  149. local on_push_item = minetest.registered_nodes[dest_node.name].on_push_item
  150. --print("on_timer: dest_pos="..S(dest_pos).." dest_dir="..dest_dir)
  151. local inv = minetest.get_inventory({type="node", pos=dest_pos})
  152. local stack = ItemStack("default:dirt")
  153. if on_push_item then
  154. return on_push_item(dest_pos, dest_dir, stack)
  155. elseif inv then
  156. local leftover = inv:add_item("main", stack)
  157. if leftover:get_count() == 0 then
  158. return true
  159. end
  160. elseif dest_node.name == "air" then
  161. minetest.add_item(dest_pos, stack)
  162. return true
  163. end
  164. return false
  165. end
  166. minetest.register_node("tubelib2:source", {
  167. description = "Tubelib2 Item Source",
  168. tiles = {
  169. -- up, down, right, left, back, front
  170. 'tubelib2_source.png',
  171. 'tubelib2_source.png',
  172. 'tubelib2_source.png',
  173. 'tubelib2_source.png',
  174. 'tubelib2_source.png',
  175. 'tubelib2_conn.png',
  176. },
  177. after_place_node = function(pos, placer)
  178. local tube_dir = ((minetest.dir_to_facedir(placer:get_look_dir()) + 2) % 4) + 1
  179. M(pos):set_int("tube_dir", tube_dir)
  180. Tube:after_place_node(pos, {tube_dir})
  181. minetest.get_node_timer(pos):start(2)
  182. end,
  183. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  184. local tube_dir = tonumber(oldmetadata.fields.tube_dir or 0)
  185. Tube:after_dig_node(pos, {tube_dir})
  186. end,
  187. on_timer = function(pos, elapsed)
  188. if not push_item(pos) then
  189. print("push_item error")
  190. end
  191. return true
  192. end,
  193. paramtype2 = "facedir", -- important!
  194. on_rotate = screwdriver.disallow, -- important!
  195. paramtype = "light",
  196. sunlight_propagates = true,
  197. is_ground_content = false,
  198. groups = {crumbly = 3, cracky = 3, snappy = 3},
  199. })
  200. -- Can't be used for item transport (hyperloop test node)
  201. minetest.register_node("tubelib2:junction", {
  202. description = "Tubelib2 Junction",
  203. tiles = {
  204. 'tubelib2_conn.png',
  205. },
  206. after_place_node = function(pos, placer, itemstack, pointed_thing)
  207. local meta = minetest.get_meta(pos)
  208. meta:set_string("infotext", "Position "..P2S(pos))
  209. Tube:after_place_node(pos)
  210. end,
  211. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  212. Tube:after_dig_node(pos)
  213. end,
  214. paramtype2 = "facedir", -- important!
  215. on_rotate = screwdriver.disallow, -- important!
  216. paramtype = "light",
  217. sunlight_propagates = true,
  218. is_ground_content = false,
  219. groups = {crumbly = 3, cracky = 3, snappy = 3},
  220. })
  221. -------------------------------------------------------------------------------
  222. -- Teleporter
  223. -------------------------------------------------------------------------------
  224. local pairingList = {}
  225. local sFormspec = "size[7.5,3]"..
  226. "field[0.5,1;7,1;channel;Enter channel string;]" ..
  227. "button_exit[2,2;3,1;exit;Save]"
  228. local function store_connection(pos, peer_pos)
  229. local meta = M(pos)
  230. meta:set_string("peer_pos", P2S(peer_pos))
  231. meta:set_string("channel", "")
  232. meta:set_string("formspec", "")
  233. meta:set_string("infotext", "Connected to "..P2S(peer_pos))
  234. end
  235. local function prepare_pairing(pos)
  236. local meta = M(pos)
  237. meta:set_string("peer_pos", "")
  238. meta:set_string("channel", "")
  239. meta:set_string("formspec", sFormspec)
  240. meta:set_string("infotext", "Pairing is missing")
  241. end
  242. local function pairing(pos, channel)
  243. if pairingList[channel] and not vector.equals(pos, pairingList[channel]) then
  244. -- store peer position on both nodes
  245. local peer_pos = pairingList[channel]
  246. store_connection(pos, peer_pos)
  247. store_connection(peer_pos, pos)
  248. pairingList[channel] = nil
  249. return true
  250. else
  251. pairingList[channel] = pos
  252. prepare_pairing(pos)
  253. return false
  254. end
  255. end
  256. local function stop_pairing(pos, oldmetadata)
  257. -- unpair peer node
  258. if oldmetadata and oldmetadata.fields then
  259. if oldmetadata.fields.peer_pos then
  260. local peer_pos = S2P(oldmetadata.fields.peer_pos)
  261. prepare_pairing(peer_pos)
  262. elseif oldmetadata.fields.channel then
  263. pairingList[oldmetadata.fields.channel] = nil
  264. end
  265. end
  266. end
  267. minetest.register_node("tubelib2:teleporter", {
  268. description = "Tubelib2 Teleporter",
  269. tiles = {
  270. -- up, down, right, left, back, front
  271. 'tubelib2_tele.png',
  272. 'tubelib2_tele.png',
  273. 'tubelib2_tele.png',
  274. 'tubelib2_tele.png',
  275. 'tubelib2_tele.png',
  276. 'tubelib2_conn.png',
  277. },
  278. after_place_node = function(pos, placer)
  279. -- the tube_dir calculation depends on the player look-dir and the hole side of the node
  280. local tube_dir = ((minetest.dir_to_facedir(placer:get_look_dir()) + 2) % 4) + 1
  281. M(pos):set_int("tube_dir", tube_dir)
  282. Tube:after_place_node(pos, {tube_dir})
  283. prepare_pairing(pos)
  284. end,
  285. on_receive_fields = function(pos, formname, fields, player)
  286. if fields.channel ~= nil then
  287. pairing(pos, fields.channel)
  288. end
  289. end,
  290. on_push_item = function(pos, dir, item)
  291. local tube_dir = M(pos):get_int("tube_dir")
  292. if dir == tubelib2.Turn180Deg[tube_dir] then
  293. local s = M(pos):get_string("peer_pos")
  294. if s and s ~= "" then
  295. push_item(S2P(s))
  296. return true
  297. end
  298. end
  299. end,
  300. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  301. stop_pairing(pos, oldmetadata)
  302. local tube_dir = tonumber(oldmetadata.fields.tube_dir or 0)
  303. Tube:after_dig_node(pos, {tube_dir})
  304. end,
  305. paramtype2 = "facedir", -- important!
  306. on_rotate = screwdriver.disallow, -- important!
  307. paramtype = "light",
  308. sunlight_propagates = true,
  309. is_ground_content = false,
  310. groups = {crumbly = 3, cracky = 3, snappy = 3},
  311. })
  312. -------------------------------------------------------------------------------
  313. -- Tool
  314. -------------------------------------------------------------------------------
  315. local function read_param2(pos, player)
  316. local node = minetest.get_node(pos)
  317. local dir1, dir2, num_tubes = Tube:decode_param2(pos, node.param2)
  318. minetest.chat_send_player(player:get_player_name(), "[Tubelib2] pos="..P2S(pos)..", dir1="..dir1..", dir2="..dir2..", num_tubes="..num_tubes)
  319. end
  320. local function remove_tube(itemstack, placer, pointed_thing)
  321. if pointed_thing.type == "node" then
  322. local pos = pointed_thing.under
  323. if placer:get_player_control().sneak then
  324. read_param2(pos, placer)
  325. else
  326. Tube:tool_remove_tube(pos, "default_break_glass")
  327. end
  328. else
  329. local dir = (minetest.dir_to_facedir(placer:get_look_dir()) % 4) + 1
  330. minetest.chat_send_player(placer:get_player_name(),
  331. "[Tool Help] dir="..dir.."\n"..
  332. " left: remove node\n"..
  333. " right: repair tube line\n")
  334. end
  335. end
  336. --local function walk(itemstack, placer, pointed_thing)
  337. -- if pointed_thing.type == "node" then
  338. -- local pos = pointed_thing.under
  339. -- local dir = (minetest.dir_to_facedir(placer:get_look_dir()) % 4) + 1
  340. -- local t = minetest.get_us_time()
  341. -- local pos1, outdir1, pos2, outdir2, cnt = Tube:walk(pos, dir)
  342. -- t = minetest.get_us_time() - t
  343. -- print("time", t)
  344. -- if pos1 then
  345. -- local s = "[Tubelib2] pos1="..P2S(pos1)..", outdir1="..outdir1..", pos2="..P2S(pos2)..", outdir2="..outdir2..", cnt="..cnt
  346. -- minetest.chat_send_player(placer:get_player_name(), s)
  347. -- end
  348. -- else
  349. -- local dir = (minetest.dir_to_facedir(placer:get_look_dir()) % 4) + 1
  350. -- minetest.chat_send_player(placer:get_player_name(),
  351. -- "[Tool Help] dir="..dir.."\n"..
  352. -- " left: remove node\n"..
  353. -- " right: repair tube line\n")
  354. -- end
  355. --end
  356. local function repair_tube(itemstack, placer, pointed_thing)
  357. if pointed_thing.type == "node" then
  358. local pos = pointed_thing.under
  359. local _, _, fpos1, fpos2, _, _, cnt1, cnt2 = Tube:tool_repair_tube(pos)
  360. local length = cnt1 + cnt2
  361. local s = "Tube from " .. P2S(fpos1) .. " to " .. P2S(fpos2) .. ". Lenght = " .. length
  362. minetest.chat_send_player(placer:get_player_name(), s)
  363. if length > Tube.max_tube_length then
  364. local s = string.char(0x1b) .. "(c@#ff0000)" .. "Tube length error!"
  365. minetest.chat_send_player(placer:get_player_name(), s)
  366. end
  367. minetest.sound_play("carts_cart_new", {
  368. pos = pos,
  369. gain = 1,
  370. max_hear_distance = 5})
  371. else
  372. local dir = (minetest.dir_to_facedir(placer:get_look_dir()) % 4) + 1
  373. minetest.chat_send_player(placer:get_player_name(),
  374. "[Tool Help] dir="..dir.."\n"..
  375. " left: remove node\n"..
  376. " right: repair tube line\n")
  377. end
  378. end
  379. -- Tool for tube workers to crack a protected tube line
  380. minetest.register_node("tubelib2:tool", {
  381. description = "Tubelib2 Tool",
  382. inventory_image = "tubelib2_tool.png",
  383. wield_image = "tubelib2_tool.png",
  384. use_texture_alpha = true,
  385. groups = {cracky=1, book=1},
  386. on_use = remove_tube,
  387. on_place = repair_tube,
  388. node_placement_prediction = "",
  389. stack_max = 1,
  390. })