Skip to content

Commit

Permalink
More effective rack unlinking checks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thecraftianman committed Mar 22, 2024
1 parent 3270845 commit 1856227
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions lua/entities/acf_rack/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ local ACF = ACF
local Classes = ACF.Classes
local Utilities = ACF.Utilities
local Clock = Utilities.Clock
local MaxDistance = ACF.LinkDistance * ACF.LinkDistance
local UnlinkSound = "physics/metal/metal_box_impact_bullet%s.wav"

local function UpdateTotalAmmo(Entity)
local Total = 0
Expand All @@ -26,9 +28,22 @@ local function UpdateTotalAmmo(Entity)
WireLib.TriggerOutput(Entity, "Total Ammo", Total)
end

local function CheckDistantLink(Entity, Crate, EntPos)
local CrateUnlinked = false

if EntPos:DistToSqr(Crate:GetPos()) > MaxDistance then
local Sound = UnlinkSound:format(math.random(1, 3))

Entity:EmitSound(Sound, 70, math.random(99, 109), ACF.Volume)
Crate:EmitSound(Sound, 70, math.random(99, 109), ACF.Volume)

CrateUnlinked = Entity:Unlink(Crate)
end

return CrateUnlinked
end

do -- Spawning and Updating --------------------
local UnlinkSound = "physics/metal/metal_box_impact_bullet%s.wav"
local MaxDistance = ACF.LinkDistance * ACF.LinkDistance
local CheckLegal = ACF.CheckLegal
local WireIO = Utilities.WireIO
local Entities = Classes.Entities
Expand Down Expand Up @@ -157,21 +172,6 @@ do -- Spawning and Updating --------------------
UpdateTotalAmmo(Entity)
end

local function CheckDistantLinks(Entity, Source)
local Position = Entity:GetPos()

for Link in pairs(Entity[Source]) do
if Position:DistToSqr(Link:GetPos()) > MaxDistance then
local Sound = UnlinkSound:format(math.random(1, 3))

Entity:EmitSound(Sound, 70, math.random(99, 109), ACF.Volume)
Link:EmitSound(Sound, 70, math.random(99, 109), ACF.Volume)

Entity:Unlink(Link)
end
end
end

hook.Add("ACF_OnSetupInputs", "ACF Rack Motor Delay", function(Entity, List, _, Rack)
if Entity:GetClass() ~= "acf_rack" then return end
if not Rack.CanDropMissile then return end
Expand Down Expand Up @@ -239,7 +239,11 @@ do -- Spawning and Updating --------------------
timer.Create("ACF Rack Clock " .. Rack:EntIndex(), 3, 0, function()
if not IsValid(Rack) then return end

CheckDistantLinks(Rack, "Crates")
local Position = Rack:GetPos()

for Link in pairs(Rack.Crates) do
CheckDistantLink(Rack, Link, Position)
end
end)

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

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

local Crates = self.Crates

if next(Crates) then
for Crate in pairs(Crates) do
self:Unlink(Crate)
end
end

self:UpdateOverlay(true)

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

local Blacklist = Target.RoundData.Blacklist

Expand Down Expand Up @@ -624,7 +637,7 @@ do -- Loading ----------------------------------
local Index, Point = self:GetNextMountPoint("Empty")
local Crate = GetNextCrate(self)

if not self.Firing and Index and Crate then
if not self.Firing and Index and Crate and not CheckDistantLink(self, Crate, self:GetPos()) then
local Missile = AddMissile(self, Point, Crate)
local ReloadTime = Missile.ReloadTime

Expand Down

0 comments on commit 1856227

Please sign in to comment.