Make Shroud object- instead of actor-based

This commit is contained in:
reaperrr
2016-10-23 20:09:52 +02:00
parent 6eade2dd3f
commit 0aefd87485
3 changed files with 31 additions and 50 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 // Cache of visibility that was added, so no matter what crazy trait code does, it
// can't make us invalid. // can't make us invalid.
readonly Dictionary<Actor, PPos[]> visibility = new Dictionary<Actor, PPos[]>(); readonly Dictionary<object, PPos[]> visibility = new Dictionary<object, PPos[]>();
readonly Dictionary<Actor, PPos[]> generation = new Dictionary<Actor, PPos[]>(); readonly Dictionary<object, PPos[]> generation = new Dictionary<object, PPos[]>();
[Sync] bool disabled; [Sync] bool disabled;
public bool Disabled public bool Disabled
@@ -135,11 +135,8 @@ namespace OpenRA.Traits
return ProjectedCellsInRange(map, map.CenterOfCell(cell), range); 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) foreach (var puv in visible)
{ {
// Force cells outside the visible bounds invisible // Force cells outside the visible bounds invisible
@@ -151,17 +148,17 @@ namespace OpenRA.Traits
explored[uv] = true; explored[uv] = true;
} }
if (visibility.ContainsKey(a)) if (visibility.ContainsKey(key))
throw new InvalidOperationException("Attempting to add duplicate actor visibility"); throw new InvalidOperationException("Attempting to add duplicate actor visibility");
visibility[a] = visible; visibility[key] = visible;
Invalidate(visible); Invalidate(visible);
} }
public void RemoveVisibility(Actor a) public void RemoveVisibility(object key)
{ {
PPos[] visible; PPos[] visible;
if (!visibility.TryGetValue(a, out visible)) if (!visibility.TryGetValue(key, out visible))
return; return;
foreach (var puv in visible) foreach (var puv in visible)
@@ -171,64 +168,35 @@ namespace OpenRA.Traits
visibleCount[(MPos)puv]--; visibleCount[(MPos)puv]--;
} }
visibility.Remove(a); visibility.Remove(key);
Invalidate(visible); 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) foreach (var uv in shrouded)
generatedShroudCount[(MPos)uv]++; generatedShroudCount[(MPos)uv]++;
if (generation.ContainsKey(a)) if (generation.ContainsKey(key))
throw new InvalidOperationException("Attempting to add duplicate shroud generation"); throw new InvalidOperationException("Attempting to add duplicate shroud generation");
generation[a] = shrouded; generation[key] = shrouded;
Invalidate(shrouded); Invalidate(shrouded);
} }
public void RemoveShroudGeneration(Actor a) public void RemoveShroudGeneration(object key)
{ {
PPos[] shrouded; PPos[] shrouded;
if (!generation.TryGetValue(a, out shrouded)) if (!generation.TryGetValue(key, out shrouded))
return; return;
foreach (var uv in shrouded) foreach (var uv in shrouded)
generatedShroudCount[(MPos)uv]--; generatedShroudCount[(MPos)uv]--;
generation.Remove(a); generation.Remove(key);
Invalidate(shrouded); 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) public void ExploreProjectedCells(World world, IEnumerable<PPos> cells)
{ {
var changed = new HashSet<PPos>(); var changed = new HashSet<PPos>();

View File

@@ -20,8 +20,15 @@ namespace OpenRA.Mods.Common.Traits
{ {
public CreatesShroud(Actor self, CreatesShroudInfo info) public CreatesShroud(Actor self, CreatesShroudInfo info)
: base(self, 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); } protected override void AddCellsToPlayerShroud(Actor self, Player p, PPos[] uv)
{
if (!self.Owner.IsAlliedWith(p))
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(); } protected override bool IsDisabled(Actor self) { return self.IsDisabled(); }
} }
} }

View File

@@ -20,7 +20,13 @@ namespace OpenRA.Mods.Common.Traits
{ {
public RevealsShroud(Actor self, RevealsShroudInfo info) public RevealsShroud(Actor self, RevealsShroudInfo info)
: base(self, 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); } protected override void AddCellsToPlayerShroud(Actor self, Player p, PPos[] uv)
{
if (self.Owner.IsAlliedWith(p))
p.Shroud.AddProjectedVisibility(this, uv);
}
protected override void RemoveCellsFromPlayerShroud(Actor self, Player p) { p.Shroud.RemoveVisibility(this); }
} }
} }