/// <summary>Initializes a map loaded from disk.</summary> public Map(string path) { Path = path; Container = GlobalFileSystem.OpenPackage(path, null, int.MaxValue); AssertExists("map.yaml"); AssertExists("map.bin"); var yaml = new MiniYaml(null, MiniYaml.FromStream(Container.GetContent("map.yaml"), path)); FieldLoader.Load(this, yaml); // Support for formats 1-3 dropped 2011-02-11. // Use release-20110207 to convert older maps to format 4 // Use release-20110511 to convert older maps to format 5 // Use release-20141029 to convert older maps to format 6 if (MapFormat < MinimumSupportedMapFormat) { throw new InvalidDataException("Map format {0} is not supported.\n File: {1}".F(MapFormat, path)); } var nd = yaml.ToDictionary(); // Format 6 -> 7 combined the Selectable and UseAsShellmap flags into the Class enum if (MapFormat < 7) { MiniYaml useAsShellmap; if (nd.TryGetValue("UseAsShellmap", out useAsShellmap) && bool.Parse(useAsShellmap.Value)) { Visibility = MapVisibility.Shellmap; } else if (Type == "Mission" || Type == "Campaign") { Visibility = MapVisibility.MissionSelector; } } SpawnPoints = Exts.Lazy(() => { var spawns = new List <CPos>(); foreach (var kv in ActorDefinitions.Where(d => d.Value.Value == "mpspawn")) { var s = new ActorReference(kv.Value.Value, kv.Value.ToDictionary()); spawns.Add(s.InitDict.Get <LocationInit>().Value(null)); } return(spawns.ToArray()); }); RuleDefinitions = MiniYaml.NodesOrEmpty(yaml, "Rules"); SequenceDefinitions = MiniYaml.NodesOrEmpty(yaml, "Sequences"); VoxelSequenceDefinitions = MiniYaml.NodesOrEmpty(yaml, "VoxelSequences"); WeaponDefinitions = MiniYaml.NodesOrEmpty(yaml, "Weapons"); VoiceDefinitions = MiniYaml.NodesOrEmpty(yaml, "Voices"); MusicDefinitions = MiniYaml.NodesOrEmpty(yaml, "Music"); NotificationDefinitions = MiniYaml.NodesOrEmpty(yaml, "Notifications"); TranslationDefinitions = MiniYaml.NodesOrEmpty(yaml, "Translations"); PlayerDefinitions = MiniYaml.NodesOrEmpty(yaml, "Players"); ActorDefinitions = MiniYaml.NodesOrEmpty(yaml, "Actors"); SmudgeDefinitions = MiniYaml.NodesOrEmpty(yaml, "Smudges"); MapTiles = Exts.Lazy(LoadMapTiles); MapResources = Exts.Lazy(LoadResourceTiles); MapHeight = Exts.Lazy(LoadMapHeight); Grid = Game.ModData.Manifest.Get <MapGrid>(); SubCellOffsets = Grid.SubCellOffsets; LastSubCell = (SubCell)(SubCellOffsets.Length - 1); DefaultSubCell = (SubCell)Grid.SubCellDefaultIndex; if (Container.Exists("map.png")) { using (var dataStream = Container.GetContent("map.png")) CustomPreview = new Bitmap(dataStream); } PostInit(); // The Uid is calculated from the data on-disk, so // format changes must be flushed to disk. // TODO: this isn't very nice if (MapFormat < 7) { Save(path); } Uid = ComputeHash(); }
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); }
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); }
/// <summary> /// Initializes a new map created by the editor or importer. /// The map will not receive a valid UID until after it has been saved and reloaded. /// </summary> public Map(ModData modData, TileSet tileset, int width, int height) { this.modData = modData; var size = new Size(width, height); Grid = modData.Manifest.Get<MapGrid>(); var tileRef = new TerrainTile(tileset.Templates.First().Key, 0); Title = "Name your map here"; Author = "Your name here"; MapSize = new int2(size); Tileset = tileset.Id; // Empty rules that can be added to by the importers. // Will be dropped on save if nothing is added to it RuleDefinitions = new MiniYaml(""); Tiles = new CellLayer<TerrainTile>(Grid.Type, size); Resources = new CellLayer<ResourceTile>(Grid.Type, size); Height = new CellLayer<byte>(Grid.Type, size); if (Grid.MaximumTerrainHeight > 0) { Height.CellEntryChanged += UpdateProjection; Tiles.CellEntryChanged += UpdateProjection; } Tiles.Clear(tileRef); PostInit(); }
/// <summary>Initializes a map loaded from disk.</summary> public Map(string path) { Path = path; Container = GlobalFileSystem.OpenPackage(path, null, int.MaxValue); AssertExists("map.yaml"); AssertExists("map.bin"); var yaml = new MiniYaml(null, MiniYaml.FromStream(Container.GetContent("map.yaml"), path)); FieldLoader.Load(this, yaml); // Support for formats 1-3 dropped 2011-02-11. // Use release-20110207 to convert older maps to format 4 // Use release-20110511 to convert older maps to format 5 // Use release-20141029 to convert older maps to format 6 if (MapFormat < MinimumSupportedMapFormat) throw new InvalidDataException("Map format {0} is not supported.\n File: {1}".F(MapFormat, path)); var nd = yaml.ToDictionary(); // Format 6 -> 7 combined the Selectable and UseAsShellmap flags into the Class enum if (MapFormat < 7) { MiniYaml useAsShellmap; if (nd.TryGetValue("UseAsShellmap", out useAsShellmap) && bool.Parse(useAsShellmap.Value)) Visibility = MapVisibility.Shellmap; else if (Type == "Mission" || Type == "Campaign") Visibility = MapVisibility.MissionSelector; } SpawnPoints = Exts.Lazy(() => { var spawns = new List<CPos>(); foreach (var kv in ActorDefinitions.Where(d => d.Value.Value == "mpspawn")) { var s = new ActorReference(kv.Value.Value, kv.Value.ToDictionary()); spawns.Add(s.InitDict.Get<LocationInit>().Value(null)); } return spawns.ToArray(); }); RuleDefinitions = MiniYaml.NodesOrEmpty(yaml, "Rules"); SequenceDefinitions = MiniYaml.NodesOrEmpty(yaml, "Sequences"); VoxelSequenceDefinitions = MiniYaml.NodesOrEmpty(yaml, "VoxelSequences"); WeaponDefinitions = MiniYaml.NodesOrEmpty(yaml, "Weapons"); VoiceDefinitions = MiniYaml.NodesOrEmpty(yaml, "Voices"); MusicDefinitions = MiniYaml.NodesOrEmpty(yaml, "Music"); NotificationDefinitions = MiniYaml.NodesOrEmpty(yaml, "Notifications"); TranslationDefinitions = MiniYaml.NodesOrEmpty(yaml, "Translations"); PlayerDefinitions = MiniYaml.NodesOrEmpty(yaml, "Players"); ActorDefinitions = MiniYaml.NodesOrEmpty(yaml, "Actors"); SmudgeDefinitions = MiniYaml.NodesOrEmpty(yaml, "Smudges"); MapTiles = Exts.Lazy(LoadMapTiles); MapResources = Exts.Lazy(LoadResourceTiles); MapHeight = Exts.Lazy(LoadMapHeight); Grid = Game.ModData.Manifest.Get<MapGrid>(); SubCellOffsets = Grid.SubCellOffsets; LastSubCell = (SubCell)(SubCellOffsets.Length - 1); DefaultSubCell = (SubCell)Grid.SubCellDefaultIndex; if (Container.Exists("map.png")) using (var dataStream = Container.GetContent("map.png")) CustomPreview = new Bitmap(dataStream); PostInit(); // The Uid is calculated from the data on-disk, so // format changes must be flushed to disk. // TODO: this isn't very nice if (MapFormat < 7) Save(path); Uid = ComputeHash(); }
/// <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 void LoadMap(string map, IReadOnlyPackage package, MapClassification classification, MapGrid mapGrid, string oldMap) { IReadOnlyPackage mapPackage = null; try { using (new Support.PerfTimer(map)) { mapPackage = package.OpenPackage(map, modData.ModFiles); if (mapPackage != null) { var uid = Map.ComputeUID(mapPackage); previews[uid].UpdateFromMap(mapPackage, package, classification, modData.Manifest.MapCompatibility, mapGrid.Type); if (oldMap != uid) { MapUpdated(oldMap, uid); } } } } catch (Exception e) { mapPackage?.Dispose(); Console.WriteLine("Failed to load map: {0}", map); Console.WriteLine("Details: {0}", e); Log.Write("debug", "Failed to load map: {0}", map); Log.Write("debug", "Details: {0}", e); } }