public void Resize(int width, int height) // editor magic. { var oldMapTiles = MapTiles.Value; var oldMapResources = MapResources.Value; var oldMapHeight = MapHeight.Value; var newSize = new Size(width, height); MapTiles = Exts.Lazy(() => CellLayer.Resize(oldMapTiles, newSize, oldMapTiles[MPos.Zero])); MapResources = Exts.Lazy(() => CellLayer.Resize(oldMapResources, newSize, oldMapResources[MPos.Zero])); MapHeight = Exts.Lazy(() => CellLayer.Resize(oldMapHeight, newSize, oldMapHeight[MPos.Zero])); MapSize = new int2(newSize); }
public void CopyValuesFrom(CellLayer <T> anotherLayer) { if (Size != anotherLayer.Size || GridType != anotherLayer.GridType) { throw new ArgumentException( "layers must have a matching size and shape (grid type).", "anotherLayer"); } if (CellEntryChanged != null) { throw new InvalidOperationException( "Cannot copy values when there are listeners attached to the CellEntryChanged event."); } Array.Copy(anotherLayer.entries, entries, entries.Length); }
public static CellLayer <T> CreateInstance(Func <MPos, T> initialCellValueFactory, Size size, MapGridType mapGridType) { var cellLayer = new CellLayer <T>(mapGridType, size); for (var v = 0; v < size.Height; v++) { for (var u = 0; u < size.Width; u++) { var mpos = new MPos(u, v); cellLayer[mpos] = initialCellValueFactory(mpos); } } return(cellLayer); }
void PostInit() { rules = Exts.Lazy(() => { try { return(Game.ModData.RulesetCache.Load(this)); } catch (Exception e) { InvalidCustomRules = true; Log.Write("debug", "Failed to load rules for {0} with error {1}", Title, e.Message); } return(Game.ModData.DefaultRules); }); cachedTileSet = Exts.Lazy(() => Rules.TileSets[Tileset]); var tl = new MPos(0, 0).ToCPos(this); var br = new MPos(MapSize.X - 1, MapSize.Y - 1).ToCPos(this); AllCells = new CellRegion(Grid.Type, tl, br); var btl = new PPos(Bounds.Left, Bounds.Top); var bbr = new PPos(Bounds.Right - 1, Bounds.Bottom - 1); SetBounds(btl, bbr); CustomTerrain = new CellLayer <byte>(this); foreach (var uv in AllCells.MapCoords) { CustomTerrain[uv] = byte.MaxValue; } var leftDelta = Grid.Type == MapGridType.RectangularIsometric ? new WVec(-512, 0, 0) : new WVec(-512, -512, 0); var topDelta = Grid.Type == MapGridType.RectangularIsometric ? new WVec(0, -512, 0) : new WVec(512, -512, 0); var rightDelta = Grid.Type == MapGridType.RectangularIsometric ? new WVec(512, 0, 0) : new WVec(512, 512, 0); var bottomDelta = Grid.Type == MapGridType.RectangularIsometric ? new WVec(0, 512, 0) : new WVec(-512, 512, 0); CellCorners = CellCornerHalfHeights.Select(ramp => new WVec[] { leftDelta + new WVec(0, 0, 512 * ramp[0]), topDelta + new WVec(0, 0, 512 * ramp[1]), rightDelta + new WVec(0, 0, 512 * ramp[2]), bottomDelta + new WVec(0, 0, 512 * ramp[3]) }).ToArray(); }
void PostInit() { rules = Exts.Lazy(() => Game.modData.RulesetCache.LoadMapRules(this)); cachedTileSet = Exts.Lazy(() => Rules.TileSets[Tileset]); var tl = Map.MapToCell(TileShape, new CPos(Bounds.Left, Bounds.Top)); var br = Map.MapToCell(TileShape, new CPos(Bounds.Right - 1, Bounds.Bottom - 1)); Cells = new CellRegion(TileShape, tl, br); CustomTerrain = new CellLayer <int>(this); foreach (var cell in Cells) { CustomTerrain[cell] = -1; } }
public void Resize(int width, int height) // editor magic. { var oldMapTiles = MapTiles.Value; var oldMapResources = MapResources.Value; var oldMapHeight = MapHeight.Value; var newSize = new Size(width, height); MapTiles = Exts.Lazy(() => CellLayer.Resize(oldMapTiles, newSize, oldMapTiles[MPos.Zero])); MapResources = Exts.Lazy(() => CellLayer.Resize(oldMapResources, newSize, oldMapResources[MPos.Zero])); MapHeight = Exts.Lazy(() => CellLayer.Resize(oldMapHeight, newSize, oldMapHeight[MPos.Zero])); MapSize = new int2(newSize); var tl = new MPos(0, 0).ToCPos(this); var br = new MPos(MapSize.X - 1, MapSize.Y - 1).ToCPos(this); AllCells = new CellRegion(Grid.Type, tl, br); }
public void Resize(int width, int height) { var oldMapTiles = Tiles; var oldMapResources = Resources; var oldMapHeight = Height; var newSize = new Size(width, height); Tiles = CellLayer.Resize(oldMapTiles, newSize, oldMapTiles[MPos.Zero]); Resources = CellLayer.Resize(oldMapResources, newSize, oldMapResources[MPos.Zero]); Height = CellLayer.Resize(oldMapHeight, newSize, oldMapHeight[MPos.Zero]); MapSize = new int2(newSize); var tl = new MPos(0, 0); var br = new MPos(MapSize.X - 1, MapSize.Y - 1); AllCells = new CellRegion(Grid.Type, tl.ToCPos(this), br.ToCPos(this)); SetBounds(new PPos(tl.U + 1, tl.V + 1), new PPos(br.U - 1, br.V - 1)); }
/// <summary> /// Initializes a new map created by the editor or importer. /// The map will not recieve a valid UID until after it has been saved and reloaded. /// </summary> public Map(TileSet tileset, int width, int height) { var size = new Size(width, height); Grid = Game.ModData.Manifest.Get <MapGrid>(); var tileRef = new TerrainTile(tileset.Templates.First().Key, (byte)0); Title = "Name your map here"; Description = "Describe your map here"; Author = "Your name here"; MapSize = new int2(size); Tileset = tileset.Id; Videos = new MapVideos(); Options = new MapOptions(); MapResources = Exts.Lazy(() => new CellLayer <ResourceTile>(Grid.Type, size)); MapTiles = Exts.Lazy(() => { var ret = new CellLayer <TerrainTile>(Grid.Type, size); ret.Clear(tileRef); if (Grid.MaximumTerrainHeight > 0) { ret.CellEntryChanged += UpdateProjection; } return(ret); }); MapHeight = Exts.Lazy(() => { var ret = new CellLayer <byte>(Grid.Type, size); ret.Clear(0); if (Grid.MaximumTerrainHeight > 0) { ret.CellEntryChanged += UpdateProjection; } return(ret); }); SpawnPoints = Exts.Lazy(() => new CPos[0]); PostInit(); }
public CellLayer <TerrainTile> LoadMapTiles() { var tiles = new CellLayer <TerrainTile>(this); 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 var data = dataStream.ReadBytes(MapSize.X * MapSize.Y * 3); var d = 0; for (var i = 0; i < MapSize.X; i++) { for (var j = 0; j < MapSize.Y; j++) { var tile = BitConverter.ToUInt16(data, d); d += 2; var index = data[d++]; if (index == byte.MaxValue) { index = (byte)(i % 4 + (j % 4) * 4); } tiles[i, j] = new TerrainTile(tile, index); } } } return(tiles); }
public static Map FromTileset(TileSet tileset) { var size = new Size(1, 1); var tileShape = Game.ModData.Manifest.TileShape; var tileRef = new TerrainTile(tileset.Templates.First().Key, (byte)0); var makeMapTiles = Exts.Lazy(() => { var ret = new CellLayer <TerrainTile>(tileShape, size); ret.Clear(tileRef); return(ret); }); var makeMapHeight = Exts.Lazy(() => { var ret = new CellLayer <byte>(tileShape, size); ret.Clear(0); return(ret); }); var map = new Map() { Title = "Name your map here", Description = "Describe your map here", Author = "Your name here", MapSize = new int2(size), Tileset = tileset.Id, Videos = new MapVideos(), Options = new MapOptions(), MapResources = Exts.Lazy(() => new CellLayer <ResourceTile>(tileShape, size)), MapTiles = makeMapTiles, MapHeight = makeMapHeight, SpawnPoints = Exts.Lazy(() => new CPos[0]) }; map.PostInit(); return(map); }
public CellLayer <byte> LoadMapHeight() { var tiles = new CellLayer <byte>(this); using (var s = Container.GetContent("map.bin")) { var header = new BinaryDataHeader(s, MapSize); if (header.HeightsOffset > 0) { s.Position = header.HeightsOffset; for (var i = 0; i < MapSize.X; i++) { for (var j = 0; j < MapSize.Y; j++) { tiles[new MPos(i, j)] = s.ReadUInt8().Clamp((byte)0, MaximumTerrainHeight); } } } } return(tiles); }
/// <summary> /// Initializes a new map created by the editor or importer. /// The map will not recieve a valid UID until after it has been saved and reloaded. /// </summary> public Map(TileSet tileset, int width, int height) { containsTest = Contains; var size = new Size(width, height); var tileShape = Game.ModData.Manifest.TileShape; var tileRef = new TerrainTile(tileset.Templates.First().Key, (byte)0); Title = "Name your map here"; Description = "Describe your map here"; Author = "Your name here"; MapSize = new int2(size); Tileset = tileset.Id; Videos = new MapVideos(); Options = new MapOptions(); MapResources = Exts.Lazy(() => new CellLayer <ResourceTile>(tileShape, size)); MapTiles = Exts.Lazy(() => { var ret = new CellLayer <TerrainTile>(tileShape, size); ret.Clear(tileRef); return(ret); }); MapHeight = Exts.Lazy(() => { var ret = new CellLayer <byte>(tileShape, size); ret.Clear(0); return(ret); }); SpawnPoints = Exts.Lazy(() => new CPos[0]); TileShape = tileShape; MaximumTerrainHeight = Game.ModData.Manifest.MaximumTerrainHeight; PostInit(); }
public CellLayer <ResourceTile> LoadResourceTiles() { var resources = new CellLayer <ResourceTile>(this); 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); var data = dataStream.ReadBytes(MapSize.X * MapSize.Y * 2); var d = 0; // Load resource data for (var i = 0; i < MapSize.X; i++) { for (var j = 0; j < MapSize.Y; j++) { resources[i, j] = new ResourceTile(data[d++], data[d++]); } } } return(resources); }
public CellLayer <ResourceTile> LoadResourceTiles() { var resources = new CellLayer <ResourceTile>(this); using (var s = Container.GetContent("map.bin")) { var header = new BinaryDataHeader(s, MapSize); if (header.ResourcesOffset > 0) { s.Position = header.ResourcesOffset; for (var i = 0; i < MapSize.X; i++) { for (var j = 0; j < MapSize.Y; j++) { var type = s.ReadUInt8(); var density = s.ReadUInt8(); resources[new MPos(i, j)] = new ResourceTile(type, density); } } } } return(resources); }
public CellLayer <TerrainTile> LoadMapTiles() { var tiles = new CellLayer <TerrainTile>(this); using (var s = Container.GetContent("map.bin")) { var header = new BinaryDataHeader(s, MapSize); if (header.TilesOffset > 0) { s.Position = header.TilesOffset; for (var i = 0; i < MapSize.X; i++) { for (var j = 0; j < MapSize.Y; j++) { var tile = s.ReadUInt16(); var index = s.ReadUInt8(); // TODO: Remember to remove this when rewriting tile variants / PickAny if (index == byte.MaxValue) { index = (byte)(i % 4 + (j % 4) * 4); } tiles[new MPos(i, j)] = new TerrainTile(tile, index); } } } } if (Grid.MaximumTerrainHeight > 0) { tiles.CellEntryChanged += UpdateProjection; } return(tiles); }
public Map(ModData modData, IReadOnlyPackage package) { this.modData = modData; Package = package; if (!Package.Contains("map.yaml") || !Package.Contains("map.bin")) { throw new InvalidDataException("Not a valid map\n File: {0}".F(package.Name)); } var yaml = new MiniYaml(null, MiniYaml.FromStream(Package.GetStream("map.yaml"), package.Name)); foreach (var field in YamlFields) { field.Deserialize(this, yaml.Nodes); } if (MapFormat != SupportedMapFormat) { throw new InvalidDataException("Map format {0} is not supported.\n File: {1}".F(MapFormat, package.Name)); } PlayerDefinitions = MiniYaml.NodesOrEmpty(yaml, "Players"); ActorDefinitions = MiniYaml.NodesOrEmpty(yaml, "Actors"); Grid = modData.Manifest.Get <MapGrid>(); var size = new Size(MapSize.X, MapSize.Y); Tiles = new CellLayer <TerrainTile>(Grid.Type, size); Resources = new CellLayer <ResourceTile>(Grid.Type, size); Height = new CellLayer <byte>(Grid.Type, size); using (var s = Package.GetStream("map.bin")) { var header = new BinaryDataHeader(s, MapSize); if (header.TilesOffset > 0) { s.Position = header.TilesOffset; for (var i = 0; i < MapSize.X; i++) { for (var j = 0; j < MapSize.Y; j++) { var tile = s.ReadUInt16(); var index = s.ReadUInt8(); // TODO: Remember to remove this when rewriting tile variants / PickAny if (index == byte.MaxValue) { index = (byte)(i % 4 + (j % 4) * 4); } Tiles[new MPos(i, j)] = new TerrainTile(tile, index); } } } if (header.ResourcesOffset > 0) { s.Position = header.ResourcesOffset; for (var i = 0; i < MapSize.X; i++) { for (var j = 0; j < MapSize.Y; j++) { var type = s.ReadUInt8(); var density = s.ReadUInt8(); Resources[new MPos(i, j)] = new ResourceTile(type, density); } } } if (header.HeightsOffset > 0) { s.Position = header.HeightsOffset; for (var i = 0; i < MapSize.X; i++) { for (var j = 0; j < MapSize.Y; j++) { Height[new MPos(i, j)] = s.ReadUInt8().Clamp((byte)0, Grid.MaximumTerrainHeight); } } } } if (Grid.MaximumTerrainHeight > 0) { Tiles.CellEntryChanged += UpdateProjection; Height.CellEntryChanged += UpdateProjection; } PostInit(); Uid = ComputeUID(Package); }