ctf.join() bug fixes and ctf.register_on_new_game()

master
rubenwardy 2015-07-14 11:55:12 +01:00
parent 1aa85c35b8
commit 60f9060bd5
4 changed files with 93 additions and 44 deletions

View File

@ -40,6 +40,16 @@ function ctf.register_on_territory_query(func)
end
table.insert(ctf.registered_on_territory_query, func)
end
ctf.registered_on_new_game = {}
function ctf.register_on_new_game(func)
if ctf._mt_loaded then
error("You can't register callbacks at game time!")
end
table.insert(ctf.registered_on_new_game, func)
if ctf._new_game then
func()
end
end
function vector.distanceSQ(p1, p2)
local x = p1.x - p2.x
@ -159,6 +169,10 @@ function ctf.load()
ctf._loaddata = table
else
ctf.log("io", "ctf.txt is not present in the world folder")
ctf._new_game = true
for i = 1, #ctf.registered_on_new_game do
ctf.registered_on_new_game[i]()
end
end
end

View File

@ -99,7 +99,7 @@ end
-- Player joins team
-- Called by /join, /team join or auto allocate.
function ctf.join(name, team, force)
function ctf.join(name, team, force, by)
if not name or name == "" or not team or team == "" then
ctf.log("team", "Missing parameters to ctf.join")
return false
@ -109,17 +109,36 @@ function ctf.join(name, team, force)
if not force and not ctf.setting("players_can_change_team")
and not player.team then
ctf.action("teams", name .. " attempted to change to " .. team)
minetest.chat_send_player(name, "You are not allowed to switch teams, traitor!")
if by then
if by == name then
ctf.action("teams", name .. " attempted to change to " .. team)
minetest.chat_send_player(by, "You are not allowed to switch teams, traitor!")
else
ctf.action("teams", by .. " attempted to change " .. name .. " to " .. team)
minetest.chat_send_player(by, "Failed to add " .. name .. " to " .. team ..
" as players_can_change_team = false")
end
else
ctf.log("teams", "failed to add " .. name .. " to " .. team ..
" as players_can_change_team = false")
end
return false
end
local team_data = ctf.team(team)
if team_data then
minetest.log("action", name .. " attempted to join " .. team .. ", which doesn't exist")
minetest.chat_send_player(name, "No such team.")
ctf.list_teams(name)
if not team_data then
if by then
minetest.chat_send_player(by, "No such team.")
ctf.list_teams(by)
if by == name then
minetest.log("action", by .. " tried to move " .. name .. " to " .. team .. ", which doesn't exist")
else
minetest.log("action", name .. " attempted to join " .. team .. ", which doesn't exist")
end
else
ctf.log("teams", "failed to add " .. name .. " to " .. team ..
" as team does not exist")
end
return false
end
@ -287,6 +306,11 @@ minetest.register_on_newplayer(function(player)
if team then
ctf.log("autoalloc", name .. " was allocated to " .. team)
ctf.join(name, team)
local spawn = ctf.get_spawn(team)
if spawn then
player:moveto(spawn, false)
end
end
end)

View File

@ -75,7 +75,7 @@ minetest.register_chatcommand("team", {
"' to team '" .. tteam .. "'",false)
local privs = minetest.get_player_privs(name)
if privs and privs.ctf_admin == true then
if ctf.join(tplayer, tteam, true) then
if ctf.join(tplayer, tteam, true, name) then
minetest.chat_send_all(tplayer.." has joined team "..tteam)
else
minetest.char_send_player(name, "Failed to add player to team.")
@ -107,7 +107,7 @@ minetest.register_chatcommand("join", {
params = "team name",
description = "Add to team",
func = function(name, param)
ctf.join(name, param, false)
ctf.join(name, param, false, name)
end,
})

View File

@ -42,44 +42,55 @@ ctf_flag.register_on_capture(function(attname, flag)
minetest.chat_send_all("Team " .. winner .. " won!")
minetest.chat_send_all("Resetting the map, this may take a few moments...")
minetest.after(0.5, function()
ctf.reset()
minetest.delete_area(vector.new(-16*3, -16*3, -16*3), vector.new(16*3, 16*3, 16*3))
ctf.team({name="red", color="red", add_team=true})
ctf.team({name="blue", color="blue", add_team=true})
minetest.after(1, function()
local fred = {x=15, y=7, z=39, team="red"}
local fblue = {x=-9,y=9,z=-50, team="blue"}
ctf_flag.add("red", fred)
ctf_flag.add("blue", fblue)
safe_place(fred, {name="ctf_flag:flag"})
safe_place(fblue, {name="ctf_flag:flag"})
for i, player in pairs(minetest.get_connected_players()) do
local name = player:get_player_name()
local inv = player:get_inventory()
inv:set_list("main", {})
inv:set_list("craft", {})
local alloc_mode = tonumber(ctf.setting("allocate_mode"))
if alloc_mode == 0 then
return
end
local team = ctf.autoalloc(name, alloc_mode)
if team then
ctf.log("autoalloc", name .. " was allocated to " .. team)
ctf.join(name, team)
end
team = ctf.player(name).team
if ctf.team(team) then
local spawn = ctf.get_spawn(team)
player:moveto(spawn, false)
end
end
minetest.log("endgame", "reset done")
minetest.chat_send_all("All done! Next round!")
ctf.reset()
end)
end)
end
end)
ctf.register_on_new_game(function()
ctf.log("endgame", "Setting up new game!")
ctf.team({name="red", color="red", add_team=true})
ctf.team({name="blue", color="blue", add_team=true})
local fred = {x=15, y=7, z=39, team="red"}
local fblue = {x=-9, y=9, z=-50, team="blue"}
ctf_flag.add("red", fred)
ctf_flag.add("blue", fblue)
minetest.after(0, function()
safe_place(fred, {name="ctf_flag:flag"})
safe_place(fblue, {name="ctf_flag:flag"})
end)
for i, player in pairs(minetest.get_connected_players()) do
local name = player:get_player_name()
local inv = player:get_inventory()
inv:set_list("main", {})
inv:set_list("craft", {})
local alloc_mode = tonumber(ctf.setting("allocate_mode"))
if alloc_mode == 0 then
return
end
local team = ctf.autoalloc(name, alloc_mode)
if team then
ctf.log("autoalloc", name .. " was allocated to " .. team)
ctf.join(name, team)
end
team = ctf.player(name).team
if ctf.team(team) then
local spawn = ctf.get_spawn(team)
if spawn then
player:moveto(spawn, false)
end
end
end
minetest.log("endgame", "reset done")
minetest.chat_send_all("Next round!")
end)