-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
AffectsShroud.cs
175 lines (139 loc) · 5.14 KB
/
AffectsShroud.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* 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;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public abstract class AffectsShroudInfo : ConditionalTraitInfo
{
public readonly WDist MinRange = WDist.Zero;
public readonly WDist Range = WDist.Zero;
[Desc("If >= 0, prevent cells that are this much higher than the actor from being revealed.")]
public readonly int MaxHeightDelta = -1;
[Desc("If > 0, force visibility to be recalculated if the unit moves within a cell by more than this distance.")]
public readonly WDist MoveRecalculationThreshold = new(256);
[Desc("Possible values are CenterPosition (measure range from the center) and ",
"Footprint (measure range from the footprint)")]
public readonly VisibilityType Type = VisibilityType.Footprint;
}
public abstract class AffectsShroud : ConditionalTrait<AffectsShroudInfo>, ISync, INotifyAddedToWorld,
INotifyRemovedFromWorld, INotifyMoving, INotifyCenterPositionChanged, ITick
{
static readonly PPos[] NoCells = Array.Empty<PPos>();
readonly HashSet<PPos> footprint;
[Sync]
CPos cachedLocation;
[Sync]
WDist cachedRange;
[Sync]
protected bool CachedTraitDisabled { get; private set; }
WPos cachedPos;
protected abstract void AddCellsToPlayerShroud(Actor self, Player player, PPos[] uv);
protected abstract void RemoveCellsFromPlayerShroud(Actor self, Player player);
protected AffectsShroud(AffectsShroudInfo info)
: base(info)
{
if (Info.Type == VisibilityType.Footprint)
footprint = new HashSet<PPos>();
}
PPos[] ProjectedCells(Actor self)
{
var map = self.World.Map;
var minRange = Info.MinRange;
var maxRange = Range;
if (maxRange <= minRange)
return NoCells;
if (Info.Type == VisibilityType.Footprint)
{
// PERF: Reuse collection to avoid allocations.
footprint.UnionWith(self.OccupiesSpace.OccupiedCells()
.SelectMany(kv => Shroud.ProjectedCellsInRange(map, map.CenterOfCell(kv.Cell), minRange, maxRange, Info.MaxHeightDelta)));
var cells = footprint.ToArray();
footprint.Clear();
return cells;
}
var pos = self.CenterPosition;
if (Info.Type == VisibilityType.GroundPosition)
pos -= new WVec(WDist.Zero, WDist.Zero, self.World.Map.DistanceAboveTerrain(pos));
return Shroud.ProjectedCellsInRange(map, pos, minRange, maxRange, Info.MaxHeightDelta)
.ToArray();
}
void INotifyCenterPositionChanged.CenterPositionChanged(Actor self, byte oldLayer, byte newLayer)
{
if (!self.IsInWorld)
return;
var centerPosition = self.CenterPosition;
var projectedPos = centerPosition - new WVec(0, centerPosition.Z, centerPosition.Z);
var projectedLocation = self.World.Map.CellContaining(projectedPos);
var pos = self.CenterPosition;
var dirty = Info.MoveRecalculationThreshold.Length > 0 && (pos - cachedPos).LengthSquared > Info.MoveRecalculationThreshold.LengthSquared;
if (!dirty && cachedLocation == projectedLocation)
return;
cachedLocation = projectedLocation;
cachedPos = pos;
UpdateShroudCells(self);
}
void ITick.Tick(Actor self)
{
if (!self.IsInWorld)
return;
var traitDisabled = IsTraitDisabled;
var range = Range;
if (cachedRange == range && traitDisabled == CachedTraitDisabled)
return;
cachedRange = range;
CachedTraitDisabled = traitDisabled;
UpdateShroudCells(self);
}
void UpdateShroudCells(Actor self)
{
var cells = ProjectedCells(self);
foreach (var p in self.World.Players)
{
RemoveCellsFromPlayerShroud(self, p);
AddCellsToPlayerShroud(self, p, cells);
}
}
void INotifyAddedToWorld.AddedToWorld(Actor self)
{
var centerPosition = self.CenterPosition;
var projectedPos = centerPosition - new WVec(0, centerPosition.Z, centerPosition.Z);
cachedLocation = self.World.Map.CellContaining(projectedPos);
cachedPos = centerPosition;
CachedTraitDisabled = IsTraitDisabled;
var cells = ProjectedCells(self);
foreach (var p in self.World.Players)
AddCellsToPlayerShroud(self, p, cells);
}
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
{
foreach (var p in self.World.Players)
RemoveCellsFromPlayerShroud(self, p);
}
public virtual WDist Range => CachedTraitDisabled ? WDist.Zero : Info.Range;
void INotifyMoving.MovementTypeChanged(Actor self, MovementType type)
{
// Recalculate the visibility at our final stop position
if (type == MovementType.None && self.IsInWorld)
{
var centerPosition = self.CenterPosition;
var projectedPos = centerPosition - new WVec(0, centerPosition.Z, centerPosition.Z);
var projectedLocation = self.World.Map.CellContaining(projectedPos);
var pos = self.CenterPosition;
cachedLocation = projectedLocation;
cachedPos = pos;
UpdateShroudCells(self);
}
}
}
}