Skip to content

Commit

Permalink
fix ROF error, further adjustments and code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
KemGus committed Oct 2, 2023
1 parent 647d16a commit 1ee9720
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 31 deletions.
6 changes: 3 additions & 3 deletions lua/acf/shared/guns/cannon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ ACF_defineGun("100mmC", {
caliber = 10.0,
weight = 1400,
year = 1944,
maxrof = 15, -- maximum rounds per minute
maxrof = 17, -- maximum rounds per minute
round = {
maxlength = 93,
propweight = 9.5
Expand All @@ -102,7 +102,7 @@ ACF_defineGun("120mmC", {
caliber = 12.0,
weight = 2100,
year = 1955,
maxrof = 12, -- maximum rounds per minute
maxrof = 14, -- maximum rounds per minute
round = {
maxlength = 110,
propweight = 18
Expand All @@ -118,7 +118,7 @@ ACF_defineGun("140mmC", {
caliber = 14.0,
weight = 3900,
year = 1990,
maxrof = 8.5, -- maximum rounds per minute
maxrof = 11, -- maximum rounds per minute
round = {
maxlength = 127,
propweight = 28
Expand Down
6 changes: 3 additions & 3 deletions lua/acf/shared/guns/smoothcannon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ ACF_defineGun("100mmSBC", {
gunclass = "SBC",
caliber = 10.0,
weight = 1700,
maxrof = 15, -- maximum rounds per minute
maxrof = 17, -- maximum rounds per minute
year = 1960,
round = {
maxlength = 93,
Expand All @@ -70,7 +70,7 @@ ACF_defineGun("120mmSBC", {
gunclass = "SBC",
caliber = 12.0,
weight = 3200,
maxrof = 12, -- maximum rounds per minute
maxrof = 14, -- maximum rounds per minute
year = 1970,
round = {
maxlength = 115,
Expand All @@ -86,7 +86,7 @@ ACF_defineGun("140mmSBC", {
gunclass = "SBC",
caliber = 14.0,
weight = 4300,
maxrof = 8.5, -- maximum rounds per minute
maxrof = 11, -- maximum rounds per minute
year = 1995,
round = {
maxlength = 140,
Expand Down
10 changes: 10 additions & 0 deletions lua/acf/shared/sh_ace_functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,13 @@ function ACE_Msg( type, txt )
end
]]

-- Helper function to check if a value exists in a table
function ACE_table_contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
68 changes: 43 additions & 25 deletions lua/entities/acf_gun/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ do
end

Gun.PGRoFmod = Lookup.rofmod and math.max(0.01, Lookup.rofmod) or 1 --per gun rof
Gun.maxrof = Lookup.maxrof
Gun.CurrentShot = 0
Gun.MagSize = 1

Expand Down Expand Up @@ -257,7 +258,7 @@ do

Gun:UpdateOverlayText()

Owner:AddCleanup( "acfmenu", Gun )
Owner:AddCleanup("acfmenu", Gun)

ACF_Activate(Gun, 0)

Expand Down Expand Up @@ -537,8 +538,9 @@ function ENT:TriggerInput(iname, value)
end
elseif iname == "ROFLimit" then
-- Set the rate of fire limit if value is greater than 0
local lowestROF = 0.1
if value > 0 then
self.ROFLimit = math.min(1 / (value / 60), 600) -- Limit the rate of fire
self.ROFLimit = math.max(value, lowestROF) -- Limit the rate of fire
else
self.ROFLimit = 0
end
Expand All @@ -555,7 +557,7 @@ function ENT:Heat_Function()
Wire_TriggerOutput(self, "Heat", math.Round(self.Heat))

-- TODO: instead of breaking the gun by heat, decrease accurancy and jam it
local OverHeat = math.max(self.Heat / 200,0) --overheat will start affecting the gun at 200° celcius. STILL unrealistic, weird
local OverHeat = math.max(self.Heat / 200, 0) --overheat will start affecting the gun at 200° celcius. STILL unrealistic, weird
if OverHeat > 1 and self.Caliber < 10 then --leave the low calibers to damage themselves only

self.IsOverheated = true
Expand Down Expand Up @@ -875,10 +877,6 @@ do
end
Wire_TriggerOutput(self, "Ready", 0)

if self.LoaderCount > 0 then
self:ChooseLoader():DecreaseStamina()
end

else

self.CurrentShot = 0
Expand Down Expand Up @@ -927,7 +925,6 @@ function ENT:LoadAmmo( AddTime, Reload )
self.BulletData.Crate = AmmoEnt:EntIndex()

local Adj = not self.BulletData.LengthAdj and 1 or self.BulletData.LengthAdj --FL firerate bonus adjustment
local CrewReload = 1
local curLoaderStamina = 100

local curLoader = self:ChooseLoader()
Expand All @@ -936,24 +933,45 @@ function ENT:LoadAmmo( AddTime, Reload )
end

local maxRof = self.ROFLimit
-- Check if Lookup is valid and not nil
if Lookup and isValid(Lookup.maxrof) and Lookup.maxrof ~= nil then
maxRof = math.min(maxRof, Lookup.maxrof)

if self.maxrof and self.ROFLimit ~= 0 then
maxRof = math.min(self.maxrof, self.ROFLimit)
elseif self.maxrof and self.ROFLimit == 0 then
maxRof = self.maxrof
end

-- Define a table of valid classes
local invalidClasses = {"AC", "MG", "RAC", "HMG", "GL", "SA"}

if not (self.Class == "AC" or self.Class == "MG" or self.Class == "RAC" or self.Class == "HMG" or self.Class == "GL" or self.Class == "SA") then
if self.LoaderCount > 0 then
local Mul = 2
CrewReload = curLoaderStamina / 100 * Mul
else
CrewReload = 1
local fireRateModifier = self.RoFmod * self.PGRoFmod * (AmmoEnt.RoFMul + 1)
local defaultReloadTime = ((math.max(self.BulletData.RoundVolume, self.MinLengthBonus * Adj) / 500) ^ 0.60) * fireRateModifier
local lowestReloadTime = defaultReloadTime

if maxRof > 0 then
lowestReloadTime = 60 / maxRof
end

--print(maxRof)

-- Check if self.Class is in the invalidClasses table
if not ACE_table_contains(invalidClasses, self.Class) and self.maxrof then

if self.LoaderCount > 0 then -- if loaders are linked then

local CrewReload = curLoaderStamina / 100
local reloadTime = lowestReloadTime / CrewReload -- in seconds!!!!

self.ReloadTime = math.Clamp(reloadTime, lowestReloadTime, defaultReloadTime)

--print(lowestReloadTime, defaultReloadTime)
curLoader:DecreaseStamina()
else --no loader
self.ReloadTime = math.max(defaultReloadTime, lowestReloadTime)
end
else -- gun cannot have loader
self.ReloadTime = math.max(defaultReloadTime, lowestReloadTime)
end

local reloadTimeMath_Mul = self.RoFmod * self.PGRoFmod / CrewReload * (AmmoEnt.RoFMul + 1)
local reloadTimeMath = ((math.max(self.BulletData.RoundVolume, self.MinLengthBonus * Adj) / 500) ^ 0.60) * reloadTimeMath_Mul
self.ReloadTime = math.max(reloadTimeMath, maxRof) -- keeping it with 3 variables, because too many things in one line
Wire_TriggerOutput(self, "Loaded", self.BulletData.Type)

self.RateOfFire = (60 / self.ReloadTime)
Expand Down Expand Up @@ -987,7 +1005,7 @@ function ENT:LoadAmmo( AddTime, Reload )
self.BulletData.PropMass = 0
self.BulletData.ProjMass = 0

self:EmitSound("weapons/shotgun/shotgun_empty.wav",68,100)
self:EmitSound("weapons/shotgun/shotgun_empty.wav", 68, 100)
Wire_TriggerOutput(self, "Loaded", "Empty")

self.NextFire = curTime + 0.5
Expand All @@ -1012,11 +1030,11 @@ function ENT:UnloadAmmo()

self.Ready = false
Wire_TriggerOutput(self, "Ready", 0)
self:EmitSound("weapons/shotgun/shotgun_empty.wav",68,100)
self:EmitSound("weapons/shotgun/shotgun_empty.wav", 68, 100)

local unloadtime = self.ReloadTime / 2 -- base time to swap a fully loaded shell out
if self.NextFire < CurTime() then -- unloading in middle of reload
unloadtime = math.min(unloadtime, math.max(self.ReloadTime - (self.NextFire - CurTime()),0) )
unloadtime = math.min(unloadtime, math.max(self.ReloadTime - (self.NextFire - CurTime()), 0) )
end
self:LoadAmmo( unloadtime, true )

Expand All @@ -1033,7 +1051,7 @@ function ENT:MuzzleEffect()

if self.AutoSound and self.Sound ~= "" then
timer.Simple(0.6, function()
self:EmitSound(self.AutoSound, 73, math.random(84,86))
self:EmitSound(self.AutoSound, 73, math.random(84, 86))
end )
end
end
Expand Down Expand Up @@ -1087,7 +1105,7 @@ function ENT:PostEntityPaste( Player, Ent, CreatedEntities )

if AmmoLink.entities and table.Count(AmmoLink.entities) > 0 then

for _,AmmoID in pairs(AmmoLink.entities) do
for _, AmmoID in pairs(AmmoLink.entities) do

local Ammo = CreatedEntities[ AmmoID ]

Expand Down

0 comments on commit 1ee9720

Please sign in to comment.