Hi everyone,
I’m working on creating a mod for FAF (Forged Alliance Forever) that dynamically modifies the Threat Levels of all units during runtime, without manually editing .bp files. The goal is to implement a system that calculates new Threat Levels based on the unit's stats (like DPS, range, and HP) and applies these changes automatically.
This idea stems from my desire to make my games with the M28AI mod more interesting and balanced. I love the M28AI, but I’ve noticed that units with very low Threat Levels tend to be too passive, while units with overly high Threat Levels can be excessively aggressive. By dynamically recalculating Threat Levels to better represent the capabilities of each unit, I believe this will not only improve how M28AI plays but also enhance the performance of other AI mods or systems that rely on Threat Levels for decision-making.
The specific section I want to adjust in the .bp files looks like this:
Defense = {
AirThreatLevel = 0,
ArmorType = 'Normal',
EconomyThreatLevel = 0,
Health = 13000,
MaxHealth = 13000,
RegenRate = 0,
SubThreatLevel = 0,
SurfaceThreatLevel = 50,
},
What I Want to Achieve:
- Dynamically calculate Threat Levels using relevant unit stats:
-
- Combat units: Use stats like DPS, range, and HP to compute values for AirThreatLevel, SurfaceThreatLevel, and SubThreatLevel.
-
- Economic units: Use mass and energy production to calculate EconomyThreatLevel.
- Apply these calculations at runtime without manually editing .bp files, ensuring compatibility with all units (including those added by other mods).
- Ensure compatibility with AI mods like M28AI and potentially improve how AI systems interact with units based on their adjusted Threat Levels.
My Current Approach:
I believe this can be achieved by overriding the UnitBlueprint function in Lua, which processes unit blueprints during game loading. The idea would be to intercept each blueprint, extract the relevant stats, calculate new Threat Levels, and update the blueprint accordingly.
Here’s a rough outline of how I imagine the logic:
local OldUnitBlueprint = UnitBlueprint
function UnitBlueprint(bp)
if bp.Defense then
-- Example calculation for combat Threat Levels
local dps = 0
local range = 0
local hp = bp.Defense.MaxHealth or 0
if bp.Weapon then
for _, weapon in ipairs(bp.Weapon) do
dps = dps + (weapon.Damage or 0) * (weapon.RateOfFire or 1)
range = math.max(range, weapon.MaxRadius or 0)
end
end
-- Calculate new Threat Levels dynamically
if dps > 0 and range > 0 then
bp.Defense.SurfaceThreatLevel = (dps * range) / 1000 + (hp / 1000)
end
-- Example calculation for economic Threat Levels
if bp.Economy then
local massProd = bp.Economy.ProductionPerSecondMass or 0
local energyProd = bp.Economy.ProductionPerSecondEnergy or 0
bp.Defense.EconomyThreatLevel = (massProd * 10) + (energyProd / 10)
end
end
-- Call the original function to ensure other mods and game features work
OldUnitBlueprint(bp)
end
Questions:
- Is overriding UnitBlueprint the best way to dynamically adjust Threat Levels based on unit stats?
- For extracting stats like DPS and range from weapons, is there a better approach than iterating over bp.Weapon?
- How can I ensure compatibility with other mods and make sure my mod loads after them?
- Are there any potential performance concerns with dynamically calculating Threat Levels for all units during loading?
- Has anyone implemented a similar system, and are there best practices or caveats I should keep in mind?
Why This Matters:
My main objective is to improve how AI mods like M28AI handle units, but I suspect this would also apply to other AI mods or systems I haven’t yet considered. By dynamically calculating Threat Levels based on unit stats, I hope to make the AI’s decision-making more logical and engaging, resulting in more interesting games.
I’d love to hear any advice, suggestions, or experiences you might have. Thank you in advance for your help!