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);
}
}