/
FlyAttack.cs
148 lines (124 loc) · 5.21 KB
/
FlyAttack.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#region Copyright & License Information
/*
* Copyright 2007-2019 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 System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities
{
public class FlyAttack : Activity
{
readonly Aircraft aircraft;
readonly AttackAircraft attackAircraft;
readonly Rearmable rearmable;
readonly int ticksUntilTurn;
Target target;
Target lastVisibleTarget;
WDist lastVisibleMaximumRange;
bool useLastVisibleTarget;
bool hasTicked;
public FlyAttack(Actor self, Target target)
{
this.target = target;
aircraft = self.Trait<Aircraft>();
attackAircraft = self.Trait<AttackAircraft>();
rearmable = self.TraitOrDefault<Rearmable>();
ticksUntilTurn = attackAircraft.Info.AttackTurnDelay;
// The target may become hidden between the initial order request and the first tick (e.g. if queued)
// Moving to any position (even if quite stale) is still better than immediately giving up
if ((target.Type == TargetType.Actor && target.Actor.CanBeViewedByPlayer(self.Owner))
|| target.Type == TargetType.FrozenActor || target.Type == TargetType.Terrain)
{
lastVisibleTarget = Target.FromPos(target.CenterPosition);
lastVisibleMaximumRange = attackAircraft.GetMaximumRangeVersusTarget(target);
}
}
public override Activity Tick(Actor self)
{
if (ChildActivity != null)
{
ChildActivity = ActivityUtils.RunActivity(self, ChildActivity);
if (ChildActivity != null)
return this;
}
// Refuse to take off if it would land immediately again.
if (aircraft.ForceLanding)
Cancel(self);
if (IsCanceling)
{
// Cancel the requested target, but keep firing on it while in range
if (attackAircraft.Info.PersistentTargeting)
{
attackAircraft.OpportunityTarget = attackAircraft.RequestedTarget;
attackAircraft.OpportunityForceAttack = attackAircraft.RequestedForceAttack;
attackAircraft.OpportunityTargetIsPersistentTarget = true;
}
attackAircraft.RequestedTarget = Target.Invalid;
return NextActivity;
}
// Check that AttackFollow hasn't cancelled the target by modifying attack.Target
// Having both this and AttackFollow modify that field is a horrible hack.
if (hasTicked && attackAircraft.RequestedTarget.Type == TargetType.Invalid)
return NextActivity;
if (attackAircraft.IsTraitPaused)
return this;
bool targetIsHiddenActor;
attackAircraft.RequestedTarget = target = target.Recalculate(self.Owner, out targetIsHiddenActor);
attackAircraft.RequestedTargetLastTick = self.World.WorldTick;
hasTicked = true;
if (!targetIsHiddenActor && target.Type == TargetType.Actor)
{
lastVisibleTarget = Target.FromTargetPositions(target);
lastVisibleMaximumRange = attackAircraft.GetMaximumRangeVersusTarget(target);
}
var oldUseLastVisibleTarget = useLastVisibleTarget;
useLastVisibleTarget = targetIsHiddenActor || !target.IsValidFor(self);
// Update target lines if required
if (useLastVisibleTarget != oldUseLastVisibleTarget)
self.SetTargetLine(useLastVisibleTarget ? lastVisibleTarget : target, Color.Red, false);
// Target is hidden or dead, and we don't have a fallback position to move towards
if (useLastVisibleTarget && !lastVisibleTarget.IsValidFor(self))
{
attackAircraft.RequestedTarget = Target.Invalid;
return NextActivity;
}
// If all valid weapons have depleted their ammo and Rearmable trait exists, return to RearmActor to reload and then resume the activity
if (rearmable != null && !useLastVisibleTarget && attackAircraft.Armaments.All(x => x.IsTraitPaused || !x.Weapon.IsValidAgainst(target, self.World, self)))
{
QueueChild(self, new ReturnToBase(self, aircraft.Info.AbortOnResupply), true);
return this;
}
var pos = self.CenterPosition;
var checkTarget = useLastVisibleTarget ? lastVisibleTarget : target;
// We don't know where the target actually is, so move to where we last saw it
if (useLastVisibleTarget)
{
// We've reached the assumed position but it is not there - give up
if (checkTarget.IsInRange(pos, lastVisibleMaximumRange))
{
attackAircraft.RequestedTarget = Target.Invalid;
return NextActivity;
}
// Fly towards the last known position
QueueChild(self, new Fly(self, target, WDist.Zero, lastVisibleMaximumRange, checkTarget.CenterPosition, Color.Red), true);
return this;
}
if (self.World.Map.DistanceAboveTerrain(self.CenterPosition).Length < aircraft.Info.MinAirborneAltitude)
QueueChild(self, new TakeOff(self), true);
if (attackAircraft != null && target.IsInRange(self.CenterPosition, attackAircraft.GetMinimumRange()))
QueueChild(self, new FlyTimed(ticksUntilTurn, self), true);
QueueChild(self, new Fly(self, target, checkTarget.CenterPosition, Color.Red), true);
QueueChild(self, new FlyTimed(ticksUntilTurn, self));
return this;
}
}
}