From d9e49e7352ef064b0ccc4f41f755ba952f005a29 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 18 Nov 2024 17:25:39 +0000 Subject: [PATCH] Apply new enhancement processing convention to Seraphim SACU (#6518) --------- Co-authored-by: lL1l1 <82986251+lL1l1@users.noreply.github.com> --- changelog/snippets/other.6498.md | 2 +- units/XSL0301/XSL0301_script.lua | 285 +++++++++++++++++++------------ 2 files changed, 178 insertions(+), 109 deletions(-) diff --git a/changelog/snippets/other.6498.md b/changelog/snippets/other.6498.md index 65ecdb4f9b..0be67f4d48 100644 --- a/changelog/snippets/other.6498.md +++ b/changelog/snippets/other.6498.md @@ -1 +1 @@ -- (#6498, #6502, #6503, #6514, #6517) 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, #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/XSL0301/XSL0301_script.lua b/units/XSL0301/XSL0301_script.lua index d848c1c289..d99402b11e 100644 --- a/units/XSL0301/XSL0301_script.lua +++ b/units/XSL0301/XSL0301_script.lua @@ -38,10 +38,12 @@ XSL0301 = ClassUnit(CommandUnit) { }, }, + ---@param self XSL0301 __init = function(self) CommandUnit.__init(self, 'LightChronatronCannon') end, + ---@param self XSL0301 OnCreate = function(self) CommandUnit.OnCreate(self) self:SetCapturable(false) @@ -51,126 +53,193 @@ XSL0301 = ClassUnit(CommandUnit) { self:GetWeaponByLabel('AutoOverCharge').NeedsUpgrade = true end, + ---@param self XSL0301 + ---@param builder Unit + ---@param layer Layer StartBeingBuiltEffects = function(self, builder, layer) CommandUnit.StartBeingBuiltEffects(self, builder, layer) self.Trash:Add(ForkThread(EffectUtil.CreateSeraphimBuildThread, self, builder, self.OnBeingBuiltEffectsBag, 2)) end, + ---@param self XSL0301 + ---@param unitBeingBuilt Unit + ---@param order string unused CreateBuildEffects = function(self, unitBeingBuilt, order) - EffectUtil.CreateSeraphimUnitEngineerBuildingEffects(self, unitBeingBuilt, self.BuildEffectBones, - self.BuildEffectsBag) + EffectUtil.CreateSeraphimUnitEngineerBuildingEffects(self, unitBeingBuilt, self.BuildEffectBones, self.BuildEffectsBag) end, + -- ===================================================================================================================== + -- EMHANCEMENTS + + ---@param self XSL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementTeleporter = function(self, bp) + self:AddCommandCap('RULEUCC_Teleport') + end, + + ---@param self XSL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementTeleporterRemove = function(self, bp) + self:RemoveCommandCap('RULEUCC_Teleport') + end, + + ---@param self XSL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementMissile = function(self, bp) + self:AddCommandCap('RULEUCC_Tactical') + self:AddCommandCap('RULEUCC_SiloBuildTactical') + self:SetWeaponEnabledByLabel('Missile', true) + end, + + ---@param self XSL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementMissileRemove = function(self, bp) + self:RemoveCommandCap('RULEUCC_Tactical') + self:RemoveCommandCap('RULEUCC_SiloBuildTactical') + self:SetWeaponEnabledByLabel('Missile', false) + end, + + ---@param self XSL0301 + ---@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 XSL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementShieldRemove = function(self, bp) + self:DestroyShield() + self:SetMaintenanceConsumptionInactive() + self:RemoveToggleCap('RULEUTC_ShieldToggle') + end, + + ---@param self XSL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementOvercharge = function(self, bp) + self:AddCommandCap('RULEUCC_Overcharge') + self:GetWeaponByLabel('OverCharge').NeedsUpgrade = false + self:GetWeaponByLabel('AutoOverCharge').NeedsUpgrade = false + end, + + ---@param self XSL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementOverchargeRemove = function(self, bp) + self:RemoveCommandCap('RULEUCC_Overcharge') + self:SetWeaponEnabledByLabel('OverCharge', false) + self:SetWeaponEnabledByLabel('AutoOverCharge', false) + self:GetWeaponByLabel('OverCharge').NeedsUpgrade = true + self:GetWeaponByLabel('AutoOverCharge').NeedsUpgrade = true + end, + + ---@param self XSL0301 + ---@param bp UnitBlueprintEnhancement + ProcessEnhancementEngineeringThroughput = function(self, bp) + if not Buffs['SeraphimSCUBuildRate'] then + BuffBlueprint { + Name = 'SeraphimSCUBuildRate', + DisplayName = 'SeraphimSCUBuildRate', + BuffType = 'SCUBUILDRATE', + Stacks = 'REPLACE', + Duration = -1, + Affects = { + BuildRate = { + Add = bp.NewBuildRate - self.Blueprint.Economy.BuildRate, + Mult = 1, + }, + }, + } + end + Buff.ApplyBuff(self, 'SeraphimSCUBuildRate') + end, + + ---@param self XSL0301 + ---@param bp UnitBlueprintEnhancement + ProcessEnhancementEngineeringThroughputRemove = function(self, bp) + if Buff.HasBuff(self, 'SeraphimSCUBuildRate') then + Buff.RemoveBuff(self, 'SeraphimSCUBuildRate') + end + end, + + ---@param self XSL0301 + ---@param bp UnitBlueprintEnhancement + ProcessEnhancementDamageStabilization = function (self, bp) + if not Buffs['SeraphimSCUDamageStabilization'] then + BuffBlueprint { + Name = 'SeraphimSCUDamageStabilization', + DisplayName = 'SeraphimSCUDamageStabilization', + BuffType = 'SCUUPGRADEDMG', + Stacks = 'ALWAYS', + Duration = -1, + Affects = { + MaxHealth = { + Add = bp.NewHealth, + Mult = 1.0, + }, + Regen = { + Add = bp.NewRegenRate, + Mult = 1.0, + }, + }, + } + end + if Buff.HasBuff(self, 'SeraphimSCUDamageStabilization') then + Buff.RemoveBuff(self, 'SeraphimSCUDamageStabilization') + end + Buff.ApplyBuff(self, 'SeraphimSCUDamageStabilization') + end, + + ---@param self XSL0301 + ---@param bp UnitBlueprintEnhancement unused + ProcessEnhancementDamageStabilizationRemove = function (self, bp) + if Buff.HasBuff(self, 'SeraphimSCUDamageStabilization') then + Buff.RemoveBuff(self, 'SeraphimSCUDamageStabilization') + end + end, + + ---@param self XSL0301 + ---@param bp UnitBlueprintEnhancement + ProcessEnhancementEnhancedSensors = function(self, bp) + self:SetIntelRadius('Vision', bp.NewVisionRadius or 104) + self:SetIntelRadius('Omni', bp.NewOmniRadius or 104) + local wep = self:GetWeaponByLabel('LightChronatronCannon') + wep:ChangeMaxRadius(bp.NewMaxRadius or 35) + local wep = self:GetWeaponByLabel('OverCharge') + wep:ChangeMaxRadius(35) + local aoc = self:GetWeaponByLabel('AutoOverCharge') + aoc:ChangeMaxRadius(35) + end, + + ---@param self XSL0301 + ---@param bp UnitBlueprintEnhancement + ProcessEnhancementEnhancedSensorsRemove = function(self, bp) + local bpIntel = self.Blueprint.Intel + self:SetIntelRadius('Vision', bpIntel.VisionRadius or 26) + self:SetIntelRadius('Omni', bpIntel.OmniRadius or 16) + local wep = self:GetWeaponByLabel('LightChronatronCannon') + wep:ChangeMaxRadius(bp.NewMaxRadius or 25) + local wep = self:GetWeaponByLabel('OverCharge') + wep:ChangeMaxRadius(bp.NewMaxRadius or 25) + local aoc = self:GetWeaponByLabel('AutoOverCharge') + aoc:ChangeMaxRadius(bp.NewMaxRadius or 25) + end, + + ---@param self XSL0301 + ---@param enh Enhancement 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') - -- Missile - elseif enh == 'Missile' then - self:AddCommandCap('RULEUCC_Tactical') - self:AddCommandCap('RULEUCC_SiloBuildTactical') - self:SetWeaponEnabledByLabel('Missile', true) - elseif enh == 'MissileRemove' then - self:RemoveCommandCap('RULEUCC_Tactical') - self:RemoveCommandCap('RULEUCC_SiloBuildTactical') - self:SetWeaponEnabledByLabel('Missile', false) - -- 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') - -- Overcharge - elseif enh == 'Overcharge' then - self:AddCommandCap('RULEUCC_Overcharge') - self:GetWeaponByLabel('OverCharge').NeedsUpgrade = false - self:GetWeaponByLabel('AutoOverCharge').NeedsUpgrade = false - elseif enh == 'OverchargeRemove' then - self:RemoveCommandCap('RULEUCC_Overcharge') - self:SetWeaponEnabledByLabel('OverCharge', false) - self:SetWeaponEnabledByLabel('AutoOverCharge', false) - self:GetWeaponByLabel('OverCharge').NeedsUpgrade = true - self:GetWeaponByLabel('AutoOverCharge').NeedsUpgrade = true - -- Engineering Throughput Upgrade - elseif enh == 'EngineeringThroughput' then - if not Buffs['SeraphimSCUBuildRate'] then - BuffBlueprint { - Name = 'SeraphimSCUBuildRate', - DisplayName = 'SeraphimSCUBuildRate', - BuffType = 'SCUBUILDRATE', - Stacks = 'REPLACE', - Duration = -1, - Affects = { - BuildRate = { - Add = bp.NewBuildRate - self.Blueprint.Economy.BuildRate, - Mult = 1, - }, - }, - } - end - Buff.ApplyBuff(self, 'SeraphimSCUBuildRate') - elseif enh == 'EngineeringThroughputRemove' then - if Buff.HasBuff(self, 'SeraphimSCUBuildRate') then - Buff.RemoveBuff(self, 'SeraphimSCUBuildRate') - end - -- Damage Stabilization - elseif enh == 'DamageStabilization' then - if not Buffs['SeraphimSCUDamageStabilization'] then - BuffBlueprint { - Name = 'SeraphimSCUDamageStabilization', - DisplayName = 'SeraphimSCUDamageStabilization', - BuffType = 'SCUUPGRADEDMG', - Stacks = 'ALWAYS', - Duration = -1, - Affects = { - MaxHealth = { - Add = bp.NewHealth, - Mult = 1.0, - }, - Regen = { - Add = bp.NewRegenRate, - Mult = 1.0, - }, - }, - } - end - if Buff.HasBuff(self, 'SeraphimSCUDamageStabilization') then - Buff.RemoveBuff(self, 'SeraphimSCUDamageStabilization') - end - Buff.ApplyBuff(self, 'SeraphimSCUDamageStabilization') - elseif enh == 'DamageStabilizationRemove' then - if Buff.HasBuff(self, 'SeraphimSCUDamageStabilization') then - Buff.RemoveBuff(self, 'SeraphimSCUDamageStabilization') - end - -- Enhanced Sensor Systems - elseif enh == 'EnhancedSensors' then - self:SetIntelRadius('Vision', bp.NewVisionRadius or 104) - self:SetIntelRadius('Omni', bp.NewOmniRadius or 104) - local wep = self:GetWeaponByLabel('LightChronatronCannon') - wep:ChangeMaxRadius(bp.NewMaxRadius or 35) - local wep = self:GetWeaponByLabel('OverCharge') - wep:ChangeMaxRadius(35) - local aoc = self:GetWeaponByLabel('AutoOverCharge') - aoc:ChangeMaxRadius(35) - elseif enh == 'EnhancedSensorsRemove' then - local bpIntel = self.Blueprint.Intel - self:SetIntelRadius('Vision', bpIntel.VisionRadius or 26) - self:SetIntelRadius('Omni', bpIntel.OmniRadius or 16) - local wep = self:GetWeaponByLabel('LightChronatronCannon') - wep:ChangeMaxRadius(bp.NewMaxRadius or 25) - local wep = self:GetWeaponByLabel('OverCharge') - wep:ChangeMaxRadius(bp.NewMaxRadius or 25) - local aoc = self:GetWeaponByLabel('AutoOverCharge') - aoc:ChangeMaxRadius(bp.NewMaxRadius or 25) + + 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, }