Skip to content

Commit

Permalink
Warp wrecks of units that are off map back into the playable area (#5832
Browse files Browse the repository at this point in the history
)

The behavior only applies to skirmish maps
  • Loading branch information
lL1l1 authored Jan 21, 2024
1 parent 4ebd201 commit d8d64c7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
15 changes: 12 additions & 3 deletions lua/ScenarioFramework.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,7 @@ function AntiOffMapMainThread()
end
end
end

-- This is for bad units who choose to go off map, shame on them
function MoveOnMapThread(unit)
unit.OffMapTime = 0
Expand Down Expand Up @@ -2293,7 +2294,16 @@ end
--- Clears a unit's orders and issues a move order to the closest point on the map
---@param unit Unit
function MoveOnMap(unit)
local position = unit:GetPosition()
local nearestPoint = GetNearestPlayablePoint( unit:GetPosition() )

IssueToUnitClearCommands(unit)
IssueToUnitMove(unit, nearestPoint)
end

--- Returns the closest point on the map
---@param pos Vector
---@return Vector
function GetNearestPlayablePoint(position)
local playableArea = ScenarioInfo.PlayableArea
local nearestPoint = {position[1], position[2], position[3]}

Expand All @@ -2309,8 +2319,7 @@ function MoveOnMap(unit)
nearestPoint[3] = playableArea[4] - 5
end

IssueToUnitClearCommands(unit)
IssueToUnitMove(unit, nearestPoint)
return nearestPoint
end

--- Returns if the unit's army is human
Expand Down
9 changes: 8 additions & 1 deletion lua/sim/Unit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ local UpdateAssistersConsumptionCats = categories.REPAIR - categories.INSIGNIFIC

local DefaultTerrainType = GetTerrainType(-1, -1)

local GetNearestPlayablePoint = import("/lua/scenarioframework.lua").GetNearestPlayablePoint

--- Structures that are reused for performance reasons
--- Maps unit.techCategory to a number so we can do math on it for naval units

Expand Down Expand Up @@ -1702,6 +1704,11 @@ Unit = ClassUnit(moho.unit_methods, IntelComponent, VeterancyComponent) {
energy = energy * 0.6
end

-- Create potentially offmap wrecks on-map. Exclude campaign maps that may do weird scripted things.
if self.Brain.BrainType == 'Human' and (not ScenarioInfo.CampaignMode) then
pos = GetNearestPlayablePoint(pos)
end

local halfBuilt = self:GetFractionComplete() < 1

-- Make sure air / naval wrecks stick to ground / seabottom, unless they're in a factory.
Expand All @@ -1722,7 +1729,7 @@ Unit = ClassUnit(moho.unit_methods, IntelComponent, VeterancyComponent) {
-- Attempt to copy our animation pose to the prop. Only works if
-- the mesh and skeletons are the same, but will not produce an error if not.
if self.Tractored or (layer ~= 'Air' or (layer == "Air" and halfBuilt)) then
TryCopyPose(self, prop, true)
TryCopyPose(self, prop, false)
end

-- Create some ambient wreckage smoke
Expand Down
9 changes: 3 additions & 6 deletions lua/sim/units/AirUnit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ AirUnit = ClassUnit(MobileUnit) {
OnImpact = function(self, with)
if self.GroundImpacted then return end

-- Immediately destroy units outside the map
if not ScenarioFramework.IsUnitInPlayableArea(self) then
self:Destroy()
end

-- Only call this code once
self.GroundImpacted = true

Expand Down Expand Up @@ -181,7 +176,9 @@ AirUnit = ClassUnit(MobileUnit) {
-- Additional stupidity: An idle transport, bot loaded and unloaded, counts as 'Land' layer so it would die with the wreck hovering.
-- It also wouldn't call this code, and hence the cargo destruction. Awful!
if self:GetFractionComplete() == 1 and
(self.Layer == 'Air' or EntityCategoryContains(categories.TRANSPORTATION, self)) then
(self.Layer == 'Air' or EntityCategoryContains(categories.TRANSPORTATION, self))
then
self.Dead = true
self:CreateUnitAirDestructionEffects(1.0)
self:DestroyTopSpeedEffects()
self:DestroyBeamExhaust()
Expand Down
2 changes: 1 addition & 1 deletion lua/sim/units/MobileUnit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ MobileUnit = ClassUnit(Unit, TreadComponent) {
---@param type string
---@param overkillRatio number
OnKilled = function(self, instigator, type, overkillRatio)
-- This unit was in a transport and should create a wreck on crash
-- OnKilled will be called a second time by the transport's OnImpact
if self.killedInTransport then
self.killedInTransport = false
else
Expand Down
3 changes: 2 additions & 1 deletion projectiles/Sinker/Sinker_script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ Sinker = ClassProjectile(Projectile) {
---@param targetType string
---@param targetEntity Prop|Unit
OnImpact = function(self, targetType, targetEntity)
if targetType == 'Terrain' then
-- 'Underwater' is impacted when sinking off map for a long time.
if targetType == 'Terrain' or targetType == 'Underwater' then
self:Destroy()
if self.callback then
ForkThread(self.callback)
Expand Down

0 comments on commit d8d64c7

Please sign in to comment.