/
ReturnOnIdle.cs
68 lines (58 loc) · 2.12 KB
/
ReturnOnIdle.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
#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.Mods.Common.Activities;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Return to a player owned RearmActor. If none available, head back to base and circle over it.")]
public class ReturnOnIdleInfo : ITraitInfo, Requires<AircraftInfo>
{
public object Create(ActorInitializer init) { return new ReturnOnIdle(init.Self, this); }
}
public class ReturnOnIdle : INotifyIdle
{
readonly AircraftInfo aircraftInfo;
public ReturnOnIdle(Actor self, ReturnOnIdleInfo info)
{
aircraftInfo = self.Info.TraitInfo<AircraftInfo>();
}
void INotifyIdle.TickIdle(Actor self)
{
// We're on the ground, let's stay there.
if (self.World.Map.DistanceAboveTerrain(self.CenterPosition).Length < aircraftInfo.MinAirborneAltitude)
return;
var resupplier = ReturnToBase.ChooseResupplier(self, true);
if (resupplier != null)
self.QueueActivity(new ReturnToBase(self, aircraftInfo.AbortOnResupply, resupplier));
else
{
// nowhere to land, pick something friendly and circle over it.
// I'd prefer something we own
var someBuilding = self.World.ActorsHavingTrait<Building>()
.FirstOrDefault(a => a.Owner == self.Owner);
// failing that, something unlikely to shoot at us
if (someBuilding == null)
someBuilding = self.World.ActorsHavingTrait<Building>()
.FirstOrDefault(a => self.Owner.Stances[a.Owner] == Stance.Ally);
if (someBuilding == null)
{
// ... going down the garden to eat worms ...
self.QueueActivity(new FlyOffMap(self));
self.QueueActivity(new RemoveSelf());
return;
}
self.QueueActivity(new Fly(self, Target.FromActor(someBuilding)));
self.QueueActivity(new FlyCircle(self));
}
}
}
}