Skip to content

Commit 1856227

Browse files
More effective rack unlinking checks
This fixes being able to load any rack with any missile type and being able to load racks with ammo boxes that are too far away.
1 parent 3270845 commit 1856227

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

lua/entities/acf_rack/init.lua

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ local ACF = ACF
1111
local Classes = ACF.Classes
1212
local Utilities = ACF.Utilities
1313
local Clock = Utilities.Clock
14+
local MaxDistance = ACF.LinkDistance * ACF.LinkDistance
15+
local UnlinkSound = "physics/metal/metal_box_impact_bullet%s.wav"
1416

1517
local function UpdateTotalAmmo(Entity)
1618
local Total = 0
@@ -26,9 +28,22 @@ local function UpdateTotalAmmo(Entity)
2628
WireLib.TriggerOutput(Entity, "Total Ammo", Total)
2729
end
2830

31+
local function CheckDistantLink(Entity, Crate, EntPos)
32+
local CrateUnlinked = false
33+
34+
if EntPos:DistToSqr(Crate:GetPos()) > MaxDistance then
35+
local Sound = UnlinkSound:format(math.random(1, 3))
36+
37+
Entity:EmitSound(Sound, 70, math.random(99, 109), ACF.Volume)
38+
Crate:EmitSound(Sound, 70, math.random(99, 109), ACF.Volume)
39+
40+
CrateUnlinked = Entity:Unlink(Crate)
41+
end
42+
43+
return CrateUnlinked
44+
end
45+
2946
do -- Spawning and Updating --------------------
30-
local UnlinkSound = "physics/metal/metal_box_impact_bullet%s.wav"
31-
local MaxDistance = ACF.LinkDistance * ACF.LinkDistance
3247
local CheckLegal = ACF.CheckLegal
3348
local WireIO = Utilities.WireIO
3449
local Entities = Classes.Entities
@@ -157,21 +172,6 @@ do -- Spawning and Updating --------------------
157172
UpdateTotalAmmo(Entity)
158173
end
159174

160-
local function CheckDistantLinks(Entity, Source)
161-
local Position = Entity:GetPos()
162-
163-
for Link in pairs(Entity[Source]) do
164-
if Position:DistToSqr(Link:GetPos()) > MaxDistance then
165-
local Sound = UnlinkSound:format(math.random(1, 3))
166-
167-
Entity:EmitSound(Sound, 70, math.random(99, 109), ACF.Volume)
168-
Link:EmitSound(Sound, 70, math.random(99, 109), ACF.Volume)
169-
170-
Entity:Unlink(Link)
171-
end
172-
end
173-
end
174-
175175
hook.Add("ACF_OnSetupInputs", "ACF Rack Motor Delay", function(Entity, List, _, Rack)
176176
if Entity:GetClass() ~= "acf_rack" then return end
177177
if not Rack.CanDropMissile then return end
@@ -239,7 +239,11 @@ do -- Spawning and Updating --------------------
239239
timer.Create("ACF Rack Clock " .. Rack:EntIndex(), 3, 0, function()
240240
if not IsValid(Rack) then return end
241241

242-
CheckDistantLinks(Rack, "Crates")
242+
local Position = Rack:GetPos()
243+
244+
for Link in pairs(Rack.Crates) do
245+
CheckDistantLink(Rack, Link, Position)
246+
end
243247
end)
244248

245249
timer.Create("ACF Rack Ammo " .. Rack:EntIndex(), 1, 0, function()
@@ -285,6 +289,14 @@ do -- Spawning and Updating --------------------
285289

286290
HookRun("ACF_OnEntityUpdate", "acf_rack", self, Data, Rack)
287291

292+
local Crates = self.Crates
293+
294+
if next(Crates) then
295+
for Crate in pairs(Crates) do
296+
self:Unlink(Crate)
297+
end
298+
end
299+
288300
self:UpdateOverlay(true)
289301

290302
net.Start("ACF_UpdateEntity")
@@ -386,6 +398,7 @@ do -- Entity Link/Unlink -----------------------
386398
if Weapon.Crates[Target] then return false, "This rack is already linked to this crate." end
387399
if Target.Weapons[Weapon] then return false, "This rack is already linked to this crate." end
388400
if Target.IsRefill then return false, "Refill crates cannot be linked!" end
401+
if Target:GetPos():DistToSqr(Weapon:GetPos()) > MaxDistance then return false, "This crate is too far away from this rack." end
389402

390403
local Blacklist = Target.RoundData.Blacklist
391404

@@ -624,7 +637,7 @@ do -- Loading ----------------------------------
624637
local Index, Point = self:GetNextMountPoint("Empty")
625638
local Crate = GetNextCrate(self)
626639

627-
if not self.Firing and Index and Crate then
640+
if not self.Firing and Index and Crate and not CheckDistantLink(self, Crate, self:GetPos()) then
628641
local Missile = AddMissile(self, Point, Crate)
629642
local ReloadTime = Missile.ReloadTime
630643

0 commit comments

Comments
 (0)