an_televator/init.lua

109 lines
3.1 KiB
Lua
Raw Normal View History

--[[
MIT License
Copyright (c) 2017 Elijah Duffy
Copyright (c) 2019 Noctis Labs
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--]]
-- an_televator/init.lua
2017-05-16 03:36:12 +02:00
local delay = {}
local itemset
---
--- Functions
---
local function get_near_televators(pos, which)
for i = 1, 32 do
2017-05-17 01:20:54 +02:00
local cpos = vector.new(pos)
if which == "above" then
cpos.y = cpos.y + i
elseif which == "below" then
cpos.y = cpos.y - i
end
local name = minetest.get_node(cpos).name
if (which == "above" and name == "an_televator:televator")
or (which == "below" and i ~= 1 and name == "an_televator:televator") then
2017-05-17 01:20:54 +02:00
cpos.y = cpos.y + 1
return cpos
end
end
2017-05-16 03:36:12 +02:00
end
local function is_safe(pos)
2017-05-17 01:20:54 +02:00
for i = 0, 1 do
local tpos = vector.new(pos)
tpos.y = tpos.y + i
if minetest.get_node(tpos).name ~= "air" then
return
end
end
return true
2017-05-16 03:36:12 +02:00
end
---
--- Registrations
---
minetest.register_node("an_televator:televator", {
description = "Televator",
tiles = {"televator_televator.png"},
2017-05-17 01:20:54 +02:00
groups = {cracky = 2, disable_jump = 1},
after_place_node = function(pos)
minetest.get_meta(pos):set_string("infotext", "Televator")
2017-05-17 01:20:54 +02:00
end,
2017-05-16 03:36:12 +02:00
})
minetest.register_globalstep(function(dtime)
2017-05-17 01:20:54 +02:00
for _, player in pairs(minetest.get_connected_players()) do
local pos = player:get_pos()
local name = player:get_player_name()
if not delay[name] then
delay[name] = 0.5
else
delay[name] = delay[name] + dtime
end
if not delay[name] or delay[name] > 0.5 then
if minetest.get_node({x = pos.x, y = pos.y - 0.5, z = pos.z}).name == "an_televator:televator" then
2017-05-17 01:20:54 +02:00
local where
local controls = player:get_player_control()
if controls.jump then
where = "above"
elseif controls.sneak then
where = "below"
else return end
local epos = get_near_televators(pos, where)
2017-05-17 01:20:54 +02:00
if epos and is_safe(epos) then
2019-10-01 22:05:19 +02:00
player:set_pos(epos)
2017-05-17 01:20:54 +02:00
minetest.sound_play("televator_whoosh", {
gain = 0.75,
pos = epos,
max_hear_distance = 5,
})
else
minetest.sound_play("televator_error", {
gain = 0.75,
pos = epos,
max_hear_distance = 5,
})
2017-05-17 01:20:54 +02:00
end
2019-10-01 22:05:19 +02:00
delay[name] = 0
2017-05-17 01:20:54 +02:00
end
end
end
2017-05-16 03:36:12 +02:00
end)