Cargo improvements

This commit is contained in:
Voidwalker
2019-03-10 14:54:09 +01:00
committed by Oliver Brakmann
parent 30de4df749
commit a46ec5d930
2 changed files with 27 additions and 17 deletions

View File

@@ -80,7 +80,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
public readonly CargoInfo Info; public readonly CargoInfo Info;
readonly Actor self; readonly Actor self;
readonly Stack<Actor> cargo = new Stack<Actor>(); readonly List<Actor> cargo = new List<Actor>();
readonly HashSet<Actor> reserves = new HashSet<Actor>(); readonly HashSet<Actor> reserves = new HashSet<Actor>();
readonly Dictionary<string, Stack<int>> passengerTokens = new Dictionary<string, Stack<int>>(); readonly Dictionary<string, Stack<int>> passengerTokens = new Dictionary<string, Stack<int>>();
readonly Lazy<IFacing> facing; readonly Lazy<IFacing> facing;
@@ -108,7 +108,7 @@ namespace OpenRA.Mods.Common.Traits
if (init.Contains<RuntimeCargoInit>()) if (init.Contains<RuntimeCargoInit>())
{ {
cargo = new Stack<Actor>(init.Get<RuntimeCargoInit, Actor[]>()); cargo = new List<Actor>(init.Get<RuntimeCargoInit, Actor[]>());
totalWeight = cargo.Sum(c => GetWeight(c)); totalWeight = cargo.Sum(c => GetWeight(c));
} }
else if (init.Contains<CargoInit>()) else if (init.Contains<CargoInit>())
@@ -118,7 +118,7 @@ namespace OpenRA.Mods.Common.Traits
var unit = self.World.CreateActor(false, u.ToLowerInvariant(), var unit = self.World.CreateActor(false, u.ToLowerInvariant(),
new TypeDictionary { new OwnerInit(self.Owner) }); new TypeDictionary { new OwnerInit(self.Owner) });
cargo.Push(unit); cargo.Add(unit);
} }
totalWeight = cargo.Sum(c => GetWeight(c)); totalWeight = cargo.Sum(c => GetWeight(c));
@@ -130,7 +130,7 @@ namespace OpenRA.Mods.Common.Traits
var unit = self.World.CreateActor(false, u.ToLowerInvariant(), var unit = self.World.CreateActor(false, u.ToLowerInvariant(),
new TypeDictionary { new OwnerInit(self.Owner) }); new TypeDictionary { new OwnerInit(self.Owner) });
cargo.Push(unit); cargo.Add(unit);
} }
totalWeight = cargo.Sum(c => GetWeight(c)); totalWeight = cargo.Sum(c => GetWeight(c));
@@ -271,33 +271,35 @@ namespace OpenRA.Mods.Common.Traits
public bool HasSpace(int weight) { return totalWeight + reservedWeight + weight <= Info.MaxWeight; } public bool HasSpace(int weight) { return totalWeight + reservedWeight + weight <= Info.MaxWeight; }
public bool IsEmpty(Actor self) { return cargo.Count == 0; } public bool IsEmpty(Actor self) { return cargo.Count == 0; }
public Actor Peek(Actor self) { return cargo.Peek(); } public Actor Peek(Actor self) { return cargo.Last(); }
public Actor Unload(Actor self) public Actor Unload(Actor self, Actor passenger = null)
{ {
var a = cargo.Pop(); passenger = passenger ?? cargo.Last();
if (!cargo.Remove(passenger))
throw new ArgumentException("Attempted to unload an actor that is not a passenger.");
totalWeight -= GetWeight(a); totalWeight -= GetWeight(passenger);
SetPassengerFacing(a); SetPassengerFacing(passenger);
foreach (var npe in self.TraitsImplementing<INotifyPassengerExited>()) foreach (var npe in self.TraitsImplementing<INotifyPassengerExited>())
npe.OnPassengerExited(self, a); npe.OnPassengerExited(self, passenger);
foreach (var nec in a.TraitsImplementing<INotifyExitedCargo>()) foreach (var nec in passenger.TraitsImplementing<INotifyExitedCargo>())
nec.OnExitedCargo(a, self); nec.OnExitedCargo(passenger, self);
var p = a.Trait<Passenger>(); var p = passenger.Trait<Passenger>();
p.Transport = null; p.Transport = null;
Stack<int> passengerToken; Stack<int> passengerToken;
if (passengerTokens.TryGetValue(a.Info.Name, out passengerToken) && passengerToken.Any()) if (passengerTokens.TryGetValue(passenger.Info.Name, out passengerToken) && passengerToken.Any())
conditionManager.RevokeCondition(self, passengerToken.Pop()); conditionManager.RevokeCondition(self, passengerToken.Pop());
if (loadedTokens.Any()) if (loadedTokens.Any())
conditionManager.RevokeCondition(self, loadedTokens.Pop()); conditionManager.RevokeCondition(self, loadedTokens.Pop());
return a; return passenger;
} }
void SetPassengerFacing(Actor passenger) void SetPassengerFacing(Actor passenger)
@@ -339,7 +341,7 @@ namespace OpenRA.Mods.Common.Traits
public void Load(Actor self, Actor a) public void Load(Actor self, Actor a)
{ {
cargo.Push(a); cargo.Add(a);
var w = GetWeight(a); var w = GetWeight(a);
totalWeight += w; totalWeight += w;
if (reserves.Contains(a)) if (reserves.Contains(a))

View File

@@ -51,7 +51,7 @@ namespace OpenRA.Mods.Common.Traits
public object Create(ActorInitializer init) { return new Passenger(this); } public object Create(ActorInitializer init) { return new Passenger(this); }
} }
public class Passenger : INotifyCreated, IIssueOrder, IResolveOrder, IOrderVoice, INotifyRemovedFromWorld, INotifyEnteredCargo, INotifyExitedCargo public class Passenger : INotifyCreated, IIssueOrder, IResolveOrder, IOrderVoice, INotifyRemovedFromWorld, INotifyEnteredCargo, INotifyExitedCargo, INotifyKilled
{ {
public readonly PassengerInfo Info; public readonly PassengerInfo Info;
public Actor Transport; public Actor Transport;
@@ -183,5 +183,13 @@ namespace OpenRA.Mods.Common.Traits
ReservedCargo.UnreserveSpace(self); ReservedCargo.UnreserveSpace(self);
ReservedCargo = null; ReservedCargo = null;
} }
void INotifyKilled.Killed(Actor self, AttackInfo e)
{
if (Transport == null)
return;
Transport.Trait<Cargo>().Unload(Transport, self);
}
} }
} }