diff --git a/lua/starfall/libs_cl/light.lua b/lua/starfall/libs_cl/light.lua index 480a6b5f9..1f3edf219 100644 --- a/lua/starfall/libs_cl/light.lua +++ b/lua/starfall/libs_cl/light.lua @@ -87,7 +87,26 @@ SF.RegisterType("Light", true, false) return function(instance) local checkpermission = instance.player ~= SF.Superuser and SF.Permissions.check or function() end +local light_library = instance.Libraries.light +local light_methods, light_meta, wrap, unwrap = instance.Types.Light.Methods, instance.Types.Light, instance.Types.Light.Wrap, instance.Types.Light.Unwrap +local vec_meta, vwrap, vunwrap = instance.Types.Vector, instance.Types.Vector.Wrap, instance.Types.Vector.Unwrap +local col_meta, cwrap, cunwrap = instance.Types.Color, instance.Types.Color.Wrap, instance.Types.Color.Unwrap + +local numlights = 0 local lights = {} +local function registerLight(light) + lights[light] = true + gSFLights[light.slot] = light + numlights = numlights + 1 +end +local function destroyLight(light) + if lights[light] then + lights[light] = nil + gSFLights[light.slot] = nil + numlights = numlights - 1 + end +end + instance.data.light = {lights = lights} instance:AddHook("deinitialize", function() for light in pairs(lights) do @@ -95,13 +114,6 @@ instance:AddHook("deinitialize", function() end end) - -local light_library = instance.Libraries.light -local light_methods, light_meta, wrap, unwrap = instance.Types.Light.Methods, instance.Types.Light, instance.Types.Light.Wrap, instance.Types.Light.Unwrap -local vec_meta, vwrap, vunwrap = instance.Types.Vector, instance.Types.Vector.Wrap, instance.Types.Vector.Unwrap -local col_meta, cwrap, cunwrap = instance.Types.Color, instance.Types.Color.Wrap, instance.Types.Color.Unwrap - - --- Creates a dynamic light (make sure to draw it) -- @param Vector pos The position of the light -- @param number size The size of the light. Must be lower than sf_light_maxsize @@ -109,7 +121,7 @@ local col_meta, cwrap, cunwrap = instance.Types.Color, instance.Types.Color.Wrap -- @param Color color The color of the light -- @return Light Dynamic light function light_library.create(pos, size, brightness, color) - if table.Count(lights) >= 256 then SF.Throw("Too many lights have already been allocated (max 256)", 2) end + if numlights >= 256 then SF.Throw("Too many lights have already been allocated (max 256)", 2) end if maxSize:GetFloat() == 0 then SF.Throw("sf_light_maxsize is set to 0", 2) end checkpermission(instance, nil, "light.create") checkluatype(size, TYPE_NUMBER) @@ -124,9 +136,7 @@ function light_library.create(pos, size, brightness, color) dietime = 1 } - lights[light] = true - gSFLights[slot] = light - + registerLight(light) return wrap(light) end @@ -242,4 +252,12 @@ function light_methods:setColor(color) data.b = col.b end +--- Destroys the light object freeing up whatever slot it was using +function light_methods:destroy() + local light = unwrap(self) + destroyLight(light) + light_meta.sf2sensitive[self] = nil + light_meta.sensitive2sf[light] = nil +end + end diff --git a/lua/starfall/libs_cl/material.lua b/lua/starfall/libs_cl/material.lua index bd2617769..494c85bbf 100644 --- a/lua/starfall/libs_cl/material.lua +++ b/lua/starfall/libs_cl/material.lua @@ -586,9 +586,7 @@ end --- Frees a user created material allowing you to create others function material_methods:destroy() - local m = unwrap(self) - if not m then SF.Throw("The material is already destroyed?", 2) end local name = m:GetName() local rt = instance.data.render.rendertargets[name] @@ -596,11 +594,8 @@ function material_methods:destroy() instance.env.render.destroyRenderTarget(name) end - local sensitive2sf, sf2sensitive = material_meta.sensitive2sf, material_meta.sf2sensitive - sensitive2sf[m] = nil - sf2sensitive[self] = nil - dsetmeta(self, nil) - + material_meta.sf2sensitive[self] = nil + material_meta.sensitive2sf[m] = nil usermaterials[m] = nil material_bank:free(instance.player, m, m:GetShader()) end diff --git a/lua/starfall/libs_sh/sound.lua b/lua/starfall/libs_sh/sound.lua index c0de4e2a9..33b3c5b70 100644 --- a/lua/starfall/libs_sh/sound.lua +++ b/lua/starfall/libs_sh/sound.lua @@ -16,15 +16,6 @@ local soundsByEntity = SF.EntityTable("soundsByEntity", function(e, t) end end) -local function deleteSound(ply, ent, sound) - sound:Stop() - plyCount:free(ply, 1) - if soundsByEntity[ent] then - soundsByEntity[ent][sound] = nil - end -end - - --- Sound library. -- @name sound -- @class library @@ -41,23 +32,41 @@ SF.RegisterType("Sound", true, false) return function(instance) local checkpermission = instance.player ~= SF.Superuser and SF.Permissions.check or function() end +local sound_library = instance.Libraries.sound +local sound_methods, sound_meta, wrap, unwrap = instance.Types.Sound.Methods, instance.Types.Sound, instance.Types.Sound.Wrap, instance.Types.Sound.Unwrap +local ent_meta, ewrap, eunwrap = instance.Types.Entity, instance.Types.Entity.Wrap, instance.Types.Entity.Unwrap + local sounds = {} +local function registerSound(ent, sound) + local sndsByEnt = soundsByEntity[ent] + if not sndsByEnt then sndsByEnt = {} soundsByEntity[ent] = sndsByEnt end + sndsByEnt[sound] = true + sounds[sound] = ent +end + +local function destroySound(sound) + sound:Stop() + local ent = sounds[sound] + if ent then + sounds[sound] = nil + plyCount:free(instance.player, 1) + if soundsByEntity[ent] then + soundsByEntity[ent][sound] = nil + end + end +end + local getent instance:AddHook("initialize", function() getent = instance.Types.Entity.GetEntity end) instance:AddHook("deinitialize", function() - for s, ent in pairs(sounds) do - deleteSound(instance.player, ent, s) + for s in pairs(sounds) do + destroySound(s) end end) -local sound_library = instance.Libraries.sound -local sound_methods, sound_meta, wrap, unwrap = instance.Types.Sound.Methods, instance.Types.Sound, instance.Types.Sound.Wrap, instance.Types.Sound.Unwrap -local ent_meta, ewrap, eunwrap = instance.Types.Entity, instance.Types.Entity.Wrap, instance.Types.Entity.Unwrap - - --- Creates a sound and attaches it to an entity -- @param Entity ent Entity to attach sound to. -- @param string path Filepath to the sound file. @@ -82,13 +91,10 @@ function sound_library.create(ent, path, nofilter) filter = RecipientFilter() filter:AddAllPlayers() end - local soundPatch = CreateSound(e, path, filter) - local snds = soundsByEntity[e] - if not snds then snds = {} soundsByEntity[e] = snds end - snds[soundPatch] = true - sounds[soundPatch] = e - return wrap(soundPatch) + local sound = CreateSound(e, path, filter) + registerSound(e, sound) + return wrap(sound) end @@ -133,16 +139,9 @@ end --- Removes the sound from the game so new one can be created if limit is reached function sound_methods:destroy() local snd = unwrap(self) - if snd and sounds[snd] then - deleteSound(instance.player, sounds[snd], snd) - sounds[snd] = nil - local sensitive2sf, sf2sensitive = sound_meta.sensitive2sf, sound_meta.sf2sensitive - sensitive2sf[snd] = nil - sf2sensitive[self] = nil - debug.setmetatable(self, nil) - else - SF.Throw("Tried to destroy invalid sound", 2) - end + destroySound(snd) + sound_meta.sf2sensitive[self] = nil + sound_meta.sensitive2sf[snd] = nil end --- Sets the volume of the sound. Won't work unless the sound is playing. diff --git a/lua/starfall/libs_sh/trace.lua b/lua/starfall/libs_sh/trace.lua index 496da1eb6..ae4bd4472 100644 --- a/lua/starfall/libs_sh/trace.lua +++ b/lua/starfall/libs_sh/trace.lua @@ -41,7 +41,7 @@ local function convertFilter(filter) if filter == nil then return nil elseif istable(filter) then - if ent_meta.sf2sensitive[filter]==nil then + if debug.getmetatable(filter)~=ent_meta then local l = {} for i, v in ipairs(filter) do l[i] = eunwrap(v) diff --git a/lua/starfall/libs_sv/navmesh.lua b/lua/starfall/libs_sv/navmesh.lua index 55d8c87dc..31c24c036 100644 --- a/lua/starfall/libs_sv/navmesh.lua +++ b/lua/starfall/libs_sv/navmesh.lua @@ -653,10 +653,8 @@ return function(instance) function navarea_methods:remove() local nav = navunwrap(self) entList:remove(instance, nav) - - local sensitive2sf, sf2sensitive = navarea_meta.sensitive2sf, navarea_meta.sf2sensitive - sensitive2sf[nav] = nil - sf2sensitive[self] = nil + navarea_meta.sf2sensitive[self] = nil + navarea_meta.sensitive2sf[nav] = nil end --- Removes the given NavArea from the Closed List