From 2653a09329d9a0dec9f8c03407b5f30065097d25 Mon Sep 17 00:00:00 2001 From: ANAND Date: Thu, 31 Oct 2019 20:00:50 +0530 Subject: [PATCH] Allocate players into teams on the basis of cumulative team scores (#319) Implement `ctf.custom_alloc`, which recalculates cumulative team scores and returns the name of the team with the lowest cumulative score. --- minetest.conf | 2 +- mods/ctf/ctf_alloc/init.lua | 57 +++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/minetest.conf b/minetest.conf index a07ce3b..0eb278f 100644 --- a/minetest.conf +++ b/minetest.conf @@ -24,7 +24,7 @@ ctf.flag.nobuild_radius = 3 ctf.flag.alerts = true ctf.flag.drop_time = 300 -ctf.allocate_mode = 3 +ctf.allocate_mode = 4 ctf.players_can_change_team = false ctf.friendly_fire = false ctf.autoalloc_on_joinplayer = true diff --git a/mods/ctf/ctf_alloc/init.lua b/mods/ctf/ctf_alloc/init.lua index 26a6e58..a09463f 100644 --- a/mods/ctf/ctf_alloc/init.lua +++ b/mods/ctf/ctf_alloc/init.lua @@ -1,6 +1,7 @@ local storage = minetest.get_mod_storage() local data = minetest.parse_json(storage:get_string("locktoteam")) or {} +-- Override autoalloc function to implement team-locking local ctf_autoalloc = ctf.autoalloc function ctf.autoalloc(name, alloc_mode) if data[name] then @@ -10,7 +11,7 @@ function ctf.autoalloc(name, alloc_mode) return ctf_autoalloc(name, alloc_mode) end -ChatCmdBuilder.new("ctf_lockpt", function(cmd) +ChatCmdBuilder.new("ctf_lock_to_team", function(cmd) cmd:sub(":name :team", function(name, pname, team) if team == "!" then data[pname] = nil @@ -29,6 +30,59 @@ end, { } }) +-- Struct containing the name and score of the team with lowest cumulative score +local lowest = {} +--[[ + lowest = { + team =, + score = + } +]] + +-- List of cumulative team scores indexed by team name +local scores = {} +--[[ + scores = { + red = , + blue + } +]] + +local function update_lowest() + -- Update lowest.score and lowest.team + lowest = {} + for tname, score in pairs(scores) do + if not lowest.score or score <= lowest.score then + lowest.score = score + lowest.team = tname + end + end + print("\nlowest = " .. dump(lowest) .. "\n") +end + +local function calc_scores() + -- Update the cumulative score of all teams + print("\n[calc_scores]") + for tname, team in pairs(ctf.teams) do + local score = 0 + for pname, _ in pairs(team.players) do + score = score + ctf_stats.player(pname).score + end + scores[tname] = score + print("\t" .. tname .. " = " .. score) + end + + update_lowest() + print("[calc_scores] ********\n") +end + +-- Override team-allocation logic +-- Allocate player into the team with the lowest cumulative score +function ctf.custom_alloc(name) + calc_scores() + return lowest.team +end + function table.map_inplace(t, f) -- luacheck: ignore for key, value in pairs(t) do t[key] = f(value) @@ -67,7 +121,6 @@ function ctf_alloc.set_all() ctf.log("autoalloc", name .. " was allocated to " .. team) ctf.join(name, team) end - ctf.move_to_spawn(name) if ctf.setting("match.clear_inv") then