Merge pull request #3020 from pchote/shroud-sanity

Refactor per-player shrouds.
This commit is contained in:
Chris Forbes
2013-04-12 01:53:31 -07:00
45 changed files with 364 additions and 340 deletions

View File

@@ -139,7 +139,7 @@ namespace OpenRA.Graphics
foreach (var t in world.ActorsWithTrait<IRadarSignature>())
{
if (!world.RenderedShroud.IsVisible(t.Actor))
if (world.ShroudObscures(t.Actor))
continue;
var color = t.Trait.RadarSignatureColor(t.Actor);
@@ -158,7 +158,7 @@ namespace OpenRA.Graphics
var map = world.Map;
var size = Exts.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height));
var bitmap = new Bitmap(size, size);
if (world.RenderedShroud.Disabled)
if (world.RenderPlayer == null)
return bitmap;
var bitmapData = bitmap.LockBits(bitmap.Bounds(),
@@ -174,11 +174,10 @@ namespace OpenRA.Graphics
for (var x = 0; x < map.Bounds.Width; x++)
for (var y = 0; y < map.Bounds.Height; y++)
{
var mapX = x + map.Bounds.Left;
var mapY = y + map.Bounds.Top;
if (!world.RenderedShroud.IsExplored(mapX, mapY))
var p = new CPos(x + map.Bounds.Left, y + map.Bounds.Top);
if (world.ShroudObscures(p))
*(c + (y * bitmapData.Stride >> 2) + x) = shroud;
else if (!world.RenderedShroud.IsVisible(mapX,mapY))
else if (world.FogObscures(p))
*(c + (y * bitmapData.Stride >> 2) + x) = fog;
}
}

View File

