mirror of
https://github.com/joe7575/tubelib2.git
synced 2024-11-19 22:13:49 +01:00
Proposal to improve teleporters
This commit is contained in:
parent
ff14dc2fbe
commit
49fc305abc
140
tube_test.lua
140
tube_test.lua
@ -15,8 +15,8 @@
|
||||
]]--
|
||||
|
||||
-- for lazy programmers
|
||||
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
|
||||
local P = minetest.string_to_pos
|
||||
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
|
||||
|
||||
|
||||
@ -56,9 +56,7 @@ minetest.register_entity(":tubelib2:marker_cube", {
|
||||
end,
|
||||
})
|
||||
|
||||
|
||||
-- Test tubes
|
||||
|
||||
local Tube = tubelib2.Tube:new({
|
||||
-- North, East, South, West, Down, Up
|
||||
-- dirs_to_check = {1,2,3,4}, -- horizontal only
|
||||
@ -72,20 +70,20 @@ local Tube = tubelib2.Tube:new({
|
||||
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,
|
||||
--debug_info = debug_info,
|
||||
})
|
||||
|
||||
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(S(pos).." to the "..sdir..": Not connected")
|
||||
print(P2S(pos).." to the "..sdir..": Not connected")
|
||||
elseif Tube:is_secondary_node(peer_pos) then
|
||||
local node = minetest.get_node(peer_pos)
|
||||
print(S(pos).." to the "..sdir..": Connected with "..node.name.." at "..S(peer_pos).."/"..peer_in_dir)
|
||||
print(P2S(pos).." to the "..sdir..": Connected with "..node.name.." at "..P2S(peer_pos).."/"..peer_in_dir)
|
||||
else
|
||||
print(S(pos).." to the "..sdir..": Connected with "..S(peer_pos).."/"..peer_in_dir)
|
||||
print(P2S(pos).." to the "..sdir..": Connected with "..P2S(peer_pos).."/"..peer_in_dir)
|
||||
for i, pos, node in Tube:get_tube_line(pos, out_dir) do
|
||||
print("walk", S(pos), node.name)
|
||||
print("walk", P2S(pos), node.name)
|
||||
end
|
||||
end
|
||||
end)
|
||||
@ -165,10 +163,28 @@ minetest.register_node("tubelib2:tubeA", {
|
||||
drop = "tubelib2:tubeS",
|
||||
})
|
||||
|
||||
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 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",
|
||||
@ -195,22 +211,8 @@ minetest.register_node("tubelib2:source", {
|
||||
end,
|
||||
|
||||
on_timer = function(pos, elapsed)
|
||||
local tube_dir = M(pos):get_int("tube_dir")
|
||||
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)
|
||||
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")
|
||||
if not push_item(pos) then
|
||||
print("push_item error")
|
||||
end
|
||||
return true
|
||||
end,
|
||||
@ -224,6 +226,7 @@ minetest.register_node("tubelib2:source", {
|
||||
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 = {
|
||||
@ -232,7 +235,7 @@ minetest.register_node("tubelib2:junction", {
|
||||
|
||||
after_place_node = function(pos, placer, itemstack, pointed_thing)
|
||||
local meta = minetest.get_meta(pos)
|
||||
meta:set_string("infotext", "Position "..S(pos))
|
||||
meta:set_string("infotext", "Position "..P2S(pos))
|
||||
Tube:after_place_node(pos)
|
||||
end,
|
||||
|
||||
@ -249,6 +252,58 @@ minetest.register_node("tubelib2:junction", {
|
||||
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 with "..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 = {
|
||||
@ -264,18 +319,30 @@ minetest.register_node("tubelib2:teleporter", {
|
||||
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)
|
||||
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
|
||||
Tube:pairing(pos, fields.channel)
|
||||
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)
|
||||
Tube:stop_pairing(pos, oldmetadata, sFormspec)
|
||||
stop_pairing(pos, oldmetadata)
|
||||
local tube_dir = tonumber(oldmetadata.fields.tube_dir or 0)
|
||||
Tube:after_dig_node(pos, {tube_dir})
|
||||
end,
|
||||
@ -289,10 +356,13 @@ minetest.register_node("tubelib2:teleporter", {
|
||||
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="..S(pos)..", dir1="..dir1..", dir2="..dir2..", num_tubes="..num_tubes)
|
||||
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)
|
||||
@ -321,7 +391,7 @@ local function walk(itemstack, placer, pointed_thing)
|
||||
t = minetest.get_us_time() - t
|
||||
print("time", t)
|
||||
if pos1 then
|
||||
local s = "[Tubelib2] pos1="..S(pos1)..", outdir1="..outdir1..", pos2="..S(pos2)..", outdir2="..outdir2..", cnt="..cnt
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user