capturetheflag/mods/ctf/ctf_match/buildtime.lua
Lars Müller 87f7212f43
Replace chat with HUD events, add BS 5 palette (#848)
* Replace chat with HUD events, add BS 5 palette

* Use HUD events for paxel too

* Move paxel_stop function up
2021-03-29 14:00:46 -07:00

122 lines
3.5 KiB
Lua

ctf.register_on_init(function()
ctf._set("match.build_time", 60)
end)
ctf_match.registered_on_build_time_start = {}
function ctf_match.register_on_build_time_start(func)
if ctf._mt_loaded then
error("You can't register callbacks at game time!")
end
table.insert(ctf_match.registered_on_build_time_start, func)
end
ctf_match.registered_on_build_time_end = {}
function ctf_match.register_on_build_time_end(func)
if ctf._mt_loaded then
error("You can't register callbacks at game time!")
end
table.insert(ctf_match.registered_on_build_time_end, func)
end
ctf_match.build_timer = 0
function ctf_match.is_in_build_time()
return ctf_match.build_timer > 0
end
ctf_match.register_on_new_match(function()
ctf_match.build_timer = ctf.setting("match.build_time")
end)
ctf.register_on_new_game(function()
ctf_match.build_timer = ctf.setting("match.build_time")
if ctf_match.build_timer > 0 then
for i = 1, #ctf_match.registered_on_build_time_start do
ctf_match.registered_on_build_time_start[i]()
end
end
end)
local function get_m_s_from_s(s)
local m = math.floor(s / 60)
s = math.floor(s - m * 60)
return m .. "m " .. s .. "s"
end
local last = 0
minetest.register_globalstep(function(delta)
if ctf_match.build_timer > 0 then
ctf_match.build_timer = ctf_match.build_timer - delta
if ctf_match.build_timer <= 0 then
for i = 1, #ctf_match.registered_on_build_time_end do
ctf_match.registered_on_build_time_end[i]()
end
end
local rbt = math.floor(ctf_match.build_timer)
if last ~= rbt then
local text = get_m_s_from_s(ctf_match.build_timer) .. " until match begins!"
for _, player in pairs(minetest.get_connected_players()) do
ctf.hud:change(player, "ctf_match:countdown", "text", text)
end
end
end
end)
local old_can_attack = ctf.can_attack
function ctf.can_attack(player, hitter, ...)
if ctf_match.is_in_build_time() then
if hitter:is_player() then
hud_event.new(hitter:get_player_name(), {
name = "ctf_match:buildtime_hit",
color = "warning",
value = "Match hasn't started yet!",
})
end
return false
end
return old_can_attack(player, hitter, ...)
end
ctf_match.register_on_build_time_start(function()
minetest.chat_send_all(minetest.colorize("#fcca05", ("Prepare your base! Match starts in " ..
ctf.setting("match.build_time") .. " seconds.")))
end)
ctf_match.register_on_build_time_end(function()
if minetest.global_exists("chatplus") then
chatplus.log("Build time over!")
end
minetest.chat_send_all(minetest.colorize("#dd5f04", "Build time over! Attack and defend!"))
for _, player in pairs(minetest.get_connected_players()) do
ctf.hud:remove(player, "ctf_match:countdown")
end
end)
ctf_flag.register_on_prepick_up(function(name, flag)
if ctf_match.is_in_build_time() then
minetest.chat_send_player(name, "Match hasn't started yet!")
ctf.move_to_spawn(name)
return false
elseif not ctf.get_spawn(ctf.player(name).team) then
return false
else
return true
end
end)
ctf.hud.register_part(function(player, name, tplayer)
if ctf_match.build_timer <= 0 then
ctf.hud:remove(player, "ctf_match:countdown")
elseif not ctf.hud:exists(player, "ctf_match:countdown") then
ctf.hud:add(player, "ctf_match:countdown", {
hud_elem_type = "text",
position = {x = 0.5, y = 0.5},
scale = {x = 0, y = 70},
text = get_m_s_from_s(ctf_match.build_timer) .. " until match begins!",
number = 0xFFFFFF,
offset = {x = -20, y = 20},
alignment = {x = 0.2, y = 0}
})
end
end)