Fixed rare errors.

Consider possibility of nil or NULL being a value in the tables returned
from util.FindInSphere, util.FindInBox, and ents.GetAll.
This commit is contained in:
JetBoom 2014-11-19 09:07:50 -05:00
parent 15d3347371
commit b9e182385f
15 changed files with 49 additions and 31 deletions

View file

@ -23,7 +23,7 @@ function ENT:SetProp(ent)
local teamid = self:GetTeam()
local inrad = false
for _, pl in pairs(ents.FindInSphere(ent:LocalToWorld(ent:OBBCenter()), ent:BoundingRadius() / 2 + self:GetExtraRadius())) do
if pl:IsValid() and pl:IsPlayer() and pl:Alive() and (teamid == 0 or pl:Team() == teamid) then
if pl and pl:IsValid() and pl:IsPlayer() and pl:Alive() and (teamid == 0 or pl:Team() == teamid) then
inrad = true
break
end
@ -65,7 +65,7 @@ function ENT:Think()
local center = ent:LocalToWorld(ent:OBBCenter())
for _, pl in pairs(ents.FindInSphere(center, ent:BoundingRadius() / 2 + self:GetExtraRadius())) do
if pl:IsValid() and pl:IsPlayer() and pl:Alive() and (teamid == 0 or pl:Team() == teamid) then
if pl and pl:IsValid() and pl:IsPlayer() and pl:Alive() and (teamid == 0 or pl:Team() == teamid) then
pushout = true
if timeout then

View file

@ -13,7 +13,7 @@ end
function ENT:IsInsideProp()
for _, ent in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if ent ~= self and ent:IsValid() and ent:GetMoveType() == MOVETYPE_VPHYSICS and ent:GetSolid() > 0 then return true end
if ent and ent ~= self and ent:IsValid() and ent:GetMoveType() == MOVETYPE_VPHYSICS and ent:GetSolid() > 0 then return true end
end
return false
@ -41,7 +41,7 @@ function ENT:RecalculateValidity()
valid = false
else
for _, ent in pairs(ents.FindInSphere(tr.HitPos, 128)) do
if ent:IsValid() and ent:GetClass() == "prop_arsenalcrate" then
if ent and ent:IsValid() and ent:GetClass() == "prop_arsenalcrate" then
valid = false
break
end

View file

@ -19,7 +19,7 @@ end
function ENT:IsInsideProp()
for _, ent in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if ent ~= self and ent:IsValid() and ent:GetMoveType() == MOVETYPE_VPHYSICS and ent:GetSolid() > 0 then return true end
if ent and ent ~= self and ent:IsValid() and ent:GetMoveType() == MOVETYPE_VPHYSICS and ent:GetSolid() > 0 then return true end
end
return false
@ -56,7 +56,7 @@ function ENT:RecalculateValidity()
valid = false
elseif self.GhostDistance then
for _, ent in pairs(ents.FindInSphere(tr.HitPos, self.GhostDistance)) do
if ent:IsValid() and ent:GetClass() == self.GhostEntity then
if ent and ent:IsValid() and ent:GetClass() == self.GhostEntity then
valid = false
break
end

View file

@ -14,7 +14,7 @@ end
function ENT:IsInsideProp()
for _, ent in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if ent ~= self and ent:IsValid() and ent:GetMoveType() == MOVETYPE_VPHYSICS and ent:GetSolid() > 0 then return true end
if ent and ent ~= self and ent:IsValid() and ent:GetMoveType() == MOVETYPE_VPHYSICS and ent:GetSolid() > 0 then return true end
end
return false
@ -37,7 +37,7 @@ function ENT:RecalculateValidity()
valid = false
else
for _, ent in pairs(ents.FindInSphere(tr.HitPos, 48)) do
if ent:IsValid() and ent:GetClass() == "prop_messagebeacon" then
if ent and ent:IsValid() and ent:GetClass() == "prop_messagebeacon" then
valid = false
break
end

View file

