Merge pull request #8056 from penev92/bleed_carryalls

Added unit repair via order button with optional Carryall transport
This commit is contained in:
Oliver Brakmann
2015-05-26 12:14:02 +02:00
9 changed files with 168 additions and 46 deletions

View File

@@ -0,0 +1,51 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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. For more information,
* see COPYING.
*/
#endregion
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities
{
public class WaitForTransport : Activity
{
readonly ICallForTransport transportable;
Activity inner;
public WaitForTransport(Actor self, Activity innerActivity)
{
transportable = self.TraitOrDefault<ICallForTransport>();
inner = innerActivity;
}
public override Activity Tick(Actor self)
{
if (inner == null)
{
if (transportable != null)
transportable.MovementCancelled(self);
return NextActivity;
}
inner = Util.RunActivity(self, inner);
return this;
}
public override void Cancel(Actor self)
{
if (transportable != null)
transportable.WantsTransport = false;
inner.Cancel(self);
}
}
}

View File

@@ -115,6 +115,7 @@
<Compile Include="Activities\Turn.cs" />
<Compile Include="Activities\UnloadCargo.cs" />
<Compile Include="Activities\Wait.cs" />
<Compile Include="Activities\WaitForTransport.cs" />
<Compile Include="ActorExts.cs" />
<Compile Include="AI\AttackOrFleeFuzzy.cs" />
<Compile Include="AI\BaseBuilder.cs" />

View File

@@ -26,21 +26,35 @@ namespace OpenRA.Mods.Common.Orders
return OrderInner(world, mi);
}
IEnumerable<Order> OrderInner(World world, MouseInput mi)
static IEnumerable<Order> OrderInner(World world, MouseInput mi)
{
if (mi.Button == MouseButton.Left)
{
var underCursor = world.ScreenMap.ActorsAt(mi)
.FirstOrDefault(a => !world.FogObscures(a) && a.AppearsFriendlyTo(world.LocalPlayer.PlayerActor)
&& a.TraitsImplementing<RepairableBuilding>().Any(t => !t.IsTraitDisabled));
if (mi.Button != MouseButton.Left)
yield break;
if (underCursor == null)
yield break;
var underCursor = world.ScreenMap.ActorsAt(mi)
.FirstOrDefault(a => a.AppearsFriendlyTo(world.LocalPlayer.PlayerActor) && !world.FogObscures(a));
if (underCursor.Info.Traits.Contains<RepairableBuildingInfo>()
&& underCursor.GetDamageState() > DamageState.Undamaged)
yield return new Order("RepairBuilding", world.LocalPlayer.PlayerActor, false) { TargetActor = underCursor };
}
if (underCursor == null)
yield break;
if (underCursor.GetDamageState() == DamageState.Undamaged)
yield break;
// Repair a building.
if (underCursor.Info.Traits.Contains<RepairableBuildingInfo>())
yield return new Order("RepairBuilding", world.LocalPlayer.PlayerActor, false) { TargetActor = underCursor };
// Test for generic Repairable (used on units).
var repairable = underCursor.TraitOrDefault<Repairable>();
if (repairable == null)
yield break;
// Find a building to repair at.
var repairBuilding = repairable.FindRepairBuilding(underCursor);
if (repairBuilding == null)
yield break;
yield return new Order("Repair", underCursor, false) { TargetActor = repairBuilding };
}
public void Tick(World world)

View File

