zcnpci/lua/entities/npc_ragdoll_target.lua

99 lines
2.7 KiB
Lua
Raw Normal View History

2026-05-30 22:42:24 +00:00
AddCSLuaFile()
ENT.Type = "ai"
ENT.Base = "base_ai"
ENT.AutomaticFrameAdvance = true
local debug_show_ragdoll_targets = CreateConVar("zcnpci_debug_show_ragdoll_targets", 0, bit.bor(FCVAR_ARCHIVE, FCVAR_REPLICATED))
local function UpdateRelationship(ent, me)
if !IsValid(ent) or !ent:IsNPC() then return end
if ent:Disposition(me.dummy_entity) == D_HT then
2026-05-31 01:35:28 +00:00
ent:AddEntityRelationship(me, D_HT, -1)
2026-05-30 22:42:24 +00:00
end
end
local function RemoveRelationship(ent, me)
if !IsValid(ent) or !ent:IsNPC() then return end
if ent:Disposition(me.dummy_entity) == D_HT then
2026-05-31 01:35:28 +00:00
ent:AddEntityRelationship(me, D_NU, -1)
2026-05-30 22:42:24 +00:00
ent:ClearEnemyMemory(me)
2026-05-31 01:35:28 +00:00
if ent:GetEnemy() == me then
ent:SetEnemy(nil)
ent:ClearSchedule()
end
2026-05-30 22:42:24 +00:00
end
end
function ENT:Initialize()
if SERVER then
self:SetModel("models/maxofs2d/hover_basic.mdl")
self:SetCollisionGroup(COLLISION_GROUP_DEBRIS)
self:SetHullType(HULL_TINY_CENTERED)
self:SetNoDraw(!debug_show_ragdoll_targets:GetBool())
self.dummy_entity = ents.Create(self.ragdoll_to_follow.class_in_previous_life)
2026-05-31 01:35:28 +00:00
self.last_stop_targeting = nil
2026-05-30 22:42:24 +00:00
hook.Add("OnEntityCreated", "zcnpci-"..self:GetCreationID(), function(new_entity)
if !IsValid(new_entity) or !new_entity:IsNPC() then return end
if !self.last_stop_targeting then
UpdateRelationship(new_entity, self)
end
end)
end
end
function ENT:Think()
if SERVER then
local ragdoll = self.ragdoll_to_follow
if !IsValid(ragdoll) then return end
self:SetPos(ragdoll:GetPos())
self:SetNoDraw(!debug_show_ragdoll_targets:GetBool())
if !ragdoll.organism then return true end
local should_stop_targeting = (
(ragdoll.organism.consciousness <= 0.4) or
(ragdoll.organism.critical)
)
if should_stop_targeting != self.last_stop_targeting then
self.last_stop_targeting = should_stop_targeting
if should_stop_targeting then
self:SetColor(Color(255, 0, 0))
for i,ent in ipairs(ents.GetAll()) do
RemoveRelationship(ent, self)
end
else
2026-05-31 01:35:28 +00:00
self:SetColor(Color(0, 255, 0))
2026-05-30 22:42:24 +00:00
for i,ent in ipairs(ents.GetAll()) do
UpdateRelationship(ent, self)
end
end
end
self:NextThink(CurTime())
end
return true
end
function ENT:OnRemove()
if SERVER then
for i,ent in ipairs(ents.GetAll()) do
RemoveRelationship(ent, self)
end
2026-05-31 01:35:28 +00:00
self.dummy_entity:Remove()
2026-05-30 22:42:24 +00:00
hook.Remove("OnEntityCreated", "zcnpci-"..self:GetCreationID())
end
end