using System; using System.Collections.Generic; using System.Linq; using System.Text; using IjwFramework.Types; using OpenRa.Graphics; using OpenRa.Traits; using OpenRa.FileFormats; namespace OpenRa { public class Shroud { bool[,] explored = new bool[128, 128]; Sprite[] shadowBits = SpriteSheetBuilder.LoadAllSprites("shadow"); Sprite[,] sprites = new Sprite[128, 128]; bool dirty = true; bool hasGPS = false; Player owner; Map map; public Shroud(Player owner, Map map) { this.owner = owner; this.map = map; } int gapOpaqueTicks = (int)(Rules.General.GapRegenInterval * 25 * 60); int gapTicks; int[,] gapField = new int[128, 128]; bool[,] gapActive = new bool[128, 128]; public bool HasGPS { get { return hasGPS; } set { hasGPS = value; dirty = true;} } public void Tick( World world ) { if (gapTicks > 0) { --gapTicks; return; } // Clear active flags gapActive = new bool[128, 128]; foreach (var a in world.Queries.WithTrait().Where(a => owner != a.Actor.Owner)) foreach (var t in a.Trait.GetShroudedTiles()) { gapActive[t.X, t.Y] = true; explored[t.X, t.Y] = false; dirty = true; } gapTicks = gapOpaqueTicks; } public bool IsExplored(int2 xy) { return IsExplored(xy.X, xy.Y); } public bool IsExplored(int x, int y) { if (gapField[ x, y ] > 0) return false; if (hasGPS) return true; return explored[ x, y ]; } public bool DisplayOnRadar(int x, int y) { // Active gap is never shown on radar, even if a unit is in range if (gapActive[x , y]) return false; return IsExplored(x,y); } public void Explore(World w, int2 center, int range) { foreach (var t in w.FindTilesInCircle(center, range)) { explored[t.X, t.Y] = true; gapField[t.X, t.Y] = 0; } dirty = true; } public void Explore(Actor a) { Explore(a.World, (1f / Game.CellSize * a.CenterLocation).ToInt2(), a.Info.Traits.Get().Sight); } static readonly byte[][] SpecialShroudTiles = { new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, new byte[] { 32, 32, 25, 25, 19, 19, 20, 20 }, new byte[] { 33, 33, 33, 33, 26, 26, 26, 26, 21, 21, 21, 21, 23, 23, 23, 23 }, new byte[] { 36, 36, 36, 36, 30, 30, 30, 30 }, new byte[] { 34, 16, 34, 16, 34, 16, 34, 16, 27, 22, 27, 22, 27, 22, 27, 22 }, new byte[] { 44 }, new byte[] { 37, 37, 37, 37, 37, 37, 37, 37, 31, 31, 31, 31, 31, 31, 31, 31 }, new byte[] { 40 }, new byte[] { 35, 24, 17, 18 }, new byte[] { 39, 39, 29, 29 }, new byte[] { 45 }, new byte[] { 43 }, new byte[] { 38, 28 }, new byte[] { 42 }, new byte[] { 41 }, new byte[] { 46 }, }; Sprite ChooseShroud(int i, int j) { if( !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( !IsExplored( i, j - 1 ) ) { v |= 1; u |= 3; } if( !IsExplored( i + 1, j ) ) { v |= 2; u |= 6; } if( !IsExplored( i, j + 1 ) ) { v |= 4; u |= 12; } if( !IsExplored( i - 1, j ) ) { v |= 8; u |= 9; } var uSides = u; if( !IsExplored( i - 1, j - 1 ) ) u |= 1; if( !IsExplored( i + 1, j - 1 ) ) u |= 2; if( !IsExplored( i + 1, j + 1 ) ) u |= 4; if( !IsExplored( i - 1, j + 1 ) ) u |= 8; return shadowBits[ SpecialShroudTiles[ u ^ uSides ][ v ] ]; } internal void Draw(SpriteRenderer r) { if (dirty) { dirty = false; for (int j = map.YOffset; j < map.YOffset + map.Height; j++) for (int i = map.XOffset; i < map.XOffset + map.Width; i++) sprites[i, j] = ChooseShroud(i, j); } for (var j = map.YOffset; j < map.YOffset + map.Height; j++) { var starti = map.XOffset; for (var i = map.XOffset; i < map.XOffset + map.Width; i++) { if (sprites[i, j] == shadowBits[0x0f]) continue; if (starti != i) { r.DrawSprite(sprites[starti,j], Game.CellSize * new float2(starti, j), PaletteType.Shroud, new float2(Game.CellSize * (i - starti), Game.CellSize)); starti = i+1; } r.DrawSprite(sprites[i, j], Game.CellSize * new float2(i, j), PaletteType.Shroud); starti = i+1; } if (starti < map.XOffset + map.Width) r.DrawSprite(sprites[starti, j], Game.CellSize * new float2(starti, j), PaletteType.Shroud, new float2(Game.CellSize * (map.XOffset + map.Width - starti), Game.CellSize)); } } } }