Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update UEF SACU Enhancement Script #6515

Merged
merged 7 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"Athanah",
"atleast",
"attachedunit",
"attachee",
"attackmove",
"autobalance",
"autoguns",
Expand Down
2 changes: 1 addition & 1 deletion changelog/snippets/other.6498.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- (#6498, #6502, #6503) 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) 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.
278 changes: 185 additions & 93 deletions units/UEL0301/UEL0301_script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
-- Copyright © 2005 Gas Powered Games, Inc. All rights reserved.
-----------------------------------------------------------------

local Shield = import("/lua/shield.lua").Shield
local DefaultWep = import("/lua/sim/defaultweapons.lua")
local DefaultUnit = import("/lua/defaultunits.lua")
local EffectUtil = import("/lua/effectutilities.lua")
local CommandUnit = import("/lua/defaultunits.lua").CommandUnit
local TWeapons = import("/lua/terranweapons.lua")

local CommandUnit = DefaultUnit.CommandUnit
local TDFHeavyPlasmaCannonWeapon = TWeapons.TDFHeavyPlasmaCannonWeapon
local SCUDeathWeapon = import("/lua/sim/defaultweapons.lua").SCUDeathWeapon
local SCUDeathWeapon = DefaultWep.SCUDeathWeapon

---@class UEL0301 : CommandUnit
UEL0301 = ClassUnit(CommandUnit) {
Expand Down Expand Up @@ -54,7 +56,7 @@ UEL0301 = ClassUnit(CommandUnit) {

---@param self UEL0301
---@param unitBeingBuilt Unit
---@param order string
---@param order string unused
CreateBuildEffects = function(self, unitBeingBuilt, order)
-- Different effect if we have building cube
if unitBeingBuilt.BuildingCube then
Expand Down Expand Up @@ -127,7 +129,7 @@ UEL0301 = ClassUnit(CommandUnit) {
end,

---@param self UEL0301
---@param pod TConstructionPodUnit
---@param pod TConstructionPodUnit unused
---@param rebuildDrone boolean
NotifyOfPodDeath = function(self, pod, rebuildDrone)
if rebuildDrone == true then
Expand Down Expand Up @@ -171,101 +173,187 @@ UEL0301 = ClassUnit(CommandUnit) {
attachee:SetDoNotTarget(false)
end,


-- ============================================================================================================================================================
-- ENHANCEMENTS

--- Drone Upgrade
---@param self UEL0301
---@param enh string
CreateEnhancement = function(self, enh)
CommandUnit.CreateEnhancement(self, enh)
local bp = self:GetBlueprint().Enhancements[enh]
if not bp then return end
if enh == 'Pod' then
local location = self:GetPosition('AttachSpecial01')
local pod = CreateUnitHPR('UEA0003', self.Army, location[1], location[2], location[3], 0, 0, 0)
pod:SetParent(self, 'Pod')
pod:SetCreator(self)
self.Trash:Add(pod)
self.HasPod = true
self.Pod = pod
elseif enh == 'PodRemove' then
if self.HasPod == true then
self.HasPod = false
if self.Pod and not self.Pod:BeenDestroyed() then
self.Pod:Kill()
self.Pod = nil
end
if self.RebuildingPod ~= nil then
RemoveEconomyEvent(self, self.RebuildingPod)
self.RebuildingPod = nil
end
---@param bp UnitBlueprintEnhancement unused
ProcessEnhancementPod = function(self, bp)
MrRowey marked this conversation as resolved.
Show resolved Hide resolved
local location = self:GetPosition('AttachSpecial01')
local pod = CreateUnitHPR('UEA0003', self.Army, location[1], location[2], location[3], 0, 0, 0)
pod:SetParent(self, 'Pod')
pod:SetCreator(self)
self.Trash:Add(pod)
self.HasPod = true
self.Pod = pod
end,

---@param self UEL0301
---@param bp UnitBlueprintEnhancement unused
ProcessEnhancementPodRemove = function(self, bp)
if self.HasPod == true then
self.HasPod = false
if self.Pod and not self.Pod:BeenDestroyed() then
self.Pod:Kill()
self.Pod = nil
end
if self.RebuildingPod ~= nil then
RemoveEconomyEvent(self, self.RebuildingPod)
self.RebuildingPod = nil
end
KillThread(self.RebuildThread)
elseif enh == 'Shield' then
self:AddToggleCap('RULEUTC_ShieldToggle')
end
KillThread(self.RebuildThread)
end,

---@param self UEL0301
---@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 UEL0301
---@param bp UnitBlueprintEnhancement unused
ProcessEnhancementShieldRemove = function (self, bp)
RemoveUnitEnhancement(self, 'Shield')
self:DestroyShield()
self:SetMaintenanceConsumptionInactive()
self:RemoveToggleCap('RULEUTC_ShieldToggle')
end,

---@param self UEL0301
---@param bp UnitBlueprintEnhancement
ProcessEnhancementShieldGeneratorField = function(self, bp)
self:DestroyShield()
self:ForkThread(function()
WaitTicks(1)
self:CreateShield(bp)
self:SetEnergyMaintenanceConsumptionOverride(bp.MaintenanceConsumptionPerSecondEnergy or 0)
self:SetMaintenanceConsumptionActive()
self:CreateShield(bp)
elseif enh == 'ShieldRemove' then
RemoveUnitEnhancement(self, 'Shield')
self:DestroyShield()
self:SetMaintenanceConsumptionInactive()
self:RemoveToggleCap('RULEUTC_ShieldToggle')
elseif enh == 'ShieldGeneratorField' then
self:DestroyShield()
self:ForkThread(function()
WaitTicks(1)
self:CreateShield(bp)
self:SetEnergyMaintenanceConsumptionOverride(bp.MaintenanceConsumptionPerSecondEnergy or 0)
self:SetMaintenanceConsumptionActive()
end)
elseif enh == 'ShieldGeneratorFieldRemove' then
self:DestroyShield()
self:SetMaintenanceConsumptionInactive()
self:RemoveToggleCap('RULEUTC_ShieldToggle')
elseif enh =='ResourceAllocation' then
local bp = self:GetBlueprint().Enhancements[enh]
local bpEcon = self:GetBlueprint().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:GetBlueprint().Economy
self:SetProductionPerSecondEnergy(bpEcon.ProductionPerSecondEnergy or 0)
self:SetProductionPerSecondMass(bpEcon.ProductionPerSecondMass or 0)
elseif enh == 'SensorRangeEnhancer' then
self:SetIntelRadius('Vision', bp.NewVisionRadius or 104)
self:SetIntelRadius('Omni', bp.NewOmniRadius or 104)
elseif enh == 'SensorRangeEnhancerRemove' then
local bpIntel = self:GetBlueprint().Intel
self:SetIntelRadius('Vision', bpIntel.VisionRadius or 26)
self:SetIntelRadius('Omni', bpIntel.OmniRadius or 26)
elseif enh == 'RadarJammer' then
self:SetIntelRadius('Jammer', bp.NewJammerRadius or 26)
self.RadarJammerEnh = true
self:EnableUnitIntel('Enhancement', 'Jammer')
self:AddToggleCap('RULEUTC_JammingToggle')
elseif enh == 'RadarJammerRemove' then
local bpIntel = self:GetBlueprint().Intel
self:SetIntelRadius('Jammer', 0)
self:DisableUnitIntel('Enhancement', 'Jammer')
self.RadarJammerEnh = false
self:RemoveToggleCap('RULEUTC_JammingToggle')
elseif enh =='AdvancedCoolingUpgrade' then
local wep = self:GetWeaponByLabel('RightHeavyPlasmaCannon')
wep:ChangeRateOfFire(bp.NewRateOfFire)
elseif enh =='AdvancedCoolingUpgradeRemove' then
local wep = self:GetWeaponByLabel('RightHeavyPlasmaCannon')
wep:ChangeRateOfFire(self:GetBlueprint().Weapon[1].RateOfFire or 1)
elseif enh =='HighExplosiveOrdnance' then
local wep = self:GetWeaponByLabel('RightHeavyPlasmaCannon')
wep:AddDamageRadiusMod(bp.NewDamageRadius)
wep:ChangeMaxRadius(bp.NewMaxRadius or 35)
elseif enh =='HighExplosiveOrdnanceRemove' then
local wep = self:GetWeaponByLabel('RightHeavyPlasmaCannon')
wep:AddDamageRadiusMod(bp.NewDamageRadius)
wep:ChangeMaxRadius(bp.NewMaxRadius or 25)
end)
end,

---@param self UEL0301
---@param bp UnitBlueprintEnhancement unused
ProcessEnhancementShieldGeneratorFieldRemove = function(self, bp)
self:DestroyShield()
self:SetMaintenanceConsumptionInactive()
self:RemoveToggleCap('RULEUTC_ShieldToggle')
end,

---@param self UEL0301
---@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 UEL0301
---@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 UEL0301
---@param bp UnitBlueprintEnhancement
ProcessEnhancementSensorRangeEnhancer = function(self, bp)
self:SetIntelRadius('Vision', bp.NewVisionRadius or 104)
self:SetIntelRadius('Omni', bp.NewOmniRadius or 104)
end,

---@param self UEL0301
---@param bp UnitBlueprintEnhancement unused
ProcessEnhancementSensorRangeEnhancerRemove = function(self, bp)
local bpIntel = self.Blueprint.Intel
self:SetIntelRadius('Vision', bpIntel.VisionRadius or 26)
self:SetIntelRadius('Omni', bpIntel.OmniRadius or 26)
end,

---@param self UEL0301
---@param bp UnitBlueprintEnhancement
ProcessEnhancementRadarJammer = function(self, bp)
self:SetIntelRadius('Jammer', bp.NewJammerRadius or 26)
self:EnableUnitIntel('Enhancement', 'Jammer')
self:AddToggleCap('RULEUTC_JammingToggle')
self:SetEnergyMaintenanceConsumptionOverride(bp.MaintenanceConsumptionPerSecondEnergy or 0)
self:SetMaintenanceConsumptionActive()

if self.IntelEffects then
self.IntelEffectsBag = {}
self:CreateTerrainTypeEffects(self.IntelEffects, 'FXIdle', self.Layer, nil, self.IntelEffectsBag)
end

end,

---@param self UEL0301
---@param bp UnitBlueprintEnhancement unused
ProcessEnhancementRadarJammerRemove = function(self, bp)
self.RadarJammerEnh = false
self:SetIntelRadius('Jammer', 0)
self:DisableUnitIntel('Enhancement', 'Jammer')
self:RemoveToggleCap('RULEUTC_JammingToggle')
self:SetMaintenanceConsumptionInactive()

if self.IntelEffectsBag then
EffectUtil.CleanupEffectBag(self, 'IntelEffectsBag')
end

end,

---@param self UEL0301
---@param bp UnitBlueprintEnhancement
ProcessEnhancementAdvancedCoolingUpgrade = function(self, bp)
local wep = self:GetWeaponByLabel('RightHeavyPlasmaCannon')
wep:ChangeRateOfFire(bp.NewRateOfFire)
end,

---@param self UEL0301
---@param bp UnitBlueprintEnhancement unused
ProcessEnhancementAdvancedCoolingUpgradeRemove = function(self, bp)
local wep = self:GetWeaponByLabel('RightHeavyPlasmaCannon')
wep:ChangeRateOfFire(self.Blueprint.Weapon[1].RateOfFire or 1)
end,

---@param self UEL0301
---@param bp UnitBlueprintEnhancement
ProcessEnhancementHighExplosiveOrdnance = function(self, bp)
local wep = self:GetWeaponByLabel('RightHeavyPlasmaCannon')
wep:AddDamageRadiusMod(bp.NewDamageRadius)
wep:ChangeMaxRadius(bp.NewMaxRadius or 35)
end,

---@param self UEL0301
---@param bp UnitBlueprintEnhancement
ProcessEnhancementHighExplosiveOrdnanceRemove = function(self, bp)
local wep = self:GetWeaponByLabel('RightHeavyPlasmaCannon')
wep:AddDamageRadiusMod(bp.NewDamageRadius)
wep:ChangeMaxRadius(bp.NewMaxRadius or 25)
end,

---@param self UEL0301
---@param enh string
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,

---@param self UEL0301
---@param intel IntelType
OnIntelEnabled = function(self, intel)
CommandUnit.OnIntelEnabled(self, intel)
Expand All @@ -274,7 +362,7 @@ UEL0301 = ClassUnit(CommandUnit) {
self.IntelEffectsBag = {}
self:CreateTerrainTypeEffects(self.IntelEffects, 'FXIdle', self.Layer, nil, self.IntelEffectsBag)
end
self:SetEnergyMaintenanceConsumptionOverride(self:GetBlueprint().Enhancements['RadarJammer'].MaintenanceConsumptionPerSecondEnergy or 0)
self:SetEnergyMaintenanceConsumptionOverride(self.Blueprint.Enhancements['RadarJammer'].MaintenanceConsumptionPerSecondEnergy or 0)
self:SetMaintenanceConsumptionActive()
end
end,
Expand All @@ -293,3 +381,7 @@ UEL0301 = ClassUnit(CommandUnit) {
}

TypeClass = UEL0301

--#region Mod Compatibility
local Shield = import("/lua/shield.lua").Shield
--#endregion
Loading