Skip to content

Commit

Permalink
Add ReplaceFirstWhenFull to Cargo
Browse files Browse the repository at this point in the history
To make combat cycle passenger replacement logic.
  • Loading branch information
MustaphaTR committed Aug 13, 2018
1 parent e9883aa commit e49bbb0
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions OpenRA.Mods.Common/Traits/Cargo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class CargoInfo : PausableConditionalTraitInfo, Requires<IOccupySpaceInfo
[Desc("A list of actor types that are initially spawned into this actor.")]
public readonly string[] InitialUnits = { };

[Desc("When cargo is full, unload the first passenger instead of disabling loading.")]
public readonly bool ReplaceFirstWhenFull = false;

[Desc("When this actor is sold should all of its passengers be unloaded?")]
public readonly bool EjectOnSell = true;

Expand Down Expand Up @@ -269,7 +272,7 @@ public string VoicePhraseForOrder(Actor self, Order order)
return Info.UnloadVoice;
}

public bool HasSpace(int weight) { return totalWeight + reservedWeight + weight <= Info.MaxWeight; }
public bool HasSpace(int weight) { return Info.ReplaceFirstWhenFull || totalWeight + reservedWeight + weight <= Info.MaxWeight; }
public bool IsEmpty(Actor self) { return cargo.Count == 0; }

public Actor Peek(Actor self) { return cargo.Peek(); }
Expand Down Expand Up @@ -343,9 +346,30 @@ PipType GetPipAt(int i)

public void Load(Actor self, Actor a)
{
cargo.Push(a);
var w = GetWeight(a);
totalWeight += w;

while (Info.ReplaceFirstWhenFull && totalWeight > Info.MaxWeight)
{
var passenger = Unload(self);
var cp = self.CenterPosition;
var inAir = self.World.Map.DistanceAboveTerrain(cp).Length != 0;
var positionable = passenger.Trait<IPositionable>();
var health = passenger.TraitOrDefault<Health>();
positionable.SetPosition(passenger, self.Location);

if (self.Owner.WinState != WinState.Lost && !inAir && positionable.CanEnterCell(self.Location, self, false))
{
self.World.AddFrameEndTask(world => world.Add(passenger));
var nbms = passenger.TraitsImplementing<INotifyBlockingMove>();
foreach (var nbm in nbms)
nbm.OnNotifyBlockingMove(passenger, passenger);
}
else
passenger.Kill(self);
}

cargo.Push(a);
if (reserves.Contains(a))
{
reservedWeight -= w;
Expand Down Expand Up @@ -390,8 +414,8 @@ void INotifyKilled.Killed(Actor self, AttackInfo e)
if (self.Owner.WinState != WinState.Lost && !inAir && positionable.CanEnterCell(self.Location, self, false))
{
self.World.AddFrameEndTask(w => w.Add(passenger));
var nbm = passenger.TraitOrDefault<INotifyBlockingMove>();
if (nbm != null)
var nbms = passenger.TraitsImplementing<INotifyBlockingMove>();
foreach (var nbm in nbms)
nbm.OnNotifyBlockingMove(passenger, passenger);

if (Info.EjectOnDeathDamage > 0 && health != null)
Expand Down

0 comments on commit e49bbb0

Please sign in to comment.