Add passive visibility type.

This commit is contained in:
Paul Chote
2016-10-25 20:30:39 +01:00
parent fdac5a6d8a
commit 983d8ac39e
2 changed files with 31 additions and 5 deletions

View File

@@ -40,7 +40,7 @@ namespace OpenRA.Traits
public class Shroud : ISync, INotifyCreated public class Shroud : ISync, INotifyCreated
{ {
public enum SourceType : byte { Shroud, Visibility } public enum SourceType : byte { PassiveVisibility, Shroud, Visibility }
public event Action<IEnumerable<PPos>> CellsChanged; public event Action<IEnumerable<PPos>> CellsChanged;
enum ShroudCellType : byte { Shroud, Fog, Visible } enum ShroudCellType : byte { Shroud, Fog, Visible }
@@ -64,6 +64,7 @@ namespace OpenRA.Traits
readonly Dictionary<object, ShroudSource> sources = new Dictionary<object, ShroudSource>(); readonly Dictionary<object, ShroudSource> sources = new Dictionary<object, ShroudSource>();
// Per-cell count of each source type, used to resolve the final cell type // Per-cell count of each source type, used to resolve the final cell type
readonly CellLayer<short> passiveVisibleCount;
readonly CellLayer<short> visibleCount; readonly CellLayer<short> visibleCount;
readonly CellLayer<short> generatedShroudCount; readonly CellLayer<short> generatedShroudCount;
readonly CellLayer<bool> explored; readonly CellLayer<bool> explored;
@@ -97,6 +98,7 @@ namespace OpenRA.Traits
// Enabled at runtime on first use // Enabled at runtime on first use
bool shroudGenerationEnabled; bool shroudGenerationEnabled;
bool passiveVisibilityEnabled;
public Shroud(Actor self, ShroudInfo info) public Shroud(Actor self, ShroudInfo info)
{ {
@@ -104,6 +106,7 @@ namespace OpenRA.Traits
this.info = info; this.info = info;
map = self.World.Map; map = self.World.Map;
passiveVisibleCount = new CellLayer<short>(map);
visibleCount = new CellLayer<short>(map); visibleCount = new CellLayer<short>(map);
generatedShroudCount = new CellLayer<short>(map); generatedShroudCount = new CellLayer<short>(map);
explored = new CellLayer<bool>(map); explored = new CellLayer<bool>(map);
@@ -130,7 +133,13 @@ namespace OpenRA.Traits
var type = ShroudCellType.Shroud; var type = ShroudCellType.Shroud;
if (explored[uv] && (!shroudGenerationEnabled || generatedShroudCount[uv] == 0 || visibleCount[uv] > 0)) if (explored[uv] && (!shroudGenerationEnabled || generatedShroudCount[uv] == 0 || visibleCount[uv] > 0))
type = visibleCount[uv] > 0 ? ShroudCellType.Visible: ShroudCellType.Fog; {
var count = visibleCount[uv];
if (passiveVisibilityEnabled)
count += passiveVisibleCount[uv];
type = count > 0 ? ShroudCellType.Visible : ShroudCellType.Fog;
}
resolvedType[uv] = type; resolvedType[uv] = type;
} }
@@ -182,6 +191,11 @@ namespace OpenRA.Traits
var uv = (MPos)puv; var uv = (MPos)puv;
switch (type) switch (type)
{ {
case SourceType.PassiveVisibility:
passiveVisibilityEnabled = true;
passiveVisibleCount[uv]++;
explored[uv] = true;
break;
case SourceType.Visibility: case SourceType.Visibility:
visibleCount[uv]++; visibleCount[uv]++;
explored[uv] = true; explored[uv] = true;
@@ -210,6 +224,9 @@ namespace OpenRA.Traits
var uv = (MPos)puv; var uv = (MPos)puv;
switch (state.Type) switch (state.Type)
{ {
case SourceType.PassiveVisibility:
passiveVisibleCount[uv]--;
break;
case SourceType.Visibility: case SourceType.Visibility:
visibleCount[uv]--; visibleCount[uv]--;
break; break;
@@ -281,7 +298,7 @@ namespace OpenRA.Traits
foreach (var puv in map.ProjectedCellBounds) foreach (var puv in map.ProjectedCellBounds)
{ {
var uv = (MPos)puv; var uv = (MPos)puv;
var visible = visibleCount[uv] > 0; var visible = visibleCount[uv] + passiveVisibleCount[uv] > 0;
if (explored[uv] != visible) if (explored[uv] != visible)
{ {
explored[uv] = visible; explored[uv] = visible;

View File

@@ -18,22 +18,31 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Stance the watching player needs to see the shroud removed.")] [Desc("Stance the watching player needs to see the shroud removed.")]
public readonly Stance ValidStances = Stance.Ally; public readonly Stance ValidStances = Stance.Ally;
[Desc("Can this actor reveal shroud generated by the GeneratesShroud trait?")]
public readonly bool RevealGeneratedShroud = true;
public override object Create(ActorInitializer init) { return new RevealsShroud(init.Self, this); } public override object Create(ActorInitializer init) { return new RevealsShroud(init.Self, this); }
} }
public class RevealsShroud : AffectsShroud public class RevealsShroud : AffectsShroud
{ {
readonly RevealsShroudInfo info; readonly RevealsShroudInfo info;
readonly Shroud.SourceType type;
public RevealsShroud(Actor self, RevealsShroudInfo info) public RevealsShroud(Actor self, RevealsShroudInfo info)
: base(self, info) { this.info = info; } : base(self, info)
{
this.info = info;
type = info.RevealGeneratedShroud ? Shroud.SourceType.Visibility
: Shroud.SourceType.PassiveVisibility;
}
protected override void AddCellsToPlayerShroud(Actor self, Player p, PPos[] uv) protected override void AddCellsToPlayerShroud(Actor self, Player p, PPos[] uv)
{ {
if (!info.ValidStances.HasStance(p.Stances[self.Owner])) if (!info.ValidStances.HasStance(p.Stances[self.Owner]))
return; return;
p.Shroud.AddSource(this, Shroud.SourceType.Visibility, uv); p.Shroud.AddSource(this, type, uv);
} }
protected override void RemoveCellsFromPlayerShroud(Actor self, Player p) { p.Shroud.RemoveSource(this); } protected override void RemoveCellsFromPlayerShroud(Actor self, Player p) { p.Shroud.RemoveSource(this); }