tubelib2/tube_test.lua

311 lines
8.9 KiB
Lua
Raw Normal View History

2018-10-05 21:55:40 +02:00
--[[
Tube Library 2
==============
Copyright (C) 2017-2018 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
tube_test.lua
THIS FILE IS ONLY FOR TESTING PURPOSES
]]--
-- for lazy programmers
2018-11-12 21:05:43 +01:00
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
2018-10-06 21:46:38 +02:00
local P = minetest.string_to_pos
2018-10-05 21:55:40 +02:00
local M = minetest.get_meta
-- Test tubes
local Tube = tubelib2.Tube:new({
2018-10-16 20:21:41 +02:00
-- 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},
2018-10-05 21:55:40 +02:00
max_tube_length = 1000,
2018-10-16 20:21:41 +02:00
show_infotext = false,
2018-10-05 21:55:40 +02:00
primary_node_names = {"tubelib2:tubeS", "tubelib2:tubeA"},
2018-10-14 21:28:31 +02:00
secondary_node_names = {"default:chest", "default:chest_open",
"tubelib2:source", "tubelib2:junction", "tubelib2:teleporter"},
2018-10-28 18:45:07 +01:00
after_place_tube = function(pos, param2, tube_type, num_tubes, tbl)
minetest.swap_node(pos, {name = "tubelib2:tube"..tube_type, param2 = param2})
2018-10-15 20:05:41 +02:00
end,
2018-10-05 21:55:40 +02:00
})
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()
2018-10-15 20:05:41 +02:00
if not Tube:after_place_tube(pos, placer, pointed_thing) then
2018-10-05 21:55:40 +02:00
minetest.remove_node(pos)
return true
end
print("place time", minetest.get_us_time() - t)
2018-10-15 20:05:41 +02:00
return false
2018-10-05 21:55:40 +02:00
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
2018-10-15 20:05:41 +02:00
Tube:after_dig_tube(pos, oldnode, oldmetadata)
2018-10-05 21:55:40 +02:00
end,
paramtype2 = "facedir", -- important!
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-2/8, -2/8, -4/8, 2/8, 2/8, 4/8},
},
},
2018-10-06 21:46:38 +02:00
on_rotate = screwdriver.disallow, -- important!
2018-10-05 21:55:40 +02:00
paramtype = "light",
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)
2018-10-15 20:05:41 +02:00
Tube:after_dig_tube(pos, oldnode, oldmetadata)
2018-10-05 21:55:40 +02:00
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!
2018-10-05 21:55:40 +02:00
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
groups = {crumbly = 3, cracky = 3, snappy = 3, not_in_creative_inventory=1},
2018-10-05 21:55:40 +02:00
sounds = default.node_sound_glass_defaults(),
drop = "tubelib2:tubeS",
})
2018-10-06 21:46:38 +02:00
local sFormspec = "size[7.5,3]"..
"field[0.5,1;7,1;channel;Enter channel string;]" ..
"button_exit[2,2;3,1;exit;Save]"
2018-10-14 21:28:31 +02:00
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})
2018-10-14 21:28:31 +02:00
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})
2018-10-14 21:28:31 +02:00
end,
on_timer = function(pos, elapsed)
local tube_dir = M(pos):get_int("tube_dir")
2018-10-20 19:19:44 +02:00
local dest_pos, dest_dir = Tube:get_connected_node_pos(pos, tube_dir)
print("on_timer: dest_pos="..S(dest_pos).." dest_dir="..dest_dir)
2018-10-14 21:28:31 +02:00
local inv = minetest.get_inventory({type="node", pos=dest_pos})
local stack = ItemStack("default:dirt")
if inv then
local leftover = inv:add_item("main", stack)
if leftover:get_count() == 0 then
return true
end
end
local node = minetest.get_node(dest_pos)
if node.name == "air" then
minetest.add_item(dest_pos, stack)
else
print("add_item error")
end
return true
end,
paramtype2 = "facedir", -- important!
on_rotate = screwdriver.disallow, -- important!
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
groups = {crumbly = 3, cracky = 3, snappy = 3},
sounds = default.node_sound_glass_defaults(),
})
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 "..S(pos))
Tube:after_place_node(pos)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
Tube:after_dig_node(pos)
end,
tubelib2_on_update = function(pos, out_dir, peer_pos, peer_in_dir)
2018-11-12 21:05:43 +01:00
local sdir = tubelib2.dir_to_string(out_dir)
if not peer_pos then
print(S(pos).." to the "..sdir..": Not connected")
elseif Tube:secondary_node(peer_pos) then
local node = minetest.get_node(peer_pos)
2018-11-12 21:05:43 +01:00
print(S(pos).." to the "..sdir..": Connected with "..node.name)
else
2018-11-12 21:05:43 +01:00
print(S(pos).." to the "..sdir..": Connected with "..S(peer_pos))
end
end,
paramtype2 = "facedir", -- important!
on_rotate = screwdriver.disallow, -- important!
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
groups = {crumbly = 3, cracky = 3, snappy = 3},
sounds = default.node_sound_glass_defaults(),
})
2018-10-06 21:46:38 +02:00
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
Tube:prepare_pairing(pos, tube_dir, sFormspec)
Tube:after_place_node(pos, {tube_dir})
2018-10-06 21:46:38 +02:00
end,
on_receive_fields = function(pos, formname, fields, player)
if fields.channel ~= nil then
Tube:pairing(pos, fields.channel)
end
end,
2018-10-14 21:28:31 +02:00
after_dig_node = function(pos, oldnode, oldmetadata, digger)
Tube:stop_pairing(pos, oldmetadata, sFormspec)
local tube_dir = tonumber(oldmetadata.fields.tube_dir or 0)
Tube:after_dig_node(pos, {tube_dir})
2018-10-06 21:46:38 +02:00
end,
paramtype2 = "facedir", -- important!
on_rotate = screwdriver.disallow, -- important!
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
groups = {crumbly = 3, cracky = 3, snappy = 3},
sounds = default.node_sound_glass_defaults(),
})
2018-10-05 21:55:40 +02:00
local function read_param2(pos, player)
local node = minetest.get_node(pos)
local num = math.floor(node.param2/32)
local axis = math.floor(node.param2/4) % 8
local rot = node.param2 % 4
minetest.chat_send_player(player:get_player_name(), "[Tubelib2] param2 = "..node.param2.."/"..num.."/"..axis.."/"..rot)
end
local function chat_message(dir, cnt, peer_pos, peer_dir)
local sdir = tubelib2.dir_to_string(dir)
if Tube:secondary_node(peer_pos, peer_dir) then
local npos, node = Tube:get_node(peer_pos, peer_dir)
return "[Tubelib2] To the "..sdir..": "..cnt.." tube nodes to "..node.name.." at "..S(npos)
else
return "[Tubelib2] To the "..sdir..": "..cnt.." tube nodes to "..S(peer_pos)
end
end
2018-10-05 21:55:40 +02:00
local function repair_tubes(itemstack, placer, pointed_thing)
if pointed_thing.type == "node" then
local pos = pointed_thing.under
local t = minetest.get_us_time()
local dir1, dir2, fpos1, fpos2, fdir1, fdir2, cnt1, cnt2 =
Tube:tool_repair_tube(pos, placer, pointed_thing)
t = minetest.get_us_time() - t
print("time", t)
if fpos1 and fpos2 then
minetest.chat_send_player(placer:get_player_name(), chat_message(dir1, cnt1, fpos1, fdir1))
minetest.chat_send_player(placer:get_player_name(), chat_message(dir2, cnt2, fpos2, fdir2))
2018-10-06 13:29:47 +02:00
end
2018-10-05 21:55:40 +02:00
end
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
2018-10-14 21:28:31 +02:00
Tube:tool_remove_tube(pos, "default_break_glass")
2018-10-05 21:55:40 +02:00
end
else
minetest.chat_send_player(placer:get_player_name(),
"[Tool Help]\n"..
" left: remove node\n"..
" right: repair tube line\n")
2018-10-05 21:55:40 +02:00
end
end
2018-10-16 20:21:41 +02:00
-- Tool for tube workers to crack a protected tube line
2018-10-05 21:55:40 +02:00
minetest.register_node("tubelib2:tool", {
description = "Tubelib2 Tool",
inventory_image = "tubelib2_tool.png",
wield_image = "tubelib2_tool.png",
use_texture_alpha = true,
groups = {cracky=1, book=1},
on_use = remove_tube,
on_place = repair_tubes,
node_placement_prediction = "",
2018-10-05 22:52:51 +02:00
stack_max = 1,
2018-10-05 21:55:40 +02:00
})