Files
OpenRA/OpenRA.Mods.Common/Traits/ParaDrop.cs
RoosterDragon 5765e51c56 Fix crushables and crates causing HPF to crash.
When crushables and crates change their Location/TopLeft, their crushability is cached, but when their CenterPosition is changed, their cached crushability is not refreshed. Since their CrushableBy functions depends on IsAtGroundLevel, which depends on the CenterPosition, this means that when the crushability is cached it will depend on the current height of the object. If the height of the object changes, the cache is not refreshed and now contains out of date information.

The Locomotor cache and the HPF both cache this same information, but at different times. HPF caches immediately, but Locomotor caches on demand which means there can be a delay. This means they can have inconsistent, differing views of the crushability information. This eventually surfaces in a "The abstract path should never be searched for an unreachable point." error from HPF when it detects the inconsistency.

The bug is that Locomotor was caching information without refreshing it when required. Fixing this to refresh the cache when the CenterPosition changes is likely to have negative performance impacts. As would removing crushability from the cache. These would both be fixes that address the underlying bug.

The high impacts of a proper fix lead us to a workaround instead. If we set the CenterPosition before setting the Location, then when the Location is set and the caches are refreshed, the new CenterPosition is available when caching the crushability information. This means logic depending on IsAtGroundLevel will get the new information and cache a more up-to-date view of things. This means when changing both the CenterPosition and Location together we now cache correct information. However calls that set only the CenterPosition and not the Location can still result in a bad cache state. Although this is imperfect it is an improvement over current affairs, and has less impact.
2022-09-24 15:15:53 +02:00

124 lines
3.3 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2022 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("This unit can spawn and eject other actors while flying.")]
public class ParaDropInfo : TraitInfo, Requires<CargoInfo>
{
[Desc("Distance around the drop-point to unload troops.")]
public readonly WDist DropRange = WDist.FromCells(4);
[Desc("Wait at least this many ticks between each drop.")]
public readonly int DropInterval = 5;
[Desc("Sound to play when dropping.")]
public readonly string ChuteSound = null;
public override object Create(ActorInitializer init) { return new ParaDrop(init.Self, this); }
}
public class ParaDrop : ITick, ISync, INotifyRemovedFromWorld
{
readonly ParaDropInfo info;
readonly Actor self;
readonly Cargo cargo;
public event Action<Actor> OnRemovedFromWorld = self => { };
public event Action<Actor> OnEnteredDropRange = self => { };
public event Action<Actor> OnExitedDropRange = self => { };
[Sync]
bool inDropRange;
[Sync]
Target target;
[Sync]
int dropDelay;
bool checkForSuitableCell;
public ParaDrop(Actor self, ParaDropInfo info)
{
this.info = info;
this.self = self;
cargo = self.Trait<Cargo>();
}
public void SetLZ(CPos lz, bool checkLandingCell)
{
target = Target.FromCell(self.World, lz);
checkForSuitableCell = checkLandingCell;
}
void ITick.Tick(Actor self)
{
if (dropDelay > 0)
{
dropDelay--;
return;
}
var wasInDropRange = inDropRange;
inDropRange = target.IsInRange(self.CenterPosition, info.DropRange);
if (inDropRange && !wasInDropRange)
OnEnteredDropRange(self);
if (!inDropRange && wasInDropRange)
OnExitedDropRange(self);
// Are we able to drop the next trooper?
if (!inDropRange || cargo.IsEmpty() || !self.World.Map.Contains(self.Location))
return;
var dropActor = cargo.Peek();
var dropPositionable = dropActor.Trait<IPositionable>();
var dropCell = self.Location;
var dropSubCell = dropPositionable.GetAvailableSubCell(dropCell);
if (dropSubCell == SubCell.Invalid)
{
if (checkForSuitableCell)
return;
dropSubCell = SubCell.Any;
}
// Unload here
if (cargo.Unload(self) != dropActor)
throw new InvalidOperationException("Peeked cargo was not unloaded!");
self.World.AddFrameEndTask(w =>
{
// HACK: Call SetCenterPosition before SetPosition
// So when SetPosition calls ActorMap.CellUpdated
// the listeners see the new CenterPosition.
var dropPosition = dropActor.CenterPosition + new WVec(0, 0, self.CenterPosition.Z - dropActor.CenterPosition.Z);
dropPositionable.SetCenterPosition(dropActor, dropPosition);
dropPositionable.SetPosition(dropActor, dropCell, dropSubCell);
w.Add(dropActor);
});
Game.Sound.Play(SoundType.World, info.ChuteSound, self.CenterPosition);
dropDelay = info.DropInterval;
}
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
{
OnRemovedFromWorld(self);
}
}
}