Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 changelog/snippets/sections/features.7096.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- (#7095) Randomise flight height of air units, within bounds.
3 changes: 3 additions & 0 deletions engine/Core/Categories.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -371,6 +373,7 @@ categories = {
---| "NAVAL"
---| "NEEDMOBILEBUILD"
---| "NOFORMATION"
---| "NORANDOMELEVATION"
---| "NOSPLASHDAMAGE"
---| "NUKE"
---| "NUKESUB"
Expand Down
37 changes: 37 additions & 0 deletions lua/sim/units/AirUnit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ AirUnit = ClassUnit(MobileUnit) {
DestructionExplosionWaitDelayMax = 0,
DestroyNoFallRandomChance = 0.5,

-- ELEVATION PARAMS
MaxElevationDelta = 5,
MaxElevationPercentageIncrease = 0.05,
MaxElevationPercentageDecrease = 0.05,

Comment on lines +23 to +27
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these parameters are never changed in runtime it would be better to assign them in blueprint loading (blueprints-units.lua).
Storing it as an absolute change in the blueprint may also be more convenient for authors who edit their blueprint specifically. You can use the percentage multiplication to calculate that absolute value when it doesn't exist.
Storing it in the blueprint has an added benefit of not requiring a new category to exclude units from the mechanic, since the author can just set the random offset min/max values to 0.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that per-unit control can be beneficial. I had a look at blueprints-units.lua and I am a bit confused. This seems to mostly alter and process some blueprint values, but very little actual default values. Where are the default values to the various blueprint fields specified that are used when a specific unit doesn't specify that field? I feel like the new variable definitions should go there.
I'm not yet convinced that storing it as an absolute change is really more convenient.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, I don't like switching to a proportion of the hitbox. I understand the idea, but I don't see the need to make the variations even smaller, the point of this PR is to create visible variations in the first place, and it could lead to weird effects with bulky units like gunships. Or the Soulripper for that matter. I think just using a flat percentage makes it easier to predict the results.

---@param self AirUnit
OnCreate = function(self)
MobileUnitOnCreate(self)
self.HasFuel = true
self:AddPingPong()
self:RandomizeElevation()
end,

---@param self AirUnit
Expand All @@ -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
Expand Down