Skip to content

Commit 67fd71a

Browse files
abcdefg30reaperrr
authored andcommitted
Add a ProjectedCellLayer and use it in Shroud.cs
1 parent 385e705 commit 67fd71a

File tree

2 files changed

+94
-56
lines changed

2 files changed

+94
-56
lines changed

OpenRA.Game/Map/ProjectedCellLayer.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#region Copyright & License Information
2+
/*
3+
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
4+
* This file is part of OpenRA, which is free software. It is made
5+
* available to you under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation, either version 3 of
7+
* the License, or (at your option) any later version. For more
8+
* information, see COPYING.
9+
*/
10+
#endregion
11+
12+
using System;
13+
using OpenRA.Primitives;
14+
15+
namespace OpenRA
16+
{
17+
public sealed class ProjectedCellLayer<T> : CellLayerBase<T>
18+
{
19+
public ProjectedCellLayer(Map map)
20+
: base(map) { }
21+
22+
public ProjectedCellLayer(MapGridType gridType, Size size)
23+
: base(gridType, size) { }
24+
25+
// Resolve an array index from map coordinates.
26+
int Index(PPos uv)
27+
{
28+
return uv.V * Size.Width + uv.U;
29+
}
30+
31+
/// <summary>Gets or sets the layer contents using projected map coordinates.</summary>
32+
public T this[PPos uv]
33+
{
34+
get
35+
{
36+
return entries[Index(uv)];
37+
}
38+
39+
set
40+
{
41+
entries[Index(uv)] = value;
42+
}
43+
}
44+
45+
public bool Contains(PPos uv)
46+
{
47+
return bounds.Contains(uv.U, uv.V);
48+
}
49+
}
50+
}

OpenRA.Game/Traits/Player/Shroud.cs

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ public ShroudSource(SourceType type, PPos[] projectedCells)
9494
readonly Dictionary<object, ShroudSource> sources = new Dictionary<object, ShroudSource>();
9595

9696
// Per-cell count of each source type, used to resolve the final cell type
97-
readonly CellLayer<short> passiveVisibleCount;
98-
readonly CellLayer<short> visibleCount;
99-
readonly CellLayer<short> generatedShroudCount;
100-
readonly CellLayer<bool> explored;
101-
readonly CellLayer<bool> touched;
97+
readonly ProjectedCellLayer<short> passiveVisibleCount;
98+
readonly ProjectedCellLayer<short> visibleCount;
99+
readonly ProjectedCellLayer<short> generatedShroudCount;
100+
readonly ProjectedCellLayer<bool> explored;
101+
readonly ProjectedCellLayer<bool> touched;
102102

103103
// Per-cell cache of the resolved cell type (shroud/fog/visible)
104-
readonly CellLayer<ShroudCellType> resolvedType;
104+
readonly ProjectedCellLayer<ShroudCellType> resolvedType;
105105

106106
[Sync]
107107
bool disabled;
@@ -137,14 +137,14 @@ public Shroud(Actor self, ShroudInfo info)
137137
this.info = info;
138138
map = self.World.Map;
139139

140-
passiveVisibleCount = new CellLayer<short>(map);
141-
visibleCount = new CellLayer<short>(map);
142-
generatedShroudCount = new CellLayer<short>(map);
143-
explored = new CellLayer<bool>(map);
144-
touched = new CellLayer<bool>(map);
140+
passiveVisibleCount = new ProjectedCellLayer<short>(map);
141+
visibleCount = new ProjectedCellLayer<short>(map);
142+
generatedShroudCount = new ProjectedCellLayer<short>(map);
143+
explored = new ProjectedCellLayer<bool>(map);
144+
touched = new ProjectedCellLayer<bool>(map);
145145

146146
// Defaults to 0 = Shroud
147-
resolvedType = new CellLayer<ShroudCellType>(map);
147+
resolvedType = new ProjectedCellLayer<ShroudCellType>(map);
148148
}
149149

150150
void INotifyCreated.Created(Actor self)
@@ -164,27 +164,26 @@ void ITick.Tick(Actor self)
164164

165165
foreach (var puv in map.ProjectedCellBounds)
166166
{
167-
var uv = (MPos)puv;
168-
if (!touched[uv])
167+
if (!touched[puv])
169168
continue;
170169

171-
touched[uv] = false;
170+
touched[puv] = false;
172171

173172
var type = ShroudCellType.Shroud;
174173

175-
if (explored[uv] && (!shroudGenerationEnabled || generatedShroudCount[uv] == 0 || visibleCount[uv] > 0))
174+
if (explored[puv] && (!shroudGenerationEnabled || generatedShroudCount[puv] == 0 || visibleCount[puv] > 0))
176175
{
177-
var count = visibleCount[uv];
176+
var count = visibleCount[puv];
178177
if (passiveVisibilityEnabled)
179-
count += passiveVisibleCount[uv];
178+
count += passiveVisibleCount[puv];
180179

181180
type = count > 0 ? ShroudCellType.Visible : ShroudCellType.Fog;
182181
}
183182

184-
var oldResolvedType = resolvedType[uv];
185-
resolvedType[uv] = type;
183+
var oldResolvedType = resolvedType[puv];
184+
resolvedType[puv] = type;
186185
if (type != oldResolvedType)
187-
OnShroudChanged((PPos)uv);
186+
OnShroudChanged(puv);
188187
}
189188

