public Sprite TileSprite(TileReference<ushort, byte> r) { Sprite[] template; if (templates.TryGetValue(r.Type, out template)) if (template.Length > r.Index && template[r.Index] != null) return template[r.Index]; return missingTile; }
public Sprite TileSprite(TileReference<ushort, byte> r) { Sprite[] template; if (!templates.TryGetValue(r.Type, out template)) return missingTile; if (r.Index >= template.Length) return missingTile; return template[r.Index]; }
public byte[] GetBytes(TileReference r) { Terrain tile; if( tiles.TryGetValue( r.tile, out tile ) ) return tile.TileBitmapBytes[ r.image ]; byte[] missingTile = new byte[ 24 * 24 ]; for( int i = 0 ; i < missingTile.Length ; i++ ) missingTile[ i ] = 0x36; return missingTile; }
public Map(string filename) { IniFile file = new IniFile(FileSystem.Open(filename)); IniSection basic = file.GetSection("Basic"); Title = basic.GetValue("Name", "(null)"); INIFormat = int.Parse(basic.GetValue("NewINIFormat", "0")); IniSection map = file.GetSection("Map"); Theater = Truncate(map.GetValue("Theater", "TEMPERAT"), 8); XOffset = int.Parse(map.GetValue("X", "0")); YOffset = int.Parse(map.GetValue("Y", "0")); Width = int.Parse(map.GetValue("Width", "0")); Height = int.Parse(map.GetValue("Height", "0")); MapSize = (INIFormat == 3) ? 128 : 64; MapTiles = new TileReference[ MapSize, MapSize ]; for (int j = 0; j < MapSize; j++) for (int i = 0; i < MapSize; i++) MapTiles[i, j] = new TileReference(); if (INIFormat == 3) // RA map { UnpackRATileData(ReadPackedSection(file.GetSection("MapPack"))); UnpackRAOverlayData(ReadPackedSection(file.GetSection("OverlayPack"))); ReadRATrees(file); } else // CNC { UnpackCncTileData(FileSystem.Open(filename.Substring(0,filename.Length-4)+".bin")); ReadCncOverlay(file); ReadCncTrees(file); } LoadActors(file, "STRUCTURES"); LoadActors(file, "UNITS"); LoadActors(file, "INFANTRY"); SpawnPoints = file.GetSection("Waypoints") .Where(kv => int.Parse(kv.Value) > 0) .Select(kv => Pair.New(int.Parse(kv.Key), new int2(int.Parse(kv.Value) % MapSize, int.Parse(kv.Value) / MapSize))) .Where(a => a.First < 8) .Select(a => a.Second) .ToArray(); }
public void AddSmudge(CPos loc) { if (Game.CosmeticRandom.Next(0, 100) <= Info.SmokePercentage) world.AddFrameEndTask(w => w.Add(new Smoke(w, loc.CenterPosition, Info.SmokeType))); if (!dirty.ContainsKey(loc) && !tiles.ContainsKey(loc)) { // No smudge; create a new one var st = (byte)(1 + world.SharedRandom.Next(Info.Types.Length - 1)); dirty[loc] = new TileReference<byte, byte>(st, (byte)0); } else { // Existing smudge; make it deeper var tile = dirty.ContainsKey(loc) ? dirty[loc] : tiles[loc]; var depth = Info.Depths[tile.Type - 1]; if (tile.Index < depth - 1) tile.Index++; dirty[loc] = tile; } }
public string GetTerrainType(TileReference<ushort, byte> r) { var tt = Templates[r.Type].Tiles; string ret; if (!tt.TryGetValue(r.Index, out ret)) return "Clear"; // Default walkable return ret; }
public byte[] GetBytes(TileReference<ushort,byte> r) { TileTemplate tile; if( Templates.TryGetValue( r.type, out tile ) ) return tile.Data.TileBitmapBytes[ r.index ]; byte[] missingTile = new byte[ TileSize * TileSize ]; for( int i = 0 ; i < missingTile.Length ; i++ ) missingTile[ i ] = 0x36; return missingTile; }
public TileReference<byte, byte>[,] LoadResourceTiles() { var resources = new TileReference<byte, byte>[MapSize.X, MapSize.Y]; using (var dataStream = container.GetContent("map.bin")) { if (dataStream.ReadUInt8() != 1) throw new InvalidDataException("Unknown binary map format"); // Load header info var width = dataStream.ReadUInt16(); var height = dataStream.ReadUInt16(); if (width != MapSize.X || height != MapSize.Y) throw new InvalidDataException("Invalid tile data"); // Skip past tile data dataStream.Seek(3 * MapSize.X * MapSize.Y, SeekOrigin.Current); // Load resource data for (var i = 0; i < MapSize.X; i++) for (var j = 0; j < MapSize.Y; j++) { var type = dataStream.ReadUInt8(); var index = dataStream.ReadUInt8(); resources[i, j] = new TileReference<byte, byte>(type, index); } } return resources; }
public TileReference<ushort, byte>[,] LoadMapTiles() { var tiles = new TileReference<ushort, byte>[MapSize.X, MapSize.Y]; using (var dataStream = container.GetContent("map.bin")) { if (dataStream.ReadUInt8() != 1) throw new InvalidDataException("Unknown binary map format"); // Load header info var width = dataStream.ReadUInt16(); var height = dataStream.ReadUInt16(); if (width != MapSize.X || height != MapSize.Y) throw new InvalidDataException("Invalid tile data"); // Load tile data for (int i = 0; i < MapSize.X; i++) for (int j = 0; j < MapSize.Y; j++) { var tile = dataStream.ReadUInt16(); var index = dataStream.ReadUInt8(); if (index == byte.MaxValue) index = (byte)(i % 4 + (j % 4) * 4); tiles[i, j] = new TileReference<ushort, byte>(tile, index); } } return tiles; }
public static Map FromTileset(string tileset) { var tile = OpenRA.Rules.TileSets[tileset].Templates.First(); var tileRef = new TileReference<ushort, byte> { Type = tile.Key, Index = (byte)0 }; Map map = new Map() { Title = "Name your map here", Description = "Describe your map here", Author = "Your name here", MapSize = new int2(1, 1), Tileset = tileset, MapResources = Lazy.New(() => new TileReference<byte, byte>[1, 1]), MapTiles = Lazy.New(() => new TileReference<ushort, byte>[1, 1] { { tileRef } }), Actors = Lazy.New(() => new Dictionary<string, ActorReference>()), Smudges = Lazy.New(() => new List<SmudgeReference>()) }; return map; }
int2 FindEdge(int2 p, int2 d, TileReference<ushort, byte> replace) { for (; ; ) { var q = p+d; if (!Map.IsInMap(q)) return p; if (!Map.MapTiles[q.X, q.Y].Equals(replace)) return p; p = q; } }
static CPos FindEdge(Surface s, CPos p, CVec d, TileReference<ushort, byte> replace) { for (;;) { var q = p + d; if (!s.Map.IsInMap(q)) return p; if (s.Map.MapTiles.Value[q.X, q.Y].Type != replace.Type) return p; p = q; } }
void ClearSelection() { SelectionStart = CPos.Zero; SelectionEnd = CPos.Zero; TileSelection = null; ResourceSelection = null; }
public void CopySelection() { // Grab tiles and resources within selection (doesn't do actors) var start = SelectionStart; var end = SelectionEnd; if (start == end) return; int width = Math.Abs((start - end).X); int height = Math.Abs((start - end).Y); TileSelection = new TileReference<ushort, byte>[width, height]; ResourceSelection = new TileReference<byte, byte>[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { //todo: crash prevention TileSelection[x, y] = Map.MapTiles.Value[start.X + x, start.Y + y]; ResourceSelection[x, y] = Map.MapResources.Value[start.X + x, start.Y + y]; } } }
public void WorldLoaded(World w) { world = w; tiles = new TileReference<byte,byte>[w.Map.MapSize.X,w.Map.MapSize.Y]; // Add map smudges foreach (var s in w.Map.Smudges.Where( s => Info.Types.Contains(s.Type ))) tiles[s.Location.X,s.Location.Y] = new TileReference<byte,byte>((byte)Array.IndexOf(Info.Types,s.Type), (byte)s.Depth); }
public TerrainType GetTerrainType(TileReference r) { if (r.tile == 0xff || r.tile == 0xffff) r.image = 0; try { return walk[r.tile].TerrainType[r.image]; } catch (KeyNotFoundException) { return 0; // Default zero (walkable) } }