From 29f2fefcd52c116814ea671420e71a7a7c9f039d Mon Sep 17 00:00:00 2001 From: cx-koraka <40576336+cx-koraka@users.noreply.github.com> Date: Thu, 16 Apr 2026 17:17:48 +0100 Subject: [PATCH 1/8] Add randomisation to the air unit elevation when created --- lua/sim/units/AirUnit.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lua/sim/units/AirUnit.lua b/lua/sim/units/AirUnit.lua index 1a75840b956..40c18073de3 100644 --- a/lua/sim/units/AirUnit.lua +++ b/lua/sim/units/AirUnit.lua @@ -25,6 +25,7 @@ AirUnit = ClassUnit(MobileUnit) { MobileUnitOnCreate(self) self.HasFuel = true self:AddPingPong() + self:RandomiseElevation() end, ---@param self AirUnit @@ -40,6 +41,27 @@ AirUnit = ClassUnit(MobileUnit) { end end, + --@paranm self AirUnit + RandomiseElevation = function(self) + local maxElevationDelta = 5 + local maxElevationPercentageIncrease = 5 + local maxElevationPercentageDecrease = 5 + + local blueprintPhysics = self.Blueprint.Physics + local originalElevation = blueprintPhysics.Elevation + local elevationMultiplier = math.random(100-maxElevationPercentageDecrease, 100+maxElevationPercentageIncrease) * 0.01 + + local newElevation + + if elevationMultiplier > 1 then + newElevation = math.min(originalElevation * elevationMultiplier, originalElevation + 5) + else + newElevation = math.max(originalElevation * elevationMultiplier, originalElevation - 5) + end + + self:SetElevation(newElevation) + end, + ---@param self AirUnit ---@param new VerticalMovementState ---@param old VerticalMovementState From 1fd0a288c69fb160fd91f401a87a422e5ebe9f82 Mon Sep 17 00:00:00 2001 From: cx-koraka <40576336+cx-koraka@users.noreply.github.com> Date: Thu, 16 Apr 2026 17:33:48 +0100 Subject: [PATCH 2/8] Fix param typo --- lua/sim/units/AirUnit.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/sim/units/AirUnit.lua b/lua/sim/units/AirUnit.lua index 40c18073de3..d4c090f5b0d 100644 --- a/lua/sim/units/AirUnit.lua +++ b/lua/sim/units/AirUnit.lua @@ -41,7 +41,7 @@ AirUnit = ClassUnit(MobileUnit) { end end, - --@paranm self AirUnit + --@param self AirUnit RandomiseElevation = function(self) local maxElevationDelta = 5 local maxElevationPercentageIncrease = 5 From 74a30d214ac2611b5edf944d15d4243fbf060b8d Mon Sep 17 00:00:00 2001 From: cx-koraka <40576336+cx-koraka@users.noreply.github.com> Date: Thu, 16 Apr 2026 17:39:47 +0100 Subject: [PATCH 3/8] Add snippet --- changelog/snippets/sections/features.7096.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/snippets/sections/features.7096.md diff --git a/changelog/snippets/sections/features.7096.md b/changelog/snippets/sections/features.7096.md new file mode 100644 index 00000000000..7835e72c725 --- /dev/null +++ b/changelog/snippets/sections/features.7096.md @@ -0,0 +1 @@ +- (#7095) Randomise flight height of air units, within bounds. \ No newline at end of file From be2bc8e5bd5f48fe28738f8c4766d8e347f72bf3 Mon Sep 17 00:00:00 2001 From: cx-koraka <40576336+cx-koraka@users.noreply.github.com> Date: Thu, 16 Apr 2026 18:16:29 +0100 Subject: [PATCH 4/8] Access elevation safely, use sync-safe Random() function --- lua/sim/units/AirUnit.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lua/sim/units/AirUnit.lua b/lua/sim/units/AirUnit.lua index d4c090f5b0d..c3ef4918ba5 100644 --- a/lua/sim/units/AirUnit.lua +++ b/lua/sim/units/AirUnit.lua @@ -47,9 +47,14 @@ AirUnit = ClassUnit(MobileUnit) { local maxElevationPercentageIncrease = 5 local maxElevationPercentageDecrease = 5 - local blueprintPhysics = self.Blueprint.Physics - local originalElevation = blueprintPhysics.Elevation - local elevationMultiplier = math.random(100-maxElevationPercentageDecrease, 100+maxElevationPercentageIncrease) * 0.01 ++ local blueprint = self.Blueprint ++ local blueprintPhysics = blueprint and blueprint.Physics ++ local originalElevation = blueprintPhysics and blueprintPhysics.Elevation ++ if type(originalElevation) ~= 'number' then ++ return ++ end + + local elevationMultiplier = Random(100-maxElevationPercentageDecrease, 100+maxElevationPercentageIncrease) * 0.01 local newElevation From 9a73b254cde75ae0d37e081be0e62c6cd7858743 Mon Sep 17 00:00:00 2001 From: cx-koraka <40576336+cx-koraka@users.noreply.github.com> Date: Thu, 16 Apr 2026 20:34:09 +0100 Subject: [PATCH 5/8] remove errant +'s --- lua/sim/units/AirUnit.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lua/sim/units/AirUnit.lua b/lua/sim/units/AirUnit.lua index c3ef4918ba5..51eb7d07541 100644 --- a/lua/sim/units/AirUnit.lua +++ b/lua/sim/units/AirUnit.lua @@ -47,12 +47,12 @@ AirUnit = ClassUnit(MobileUnit) { local maxElevationPercentageIncrease = 5 local maxElevationPercentageDecrease = 5 -+ local blueprint = self.Blueprint -+ local blueprintPhysics = blueprint and blueprint.Physics -+ local originalElevation = blueprintPhysics and blueprintPhysics.Elevation -+ if type(originalElevation) ~= 'number' then -+ return -+ end + local blueprint = self.Blueprint + local blueprintPhysics = blueprint and blueprint.Physics + local originalElevation = blueprintPhysics and blueprintPhysics.Elevation + if type(originalElevation) ~= 'number' then + return + end local elevationMultiplier = Random(100-maxElevationPercentageDecrease, 100+maxElevationPercentageIncrease) * 0.01 From 2844ab94eb24c8afabb825fc90bcf0c42a249b49 Mon Sep 17 00:00:00 2001 From: cx-koraka <40576336+cx-koraka@users.noreply.github.com> Date: Fri, 17 Apr 2026 11:43:41 +0100 Subject: [PATCH 6/8] add new category NORANDOMELEVATION, units with this category will not have elevation randomised --- engine/Core/Categories.lua | 3 +++ lua/sim/units/AirUnit.lua | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/engine/Core/Categories.lua b/engine/Core/Categories.lua index 1fa8f556103..cedd6ebb6df 100644 --- a/engine/Core/Categories.lua +++ b/engine/Core/Categories.lua @@ -129,6 +129,8 @@ categories = { --- Allows this unit to be built by engineers NEEDMOBILEBUILD = categoryValue, NOFORMATION = categoryValue, + --- Prevents the elevation of the unit being randomised + NORANDOMELEVATION = categoryValue, --- Prevents splash damage being applied to the entity NOSPLASHDAMAGE = categoryValue, NUKE = categoryValue, @@ -371,6 +373,7 @@ categories = { ---| "NAVAL" ---| "NEEDMOBILEBUILD" ---| "NOFORMATION" +---| "NORANDOMELEVATION" ---| "NOSPLASHDAMAGE" ---| "NUKE" ---| "NUKESUB" diff --git a/lua/sim/units/AirUnit.lua b/lua/sim/units/AirUnit.lua index 51eb7d07541..650630bf54f 100644 --- a/lua/sim/units/AirUnit.lua +++ b/lua/sim/units/AirUnit.lua @@ -48,6 +48,13 @@ AirUnit = ClassUnit(MobileUnit) { local maxElevationPercentageDecrease = 5 local blueprint = self.Blueprint + + local blueprintCategoriesHash = blueprint.CategoriesHash + + if (blueprintCategoriesHash["NORANDOMELEVATION"]) then + return + end + local blueprintPhysics = blueprint and blueprint.Physics local originalElevation = blueprintPhysics and blueprintPhysics.Elevation if type(originalElevation) ~= 'number' then @@ -59,9 +66,9 @@ AirUnit = ClassUnit(MobileUnit) { local newElevation if elevationMultiplier > 1 then - newElevation = math.min(originalElevation * elevationMultiplier, originalElevation + 5) + newElevation = math.min(originalElevation * elevationMultiplier, originalElevation + maxElevationDelta) else - newElevation = math.max(originalElevation * elevationMultiplier, originalElevation - 5) + newElevation = math.max(originalElevation * elevationMultiplier, originalElevation - maxElevationDelta) end self:SetElevation(newElevation) From 4bd64904bc0c32b6f128ed353063b8e0e9b48603 Mon Sep 17 00:00:00 2001 From: cx-koraka <40576336+cx-koraka@users.noreply.github.com> Date: Fri, 17 Apr 2026 13:13:03 +0100 Subject: [PATCH 7/8] fix typos, optimise guards --- lua/sim/units/AirUnit.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/sim/units/AirUnit.lua b/lua/sim/units/AirUnit.lua index 650630bf54f..9332c020554 100644 --- a/lua/sim/units/AirUnit.lua +++ b/lua/sim/units/AirUnit.lua @@ -41,8 +41,8 @@ AirUnit = ClassUnit(MobileUnit) { end end, - --@param self AirUnit - RandomiseElevation = function(self) + ---@param self AirUnit + RandomizeElevation = function(self) local maxElevationDelta = 5 local maxElevationPercentageIncrease = 5 local maxElevationPercentageDecrease = 5 @@ -55,9 +55,9 @@ AirUnit = ClassUnit(MobileUnit) { return end - local blueprintPhysics = blueprint and blueprint.Physics + local blueprintPhysics = blueprint.Physics local originalElevation = blueprintPhysics and blueprintPhysics.Elevation - if type(originalElevation) ~= 'number' then + if originalElevation == nil then return end From 0c18c0f3a00153ccdb2c4d2cd9404961d34dc64e Mon Sep 17 00:00:00 2001 From: cx-koraka <40576336+cx-koraka@users.noreply.github.com> Date: Fri, 17 Apr 2026 18:34:17 +0100 Subject: [PATCH 8/8] fix crash, extract vals to class level to make them hookable, change random logic to give smooth range --- lua/sim/units/AirUnit.lua | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lua/sim/units/AirUnit.lua b/lua/sim/units/AirUnit.lua index 9332c020554..01e2846f44e 100644 --- a/lua/sim/units/AirUnit.lua +++ b/lua/sim/units/AirUnit.lua @@ -20,12 +20,17 @@ AirUnit = ClassUnit(MobileUnit) { DestructionExplosionWaitDelayMax = 0, DestroyNoFallRandomChance = 0.5, + -- ELEVATION PARAMS + MaxElevationDelta = 5, + MaxElevationPercentageIncrease = 0.05, + MaxElevationPercentageDecrease = 0.05, + ---@param self AirUnit OnCreate = function(self) MobileUnitOnCreate(self) self.HasFuel = true self:AddPingPong() - self:RandomiseElevation() + self:RandomizeElevation() end, ---@param self AirUnit @@ -43,10 +48,6 @@ AirUnit = ClassUnit(MobileUnit) { ---@param self AirUnit RandomizeElevation = function(self) - local maxElevationDelta = 5 - local maxElevationPercentageIncrease = 5 - local maxElevationPercentageDecrease = 5 - local blueprint = self.Blueprint local blueprintCategoriesHash = blueprint.CategoriesHash @@ -61,14 +62,16 @@ AirUnit = ClassUnit(MobileUnit) { return end - local elevationMultiplier = Random(100-maxElevationPercentageDecrease, 100+maxElevationPercentageIncrease) * 0.01 + local minVal = 1.00 - self.MaxElevationPercentageDecrease + local maxVal = 1.00 + self.MaxElevationPercentageIncrease + local elevationMultiplier = Random() * (maxVal - minVal) + minVal local newElevation if elevationMultiplier > 1 then - newElevation = math.min(originalElevation * elevationMultiplier, originalElevation + maxElevationDelta) + newElevation = math.min(originalElevation * elevationMultiplier, originalElevation + self.MaxElevationDelta) else - newElevation = math.max(originalElevation * elevationMultiplier, originalElevation - maxElevationDelta) + newElevation = math.max(originalElevation * elevationMultiplier, originalElevation - self.MaxElevationDelta) end self:SetElevation(newElevation)