190189
Hash = Sync.HashPlayer(self.Owner) + self.World.WorldTick;
@@ -232,22 +231,21 @@ public void AddSource(object key, SourceType type, PPos[] projectedCells)
232231
if (!map.Contains(puv))
233232
continue;
234233

235-
var uv = (MPos)puv;
236-
touched[uv] = true;
234+
touched[puv] = true;
237235
switch (type)
238236
{
239237
case SourceType.PassiveVisibility:
240238
passiveVisibilityEnabled = true;
241-
passiveVisibleCount[uv]++;
242-
explored[uv] = true;
239+
passiveVisibleCount[puv]++;
240+
explored[puv] = true;
243241
break;
244242
case SourceType.Visibility:
245-
visibleCount[uv]++;
246-
explored[uv] = true;
243+
visibleCount[puv]++;
244+
explored[puv] = true;
247245
break;
248246
case SourceType.Shroud:
249247
shroudGenerationEnabled = true;
250-
generatedShroudCount[uv]++;
248+
generatedShroudCount[puv]++;
251249
break;
252250
}
253251
}
@@ -264,18 +262,17 @@ public void RemoveSource(object key)
264262
// Cells outside the visible bounds don't increment visibleCount
265263
if (map.Contains(puv))
266264
{
267-
var uv = (MPos)puv;
268-
touched[uv] = true;
265+
touched[puv] = true;
269266
switch (state.Type)
270267
{
271268
case SourceType.PassiveVisibility:
272-
passiveVisibleCount[uv]--;
269+
passiveVisibleCount[puv]--;
273270
break;
274271
case SourceType.Visibility:
275-
visibleCount[uv]--;
272+
visibleCount[puv]--;
276273
break;
277274
case SourceType.Shroud:
278-
generatedShroudCount[uv]--;
275+
generatedShroudCount[puv]--;
279276
break;
280277
}
281278
}
@@ -288,11 +285,10 @@ public void ExploreProjectedCells(World world, IEnumerable<PPos> cells)
288285
{
289286
foreach (var puv in cells)
290287
{
291-
var uv = (MPos)puv;
292-
if (map.Contains(puv) && !explored[uv])
288+
if (map.Contains(puv) && !explored[puv])
293289
{
294-
touched[uv] = true;
295-
explored[uv] = true;
290+
touched[puv] = true;
291+
explored[puv] = true;
296292
}
297293
}
298294
}
@@ -304,11 +300,10 @@ public void Explore(Shroud s)
304300

305301
foreach (var puv in map.ProjectedCellBounds)
306302
{
307-
var uv = (MPos)puv;
308-
if (!explored[uv] && s.explored[uv])
303+
if (!explored[puv] && s.explored[puv])
309304
{
310-
touched[uv] = true;
311-
explored[uv] = true;
305+
touched[puv] = true;
306+
explored[puv] = true;
312307
}
313308
}
314309
}
@@ -317,11 +312,10 @@ public void ExploreAll()
317312
{
318313
foreach (var puv in map.ProjectedCellBounds)
319314
{
320-
var uv = (MPos)puv;
321-
if (!explored[uv])
315+
if (!explored[puv])
322316
{
323-
touched[uv] = true;
324-
explored[uv] = true;
317+
touched[puv] = true;
318+
explored[puv] = true;
325319
}
326320
}
327321
}
@@ -330,9 +324,8 @@ public void ResetExploration()
330324
{
331325
foreach (var puv in map.ProjectedCellBounds)
332326
{
333-
var uv = (MPos)puv;
334-
touched[uv] = true;
335-
explored[uv] = (visibleCount[uv] + passiveVisibleCount[uv]) > 0;
327+
touched[puv] = true;
328+
explored[puv] = (visibleCount[puv] + passiveVisibleCount[puv]) > 0;
336329
}
337330
}
338331

@@ -363,8 +356,7 @@ public bool IsExplored(PPos puv)
363356
if (Disabled)
364357
return map.Contains(puv);
365358

366-
var uv = (MPos)puv;
367-
return resolvedType.Contains(uv) && resolvedType[uv] > ShroudCellType.Shroud;
359+
return resolvedType.Contains(puv) && resolvedType[puv] > ShroudCellType.Shroud;
368360
}
369361

370362
public bool IsVisible(WPos pos)
@@ -379,9 +371,6 @@ public bool IsVisible(CPos cell)
379371

380372
public bool IsVisible(MPos uv)
381373
{
382-
if (!resolvedType.Contains(uv))
383-
return false;
384-
385374
foreach (var puv in map.ProjectedCellsCovering(uv))
386375
if (IsVisible(puv))
387376
return true;
@@ -395,15 +384,14 @@ public bool IsVisible(PPos puv)
395384
if (!FogEnabled)
396385
return map.Contains(puv);
397386

398-
var uv = (MPos)puv;
399-
return resolvedType.Contains(uv) && resolvedType[uv] == ShroudCellType.Visible;
387+
return resolvedType.Contains(puv) && resolvedType[puv] == ShroudCellType.Visible;
400388
}
401389

402390
public bool Contains(PPos uv)
403391
{
404392
// Check that uv is inside the map area. There is nothing special
405393
// about explored here: any of the CellLayers would have been suitable.
406-
return explored.Contains((MPos)uv);
394+
return explored.Contains(uv);
407395
}
408396
}
409397
}

0 commit comments

Comments
 (0)