Skip to content

ACF Objects

LengthenedGradient edited this page Apr 12, 2024 · 17 revisions

In ACF classes, a objects represents a way to store data and rules.

Usage

  • As opposed to simple classes, objects should store both fields (properties) and methods (functions).
  • classes\ammo_types, classes\armor_types use objects.

Example:

-- From `ACF-3\lua\acf\core\classes\ammo_types\registration.lua`
local Classes   = ACF.Classes
local AmmoTypes = Classes.AmmoTypes
local Entries   = {}

function AmmoTypes.Register(ID, Base)
	return Classes.AddObject(ID, Base, Entries)
end

Classes.AddSimpleFunctions(AmmoTypes, Entries)
  • When registering a new ammo type, it will use objects.
  • Similar to the registration file for a simple class, Classes.AddSimpleFunctions should still be used.
-- From ACF-3\lua\acf\entities\ammo_types\ap.lua
local ACF       = ACF
local Classes   = ACF.Classes
local AmmoTypes = Classes.AmmoTypes
local Ammo      = AmmoTypes.Register("AP")

function Ammo:OnLoaded()
	self.Name		 = "Armor Piercing"
	self.Model		 = "models/munitions/round_100mm_shot.mdl"
	self.Description = "A shell made out of a solid piece of steel, meant to penetrate armor."
	self.Blacklist = {
		GL = true,
		SL = true,
	}
end

function Ammo:GetPenetration(Bullet, Speed)
	if not isnumber(Speed) then
		Speed = Bullet.Flight and Bullet.Flight:Length() / ACF.Scale * 0.0254 or Bullet.MuzzleVel
	end

	return ACF.Penetration(Speed, Bullet.ProjMass, Bullet.Diameter * 10)
end

-- There's more code, but we had to truncate it.
-- From ACF-3\lua\acf\entities\ammo_types\heat.lua
local ACF       = ACF
local Classes   = ACF.Classes
local Damage    = ACF.Damage
local AmmoTypes = Classes.AmmoTypes
local Ammo      = AmmoTypes.Register("HEAT", "AP")

function Ammo:OnLoaded()
	Ammo.BaseClass.OnLoaded(self)

	self.Name		 = "High Explosive Anti-Tank"
	self.Description = "A round with a shaped charge inside. Fires a high-velocity jet on detonation."
	self.Blacklist = {
		AC = true,
		MG = true,
		SL = true,
		LAC = true,
		RAC = true,
	}
end

function Ammo:GetPenetration(Bullet, Standoff)
	if not isnumber(Standoff) then
		return 1 -- Does not matter, just so calls to damage functions don't go sneedmode
	end

	local BreakupT      = Bullet.BreakupTime
	local MaxVel        = Bullet.JetMaxVel
	local PenMul        = Bullet.PenMul or 1
	local TargetDensity = ACF.RHADensity -- Assuming RHA
	local Gamma         = math.sqrt(TargetDensity / ACF.CopperDensity)

	local Penetration = 0
	if Standoff < Bullet.BreakupDist then
		local JetTravel = BreakupT * MaxVel
		local K1 = 1 + Gamma
		local K2 = 1 / K1
		Penetration = (K1 * (JetTravel * Standoff) ^ K2 - math.sqrt(K1 * ACF.HEATMinPenVel * BreakupT * JetTravel ^ K2 * Standoff ^ (Gamma * K2))) / Gamma - Standoff
	else
		Penetration = (MaxVel * BreakupT - math.sqrt(ACF.HEATMinPenVel * BreakupT * (MaxVel * BreakupT + Gamma * Standoff))) / Gamma
	end

	return math.max(Penetration * ACF.HEATPenMul * PenMul * 1e3, 0) -- m to mm
end

-- There's more code, but we had to truncate it.
  • Here, objects are used to represent properties (e.g. name, blacklist) and logic (e.g. Ammo:GetPenetration) specific to each ammo type.

Relevant Functions

Classes.AddObject(ID, Base, Destiny)

  • Creates a new object with the given ID, as a subclass of the Base class provided

Clone this wiki locally