capturetheflag/mods/ctf/ctf_match/buildtime.lua

119 lines
3.4 KiB
Lua
Raw Normal View History

2015-11-28 00:56:10 +00:00
ctf.register_on_init(function()
2015-11-28 01:12:02 +00:00
ctf._set("match.build_time", 60)
2015-11-28 00:56:10 +00:00
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
2015-11-28 01:53:12 +00:00
ctf_match.register_on_new_match(function()
ctf_match.build_timer = ctf.setting("match.build_time")
end)
2015-11-28 00:56:10 +00:00
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)
2015-12-30 12:53:10 +00:00
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
2015-11-28 00:56:10 +00:00
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
2015-12-30 12:53:10 +00:00
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
2015-11-28 00:56:10 +00:00
end
end)
local old_can_attack = ctf.can_attack
function ctf.can_attack(player, hitter, ...)
if ctf_match.is_in_build_time() then
2021-01-03 17:20:59 +00:00
if hitter:is_player() then
minetest.chat_send_player(hitter:get_player_name(), "Match hasn't started yet!")
end
return false
end
return old_can_attack(player, hitter, ...)
end
2015-11-28 00:56:10 +00:00
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.")))
2015-11-28 00:56:10 +00:00
end)
ctf_match.register_on_build_time_end(function()
2015-11-28 02:16:35 +00:00
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},
2015-12-30 12:53:10 +00:00
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
2015-11-28 00:56:10 +00:00
end)