Added power system

This commit is contained in:
Billy S 2018-12-27 19:42:59 -05:00
parent 80c08112c0
commit b309a043c1
8 changed files with 151 additions and 19 deletions

View File

@ -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

View File

@ -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 })
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 })
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" })

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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..")")
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})

View File

@ -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