Skip to content

Commit

Permalink
Merge remote-tracking branch 'darkademic/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Inq8 committed Feb 4, 2024
2 parents 77286f9 + b574668 commit ed3a953
Show file tree
Hide file tree
Showing 35 changed files with 718 additions and 213 deletions.
147 changes: 147 additions & 0 deletions OpenRA.Mods.CA/Activities/Attach.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#region Copyright & License Information
/**
* Copyright (c) The OpenRA Combined Arms Developers (see CREDITS).
* This file is part of OpenRA Combined Arms, 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 System.Collections.Generic;
using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.CA.Traits;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA.Mods.CA.Activities
{
enum AttachState { Approaching, Attaching, Finished }

public class Attach : Activity

Check warning on line 23 in OpenRA.Mods.CA/Activities/Attach.cs

View workflow job for this annotation

GitHub Actions / Windows (.NET 6.0)

Missing XML comment for publicly visible type or member 'Attach'
{
Target target;
Attachable attachable;
AttachableTo attachableTo;
IMove move;
readonly Color targetLineColor;
AttachState state;

bool TargetIsValid => target.Actor != null && target.Type == TargetType.Actor && target.Actor.IsInWorld && !target.Actor.IsDead && attachableTo.CanAttach(attachable);

public Attach(Actor self, in Target target, Attachable attachable, Color? targetLineColor)

Check warning on line 34 in OpenRA.Mods.CA/Activities/Attach.cs

View workflow job for this annotation

GitHub Actions / Windows (.NET 6.0)

Missing XML comment for publicly visible type or member 'Attach.Attach(Actor, in Target, Attachable, Color?)'
{
this.target = target;
this.attachable = attachable;
move = self.TraitOrDefault<IMove>();
var moveInfo = self.Info.TraitInfoOrDefault<IMoveInfo>();
attachableTo = target.Actor.TraitsImplementing<AttachableTo>().FirstOrDefault();
this.targetLineColor = targetLineColor ?? moveInfo.GetTargetLineColor();
state = AttachState.Approaching;
}

public override bool Tick(Actor self)

Check warning on line 45 in OpenRA.Mods.CA/Activities/Attach.cs

View workflow job for this annotation

GitHub Actions / Windows (.NET 6.0)

Missing XML comment for publicly visible type or member 'Attach.Tick(Actor)'
{
if (IsCanceling)
{
return true;
}

if (!TargetIsValid)
return true;

bool isCloseEnough;

if (attachable.Info.MinAttachDistance < WDist.Zero)
isCloseEnough = true;
else
isCloseEnough = (target.CenterPosition - self.CenterPosition).HorizontalLengthSquared <= attachable.Info.MinAttachDistance.LengthSquared;

if (isCloseEnough)
{
if (state == AttachState.Approaching)
{
OnMinimumDistanceReached(self);
}
}
else if (state != AttachState.Attaching && state != AttachState.Finished)
{
QueueChild(move.MoveWithinRange(target, attachable.Info.MinAttachDistance, targetLineColor: targetLineColor));
return false;
}

if (state == AttachState.Finished)
{
return true;
}

return false;
}

void OnMinimumDistanceReached(Actor self)
{
BeginAttachment(self);
}

void BeginAttachment(Actor self)
{
state = AttachState.Attaching;

if (!attachableTo.CanAttach(attachable))
{
state = AttachState.Finished;
return;
}

attachableTo.Reserve();

if (attachable.Info.OnAttachTransformInto != null)
{
var faction = self.Owner.Faction.InternalName;
var transform = new InstantTransform(self, attachable.Info.OnAttachTransformInto)
{
ForceHealthPercentage = 0,
Faction = faction,
OnComplete = a => CompleteAttachment(a),
SkipMakeAnims = true,
};

QueueChild(transform);
}
else
CompleteAttachment(self);
}

void CompleteAttachment(Actor a)
{
state = AttachState.Finished;
var attachable = a.TraitOrDefault<Attachable>();
if (attachable == null)
return;

var attached = attachableTo.Attach(attachable, true);

if (attached && attachable.Info.AttachSound != null)
Game.Sound.Play(SoundType.World, attachable.Info.AttachSound, a.CenterPosition);
}

public override IEnumerable<TargetLineNode> TargetLineNodes(Actor self)

Check warning on line 130 in OpenRA.Mods.CA/Activities/Attach.cs

View workflow job for this annotation

GitHub Actions / Windows (.NET 6.0)

Missing XML comment for publicly visible type or member 'Attach.TargetLineNodes(Actor)'
{
if (ChildActivity == null)
yield return new TargetLineNode(target, targetLineColor);
else
{
var current = ChildActivity;
while (current != null)
{
foreach (var n in current.TargetLineNodes(self))
yield return n;

current = current.NextActivity;
}
}
}
}
}
20 changes: 12 additions & 8 deletions OpenRA.Mods.CA/Activities/InstantTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class InstantTransform : Activity
public int ForceHealthPercentage = 0;
public bool SkipMakeAnims = false;
public string Faction = null;
public Action OnComplete;
public Action<Actor> OnComplete;

public InstantTransform(Actor self, string toActor)
{
Expand Down Expand Up @@ -93,11 +93,12 @@ void DoTransform(Actor self)
Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", Notification, self.Owner.Faction.InternalName);
var cell = self.Location + Offset;
var init = new TypeDictionary
{
new LocationInit(self.Location + Offset),
new LocationInit(cell),
new OwnerInit(self.Owner),
new CenterPositionInit(self.CenterPosition),
new CenterPositionInit(self.World.Map.CenterOfCell(cell) + new WVec(0, 0, self.CenterPosition.Z)),
};
var facing = self.TraitOrDefault<IFacing>();
Expand Down Expand Up @@ -142,14 +143,17 @@ void DoTransform(Actor self)
self.ReplacedByActor = a;
if (selected)
w.Selection.Add(a);
if (a.TraitOrDefault<Selectable>() != null)
{
if (selected)
w.Selection.Add(a);
if (controlgroup.HasValue)
w.ControlGroups.AddToControlGroup(a, controlgroup.Value);
if (controlgroup.HasValue)
w.ControlGroups.AddToControlGroup(a, controlgroup.Value);
}
if (OnComplete != null)
OnComplete();
OnComplete(a);
});
}
}
Expand Down
61 changes: 35 additions & 26 deletions OpenRA.Mods.CA/Activities/Upgrade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using OpenRA.Mods.CA.Traits;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA.Mods.CA.Activities
Expand All @@ -34,12 +35,14 @@ public class Upgrade : Activity
INotifyResupply[] notifyResupplies;
IMove move;
IMoveInfo moveInfo;
readonly Color targetLineColor;
Action<int> updateTicksRemaining;

bool upgradeInProgress = false;

public Upgrade(Actor self, Upgradeable upgradeable, PlayerResources playerResources, Action<int> updateTicksRemaining)
public Upgrade(Actor self, in Target target, Upgradeable upgradeable, PlayerResources playerResources, Action<int> updateTicksRemaining, Color? targetLineColor)
{
host = target;
this.upgradeable = upgradeable;
this.playerResources = playerResources;
this.updateTicksRemaining = updateTicksRemaining;
Expand All @@ -48,11 +51,14 @@ public Upgrade(Actor self, Upgradeable upgradeable, PlayerResources playerResour
upgradingConditionToken = Actor.InvalidConditionToken;
move = self.TraitOrDefault<IMove>();
moveInfo = self.Info.TraitInfoOrDefault<IMoveInfo>();
this.targetLineColor = targetLineColor ?? moveInfo.GetTargetLineColor();
}

protected override void OnFirstRun(Actor self)
{
var hostActor = FindNearestHost(self);
// Host might be self if order issued via hotkey or upgrade button
var hostActor = host.Type != TargetType.Actor || host.Actor == self ? FindNearestHost(self) : host.Actor;

if (hostActor != null)
{
host = Target.FromActor(hostActor);
Expand All @@ -70,33 +76,29 @@ public override bool Tick(Actor self)
}

updateTicksRemaining(upgradeTicksRemaining);
var isHostInvalid = upgradeable.Info.UpgradeAtActors.Any() && (host.Actor == null || host.Type != TargetType.Actor || !host.Actor.IsInWorld);
var isCloseEnough = false;
var isHostInvalid = upgradeable.Info.UpgradeAtActors.Any() && (host.Actor == null || host.Type != TargetType.Actor || !host.Actor.IsInWorld || host.Actor == self);

if (!isHostInvalid)
if (isHostInvalid)
{
// Negative means there's no distance limit.
// If RepairableNear, use TargetablePositions instead of CenterPosition
// to ensure the actor moves close enough to the host.
// Otherwise check against host CenterPosition.
if (upgradeable.Info.UpgradeAtRange < WDist.Zero)
isCloseEnough = true;
else
isCloseEnough = (host.CenterPosition - self.CenterPosition).HorizontalLengthSquared <= upgradeable.Info.UpgradeAtRange.LengthSquared;
}

// This ensures transports are also cancelled when the host becomes invalid
if (!IsCanceling && isHostInvalid)
// This ensures transports are also cancelled when the host becomes invalid
Cancel(self, true);

if (IsCanceling || isHostInvalid)
{
return true;
}
else if (!isCloseEnough)

bool isCloseEnough;

// Negative means there's no distance limit.
// If RepairableNear, use TargetablePositions instead of CenterPosition
// to ensure the actor moves close enough to the host.
// Otherwise check against host CenterPosition.
if (upgradeable.Info.UpgradeAtRange < WDist.Zero)
isCloseEnough = true;
else
isCloseEnough = (host.CenterPosition - self.CenterPosition).HorizontalLengthSquared <= upgradeable.Info.UpgradeAtRange.LengthSquared;

if (!isCloseEnough)
{
var targetCell = self.World.Map.CellContaining(host.Actor.CenterPosition);
QueueChild(move.MoveWithinRange(host, upgradeable.Info.UpgradeAtRange - WDist.FromCells(1), targetLineColor: moveInfo.GetTargetLineColor()));
QueueChild(move.MoveWithinRange(host, upgradeable.Info.UpgradeAtRange - WDist.FromCells(1), targetLineColor: targetLineColor));
return false;
}

Expand Down Expand Up @@ -176,8 +178,13 @@ void ApplyUpgrade(Actor self)

void Transform(Actor self, string faction)
{
var transform = new InstantTransform(self, upgradeable.Info.Actor) { ForceHealthPercentage = 0, Faction = faction, OnComplete = () => { CompleteUpgrade(self, faction); } };
transform.SkipMakeAnims = upgradeable.Info.SkipMakeAnims;
var transform = new InstantTransform(self, upgradeable.Info.Actor)
{
ForceHealthPercentage = 0,
Faction = faction,
OnComplete = (Actor a) => { CompleteUpgrade(self, faction); },
SkipMakeAnims = upgradeable.Info.SkipMakeAnims
};
QueueChild(transform);
}

Expand Down Expand Up @@ -218,7 +225,9 @@ void CancelUpgrade(Actor self)
public override IEnumerable<TargetLineNode> TargetLineNodes(Actor self)
{
if (ChildActivity == null)
yield return new TargetLineNode(host, moveInfo.GetTargetLineColor());
{
yield return new TargetLineNode(host, targetLineColor);
}
else
{
var current = ChildActivity;
Expand Down
Loading

0 comments on commit ed3a953

Please sign in to comment.