forked from OpenRA/OpenRA
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7862603
commit fe04e22
Showing
8 changed files
with
790 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#region Copyright & License Information | ||
/* | ||
* Copyright 2007-2018 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.Mods.Common.Traits; | ||
using OpenRA.Traits; | ||
|
||
namespace OpenRA.Mods.Common.Activities | ||
{ | ||
public class EnterSharedTransport : Enter | ||
{ | ||
readonly SharedPassenger passenger; | ||
readonly int maxTries; | ||
Actor transport; | ||
SharedCargo cargo; | ||
|
||
public EnterSharedTransport(Actor self, Actor transport, int maxTries = 0, bool repathWhileMoving = true) | ||
: base(self, transport, EnterBehaviour.Exit, WDist.Zero, maxTries, repathWhileMoving) | ||
{ | ||
this.transport = transport; | ||
this.maxTries = maxTries; | ||
cargo = transport.Trait<SharedCargo>(); | ||
passenger = self.Trait<SharedPassenger>(); | ||
} | ||
|
||
protected override void Unreserve(Actor self, bool abort) { passenger.Unreserve(self); } | ||
protected override bool CanReserve(Actor self) { return cargo.Unloading || cargo.CanLoad(transport, self); } | ||
protected override ReserveStatus Reserve(Actor self) | ||
{ | ||
var status = base.Reserve(self); | ||
if (status != ReserveStatus.Ready) | ||
return status; | ||
if (passenger.Reserve(self, cargo)) | ||
return ReserveStatus.Ready; | ||
return ReserveStatus.Pending; | ||
} | ||
|
||
protected override void OnInside(Actor self) | ||
{ | ||
self.World.AddFrameEndTask(w => | ||
{ | ||
if (self.IsDead || transport.IsDead || !cargo.CanLoad(transport, self)) | ||
return; | ||
cargo.Load(transport, self); | ||
w.Remove(self); | ||
}); | ||
|
||
Done(self); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#region Copyright & License Information | ||
/* | ||
* Copyright 2007-2018 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.Collections.Generic; | ||
using System.Drawing; | ||
using System.Linq; | ||
using OpenRA.Activities; | ||
using OpenRA.Mods.Common.Traits; | ||
using OpenRA.Primitives; | ||
using OpenRA.Traits; | ||
|
||
namespace OpenRA.Mods.Common.Activities | ||
{ | ||
public class UnloadSharedCargo : Activity | ||
{ | ||
readonly Actor self; | ||
readonly SharedCargo cargo; | ||
readonly INotifyUnload[] notifiers; | ||
readonly bool unloadAll; | ||
|
||
public UnloadSharedCargo(Actor self, bool unloadAll) | ||
{ | ||
this.self = self; | ||
cargo = self.Trait<SharedCargo>(); | ||
notifiers = self.TraitsImplementing<INotifyUnload>().ToArray(); | ||
this.unloadAll = unloadAll; | ||
} | ||
|
||
public Pair<CPos, SubCell>? ChooseExitSubCell(Actor passenger) | ||
{ | ||
var pos = passenger.Trait<IPositionable>(); | ||
|
||
return cargo.CurrentAdjacentCells | ||
.Shuffle(self.World.SharedRandom) | ||
.Select(c => Pair.New(c, pos.GetAvailableSubCell(c))) | ||
.Cast<Pair<CPos, SubCell>?>() | ||
.FirstOrDefault(s => s.Value.Second != SubCell.Invalid); | ||
} | ||
|
||
IEnumerable<CPos> BlockedExitCells(Actor passenger) | ||
{ | ||
var pos = passenger.Trait<IPositionable>(); | ||
|
||
// Find the cells that are blocked by transient actors | ||
return cargo.CurrentAdjacentCells | ||
.Where(c => pos.CanEnterCell(c, null, true) != pos.CanEnterCell(c, null, false)); | ||
} | ||
|
||
public override Activity Tick(Actor self) | ||
{ | ||
cargo.Unloading = false; | ||
if (IsCanceled || cargo.Manager.IsEmpty()) | ||
return NextActivity; | ||
|
||
foreach (var inu in notifiers) | ||
inu.Unloading(self); | ||
|
||
var actor = cargo.Peek(self); | ||
var spawn = self.CenterPosition; | ||
|
||
var exitSubCell = ChooseExitSubCell(actor); | ||
if (exitSubCell == null) | ||
{ | ||
self.NotifyBlocker(BlockedExitCells(actor)); | ||
|
||
return ActivityUtils.SequenceActivities(new Wait(10), this); | ||
} | ||
|
||
cargo.Unload(self); | ||
self.World.AddFrameEndTask(w => | ||
{ | ||
if (actor.Disposed) | ||
return; | ||
var move = actor.Trait<IMove>(); | ||
var pos = actor.Trait<IPositionable>(); | ||
actor.CancelActivity(); | ||
pos.SetVisualPosition(actor, spawn); | ||
actor.QueueActivity(move.MoveIntoWorld(actor, exitSubCell.Value.First, exitSubCell.Value.Second)); | ||
actor.SetTargetLine(Target.FromCell(w, exitSubCell.Value.First, exitSubCell.Value.Second), Color.Green, false); | ||
w.Add(actor); | ||
}); | ||
|
||
if (!unloadAll || cargo.Manager.IsEmpty()) | ||
return NextActivity; | ||
|
||
cargo.Unloading = true; | ||
return this; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using OpenRA.Mods.Common.Traits; | ||
using OpenRA.Traits; | ||
|
||
namespace OpenRA.Mods.Common.Orders | ||
{ | ||
public class EnterSharedTransportTargeter : EnterAlliedActorTargeter<SharedCargoInfo> | ||
{ | ||
public EnterSharedTransportTargeter(string order, int priority, | ||
Func<Actor, bool> canTarget, Func<Actor, bool> useEnterCursor) | ||
: base(order, priority, canTarget, useEnterCursor) { } | ||
|
||
public override bool CanTargetActor(Actor self, Actor target, TargetModifiers modifiers, ref string cursor) | ||
{ | ||
return base.CanTargetActor(self, target, modifiers, ref cursor); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#region Copyright & License Information | ||
/* | ||
* Copyright 2007-2018 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.Collections.Generic; | ||
using OpenRA.Traits; | ||
|
||
namespace OpenRA.Mods.Common.Traits | ||
{ | ||
[Desc("Manages the contents of shared cargos like GLA Tunnel Networks")] | ||
public class SharedCargoManagerInfo : ITraitInfo | ||
{ | ||
[Desc("Type of shared cargo")] | ||
public readonly string Type = "tunnel"; | ||
|
||
[Desc("The maximum sum of Passenger.Weight that this actor can support.")] | ||
public readonly int MaxWeight = 0; | ||
|
||
public object Create(ActorInitializer init) { return new SharedCargoManager(init.Self, this); } | ||
} | ||
|
||
public class SharedCargoManager | ||
{ | ||
public SharedCargoManagerInfo Info; | ||
public Stack<Actor> Cargo = new Stack<Actor>(); | ||
public HashSet<Actor> Reserves = new HashSet<Actor>(); | ||
|
||
public IEnumerable<Actor> Passengers { get { return Cargo; } } | ||
public int PassengerCount { get { return Cargo.Count; } } | ||
|
||
public int TotalWeight = 0; | ||
public int ReservedWeight = 0; | ||
|
||
public SharedCargoManager(Actor self, SharedCargoManagerInfo info) | ||
{ | ||
Info = info; | ||
} | ||
|
||
public bool HasSpace(int weight) { return TotalWeight + ReservedWeight + weight <= Info.MaxWeight; } | ||
public bool IsEmpty() { return Cargo.Count == 0; } | ||
|
||
public void Clear(AttackInfo e = null) | ||
{ | ||
foreach (var passenger in Cargo) | ||
if (e != null) | ||
passenger.Kill(e.Attacker, e.Damage.DamageTypes); | ||
else | ||
passenger.Dispose(); | ||
|
||
Cargo.Clear(); | ||
TotalWeight = 0; | ||
} | ||
} | ||
} |
Oops, something went wrong.