284 lines
11 KiB
Lua
284 lines
11 KiB
Lua
if not SERVER then return end
|
|
|
|
local scannerClass = "feedc0de_friendly_scanner"
|
|
local managedTag = "CustomCoopGamemodeManaged"
|
|
local reanchorTimer = "Feedc0deCoopFriendlyScannerInitialAnchor"
|
|
local respawnTimer = "Feedc0deCoopFriendlyScannerRespawn"
|
|
local managedScanner
|
|
local anchorFinalized = false
|
|
local hoverHeight = 97
|
|
|
|
local preferredSpawnClasses = {
|
|
info_player_deathmatch = 1,
|
|
info_player_coop = 1,
|
|
info_coop_spawn = 1,
|
|
gmod_player_start = 1,
|
|
info_player_rebel = 2,
|
|
info_player_start = 3,
|
|
info_player_combine = 4
|
|
}
|
|
|
|
for _, className in ipairs(scripted_ents.GetMember("gmod_player_start", "SpawnPointClasses") or {}) do
|
|
preferredSpawnClasses[className] = preferredSpawnClasses[className] or 2
|
|
end
|
|
|
|
local function FindManagedScanner()
|
|
if IsValid(managedScanner) then return managedScanner end
|
|
for _, candidate in ipairs(ents.FindByClass(scannerClass)) do
|
|
if candidate:GetNW2Bool(managedTag, false) then
|
|
managedScanner = candidate
|
|
return candidate
|
|
end
|
|
end
|
|
end
|
|
|
|
local function FindClearPosition(base, ignoredEntity)
|
|
local ignored = {}
|
|
if IsValid(managedScanner) then ignored[#ignored + 1] = managedScanner end
|
|
if IsValid(ignoredEntity) then ignored[#ignored + 1] = ignoredEntity end
|
|
local offsets = {
|
|
Vector(36, 0, 0),
|
|
Vector(-36, 0, 0),
|
|
Vector(0, 36, 0),
|
|
Vector(0, -36, 0),
|
|
Vector(56, 0, 0),
|
|
Vector(-56, 0, 0),
|
|
Vector(0, 56, 0),
|
|
Vector(0, -56, 0),
|
|
Vector(0, 0, 0),
|
|
Vector(88, 88, 0),
|
|
Vector(-88, -88, 0),
|
|
Vector(88, -88, 0),
|
|
Vector(-88, 88, 0)
|
|
}
|
|
for _, offset in ipairs(offsets) do
|
|
local horizontal = base + offset
|
|
local floorTrace = util.TraceLine({
|
|
start = Vector(horizontal.x, horizontal.y, base.z + 32),
|
|
endpos = Vector(horizontal.x, horizontal.y, base.z - 80),
|
|
mask = MASK_SOLID_BRUSHONLY
|
|
})
|
|
if floorTrace.Hit and not floorTrace.StartSolid and floorTrace.HitNormal.z > 0.45 then
|
|
local floorZ = floorTrace.HitPos.z
|
|
local ceilingTrace = util.TraceLine({
|
|
start = Vector(horizontal.x, horizontal.y, floorZ + 20),
|
|
endpos = Vector(horizontal.x, horizontal.y, floorZ + hoverHeight + 64),
|
|
mask = MASK_SOLID_BRUSHONLY
|
|
})
|
|
local height = hoverHeight
|
|
if ceilingTrace.Hit then height = math.min(height, ceilingTrace.HitPos.z - floorZ - 24) end
|
|
if height >= 30 then
|
|
local position = Vector(horizontal.x, horizontal.y, floorZ + height)
|
|
local clearance = util.TraceHull({
|
|
start = position,
|
|
endpos = position,
|
|
mins = Vector(-34, -34, -22),
|
|
maxs = Vector(34, 34, 22),
|
|
filter = ignored,
|
|
mask = MASK_SOLID
|
|
})
|
|
if not clearance.StartSolid and util.IsInWorld(position) then return position end
|
|
end
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function FindMapSpawnPosition()
|
|
local bestPriority
|
|
local candidates = {}
|
|
for _, candidate in ipairs(ents.GetAll()) do
|
|
local priority = preferredSpawnClasses[candidate:GetClass()]
|
|
if priority and candidate:IsInWorld() then
|
|
if candidate:HasSpawnFlags(1) then priority = 0 end
|
|
if not bestPriority or priority < bestPriority then
|
|
bestPriority = priority
|
|
candidates = { candidate }
|
|
elseif priority == bestPriority then
|
|
candidates[#candidates + 1] = candidate
|
|
end
|
|
end
|
|
end
|
|
local base = #candidates > 0 and candidates[math.random(#candidates)]:GetPos() or vector_origin
|
|
return FindClearPosition(base)
|
|
end
|
|
|
|
local function SpawnManagedScanner(position)
|
|
local enabled = GetConVar("gc_friendlyscanner_enabled")
|
|
if enabled and not enabled:GetBool() then return end
|
|
local existing = FindManagedScanner()
|
|
if IsValid(existing) then return existing end
|
|
|
|
local scanner = ents.Create(scannerClass)
|
|
if not IsValid(scanner) then
|
|
ErrorNoHalt("[feedc0de_coop] The friendly scanner addon entity is unavailable\n")
|
|
return
|
|
end
|
|
position = position or FindMapSpawnPosition()
|
|
if not isvector(position) then
|
|
ErrorNoHalt("[feedc0de_coop] No in-world clearance for the friendly scanner near a player spawn\n")
|
|
scanner:Remove()
|
|
return
|
|
end
|
|
scanner:SetPos(position)
|
|
scanner:SetAngles(Angle(0, math.random(0, 359), 0))
|
|
scanner:Spawn()
|
|
scanner:Activate()
|
|
scanner:SetPatrolAnchor(position, true)
|
|
scanner:SetNW2Bool(managedTag, true)
|
|
managedScanner = scanner
|
|
return scanner
|
|
end
|
|
|
|
local function PlayerSpawnPosition(ply)
|
|
if IsValid(ply) then
|
|
local spawnpoint = ply:GetVar("LastSpawnpoint")
|
|
if IsValid(spawnpoint) then
|
|
local position = FindClearPosition(spawnpoint:GetPos())
|
|
if isvector(position) then return position end
|
|
end
|
|
|
|
-- Teleport-based coop maps can keep their nominal Source spawn in
|
|
-- empty void. Retry against the player's actual post-teleport origin.
|
|
local position = FindClearPosition(ply:GetPos(), ply)
|
|
if isvector(position) then return position end
|
|
end
|
|
return FindMapSpawnPosition()
|
|
end
|
|
|
|
local function BeginPlayerSpawnTracking(ply)
|
|
if anchorFinalized or timer.Exists(reanchorTimer) or not IsValid(ply) then return end
|
|
|
|
local previousPosition
|
|
local stableSamples = 0
|
|
timer.Create(reanchorTimer, 0.15, 30, function()
|
|
if anchorFinalized then
|
|
timer.Remove(reanchorTimer)
|
|
return
|
|
end
|
|
if not IsValid(ply) or not ply:Alive() then
|
|
previousPosition = nil
|
|
stableSamples = 0
|
|
return
|
|
end
|
|
|
|
local currentPosition = ply:GetPos()
|
|
local candidate = FindClearPosition(currentPosition, ply)
|
|
local ordinaryMovement = isvector(previousPosition)
|
|
and currentPosition:DistToSqr(previousPosition) <= 80 * 80
|
|
if ordinaryMovement and isvector(candidate) then
|
|
stableSamples = stableSamples + 1
|
|
else
|
|
-- Teleports and invalid/void positions restart stabilization.
|
|
stableSamples = 0
|
|
end
|
|
previousPosition = currentPosition
|
|
|
|
if stableSamples < 4 then return end
|
|
local scanner = FindManagedScanner() or SpawnManagedScanner(candidate)
|
|
if not IsValid(scanner) then return end
|
|
scanner:SetPatrolAnchor(candidate, true)
|
|
anchorFinalized = true
|
|
timer.Remove(reanchorTimer)
|
|
end)
|
|
end
|
|
|
|
local function ResetManagedScanner()
|
|
timer.Remove(reanchorTimer)
|
|
timer.Remove(respawnTimer)
|
|
managedScanner = nil
|
|
anchorFinalized = false
|
|
end
|
|
|
|
hook.Add("InitPostEntity", "Feedc0deCoopFriendlyScannerSpawn", ResetManagedScanner)
|
|
hook.Add("PostCleanupMap", "Feedc0deCoopFriendlyScannerCleanup", ResetManagedScanner)
|
|
hook.Add("PlayerSpawn", "Feedc0deCoopFriendlyScannerInitialPlayerAnchor", function(ply)
|
|
if ply:IsBot() then return end
|
|
BeginPlayerSpawnTracking(ply)
|
|
end)
|
|
|
|
hook.Add("Feedc0deFriendlyScannerDestroyed", "Feedc0deCoopFriendlyScannerRespawn", function(scanner, anchor, managed)
|
|
if not managed then return end
|
|
if managedScanner == scanner then managedScanner = nil end
|
|
timer.Remove(respawnTimer)
|
|
local delay = GetConVar("gc_friendlyscanner_respawntime")
|
|
timer.Create(respawnTimer, math.max(delay and delay:GetFloat() or 5, 1), 1, function()
|
|
SpawnManagedScanner(anchor)
|
|
end)
|
|
end)
|
|
|
|
concommand.Remove("coop_friendlyscanner_here")
|
|
concommand.Add("coop_friendlyscanner_here", function(ply)
|
|
if IsValid(ply) and not ply:IsAdmin() then return end
|
|
local anchorPlayer = IsValid(ply) and ply or player.GetHumans()[1]
|
|
if not IsValid(anchorPlayer) then return end
|
|
local scanner = FindManagedScanner() or SpawnManagedScanner()
|
|
if not IsValid(scanner) then return end
|
|
local position = FindClearPosition(anchorPlayer:GetPos(), anchorPlayer)
|
|
if not isvector(position) then return end
|
|
scanner:SetPatrolAnchor(position, true)
|
|
anchorFinalized = true
|
|
end)
|
|
|
|
concommand.Remove("coop_friendlyscanner_reset_spawn")
|
|
concommand.Add("coop_friendlyscanner_reset_spawn", function(ply)
|
|
if IsValid(ply) and not ply:IsAdmin() then return end
|
|
local referencePlayer = IsValid(ply) and ply or player.GetHumans()[1]
|
|
local scanner = FindManagedScanner() or SpawnManagedScanner()
|
|
if not IsValid(scanner) then return end
|
|
local position = PlayerSpawnPosition(referencePlayer)
|
|
if not isvector(position) then return end
|
|
scanner:SetPatrolAnchor(position, true)
|
|
anchorFinalized = true
|
|
end)
|
|
|
|
concommand.Remove("coop_friendlyscanner_arm_next_spawn")
|
|
concommand.Add("coop_friendlyscanner_arm_next_spawn", function(ply)
|
|
if IsValid(ply) and not ply:IsAdmin() then return end
|
|
timer.Remove(reanchorTimer)
|
|
timer.Remove(respawnTimer)
|
|
for _, scanner in ipairs(ents.FindByClass(scannerClass)) do
|
|
if scanner:GetNW2Bool(managedTag, false) then scanner:Remove() end
|
|
end
|
|
managedScanner = nil
|
|
anchorFinalized = false
|
|
end)
|
|
|
|
concommand.Remove("coop_friendlyscanner_reload")
|
|
concommand.Add("coop_friendlyscanner_reload", function(ply)
|
|
if IsValid(ply) and not ply:IsAdmin() then return end
|
|
local scanner = FindManagedScanner()
|
|
if not IsValid(scanner) then return end
|
|
local anchor = scanner:GetSpawnAnchor()
|
|
scanner:SetNW2Bool(managedTag, false)
|
|
scanner:Remove()
|
|
managedScanner = nil
|
|
timer.Simple(0.1, function()
|
|
SpawnManagedScanner(anchor)
|
|
end)
|
|
end)
|
|
|
|
-- Remove hooks from the pre-addon implementation during live migration.
|
|
hook.Remove("Think", "CustomCoopFriendlyScannerThink")
|
|
hook.Remove("PlayerInitialSpawn", "CustomCoopFriendlyScannerPlayerRelationship")
|
|
hook.Remove("PlayerSpawn", "CustomCoopFriendlyScannerFollowActualSpawn")
|
|
hook.Remove("PhysgunPickup", "CustomCoopFriendlyScannerPhysgunPickup")
|
|
hook.Remove("PhysgunDrop", "CustomCoopFriendlyScannerPhysgunDrop")
|
|
hook.Remove("EntityTakeDamage", "CustomCoopFriendlyScannerPhysgunProtection")
|
|
hook.Remove("OnNPCKilled", "CustomCoopFriendlyScannerKilled")
|
|
timer.Remove("CustomCoopFriendlyScannerRespawn")
|
|
|
|
timer.Simple(0, function()
|
|
for _, candidate in ipairs(ents.GetAll()) do
|
|
if candidate:GetNW2Bool("CustomCoopFriendlyScanner", false) and candidate:GetClass() ~= scannerClass then
|
|
candidate:Remove()
|
|
end
|
|
end
|
|
local existing = FindManagedScanner()
|
|
if IsValid(existing) and CurTime() > 10 then
|
|
-- Hot-refreshing this file must not make the next ordinary player
|
|
-- respawn reposition an already-established scanner.
|
|
anchorFinalized = true
|
|
end
|
|
end)
|