Skip to content

Commit

Permalink
Allow queued structure rallypoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
tovl authored and abcdefg30 committed Dec 13, 2019
1 parent 3236499 commit 203fff0
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 40 deletions.
6 changes: 4 additions & 2 deletions OpenRA.Mods.Common/Activities/Resupply.cs
Expand Up @@ -188,7 +188,8 @@ void OnResupplyEnding(Actor self, bool isHostInvalid = false)
if (wasRepaired || isHostInvalid || (!stayOnResupplier && aircraft.Info.TakeOffOnResupply))
{
if (self.CurrentActivity.NextActivity == null && rp != null)
QueueChild(move.MoveTo(rp.Location, ignoreActor: repairableNear != null ? null : host.Actor, targetLineColor: Color.Green));
foreach (var cell in rp.Path)
QueueChild(move.MoveTo(cell, 1, ignoreActor: repairableNear != null ? null : host.Actor, targetLineColor: Color.Green));
else
QueueChild(new TakeOff(self));

Expand All @@ -208,7 +209,8 @@ void OnResupplyEnding(Actor self, bool isHostInvalid = false)
if (self.CurrentActivity.NextActivity == null)
{
if (rp != null)
QueueChild(move.MoveTo(rp.Location, ignoreActor: repairableNear != null ? null : host.Actor));
foreach (var cell in rp.Path)
QueueChild(move.MoveTo(cell, 1, repairableNear != null ? null : host.Actor, true));
else if (repairableNear == null)
QueueChild(move.MoveToTarget(self, host));
}
Expand Down
34 changes: 23 additions & 11 deletions OpenRA.Mods.Common/Effects/RallyPointIndicator.cs
Expand Up @@ -25,8 +25,8 @@ class RallyPointIndicator : IEffect, IEffectAboveShroud, IEffectAnnotation
readonly Animation circles;
readonly ExitInfo[] exits;

readonly WPos[] targetLine = new WPos[2];
CPos cachedLocation;
List<WPos> targetLineNodes = new List<WPos> { };
List<CPos> cachedLocations;

public RallyPointIndicator(Actor building, RallyPoint rp, ExitInfo[] exits)
{
Expand All @@ -52,28 +52,29 @@ void IEffect.Tick(World world)
if (circles != null)
circles.Tick();

if (cachedLocation != rp.Location)
if (cachedLocations == null || !cachedLocations.SequenceEqual(rp.Path))
{
cachedLocation = rp.Location;
cachedLocations = new List<CPos>(rp.Path);
targetLineNodes.Clear();
foreach (var c in cachedLocations)
targetLineNodes.Add(world.Map.CenterOfCell(c));

var rallyPos = world.Map.CenterOfCell(cachedLocation);
var exitPos = building.CenterPosition;

// Find closest exit
var dist = int.MaxValue;
foreach (var exit in exits)
{
var ep = building.CenterPosition + exit.SpawnOffset;
var len = (rallyPos - ep).Length;
var len = (targetLineNodes[0] - ep).Length;
if (len < dist)
{
dist = len;
exitPos = ep;
}
}

targetLine[0] = exitPos;
targetLine[1] = rallyPos;
targetLineNodes.Insert(0, exitPos);

if (circles != null)
circles.Play(rp.Info.CirclesSequence);
Expand All @@ -98,10 +99,10 @@ IEnumerable<IRenderable> IEffectAboveShroud.RenderAboveShroud(WorldRenderer wr)
{
var palette = wr.Palette(rp.PaletteName);
if (circles != null)
renderables = renderables.Concat(circles.Render(targetLine[1], palette));
renderables = renderables.Concat(circles.Render(targetLineNodes.Last(), palette));

if (flag != null)
renderables = renderables.Concat(flag.Render(targetLine[1], palette));
renderables = renderables.Concat(flag.Render(targetLineNodes.Last(), palette));
}

return renderables;
Expand All @@ -118,7 +119,18 @@ IEnumerable<IRenderable> IEffectAnnotation.RenderAnnotation(WorldRenderer wr)
if (!building.World.Selection.Contains(building))
return SpriteRenderable.None;

return new IRenderable[] { new TargetLineRenderable(targetLine, building.Owner.Color, rp.Info.LineWidth) };
return RenderInner(wr);
}

IEnumerable<IRenderable> RenderInner(WorldRenderer wr)
{
var prev = targetLineNodes[0];
foreach (var pos in targetLineNodes.Skip(1))
{
var targetLine = new[] { prev, pos };
prev = pos;
yield return new TargetLineRenderable(targetLine, building.Owner.Color, rp.Info.LineWidth);
}
}
}
}
Expand Up @@ -79,8 +79,8 @@ public RallyPointProperties(ScriptContext context, Actor self)
[Desc("Query or set a factory's rally point.")]
public CPos RallyPoint
{
get { return rp.Location; }
set { rp.Location = value; }
get { return rp.Path.Last(); }
set { rp.Path = new List<CPos> { value }; }
}
}

