/
RecursiveShadowcastingFOV.cs
244 lines (213 loc) · 11.4 KB
/
RecursiveShadowcastingFOV.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using SadRogue.Primitives;
using SadRogue.Primitives.GridViews;
namespace GoRogue.FOV
{
/// <summary>
/// Implements <see cref="IFOV"/> by using a recursive shadow-casting implementation.
/// </summary>
/// <remarks>
/// Compared to the functionally equivalent implementation of recursive shadowcasting, <see cref="RecursiveShadowcastingBooleanBasedFOV"/>, this version
/// is usually a reasonable default. However, this version can take up a great deal of memory for large maps; in these cases, you may prefer
/// <see cref="RecursiveShadowcastingBooleanBasedFOV"/> instead.
/// </remarks>
[PublicAPI]
public class RecursiveShadowcastingFOV : DoubleBasedFOVBase
{
private HashSet<Point> _currentFOV;
private HashSet<Point> _previousFOV;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="transparencyView">
/// The values used to calculate field of view. Values of true are considered
/// non-blocking (transparent) to line of sight, while false values are considered
/// to be blocking.
/// </param>
/// <param name="hasher">The hashing algorithm to use for points in hash sets. Defaults to the default hash algorithm for Points.</param>
public RecursiveShadowcastingFOV(IGridView<bool> transparencyView, IEqualityComparer<Point>? hasher = null)
: base(transparencyView, new ArrayView<double>(transparencyView.Width, transparencyView.Height))
{
hasher ??= EqualityComparer<Point>.Default;
_currentFOV = new HashSet<Point>(hasher);
_previousFOV = new HashSet<Point>(hasher);
}
/// <inheritdoc />
public override IEnumerable<Point> CurrentFOV => _currentFOV;
/// <inheritdoc />
public override IEnumerable<Point> NewlySeen => _currentFOV.Where(pos => !_previousFOV.Contains(pos));
/// <inheritdoc />
public override IEnumerable<Point> NewlyUnseen => _previousFOV.Where(pos => !_currentFOV.Contains(pos));
/// <inheritdoc/>
protected override void OnCalculate(int originX, int originY, double radius, Distance distanceCalc)
{
radius = Math.Max(1, radius);
var decay = 1.0 / (radius + 1);
ResultView[originX, originY] = 1; // Full power to starting space
_currentFOV.Add(new Point(originX, originY));
foreach (var d in AdjacencyRule.Diagonals.DirectionsOfNeighbors())
{
ShadowCast(1, 1.0, 0.0, 0, d.DeltaX, d.DeltaY, 0, radius, originX, originY, decay, ResultView, _currentFOV,
TransparencyView, distanceCalc);
ShadowCast(1, 1.0, 0.0, d.DeltaX, 0, 0, d.DeltaY, radius, originX, originY, decay, ResultView, _currentFOV,
TransparencyView, distanceCalc);
}
}
/// <inheritdoc/>
protected override void OnCalculate(int originX, int originY, double radius, Distance distanceCalc, double angle, double span)
{
radius = Math.Max(1, radius);
var decay = 1.0 / (radius + 1);
// Convert from 0 pointing up to 0 pointing right, which is what is expected by the ShadowCastLimited
// implementation
angle -= 90;
// Convert to radians
angle = (angle > 360.0 || angle < 0 ? MathHelpers.WrapAround(angle, 360.0) : angle) *
SadRogue.Primitives.MathHelpers.DegreePctOfCircle;
span *= SadRogue.Primitives.MathHelpers.DegreePctOfCircle;
ResultView[originX, originY] = 1; // Full power to starting space
_currentFOV.Add(new Point(originX, originY));
ShadowCastLimited(1, 1.0, 0.0, 0, 1, 1, 0, radius, originX, originY, decay, ResultView, _currentFOV, TransparencyView,
distanceCalc, angle, span);
ShadowCastLimited(1, 1.0, 0.0, 1, 0, 0, 1, radius, originX, originY, decay, ResultView, _currentFOV, TransparencyView,
distanceCalc, angle, span);
ShadowCastLimited(1, 1.0, 0.0, 0, -1, 1, 0, radius, originX, originY, decay, ResultView, _currentFOV, TransparencyView,
distanceCalc, angle, span);
ShadowCastLimited(1, 1.0, 0.0, -1, 0, 0, 1, radius, originX, originY, decay, ResultView, _currentFOV, TransparencyView,
distanceCalc, angle, span);
ShadowCastLimited(1, 1.0, 0.0, 0, -1, -1, 0, radius, originX, originY, decay, ResultView, _currentFOV, TransparencyView,
distanceCalc, angle, span);
ShadowCastLimited(1, 1.0, 0.0, -1, 0, 0, -1, radius, originX, originY, decay, ResultView, _currentFOV, TransparencyView,
distanceCalc, angle, span);
ShadowCastLimited(1, 1.0, 0.0, 0, 1, -1, 0, radius, originX, originY, decay, ResultView, _currentFOV, TransparencyView,
distanceCalc, angle, span);
ShadowCastLimited(1, 1.0, 0.0, 1, 0, 0, -1, radius, originX, originY, decay, ResultView, _currentFOV, TransparencyView,
distanceCalc, angle, span);
}
/// <inheritdoc/>
protected override void OnReset()
{
// Reset visibility
if (ResultView.Width != TransparencyView.Width || ResultView.Height != TransparencyView.Height)
ResultView = new ArrayView<double>(TransparencyView.Width, TransparencyView.Height);
else // ArrayView.Clear is faster than the generic Fill, so we'll cast and use that since we know ResultView is an ArrayView
((ArrayView<double>)ResultView).Clear();
// Cycle current and previous FOVs
(_previousFOV, _currentFOV) = (_currentFOV, _previousFOV);
_currentFOV.Clear();
}
private static void ShadowCast(int row, double start, double end, int xx, int xy, int yx, int yy,
double radius, int startX, int startY, double decay, ISettableGridView<double> lightMap,
HashSet<Point> fovSet,
IGridView<bool> map, Distance distanceStrategy)
{
double newStart = 0;
if (start < end)
return;
var blocked = false;
for (var distance = row; distance <= radius && distance < map.Width + map.Height && !blocked; distance++)
{
var deltaY = -distance;
for (var deltaX = -distance; deltaX <= 0; deltaX++)
{
var currentX = startX + deltaX * xx + deltaY * xy;
var currentY = startY + deltaX * yx + deltaY * yy;
double leftSlope = (deltaX - 0.5f) / (deltaY + 0.5f);
double rightSlope = (deltaX + 0.5f) / (deltaY - 0.5f);
if (!(currentX >= 0 && currentY >= 0 && currentX < map.Width && currentY < map.Height) ||
start < rightSlope)
continue;
if (end > leftSlope)
break;
var deltaRadius = distanceStrategy.Calculate(deltaX, deltaY);
// If within lightable area, light if needed
if (deltaRadius <= radius)
{
var bright = 1 - decay * deltaRadius;
if (bright > lightMap[currentX, currentY])
{
lightMap[currentX, currentY] = bright;
fovSet.Add(new Point(currentX, currentY));
}
}
if (blocked) // Previous cell was blocked
{
if (!map[currentX, currentY]) // Hit a wall...
newStart = rightSlope;
else
{
blocked = false;
start = newStart;
}
}
else
{
if (map[currentX, currentY] || !(distance < radius)) continue;
blocked = true;
ShadowCast(distance + 1, start, leftSlope, xx, xy, yx, yy, radius, startX, startY, decay,
lightMap, fovSet, map, distanceStrategy);
newStart = rightSlope;
}
}
}
}
private static void ShadowCastLimited(int row, double start, double end, int xx, int xy, int yx, int yy,
double radius, int startX, int startY, double decay,
ISettableGridView<double> lightMap, HashSet<Point> fovSet, IGridView<bool> map,
Distance distanceStrategy, double angle, double span)
{
double newStart = 0;
if (start < end)
return;
var blocked = false;
for (var distance = row; distance <= radius && distance < map.Width + map.Height && !blocked; distance++)
{
var deltaY = -distance;
for (var deltaX = -distance; deltaX <= 0; deltaX++)
{
var currentX = startX + deltaX * xx + deltaY * xy;
var currentY = startY + deltaX * yx + deltaY * yy;
double leftSlope = (deltaX - 0.5f) / (deltaY + 0.5f);
double rightSlope = (deltaX + 0.5f) / (deltaY - 0.5f);
if (!(currentX >= 0 && currentY >= 0 && currentX < map.Width && currentY < map.Height) ||
start < rightSlope)
continue;
if (end > leftSlope)
break;
var deltaRadius = distanceStrategy.Calculate(deltaX, deltaY);
var at2 = Math.Abs(angle - MathHelpers.ScaledAtan2Approx(currentY - startY, currentX - startX));
// Check if within lightable area, light if needed
if (deltaRadius <= radius && (at2 <= span * 0.5 || at2 >= 1.0 - span * 0.5))
{
var bright = 1 - decay * deltaRadius;
if (bright > lightMap[currentX, currentY])
{
lightMap[currentX, currentY] = bright;
fovSet.Add(new Point(currentX, currentY));
}
}
if (blocked) // Previous cell was blocking
{
if (!map[currentX, currentY]) // We hit a wall...
newStart = rightSlope;
else
{
blocked = false;
start = newStart;
}
}
else if (!map[currentX, currentY] && distance < radius) // Wall within line of sight
{
blocked = true;
ShadowCastLimited(distance + 1, start, leftSlope, xx, xy, yx, yy, radius, startX, startY, decay,
lightMap, fovSet, map, distanceStrategy, angle, span);
newStart = rightSlope;
}
}
}
}
}
}