Skip to content

Commit

Permalink
Merge pull request #452 from ACF-Team/settings-changes-2
Browse files Browse the repository at this point in the history
Settings API improvements
  • Loading branch information
marchc1 authored Jan 17, 2025
2 parents 8814b06 + 9d8ef2e commit d26a777
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 456 deletions.
174 changes: 149 additions & 25 deletions lua/acf/core/globals.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,113 @@
local ACF = ACF

do
-- MARCH:
-- Until things load, theres no way to register settings, so all the settings stuff gets created here.
-- Data callbacks later creates ACF.__OnDefinedSetting (and also goes through everything defined here).
-- Both combined should ensure that no matter where ACF.DefineSetting is called, it will be registered
-- (but for official addon stuff, we should just define it here)

-- It would be ideal if we could put this stuff somewhere else for organization's sake, but for that I
-- think we'd need to modify gloader, since iirc load order starts with Addon/core/[A-Z order] and
-- globals is the first file that would load...

ACF.__DefinedSettings = ACF.__DefinedSettings or {}

-- This is kind of a weird API, but it allows DataCallback functions to set setting data as long as the key
-- isn't the default values (Key, Default, TextWhenChanged, Callback). Just call ACF.GetWorkingSetting() in
-- a data callback
local SettingData = {}

-- Defines a single setting.
-- Internally sets up the global, then registers the setting data.

-- A callback function can be provided. This callback should be a function that takes in a string Key and a
-- arbitrary type Value, and returns Value back. You can use a function that returns a function to create a
-- standard for a specific type; see ACF.BooleanDataCallback, FloatDataCallback, etc for examples of how that
-- behavior works.

function ACF.DefineSetting(Key, Default, TextWhenChanged, Callback)
ACF[Key] = Default

SettingData.Key = Key
SettingData.Default = Default
SettingData.TextWhenChanged = TextWhenChanged
SettingData.Callback = Callback

ACF.__DefinedSettings[Key] = SettingData
SettingData = {}

if ACF.__OnDefinedSetting then
ACF.__OnDefinedSetting(Key, Default, TextWhenChanged, Callback)
end
end

-- Returns the current value for the setting (ACF[Key]), along with the settings data.
function ACF.GetSetting(Key)
if not ACF[Key] then return end

return ACF[Key], ACF.__DefinedSettings[Key]
end

-- Returns the current settings table. Note that immediately upon the execution of ACF.DefineSetting(),
-- SettingData is set to a new table; this is meant to be used in a data callback context (to write things
-- like minimum and maximum values for the panel, for example)
function ACF.GetWorkingSetting()
return SettingData
end

function ACF.BooleanDataCallback(Callback)
local SettingData = ACF.GetWorkingSetting()
SettingData.Type = "Boolean"

return function(_, Value)
Value = tobool(Value)

if Callback then
Callback(Value)
end

return Value
end
end

function ACF.FactorDataCallback(ThreshKey, Min, Max, Decimals)
local SettingData = ACF.GetWorkingSetting()
SettingData.Type = "Factor"
SettingData.ThreshKey = ThreshKey
SettingData.Min = Min
SettingData.Max = Max
SettingData.Decimals = Decimals

return function(Key, Value)
local Factor = math.Round(tonumber(Value) or 1, Decimals or 2)
if Min then Factor = math.max(Factor, Min) end
if Max then Factor = math.min(Factor, Max) end

local Old = ACF[Key]
ACF[ThreshKey] = ACF[ThreshKey] / Old * Factor

return Factor
end
end

function ACF.FloatDataCallback(Min, Max, Decimals)
local SettingData = ACF.GetWorkingSetting()
SettingData.Type = "Float"
SettingData.Min = Min
SettingData.Max = Max
SettingData.Decimals = Decimals

return function(_, Value)
local Float = math.Round(tonumber(Value) or 1, Decimals or 2)
if Min then Float = math.max(Float, Min) end
if Max then Float = math.min(Float, Max) end

return Float
end
end
end

do -- ACF global vars
ACF.AmmoCrates = ACF.AmmoCrates or {}
ACF.FuelTanks = ACF.FuelTanks or {}
Expand All @@ -9,32 +117,55 @@ do -- ACF global vars
ACF.ModelData = ACF.ModelData or { Models = {} }