@ -37,7 +37,7 @@ function ENT:AcceptInput(name, activator, caller, arg)
local vPos = self:GetPos()
for _, ent in pairs(ents.FindInSphere(vPos, self:GetRadius())) do
if ent:IsPlayer() and ent:Alive() and WorldVisible(vPos, ent:NearestPoint(vPos)) then
if ent and ent:IsValid() and ent:IsPlayer() and ent:Alive() and WorldVisible(vPos, ent:NearestPoint(vPos)) then
if ent:Team() == TEAM_UNDEAD then
--[[if ent:Health() < ent:GetMaxHealth() and not ent:GetZombieClassTable().Boss then
ent:SetHealth(math.min(ent:GetMaxZombieHealth(), ent:Health() + self.Heal))

View file

@ -15,7 +15,7 @@ function SWEP:Think()
local origin = self.Owner:LocalToWorld(self.Owner:OBBCenter())
for _, ent in pairs(ents.FindInSphere(origin, 40)) do
if ent:IsPlayer() and ent:Team() ~= TEAM_UNDEAD and ent:Alive() and TrueVisible(origin, ent:NearestPoint(origin)) then
if ent and ent:IsValid() and ent:IsPlayer() and ent:Team() ~= TEAM_UNDEAD and ent:Alive() and TrueVisible(origin, ent:NearestPoint(origin)) then
ent:PoisonDamage(1, self.Owner, self)
end
end

View file

@ -93,7 +93,7 @@ function SWEP:DoAlert()
local mouthpos = owner:EyePos() + owner:GetUp() * -3
local screampos = mouthpos + owner:GetAimVector() * 16
for _, ent in pairs(ents.FindInSphere(screampos, 92)) do
if ent:IsPlayer() and ent:Team() ~= owner:Team() then
if ent and ent:IsValid() and ent:IsPlayer() and ent:Team() ~= owner:Team() then
local entearpos = ent:EyePos()
local dist = screampos:Distance(entearpos)
if dist <= 92 and TrueVisible(entearpos, screampos) then

View file

@ -787,7 +787,7 @@ function GM:PlayerSelectSpawn(pl)
local blocked
local spawnpos = spawn:GetPos()
for _, ent in pairs(ents.FindInBox(spawnpos + playermins, spawnpos + playermaxs)) do
if ent:IsPlayer() and not spawninplayer or string.sub(ent:GetClass(), 1, 5) == "prop_" then
if ent and ent:IsValid() and ent:IsPlayer() and not spawninplayer or string.sub(ent:GetClass(), 1, 5) == "prop_" then
blocked = true
break
end
@ -1737,7 +1737,7 @@ end
function GM:EvaluatePropFreeze(ent, neighbors)
if not ent then
for _, e in pairs(ents.GetAll()) do
if e:IsValid() then
if e and e:IsValid() then
self:EvaluatePropFreeze(e)
end
end

View file

@ -153,7 +153,7 @@ end
local function barricadetimer(self, timername)
if self:IsValid() then
for _, e in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if e:IsPlayer() and e:Alive() then
if e and e:IsValid() and e:IsPlayer() and e:Alive() then
return
end
end
@ -169,7 +169,7 @@ function meta:TemporaryBarricadeObject()
if self.IsBarricadeObject then return end
for _, e in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if e:IsPlayer() and e:Alive() then
if e and e:IsValid() and e:IsPlayer() and e:Alive() then
self.IsBarricadeObject = true
self:CollisionRulesChanged()

View file

@ -367,7 +367,7 @@ end
local function GetNailOwner(nail, filter)
for _, ent in pairs(ents.GetAll()) do
if ent ~= filter and ent.Nails then
if ent and ent ~= filter and ent.Nails and ent:isValid() then
for __, n in pairs(ent.Nails) do
if n == nail then
return ent

View file

@ -444,7 +444,7 @@ end
local function nocollidetimer(self, timername)
if self:IsValid() then
for _, e in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if e:IsPlayer() and e ~= self and GAMEMODE:ShouldCollide(self, e) then
if e and e:IsValid() and e:IsPlayer() and e ~= self and GAMEMODE:ShouldCollide(self, e) then
return
end
end
@ -459,7 +459,7 @@ function meta:TemporaryNoCollide(force)
if self:GetCollisionGroup() ~= COLLISION_GROUP_PLAYER and not force then return end
for _, e in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if e:IsPlayer() and e ~= self and GAMEMODE:ShouldCollide(self, e) then
if e and e:IsValid() and e:IsPlayer() and e ~= self and GAMEMODE:ShouldCollide(self, e) then
self:SetCollisionGroup(COLLISION_GROUP_DEBRIS_TRIGGER)
local timername = "TemporaryNoCollide"..self:UniqueID()
@ -547,7 +547,7 @@ function meta:ActiveBarricadeGhosting(override)
if self:Team() ~= TEAM_HUMAN and not override or not self:GetBarricadeGhosting() then return false end
for _, ent in pairs(ents.FindInBox(self:WorldSpaceAABB())) do
if self:ShouldBarricadeGhostWith(ent) then return true end
if ent and ent:IsValid() and self:ShouldBarricadeGhostWith(ent) then return true end
end
return false

View file

@ -182,7 +182,7 @@ function GM:CanCraft(pl, craft)
if not pl:IsValid() or not pl:Alive() or pl:Team() ~= TEAM_HUMAN then return false end
local plpos = pl:EyePos()
local entities = ents.FindInSphere(plpos, self.CraftingRange)
local entities = ents.FindValidInSphere(plpos, self.CraftingRange)
if craft.CanCraft and not craft:CanCraft(pl, entities, plpos) then return false end

View file

@ -118,26 +118,42 @@ end
function util.BlastDamageEx(inflictor, attacker, epicenter, radius, damage, damagetype)
local filter = inflictor
for _, ent in pairs(ents.FindInSphere(epicenter, radius)) do
if ent and ent:IsValid() then
local nearest = ent:NearestPoint(epicenter)
if TrueVisibleFilters(epicenter, nearest, inflictor, ent) then
ent:TakeSpecialDamage(((radius - nearest:Distance(epicenter)) / radius) * damage, damagetype, attacker, inflictor, nearest)
end
end
end
end
function util.BlastDamage2(inflictor, attacker, epicenter, radius, damage)
util.BlastDamageEx(inflictor, attacker, epicenter, radius, damage, DMG_BLAST)
end
function util.FindValidInSphere(pos, radius)
local ret = {}
for _, ent in pairs(util.FindInSphere(pos, radius)) do
if ent and ent:IsValid() then
ret[#ret + 1] = ent
end
end
return ret
end
function util.PoisonBlastDamage(inflictor, attacker, epicenter, radius, damage, noreduce)
local filter = inflictor
for _, ent in pairs(ents.FindInSphere(epicenter, radius)) do
if ent and ent:IsValid() then
local nearest = ent:NearestPoint(epicenter)
if TrueVisibleFilters(epicenter, nearest, inflictor, ent) then
ent:PoisonDamage(((radius - nearest:Distance(epicenter)) / radius) * damage, attacker, inflictor, nil, noreduce)
end
end
end
end
function util.ToMinutesSeconds(seconds)
local minutes = math.floor(seconds / 60)

View file

@ -128,12 +128,14 @@ function GM:ProfilerPlayerValid(pl)
-- Are they near a trigger hurt?
for _, ent in pairs(ents.FindInSphere(plcenter, 256)) do
if ent and ent:IsValid() then
local entclass = ent:GetClass()
if entclass == "trigger_hurt" then
--print('trigger hurt')
return false
end
end
end
-- What about zombie spawns?
for _, ent in pairs(team.GetValidSpawnPoint(TEAM_UNDEAD)) do

View file

@ -37,7 +37,7 @@ hook.Add("InitPostEntityMap", "zombieescape", function(fromze)
end
for _, ent in pairs(ents.GetAll()) do
if ent.ZEDelete then
if ent and ent.ZEDelete and ent:IsValid() then
ent:Remove()
end
end