Merge pull request #12308 from reaperrr/refactor-shroud-traits

Refactor Shroud and shroud-modifying traits
This commit is contained in:
abcdefg30
2016-10-30 17:55:59 +01:00
committed by GitHub
4 changed files with 66 additions and 64 deletions

View File

@@ -54,8 +54,8 @@ namespace OpenRA.Traits
// Cache of visibility that was added, so no matter what crazy trait code does, it
// can't make us invalid.
readonly Dictionary<Actor, PPos[]> visibility = new Dictionary<Actor, PPos[]>();
readonly Dictionary<Actor, PPos[]> generation = new Dictionary<Actor, PPos[]>();
readonly Dictionary<object, PPos[]> visibility = new Dictionary<object, PPos[]>();
readonly Dictionary<object, PPos[]> generation = new Dictionary<object, PPos[]>();
[Sync] bool disabled;
public bool Disabled
@@ -135,11 +135,8 @@ namespace OpenRA.Traits
return ProjectedCellsInRange(map, map.CenterOfCell(cell), range);
}
public void AddProjectedVisibility(Actor a, PPos[] visible)
public void AddProjectedVisibility(object key, PPos[] visible)
{
if (!a.Owner.IsAlliedWith(self.Owner))
return;
foreach (var puv in visible)
{
// Force cells outside the visible bounds invisible
@@ -151,17 +148,17 @@ namespace OpenRA.Traits
explored[uv] = true;
}
if (visibility.ContainsKey(a))
if (visibility.ContainsKey(key))
throw new InvalidOperationException("Attempting to add duplicate actor visibility");
visibility[a] = visible;
visibility[key] = visible;
Invalidate(visible);
}
public void RemoveVisibility(Actor a)
public void RemoveVisibility(object key)
{
PPos[] visible;
if (!visibility.TryGetValue(a, out visible))
if (!visibility.TryGetValue(key, out visible))
return;
foreach (var puv in visible)
@@ -171,64 +168,35 @@ namespace OpenRA.Traits
visibleCount[(MPos)puv]--;
}
visibility.Remove(a);
visibility.Remove(key);
Invalidate(visible);
}
public void AddProjectedShroudGeneration(Actor a, PPos[] shrouded)
public void AddProjectedShroudGeneration(object key, PPos[] shrouded)
{
if (a.Owner.IsAlliedWith(self.Owner))
return;
foreach (var uv in shrouded)
generatedShroudCount[(MPos)uv]++;
if (generation.ContainsKey(a))
if (generation.ContainsKey(key))
throw new InvalidOperationException("Attempting to add duplicate shroud generation");
generation[a] = shrouded;
generation[key] = shrouded;
Invalidate(shrouded);
}
public void RemoveShroudGeneration(Actor a)
public void RemoveShroudGeneration(object key)
{
PPos[] shrouded;
if (!generation.TryGetValue(a, out shrouded))
if (!generation.TryGetValue(key, out shrouded))
return;
foreach (var uv in shrouded)
generatedShroudCount[(MPos)uv]--;
generation.Remove(a);
generation.Remove(key);
Invalidate(shrouded);
}
public void UpdatePlayerStance(World w, Player player, Stance oldStance, Stance newStance)
{
if (oldStance == newStance)
return;
foreach (var a in w.Actors.Where(a => a.Owner == player))
{
PPos[] visible = null;
PPos[] shrouded = null;
foreach (var p in self.World.Players)
{
if (p.Shroud.visibility.TryGetValue(self, out visible))
{
p.Shroud.RemoveVisibility(self);
p.Shroud.AddProjectedVisibility(self, visible);
}
if (p.Shroud.generation.TryGetValue(self, out shrouded))
{
p.Shroud.RemoveShroudGeneration(self);
p.Shroud.AddProjectedShroudGeneration(self, shrouded);
}
}
}
}
public void ExploreProjectedCells(World world, IEnumerable<PPos> cells)
{
var changed = new HashSet<PPos>();

View File

@@ -14,30 +14,29 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public abstract class AffectsShroudInfo : ITraitInfo
public abstract class AffectsShroudInfo : UpgradableTraitInfo
{
public readonly WDist Range = WDist.Zero;
[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 object Create(ActorInitializer init);
}
public abstract class AffectsShroud : ITick, ISync, INotifyAddedToWorld, INotifyRemovedFromWorld
public abstract class AffectsShroud : UpgradableTrait<AffectsShroudInfo>, ITick, ISync, INotifyAddedToWorld, INotifyRemovedFromWorld
{
static readonly PPos[] NoCells = { };
readonly AffectsShroudInfo info;
[Sync] CPos cachedLocation;
[Sync] bool cachedDisabled;
[Sync] bool cachedTraitDisabled;
protected abstract void AddCellsToPlayerShroud(Actor self, Player player, PPos[] uv);
protected abstract void RemoveCellsFromPlayerShroud(Actor self, Player player);
protected virtual bool IsDisabled(Actor self) { return false; }
public AffectsShroud(Actor self, AffectsShroudInfo info) { this.info = info; }
public AffectsShroud(Actor self, AffectsShroudInfo info)
: base(info) { }
PPos[] ProjectedCells(Actor self)
{
@@ -46,7 +45,7 @@ namespace OpenRA.Mods.Common.Traits
if (range == WDist.Zero)
return NoCells;
if (info.Type == VisibilityType.Footprint)
if (Info.Type == VisibilityType.Footprint)
return self.OccupiesSpace.OccupiedCells()
.SelectMany(kv => Shroud.ProjectedCellsInRange(map, kv.First, range))
.Distinct().ToArray();
@@ -55,7 +54,7 @@ namespace OpenRA.Mods.Common.Traits
.ToArray();
}
public void Tick(Actor self)
void ITick.Tick(Actor self)
{
if (!self.IsInWorld)
return;
@@ -64,12 +63,14 @@ namespace OpenRA.Mods.Common.Traits
var projectedPos = centerPosition - new WVec(0, centerPosition.Z, centerPosition.Z);
var projectedLocation = self.World.Map.CellContaining(projectedPos);
var disabled = IsDisabled(self);
var traitDisabled = IsTraitDisabled;
if (cachedLocation == projectedLocation && cachedDisabled == disabled)
if (cachedLocation == projectedLocation && traitDisabled == cachedTraitDisabled && cachedDisabled == disabled)
return;
cachedLocation = projectedLocation;
cachedDisabled = disabled;
cachedTraitDisabled = traitDisabled;
var cells = ProjectedCells(self);
foreach (var p in self.World.Players)
@@ -79,23 +80,25 @@ namespace OpenRA.Mods.Common.Traits
}
}
public void AddedToWorld(Actor self)
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);
cachedDisabled = IsDisabled(self);
cachedTraitDisabled = IsTraitDisabled;
var cells = ProjectedCells(self);
foreach (var p in self.World.Players)
AddCellsToPlayerShroud(self, p, cells);
}
public void RemovedFromWorld(Actor self)
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
{
foreach (var p in self.World.Players)
RemoveCellsFromPlayerShroud(self, p);
}
public WDist Range { get { return cachedDisabled ? WDist.Zero : info.Range; } }
public WDist Range { get { return (cachedDisabled || cachedTraitDisabled) ? WDist.Zero : Info.Range; } }
}
}