@@ -15,26 +15,14 @@ namespace OpenRA.Graphics
{
public class ShroudRenderer
{
World world;
Traits.Shroud shroud {
get {
return world.RenderedShroud;
}
}
Map map;
ShroudInfo shroudInfo;
Sprite[] shadowBits = Game.modData.SpriteLoader.LoadAllSprites("shadow");
Sprite[,] sprites, fogSprites;
int shroudHash;
Map map;
public ShroudRenderer(World world)
{
this.world = world;
this.map = world.Map;
sprites = new Sprite[map.MapSize.X, map.MapSize.Y];
fogSprites = new Sprite[map.MapSize.X, map.MapSize.Y];
}
bool initializePalettes = true;
PaletteReference fogPalette, shroudPalette;
static readonly byte[][] SpecialShroudTiles =
{
@@ -56,81 +44,128 @@ namespace OpenRA.Graphics
new byte[] { 46 },
};
Sprite ChooseShroud(int i, int j)
public ShroudRenderer(World world)
{
if( !shroud.IsExplored( i, j ) ) return shadowBits[ 0xf ];
this.map = world.Map;
shroudInfo = Rules.Info["player"].Traits.Get<ShroudInfo>();
// bits are for unexploredness: up, right, down, left
var v = 0;
// bits are for unexploredness: TL, TR, BR, BL
var u = 0;
sprites = new Sprite[map.MapSize.X, map.MapSize.Y];
fogSprites = new Sprite[map.MapSize.X, map.MapSize.Y];
if( !shroud.IsExplored( i, j - 1 ) ) { v |= 1; u |= 3; }
if( !shroud.IsExplored( i + 1, j ) ) { v |= 2; u |= 6; }
if( !shroud.IsExplored( i, j + 1 ) ) { v |= 4; u |= 12; }
if( !shroud.IsExplored( i - 1, j ) ) { v |= 8; u |= 9; }
var uSides = u;
if( !shroud.IsExplored( i - 1, j - 1 ) ) u |= 1;
if( !shroud.IsExplored( i + 1, j - 1 ) ) u |= 2;
if( !shroud.IsExplored( i + 1, j + 1 ) ) u |= 4;
if( !shroud.IsExplored( i - 1, j + 1 ) ) u |= 8;
return shadowBits[ SpecialShroudTiles[ u ^ uSides ][ v ] ];
// Force update on first render
shroudHash = -1;
}
Sprite ChooseFog(int i, int j)
Sprite ChooseShroud(Shroud s, int i, int j)
{
if (!shroud.IsVisible(i,j)) return shadowBits[0xf];
if (!shroud.IsExplored(i, j)) return shadowBits[0xf];
if (!s.IsExplored(i, j))
return shadowBits[0xf];
// bits are for unexploredness: up, right, down, left
var v = 0;
// bits are for unexploredness: TL, TR, BR, BL
var u = 0;
if (!shroud.IsVisible(i, j - 1)) { v |= 1; u |= 3; }
if (!shroud.IsVisible(i + 1, j)) { v |= 2; u |= 6; }
if (!shroud.IsVisible(i, j + 1)) { v |= 4; u |= 12; }
if (!shroud.IsVisible(i - 1, j)) { v |= 8; u |= 9; }
if (!s.IsExplored(i, j - 1)) { v |= 1; u |= 3; }
if (!s.IsExplored(i + 1, j)) { v |= 2; u |= 6; }
if (!s.IsExplored(i, j + 1)) { v |= 4; u |= 12; }
if (!s.IsExplored(i - 1, j)) { v |= 8; u |= 9; }
var uSides = u;
if (!shroud.IsVisible(i - 1, j - 1)) u |= 1;
if (!shroud.IsVisible(i + 1, j - 1)) u |= 2;
if (!shroud.IsVisible(i + 1, j + 1)) u |= 4;
if (!shroud.IsVisible(i - 1, j + 1)) u |= 8;
if (!s.IsExplored(i - 1, j - 1)) u |= 1;
if (!s.IsExplored(i + 1, j - 1)) u |= 2;
if (!s.IsExplored(i + 1, j + 1)) u |= 4;
if (!s.IsExplored(i - 1, j + 1)) u |= 8;
return shadowBits[SpecialShroudTiles[u ^ uSides][v]];
}
bool initializePalettes = true;
PaletteReference fogPalette, shroudPalette;
internal void Draw(WorldRenderer wr)
Sprite ChooseFog(Shroud s, int i, int j)
{
if (!s.IsVisible(i, j)) return shadowBits[0xf];
if (!s.IsExplored(i, j)) return shadowBits[0xf];
// bits are for unexploredness: up, right, down, left
var v = 0;
// bits are for unexploredness: TL, TR, BR, BL
var u = 0;
if (!s.IsVisible(i, j - 1)) { v |= 1; u |= 3; }
if (!s.IsVisible(i + 1, j)) { v |= 2; u |= 6; }
if (!s.IsVisible(i, j + 1)) { v |= 4; u |= 12; }
if (!s.IsVisible(i - 1, j)) { v |= 8; u |= 9; }
var uSides = u;
if (!s.IsVisible(i - 1, j - 1)) u |= 1;
if (!s.IsVisible(i + 1, j - 1)) u |= 2;
if (!s.IsVisible(i + 1, j + 1)) u |= 4;
if (!s.IsVisible(i - 1, j + 1)) u |= 8;
return shadowBits[SpecialShroudTiles[u ^ uSides][v]];
}
void GenerateSprites(Shroud shroud)
{
var hash = shroud != null ? shroud.Hash : 0;
if (shroudHash == hash)
return;
shroudHash = hash;
if (shroud == null)
{
// Players with no shroud see the whole map so we only need to set the edges
var b = map.Bounds;
for (int i = b.Left; i < b.Right; i++)
for (int j = b.Top; j < b.Bottom; j++)
{
var v = 0;
var u = 0;
if (j == b.Top) { v |= 1; u |= 3; }
if (i == b.Right - 1) { v |= 2; u |= 6; }
if (j == b.Bottom - 1) { v |= 4; u |= 12; }
if (i == b.Left) { v |= 8; u |= 9; }
var uSides = u;
if (i == b.Left && j == b.Top) u |= 1;
if (i == b.Right - 1 && j == b.Top) u |= 2;
if (i == b.Right - 1 && j == b.Bottom - 1) u |= 4;
if (i == b.Left && j == b.Bottom - 1) u |= 8;
sprites[i, j] = fogSprites[i, j] = shadowBits[SpecialShroudTiles[u ^ uSides][v]];
}
}
else
{
for (int i = map.Bounds.Left; i < map.Bounds.Right; i++)
for (int j = map.Bounds.Top; j < map.Bounds.Bottom; j++)
sprites[i, j] = ChooseShroud(shroud, i, j);
for (int i = map.Bounds.Left; i < map.Bounds.Right; i++)
for (int j = map.Bounds.Top; j < map.Bounds.Bottom; j++)
fogSprites[i, j] = ChooseFog(shroud, i, j);
}
}
internal void Draw(WorldRenderer wr, Shroud shroud)
{
if (initializePalettes)
{
fogPalette = wr.Palette("fog");
if (shroudInfo.Fog)
fogPalette = wr.Palette("fog");
shroudPalette = wr.Palette("shroud");
initializePalettes = false;
}
if (shroud != null && shroud.dirty)
{
shroud.dirty = false;
for (int i = map.Bounds.Left; i < map.Bounds.Right; i++)
for (int j = map.Bounds.Top; j < map.Bounds.Bottom; j++)
sprites[i, j] = ChooseShroud(i, j);
for (int i = map.Bounds.Left; i < map.Bounds.Right; i++)
for (int j = map.Bounds.Top; j < map.Bounds.Bottom; j++)
fogSprites[i, j] = ChooseFog(i, j);
}
GenerateSprites(shroud);
var clipRect = Game.viewport.WorldBounds(wr.world);
// We draw the shroud when disabled to hide the sharp map edges
DrawShroud(wr, clipRect, sprites, shroudPalette);
if (wr.world.WorldActor.HasTrait<Fog>())
if (shroudInfo.Fog)
DrawShroud(wr, clipRect, fogSprites, fogPalette);
}

View File

@@ -72,9 +72,9 @@ namespace OpenRA.Graphics
if (firstRow < 0) firstRow = 0;
if (lastRow > map.Bounds.Height) lastRow = map.Bounds.Height;
if (world.RenderedPlayer != null && !world.RenderedShroud.Disabled && world.RenderedShroud.Bounds.HasValue)
if (world.VisibleBounds.HasValue)
{
var r = world.RenderedShroud.Bounds.Value;
var r = world.VisibleBounds.Value;
if (firstRow < r.Top - map.Bounds.Top)
firstRow = r.Top - map.Bounds.Top;

View File

@@ -196,7 +196,7 @@ namespace OpenRA.Graphics
cachedScroll = scrollPosition;
}
var b = world.RenderedShroud.Bounds;
var b = world.VisibleBounds;
return (b.HasValue) ? Rectangle.Intersect(cachedRect, b.Value) : cachedRect;
}
}

