-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Wanders.cs
89 lines (71 loc) · 2.57 KB
/
Wanders.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
#region Copyright & License Information
/*
* Copyright 2007-2015 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. For more information,
* see COPYING.
*/
#endregion
using System;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Wanders around aimlessly while idle.")]
public abstract class WandersInfo : ITraitInfo
{
public readonly int WanderMoveRadius = 1;
[Desc("Number of ticks to wait before decreasing the effective move radius.")]
public readonly int TicksToWaitBeforeReducingMoveRadius = 5;
[Desc("Mimimum ammount of ticks the actor will sit idly before starting to wander.")]
public readonly int MinMoveDelayInTicks = 0;
[Desc("Maximum ammount of ticks the actor will sit idly before starting to wander.")]
public readonly int MaxMoveDelayInTicks = 0;
public abstract object Create(ActorInitializer init);
}
public class Wanders : INotifyIdle, INotifyBecomingIdle
{
readonly Actor self;
readonly WandersInfo info;
int countdown;
int ticksIdle;
int effectiveMoveRadius;
public Wanders(Actor self, WandersInfo info)
{
this.self = self;
this.info = info;
effectiveMoveRadius = info.WanderMoveRadius;
}
public virtual void OnBecomingIdle(Actor self)
{
countdown = self.World.SharedRandom.Next(info.MinMoveDelayInTicks, info.MaxMoveDelayInTicks);
}
public void TickIdle(Actor self)
{
if (--countdown > 0)
return;
var targetCell = PickTargetLocation();
if (targetCell != CPos.Zero)
DoAction(self, targetCell);
}
CPos PickTargetLocation()
{
var target = self.CenterPosition + new WVec(0, -1024 * effectiveMoveRadius, 0).Rotate(WRot.FromFacing(self.World.SharedRandom.Next(255)));
var targetCell = self.World.Map.CellContaining(target);
if (!self.World.Map.Contains(targetCell))
{
// If MoveRadius is too big there might not be a valid cell to order the attack to (if actor is on a small island and can't leave)
if (++ticksIdle % info.TicksToWaitBeforeReducingMoveRadius == 0)
effectiveMoveRadius--;
return CPos.Zero; // We'll be back the next tick; better to sit idle for a few seconds than prolong this tick indefinitely with a loop
}
ticksIdle = 0;
effectiveMoveRadius = info.WanderMoveRadius;
return targetCell;
}
public virtual void DoAction(Actor self, CPos targetCell)
{
throw new NotImplementedException("Base class Wanders does not implement method DoAction!");
}
}
}