From a7d5d29220ea1ed6cb2042937036d8a5ebc0eddb Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Mon, 13 Jul 2015 13:19:19 +0100 Subject: [PATCH] Finish decoupling ctf_flags --- mods/capturetheflag/ctf/teams.lua | 71 +++++++++++++++------------ mods/capturetheflag/ctf_chat/init.lua | 36 +------------- mods/capturetheflag/ctf_flag/init.lua | 17 +++++++ 3 files changed, 58 insertions(+), 66 deletions(-) diff --git a/mods/capturetheflag/ctf/teams.lua b/mods/capturetheflag/ctf/teams.lua index 790b712..5f1bfea 100644 --- a/mods/capturetheflag/ctf/teams.lua +++ b/mods/capturetheflag/ctf/teams.lua @@ -29,6 +29,29 @@ function ctf.team(name) -- get or add a team 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 function ctf.count_players_in_team(team) local count = 0 @@ -78,6 +101,7 @@ function ctf.join(name, team, force) if not ctf.team(team) 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) return false end @@ -272,43 +296,26 @@ end) -- updates the spawn position for a team function ctf.get_spawn(team) - ctf_flag.asset_flags(team) - - 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] + if ctf.team(team) and ctf.team(team).spawn then return ctf.team(team).spawn + else + return nil end end minetest.register_on_respawnplayer(function(player) - if player and ctf.player(player:get_player_name()) then - local team = ctf.player(player:get_player_name()).team - if ctf.team(team) then - local spawn = ctf.get_spawn(team) - player:moveto(spawn, false) - return true - end + if not player then + return false 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) diff --git a/mods/capturetheflag/ctf_chat/init.lua b/mods/capturetheflag/ctf_chat/init.lua index 451bfc8..c0ca06a 100644 --- a/mods/capturetheflag/ctf_chat/init.lua +++ b/mods/capturetheflag/ctf_chat/init.lua @@ -7,7 +7,6 @@ function ctf_chat.init() end ctf_chat.init() - local function team_console_help(name) minetest.chat_send_player(name,"Try:", 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) end elseif param == "all" then - minetest.chat_send_player(name, "Teams:",false) - 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 + ctf.list_teams(name) elseif ctf.team(param) then minetest.chat_send_player(name,"Team "..param..":",false) local count = 0 @@ -124,27 +113,6 @@ minetest.register_chatcommand("join", { ctf.join(name, param, false) 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", { description = "Do admin cleaning stuff", @@ -152,7 +120,7 @@ minetest.register_chatcommand("ctf_clean", { func = function(name, param) ctf.clean_player_lists() if ctf_flag.collect_claimed then - ctf.collect_claimed() + ctf_flag.collect_claimed() end minetest.chat_send_player(name, "CTF cleaned!") end, diff --git a/mods/capturetheflag/ctf_flag/init.lua b/mods/capturetheflag/ctf_flag/init.lua index f1112b9..ab70428 100644 --- a/mods/capturetheflag/ctf_flag/init.lua +++ b/mods/capturetheflag/ctf_flag/init.lua @@ -33,6 +33,23 @@ ctf.register_on_territory_query(function(pos) return closest_team, closest_distSQ end) 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") .. "/flag_func.lua")