Skip to content

Commit

Permalink
Fix cargo initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
abcdefg30 committed Jan 9, 2020
1 parent 308c64c commit 6562007
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions OpenRA.Mods.Common/Traits/Cargo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public class Cargo : IPips, IIssueOrder, IResolveOrder, IOrderVoice, INotifyCrea
int loadingToken = ConditionManager.InvalidConditionToken;
Stack<int> loadedTokens = new Stack<int>();
bool takeOffAfterLoad;
bool initialised;

readonly CachedTransform<CPos, IEnumerable<CPos>> currentAdjacentCells;

public IEnumerable<CPos> CurrentAdjacentCells
Expand Down Expand Up @@ -161,17 +163,6 @@ public Cargo(ActorInitializer init, CargoInfo info)
totalWeight = cargo.Sum(c => GetWeight(c));
}

foreach (var c in cargo)
{
c.Trait<Passenger>().Transport = self;

foreach (var nec in c.TraitsImplementing<INotifyEnteredCargo>())
nec.OnEnteredCargo(c, self);

foreach (var npe in self.TraitsImplementing<INotifyPassengerEntered>())
npe.OnPassengerEntered(self, c);
}

facing = Exts.Lazy(self.TraitOrDefault<IFacing>);
}

Expand All @@ -192,6 +183,23 @@ void INotifyCreated.Created(Actor self)
if (!string.IsNullOrEmpty(Info.LoadedCondition))
loadedTokens.Push(conditionManager.GrantCondition(self, Info.LoadedCondition));
}

// Defer notifications until we are certain all traits on the cargo are initialised
self.World.AddFrameEndTask(w =>
{
foreach (var c in cargo)
{
c.Trait<Passenger>().Transport = self;
foreach (var nec in c.TraitsImplementing<INotifyEnteredCargo>())
nec.OnEnteredCargo(c, self);
foreach (var npe in self.TraitsImplementing<INotifyPassengerEntered>())
npe.OnPassengerEntered(self, c);
}
initialised = true;
});
}

static int GetWeight(Actor a) { return a.Info.TraitInfo<PassengerInfo>().Weight; }
Expand Down Expand Up @@ -410,14 +418,17 @@ public void Load(Actor self, Actor a)
loadingToken = conditionManager.RevokeCondition(self, loadingToken);
}

foreach (var nec in a.TraitsImplementing<INotifyEnteredCargo>())
nec.OnEnteredCargo(a, self);
// Don't initialise (effectively twice) if this runs before the FrameEndTask from Created
if (initialised)
{
a.Trait<Passenger>().Transport = self;

foreach (var npe in self.TraitsImplementing<INotifyPassengerEntered>())
npe.OnPassengerEntered(self, a);
foreach (var nec in a.TraitsImplementing<INotifyEnteredCargo>())
nec.OnEnteredCargo(a, self);

var p = a.Trait<Passenger>();
p.Transport = self;
foreach (var npe in self.TraitsImplementing<INotifyPassengerEntered>())
npe.OnPassengerEntered(self, a);
}

string passengerCondition;
if (conditionManager != null && Info.PassengerConditions.TryGetValue(a.Info.Name, out passengerCondition))
Expand Down

0 comments on commit 6562007

Please sign in to comment.