@@ -42,8 +42,7 @@ namespace OpenRA.Mods.Common.Traits
{
get
{
yield return new EnterAlliedActorTargeter<Building>("Repair", 5,
target => CanRepairAt(target), _ => CanRepair() || CanRearm());
yield return new EnterAlliedActorTargeter<Building>("Repair", 5, CanRepairAt, _ => CanRepair() || CanRearm());
}
}
@@ -92,21 +91,60 @@ namespace OpenRA.Mods.Common.Traits
self.SetTargetLine(target, Color.Green);
self.CancelActivity();
self.QueueActivity(new MoveAdjacentTo(self, target));
self.QueueActivity(movement.MoveTo(self.World.Map.CellContaining(order.TargetActor.CenterPosition), order.TargetActor));
if (CanRearmAt(order.TargetActor) && CanRearm())
self.QueueActivity(new Rearm(self));
self.QueueActivity(new WaitForTransport(self, Util.SequenceActivities(new MoveAdjacentTo(self, target),
new CallFunc(() => AfterReachActivities(self, order, movement)))));
self.QueueActivity(new Repair(order.TargetActor));
var rp = order.TargetActor.TraitOrDefault<RallyPoint>();
if (rp != null)
self.QueueActivity(new CallFunc(() =>
{
self.SetTargetLine(Target.FromCell(self.World, rp.Location), Color.Green);
self.QueueActivity(movement.MoveTo(rp.Location, order.TargetActor));
}));
TryCallTransport(self, target, new CallFunc(() => AfterReachActivities(self, order, movement)));
}
}
void AfterReachActivities(Actor self, Order order, IMove movement)
{
if (!order.TargetActor.IsInWorld || order.TargetActor.IsDead || order.TargetActor.IsDisabled())
return;
// TODO: This is hacky, but almost every single component affected
// will need to be rewritten anyway, so this is OK for now.
self.QueueActivity(movement.MoveTo(self.World.Map.CellContaining(order.TargetActor.CenterPosition), order.TargetActor));
if (CanRearmAt(order.TargetActor) && CanRearm())
self.QueueActivity(new Rearm(self));
self.QueueActivity(new Repair(order.TargetActor));
var rp = order.TargetActor.TraitOrDefault<RallyPoint>();
if (rp != null)
{
self.QueueActivity(new CallFunc(() =>
{
self.SetTargetLine(Target.FromCell(self.World, rp.Location), Color.Green);
self.QueueActivity(movement.MoveTo(rp.Location, order.TargetActor));
}));
}
}
public Actor FindRepairBuilding(Actor self)
{
var repairBuilding = self.World.ActorsWithTrait<RepairsUnits>()
.Where(a => !a.Actor.IsDead && a.Actor.IsInWorld
&& a.Actor.Owner == self.Owner &&
info.RepairBuildings.Contains(a.Actor.Info.Name))
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
// Worst case FirstOrDefault() will return a TraitPair<null, null>, which is OK.
return repairBuilding.FirstOrDefault().Actor;
}
static void TryCallTransport(Actor self, Target target, Activity nextActivity)
{
var transport = self.TraitOrDefault<ICallForTransport>();
if (transport == null)
return;
var targetCell = self.World.Map.CellContaining(target.CenterPosition);
if ((self.CenterPosition - target.CenterPosition).LengthSquared < transport.MinimumDistance.Range * transport.MinimumDistance.Range)
return;
transport.RequestTransport(targetCell, nextActivity);
}
}
}

View File

@@ -85,4 +85,12 @@ namespace OpenRA.Mods.Common.Traits
{
IEnumerable<string> PaletteNames { get; }
}
public interface ICallForTransport
{
WRange MinimumDistance { get; }
bool WantsTransport { get; set; }
void MovementCancelled(Actor self);
void RequestTransport(CPos destination, Activity afterLandActivity);
}
}

View File

