ethereal/flight.lua

198 lines
4.6 KiB
Lua
Raw Permalink Normal View History

-- translation and settings
local S = minetest.get_translator("ethereal")
2022-02-19 11:47:31 +03:00
local flight_secs = minetest.settings:get("ethereal.flightpotion_duration") or (5 * 60)
2021-02-10 15:08:46 +03:00
local timer_check = 5 -- seconds per check
-- get player timer
2021-02-10 15:08:46 +03:00
2022-07-08 15:13:04 +03:00
local function get_timer(user)
2021-02-10 15:08:46 +03:00
2022-02-19 11:47:31 +03:00
if not user then return end
local meta = user:get_meta() ; if not meta then return "" end
2021-02-13 00:24:57 +03:00
2023-08-08 19:42:04 +03:00
return meta:get_string("ethereal:fly_timer") or ""
2022-07-08 15:13:04 +03:00
end
2021-02-13 00:24:57 +03:00
-- do we have fly privs
2021-02-13 00:24:57 +03:00
2022-07-08 15:13:04 +03:00
local function has_fly(name)
return minetest.get_player_privs(name).fly
end
-- set player timer
2022-07-08 15:13:04 +03:00
local function set_timer(user, timer)
2023-08-08 19:42:04 +03:00
local meta = user:get_meta() ; if not meta then return end
2022-07-08 15:13:04 +03:00
2023-08-08 19:42:04 +03:00
meta:set_string("ethereal:fly_timer", timer)
2021-02-10 15:08:46 +03:00
end
-- give or revoke fly priv
2021-02-10 15:08:46 +03:00
2022-07-08 15:13:04 +03:00
local function set_flight(user, set)
2021-02-10 15:08:46 +03:00
local name = user and user:get_player_name() ; if not name then return end
2022-07-08 15:13:04 +03:00
local privs = minetest.get_player_privs(name)
2022-02-19 11:47:31 +03:00
2022-07-08 15:13:04 +03:00
privs.fly = set
2022-02-19 11:47:31 +03:00
2022-07-08 15:13:04 +03:00
minetest.set_player_privs(name, privs)
2022-02-19 11:47:31 +03:00
2022-07-08 15:13:04 +03:00
-- when flight removed set timer to temp position
if set ~= true then set_timer(user, "-99") end
2022-07-08 15:13:04 +03:00
end
2021-02-10 15:08:46 +03:00
2022-07-08 15:13:04 +03:00
-- after function
2022-07-08 15:13:04 +03:00
local function ethereal_set_flight(user)
local name = user and user:get_player_name() ; if not name then return end
2022-07-08 15:13:04 +03:00
local timer = tonumber(get_timer(user)) or 0
2021-02-13 00:24:57 +03:00
-- if timer ran out then remove 'fly' privelage
if timer <= 0 and timer ~= -99 then
2021-02-10 15:08:46 +03:00
set_flight(user, nil)
2021-02-13 00:24:57 +03:00
2021-02-10 15:08:46 +03:00
return
end
2021-02-13 00:24:57 +03:00
local privs = minetest.get_player_privs(name)
2021-02-10 15:08:46 +03:00
-- have we already applied 'fly' privelage?
if not privs.fly then set_flight(user, true) end
2021-02-10 15:08:46 +03:00
-- handle timer
timer = timer - timer_check
2021-02-13 00:24:57 +03:00
-- show expiration message and play sound
2022-07-08 15:13:04 +03:00
if timer <= 10 then
2021-02-10 15:08:46 +03:00
minetest.chat_send_player(name, minetest.get_color_escape_sequence("#ff5500")
2021-07-11 12:05:51 +03:00
.. S("Flight timer about to expire!"))
2021-02-10 15:08:46 +03:00
minetest.sound_play("default_dig_dig_immediate",
{to_player = name, gain = 1.0}, true)
end
set_timer(user, timer) -- set update timer
2021-02-10 15:08:46 +03:00
2022-07-08 15:13:04 +03:00
-- restart checks
2021-02-10 15:08:46 +03:00
minetest.after(timer_check, function()
ethereal_set_flight(user)
end)
end
-- on join / leave
2021-02-10 15:08:46 +03:00
minetest.register_on_joinplayer(function(player)
-- wait 2 seconds before doing flight checks on player
minetest.after(2.0, function(player)
2022-02-19 11:47:31 +03:00
-- get player name and timer
local name = player and player:get_player_name() ; if not name then return end
local timer = get_timer(player)
2022-02-19 11:47:31 +03:00
-- if timer is blank and player can already fly then default and return
if timer == "" and has_fly(name) then
2021-02-10 15:08:46 +03:00
set_timer(player, "-99")
2022-04-25 17:22:43 +03:00
return
end
2022-04-25 17:22:43 +03:00
timer = tonumber(timer) or 0
2021-02-10 15:08:46 +03:00
-- if timer is set to default then return
if timer == -99 then return end
2021-02-13 00:24:57 +03:00
-- if we got this far and player is flying then start countdown check
if has_fly(name) then
minetest.after(timer_check, function()
ethereal_set_flight(player)
end)
end
end, player)
2021-02-10 15:08:46 +03:00
end)
-- potion item
2021-02-10 15:08:46 +03:00
minetest.register_node("ethereal:flight_potion", {
description = S("Flight Potion"),
drawtype = "plantlike",
tiles = {"ethereal_flight_potion.png"},
inventory_image = "ethereal_flight_potion.png",
wield_image = "ethereal_flight_potion.png",
paramtype = "light",
sunlight_propagates = true,
walkable = false,
selection_box = {
type = "fixed", fixed = {-0.2, -0.37, -0.2, 0.2, 0.31, 0.2}
2021-02-10 15:08:46 +03:00
},
groups = {dig_immediate = 3},
sounds = default.node_sound_glass_defaults(),
on_use = function(itemstack, user, pointed_thing)
if user.is_fake_player then return end
-- get info
2021-02-10 15:08:46 +03:00
local name = user:get_player_name()
local privs = minetest.get_player_privs(name)
2022-07-08 15:13:04 +03:00
local timer = get_timer(user)
2021-02-10 15:08:46 +03:00
if privs.fly then
2021-02-13 00:24:57 +03:00
local msg = timer
if timer == "" or timer == "-99" then msg = S("unlimited") end
2021-02-13 00:24:57 +03:00
minetest.chat_send_player(name, minetest.get_color_escape_sequence("#ffff00")
.. S("Flight already granted, @1 seconds left!", msg))
2021-07-11 12:05:51 +03:00
2021-02-10 15:08:46 +03:00
return
end
set_timer(user, flight_secs) -- set flight timer
2021-02-10 15:08:46 +03:00
2022-07-08 15:13:04 +03:00
-- show time remaining
2021-02-10 15:08:46 +03:00
minetest.chat_send_player(name,
minetest.get_color_escape_sequence("#1eff00")
2021-07-11 12:05:51 +03:00
.. S("Flight granted, you have @1 seconds!", flight_secs))
2021-02-10 15:08:46 +03:00
ethereal_set_flight(user) -- start check
2021-02-10 15:08:46 +03:00
itemstack:take_item() -- take item
2021-02-10 15:08:46 +03:00
-- return empty bottle
local inv = user:get_inventory()
if inv:room_for_item("main", {name = "vessels:glass_bottle"}) then
user:get_inventory():add_item("main", "vessels:glass_bottle")
else
2021-07-11 12:05:51 +03:00
minetest.add_item(user:get_pos(), {name = "vessels:glass_bottle"})
2021-02-10 15:08:46 +03:00
end
return itemstack
end
})
-- recipe
2021-02-10 15:08:46 +03:00
minetest.register_craft({
output = "ethereal:flight_potion",
recipe = {
{"ethereal:etherium_dust", "ethereal:etherium_dust", "ethereal:etherium_dust"},
{"ethereal:etherium_dust", "ethereal:fire_dust", "ethereal:etherium_dust"},
2021-07-11 12:05:51 +03:00
{"ethereal:etherium_dust", "vessels:glass_bottle", "ethereal:etherium_dust"}
2021-02-10 15:08:46 +03:00
}
})