Expand Down
Expand Up @@ -229,7 +229,7 @@ void SetRallyPointsForNewProductionBuildings(IBot bot)
foreach (var rp in world.ActorsWithTrait<RallyPoint>())
{
if (rp.Actor.Owner == player &&
!IsRallyPointValid(rp.Trait.Location, rp.Actor.Info.TraitInfoOrDefault<BuildingInfo>()))
!IsRallyPointValid(rp.Trait.Path[0], rp.Actor.Info.TraitInfoOrDefault<BuildingInfo>()))
{
bot.QueueOrder(new Order("SetRallyPoint", rp.Actor, Target.FromCell(world, ChooseRallyLocationNear(rp.Actor)), false)
{
Expand Down
27 changes: 16 additions & 11 deletions OpenRA.Mods.Common/Traits/Buildings/RallyPoint.cs
Expand Up @@ -48,23 +48,22 @@ public class RallyPoint : IIssueOrder, IResolveOrder, ISync, INotifyOwnerChanged
{
const string OrderID = "SetRallyPoint";

[Sync]
public CPos Location;
public List<CPos> Path;

public RallyPointInfo Info;
public string PaletteName { get; private set; }

const uint ForceSet = 1;

public void ResetLocation(Actor self)
public void ResetPath(Actor self)
{
Location = self.Location + Info.Offset;
Path = new List<CPos> { self.Location + Info.Offset };
}

public RallyPoint(Actor self, RallyPointInfo info)
{
Info = info;
ResetLocation(self);
ResetPath(self);
PaletteName = info.IsPlayerPalette ? info.Palette + self.Owner.InternalName : info.Palette;
}

Expand All @@ -84,7 +83,7 @@ public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
if (Info.IsPlayerPalette)
PaletteName = Info.Palette + newOwner.InternalName;

ResetLocation(self);
ResetPath(self);
}

public IEnumerable<IOrderTargeter> Orders
Expand All @@ -96,7 +95,7 @@ public Order IssueOrder(Actor self, IOrderTargeter order, Target target, bool qu
{
if (order.OrderID == OrderID)
{
return new Order(order.OrderID, self, target, false)
return new Order(order.OrderID, self, target, queued)
{
SuppressVisualFeedback = true,
ExtraData = ((RallyPointOrderTargeter)order).ForceSet ? ForceSet : 0
Expand All @@ -108,8 +107,13 @@ public Order IssueOrder(Actor self, IOrderTargeter order, Target target, bool qu

public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == OrderID)
Location = self.World.Map.CellContaining(order.Target.CenterPosition);
if (order.OrderString != OrderID)
return;

if (!order.Queued)
Path.Clear();

Path.Add(self.World.Map.CellContaining(order.Target.CenterPosition));
}

public static bool IsForceSet(Order order)
Expand All @@ -130,12 +134,15 @@ public RallyPointOrderTargeter(string cursor)
public int OrderPriority { get { return 0; } }
public bool TargetOverridesSelection(Actor self, Target target, List<Actor> actorsAt, CPos xy, TargetModifiers modifiers) { return true; }
public bool ForceSet { get; private set; }
public bool IsQueued { get; protected set; }

public bool CanTarget(Actor self, Target target, List<Actor> othersAtTarget, ref TargetModifiers modifiers, ref string cursor)
{
if (target.Type != TargetType.Terrain)
return false;

IsQueued = modifiers.HasModifier(TargetModifiers.ForceQueue);

var location = self.World.Map.CellContaining(target.CenterPosition);
if (self.World.Map.Contains(location))
{
Expand All @@ -154,8 +161,6 @@ public bool CanTarget(Actor self, Target target, List<Actor> othersAtTarget, ref

return false;
}

public bool IsQueued { get { return false; } } // unused
}
}
}
3 changes: 2 additions & 1 deletion OpenRA.Mods.Common/Traits/Buildings/Reservable.cs
Expand Up @@ -85,7 +85,8 @@ void UnReserve(Actor self)
if (reservedForAircraft.GetActorBelow() == self)
{
if (rallyPoint != null)
reservedFor.QueueActivity(reservedForAircraft.MoveTo(rallyPoint.Location, null, targetLineColor: Color.Green));
foreach (var cell in rallyPoint.Path)
reservedFor.QueueActivity(reservedForAircraft.MoveTo(cell, 1, targetLineColor: Color.Green));
else
reservedFor.QueueActivity(new TakeOff(reservedFor));
}
Expand Down
8 changes: 5 additions & 3 deletions OpenRA.Mods.Common/Traits/Production.cs
Expand Up @@ -10,6 +10,7 @@
#endregion

using System;
using System.Collections.Generic;
using OpenRA.Mods.Common.Activities;
using OpenRA.Primitives;
using OpenRA.Traits;
Expand Down Expand Up @@ -42,7 +43,7 @@ public Production(ActorInitializer init, ProductionInfo info)
public virtual void DoProduction(Actor self, ActorInfo producee, ExitInfo exitinfo, string productionType, TypeDictionary inits)
{
var exit = CPos.Zero;
var exitLocation = CPos.Zero;
var exitLocations = new List<CPos>();

// Clone the initializer dictionary for the new actor
var td = new TypeDictionary();
Expand All @@ -68,7 +69,7 @@ public virtual void DoProduction(Actor self, ActorInfo producee, ExitInfo exitin
initialFacing = delta.Yaw.Facing;
}

exitLocation = rp.Value != null ? rp.Value.Location : exit;
exitLocations = rp.Value != null ? rp.Value.Path : new List<CPos> { exit };

td.Add(new LocationInit(exit));
td.Add(new CenterPositionInit(spawn));
Expand All @@ -83,7 +84,8 @@ public virtual void DoProduction(Actor self, ActorInfo producee, ExitInfo exitin
var move = newUnit.TraitOrDefault<IMove>();
if (exitinfo != null && move != null)
newUnit.QueueActivity(new AttackMoveActivity(newUnit, () => move.MoveTo(exitLocation, 1, targetLineColor: Color.OrangeRed)));
foreach (var cell in exitLocations)
newUnit.QueueActivity(new AttackMoveActivity(newUnit, () => move.MoveTo(cell, 1, evaluateNearestMovableCell: true, targetLineColor: Color.OrangeRed)));
if (!self.IsDead)
foreach (var t in self.TraitsImplementing<INotifyProduction>())
Expand Down
13 changes: 7 additions & 6 deletions OpenRA.Mods.Common/Traits/ProductionFromMapEdge.cs
Expand Up @@ -9,8 +9,8 @@
*/
#endregion

using System.Collections.Generic;
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Traits
{
Expand Down Expand Up @@ -49,7 +49,7 @@ public override bool Produce(Actor self, ActorInfo producee, string productionTy
var aircraftInfo = producee.TraitInfoOrDefault<AircraftInfo>();
var mobileInfo = producee.TraitInfoOrDefault<MobileInfo>();

var destination = rp != null ? rp.Location : self.Location;
var destinations = rp != null ? rp.Path : new List<CPos> { self.Location };

var location = spawnLocation;
if (!location.HasValue)
Expand All @@ -61,7 +61,7 @@ public override bool Produce(Actor self, ActorInfo producee, string productionTy
{
var locomotorInfo = mobileInfo.LocomotorInfo;
location = self.World.Map.ChooseClosestMatchingEdgeCell(self.Location,
c => mobileInfo.CanEnterCell(self.World, null, c) && domainIndex.IsPassable(c, destination, locomotorInfo));
c => mobileInfo.CanEnterCell(self.World, null, c) && domainIndex.IsPassable(c, destinations[0], locomotorInfo));
}
}

Expand All @@ -75,7 +75,7 @@ public override bool Produce(Actor self, ActorInfo producee, string productionTy
if (aircraftInfo != null)
pos += new WVec(0, 0, aircraftInfo.CruiseAltitude.Length);

var initialFacing = self.World.Map.FacingBetween(location.Value, destination, 0);
var initialFacing = self.World.Map.FacingBetween(location.Value, destinations[0], 0);

self.World.AddFrameEndTask(w =>
{
Expand All @@ -91,11 +91,12 @@ public override bool Produce(Actor self, ActorInfo producee, string productionTy
var move = newUnit.TraitOrDefault<IMove>();
if (move != null)
newUnit.QueueActivity(move.MoveTo(destination, 2));
foreach (var cell in destinations)
newUnit.QueueActivity(move.MoveTo(cell, 2, evaluateNearestMovableCell: true));
if (!self.IsDead)
foreach (var t in self.TraitsImplementing<INotifyProduction>())
t.UnitProduced(self, newUnit, destination);
t.UnitProduced(self, newUnit, destinations[0]);
var notifyOthers = self.World.ActorsWithTrait<INotifyOtherProduction>();
foreach (var notify in notifyOthers)
Expand Down
8 changes: 5 additions & 3 deletions OpenRA.Mods.Common/Traits/ProductionParadrop.cs
Expand Up @@ -10,6 +10,7 @@
#endregion

using System;
using System.Collections.Generic;
using OpenRA.Activities;
using OpenRA.Mods.Common.Activities;
using OpenRA.Primitives;
Expand Down Expand Up @@ -102,7 +103,7 @@ public override bool Produce(Actor self, ActorInfo producee, string productionTy
public override void DoProduction(Actor self, ActorInfo producee, ExitInfo exitinfo, string productionType, TypeDictionary inits)
{
var exit = CPos.Zero;
var exitLocation = CPos.Zero;
var exitLocations = new List<CPos>();

var info = (ProductionParadropInfo)Info;
var actorType = info.ActorType;
Expand All @@ -122,7 +123,7 @@ public override void DoProduction(Actor self, ActorInfo producee, ExitInfo exiti

var initialFacing = exitinfo.Facing < 0 ? (to - spawn).Yaw.Facing : exitinfo.Facing;

exitLocation = rp.Value != null ? rp.Value.Location : exit;
exitLocations = rp.Value != null ? rp.Value.Path : new List<CPos> { exit };

td.Add(new LocationInit(exit));
td.Add(new CenterPositionInit(spawn));
Expand All @@ -137,7 +138,8 @@ public override void DoProduction(Actor self, ActorInfo producee, ExitInfo exiti
var move = newUnit.TraitOrDefault<IMove>();
if (move != null)
newUnit.QueueActivity(new AttackMoveActivity(newUnit, () => move.MoveTo(exitLocation, 1, targetLineColor: Color.OrangeRed)));
foreach (var cell in exitLocations)
newUnit.QueueActivity(new AttackMoveActivity(newUnit, () => move.MoveTo(cell, 1, evaluateNearestMovableCell: true, targetLineColor: Color.OrangeRed)));
if (!self.IsDead)
foreach (var t in self.TraitsImplementing<INotifyProduction>())
Expand Down

0 comments on commit 203fff0

Please sign in to comment.