Skip to content

Commit 2c1fc77

Browse files
Merge branch 'master' into dev
2 parents 8a84150 + 1856227 commit 2c1fc77

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
@@ -13,6 +13,8 @@ local Classes = ACF.Classes
1313
local Utilities = ACF.Utilities
1414
local Clock = Utilities.Clock
1515
local Sounds = Utilities.Sounds
16+
local MaxDistance = ACF.LinkDistance * ACF.LinkDistance
17+
local UnlinkSound = "physics/metal/metal_box_impact_bullet%s.wav"
1618

1719
local function UpdateTotalAmmo(Entity)
1820
local Total = 0
@@ -28,9 +30,22 @@ local function UpdateTotalAmmo(Entity)
2830
WireLib.TriggerOutput(Entity, "Total Ammo", Total)
2931
end
3032

33+
local function CheckDistantLink(Entity, Crate, EntPos)
34+
local CrateUnlinked = false
35+
36+
if EntPos:DistToSqr(Crate:GetPos()) > MaxDistance then
37+
local Sound = UnlinkSound:format(math.random(1, 3))
38+
39+
Sounds.SendSound(Entity, Sound, 70, math.random(99, 109), 1)
40+
Sounds.SendSound(Crate, Sound, 70, math.random(99, 109), 1)
41+
42+
CrateUnlinked = Entity:Unlink(Crate)
43+
end
44+
45+
return CrateUnlinked
46+
end
47+
3148
do -- Spawning and Updating --------------------
32-
local UnlinkSound = "physics/metal/metal_box_impact_bullet%s.wav"
33-
local MaxDistance = ACF.LinkDistance * ACF.LinkDistance
3449
local CheckLegal = ACF.CheckLegal
3550
local WireIO = Utilities.WireIO
3651
local Entities = Classes.Entities
@@ -154,21 +169,6 @@ do -- Spawning and Updating --------------------
154169
UpdateTotalAmmo(Entity)
155170
end
156171

157-
local function CheckDistantLinks(Entity, Source)
158-
local Position = Entity:GetPos()
159-
160-
for Link in pairs(Entity[Source]) do
161-
if Position:DistToSqr(Link:GetPos()) > MaxDistance then
162-
local Sound = UnlinkSound:format(math.random(1, 3))
163-
164-
Sounds.SendSound(Entity, Sound, 70, math.random(99, 109), 1)
165-
Sounds.SendSound(Link, Sound, 70, math.random(99, 109), 1)
166-
167-
Entity:Unlink(Link)
168-
end
169-
end
170-
end
171-
172172
hook.Add("ACF_OnSetupInputs", "ACF Rack Motor Delay", function(Entity, List, _, Rack)
173173
if Entity:GetClass() ~= "acf_rack" then return end
174174
if not Rack.CanDropMissile then return end
@@ -236,7 +236,11 @@ do -- Spawning and Updating --------------------
236236
timer.Create("ACF Rack Clock " .. Rack:EntIndex(), 3, 0, function()
237237
if not IsValid(Rack) then return end
238238

239-
CheckDistantLinks(Rack, "Crates")
239+
local Position = Rack:GetPos()
240+
241+
for Link in pairs(Rack.Crates) do
242+
CheckDistantLink(Rack, Link, Position)
243+
end
240244
end)
241245

242246
timer.Create("ACF Rack Ammo " .. Rack:EntIndex(), 1, 0, function()
@@ -282,6 +286,14 @@ do -- Spawning and Updating --------------------
282286

283287
HookRun("ACF_OnEntityUpdate", "acf_rack", self, Data, Rack)
284288

289+
local Crates = self.Crates
290+
291+
if next(Crates) then
292+
for Crate in pairs(Crates) do
293+
self:Unlink(Crate)
294+
end
295+
end
296+
285297
self:UpdateOverlay(true)
286298

287299
net.Start("ACF_UpdateEntity")
@@ -383,6 +395,7 @@ do -- Entity Link/Unlink -----------------------
383395
if Weapon.Crates[Target] then return false, "This rack is already linked to this crate." end
384396
if Target.Weapons[Weapon] then return false, "This rack is already linked to this crate." end
385397
if Target.IsRefill then return false, "Refill crates cannot be linked!" end
398+
if Target:GetPos():DistToSqr(Weapon:GetPos()) > MaxDistance then return false, "This crate is too far away from this rack." end
386399

387400
local Blacklist = Target.RoundData.Blacklist
388401

@@ -621,7 +634,7 @@ do -- Loading ----------------------------------
621634
local Index, Point = self:GetNextMountPoint("Empty")
622635
local Crate = GetNextCrate(self)
623636

624-
if not self.Firing and Index and Crate then
637+
if not self.Firing and Index and Crate and not CheckDistantLink(self, Crate, self:GetPos()) then
625638
local Missile = AddMissile(self, Point, Crate)
626639
local ReloadTime = Missile.ReloadTime
627640

0 commit comments

Comments
 (0)