Skip to content

Commit

Permalink
Add auto reloading for racks
Browse files Browse the repository at this point in the history
  • Loading branch information
thecraftianman committed Aug 26, 2024
1 parent 42036d3 commit c18069b
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions lua/entities/acf_rack/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ include("shared.lua")

-- Local Vars -----------------------------------

local EMPTY = { Type = "Empty", PropMass = 0, ProjMass = 0, Tracer = 0 }
local HookRun = hook.Run
local ACF = ACF
local Contraption = ACF.Contraption
local Classes = ACF.Classes
local Utilities = ACF.Utilities
local Clock = Utilities.Clock
local Sounds = Utilities.Sounds
local EMPTY = { Type = "Empty", PropMass = 0, ProjMass = 0, Tracer = 0 }
local HookRun = hook.Run
local ACF = ACF
local Contraption = ACF.Contraption
local Classes = ACF.Classes
local Utilities = ACF.Utilities
local Clock = Utilities.Clock
local Sounds = Utilities.Sounds
local MaxDistance = ACF.LinkDistance * ACF.LinkDistance
local UnlinkSound = "physics/metal/metal_box_impact_bullet%s.wav"

Expand Down Expand Up @@ -110,6 +110,7 @@ do -- Spawning and Updating --------------------
Entity.ShortName = Rack.ID
Entity.EntType = Rack.EntType
Entity.RackData = Rack
Entity.Class = Rack.ID
Entity.Caliber = Rack.Caliber or 0
Entity.MagSize = Rack.MagSize or 1
Entity.ForcedIndex = Entity.ForcedIndex and math.max(Entity.ForcedIndex, Entity.MagSize)
Expand Down Expand Up @@ -413,7 +414,13 @@ do -- Entity Link/Unlink -----------------------
Weapon:UpdateOverlay()
Target:UpdateOverlay()

UpdateTotalAmmo(Weapon)
if Weapon.State == "Empty" then -- When linked to an empty weapon, attempt to load it
timer.Simple(0.5, function() -- Delay by 500ms just in case the wiring isn't applied at the same time or whatever weird dupe shit happens
if IsValid(Weapon) and IsValid(Target) and Weapon.State == "Empty" and Target:CanConsume() then
Weapon:Reload()
end
end)
end

return true, "Rack linked successfully."
end)
Expand All @@ -430,8 +437,6 @@ do -- Entity Link/Unlink -----------------------
Weapon:UpdateOverlay()
Target:UpdateOverlay()

UpdateTotalAmmo(Weapon)

return true, "Weapon unlinked successfully."
end

Expand All @@ -457,9 +462,7 @@ do -- Entity Inputs ----------------------------

Entity.Reloading = tobool(Value)

if Entity:CanReload() then
Entity:Reload()
end
Entity:Reload()
end)

ACF.AddInputAction("acf_rack", "Unload", function(Entity, Value)
Expand Down Expand Up @@ -573,6 +576,12 @@ do -- Firing -----------------------------------
end
end)
end

timer.Simple(1, function()
if not IsValid(self) then return end

self:Reload()
end)
end
end ---------------------------------------------

Expand Down Expand Up @@ -623,14 +632,17 @@ do -- Loading ----------------------------------

function ENT:CanReload()
if self.RetryReload then return false end
if not self.Reloading then return false end
if not ACF.RacksCanFire then return false end
if self.Disabled then return false end
if self.MagSize == self.CurrentShot then return false end

return true
end

-- TODO: Once Unloading gets implemented, racks have to unload missiles if no empty mountpoint is found.
function ENT:Reload()
if not self:CanReload() then return end

local Index, Point = self:GetNextMountPoint("Empty")
local Crate = GetNextCrate(self)

Expand Down Expand Up @@ -677,9 +689,7 @@ do -- Loading ----------------------------------

self.RetryReload = nil

if self:CanReload() then
self:Reload()
end
self:Reload()
end)
end
end ---------------------------------------------
Expand Down Expand Up @@ -762,7 +772,8 @@ end ---------------------------------------------

do -- Overlay/networking
util.AddNetworkString("ACF.RequestRackInfo")
net.Receive("ACF.RequestRackInfo",function(_,Ply)

net.Receive("ACF.RequestRackInfo", function(_, Ply)
local Rack = net.ReadEntity()
if not IsValid(Rack) then return end

Expand Down Expand Up @@ -816,9 +827,7 @@ do -- Misc -------------------------------------
self:Shoot()
end

if self:CanReload() then
self:Reload()
end
self:Reload()
end

function ENT:Disable()
Expand All @@ -833,6 +842,8 @@ do -- Misc -------------------------------------

WireLib.TriggerOutput(self, "Status", State)
WireLib.TriggerOutput(self, "Ready", State == "Loaded" and 1 or 0)

UpdateTotalAmmo(self)
end

function ENT:GetNextMountPoint(State, CustomStart)
Expand Down

0 comments on commit c18069b

Please sign in to comment.