Skip to content

Commit

Permalink
Add Lua Scripting for Carryall.
Browse files Browse the repository at this point in the history
  • Loading branch information
MustaphaTR committed Apr 4, 2021
1 parent 35a0a3c commit 56470c1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 14 deletions.
11 changes: 7 additions & 4 deletions OpenRA.Mods.Common/Activities/DeliverUnit.cs
Expand Up @@ -23,19 +23,21 @@ public class DeliverUnit : Activity
readonly BodyOrientation body;
readonly bool assignTargetOnFirstRun;
readonly WDist deliverRange;
readonly Color? targetLineColor;

Target destination;

public DeliverUnit(Actor self, WDist deliverRange)
: this(self, Target.Invalid, deliverRange)
public DeliverUnit(Actor self, WDist deliverRange, Color? targetLineColor)
: this(self, Target.Invalid, deliverRange, targetLineColor)
{
assignTargetOnFirstRun = true;
}

public DeliverUnit(Actor self, in Target destination, WDist deliverRange)
public DeliverUnit(Actor self, in Target destination, WDist deliverRange, Color? targetLineColor)
{
this.destination = destination;
this.deliverRange = deliverRange;
this.targetLineColor = targetLineColor;

carryall = self.Trait<Carryall>();
body = self.Trait<BodyOrientation>();
Expand All @@ -60,7 +62,8 @@ protected override void OnFirstRun(Actor self)

public override IEnumerable<TargetLineNode> TargetLineNodes(Actor self)
{
yield return new TargetLineNode(destination, carryall.Info.TargetLineColor);
if (targetLineColor != null)
yield return new TargetLineNode(destination, targetLineColor.Value);
}

class ReleaseUnit : Activity
Expand Down
7 changes: 5 additions & 2 deletions OpenRA.Mods.Common/Activities/PickupUnit.cs
Expand Up @@ -26,17 +26,19 @@ public class PickupUnit : Activity
readonly BodyOrientation carryableBody;

readonly int delay;
readonly Color? targetLineColor;

// TODO: Expose this to yaml
readonly WDist targetLockRange = WDist.FromCells(4);

enum PickupState { Intercept, LockCarryable, Pickup }
PickupState state = PickupState.Intercept;

public PickupUnit(Actor self, Actor cargo, int delay)
public PickupUnit(Actor self, Actor cargo, int delay, Color? targetLineColor)
{
this.cargo = cargo;
this.delay = delay;
this.targetLineColor = targetLineColor;
carryable = cargo.Trait<Carryable>();
carryableFacing = cargo.Trait<IFacing>();
carryableBody = cargo.Trait<BodyOrientation>();
Expand Down Expand Up @@ -116,7 +118,8 @@ public override bool Tick(Actor self)

public override IEnumerable<TargetLineNode> TargetLineNodes(Actor self)
{
yield return new TargetLineNode(Target.FromActor(cargo), carryall.Info.TargetLineColor);
if (targetLineColor != null)
yield return new TargetLineNode(Target.FromActor(cargo), targetLineColor.Value);
}

class AttachUnit : Activity
Expand Down
49 changes: 49 additions & 0 deletions OpenRA.Mods.Common/Scripting/Properties/CarryallProperties.cs
@@ -0,0 +1,49 @@
#region Copyright & License Information
/*
* Copyright 2007-2021 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 Eluant;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Scripting;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Scripting
{
[ScriptPropertyGroup("Ability")]
public class CarryallProperties : ScriptActorProperties, Requires<CarryallInfo>
{
readonly Carryall carryall;

public CarryallProperties(ScriptContext context, Actor self)
: base(context, self)
{
carryall = Self.Trait<Carryall>();
}

[ScriptActorPropertyActivity]
[Desc("Pick up the target actor.")]
public void PickupCarryable(Actor target)
{
var carryable = target.TraitOrDefault<Carryable>();
if (carryable == null)
throw new LuaException("Actor '{0}' cannot carry actor '{1}'!".F(Self, target));

Self.QueueActivity(new PickupUnit(Self, target, carryall.Info.BeforeLoadDelay, null));
}

[ScriptActorPropertyActivity]
[Desc("Drop the actor being carried at the target location.")]
public void DeliverCarryable(CPos target)
{
Self.QueueActivity(new DeliverUnit(Self, Target.FromCell(Self.World, target), carryall.Info.DropRange, null));
}
}
}
7 changes: 4 additions & 3 deletions OpenRA.Mods.Common/Traits/AutoCarryall.cs
Expand Up @@ -114,18 +114,19 @@ public FerryUnit(Actor self, Actor cargo)
protected override void OnFirstRun(Actor self)
{
if (!cargo.IsDead)
QueueChild(new PickupUnit(self, cargo, 0));
QueueChild(new PickupUnit(self, cargo, 0, self.Trait<Carryall>().Info.TargetLineColor));
}

public override bool Tick(Actor self)
{
if (cargo.IsDead)
return true;

var dropRange = self.Trait<Carryall>().Info.DropRange;
var carryall = self.Trait<Carryall>().Info;
var dropRange = carryall.DropRange;
var destination = cargo.Trait<Carryable>().Destination;
if (destination != null)
self.QueueActivity(true, new DeliverUnit(self, Target.FromCell(self.World, destination.Value), dropRange));
self.QueueActivity(true, new DeliverUnit(self, Target.FromCell(self.World, destination.Value), dropRange, carryall.TargetLineColor));

return true;
}
Expand Down
5 changes: 3 additions & 2 deletions OpenRA.Mods.Common/Traits/Buildings/FreeActorWithDelivery.cs
Expand Up @@ -65,8 +65,9 @@ public void DoDelivery(CPos location, string actorName, string carrierActorName)
var carryable = cargo.Trait<Carryable>();
carryable.Reserve(cargo, carrier);

carrier.Trait<Carryall>().AttachCarryable(carrier, cargo);
carrier.QueueActivity(new DeliverUnit(carrier, Target.FromCell(self.World, location), info.DeliveryRange));
var carryall = carrier.Trait<Carryall>();
carryall.AttachCarryable(carrier, cargo);
carrier.QueueActivity(new DeliverUnit(carrier, Target.FromCell(self.World, location), info.DeliveryRange, carryall.Info.TargetLineColor));
carrier.QueueActivity(new Fly(carrier, Target.FromCell(self.World, self.World.Map.ChooseRandomEdgeCell(self.World.SharedRandom))));
carrier.QueueActivity(new RemoveSelf());

Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Mods.Common/Traits/Carryall.cs
Expand Up @@ -333,22 +333,22 @@ void IResolveOrder.ResolveOrder(Actor self, Order order)
return;

var targetLocation = move.NearestMoveableCell(cell);
self.QueueActivity(order.Queued, new DeliverUnit(self, order.Target, Info.DropRange));
self.QueueActivity(order.Queued, new DeliverUnit(self, order.Target, Info.DropRange, Info.TargetLineColor));
self.ShowTargetLines();
}
else if (order.OrderString == "Unload")
{
if (!order.Queued && !CanUnload())
return;

self.QueueActivity(order.Queued, new DeliverUnit(self, Info.DropRange));
self.QueueActivity(order.Queued, new DeliverUnit(self, Info.DropRange, Info.TargetLineColor));
}
else if (order.OrderString == "PickupUnit")
{
if (order.Target.Type != TargetType.Actor)
return;

self.QueueActivity(order.Queued, new PickupUnit(self, order.Target.Actor, Info.BeforeLoadDelay));
self.QueueActivity(order.Queued, new PickupUnit(self, order.Target.Actor, Info.BeforeLoadDelay, Info.TargetLineColor));
self.ShowTargetLines();
}
}
Expand Down

0 comments on commit 56470c1

Please sign in to comment.