View File

@@ -9,19 +9,35 @@
*/
#endregion
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class CreatesShroudInfo : AffectsShroudInfo
{
[Desc("Stance the watching player needs to see the generated shroud.")]
public readonly Stance ValidStances = Stance.Neutral | Stance.Enemy;
public override object Create(ActorInitializer init) { return new CreatesShroud(init.Self, this); }
}
public class CreatesShroud : AffectsShroud
{
readonly CreatesShroudInfo info;
public CreatesShroud(Actor self, CreatesShroudInfo info)
: base(self, info) { }
protected override void AddCellsToPlayerShroud(Actor self, Player p, PPos[] uv) { p.Shroud.AddProjectedShroudGeneration(self, uv); }
protected override void RemoveCellsFromPlayerShroud(Actor self, Player p) { p.Shroud.RemoveShroudGeneration(self); }
: base(self, info) { this.info = info; }
protected override void AddCellsToPlayerShroud(Actor self, Player p, PPos[] uv)
{
if (!info.ValidStances.HasStance(p.Stances[self.Owner]))
return;
p.Shroud.AddProjectedShroudGeneration(this, uv);
}
protected override void RemoveCellsFromPlayerShroud(Actor self, Player p) { p.Shroud.RemoveShroudGeneration(this); }
protected override bool IsDisabled(Actor self) { return self.IsDisabled(); }
}
}

View File

@@ -9,18 +9,33 @@
*/
#endregion
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class RevealsShroudInfo : AffectsShroudInfo
{
[Desc("Stance the watching player needs to see the shroud removed.")]
public readonly Stance ValidStances = Stance.Ally;
public override object Create(ActorInitializer init) { return new RevealsShroud(init.Self, this); }
}
public class RevealsShroud : AffectsShroud
{
readonly RevealsShroudInfo info;
public RevealsShroud(Actor self, RevealsShroudInfo info)
: base(self, info) { }
protected override void AddCellsToPlayerShroud(Actor self, Player p, PPos[] uv) { p.Shroud.AddProjectedVisibility(self, uv); }
protected override void RemoveCellsFromPlayerShroud(Actor self, Player p) { p.Shroud.RemoveVisibility(self); }
: base(self, info) { this.info = info; }
protected override void AddCellsToPlayerShroud(Actor self, Player p, PPos[] uv)
{
if (!info.ValidStances.HasStance(p.Stances[self.Owner]))
return;
p.Shroud.AddProjectedVisibility(this, uv);
}
protected override void RemoveCellsFromPlayerShroud(Actor self, Player p) { p.Shroud.RemoveVisibility(this); }
}
}