Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lua/entities/starfall_cnextbot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function ENT:Initialize()
ent_tbl.RUNACT = ACT_RUN
ent_tbl.IDLEACT = ACT_IDLE
ent_tbl.MoveSpeed = 200

ent_tbl.DeathCallbacks = SF.HookTable()
ent_tbl.InjuredCallbacks = SF.HookTable()
ent_tbl.LandCallbacks = SF.HookTable()
Expand All @@ -35,6 +35,9 @@ function ENT:Initialize()
ent_tbl.NavChangeCallbacks = SF.HookTable()
ent_tbl.ContactCallbacks = SF.HookTable()
ent_tbl.ReachCallbacks = SF.HookTable()
ent_tbl.RagCreationCallbacks = SF.HookTable()

ent_tbl.InstanceRagdollCreationCallback = function() end
end

local function addPerf(instance, startPerfTime)
Expand Down Expand Up @@ -136,7 +139,16 @@ function ENT:OnKilled(dmginfo)
inst.Types.Vector.Wrap(dmginfo:GetDamageForce()),
dmginfo:GetDamageType())
end
if ent_tbl.RagdollOnDeath then self:BecomeRagdoll(dmginfo) end
if ent_tbl.RagdollOnDeath then
local CreatedRagdoll = self:BecomeRagdoll(dmginfo)
ent_tbl.InstanceRagdollCreationCallback(CreatedRagdoll) -- this is used for ragdoll cleanup. check nextbot.lua at line 128

if not ent_tbl.RagCreationCallbacks:isEmpty() then
local inst = ent_tbl.instance
ent_tbl.RagCreationCallbacks:run(inst,
inst.WrapObject(CreatedRagdoll))
end
end
end

function ENT:OnLandOnGround(groundent)
Expand Down Expand Up @@ -173,4 +185,4 @@ function ENT:OnContact(colent)
if ent_tbl.ContactCallbacks:isEmpty() then return end
local inst = ent_tbl.instance
ent_tbl.ContactCallbacks:run(inst, inst.WrapObject(colent))
end
end
32 changes: 32 additions & 0 deletions lua/starfall/libs_sv/nextbot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ registerprivilege("nextbot.addReachCallback", "Add nextbot approach callback", "
registerprivilege("nextbot.removeReachCallback", "Remove nextbot approach callback", "Allows the user to remove an approach callback function from the nextbot.", {entities = {}})
registerprivilege("nextbot.addDeathCallback", "Add nextbot death callback", "Allows the user to add a callback function to run when the nextbot dies.", {entities = {}})
registerprivilege("nextbot.removeDeathCallback", "Remove nextbot death callback", "Allows the user to remove a death callback function from the nextbot.", {entities = {}})
registerprivilege("nextbot.addRagdollCreationCallback", "Add nextbot ragdoll creation callback", "Allows the user to add a callback function to run when the nextbot create a ragdoll.", {entities = {}})
registerprivilege("nextbot.removeRagdollCreationCallback", "Remove nextbot ragdoll creation callback", "Allows the user to remove a ragdoll creation function from the nextbot.", {entities = {}})
registerprivilege("nextbot.addInjuredCallback", "Add nextbot injured callback", "Allows the user to add a callback function to run when the nextbot is injured.", {entities = {}})
registerprivilege("nextbot.removeInjuredCallback", "Remove nextbot injured callback", "Allows the user to remove an on injured callback function from the nextbot.", {entities = {}})
registerprivilege("nextbot.addLandCallback", "Add nextbot land callback", "Allows the user to add a callback function to run when the nextbot lands on the ground.", {entities = {}})
Expand Down Expand Up @@ -62,6 +64,8 @@ registerprivilege("nextbot.setAvoidAllowed", "Nextbot allow avoid", "Allows the
registerprivilege("nextbot.setJumpGapsAllowed", "Nextbot allow jump gaps", "Allows the user to set whether the nextbot can jump gaps.", {entities = {}})

local entList = SF.EntManager("nextbots", "nextbots", 30, "The number of props allowed to spawn via Starfall")
local ragdollsList = SF.EntManager("nextbots_ragdolls", "nextbots_ragdolls", -1, "Auto cleanup ragdoll on deinitialize")


return function(instance)
local checkpermission = instance.player ~= SF.Superuser and SF.Permissions.check or function() end
Expand All @@ -81,6 +85,7 @@ end)

instance:AddHook("deinitialize", function()
entList:deinitialize(instance, true)
ragdollsList:deinitialize(instance, true)
end)

--- Creates a customizable NextBot
Expand Down Expand Up @@ -115,7 +120,12 @@ function nextbot_library.create(pos, mdl)
if Ent_IsValid(nb) then nb:Remove() end
SF.Throw("Failed to create entity (" .. tostring(err) .. ")", 2)
end

entList:register(instance, nb)

-- sorry i can't find a better way :)
local ent_tbl = Ent_GetTable(nb)
ent_tbl.InstanceRagdollCreationCallback = function(ragdoll) ragdollsList:register(instance, ragdoll) end
instance:checkCpu()

return nbwrap(nb)
Expand Down Expand Up @@ -468,6 +478,28 @@ function nb_methods:removeContactCallback(id)
nb.ContactCallbacks:remove(id)
end

--- Adds a callback function that will be run when the nextbot create a ragdoll. Note: this will be called only if nb:ragdollOnDeath() is set to True
-- @server
-- @param string callbackid The unique ID this callback will use.
-- @param function callback The function to run when the NB create a ragdoll. The arguments are: (The ragdoll entity the NB created.)
function nb_methods:addRagdollCreationCallback(id, func)
checkluatype(id, TYPE_STRING)
checkluatype(func, TYPE_FUNCTION)
local nb = nbunwrap(self)
checkpermission(instance, nb, "nextbot.addRagdollCreationCallback")
nb.RagCreationCallbacks:add(id, func)
end

--- Removes the ragdoll creation callback function from the NextBot if present.
-- @server
-- @param string callbackid The unique ID of the callback to remove.
function nb_methods:removeRagdollCreationCallback(id)
checkluatype(id, TYPE_STRING)
local nb = nbunwrap(self)
checkpermission(instance, nb, "nextbot.removeRagdollCreationCallback")
nb.RagCreationCallbacks:remove(id)
end

--- Enable or disable ragdolling on death for the NextBot.
-- @server
-- @param boolean ragdollondeath Whether the nextbot should ragdoll on death.
Expand Down
Loading