From b309a043c189940741b19c3a17b19d8fec5dac3d Mon Sep 17 00:00:00 2001 From: Billy S Date: Thu, 27 Dec 2018 19:42:59 -0500 Subject: [PATCH] Added power system --- mods/ctf_pvp_engine/ctf/core.lua | 11 ++- mods/ctf_pvp_engine/ctf/gui.lua | 32 +++++---- mods/ctf_pvp_engine/ctf/init.lua | 9 +++ mods/ctf_pvp_engine/ctf/power.lua | 78 ++++++++++++++++++++++ mods/ctf_pvp_engine/ctf/teams.lua | 6 +- mods/ctf_pvp_engine/ctf_chat/init.lua | 4 +- mods/ctf_pvp_engine/ctf_flag/flag_func.lua | 26 +++++++- mods/ctf_pvp_engine/ctf_flag/gui.lua | 4 ++ 8 files changed, 151 insertions(+), 19 deletions(-) create mode 100644 mods/ctf_pvp_engine/ctf/power.lua diff --git a/mods/ctf_pvp_engine/ctf/core.lua b/mods/ctf_pvp_engine/ctf/core.lua index 3d89f92..7219aae 100644 --- a/mods/ctf_pvp_engine/ctf/core.lua +++ b/mods/ctf_pvp_engine/ctf/core.lua @@ -147,7 +147,6 @@ function ctf.init() ctf._set("autoalloc_on_joinplayer", true) ctf._set("friendly_fire", true) - for i = 1, #ctf.registered_on_init do ctf.registered_on_init[i]() end @@ -209,6 +208,16 @@ function ctf.load() ctf.teams = table.teams ctf.players = table.players + -- Convert old power format (from Bob12) to new + for tName, tData in pairs(ctf.teams) do + if type(tData.power) ~= "table" then + ctf.teams[tName].power = { + max_power = ctf.get_team_maxpower(tData), + power = 0 + } + end + end + for i = 1, #ctf.registered_on_load do ctf.registered_on_load[i](table) end diff --git a/mods/ctf_pvp_engine/ctf/gui.lua b/mods/ctf_pvp_engine/ctf/gui.lua index daa452a..4670c90 100644 --- a/mods/ctf_pvp_engine/ctf/gui.lua +++ b/mods/ctf_pvp_engine/ctf/gui.lua @@ -132,9 +132,9 @@ end) ctf.gui.register_tab("applications","Applications", function(name, tname) local result = "" local data = {} - + result = result .. "label[0.5,1;Applicants to join " .. tname .. "]" - + for key, value in pairs(ctf.teams) do if key == tname then local height = 1.5 @@ -150,14 +150,14 @@ ctf.gui.register_tab("applications","Applications", function(name, tname) end end end - + minetest.show_formspec(name, "ctf:applications", "size[10,7]" .. ctf.gui.get_tabs(name, tname) .. result ) end) - + local scroll_diplomacy = 0 local scroll_max = 0 -- Team interface @@ -179,24 +179,24 @@ ctf.gui.register_tab("diplo", "Diplomacy", function(name, tname) end result = result .. "label[1,1;Diplomacy from the perspective of " .. tname .. "]" - + scroll_max = 0 for i = 1, #data do scroll_max = i end scroll_max = scroll_max - 4 - + if scroll_diplomacy > (scroll_max+4) then scroll_diplomacy = (scroll_max+4) end - + if scroll_diplomacy > 0 then result = result .. "button[9.2,0.44;1,3;scroll_up;Up]" end if scroll_diplomacy < scroll_max then result = result .. "button[9.2,3.8;1,3;scroll_down;Down]" end - + for i = 1, #data do amount = i local height = (i*1)+0.5 @@ -267,7 +267,7 @@ function remove_application_log_entry(tname, pname) end end end - + minetest.register_on_player_receive_fields(function(player, formname, fields) if not formspec_is_ctf_tab(formname) then return false @@ -354,11 +354,11 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) local acceptor_name = player:get_player_name() local team_name = ctf.player(acceptor_name).team local team = ctf.team(team_name) - + if not team or formname ~= "ctf:applications" then return false end - + for key, field in pairs(fields) do if ctf.player(acceptor_name).auth or ctf.player(acceptor_name).recruit then local applicant_name = string.match(key, "player_(.+)") @@ -386,7 +386,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) cur_team = nil return false end - + if cur_team == nil then cur_team = tname end @@ -428,7 +428,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) ctf.diplo.set(tname, tname2, "peace") ctf.post(tname, { msg = "You have cancelled the alliance treaty with " .. tname2 }) - irc:say(tname .. " has cancelled the alliance treaty with " .. tname2 .. "!") + if irc then + irc:say(tname .. " has cancelled the alliance treaty with " .. tname2 .. "!") + end minetest.chat_send_all(tname .. " has cancelled the alliance treaty with " .. tname2 .. "!") ctf.post(tname2, { msg = tname .. " has cancelled the alliance treaty" }) @@ -443,7 +445,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) ctf.diplo.set(tname, tname2, "war") ctf.post(tname, { msg = "You have declared war on " .. tname2 }) - irc:say(tname .. " has declared war on " .. tname2 .. "!") + if irc then + irc:say(tname .. " has declared war on " .. tname2 .. "!") + end minetest.chat_send_all(tname .. " has declared war on " .. tname2 .. "!") ctf.post(tname2, { msg = tname .. " has declared war on you" }) diff --git a/mods/ctf_pvp_engine/ctf/init.lua b/mods/ctf_pvp_engine/ctf/init.lua index 61d3f8f..10d5d2c 100644 --- a/mods/ctf_pvp_engine/ctf/init.lua +++ b/mods/ctf_pvp_engine/ctf/init.lua @@ -27,7 +27,16 @@ dofile(minetest.get_modpath("ctf") .. "/teams.lua") dofile(minetest.get_modpath("ctf") .. "/diplomacy.lua") dofile(minetest.get_modpath("ctf") .. "/gui.lua") dofile(minetest.get_modpath("ctf") .. "/hud.lua") +dofile(minetest.get_modpath("ctf") .. "/power.lua") -- Init ctf.init() ctf.clean_player_lists() + +-- Start power ticker +local power_tick = 300 -- 5 mins +local function power_tick_func() + ctf.on_power_tick() + minetest.after(power_tick, power_tick_func) +end +minetest.after(power_tick, power_tick_func) diff --git a/mods/ctf_pvp_engine/ctf/power.lua b/mods/ctf_pvp_engine/ctf/power.lua new file mode 100644 index 0000000..c5e893e --- /dev/null +++ b/mods/ctf_pvp_engine/ctf/power.lua @@ -0,0 +1,78 @@ +local maxpowerPerFlag = 2 +local powerIncreasePerActivePlayer = 0.1 +local powerDecreaseWhenInactivePerFlag = 0.001 + +local function elementsInTable(t) + local n = 0 + for _ in pairs(t) do n = n + 1 end + return n +end + +function ctf.get_team_maxpower(team) + return elementsInTable(team.flags) * maxpowerPerFlag +end + +function ctf.team_has_online_players(tName) + for _, p in pairs(minetest.get_connected_players()) do + local n = p:get_player_name() + if ctf.players[n] and ctf.players[n].team == tName then + return true + end + end + return false +end + +function ctf.on_power_tick() + -- Modify team's power + local active = {} + local players = minetest.get_connected_players() + for _, player in pairs(players) do + local name = player:get_player_name() + if ctf.players[name] and ctf.players[name].team then + local tName = ctf.players[name].team + if active[tName] == nil then + active[tName] = 1 + else + active[tName] = active[tName] + 1 + end + end + end + for tName, t in pairs(ctf.teams) do + if active[tName] == nil then + t.power.power = t.power.power - powerDecreaseWhenInactivePerFlag * elementsInTable(t.flags) + if t.power.power < 0 then + t.power.power = 0 + end + else + t.power.power = t.power.power + active[tName] * powerIncreasePerActivePlayer + if t.power.power > t.power.max_power then + t.power.power = t.power.max_power + end + end + end + -- Save + ctf.needs_save = true + -- Update huds + ctf.hud.updateAll() +end + +-- Hud for power +ctf.hud.register_part(function(player, name, _) + local pTeam = ctf.team(ctf.players[name].team) + if pTeam then + local powerStr = "Power: " .. pTeam.power.power .. "\nMax Power: " .. pTeam.power.max_power + if ctf.hud:exists(player, "ctf:hud_power") then + ctf.hud:change(player, "ctf:hud_power", "text", powerStr) + else + ctf.hud:add(player, "ctf:hud_power", { + hud_elem_type = "text", + position = {x = 0, y = 1}, + scale = {x = 1, y = 1}, + text = powerStr, + number = 0xFFFFFF, + offset = {x = 5, y = -5}, + alignment = {x = 1, y = -1} + }) + end + end +end) diff --git a/mods/ctf_pvp_engine/ctf/teams.lua b/mods/ctf_pvp_engine/ctf/teams.lua index 8d63715..e3b8c1d 100644 --- a/mods/ctf_pvp_engine/ctf/teams.lua +++ b/mods/ctf_pvp_engine/ctf/teams.lua @@ -39,7 +39,11 @@ function ctf.create_team(name, data) spawn = nil, players = {}, applications = {}, - access = {} + access = {}, + power = { + max_power = 0, + power = 0 + } } for i = 1, #ctf.registered_on_new_team do diff --git a/mods/ctf_pvp_engine/ctf_chat/init.lua b/mods/ctf_pvp_engine/ctf_chat/init.lua index 3a84c92..46a0fab 100644 --- a/mods/ctf_pvp_engine/ctf_chat/init.lua +++ b/mods/ctf_pvp_engine/ctf_chat/init.lua @@ -84,7 +84,8 @@ minetest.register_chatcommand("team", { elseif ctf.team(param) then minetest.chat_send_player(name, "Team "..param..":") local count = 0 - for _, value in pairs(ctf.team(param).players) do + local t = ctf.team(param) + for _, value in pairs(t.players) do count = count + 1 if value.auth then minetest.chat_send_player(name, count .. ">> " .. value.name @@ -96,6 +97,7 @@ minetest.register_chatcommand("team", { minetest.chat_send_player(name, count .. ">> " .. value.name) end end + minetest.chat_send_player(name, "Team power: " .. t.power.power .. "/" .. t.power.max_power) elseif ctf.player_or_nil(param) or test then if not test then test = param diff --git a/mods/ctf_pvp_engine/ctf_flag/flag_func.lua b/mods/ctf_pvp_engine/ctf_flag/flag_func.lua index fcf347b..38b0611 100644 --- a/mods/ctf_pvp_engine/ctf_flag/flag_func.lua +++ b/mods/ctf_pvp_engine/ctf_flag/flag_func.lua @@ -7,6 +7,10 @@ local function elementsInTable(t) return n end +local function recalc_team_maxpower(team) + team.power.max_power = ctf.get_team_maxpower(team) +end + local function can_place_flag(pos) local lpos = pos local pos1 = {x=lpos.x-r+1,y=lpos.y,z=lpos.z-r+1} @@ -106,9 +110,20 @@ local function do_capture(attname, flag, returned) end end + -- Check if this team has any power / online memebers + local tData = ctf.team(team) + if ctf.team_has_online_players(team) == false then + if tData.power.power > 0 then + minetest.chat_send_player(attname, "You cannot capture this flag right now.") + return + end + end + minetest.chat_send_all(flag_name.." has been captured ".. " by "..attname.." (team "..attacker.team..")") - irc:say(flag_name.." has been captured by "..attname.." (team "..attacker.team..")") + if irc then + irc:say(flag_name.." has been captured by "..attname.." (team "..attacker.team..")") + end ctf.action("flag", attname .. " captured " .. flag_name) @@ -131,11 +146,15 @@ local function do_capture(attname, flag, returned) ctf_flag.delete(team,pos) end + -- Recalculate team maxpowers + local aTeam = ctf.team(attacker.team) + recalc_team_maxpower(tData) + recalc_team_maxpower(aTeam) + for i = 1, #ctf_flag.registered_on_capture do ctf_flag.registered_on_capture[i](attname, flag) end end - ctf.needs_save = true end @@ -334,6 +353,9 @@ ctf_flag = { ctf.needs_save = true end + -- Recalc team max power + recalc_team_maxpower(team) + minetest.set_node(pos, {name = "ctf_flag:flag"}) minetest.set_node(pos2, {name = "ctf_flag:flag_top_" .. team.data.color}) diff --git a/mods/ctf_pvp_engine/ctf_flag/gui.lua b/mods/ctf_pvp_engine/ctf_flag/gui.lua index 44efd3e..7270947 100644 --- a/mods/ctf_pvp_engine/ctf_flag/gui.lua +++ b/mods/ctf_pvp_engine/ctf_flag/gui.lua @@ -164,6 +164,10 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.set_node(pos,{name="air"}) player:get_inventory():add_item('main', 'ctf_flag:flag') + -- Recalc max power + local t = ctf.team(team) + t.power.max_power = ctf.get_team_maxpower(t) + ctf.needs_save = true return true end