Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Lua Scripting for Carryall. #19312

Merged
merged 1 commit into from Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions OpenRA.Mods.Common/Activities/DeliverUnit.cs
Expand Up @@ -12,6 +12,7 @@
using System.Collections.Generic;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Activities
Expand All @@ -22,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 @@ -59,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
8 changes: 6 additions & 2 deletions OpenRA.Mods.Common/Activities/PickupUnit.cs
Expand Up @@ -12,6 +12,7 @@
using System.Collections.Generic;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Activities
Expand All @@ -25,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 @@ -115,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));
}
}
}
12 changes: 8 additions & 4 deletions OpenRA.Mods.Common/Traits/AutoCarryall.cs
Expand Up @@ -105,27 +105,31 @@ void FindCarryableForTransport(Actor self)
class FerryUnit : Activity
{
readonly Actor cargo;
readonly Carryable carryable;
readonly CarryallInfo carryallInfo;

public FerryUnit(Actor self, Actor cargo)
{
this.cargo = cargo;
carryable = cargo.Trait<Carryable>();
carryallInfo = self.Trait<Carryall>().Info;
}

protected override void OnFirstRun(Actor self)
{
if (!cargo.IsDead)
QueueChild(new PickupUnit(self, cargo, 0));
QueueChild(new PickupUnit(self, cargo, 0, carryallInfo.TargetLineColor));
}

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

var dropRange = self.Trait<Carryall>().Info.DropRange;
var destination = cargo.Trait<Carryable>().Destination;
var dropRange = carryallInfo.DropRange;
var destination = 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, carryallInfo.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