Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPC maker not respecting spawn frequency -1 #307

Merged
merged 3 commits into from
Jul 20, 2024
Merged
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
0.9.26 (in development)
- Improved: Going in and out of the settings no longer clips into the player model.
- Fixed: NPC makers not respecting the spawn frequency -1 causing a lot of NPCs being spawned in some cases.

0.9.25
- Improved: Hints will no longer show duplicates, instead it will renew the existing hint that has the same text.
Expand Down
38 changes: 32 additions & 6 deletions entities/entities/lambda_npcmaker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,9 @@ function ENT:Initialize()
BaseClass.Initialize(self)
self:SetSolid(SOLID_NONE)
if self:GetNWVar("Disabled") == false then
self:NextThink(CurTime() + 0.1)
self.Think = self.MakerThink
self:Disable()
else
self.Think = self.StubThink
self:Enable()
end
end

Expand Down Expand Up @@ -275,7 +274,14 @@ end
function ENT:Enable()
self:SetNWVar("Disabled", false)
self.Think = self.MakerThink
self:NextThink(CurTime())
local spawnFrequency = self:GetNWVar("SpawnFrequency")
local curTime = CurTime()
if spawnFrequency ~= -1 then
self.NextSpawnTime = curTime + spawnFrequency
else
self.NextSpawnTime = curTime
end
self:NextThink(self.NextSpawnTime)
end

function ENT:Disable()
Expand Down Expand Up @@ -358,6 +364,8 @@ function ENT:CanMakeNPC(ignoreSolidEnts)
end

function ENT:StubThink()
self:NextThink(CurTime() + 1)
return true
end

--DbgPrint(self, "ENT:StubThink", ent)
Expand All @@ -369,9 +377,25 @@ function ENT:MakerThink()
self.CachedPlayerCount = player.GetCount()
end

self:NextThink(CurTime() + self:GetNWVar("SpawnFrequency"))
local curTime = CurTime()

if self.NextSpawnTime == -1 or self.NextSpawnTime > curTime then
self:NextThink(curTime + 0.1)
return true
end

-- Spawn the next NPC.
self:MakeNPC()

local spawnFrequency = self:GetNWVar("SpawnFrequency")
if spawnFrequency ~= -1 then
self.NextSpawnTime = curTime + spawnFrequency
self:NextThink(self.NextSpawnTime)
else
self.NextSpawnTime = -1
self:NextThink(curTime + 0.1)
end

return true
end

Expand All @@ -381,8 +405,10 @@ function ENT:DeathNotice(ent)
end

self:SetNWVar("LiveChildren", self:GetNWVar("LiveChildren") - 1)

if self:GetNWVar("SpawnFrequency") == -1 then
self:MakeNPC()
-- Allow it to spawn again.
self.NextSpawnTime = CurTime()
end

if self:GetNWVar("LiveChildren") <= 0 then
Expand Down