View File

@@ -127,7 +127,8 @@ namespace OpenRA.Graphics
if (world.OrderGenerator != null)
world.OrderGenerator.RenderAfterWorld(this, world);
shroudRenderer.Draw( this );
var renderShroud = world.RenderPlayer != null ? world.RenderPlayer.Shroud : null;
shroudRenderer.Draw(this, renderShroud);
Game.Renderer.DisableScissor();
foreach (var g in world.Selection.Actors.Where(a => !a.Destroyed)

View File

@@ -182,7 +182,6 @@
<Compile Include="Traits\ValidateOrder.cs" />
<Compile Include="Traits\Waypoint.cs" />
<Compile Include="Traits\World\Country.cs" />
<Compile Include="Traits\World\Fog.cs" />
<Compile Include="Traits\World\PlayerColorPalette.cs" />
<Compile Include="Traits\World\ResourceLayer.cs" />
<Compile Include="Traits\World\ResourceType.cs" />

View File

@@ -77,7 +77,7 @@ namespace OpenRA
}
PlayerActor = world.CreateActor("Player", new TypeDictionary { new OwnerInit(this) });
Shroud = PlayerActor.Trait<Shroud>();
Shroud.Owner = this;
// Enable the bot logic on the host
IsBot = botType != null;
if (IsBot && Game.IsHost)
@@ -92,5 +92,10 @@ namespace OpenRA
}
public Dictionary<Player, Stance> Stances = new Dictionary<Player, Stance>();
public bool IsAlliedWith(Player p)
{
// Observers are considered as allies
return p == null || Stances[p] == Stance.Ally;
}
}
}

