public TerrainTemplateInfo(TileSet tileSet, MiniYaml my) { FieldLoader.Load(this, my); var nodes = my.ToDictionary()["Tiles"].Nodes; if (!PickAny) { tileInfo = new TerrainTileInfo[Size.X * Size.Y]; foreach (var node in nodes) { int key; if (!int.TryParse(node.Key, out key) || key < 0 || key >= tileInfo.Length) throw new InvalidDataException("Invalid tile key '{0}' on template '{1}' of tileset '{2}'.".F(node.Key, Id, tileSet.Id)); tileInfo[key] = LoadTileInfo(tileSet, node.Value); } } else { tileInfo = new TerrainTileInfo[nodes.Count]; var i = 0; foreach (var node in nodes) { int key; if (!int.TryParse(node.Key, out key) || key != i++) throw new InvalidDataException("Invalid tile key '{0}' on template '{1}' of tileset '{2}'.".F(node.Key, Id, tileSet.Id)); tileInfo[key] = LoadTileInfo(tileSet, node.Value); } } }
public MiniYaml Save(TileSet tileSet) { var root = new List<MiniYamlNode>(); if (Height != 0) root.Add(FieldSaver.SaveField(this, "Height")); if (RampType != 0) root.Add(FieldSaver.SaveField(this, "RampType")); if (LeftColor != tileSet.TerrainInfo[TerrainType].Color) root.Add(FieldSaver.SaveField(this, "LeftColor")); if (RightColor != tileSet.TerrainInfo[TerrainType].Color) root.Add(FieldSaver.SaveField(this, "RightColor")); return new MiniYaml(tileSet.TerrainInfo[TerrainType].Type, root); }
int[] LoadTiles(TileSet tileSet, MiniYaml y) { var nodes = y.ToDictionary()["Tiles"].Nodes; if (!PickAny) { var tiles = new int[Size.X * Size.Y]; for (var i = 0; i < tiles.Length; i++) tiles[i] = -1; foreach (var node in nodes) { int key; if (!int.TryParse(node.Key, out key) || key < 0 || key >= tiles.Length) throw new InvalidDataException("Invalid tile key '{0}' on template '{1}' of tileset '{2}'.".F(node.Key, Id, tileSet.Id)); tiles[key] = tileSet.GetTerrainIndex(node.Value.Value); } return tiles; } else { var tiles = new int[nodes.Count]; var i = 0; foreach (var node in nodes) { int key; if (!int.TryParse(node.Key, out key) || key != i++) throw new InvalidDataException("Invalid tile key '{0}' on template '{1}' of tileset '{2}'.".F(node.Key, Id, tileSet.Id)); tiles[key] = tileSet.GetTerrainIndex(node.Value.Value); } return tiles; } }
static TerrainTileInfo LoadTileInfo(TileSet tileSet, MiniYaml my) { var tile = new TerrainTileInfo(); FieldLoader.Load(tile, my); // Terrain type must be converted from a string to an index tile.GetType().GetField("TerrainType").SetValue(tile, tileSet.GetTerrainIndex(my.Value)); // Fall back to the terrain-type color if necessary var overrideColor = tileSet.TerrainInfo[tile.TerrainType].Color; if (tile.LeftColor == default(Color)) tile.GetType().GetField("LeftColor").SetValue(tile, overrideColor); if (tile.RightColor == default(Color)) tile.GetType().GetField("RightColor").SetValue(tile, overrideColor); return tile; }
public MiniYaml Save(TileSet tileSet) { var root = FieldSaver.SaveDifferences(this, Default); var tileYaml = tileInfo .Select((ti, i) => Pair.New(i.ToString(), ti)) .Where(t => t.Second != null) .Select(t => new MiniYamlNode(t.First, t.Second.Save(tileSet))) .ToList(); root.Nodes.Add(new MiniYamlNode("Tiles", null, tileYaml)); return root; }
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 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 map = new Map() { Title = "Name your map here", Description = "Describe your map here", Author = "Your name here", MapSize = new int2(size), Tileset = tileset.Id, Options = new MapOptions(), MapResources = Exts.Lazy(() => new CellLayer<ResourceTile>(tileShape, size)), MapTiles = makeMapTiles, Actors = Exts.Lazy(() => new Dictionary<string, ActorReference>()), Smudges = Exts.Lazy(() => new List<SmudgeReference>()) }; map.PostInit(); return map; }
public TileTemplate(TileSet tileSet, MiniYaml my) { FieldLoader.Load(this, my); tiles = LoadTiles(tileSet, my); }
public MiniYaml Save(TileSet tileSet) { var root = new List<MiniYamlNode>(); foreach (var field in Fields) { var f = this.GetType().GetField(field); if (f.GetValue(this) == null) continue; root.Add(new MiniYamlNode(field, FieldSaver.FormatValue(this, f))); } root.Add(new MiniYamlNode("Tiles", null, tiles.Select((terrainTypeIndex, templateIndex) => new MiniYamlNode(templateIndex.ToString(), tileSet[terrainTypeIndex].Type)).ToList())); return new MiniYaml(null, root); }
/// <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); 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 Ruleset( IReadOnlyDictionary <string, ActorInfo> actors, IReadOnlyDictionary <string, WeaponInfo> weapons, IReadOnlyDictionary <string, SoundInfo> voices, IReadOnlyDictionary <string, SoundInfo> notifications, IReadOnlyDictionary <string, MusicInfo> music, TileSet tileSet, SequenceProvider sequences, IReadOnlyDictionary <string, MiniYamlNode> modelSequences) { Actors = actors; Weapons = weapons; Voices = voices; Notifications = notifications; Music = music; TileSet = tileSet; Sequences = sequences; ModelSequences = modelSequences; foreach (var a in Actors.Values) { foreach (var t in a.TraitInfos <IRulesetLoaded>()) { try { t.RulesetLoaded(this, a); } catch (YamlException e) { throw new YamlException("Actor type {0}: {1}".F(a.Name, e.Message)); } } } foreach (var weapon in Weapons) { var projectileLoaded = weapon.Value.Projectile as IRulesetLoaded <WeaponInfo>; if (projectileLoaded != null) { try { projectileLoaded.RulesetLoaded(this, weapon.Value); } catch (YamlException e) { throw new YamlException("Projectile type {0}: {1}".F(weapon.Key, e.Message)); } } foreach (var warhead in weapon.Value.Warheads) { var cacher = warhead as IRulesetLoaded <WeaponInfo>; if (cacher != null) { try { cacher.RulesetLoaded(this, weapon.Value); } catch (YamlException e) { throw new YamlException("Weapon type {0}: {1}".F(weapon.Key, e.Message)); } } } } }
public static Map FromTileset(TileSet tileset) { var tile = 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.Id, Options = new MapOptions(), MapResources = Exts.Lazy(() => new TileReference<byte, byte>[1, 1]), MapTiles = Exts.Lazy(() => new TileReference<ushort, byte>[1, 1] { { tileRef } }), Actors = Exts.Lazy(() => new Dictionary<string, ActorReference>()), Smudges = Exts.Lazy(() => new List<SmudgeReference>()) }; map.PostInit(); return map; }
/// <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(); }
public ModData(Manifest mod, InstalledMods mods, bool useLoadScreen = false) { Languages = new string[0]; // Take a local copy of the manifest Manifest = new Manifest(mod.Id, mod.Package); ObjectCreator = new ObjectCreator(Manifest, mods); PackageLoaders = ObjectCreator.GetLoaders <IPackageLoader>(Manifest.PackageFormats, "package"); ModFiles = new FS(mod.Id, mods, PackageLoaders); ModFiles.LoadFromManifest(Manifest); Manifest.LoadCustomData(ObjectCreator); if (useLoadScreen) { LoadScreen = ObjectCreator.CreateObject <ILoadScreen>(Manifest.LoadScreen.Value); LoadScreen.Init(this, Manifest.LoadScreen.ToDictionary(my => my.Value)); LoadScreen.Display(); } WidgetLoader = new WidgetLoader(this); MapCache = new MapCache(this); SoundLoaders = ObjectCreator.GetLoaders <ISoundLoader>(Manifest.SoundFormats, "sound"); SpriteLoaders = ObjectCreator.GetLoaders <ISpriteLoader>(Manifest.SpriteFormats, "sprite"); var sequenceFormat = Manifest.Get <SpriteSequenceFormat>(); var sequenceLoader = ObjectCreator.FindType(sequenceFormat.Type + "Loader"); var sequenceCtor = sequenceLoader != null?sequenceLoader.GetConstructor(new[] { typeof(ModData) }) : null; if (sequenceLoader == null || !sequenceLoader.GetInterfaces().Contains(typeof(ISpriteSequenceLoader)) || sequenceCtor == null) { throw new InvalidOperationException("Unable to find a sequence loader for type '{0}'.".F(sequenceFormat.Type)); } SpriteSequenceLoader = (ISpriteSequenceLoader)sequenceCtor.Invoke(new[] { this }); SpriteSequenceLoader.OnMissingSpriteError = s => Log.Write("debug", s); var modelFormat = Manifest.Get <ModelSequenceFormat>(); var modelLoader = ObjectCreator.FindType(modelFormat.Type + "Loader"); var modelCtor = modelLoader != null?modelLoader.GetConstructor(new[] { typeof(ModData) }) : null; if (modelLoader == null || !modelLoader.GetInterfaces().Contains(typeof(IModelSequenceLoader)) || modelCtor == null) { throw new InvalidOperationException("Unable to find a model loader for type '{0}'.".F(modelFormat.Type)); } ModelSequenceLoader = (IModelSequenceLoader)modelCtor.Invoke(new[] { this }); ModelSequenceLoader.OnMissingModelError = s => Log.Write("debug", s); Hotkeys = new HotkeyManager(ModFiles, Game.Settings.Keys, Manifest); defaultRules = Exts.Lazy(() => Ruleset.LoadDefaults(this)); defaultTileSets = Exts.Lazy(() => { var items = new Dictionary <string, TileSet>(); foreach (var file in Manifest.TileSets) { var t = new TileSet(DefaultFileSystem, file); items.Add(t.Id, t); } return((IReadOnlyDictionary <string, TileSet>)(new ReadOnlyDictionary <string, TileSet>(items))); }); defaultSequences = Exts.Lazy(() => { var items = DefaultTileSets.ToDictionary(t => t.Key, t => new SequenceProvider(DefaultFileSystem, this, t.Value, null)); return((IReadOnlyDictionary <string, SequenceProvider>)(new ReadOnlyDictionary <string, SequenceProvider>(items))); }); initialThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId; }