Skip to content

Commit 7dfb693

Browse files
author
Ashley Williamson
committed
[Added] Burst/PersonalQuota/DefaultQuota for SF Holograms
1 parent 9b348ce commit 7dfb693

1 file changed

Lines changed: 92 additions & 14 deletions

File tree

lua/starfall/libs_sh/holograms.lua

Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ local holograms_library, _ = SF.Libraries.Register("holograms")
66
local hologram_methods, hologram_metamethods = SF.Typedef("Hologram", SF.Entities.Metatable)
77

88
SF.Holograms = {}
9+
SF.Holograms.defaultquota = CreateConVar("sf_holograms_defaultquota", "7200", {FCVAR_ARCHIVE,FCVAR_REPLICATED},
10+
"The default number of holograms allowed to spawn via Starfall scripts across all instances")
11+
12+
SF.Holograms.personalquota = CreateConVar("sf_holograms_personalquota", "300", {FCVAR_ARCHIVE,FCVAR_REPLICATED},
13+
"The default number of holograms allowed to spawn via Starfall scripts for a single instance")
14+
915
SF.Holograms.Methods = hologram_methods
1016
SF.Holograms.Metatable = hologram_metamethods
1117

@@ -19,11 +25,18 @@ function SF.Entities.Wrap(obj)
1925
return w
2026
end
2127

28+
local insts = {}
29+
local plyCount = {}
30+
2231
SF.Libraries.AddHook("initialize",function(inst)
2332
inst.data.holograms = {
2433
holos = {},
2534
count = 0,
35+
burst = 10
2636
}
37+
38+
insts[inst] = true
39+
plyCount[inst.player] = plyCount[inst.player] or inst.data.holograms.count
2740
end)
2841

2942
SF.Libraries.AddHook("deinitialize", function(inst)
@@ -37,7 +50,10 @@ SF.Libraries.AddHook("deinitialize", function(inst)
3750
holos[holo] = nil
3851
holo = next(holos)
3952
end
53+
plyCount[inst.player] = plyCount[inst.player] - inst.data.holograms.count
4054
inst.data.holograms.count = 0
55+
56+
insts[inst]= nil
4157
end)
4258

4359
local function hologramOnDestroy(holoent, holodata)
@@ -189,6 +205,48 @@ if SERVER then
189205
end
190206

191207
if SERVER then
208+
209+
--- Updates/Checks burst constraints
210+
-- @class function
211+
-- @param instance Instance table for the burst values related to current SF Instance / Player
212+
-- @param noupdate False if updating the burst should be done.
213+
local function can_spawn(instance, noupdate)
214+
if instance.data.holograms.burst > 0 then
215+
if not noupdate then instance.data.holograms.burst = instance.data.holograms.burst - 1 end
216+
return true
217+
else
218+
return false
219+
end
220+
end
221+
222+
--- Checks if the total number of holograms across all instances has reached the max limit.
223+
-- @class function
224+
-- @return True/False depending on if limit has been reached for SF Holograms
225+
local function max_reached()
226+
local c = 0
227+
for _, v in pairs( plyCount ) do
228+
c = c + v
229+
end
230+
if c >= GetConVar("sf_holograms_defaultquota"):GetInt() then return true else return false end
231+
end
232+
233+
--- Checks if the users personal limit of holograms has been exhausted
234+
-- @class function
235+
-- @param i Instance to use, this will relate to the player in question
236+
-- @return True/False depending on if the personal limit has been reached for SF Holograms
237+
local function personal_max_reached( i )
238+
print(plyCount[i.player])
239+
return plyCount[i.player] >= GetConVar("sf_holograms_personalquota"):GetInt()
240+
end
241+
242+
timer.Create("SF_Hologram_BurstCounter", 1/4, 0, function()
243+
for i, _ in pairs( insts ) do
244+
if i.data.holograms.burst < 10 then
245+
i.data.holograms.burst = i.data.holograms.burst + 1
246+
end
247+
end
248+
end)
249+
192250
--- Creates a hologram.
193251
-- @server
194252
-- @return The hologram object
@@ -198,22 +256,42 @@ if SERVER then
198256
SF.CheckType(model, "string")
199257
if scale then SF.CheckType(scale, "Vector") end
200258

201-
local holodata = SF.instance.data.holograms
259+
local instance = SF.instance
260+
if not can_spawn( instance ) then return error("Can't spawn holograms that often",2) end
261+
if personal_max_reached( instance ) then return error("Can't spawn holograms, maximum personal limit of "..GetConVar("sf_holograms_personalquota"):GetInt().." has been reached", 2 ) end
262+
print("Person Max: ", personal_max_reached( instance ))
263+
if max_reached() then return error("Can't spawn holograms, maximum limit of "..GetConVar("sf_holograms_defaultquota"):GetInt().." has been reached", 2) end
264+
print("Max: ",max_reached())
265+
266+
local holodata = instance.data.holograms
202267
local holoent = ents.Create("gmod_starfall_hologram")
203-
holoent:SetPos(pos)
204-
holoent:SetAngles(ang)
205-
holoent:SetModel(model)
206-
holoent:CallOnRemove("starfall_hologram_delete", hologramOnDestroy, holodata)
207-
holoent:Spawn()
208-
if scale then
209-
holoent:SetScale(scale)
210-
end
268+
if holoent and holoent:IsValid() then
269+
holoent:SetPos(pos)
270+
holoent:SetAngles(ang)
271+
holoent:SetModel(model)
272+
holoent:CallOnRemove("starfall_hologram_delete", hologramOnDestroy, holodata)
273+
holoent:Spawn()
274+
if scale then
275+
holoent:SetScale(scale)
276+
end
211277

212-
local holo = SF.Entities.Wrap(holoent)
278+
local holo = SF.Entities.Wrap(holoent)
213279

214-
holodata.holos[holo] = holo
215-
holodata.count = holodata.count + 1
216-
return holo
217-
-- TODO: Need to fire a umsg here to assign clientside ownership(?)
280+
holodata.holos[holo] = holo
281+
holodata.count = holodata.count + 1
282+
283+
plyCount[instance.player] = plyCount[instance.player] + 1
284+
return holo
285+
-- TODO: Need to fire a umsg here to assign clientside ownership(?)
286+
end
218287
end
288+
289+
--- Checks if a user can spawn anymore holograms.
290+
-- @server
291+
-- @return True if user can spawn holograms, False if not.
292+
function holograms_library.canSpawn()
293+
local instance = SF.instance
294+
return not personal_max_reached( instance ) and not max_reached() and can_spawn( instance, true )
295+
end
296+
219297
end

0 commit comments

Comments
 (0)