@@ -18,13 +18,13 @@ namespace OpenRA.Mods.D2k.Traits
[Desc("Can be carried by units with the trait `Carryall`.")]
public class CarryableInfo : ITraitInfo
{
[Desc("Required distance away from destination before requesting a pickup.")]
public int MinDistance = 6;
[Desc("Required distance away from destination before requesting a pickup. Default is 6 cells.")]
public WRange MinDistance = WRange.FromCells(6);
public object Create(ActorInitializer init) { return new Carryable(init.Self, this); }
}
public class Carryable : IDisableMove, INotifyHarvesterAction
public class Carryable : IDisableMove, INotifyHarvesterAction, ICallForTransport
{
readonly CarryableInfo info;
readonly Actor self;
@@ -34,7 +34,7 @@ namespace OpenRA.Mods.D2k.Traits
// If we're locked there isn't much we can do. We'll have to wait for the carrier to finish with us. We should not move or get new orders!
bool locked;
public bool WantsTransport { get; private set; }
public bool WantsTransport { get; set; }
public CPos Destination;
Activity afterLandActivity;
@@ -51,9 +51,12 @@ namespace OpenRA.Mods.D2k.Traits
public void MovingToResources(Actor self, CPos targetCell, Activity next) { RequestTransport(targetCell, next); }
public void MovingToRefinery(Actor self, CPos targetCell, Activity next) { RequestTransport(targetCell, next); }
void RequestTransport(CPos destination, Activity afterLandActivity)
public WRange MinimumDistance { get { return info.MinDistance; } }
public void RequestTransport(CPos destination, Activity afterLandActivity)
{
if (destination == CPos.Zero || (self.Location - destination).Length < info.MinDistance)
var destPos = self.World.Map.CenterOfCell(destination);
if (destination == CPos.Zero || (self.CenterPosition - destPos).LengthSquared < info.MinDistance.Range * info.MinDistance.Range)
{
WantsTransport = false; // Be sure to cancel any pending transports
return;
@@ -71,12 +74,9 @@ namespace OpenRA.Mods.D2k.Traits
.Where(c => !c.Trait.IsBusy && !c.Actor.IsDead && c.Actor.Owner == self.Owner && c.Actor.IsInWorld)
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
foreach (var carrier in carriers)
{
// Notify the carrier and see if he's willing to transport us..
if (carrier.Trait.RequestTransportNotify(self))
break; // If true then we're done
}
// Is any carrier able to transport the actor?
// Any will return once it finds a carrier that returns true.
carriers.Any(carrier => carrier.Trait.RequestTransportNotify(self));
}
// No longer want to be carried
@@ -119,7 +119,8 @@ namespace OpenRA.Mods.D2k.Traits
if (Reserved)
return false;
if ((self.Location - Destination).Length < info.MinDistance)
var destPos = self.World.Map.CenterOfCell(Destination);
if ((self.CenterPosition - destPos).LengthSquared < info.MinDistance.Range * info.MinDistance.Range)
{
MovementCancelled(self);
return false;
@@ -146,7 +147,8 @@ namespace OpenRA.Mods.D2k.Traits
return false;
// Last change to change our mind...
if ((self.Location - Destination).Length < info.MinDistance)
var destPos = self.World.Map.CenterOfCell(Destination);
if ((self.CenterPosition - destPos).LengthSquared < info.MinDistance.Range * info.MinDistance.Range)
{
MovementCancelled(self);
return false;

View File

@@ -103,6 +103,9 @@ namespace OpenRA.Mods.D2k.Traits
if (!trait.WantsTransport)
return false;
if (actor.IsIdle)
return false;
return true;
})
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);

View File

@@ -53,6 +53,11 @@
Notification: EnemyUnitsDetected
Voiced:
VoiceSet: VehicleVoice
Carryable:
WithDecorationCarryable:
Image: pips
Sequence: pickup-indicator
Offset: -12, -12
^Tank:
AppearsOnRadar:
@@ -109,6 +114,11 @@
Notification: EnemyUnitsDetected
Voiced:
VoiceSet: VehicleVoice
Carryable:
WithDecorationCarryable:
Image: pips
Sequence: pickup-indicator
Offset: -12, -12
^Husk:
Health:

View File

@@ -61,11 +61,6 @@ harvester:
UnloadTicksPerBale: 5
SearchFromProcRadius: 24
SearchFromOrderRadius: 12
Carryable:
WithDecorationCarryable:
Image: pips
Sequence: pickup-indicator
Offset: -12, -12
Health:
HP: 1000
Armor: