diff --git a/changelog/snippets/other.6498.md b/changelog/snippets/other.6498.md index 0be67f4d48..1e42ae120a 100644 --- a/changelog/snippets/other.6498.md +++ b/changelog/snippets/other.6498.md @@ -1 +1 @@ -- (#6498, #6502, #6503, #6514, #6517, #6518) Refactor the Enhancements section in the ACU/SACU scripts to replace the long if/else chain with a more modular design that is easier to maintain and hook. Each enhancement has its own dedicated function, named with the format `ProcessEnhancement[EnhancementName]`. The CreateEnhancement function now calls the appropriate enhancement function automatically by that name format. +- (#6498, #6502, #6503, #6514, #6516, #6517, #6518) Refactor the Enhancements section in the ACU/SACU scripts to replace the long if/else chain with a more modular design that is easier to maintain and hook. Each enhancement has its own dedicated function, named with the format `ProcessEnhancement[EnhancementName]`. The CreateEnhancement function now calls the appropriate enhancement function automatically by that name format. diff --git a/units/UAL0301/UAL0301_script.lua b/units/UAL0301/UAL0301_script.lua index e26355bbb4..3ec963c1ad 100644 --- a/units/UAL0301/UAL0301_script.lua +++ b/units/UAL0301/UAL0301_script.lua @@ -26,10 +26,13 @@ UAL0301 = ClassUnit(CommandUnit) { DeathWeapon = ClassWeapon(SCUDeathWeapon) {}, }, + ---@param self UAL0301 __init = function(self) CommandUnit.__init(self, 'RightReactonCannon') end, + ---@param self UAL0301 + ---@param unitBeingBuilt Unit OnStopBuild = function(self, unitBeingBuilt) CommandUnit.OnStopBuild(self, unitBeingBuilt) self:BuildManipulatorSetEnabled(false) @@ -41,6 +44,7 @@ UAL0301 = ClassUnit(CommandUnit) { self.BuildingUnit = false end, + ---@psaram self UAL0301 OnCreate = function(self) CommandUnit.OnCreate(self) self:SetCapturable(false) @@ -48,116 +52,183 @@ UAL0301 = ClassUnit(CommandUnit) { self:SetupBuildBones() end, + ---@param self UAL0301 + ---@param unitBeingBuilt Unit + ---@param order string unused CreateBuildEffects = function(self, unitBeingBuilt, order) EffectUtil.CreateAeonCommanderBuildingEffects(self, unitBeingBuilt, self.BuildEffectBones, self.BuildEffectsBag) end, - CreateEnhancement = function(self, enh) - CommandUnit.CreateEnhancement(self, enh) - local bp = self.Blueprint.Enhancements[enh] - if not bp then return end - -- Teleporter - if enh == 'Teleporter' then - self:AddCommandCap('RULEUCC_Teleport') - elseif enh == 'TeleporterRemove' then - self:RemoveCommandCap('RULEUCC_Teleport') - -- Shields - elseif enh == 'Shield' then - self:AddToggleCap('RULEUTC_ShieldToggle') - self:SetEnergyMaintenanceConsumptionOverride(bp.MaintenanceConsumptionPerSecondEnergy or 0) - self:SetMaintenanceConsumptionActive() - self:CreateShield(bp) - elseif enh == 'ShieldRemove' then - self:DestroyShield() - self:SetMaintenanceConsumptionInactive() - self:RemoveToggleCap('RULEUTC_ShieldToggle') - elseif enh == 'ShieldHeavy' then - self.Trash:Add(ForkThread(self.CreateHeavyShield, self, bp)) - elseif enh == 'ShieldHeavyRemove' then - self:DestroyShield() - self:SetMaintenanceConsumptionInactive() - self:RemoveToggleCap('RULEUTC_ShieldToggle') - -- ResourceAllocation - elseif enh == 'ResourceAllocation' then - local bp = self.Blueprint.Enhancements[enh] - local bpEcon = self.Blueprint.Economy - if not bp then return end - self:SetProductionPerSecondEnergy((bp.ProductionPerSecondEnergy + bpEcon.ProductionPerSecondEnergy) or 0) - self:SetProductionPerSecondMass((bp.ProductionPerSecondMass + bpEcon.ProductionPerSecondMass) or 0) - elseif enh == 'ResourceAllocationRemove' then - local bpEcon = self.Blueprint.Economy - self:SetProductionPerSecondEnergy(bpEcon.ProductionPerSecondEnergy or 0) - self:SetProductionPerSecondMass(bpEcon.ProductionPerSecondMass or 0) - -- Engineering Focus Module - elseif enh == 'EngineeringFocusingModule' then - if not Buffs['AeonSCUBuildRate'] then - BuffBlueprint { - Name = 'AeonSCUBuildRate', - DisplayName = 'AeonSCUBuildRate', - BuffType = 'SCUBUILDRATE', - Stacks = 'REPLACE', - Duration = -1, - Affects = { - BuildRate = { - Add = bp.NewBuildRate - self.Blueprint.Economy.BuildRate, - Mult = 1, - }, - }, - } - end - Buff.ApplyBuff(self, 'AeonSCUBuildRate') - elseif enh == 'EngineeringFocusingModuleRemove' then - if Buff.HasBuff(self, 'AeonSCUBuildRate') then - Buff.RemoveBuff(self, 'AeonSCUBuildRate') - end - -- SystemIntegrityCompensator - elseif enh == 'SystemIntegrityCompensator' then - local name = 'AeonSCURegenRate' - if not Buffs[name] then - BuffBlueprint { - Name = name, - DisplayName = name, - BuffType = 'SCUREGENRATE', - Stacks = 'REPLACE', - Duration = -1, - Affects = { - Regen = { - Add = bp.NewRegenRate - self.Blueprint.Defense.RegenRate, - Mult = 1, - }, - }, - } - end - Buff.ApplyBuff(self, name) - elseif enh == 'SystemIntegrityCompensatorRemove' then - if Buff.HasBuff(self, 'AeonSCURegenRate') then - Buff.RemoveBuff(self, 'AeonSCURegenRate') - end - -- Sacrifice - elseif enh == 'Sacrifice' then - self:AddCommandCap('RULEUCC_Sacrifice') - elseif enh == 'SacrificeRemove' then - self:RemoveCommandCap('RULEUCC_Sacrifice') - -- StabilitySupressant - elseif enh == 'StabilitySuppressant' then - local wep = self:GetWeaponByLabel('RightReactonCannon') - wep:AddDamageMod(bp.NewDamageMod or 0) - wep:AddDamageRadiusMod(bp.NewDamageRadiusMod or 0) - wep:ChangeMaxRadius(bp.NewMaxRadius or 40) - elseif enh == 'StabilitySuppressantRemove' then - local wep = self:GetWeaponByLabel('RightReactonCannon') - wep:AddDamageMod(-self.Blueprint.Enhancements['RightReactonCannon'].NewDamageMod) - wep:AddDamageRadiusMod(bp.NewDamageRadiusMod or 0) - wep:ChangeMaxRadius(bp.NewMaxRadius or 30) - end + --------------------------------------------------------------------------- + --#region Enhancements + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementTeleporter = function(self, bp) + self:AddCommandCap('RULEUCC_Teleport') + end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementTeleporterRemove = function(self, bp) + self:RemoveCommandCap('RULEUCC_Teleport') end, - CreateHeavyShield = function(self, bp) + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement + ProcessEnhancementShield = function(self, bp) + self:AddToggleCap('RULEUTC_ShieldToggle') + self:SetEnergyMaintenanceConsumptionOverride(bp.MaintenanceConsumptionPerSecondEnergy or 0) + self:SetMaintenanceConsumptionActive() + self:CreateShield(bp) + end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementShieldRemove = function(self, bp) + self:DestroyShield() + self:SetMaintenanceConsumptionInactive() + self:RemoveToggleCap('RULEUTC_ShieldToggle') + end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement + ProcessEnhancementShieldHeavy = function(self, bp) WaitTicks(1) self:CreateShield(bp) self:SetEnergyMaintenanceConsumptionOverride(bp.MaintenanceConsumptionPerSecondEnergy or 0) self:SetMaintenanceConsumptionActive() end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementShieldHeavyRemove = function(self, bp) + self:DestroyShield() + self:SetMaintenanceConsumptionInactive() + self:RemoveToggleCap('RULEUTC_ShieldToggle') + end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement + ProcessEnhancementResourceAllocation = function(self, bp) + local bpEcon = self.Blueprint.Economy + self:SetProductionPerSecondEnergy((bp.ProductionPerSecondEnergy + bpEcon.ProductionPerSecondEnergy) or 0) + self:SetProductionPerSecondMass((bp.ProductionPerSecondMass + bpEcon.ProductionPerSecondMass) or 0) + end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementResourceAllocationRemove = function(self, bp) + local bpEcon = self.Blueprint.Economy + self:SetProductionPerSecondEnergy(bpEcon.ProductionPerSecondEnergy or 0) + self:SetProductionPerSecondMass(bpEcon.ProductionPerSecondMass or 0) + end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement + ProcessEnhancementEngineeringFocusingModule = function(self, bp) + if not Buffs['AeonSCUBuildRate'] then + BuffBlueprint { + Name = 'AeonSCUBuildRate', + DisplayName = 'AeonSCUBuildRate', + BuffType = 'SCUBUILDRATE', + Stacks = 'REPLACE', + Duration = -1, + Affects = { + BuildRate = { + Add = bp.NewBuildRate - self.Blueprint.Economy.BuildRate, + Mult = 1, + }, + }, + } + end + Buff.ApplyBuff(self, 'AeonSCUBuildRate') + end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementEngineeringFocusingModuleRemove = function(self, bp) + if Buff.HasBuff(self, 'AeonSCUBuildRate') then + Buff.RemoveBuff(self, 'AeonSCUBuildRate') + end + end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement + ProcessEnhancementSystemIntegrityCompensator = function(self, bp) + if not Buffs['AeonSCURegenRate'] then + BuffBlueprint { + Name = 'AeonSCURegenRate', + DisplayName = 'AeonSCURegenRate', + BuffType = 'SCUREGENRATE', + Stacks = 'REPLACE', + Duration = -1, + Affects = { + Regen = { + Add = bp.NewRegenRate - self.Blueprint.Defense.RegenRate, + Mult = 1, + }, + }, + } + end + Buff.ApplyBuff(self, 'AeonSCURegenRate') + end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementSystemIntegrityCompensatorRemove = function(self, bp) + if Buff.HasBuff(self, 'AeonSCURegenRate') then + Buff.RemoveBuff(self, 'AeonSCURegenRate') + end + end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementSacrifice = function(self, bp) + self:AddCommandCap('RULEUCC_Sacrifice') + end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementSacrificeRemove = function(self, bp) + self:RemoveCommandCap('RULEUCC_Sacrifice') + end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement + ProcessEnhancementStabilitySuppressant = function(self, bp) + local wep = self:GetWeaponByLabel('RightReactonCannon') + wep:AddDamageMod(bp.NewDamageMod or 0) + wep:AddDamageRadiusMod(bp.NewDamageRadiusMod or 0) + wep:ChangeMaxRadius(bp.NewMaxRadius or 40) + end, + + ---@param self UAL0301 + ---@param bp UnitBlueprintEnhancement + ProcessEnhancementStabilitySuppressantRemove = function(self, bp) + local wep = self:GetWeaponByLabel('RightReactonCannon') + wep:AddDamageMod(-self.Blueprint.Enhancements['RightReactonCannon'].NewDamageMod) + wep:AddDamageRadiusMod(bp.NewDamageRadiusMod or 0) + wep:ChangeMaxRadius(bp.NewMaxRadius or 30) + end, + + ---@param self UAL0301 + ---@param enh Enhancement + CreateEnhancement = function(self, enh) + CommandUnit.CreateEnhancement(self, enh) + local bp = self.Blueprint.Enhancements[enh] + if not bp then return end + + local ref = 'ProcessEnhancement' .. enh + local handler = self[ref] + if handler then + handler(self, bp) + else + WARN("Missing enhancement: ", enh, " for unit: ", self:GetUnitId(), " note that the function name should be called: ", ref) + end + end, + + --#endregion } TypeClass = UAL0301