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 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 1a75840b956..01e2846f44e 100644 --- a/lua/sim/units/AirUnit.lua +++ b/lua/sim/units/AirUnit.lua @@ -20,11 +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:RandomizeElevation() end, ---@param self AirUnit @@ -40,6 +46,37 @@ AirUnit = ClassUnit(MobileUnit) { end end, + ---@param self AirUnit + RandomizeElevation = function(self) + local blueprint = self.Blueprint + + local blueprintCategoriesHash = blueprint.CategoriesHash + + if (blueprintCategoriesHash["NORANDOMELEVATION"]) then + return + end + + local blueprintPhysics = blueprint.Physics + local originalElevation = blueprintPhysics and blueprintPhysics.Elevation + if originalElevation == nil then + return + end + + 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 + self.MaxElevationDelta) + else + newElevation = math.max(originalElevation * elevationMultiplier, originalElevation - self.MaxElevationDelta) + end + + self:SetElevation(newElevation) + end, + ---@param self AirUnit ---@param new VerticalMovementState ---@param old VerticalMovementState