an_televator/init.lua

146 lines
3.8 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
if minetest.get_modpath("default") then
2017-05-17 01:20:54 +02:00
itemset = {
steel = "default:steel_ingot",
gold = "default:gold_ingot",
copper = "default:copper_ingot",
glass = "default:glass",
}
2017-05-16 03:36:12 +02:00
end
---
--- Functions
---
-- [function] Get near televators
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
elseif name ~= "air" and name ~= "an_televator:televator" then
2017-05-17 01:20:54 +02:00
return
end
end
2017-05-16 03:36:12 +02:00
end
-- [function] Televator safe
2017-05-16 03:36:12 +02:00
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
2017-05-16 03:36:12 +02:00
2017-05-17 01:20:54 +02:00
if minetest.get_node(tpos).name ~= "air" then
return
end
end
2017-05-16 03:36:12 +02:00
2017-05-17 01:20:54 +02:00
return true
2017-05-16 03:36:12 +02:00
end
---
--- Registrations
---
-- [register] Televator node
minetest.register_node("an_televator:televator", {
description = "Televator\n"..
minetest.colorize("grey","Place up to 32 nodes apart.\n"
.."Jump goes up, sneak goes down."),
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
})
-- [register] Recipe
if itemset then
2017-05-17 01:20:54 +02:00
minetest.register_craft({
output = "an_televator:televator",
2017-05-17 01:20:54 +02:00
recipe = {
{itemset.steel, itemset.glass, itemset.steel},
{itemset.steel, itemset.gold, itemset.steel},
{itemset.steel, itemset.copper, itemset.steel,}
},
})
2017-05-16 03:36:12 +02:00
end
-- [register] Globalstep
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 - 1, 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
player:set_pos(epos) -- Update player position
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
delay[name] = 0 -- Restart delay
end
end
end
2017-05-16 03:36:12 +02:00
end)