Auto-allocation

This commit is contained in:
rubenwardy 2014-12-27 17:36:58 +00:00
parent 8fab1cb55a
commit c89ac55ff7
4 changed files with 194 additions and 122 deletions

View File

@ -55,15 +55,12 @@ minetest.register_chatcommand("team", {
minetest.chat_send_player(name, "Teams:",false) minetest.chat_send_player(name, "Teams:",false)
for k,v in pairs(ctf.teams) do for k,v in pairs(ctf.teams) do
if v and v.players then if v and v.players then
local numItems = 0 local numPlayers = ctf.count_players_in_team(k)
for k,v in pairs(v.players) do local numFlags = 0
numItems = numItems + 1
end
local numItems2 = 0
for k, v in pairs(v.flags) do for k, v in pairs(v.flags) do
numItems2 = numItems2 + 1 numFlags = numFlags + 1
end end
minetest.chat_send_player(name, ">> "..k.." ("..numItems2.." flags, "..numItems.." players)",false) minetest.chat_send_player(name, ">> "..k.." ("..numFlags.." flags, "..numPlayers.." players)")
end end
end end
elseif ctf.team(param) then elseif ctf.team(param) then

View File

@ -24,7 +24,8 @@ function ctf.init()
ctf._set("players_can_change_team",true) ctf._set("players_can_change_team",true)
-- Settings: Teams -- Settings: Teams
--ctf._set("allocate_mode",0) -- (COMING SOON):how are players allocated to teams? ctf._set("allocate_mode", 2) -- how are players allocated to teams? 0: none, 1: random, 2: one of first two largest groups, 3 smallest group
ctf._set("maximum_in_team", -1) -- Maximum number in team, obeyed by allocation and /join. Admins don't obey this
ctf._set("default_diplo_state", "war") -- what is the default diplomatic state? (war/peace/alliance) ctf._set("default_diplo_state", "war") -- what is the default diplomatic state? (war/peace/alliance)
--ctf._setb("delete_teams",false) -- (COMING SOON):should teams be deleted when they are defeated? --ctf._setb("delete_teams",false) -- (COMING SOON):should teams be deleted when they are defeated?
@ -51,8 +52,9 @@ function ctf._set(setting,default)
end end
function ctf.setting(name) function ctf.setting(name)
if minetest.setting_get("ctf_"..name) then local set = minetest.setting_get("ctf_"..name)
return minetest.setting_get("ctf_"..name) if set ~= nil then
return set
elseif ctf._defsettings[name] ~= nil then elseif ctf._defsettings[name] ~= nil then
return ctf._defsettings[name] return ctf._defsettings[name]
else else
@ -100,6 +102,15 @@ function ctf.team(name) -- get or add a team
end end
end end
-- Count number of players in a team
function ctf.count_players_in_team(team)
local count = 0
for name, player in pairs(ctf.team(team).players) do
count = count + 1
end
return count
end
-- get a player -- get a player
function ctf.player(name) function ctf.player(name)
return ctf.players[name] return ctf.players[name]
@ -207,3 +218,72 @@ function ctf.post(team,msg)
return true return true
end end
minetest.register_on_newplayer(function(player)
local name = player:get_player_name()
local max_players = ctf.setting("maximum_in_team")
local alloc_mode = tonumber(ctf.setting("allocate_mode"))
if alloc_mode == 1 then
local index = {}
for key, team in pairs(ctf.teams) do
if max_players == -1 or ctf.count_players_in_team(key) < max_players then
table.insert(index, key)
end
end
if #index == 0 then
minetest.log("error", "[CaptureTheFlag] No teams to join!")
else
local team = index[math.random(1, #index)]
print(name.." was allocated to "..team)
ctf.join(name, team)
end
elseif alloc_mode == 2 then
local one = nil
local one_count = -1
local two = nil
local two_count = -1
for key, team in pairs(ctf.teams) do
local count = ctf.count_players_in_team(key)
if (max_players == -1 or count < max_players) then
if count > one_count then
two = one
two_count = one_count
one = key
one_count = count
end
if count > two_count then
two = key
two_count = count
end
end
end
if not one and not two then
minetest.log("error", "[CaptureTheFlag] No teams to join!")
elseif one and two then
if math.random() > 0.5 then
print(name.." was allocated to "..one)
ctf.join(name, one)
else
print(name.." was allocated to "..two)
ctf.join(name, two)
end
else
if one then
print(name.." was allocated to "..one)
ctf.join(name, one)
else
print(name.." was allocated to "..two)
ctf.join(name, two)
end
end
else
print("Unknown allocation mode: "..ctf.setting("allocate_mode"))
end
end)

View File

@ -13,7 +13,7 @@ minetest.register_node("traps:cage",{
inventory_image = minetest.inventorycube("traps_grass.png", inventory_image = minetest.inventorycube("traps_grass.png",
"default_grass_side.png", "default_grass_side.png"), "default_grass_side.png", "default_grass_side.png"),
dug_item = '', -- Get nothing dug_item = '', -- Get nothing
groups={immortal}, groups={immortal = 1},
description = "Cage Trap", description = "Cage Trap",
}) })
@ -22,7 +22,7 @@ minetest.register_node("traps:uncage",{
inventory_image = minetest.inventorycube("traps_uncage.png", inventory_image = minetest.inventorycube("traps_uncage.png",
"traps_uncage.png", "traps_uncage.png"), "traps_uncage.png", "traps_uncage.png"),
dug_item = '', -- Get nothing dug_item = '', -- Get nothing
groups={immortal}, groups={immortal = 1},
description = "Cage Trap Release", description = "Cage Trap Release",
}) })
@ -33,7 +33,7 @@ minetest.register_node("traps:cage_glass", {
inventory_image = minetest.inventorycube("default_glass.png"), inventory_image = minetest.inventorycube("default_glass.png"),
paramtype = "light", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
groups = {immortl}, groups = {immortal = 1},
sounds = default.node_sound_glass_defaults(), sounds = default.node_sound_glass_defaults(),
}) })
@ -49,13 +49,11 @@ minetest.register_abm(
print("HIT!") print("HIT!")
--local objpos=obj:getpos() --local objpos=obj:getpos()
local tmp
minetest.env:add_node(pos,{name=block_to_place}) minetest.env:add_node(pos,{name=block_to_place})
--Left --Left
print("Left") print("Left")
tmp={x=(pos.x+1),y=(pos.y+1),z=(pos.z)} local tmp = {x=(pos.x+1),y=(pos.y+1),z=(pos.z)}
minetest.env:add_node(tmp,{name=block_to_place}) minetest.env:add_node(tmp,{name=block_to_place})
--Right --Right

View File

@ -13,7 +13,7 @@ minetest.register_node("traps:lava",{
inventory_image = minetest.inventorycube("traps_grass.png", inventory_image = minetest.inventorycube("traps_grass.png",
"default_grass_side.png", "default_grass_side.png"), "default_grass_side.png", "default_grass_side.png"),
dug_item = '', -- Get nothing dug_item = '', -- Get nothing
groups={immortal}, groups = {immortal = 1},
description = "Lava Drop Trap", description = "Lava Drop Trap",
}) })
@ -28,14 +28,11 @@ minetest.register_abm(
local objs = minetest.env:get_objects_inside_radius(pos, 1) local objs = minetest.env:get_objects_inside_radius(pos, 1)
for k, obj in pairs(objs) do for k, obj in pairs(objs) do
print("HIT!") print("HIT!")
--local objpos=obj:getpos()
local tmp
minetest.env:add_node(pos, {name = "default:dirt"}) minetest.env:add_node(pos, {name = "default:dirt"})
--Left side pit --Left side pit
tmp={x=pos.x-2,y=pos.y,z=pos.z-2} local tmp = {x = pos.x-2, y = pos.y, z = pos.z-2}
minetest.env:add_node(tmp, {name = "air"}) minetest.env:add_node(tmp, {name = "air"})
tmp = {x = pos.x-2, y = pos.y, z = pos.z-1} tmp = {x = pos.x-2, y = pos.y, z = pos.z-1}