InsaneProtestor/mods/ip_elevator/elevator.lua
2022-11-24 01:06:37 -05:00

171 lines
5.4 KiB
Lua

-- This version of the elevator box allows to move up or down only.
-- The network name is determined automaticly from the position (x/z coordinates).
-- >utor: Sokomine
local S = minetest.get_translator("elevator")
function elevator.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 elevator.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 = elevator.elevator_network(pos)
-- will this be an elevator that will be added to an existing network?
if elevator.targets[owner_name][network_name]
-- does the network have any members at all?
and next(elevator.targets[owner_name][network_name], nil)
then
minetest.chat_send_player(owner_name,
S("This elevator will automaticly 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 = elevator.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 " ..
"elevator 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 differnt " ..
"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
minetest.register_node(":elevator:elevator", {
description = S("Elevator"),
drawtype = "mesh",
mesh = "elevator_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},
},
},
tiles = elevator.tiles_elevator,
inventory_image = elevator.elevator_inventory_image,
groups = {
elevator = 1
},
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())
-- request initial data
meta:set_string("formspec", ([[
size[12,10]
field[0.3,5.6;6,0.7;station_name;%s;]
button_exit[6.3,6.2;1.7,0.7;station_set;%s]
]]):format(S("Name of this station:"), S("Store")))
minetest.set_node(vector.add(pos, { x=0, y=1, z=0 }), { name="elevator:hidden_top" })
elevator.show_nearest_elevator(pos, placer:get_player_name(), minetest.dir_to_facedir(placer:get_look_dir()))
end,
on_receive_fields = elevator.on_receive_fields,
on_punch = function(pos, _, puncher)
elevator.update_formspec(pos, puncher:get_player_name())
end,
can_dig = function(pos, player)
return elevator.can_dig(pos, player, "elevator")
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
elevator.remove_box(pos, oldnode, oldmetadata, digger)
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 }))
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 ~= "elevator:hidden_top" then
minetest.chat_send_player(
placer:get_player_name(),
S("Not enough vertical space to place the elevator box!")
)
return
end
return minetest.item_place(itemstack, placer, pointed_thing)
end,
on_destruct = function(pos)
minetest.remove_node(vector.add(pos, { x=0, y=1, z=0 }))
end
})
minetest.register_craft({
output = "elevator:elevator",
recipe = elevator.elevator_recipe,
})