travelnet-cd2025/elevator.lua

170 lines
5.4 KiB
Lua
Raw Normal View History

-- This version of the travelnet box allows to move up or down only.
-- The network name is determined automaticly from the position (x/z coordinates).
-- Author: Sokomine
2020-11-19 19:57:19 +01:00
local S = minetest.get_translator("travelnet")
function travelnet.show_nearest_elevator(pos, owner_name, param2)
if not pos or not pos.x or not pos.z or not owner_name then
return
end
if not travelnet.targets[owner_name] then
minetest.chat_send_player(owner_name,
S("Congratulations! This is your first elevator. " ..
"You can build an elevator network by placing further elevators somewhere above " ..
"or below this one. Just make sure that the x and z coordinate are the same."))
return
end
local network_name = travelnet.elevator_network(pos)
-- will this be an elevator that will be added to an existing network?
if travelnet.targets[owner_name][network_name]
-- does the network have any members at all?
and next(travelnet.targets[owner_name][network_name], nil)
then
minetest.chat_send_player(owner_name,
S("This elevator will automatically connect to the " ..
"other elevators you have placed at different heights. Just enter a station name " ..
"and click on \"store\" to set it up. Or just punch it to set the height as station " ..
"name."))
return
end
local nearest_name, nearest_dist = travelnet.find_nearest_elevator_network(pos, owner_name)
if not nearest_name then
minetest.chat_send_player(owner_name,
S("This is your first elevator. It differs from " ..
"travelnet networks by only allowing movement in vertical direction (up or down). " ..
"All further elevators which you will place at the same x,z coordinates at different " ..
"heights will be able to connect to this elevator."))
return
end
local direction_strings = {
S("m to the right"),
S("m behind this elevator and"),
S("m to the left"),
S("m in front of this elevator and")
}
local direction_indexes = { x=param2+1, z=((param2+1) % 4)+1 }
-- Should X or Z be displayed first?
local direction_order = ({ [0]={"z","x"}, [1]={"x","z"} })[param2 % 2]
local text = S("Your nearest elevator network is located") .. " "
for index, direction in ipairs(direction_order) do
local nearest_dist_direction = nearest_dist[direction]
local direction_index = direction_indexes[direction]
if nearest_dist_direction < 0 then
direction_index = ((direction_indexes[direction]+1) % 4)+1
end
text = text .. tostring(math.abs(nearest_dist_direction)) .. " " .. direction_strings[direction_index]
if index == 1 then text = text .. " " end
end
minetest.chat_send_player(owner_name, text .. S(", located at x") ..
("=%f, z=%f. "):format(pos.x + nearest_dist.x, pos.z + nearest_dist.z) ..
S("This elevator here will start a new shaft/network."))
end
local function on_interact(pos, _, player)
local meta = minetest.get_meta(pos)
local legacy_formspec = meta:get_string("formspec")
if not travelnet.is_falsey_string(legacy_formspec) then
meta:set_string("formspec", "")
end
local player_name = player:get_player_name()
travelnet.show_current_formspec(pos, meta, player_name)
end
minetest.register_node("travelnet:elevator", {
description = S("Elevator"),
drawtype = "mesh",
mesh = "travelnet_elevator.obj",
sunlight_propagates = true,
paramtype = "light",
paramtype2 = "facedir",
wield_scale = { x=0.6, y=0.6, z=0.6 },
selection_box = {
type = "fixed",
fixed = { -0.5, -0.5, -0.5, 0.5, 1.5, 0.5 }
},
collision_box = {
type = "fixed",
fixed = {
{ 0.48, -0.5,-0.5, 0.5, 0.5, 0.5},
{-0.5 , -0.5, 0.48, 0.48, 0.5, 0.5},
{-0.5, -0.5,-0.5 ,-0.48, 0.5, 0.5},
--groundplate to stand on
{ -0.5,-0.5,-0.5,0.5,-0.48, 0.5},
},
},
2019-02-26 20:36:14 +01:00
tiles = travelnet.tiles_elevator,
inventory_image = travelnet.elevator_inventory_image,
groups = {
elevator = 1
},
2021-01-15 08:26:42 +01:00
light_source = 10,
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", S("Elevator (unconfigured)"))
meta:set_string("station_name", "")
meta:set_string("station_network","")
meta:set_string("owner", placer:get_player_name())
minetest.set_node(vector.add(pos, { x=0, y=1, z=0 }), { name="travelnet:hidden_top" })
travelnet.show_nearest_elevator(pos, placer:get_player_name(), minetest.dir_to_facedir(placer:get_look_dir()))
2021-01-15 08:26:42 +01:00
end,
on_rightclick = on_interact,
on_punch = on_interact,
2021-01-15 08:26:42 +01:00
can_dig = function(pos, player)
return travelnet.can_dig(pos, player, "elevator")
2021-01-15 08:26:42 +01:00
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
travelnet.remove_box(pos, oldnode, oldmetadata, digger)
2021-01-15 08:26:42 +01:00
end,
-- TNT and overenthusiastic DMs do not destroy elevators either
on_blast = function()
end,
-- taken from VanessaEs homedecor fridge
on_place = function(itemstack, placer, pointed_thing)
local node = minetest.get_node(vector.add(pointed_thing.above, { x=0, y=1, z=0 }))
2021-01-15 08:26:42 +01:00
local def = minetest.registered_nodes[node.name]
-- leftover top nodes can be removed by placing a new elevator underneath
if (not def or not def.buildable_to) and node.name ~= "travelnet:hidden_top" then
minetest.chat_send_player(
placer:get_player_name(),
S("Not enough vertical space to place the travelnet box!")
2021-01-15 08:26:42 +01:00
)
return
2021-01-15 08:26:42 +01:00
end
return minetest.item_place(itemstack, placer, pointed_thing)
2021-01-15 08:26:42 +01:00
end,
on_destruct = function(pos)
minetest.remove_node(vector.add(pos, { x=0, y=1, z=0 }))
2021-01-15 08:26:42 +01:00
end
})
2021-01-15 08:26:42 +01:00
minetest.register_craft({
output = "travelnet:elevator",
recipe = travelnet.elevator_recipe,
})