Skip to content

Commit 753a182

Browse files
committed
Resurrect #96
1 parent fa3af08 commit 753a182

1 file changed

Lines changed: 34 additions & 3 deletions

File tree

lua/cfc_random_spawn/module/sv_player_spawning.lua

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,21 @@ local CLOSENESS_LIMIT = CFCRandomSpawn.Config.CLOSENESS_LIMIT ^ 2
1313
local CENTER_UPDATE_INTERVAL = customSpawnConfigForMap.centerUpdateInterval or CFCRandomSpawn.Config.CENTER_UPDATE_INTERVAL
1414
local IGNORE_BUILDERS = CFCRandomSpawn.Config.IGNORE_BUILDERS
1515

16+
local ACTIVE_PLAYER_TIMEOUT = 120 -- players who haven't died/killed anyone in this many seconds aren't 'pvping' and won't influence the pvp center, etc
17+
1618
local DYNAMIC_CENTER_MINSIZE = 2000 -- getDynamicPvpCenter starts at this radius
1719
local DYNAMIC_CENTER_MAXSIZE = 4000 -- no bigger than this radius
1820
local DYNAMIC_CENTER_MINSPAWNS = 10 -- getDynamicPvpCenter needs at least this many spawns inside the radius
1921
local DYNAMIC_CENTER_MAXSPAWNS = 35 -- max possible spawns
2022
local DYNAMIC_CENTER_SPAWNCOUNTMATCHPVPERS = true -- pvp center gets bigger when more people are pvping
2123
local DYNAMIC_CENTER_IMPERFECT = true -- throw a bit of randomness in, makes pvp less stiff.
2224

25+
CFCRandomSpawn.recentCombatants = CFCRandomSpawn.recentCombatants or {}
26+
local recentCombatants = CFCRandomSpawn.recentCombatants
27+
28+
local CurTime = CurTime
29+
local Vector = Vector
30+
2331
local function defaultPvpCenter()
2432
return pvpCenters[1]
2533
end
@@ -79,15 +87,36 @@ local function getMeasurablePlayers()
7987
local measurablePlayers = {}
8088
local humans = IGNORE_BUILDERS and getPvpers() or player.GetAll()
8189

90+
local cur = CurTime()
91+
8292
for _, ply in pairs( humans ) do
83-
if ply:Alive() then
93+
if ply:Alive() and recentCombatants[ply] > cur then
8494
table.insert( measurablePlayers, ply )
8595
end
8696
end
8797

8898
return measurablePlayers
8999
end
90100

101+
local function countAsCombatting( ply )
102+
local cur = CurTime()
103+
recentCombatants[ply] = math.max( cur + ACTIVE_PLAYER_TIMEOUT, recentCombatants[ply] + ACTIVE_PLAYER_TIMEOUT / 2 )
104+
end
105+
106+
hook.Add( "PlayerInitialSpawn", "cfc_randomspawn_firstspawn", function( ply )
107+
recentCombatants[ply] = 0
108+
end )
109+
110+
hook.Add( "PlayerDeath", "cfc_randomspawn_trackrecentcombatants", function( died, _inflictor, attacker )
111+
countAsCombatting( died )
112+
if not attacker:IsPlayer() then return end
113+
countAsCombatting( attacker )
114+
end )
115+
116+
hook.Add( "PlayerDisconnected", "cfc_randomspawn_cleanuprecentcombatants", function( ply )
117+
recentCombatants[ply] = nil
118+
end )
119+
91120
local function getLivingPlayers()
92121
local livingPlayers = {}
93122
local humans = player.GetAll()
@@ -196,9 +225,11 @@ end
196225
local function getPlyAvg( plys, centerPos )
197226
if not plys or not plys[1] then return centerPos or Vector() end
198227

199-
local avgSum = Vector()
228+
local avgSum = plys[1]:GetPos()
229+
if #plys == 1 then return avgSum end
200230

201-
for _, ply in ipairs( plys ) do
231+
for i = 2, #plys do
232+
local ply = plys[i]
202233
avgSum = avgSum + ply:GetPos()
203234
end
204235

0 commit comments

Comments
 (0)