View File

@@ -32,29 +32,24 @@ namespace OpenRA.Traits
public void Tick(Actor self)
{
// TODO: don't tick all the time.
if(self.Owner == null) return;
if (self.Owner == null)
return;
if (previousLocation != self.Location && v != null) {
var shrouds = self.World.ActorsWithTrait<Shroud>().Select(s => s.Actor.Owner.Shroud);
if (previousLocation != self.Location && v != null)
{
previousLocation = self.Location;
var shrouds = self.World.ActorsWithTrait<Shroud>().Select(s => s.Actor.Owner.Shroud);
foreach (var shroud in shrouds) {
foreach (var shroud in shrouds)
shroud.UnhideActor(self, v, Info.Range);
}
}
if (!self.TraitsImplementing<IDisable>().Any(d => d.Disabled)) {
var shrouds = self.World.ActorsWithTrait<Shroud>().Select(s => s.Actor.Owner.Shroud);
foreach (var shroud in shrouds) {
if (!self.TraitsImplementing<IDisable>().Any(d => d.Disabled))
foreach (var shroud in shrouds)
shroud.HideActor(self, Info.Range);
}
}
else {
var shrouds = self.World.ActorsWithTrait<Shroud>().Select(s => s.Actor.Owner.Shroud);
foreach (var shroud in shrouds) {
else
foreach (var shroud in shrouds)
shroud.UnhideActor(self, v, Info.Range);
}
}
v = new Shroud.ActorVisibility {
vis = Shroud.GetVisOrigins(self).ToArray()

View File

@@ -81,7 +81,7 @@ namespace OpenRA.Traits
case "DevShroudDisable":
{
DisableShroud ^= true;
self.Owner.Shroud.Disabled = DisableShroud;
self.World.RenderPlayer = DisableShroud ? null : self.Owner;
break;
}
case "DevPathDebug":

View File

@@ -31,8 +31,10 @@ namespace OpenRA.Traits
public void RenderAfterWorld(WorldRenderer wr)
{
var bounds = self.Bounds.Value;
if (self.World.FogObscures(self))
return;
var bounds = self.Bounds.Value;
var xy = new float2(bounds.Left, bounds.Top);
var xY = new float2(bounds.Left, bounds.Bottom);
@@ -54,7 +56,8 @@ namespace OpenRA.Traits
void DrawPips(WorldRenderer wr, Actor self, float2 basePosition)
{
if (self.Owner != self.World.RenderedPlayer) return;
if (!self.Owner.IsAlliedWith(self.World.RenderPlayer))
return;
var pipSources = self.TraitsImplementing<IPips>();
if (pipSources.Count() == 0)
@@ -95,7 +98,8 @@ namespace OpenRA.Traits
void DrawTags(WorldRenderer wr, Actor self, float2 basePosition)
{
if (self.Owner != self.World.RenderedPlayer) return;
if (!self.Owner.IsAlliedWith(self.World.RenderPlayer))
return;
// If a mod wants to implement a unit with multiple tags, then they are placed on multiple rows
var tagxyBase = basePosition + new float2(-16, 2); // Correct for the offset in the shp file

View File

@@ -86,7 +86,7 @@ namespace OpenRA.Traits
Color RadarSignatureColor(Actor self);
}
public interface IVisibilityModifier { bool IsVisible(Shroud s, Actor self); }
public interface IVisibilityModifier { bool IsVisible(Actor self, Player byPlayer); }
public interface IRadarColorModifier { Color RadarColorOverride(Actor self); }
public interface IHasLocation { PPos PxPosition { get; } }

View File

@@ -1,20 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.FileFormats;
namespace OpenRA.Traits
{
[Desc("This tag trait will enable fog of war in ShroudRenderer.",
"Don't forget about HiddenUnderFog and FrozenUnderFog.")]
public class FogInfo : TraitInfo<Fog> { }
public class Fog { }
}

View File

@@ -40,7 +40,7 @@ namespace OpenRA.Traits
for (int x = clip.Left; x < clip.Right; x++)
for (int y = clip.Top; y < clip.Bottom; y++)
{
if (!world.RenderedShroud.IsExplored(new CPos(x, y)))
if (world.ShroudObscures(new CPos(x, y)))
continue;
var c = content[x, y];

View File

@@ -12,69 +12,77 @@ using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Graphics;
namespace OpenRA.Traits
{
public class ShroudInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new Shroud(init.world); }
public readonly bool Shroud = true;
public readonly bool Fog = true;
public object Create(ActorInitializer init) { return new Shroud(init.self, this); }
}
public class Shroud : ISync
public class Shroud
{
public ShroudInfo Info;
Actor self;
Map map;
World world;
[Sync] public Player Owner;
public int[,] visibleCells;
public bool[,] exploredCells;
public bool[,] foggedCells;
public Rectangle? exploredBounds;
bool disabled = false;
public bool dirty = true;
[Sync] public bool Disabled
int[,] visibleCells;
bool[,] exploredCells;
bool[,] foggedCells;
public Rectangle ExploredBounds { get; private set; }
public int Hash { get; private set; }
public Shroud(Actor self, ShroudInfo info)
{
get { return disabled; }
set { disabled = value; Dirty(); }
}
Info = info;
this.self = self;
map = self.World.Map;
public bool Observing
{
get { return world.IsShellmap || (world.LocalPlayer == null && Owner == null);; }
}
public Rectangle? Bounds
{
get { return Disabled ? null : exploredBounds; }
}
public Action Dirty = () => { };
public Shroud(World world)
{
this.world = world;
map = world.Map;
visibleCells = new int[map.MapSize.X, map.MapSize.Y];
exploredCells = new bool[map.MapSize.X, map.MapSize.Y];
foggedCells = new bool[map.MapSize.X, map.MapSize.Y];
world.ActorAdded += AddActor;
world.ActorRemoved += RemoveActor;
Dirty += () => dirty = true;
self.World.ActorAdded += AddActor;
self.World.ActorRemoved += RemoveActor;
if (!info.Shroud)
ExploredBounds = map.Bounds;
}
void Invalidate()
{
Hash = Sync.hash_player(self.Owner) + self.World.FrameNumber * 3;
}
// cache of positions that were added, so no matter what crazy trait code does, it
// can't make us invalid.
public class ActorVisibility { [Sync] public int range; [Sync] public CPos[] vis; }
public class ActorVisibility
{
[Sync] public int range;
[Sync] public CPos[] vis;
}
public Dictionary<Actor, ActorVisibility> vis = new Dictionary<Actor, ActorVisibility>();
static IEnumerable<CPos> FindVisibleTiles(World world, CPos a, int r)
{
var min = a - new CVec(r, r);
var max = a + new CVec(r, r);
if (min.X < world.Map.Bounds.Left - 1) min = new CPos(world.Map.Bounds.Left - 1, min.Y);
if (min.Y < world.Map.Bounds.Top - 1) min = new CPos(min.X, world.Map.Bounds.Top - 1);
if (max.X > world.Map.Bounds.Right) max = new CPos(world.Map.Bounds.Right, max.Y);
if (max.Y > world.Map.Bounds.Bottom) max = new CPos(max.X, world.Map.Bounds.Bottom);
if (min.X < world.Map.Bounds.Left - 1)
min = new CPos(world.Map.Bounds.Left - 1, min.Y);
if (min.Y < world.Map.Bounds.Top - 1)
min = new CPos(min.X, world.Map.Bounds.Top - 1);
if (max.X > world.Map.Bounds.Right)
max = new CPos(world.Map.Bounds.Right, max.Y);
if (max.Y > world.Map.Bounds.Bottom)
max = new CPos(max.X, world.Map.Bounds.Bottom);
for (var j = min.Y; j <= max.Y; j++)
for (var i = min.X; i <= max.X; i++)
@@ -84,13 +92,12 @@ namespace OpenRA.Traits
public void AddActor(Actor a)
{
if (!a.HasTrait<RevealsShroud>()) return;
if (a.Owner == null || Owner == null) return;
if(a.Owner.Stances[Owner] != Stance.Ally) return;
if (!a.HasTrait<RevealsShroud>() || !a.Owner.IsAlliedWith(self.Owner))
return;
ActorVisibility v = a.Sight;
if (v.range == 0) return; // don't bother for things that can't see
if (v.range == 0)
return;
foreach (var p in v.vis)
{
@@ -102,17 +109,16 @@ namespace OpenRA.Traits
}
var box = new Rectangle(p.X - v.range, p.Y - v.range, 2 * v.range + 1, 2 * v.range + 1);
exploredBounds = (exploredBounds.HasValue) ? Rectangle.Union(exploredBounds.Value, box) : box;
ExploredBounds = Rectangle.Union(ExploredBounds, box);
}
if (!Disabled)
Dirty();
Invalidate();
}
public void HideActor(Actor a, int range)
{
if (a.Owner.World.LocalPlayer == null
|| a.Owner.Stances[a.Owner.World.LocalPlayer] == Stance.Ally) return;
if (a.Owner.IsAlliedWith(self.Owner))
return;
var v = new ActorVisibility
{
@@ -123,34 +129,33 @@ namespace OpenRA.Traits
foreach (var q in FindVisibleTiles(a.World, p, range))
foggedCells[q.X, q.Y] = visibleCells[q.X, q.Y] > 0;
if (!Disabled)
Dirty();
Invalidate();
}
public void UnhideActor(Actor a, ActorVisibility v, int range) {
if (a.Owner.World.LocalPlayer == null
|| a.Owner.Stances[a.Owner.World.LocalPlayer] == Stance.Ally) return;
if (v == null)
public void UnhideActor(Actor a, ActorVisibility v, int range)
{
if (a.Owner.IsAlliedWith(self.Owner) || v == null)
return;
foreach (var p in v.vis)
foreach (var q in FindVisibleTiles(a.World, p, range))
foggedCells[q.X, q.Y] = exploredCells[q.X, q.Y];
if (!Disabled)
Dirty();
Invalidate();
}
public void MergeShroud(Shroud s) {
for (int i = map.Bounds.Left; i < map.Bounds.Right; i++) {
for (int j = map.Bounds.Top; j < map.Bounds.Bottom; j++) {
public void MergeShroud(Shroud s)
{
for (int i = map.Bounds.Left; i < map.Bounds.Right; i++)
{
for (int j = map.Bounds.Top; j < map.Bounds.Bottom; j++)
{
if (s.exploredCells[i,j] == true)
exploredCells[i, j] = true;
if (s.foggedCells[i,j] == true)
foggedCells[i, j] = true;
}
exploredBounds = Rectangle.Union(exploredBounds.Value, s.exploredBounds.Value);
ExploredBounds = Rectangle.Union(ExploredBounds, s.ExploredBounds);
}
}
@@ -166,6 +171,7 @@ namespace OpenRA.Traits
foreach (var a in toRemove)
RemoveActor(a);
}
// Is now our ally; add unit vis
if (newStance == Stance.Ally)
foreach (var a in w.Actors.Where( a => a.Owner == player ))
@@ -177,7 +183,8 @@ namespace OpenRA.Traits
int seen = 0;
for (int i = map.Bounds.Left; i < map.Bounds.Right; i++)
for (int j = map.Bounds.Top; j < map.Bounds.Bottom; j++)
if(foggedCells[i, j]) seen++;
if (foggedCells[i, j])
seen++;
return seen;
}
@@ -196,34 +203,33 @@ namespace OpenRA.Traits
public void RemoveActor(Actor a)
{
if (!a.HasTrait<RevealsShroud>())return;
if (a.Owner == null || Owner == null) return;
ActorVisibility v = a.Sight;
if(a.Owner.Stances[Owner] != Stance.Ally) {
if (a.HasTrait<CreatesShroud>()) {
if (!a.Owner.IsAlliedWith(self.Owner))
{
if (a.HasTrait<CreatesShroud>())
foreach (var p in v.vis)
foreach (var q in FindVisibleTiles(a.World, p, v.range))
foggedCells[q.X, q.Y] = exploredCells[q.X, q.Y];
}
return;
}
if (!a.HasTrait<RevealsShroud>())
return;
foreach (var p in v.vis)
foreach (var q in FindVisibleTiles(a.World, p, v.range))
--visibleCells[q.X, q.Y];
if (!Disabled)
Dirty();
Invalidate();
}
public void UpdateActor(Actor a)
{
if (a.Owner.World.LocalPlayer == null
|| a.Owner.Stances[a.Owner.World.LocalPlayer] != Stance.Ally) return;
if (!a.Owner.IsAlliedWith(self.Owner))
return;
RemoveActor(a); AddActor(a);
RemoveActor(a);
AddActor(a);
}
public void Explore(World world, CPos center, int range)
@@ -234,10 +240,9 @@ namespace OpenRA.Traits
}
var box = new Rectangle(center.X - range, center.Y - range, 2 * range + 1, 2 * range + 1);
exploredBounds = (exploredBounds.HasValue) ? Rectangle.Union(exploredBounds.Value, box) : box;
ExploredBounds = Rectangle.Union(ExploredBounds, box);
if (!Disabled)
Dirty();
Invalidate();
}
public void ExploreAll(World world)
@@ -248,10 +253,9 @@ namespace OpenRA.Traits
foggedCells[i, j] = true;
}
}
exploredBounds = world.Map.Bounds;
ExploredBounds = world.Map.Bounds;
if (!Disabled)
Dirty();
Invalidate();
}
public void ResetExploration()
@@ -264,8 +268,7 @@ namespace OpenRA.Traits
for (var i = 0; i <= foggedCells.GetUpperBound(0); i++)
foggedCells[i, j] = visibleCells[i, j] > 0;
if (!Disabled)
Dirty();
Invalidate();
}
public bool IsExplored(CPos xy) { return IsExplored(xy.X, xy.Y); }
@@ -274,40 +277,42 @@ namespace OpenRA.Traits
if (!map.IsInMap(x, y))
return false;
if (Disabled || Observing)
if (!Info.Shroud)
return true;
return foggedCells[x,y];
}
public bool IsExplored(Actor a)
{
return GetVisOrigins(a).Any(o => IsExplored(o));
}
public bool IsVisible(CPos xy) { return IsVisible(xy.X, xy.Y); }
public bool IsVisible(int x, int y)
{
if (Disabled || Observing)
return true;
// Visibility is allowed to extend beyond the map cordon so that
// the fog tiles are not visible at the edge of the world
if (x < 0 || x >= map.MapSize.X || y < 0 || y >= map.MapSize.Y)
return false;
if (!Info.Fog)
return true;
return visibleCells[x,y] != 0;
}
// Actors are hidden under shroud, but not under fog by default
public bool IsVisible(Actor a)
{
// I need to pass in the current shroud, otherwise we're just checking that true==true
if (a.TraitsImplementing<IVisibilityModifier>().Any(t => !t.IsVisible(this, a)))
if (a.TraitsImplementing<IVisibilityModifier>().Any(t => !t.IsVisible(a, self.Owner)))
return false;
if(Owner == null) return true;
return Disabled || Observing || a.Owner.Stances[Owner] == Stance.Ally || GetVisOrigins(a).Any(o => IsExplored(o));
return a.Owner.IsAlliedWith(self.Owner) || IsExplored(a);
}
public bool IsTargetable(Actor a) {
if (a.TraitsImplementing<IVisibilityModifier>().Any(t => !t.IsVisible(this, a)))
if (a.TraitsImplementing<IVisibilityModifier>().Any(t => !t.IsVisible(a, self.Owner)))
return false;
return GetVisOrigins(a).Any(o => IsVisible(o));

View File

@@ -184,7 +184,7 @@ namespace OpenRA.Widgets
IEnumerable<Actor> SelectActorsInBox(World world, PPos a, PPos b, Func<Actor, bool> cond)
{
return world.FindUnits(a, b)
.Where(x => x.HasTrait<Selectable>() && world.RenderedShroud.IsVisible(x) && cond(x))
.Where(x => x.HasTrait<Selectable>() && !world.FogObscures(x) && cond(x))
.GroupBy(x => x.GetSelectionPriority())
.OrderByDescending(g => g.Key)
.Select(g => g.AsEnumerable())

View File

@@ -10,6 +10,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Effects;
using OpenRA.FileFormats;
@@ -40,11 +41,30 @@ namespace OpenRA
public void AddPlayer(Player p) { Players.Add(p); }
public Player LocalPlayer { get; private set; }
public readonly Shroud LocalShroud;
public bool Observer { get { return LocalPlayer == null; } }
public Player RenderedPlayer;
public Shroud RenderedShroud { get { return RenderedPlayer != null ? RenderedPlayer.Shroud : LocalShroud; } }
Player renderPlayer;
public bool ObserveAfterWinOrLose;
public Player RenderPlayer
{
get { return renderPlayer == null || (ObserveAfterWinOrLose && renderPlayer.WinState != WinState.Undefined)? null : renderPlayer; }
set { renderPlayer = value; }
}
public bool FogObscures(Actor a) { return RenderPlayer != null && !RenderPlayer.Shroud.IsVisible(a); }
public bool FogObscures(CPos p) { return RenderPlayer != null && !RenderPlayer.Shroud.IsVisible(p); }
public bool ShroudObscures(Actor a) { return RenderPlayer != null && !RenderPlayer.Shroud.IsExplored(a); }
public bool ShroudObscures(CPos p) { return RenderPlayer != null && !RenderPlayer.Shroud.IsExplored(p); }
public Rectangle? VisibleBounds
{
get
{
if (RenderPlayer == null)
return null;
return RenderPlayer.Shroud.ExploredBounds;
}
}
public void SetLocalPlayer(string pr)
{
@@ -52,7 +72,7 @@ namespace OpenRA
return;
LocalPlayer = Players.FirstOrDefault(p => p.InternalName == pr);
RenderedPlayer = LocalPlayer;
RenderPlayer = LocalPlayer;
}
public readonly Actor WorldActor;
@@ -104,7 +124,6 @@ namespace OpenRA
SharedRandom = new XRandom(orderManager.LobbyInfo.GlobalSettings.RandomSeed);
WorldActor = CreateActor( "World", new TypeDictionary() );
LocalShroud = WorldActor.Trait<Shroud>();
ActorMap = new ActorMap(this);
// Add players

View File

@@ -24,7 +24,7 @@ namespace OpenRA
public static IEnumerable<Actor> FindUnitsAtMouse(this World world, int2 mouseLocation)
{
var loc = Game.viewport.ViewToWorldPx(mouseLocation);
return FindUnits(world, loc, loc).Where(a => world.RenderedShroud.IsVisible(a));
return FindUnits(world, loc, loc).Where(a => !world.FogObscures(a));
}
public static IEnumerable<Actor> FindUnits(this World world, PPos a, PPos b)