-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsv_player_spawning.lua
More file actions
executable file
·445 lines (343 loc) · 14.5 KB
/
Copy pathsv_player_spawning.lua
File metadata and controls
executable file
·445 lines (343 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
CFCRandomSpawn = CFCRandomSpawn or {}
CFCRandomSpawn.EditingPlayers = CFCRandomSpawn.EditingPlayers or {}
local customSpawnConfigForMap = CFCRandomSpawn.Config.CUSTOM_SPAWNS[game.GetMap()] or {}
local mapHasCustomSpawns = next( customSpawnConfigForMap )
local customSpawnsForMap = customSpawnConfigForMap.spawnpoints or {}
local zonesForMap = customSpawnConfigForMap.zones or {}
local DEFAULT_CENTER_RADIUS = CFCRandomSpawn.Config.DEFAULT_CENTER_RADIUS
local DEFAULT_CENTER_RADIUS_SQR = DEFAULT_CENTER_RADIUS ^ 2
local CENTER_RADIUS_SQR = DEFAULT_CENTER_RADIUS_SQR
local DYNAMIC_CENTER_FALLBACK = customSpawnConfigForMap.dynamicCenterStartingPos or Vector()
local SELECTION_SIZE = CFCRandomSpawn.Config.SELECTION_SIZE
local CLOSENESS_LIMIT = CFCRandomSpawn.Config.CLOSENESS_LIMIT ^ 2
local CENTER_UPDATE_INTERVAL = customSpawnConfigForMap.centerUpdateInterval or CFCRandomSpawn.Config.CENTER_UPDATE_INTERVAL
local IGNORE_BUILDERS = CFCRandomSpawn.Config.IGNORE_BUILDERS
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
local NO_ZONE_ID = 1
local DYNAMIC_CENTER_MINSIZE = 2000 -- getDynamicPvpCenter starts at this radius
local DYNAMIC_CENTER_MAXSIZE = 4000 -- no bigger than this radius
local DYNAMIC_CENTER_MINSPAWNS = 10 -- getDynamicPvpCenter needs at least this many spawns inside the radius
local DYNAMIC_CENTER_MAXSPAWNS = 35 -- max possible spawns
local DYNAMIC_CENTER_SPAWNCOUNTMATCHPVPERS = true -- pvp center gets bigger when more people are pvping
local DYNAMIC_CENTER_IMPERFECT = true -- throw a bit of randomness in, makes pvp less stiff.
CFCRandomSpawn.recentCombatants = CFCRandomSpawn.recentCombatants or {}
local recentCombatants = CFCRandomSpawn.recentCombatants
local noZones = false
local fallbackZoneID = nil
local spawnTraceResult = {}
local spawnTraceRequest = {
mins = Vector( -16, -16, 0 ),
maxs = Vector( 16, 16, 72 ),
mask = MASK_PLAYERSOLID,
collisiongroup = COLLISION_GROUP_PLAYER,
ignoreworld = true, -- In case some spawns are clipped slightly but can be escaped with crouch. They still should get fixed up, though!
output = spawnTraceResult,
}
local CurTime = CurTime
local Vector = Vector
local function getZoneForPos( pos )
if noZones then return NO_ZONE_ID end
for zoneID, zone in ipairs( zonesForMap ) do
if pos:WithinAABox( zone.cornerA, zone.cornerB ) then
return zoneID
end
end
return fallbackZoneID
end
local function loadZones()
noZones = table.IsEmpty( zonesForMap )
if noZones then
fallbackZoneID = NO_ZONE_ID
for _, spawn in ipairs( customSpawnsForMap ) do
spawn.zoneID = fallbackZoneID
end
return
end
fallbackZoneID = #zonesForMap + 1
for _, spawn in ipairs( customSpawnsForMap ) do
spawn.zoneID = getZoneForPos( spawn.spawnPos )
end
end
loadZones()
local activePvpCenter = CFCRandomSpawn.activePvpCenter
function CFCRandomSpawn.refreshMapInfo()
activePvpCenter = CFCRandomSpawn.activePvpCenter
customSpawnConfigForMap = CFCRandomSpawn.Config.CUSTOM_SPAWNS[game.GetMap()]
mapHasCustomSpawns = next( customSpawnConfigForMap )
if not mapHasCustomSpawns then return end
customSpawnsForMap = customSpawnConfigForMap.spawnpoints or {}
DEFAULT_CENTER_RADIUS = CFCRandomSpawn.Config.DEFAULT_CENTER_RADIUS
DEFAULT_CENTER_RADIUS_SQR = DEFAULT_CENTER_RADIUS ^ 2
CENTER_RADIUS = DEFAULT_CENTER_RADIUS
CENTER_RADIUS_SQR = CENTER_RADIUS ^ 2
SELECTION_SIZE = CFCRandomSpawn.Config.SELECTION_SIZE
CLOSENESS_LIMIT = CFCRandomSpawn.Config.CLOSENESS_LIMIT ^ 2
CENTER_UPDATE_INTERVAL = customSpawnConfigForMap.centerUpdateInterval or CFCRandomSpawn.Config.CENTER_UPDATE_INTERVAL
IGNORE_BUILDERS = CFCRandomSpawn.Config.IGNORE_BUILDERS
loadZones()
end
local function getPvpers()
local pvpers = {}
for _, ply in pairs( player.GetAll() ) do
if not ply.IsInPvp or ply:IsInPvp() then
table.insert( pvpers, ply )
end
end
return pvpers
end
local function getMeasurablePlayers()
local measurablePlayers = {}
local humans = IGNORE_BUILDERS and getPvpers() or player.GetAll()
local cur = CurTime()
for _, ply in pairs( humans ) do
if ply:Alive() and recentCombatants[ply] > cur then
table.insert( measurablePlayers, ply )
end
end
-- Retry without the combat check if no one's been active
if #measurablePlayers == 0 then
for _, ply in pairs( humans ) do
if ply:Alive() then
table.insert( measurablePlayers, ply )
end
end
end
return measurablePlayers
end
local function countAsCombatting( ply )
local cur = CurTime()
recentCombatants[ply] = cur + ACTIVE_PLAYER_TIMEOUT
end
hook.Add( "PlayerInitialSpawn", "cfc_randomspawn_firstspawn", function( ply )
recentCombatants[ply] = 0
end )
hook.Add( "PlayerDeath", "cfc_randomspawn_trackrecentcombatants", function( died, _inflictor, attacker )
if not attacker:IsPlayer() then return end
countAsCombatting( died )
countAsCombatting( attacker )
end )
hook.Add( "PlayerDisconnected", "cfc_randomspawn_cleanuprecentcombatants", function( ply )
recentCombatants[ply] = nil
end )
local function getLivingPlayers()
local livingPlayers = {}
local humans = player.GetAll()
for _, ply in pairs( humans ) do
if ply:Alive() then
table.insert( livingPlayers, ply )
end
end
return livingPlayers
end
local function isInZone( pos, zoneID )
if noZones or zoneID == fallbackZoneID then return true end
local zone = zonesForMap[zoneID]
return pos:WithinAABox( zone.cornerA, zone.cornerB )
end
local function getNearestSpawn( nearPos, spawns )
local nearestSpawn
local closestDistSqr = math.huge
for _, spawn in ipairs( spawns ) do
local distToNearSqr = spawn.spawnPos:DistToSqr( nearPos )
if distToNearSqr < closestDistSqr and isInZone( nearPos, spawn.zoneID ) then
closestDistSqr = distToNearSqr
nearestSpawn = spawn
end
end
return nearestSpawn
end
local function spawnsSortedByDistTo( nearPos, spawns, radiusSqr, zoneID )
local sortedSpawnsAndDistances = {}
for _, spawn in ipairs( spawns ) do
if zoneID == nil or spawn.zoneID == zoneID then
local dist = nearPos:DistToSqr( spawn.spawnPos )
if dist < radiusSqr then
table.insert( sortedSpawnsAndDistances, { spawn = spawn, dist = dist } )
end
end
end
table.sort( sortedSpawnsAndDistances, function( a, b ) return a.dist < b.dist end )
return sortedSpawnsAndDistances
end
local function getSpawnsInZone( spawns, zoneID )
local filteredSpawns = {}
for _, spawn in ipairs( spawns ) do
if spawn.zoneID == zoneID then
table.insert( filteredSpawns, spawn )
end
end
return filteredSpawns
end
-- Get the first SELECTION_SIZE spawns that are closest to nearPos and are within range of CENTER_RADIUS_SQR
local cachedSpawnsInsideCenter = nil
local nextSpawnsInsideCenterCache = 0
local function spawnsInsidePvpCenterCached( spawns )
if cachedSpawnsInsideCenter and nextSpawnsInsideCenterCache > CurTime() then return cachedSpawnsInsideCenter end
nextSpawnsInsideCenterCache = CurTime() + 7.5
local center = CFCRandomSpawn.activePvpCenter
local sortedSpawns = spawnsSortedByDistTo( center.centerPos, spawns, CENTER_RADIUS_SQR, center.zoneID )
cachedSpawnsInsideCenter = {}
for i = 1, SELECTION_SIZE do
if sortedSpawns[i] then
table.insert( cachedSpawnsInsideCenter, sortedSpawns[i].spawn )
end
end
-- If there are no good near points, try all spawns in the zone.
if #cachedSpawnsInsideCenter == 0 then
cachedSpawnsInsideCenter = getSpawnsInZone( cachedSpawnsInsideCenter, center.zoneID )
end
-- If still empty, use all spawns.
if #cachedSpawnsInsideCenter == 0 then
cachedSpawnsInsideCenter = spawns
end
return cachedSpawnsInsideCenter
end
local function spawnIsFree( spawnPos, playerPositions )
for _, playerPos in ipairs( playerPositions ) do
if playerPos:DistToSqr( spawnPos ) < CLOSENESS_LIMIT then
return
end
end
spawnTraceRequest.start = spawnPos
spawnTraceRequest.endpos = spawnPos
util.TraceHull( spawnTraceRequest )
if spawnTraceResult.Hit then return false end
return true
end
local function findFreeSpawnPoint( spawns, plys )
local playerPositions = {}
for _, ply in ipairs( plys ) do
table.insert( playerPositions, ply:GetPos() )
end
local function find( spawnsCopy )
for _ = 1, #spawnsCopy do
local randomIndex = math.random( 1, #spawnsCopy )
local spawn = spawnsCopy[randomIndex]
local spawnPos = spawn.spawnPos
local isFree = spawnIsFree( spawnPos, playerPositions )
if isFree then
-- this spawn is good!
return spawn
else
-- remove this spawn!
spawnsCopy[randomIndex] = spawnsCopy[#spawnsCopy]
spawnsCopy[#spawnsCopy] = nil
end
end
end
-- Try given spawn list (which is limited by zone and pvp center)
local spawn = find( table.Copy( spawns ) )
if spawn then return spawn end
-- Try all spawns with the same zoneID as the first spawn in the list (tldr remove the pvp center restriction)
if #spawns ~= 0 then
spawn = find( getSpawnsInZone( spawns, spawns[1].zoneID ) )
if spawn then return spawn end
end
return customSpawnsForMap[math.random( 1, #customSpawnsForMap )] -- If all spawnpoints are full, just return a random spawn anywhere in the map. Rare case.
end
local function getPlyAvg( plys, fallbackPos )
if not plys or not plys[1] then return fallbackPos or Vector() end
local avgSum = Vector( 0, 0, 0 )
for _, ply in ipairs( plys ) do
avgSum = avgSum + ply:GetPos()
end
return avgSum / #plys
end
local function getDynamicPvpCenter( measurablePlayers )
local playersAveragePos = getPlyAvg( measurablePlayers, DYNAMIC_CENTER_FALLBACK )
local measurablePlysCount = #measurablePlayers
-- add some randomness
if DYNAMIC_CENTER_IMPERFECT then
local offset = VectorRand() * 1000
offset.z = 0
playersAveragePos = playersAveragePos + offset
end
local closestSpawnToAverage = getNearestSpawn( playersAveragePos, customSpawnsForMap )
if not closestSpawnToAverage then -- stupid fallback
closestSpawnToAverage = customSpawnsForMap[math.random( 1, #customSpawnsForMap )]
end
local fauxPvpcenterPos = closestSpawnToAverage.spawnPos
local zoneID = closestSpawnToAverage.zoneID
-- use nearest spawnpoint as a sanity point
local dynamicPvpCenter = {}
dynamicPvpCenter.centerPos = fauxPvpcenterPos
dynamicPvpCenter.zoneID = zoneID
-- sorted spawns, for center's size stuff, this handles max size for us
local spawnsSortedToClosest = spawnsSortedByDistTo( fauxPvpcenterPos, customSpawnsForMap, DYNAMIC_CENTER_MAXSIZE^2, zoneID )
-- allow this to adapt to pvper count
local minSpawns = DYNAMIC_CENTER_MINSPAWNS
if DYNAMIC_CENTER_SPAWNCOUNTMATCHPVPERS then
minSpawns = math.Clamp( DYNAMIC_CENTER_MINSPAWNS + measurablePlysCount, DYNAMIC_CENTER_MINSPAWNS, DYNAMIC_CENTER_MAXSPAWNS )
end
-- now determine the center's size
local minSizeSqr = DYNAMIC_CENTER_MINSIZE^2
local dynamicCenterSizeSqr = minSizeSqr
local spawnCount = 0
for _, spawnAndDist in ipairs( spawnsSortedToClosest ) do
spawnCount = spawnCount + 1
if spawnCount > DYNAMIC_CENTER_MAXSPAWNS then
break
end
local currDistSqr = spawnAndDist.spawn.spawnPos:DistToSqr( fauxPvpcenterPos )
if currDistSqr > dynamicCenterSizeSqr then
dynamicCenterSizeSqr = currDistSqr
end
local bigEnough = currDistSqr > minSizeSqr
local enoughInside = spawnCount >= minSpawns
if bigEnough and enoughInside then
break
end
end
dynamicPvpCenter.radius = math.sqrt( dynamicCenterSizeSqr )
dynamicPvpCenter.radiusSqr = dynamicCenterSizeSqr
return dynamicPvpCenter
end
local function updateActivePvpCenter( measurablePlayers )
activePvpCenter = getDynamicPvpCenter( measurablePlayers )
CFCRandomSpawn.activePvpCenter = activePvpCenter
CENTER_RADIUS = activePvpCenter.radius or DEFAULT_CENTER_RADIUS
CENTER_RADIUS_SQR = activePvpCenter.radiusSqr or DEFAULT_CENTER_RADIUS_SQR
-- Network to editors
local editors = CFCRandomSpawn.EditingPlayers
if not next( editors ) then return end
-- Validate
for i = #editors, 1, -1 do
if not IsValid( editors[i] ) then
table.remove( editors, i )
end
end
if not next( editors ) then return end
CFCRandomSpawn.syncActivePvpCenter()
end
function CFCRandomSpawn.getOptimalSpawnPos()
local measurablePlayers = getMeasurablePlayers()
local allLivingPlys = getLivingPlayers()
if not CFCRandomSpawn.activePvpCenter then
updateActivePvpCenter( measurablePlayers )
end
local spawnsInsideCenter = spawnsInsidePvpCenterCached( customSpawnsForMap )
local randomFreeSpawn = findFreeSpawnPoint( spawnsInsideCenter, allLivingPlys )
return randomFreeSpawn.spawnPos, randomFreeSpawn.spawnAngle
end
function CFCRandomSpawn.handlePlayerSpawn( ply )
if not mapHasCustomSpawns then return end
if not ( ply and IsValid( ply ) ) then return end
-- TODO: make this a hook
if IsValid( ply.LinkedSpawnPoint ) then return end
local optimalSpawnPosition, optimalSpawnAngles = CFCRandomSpawn.getOptimalSpawnPos()
ply:SetPos( optimalSpawnPosition )
if optimalSpawnAngles then
ply:SetEyeAngles( optimalSpawnAngles )
end
end
hook.Add( "PlayerSpawn", "CFC_RandomSpawn_ChooseOptimalSpawnpoint", CFCRandomSpawn.handlePlayerSpawn )
timer.Create( "CFC_RandomSpawn_CalculateActivePvpCenter", CENTER_UPDATE_INTERVAL, 0, function()
if not mapHasCustomSpawns then return end
local measurablePlayers = getMeasurablePlayers()
updateActivePvpCenter( measurablePlayers )
end )
concommand.Add( "cfc_spawneditor_pvpcenter_update_interval", function( ply, _, args )
if IsValid( ply ) and not ply:IsAdmin() then return end
if #args == 0 then return end
timer.Adjust( "CFC_RandomSpawn_CalculateActivePvpCenter", tonumber( args[1] ), 0 )
end, nil, "Sets the update interval for pvp centers. Lasts until the next map change." )