diff --git a/minetest.conf.example b/minetest.conf.example index 7550d6f..dcaf6cc 100644 --- a/minetest.conf.example +++ b/minetest.conf.example @@ -1,3 +1,6 @@ +# These settings are out of date, +# look for `ctf._set(` lines in mods/capturetheflag/ etc and prepend `ctf.` + # Whether teams own the nodes around their flag #ctf.node_ownership = true #ctf.flag_protect_distance = 25 @@ -21,8 +24,8 @@ # news # flags # diplo -# admin -#ctf.team_gui_initial = news +# settings +#ctf.gui.team.initial = news # Enable tabs in Team GUI #ctf.flag_teleport_gui = true diff --git a/mods/capturetheflag/ctf/area.lua b/mods/capturetheflag/ctf/area.lua index f70c788..69b28b0 100644 --- a/mods/capturetheflag/ctf/area.lua +++ b/mods/capturetheflag/ctf/area.lua @@ -148,6 +148,7 @@ function ctf.area.get_spawn(team) end function ctf.area.asset_flags(team) + --[[ if not team or not ctf.team(team) then return false end @@ -161,5 +162,5 @@ function ctf.area.asset_flags(team) ctf.log("utils", "Replacing flag...") -- TODO: ctf.area.asset_flags end - end + end]]-- end diff --git a/mods/capturetheflag/ctf/core.lua b/mods/capturetheflag/ctf/core.lua index c21cf71..c7e49ee 100644 --- a/mods/capturetheflag/ctf/core.lua +++ b/mods/capturetheflag/ctf/core.lua @@ -1,3 +1,4 @@ +-- Registered ctf.registered_on_load = {} function ctf.register_on_load(func) table.insert(ctf.registered_on_load, func) @@ -11,10 +12,12 @@ function ctf.register_on_init(func) table.insert(ctf.registered_on_init, func) end + + +-- Debug helpers function ctf.error(area, msg) minetest.log("error", "CTF::" .. area .. " - " ..msg) end - function ctf.log(area, msg) if area and area ~= "" then print("[CaptureTheFlag] (" .. area .. ") " .. msg) @@ -22,11 +25,19 @@ function ctf.log(area, msg) print("[CaptureTheFlag] " .. msg) end end - +function ctf.action(area, msg) + if area and area ~= "" then + minetest.log("action", "[CaptureTheFlag] (" .. area .. ") " .. msg) + else + nubetest.log("action", "[CaptureTheFlag] " .. msg) + end +end function ctf.warning(area, msg) print("WARNING: [CaptureTheFlag] (" .. area .. ") " .. msg) end + + function ctf.init() ctf.log("init", "Initialising!") @@ -48,12 +59,7 @@ function ctf.init() ctf._set("flag_names", true) -- Settings: User Interface - ctf._set("gui", true) ctf._set("hud", true) - ctf._set("team_gui", true) - ctf._set("flag_teleport_gui", true) - ctf._set("spawn_in_flag_teleport_gui", false) - ctf._set("news_gui", true) -- Settings: Teams ctf._set("diplomacy", true) @@ -92,6 +98,11 @@ function ctf.setting(name) local set = minetest.setting_get("ctf."..name) or minetest.setting_get("ctf_"..name) local dset = ctf._defsettings[name] + if dset == nil then + ctf.error("setting", "No such setting - " .. name) + return nil + end + if set ~= nil then if type(dset) == "number" then return tonumber(set) @@ -100,11 +111,8 @@ function ctf.setting(name) else return set end - elseif dset ~= nil then - return ctf._defsettings[name] else - ctf.log("setting", name.." not found!") - return nil + return dset end end @@ -191,8 +199,22 @@ function ctf.count_players_in_team(team) return count end +function ctf.new_player(name) + ctf.log("team", "Creating player " .. name) + ctf.players[name] = { + name = name + } +end + -- get a player function ctf.player(name) + if not ctf.players[name] then + ctf.new_player(name) + end + return ctf.players[name] +end + +function ctf.player_or_nil(name) return ctf.players[name] end @@ -207,13 +229,9 @@ function ctf.join(name, team, force) local player = ctf.player(name) - if not player then - player = {name = name} - ctf.players[name] = player - end - - if not force and not ctf.setting("players_can_change_team") and (not player.team or player.team == "") then - minetest.log("action", name .. " attempted to change to " .. team) + 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!") return false end @@ -237,12 +255,13 @@ function ctf.join(name, team, force) return false end +-- TODO: refactor ctf.add_user etc -- Add a player to a team in data structures function ctf.add_user(team, user) local _team = ctf.team(team) local _user = ctf.player(user.name) if _team and user and user.name then - if _user and _user.team and ctf.team(_user.team) then + if _user.team and ctf.team(_user.team) then ctf.teams[_user.team].players[user.name] = nil end diff --git a/mods/capturetheflag/ctf/flag.lua b/mods/capturetheflag/ctf/flag.lua index afcb65f..0c0ce11 100644 --- a/mods/capturetheflag/ctf/flag.lua +++ b/mods/capturetheflag/ctf/flag.lua @@ -70,9 +70,9 @@ ctf.flag_func = { return end - if ctf.players and ctf.team(team) and ctf.player(player) and ctf.player(player).team then + if ctf.team(team) and ctf.player(player).team then if ctf.player(player).team ~= team then - local diplo = ctf.diplo.get(team,ctf.player(player).team) + local diplo = ctf.diplo.get(team, ctf.player(player).team) if not diplo then diplo = ctf.setting("default_diplo_state") @@ -83,11 +83,6 @@ ctf.flag_func = { return end - --ctf.post(team,{msg=flag_name.." has been captured by "..ctf.player(player).team,icon="flag_red"}) - --ctf.post(ctf.player(player).team,{msg=player.." captured '"..flag_name.."' from "..team,icon="flag_green"}) - --ctf.post(team,{msg="The flag at ("..pos.x..","..pos.z..") has been captured by "..ctf.player(player).team,icon="flag_red"}) - --ctf.post(ctf.player(player).team,{msg=player.." captured flag ("..pos.x..","..pos.z..") from "..team,icon="flag_green"}) - local flag_name = flag.name if ctf.setting("flag_capture_take") then if flag_name and flag_name~="" then diff --git a/mods/capturetheflag/ctf/gui.lua b/mods/capturetheflag/ctf/gui.lua index f559893..7e2ae4e 100644 --- a/mods/capturetheflag/ctf/gui.lua +++ b/mods/capturetheflag/ctf/gui.lua @@ -1,33 +1,74 @@ -ctf.gui = {} +ctf.gui = { + tabs = {} +} + +ctf.register_on_init(function() + ctf._set("gui", true) + ctf._set("gui.team", true) + ctf._set("gui.team.initial", "news") + ctf._set("gui.team.teleport_to_flag", true) + ctf._set("gui.team.teleport_to_spawn", false) + + for name, tab in pairs(ctf.gui.tabs) do + ctf._set("gui.tab." .. name, true) + end +end) + +function ctf.gui.register_tab(name, title, func) + ctf.gui.tabs[name] = { + name = name, + title = title, + func = func + } +end + +function ctf.gui.show(name, tab, team) + if not tab then + tab = ctf.setting("gui.team.initial") or "news" + end + + if not tab or not ctf.gui.tabs[tab] or not name or name == "" then + ctf.log("gui", "Invalid tab or name given to ctf.gui.show") + return + end + + if not ctf.setting("gui.team") or not ctf.setting("gui") then + return + end + + if not team or not ctf.team(team) then + team = ctf.player(name).team + print(team) + end + + if team and team ~= "" and ctf.team(team) then + ctf.action("gui", name .. " views " .. team .. "'s " .. tab .. " page") + ctf.gui.tabs[tab].func(name, team) + else + ctf.log("gui", "Invalid team given to ctf.gui.show") + end +end -- Get tab buttons -function ctf.gui.tabs(name,team) +function ctf.gui.get_tabs(name, team) local result = "" local id = 1 local function addtab(name,text) result = result .. "button["..(id*2-1)..",0;2,1;"..name..";"..text.."]" id = id + 1 end - if ctf.setting("news_gui") then - addtab("board","News") + + for name, tab in pairs(ctf.gui.tabs) do + if ctf.setting("gui.tab."..name) then + addtab(name, tab.title) + end end - if ctf.setting("flag_teleport_gui") then - addtab("flags","Flags") - end - if ctf.setting("diplomacy") then - addtab("diplo","Diplomacy") - end - addtab("admin","Settings") + return result end -- Team interface -function ctf.gui.team_board(name,team) - ctf.log("gui", name .. " views team_board") - if not ctf.setting("team_gui") or not ctf.setting("gui") then - return - end - +ctf.gui.register_tab("news", "News", function(name, team) local result = "" local data = ctf.teams[team].log @@ -37,9 +78,9 @@ function ctf.gui.team_board(name,team) local amount = 0 - for i=1,#data do + for i = 1, #data do if data[i].type == "request" then - if ctf.can_mod(name,team)==true then + if ctf.can_mod(name, team) then amount = amount + 2 local height = (amount*0.5) + 0.5 amount = amount + 1 @@ -59,18 +100,17 @@ function ctf.gui.team_board(name,team) end else amount = amount + 1 - local height = (amount*0.5)+0.5 + local height = (amount*0.5) + 0.5 if height > 5 then break end - result = result .. "label[0.5,".. height ..";".. minetest.formspec_escape(data[i].msg) .."]" end end - if ctf.can_mod(name,team)==true then + if ctf.can_mod(name, team) then result = result .. "button[4,6;2,1;clear;Clear all]" end @@ -79,20 +119,14 @@ function ctf.gui.team_board(name,team) "label[0.5,1.5;News such as attacks will appear here]" end - minetest.show_formspec(name, "ctf:board", + minetest.show_formspec(name, "ctf:news", "size[10,7]".. - ctf.gui.tabs(name,team).. - result - ) -end + ctf.gui.get_tabs(name,team).. + result) +end) -- Team interface -function ctf.gui.team_flags(name,team) - ctf.log("gui", name .. " views team_flags") - if not ctf.setting("team_gui") or not ctf.setting("gui") then - return - end - +ctf.gui.register_tab("flags", "Flags", function(name, team) local result = "" local t = ctf.team(team) @@ -104,8 +138,8 @@ function ctf.gui.team_flags(name,team) local y = 2 result = result .. "label[1,1;Click a flag button to go there]" - if ctf.setting("spawn_in_flag_teleport_gui") and minetest.get_setting("static_spawnpoint") then - local x,y,z = string.match(minetest.get_setting("static_spawnpoint"),"(%d+),(%d+),(%d+)") + if ctf.setting("gui.team.teleport_to_spawn") and minetest.get_setting("static_spawnpoint") then + local x,y,z = string.match(minetest.get_setting("static_spawnpoint"), "(%d+),(%d+),(%d+)") result = result .. "button[" .. x .. "," .. y .. ";2,1;goto_" @@ -115,7 +149,7 @@ function ctf.gui.team_flags(name,team) x = x + 2 end - for i=1,#t.flags do + for i=1, #t.flags do local f = t.flags[i] if x > 8 then @@ -142,18 +176,12 @@ function ctf.gui.team_flags(name,team) minetest.show_formspec(name, "ctf:flags", "size[10,7]".. - ctf.gui.tabs(name,team).. - result - ) -end + ctf.gui.get_tabs(name,team).. + result) +end) -- Team interface -function ctf.gui.team_dip(name,team) - ctf.log("gui", name .. " views team_dip") - if not ctf.setting("team_gui") or not ctf.setting("gui") then - return - end - +ctf.gui.register_tab("diplo", "Diplomacy", function(name, team) local result = "" local data = {} @@ -203,24 +231,15 @@ function ctf.gui.team_dip(name,team) end end - minetest.show_formspec(name, "ctf:dip", + minetest.show_formspec(name, "ctf:diplo", "size[10,7]".. - ctf.gui.tabs(name,team).. + ctf.gui.get_tabs(name,team).. result ) -end +end) -- Team interface -function ctf.gui.team_settings(name,team) - ctf.log("gui", name .. " views team_settings") - if not ctf.setting("team_gui") or not ctf.setting("gui") then - return - end - - if not team or not ctf.team(team) then - return - end - +ctf.gui.register_tab("settings", "Settings", function(name, team) local color = "" if ctf.team(team).data and ctf.team(team).data.color then @@ -235,71 +254,72 @@ function ctf.gui.team_settings(name,team) result = "label[0.5,1;You do not own this team!" end - minetest.show_formspec(name, "ctf:team_settings", + minetest.show_formspec(name, "ctf:settings", "size[10,7]".. - ctf.gui.tabs(name,team).. + ctf.gui.get_tabs(name,team).. result ) +end) + +local function formspec_is_ctf_tab(fsname) + for name, tab in pairs(ctf.gui.tabs) do + if fsname == "ctf:" .. name then + return true + end + end + print(fsname .. " is not a ctf_tab") + return false end + minetest.register_on_player_receive_fields(function(player, formname, fields) local name = player:get_player_name() - if formname=="ctf:board" or formname=="ctf:flags" or formname=="ctf:dip" or formname=="ctf:team_settings" then - if fields.flags then - if ctf and ctf.players and ctf.players[name] and ctf.players[name].team then - ctf.gui.team_flags(name,ctf.players[name].team) - end + if not formspec_is_ctf_tab(formname) then + return false + end + + -- Do navigation + for tname, tab in pairs(ctf.gui.tabs) do + if fields[tname] then + ctf.gui.show(name, tname) return true end - if fields.board then - if ctf and ctf.players and ctf.players[name] and ctf.players[name].team then - ctf.gui.team_board(name,ctf.players[name].team) - end - return true + end + + -- Todo: move callbacks + -- News page + if fields.clear then + if ctf and ctf.players and ctf.players[name] and ctf.players[name].team then + ctf.team(ctf.players[name].team).log = {} + ctf.save() + ctf.gui.team_board(name, ctf.players[name].team) end - if fields.diplo then - if ctf and ctf.players and ctf.players[name] and ctf.players[name].team then - ctf.gui.team_dip(name,ctf.players[name].team) - end - return true + return true + end + + -- Settings page + if fields.save and formname=="ctf:settings" then + if ctf and ctf.players and ctf.players[name] and ctf.players[name].team then + ctf.gui.team_settings(name, ctf.players[name].team) end - if fields.admin then - if ctf and ctf.players and ctf.players[name] and ctf.players[name].team then - ctf.gui.team_settings(name,ctf.players[name].team) - end - return true - end - if fields.clear then - if ctf and ctf.players and ctf.players[name] and ctf.players[name].team then - ctf.team(ctf.players[name].team).log = {} + if ctf and ctf.team(ctf.players[name].team) and ctf.team(ctf.players[name].team).data then + if minetest.registered_items["ctf:flag_top_"..fields.color] then + ctf.team(ctf.players[name].team).data.color = fields.color ctf.save() - ctf.gui.team_board(name,ctf.players[name].team) + else + minetest.chat_send_player(name,"Color "..fields.color.." does not exist!") end - return true - end - if fields.save and formname=="ctf:team_settings" then - if ctf and ctf.players and ctf.players[name] and ctf.players[name].team then - ctf.gui.team_settings(name,ctf.players[name].team) - end - if ctf and ctf.team(ctf.players[name].team) and ctf.team(ctf.players[name].team).data then - if minetest.registered_items["ctf:flag_top_"..fields.color] then - ctf.team(ctf.players[name].team).data.color = fields.color - ctf.save() - else - minetest.chat_send_player(name,"Color "..fields.color.." does not exist!") - end - end - return true end + return true end end) minetest.register_on_player_receive_fields(function(player, formname, fields) local name = player:get_player_name() - if formname=="ctf:board" then + if formname=="ctf:news" then for key, field in pairs(fields) do local ok, id = string.match(key, "btn_([yn])([0123456789]+)") if ok and id then - if ctf.player(name) and ctf.player(name).team and ctf.team(ctf.player(name).team) then + if ctf.player(name).team and ctf.team(ctf.player(name).team) then if ok == "y" then ctf.diplo.set(ctf.player(name).team, ctf.team(ctf.player(name).team).log[tonumber(id)].team, ctf.team(ctf.player(name).team).log[tonumber(id)].msg) ctf.post(ctf.player(name).team,{msg="You have accepted the "..ctf.team(ctf.player(name).team).log[tonumber(id)].msg.." request from "..ctf.team(ctf.player(name).team).log[tonumber(id)].team}) @@ -318,12 +338,13 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end) minetest.register_on_player_receive_fields(function(player, formname, fields) + -- Todo: fix security issue here local name = player:get_player_name() if formname=="ctf:flags" then for key, field in pairs(fields) do local x,y,z = string.match(key, "goto_(%d+)_(%d+)_(%d+)") if x and y and x then - player:setpos({x=x,y=y,z=z}) + player:setpos({x=x, y=y, z=z}) return true end end @@ -332,7 +353,7 @@ end) minetest.register_on_player_receive_fields(function(player, formname, fields) local name = player:get_player_name() - if formname=="ctf:dip" then + if formname=="ctf:diplo" then for key, field in pairs(fields) do local newteam = string.match(key, "team_(.+)") if newteam then diff --git a/mods/capturetheflag/ctf_chat/init.lua b/mods/capturetheflag/ctf_chat/init.lua index 9adba1e..8361e2f 100644 --- a/mods/capturetheflag/ctf_chat/init.lua +++ b/mods/capturetheflag/ctf_chat/init.lua @@ -108,18 +108,7 @@ minetest.register_chatcommand("team", { ctf.players[name].team and ctf.setting("gui") ) then - minetest.chat_send_player(name, "Showing the Team GUI") - if ctf.setting("team_gui_initial") == "news" and ctf.setting("news_gui") then - ctf.gui.team_board(name,ctf.players[name].team) - elseif ctf.setting("team_gui_initial") == "flags" and ctf.setting("flag_teleport_gui") then - ctf.gui.team_flags(name,ctf.players[name].team) - elseif ctf.setting("team_gui_initial") == "diplo" and ctf.setting("diplomacy") then - ctf.gui.team_dip(name,ctf.players[name].team) - elseif ctf.setting("team_gui_initial") == "admin" then - ctf.gui.team_settings(name,ctf.players[name].team) - elseif ctf.setting("news_gui") then - ctf.gui.team_board(name,ctf.players[name].team) - end + ctf.gui.show(name) end end end,