Finish decoupling ctf_flags

This commit is contained in:
rubenwardy 2015-07-13 13:19:19 +01:00
parent 9db50cfd3d
commit a7d5d29220
3 changed files with 58 additions and 66 deletions

View File

@ -29,6 +29,29 @@ function ctf.team(name) -- get or add a team
end end
end end
function ctf.list_teams(name)
minetest.chat_send_player(name, "Teams:")
for tname, team in pairs(ctf.teams) do
if team and team.players then
local details = ""
local numPlayers = ctf.count_players_in_team(tname)
details = numPlayers .. " members"
if team.flags then
local numFlags = 0
for flagid, flag in pairs(team.flags) do
numFlags = numFlags + 1
end
details = details .. ", " .. numFlags .. " flags"
end
minetest.chat_send_player(name, ">> " .. tname ..
" (" .. details .. ")")
end
end
end
-- Count number of players in a team -- Count number of players in a team
function ctf.count_players_in_team(team) function ctf.count_players_in_team(team)
local count = 0 local count = 0
@ -78,6 +101,7 @@ function ctf.join(name, team, force)
if not ctf.team(team) then if not ctf.team(team) then
minetest.log("action", name .. " attempted to join " .. team .. ", which doesn't exist") minetest.log("action", name .. " attempted to join " .. team .. ", which doesn't exist")
minetest.chat_send_player(name, "No such team.") minetest.chat_send_player(name, "No such team.")
ctf.list_teams(name)
return false return false
end end
@ -272,43 +296,26 @@ end)
-- updates the spawn position for a team -- updates the spawn position for a team
function ctf.get_spawn(team) function ctf.get_spawn(team)
ctf_flag.asset_flags(team) if ctf.team(team) and ctf.team(team).spawn then
if not ctf.team(team) then
return nil
end
if ctf.team(team).spawn and minetest.env:get_node(ctf.team(team).spawn).name == "ctf_flag:flag" then
local flag = ctf_flag.get(ctf.team(team).spawn)
if not flag then
return nil
end
local _team = flag.team
-- Check to see if spawn is already defined
if team == _team then
return nil
end
end
-- Get new spawn
if #ctf.team(team).flags > 0 then
ctf.team(team).spawn = ctf.team(team).flags[1]
return ctf.team(team).spawn return ctf.team(team).spawn
else
return nil
end end
end end
minetest.register_on_respawnplayer(function(player) minetest.register_on_respawnplayer(function(player)
if player and ctf.player(player:get_player_name()) then if not player then
local team = ctf.player(player:get_player_name()).team return false
if ctf.team(team) then
local spawn = ctf.get_spawn(team)
player:moveto(spawn, false)
return true
end
end end
return false local name = player:get_player_name()
local team = ctf.player(name).team
if ctf.team(team) then
local spawn = ctf.get_spawn(team)
player:moveto(spawn, false)
return true
else
return false
end
end) end)

View File

@ -7,7 +7,6 @@ function ctf_chat.init()
end end
ctf_chat.init() ctf_chat.init()
local function team_console_help(name) local function team_console_help(name)
minetest.chat_send_player(name,"Try:", false) minetest.chat_send_player(name,"Try:", false)
minetest.chat_send_player(name,"/team - show team panel", false) minetest.chat_send_player(name,"/team - show team panel", false)
@ -59,17 +58,7 @@ minetest.register_chatcommand("team", {
minetest.chat_send_player(name, "You can not do this!",false) minetest.chat_send_player(name, "You can not do this!",false)
end end
elseif param == "all" then elseif param == "all" then
minetest.chat_send_player(name, "Teams:",false) ctf.list_teams(name)
for k,v in pairs(ctf.teams) do
if v and v.players then
local numPlayers = ctf.count_players_in_team(k)
local numFlags = 0
for k, v in pairs(v.flags) do
numFlags = numFlags + 1
end
minetest.chat_send_player(name, ">> "..k.." ("..numFlags.." flags, "..numPlayers.." players)")
end
end
elseif ctf.team(param) then elseif ctf.team(param) then
minetest.chat_send_player(name,"Team "..param..":",false) minetest.chat_send_player(name,"Team "..param..":",false)
local count = 0 local count = 0
@ -124,27 +113,6 @@ minetest.register_chatcommand("join", {
ctf.join(name, param, false) ctf.join(name, param, false)
end, end,
}) })
minetest.register_chatcommand("list_teams", {
params = "",
description = "List all available teams",
func = function(name, param)
minetest.chat_send_player(name, "This command will be made obsolete! Use '/team all' instead!",false)
minetest.chat_send_player(name, "Teams:")
for k,v in pairs(ctf.teams) do
if v and v.players then
local numItems = 0
for k,v in pairs(v.players) do
numItems = numItems + 1
end
local numItems2 = 0
for k,v in pairs(v.flags) do
numItems2 = numItems2 + 1
end
minetest.chat_send_player(name, ">> "..k.." ("..numItems2.." flags, "..numItems.." players)",false)
end
end
end,
})
minetest.register_chatcommand("ctf_clean", { minetest.register_chatcommand("ctf_clean", {
description = "Do admin cleaning stuff", description = "Do admin cleaning stuff",
@ -152,7 +120,7 @@ minetest.register_chatcommand("ctf_clean", {
func = function(name, param) func = function(name, param)
ctf.clean_player_lists() ctf.clean_player_lists()
if ctf_flag.collect_claimed then if ctf_flag.collect_claimed then
ctf.collect_claimed() ctf_flag.collect_claimed()
end end
minetest.chat_send_player(name, "CTF cleaned!") minetest.chat_send_player(name, "CTF cleaned!")
end, end,

View File

@ -33,6 +33,23 @@ ctf.register_on_territory_query(function(pos)
return closest_team, closest_distSQ return closest_team, closest_distSQ
end) end)
ctf_flag = {} ctf_flag = {}
function ctf.get_spawn(team)
if not ctf.team(team) then
return nil
end
if ctf.team(team).spawn then
return ctf.team(team).spawn
end
-- Get spawn from first flag
ctf_flag.asset_flags(team)
if #ctf.team(team).flags > 0 then
return ctf.team(team).flags[1]
else
return nil
end
end
dofile(minetest.get_modpath("ctf_flag") .. "/gui.lua") dofile(minetest.get_modpath("ctf_flag") .. "/gui.lua")
dofile(minetest.get_modpath("ctf_flag") .. "/flag_func.lua") dofile(minetest.get_modpath("ctf_flag") .. "/flag_func.lua")