-- General Settings
ACF.LegalChecks = true -- Whether or not IsLegal checks should be run on ACF entities
ACF.NameAndShame = false -- Whether or not IsLegal checks should message everyone* about ACF entities getting disabled
ACF.VehicleLegalChecks = true -- Whether or not IsLegal checks should be run on vehicle entities
ACF.DefineSetting("AllowAdminData", false, "Admin server data access has been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("RestrictInfo", true, "Entity information has been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("LegalChecks", true, "Legality checks for ACF entities has been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("NameAndShame", false, "Console messages for failed legality checks have been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("VehicleLegalChecks", true, "Legality checks for vehicles has been %s.", ACF.BooleanDataCallback())

ACF.DefineSetting("GunsCanFire", true, "Gunfire has been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("GunsCanSmoke", true, "Gun sounds and particles have been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("RacksCanFire", true, "Missile racks have been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("RequireFuel", true, "Engine fuel requirements have been %s.", ACF.BooleanDataCallback())

ACF.Threshold = 264.7
ACF.DefineSetting("HealthFactor", 1, "Health multiplier has been set to a factor of %.2f.", ACF.FactorDataCallback("Threshold", 0.01, 2, 2))

ACF.ArmorMod = 1
ACF.DefineSetting("ArmorFactor", 1, "Armor multiplier has been set to a factor of %.2f.", ACF.FactorDataCallback("ArmorMod", 0.01, 2, 2))

ACF.FuelRate = 15
ACF.DefineSetting("FuelFactor", 1, "Fuel rate multiplier has been set to a factor of %.2f.", ACF.FactorDataCallback("FuelRate", 0.01, 2, 2))

ACF.MinimumArmor = 1 -- Minimum possible armor that can be given to an entity
ACF.MaximumArmor = 5000 -- Maximum possible armor that can be given to an entity
ACF.DefineSetting("MaxThickness", 300, nil, ACF.FloatDataCallback(ACF.MinimumArmor, ACF.MaximumArmor, 0))

ACF.DefineSetting("HEPush", true, "Explosive energy entity pushing has been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("KEPush", true, "Kinectic energy entity pushing has been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("RecoilPush", true, "Recoil entity pushing has been %s.", ACF.BooleanDataCallback())

ACF.DefineSetting("AllowFunEnts", true, "Fun Entities have been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("AllowProcArmor", false, "Procedural armor has been %s.", ACF.BooleanDataCallback(function(Value)
ACF.GlobalFilter["acf_armor"] = not Value
return Value
end))

ACF.DefineSetting("WorkshopContent", true, "Workshop content downloading has been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("WorkshopExtras", true, "Extra Workshop content downloading has been %s.", ACF.BooleanDataCallback())

ACF.DefineSetting("CreateDebris", true, "Networking debris has been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("CreateFireballs", false, "Debris fireballs have been %s.", ACF.BooleanDataCallback())
ACF.DefineSetting("FireballMult", 1, nil, ACF.FloatDataCallback(0.01, 1, 2))

ACF.Year = 1945
ACF.IllegalDisableTime = 30 -- Time in seconds for an entity to be disabled when it fails ACF.IsLegal
ACF.RestrictInfo = true -- If enabled, players will be only allowed to get info from entities they're allowed to mess with.
ACF.AllowAdminData = false -- Allows admins to mess with a few server settings and data variables
ACF.HEPush = true -- Whether or not HE pushes on entities
ACF.KEPush = true -- Whether or not kinetic force pushes on entities
ACF.RecoilPush = true -- Whether or not ACF guns apply recoil
ACF.Volume = 1 -- Global volume for ACF sounds
ACF.AllowFunEnts = true -- Allows entities listed under the Fun Stuff option to be used
ACF.AllowProcArmor = false --Allows procedural armor entities to be used.
ACF.WorkshopContent = true -- Enable workshop content download for clients
ACF.WorkshopExtras = false -- Enable extra workshop content download for clients
ACF.SmokeWind = 5 + math.random() * 35 --affects the ability of smoke to be used for screening effect
ACF.MobilityLinkDistance = 650 -- Maximum distance, in inches, at which mobility-related components will remain linked with each other
ACF.LinkDistance = 650 -- Maximum distance, in inches, at which components will remain linked with each other
ACF.MinimumArmor = 1 -- Minimum possible armor that can be given to an entity
ACF.MaximumArmor = 5000 -- Maximum possible armor that can be given to an entity
ACF.KillIconColor = Color(200, 200, 48)

ACF.GunsCanFire = true
ACF.GunsCanSmoke = true
ACF.RacksCanFire = true

-- Unit Conversion
ACF.MeterToInch = 39.3701 -- Meters to inches
ACF.gCmToKgIn = 0.016387064 -- g/cm³ to kg/in³ :face_vomiting: :face_vomiting: :face_vomiting:
Expand All @@ -53,10 +184,6 @@ do -- ACF global vars
-- External and Terminal Ballistics
ACF.DragDiv = 80 --Drag fudge factor
ACF.Scale = 1 --Scale factor for ACF in the game world
ACF.HealthFactor = 1
ACF.Threshold = 264.7 -- Health Divisor, directly tied to ACF.HealthFactor
ACF.ArmorMod = 1
ACF.ArmorFactor = 1 -- Multiplier for ACF.ArmorMod
ACF.Gravity = Vector(0, 0, -GetConVar("sv_gravity"):GetInt())
ACF.GlobalFilter = { -- Global ACF filter
gmod_ghost = true,
Expand Down Expand Up @@ -130,9 +257,6 @@ do -- ACF global vars
ACF.GunInaccuracyBias = 2 -- Higher numbers make shots more likely to be inaccurate. Choose between 0.5 to 4. Default is 2 (unbiased).

-- Fuel
ACF.RequireFuel = true -- Whether or not fuel usage should be required for engines
ACF.FuelRate = 15 -- Multiplier for fuel usage, 1.0 is approx real world
ACF.FuelFactor = 1 -- Multiplier for ACF.FuelRate
ACF.FuelMinSize = 6 -- Defines the shortest possible length of fuel tanks for all their axises, in gmu
ACF.FuelMaxSize = 96 -- Defines the highest possible length of fuel tanks for all their axises, in gmu
ACF.FuelArmor = 1 -- How many millimeters of armor fuel tanks have
Expand Down
Loading

0 comments on commit d26a777

Please sign in to comment.