/// <summary> /// Using the given Mapping Dictionaries this function will /// split the Mapfiles provided in the Server Map File Format /// into Binary Map Files using directly the Tileset Tile and /// Overlay Ids. The Map Files will be equally sized Chunks of /// the complete Map. These Chunks are better to stream while /// gameplay and do not need too much resources on the disk. /// /// This function will save each chunk to the user disk. /// </summary> public void Create() { string[] mapFiles = System.IO.Directory.GetFiles( String.Concat(Game.FileSystem.UserDirectory, Constants.UserData.ServerMapPath), "*.tiles.txt", SearchOption.AllDirectories); int worldMinX = int.MaxValue; int worldMinY = int.MaxValue; int worldMaxX = int.MinValue; int worldMaxY = int.MinValue; foreach (var mapFile in mapFiles) { RawMap map = LoadSingleMap(mapFile); if (!worldMapInLayers.ContainsKey(map.Layer)) { worldMapInLayers.Add(map.Layer, new List <RawMap>()); } worldMapInLayers[map.Layer].Add(map); if (map.StartX < worldMinX) { worldMinX = map.StartX; } if (map.StartY < worldMinY) { worldMinY = map.StartY; } if (map.StartX + map.Width > worldMaxX) { worldMaxX = map.StartX + map.Width; } if (map.StartY + map.Height > worldMaxY) { worldMaxY = map.StartY + map.Height; } } for (int baseX = worldMinX; baseX < worldMaxX; baseX += Constants.Map.Chunksize) { for (int baseY = worldMinY; baseY < worldMaxY; baseY += Constants.Map.Chunksize) { CreateSingleChunk(baseX, baseY); } } }
/// <summary> /// Loads a single illarion map file /// </summary> /// <param name="mapFile">the path of the illarion map file</param> /// <returns>the map file as data class</returns> private RawMap LoadSingleMap(string mapFile) { StreamReader fileReader = new StreamReader(mapFile); string line; int next; bool read = true; RawMap map = new RawMap(); // Get the map metadata: // Origin (x/y), Layer, Width, Height while (read && (next = fileReader.Peek()) != -1) { switch (next) { case 'L': line = fileReader.ReadLine(); map.Layer = int.Parse(line.Substring(3, line.Length - 3)); break; case 'X': line = fileReader.ReadLine(); map.StartX = int.Parse(line.Substring(3, line.Length - 3)); break; case 'Y': line = fileReader.ReadLine(); map.StartY = int.Parse(line.Substring(3, line.Length - 3)); break; case 'W': line = fileReader.ReadLine(); map.Width = int.Parse(line.Substring(3, line.Length - 3)); break; case 'H': line = fileReader.ReadLine(); map.Height = int.Parse(line.Substring(3, line.Length - 3)); break; case '#': line = fileReader.ReadLine(); break; case 'V': line = fileReader.ReadLine(); break; default: read = false; break; } } map.MapArray = new int[map.Width, map.Height]; // Get the compressed tile ids from the map data file while ((line = fileReader.ReadLine()) != null) { string[] rowValues = line.Split((new string[] { ";" }), StringSplitOptions.RemoveEmptyEntries); map.MapArray[int.Parse(rowValues[0]), int.Parse(rowValues[1])] = int.Parse(rowValues[2]); } fileReader.Close(); // Get the corresponding item data file and transform them into MapObjects string itemPath = String.Concat(mapFile.Substring(0, mapFile.Length - 9), "items.txt"); if (!File.Exists(itemPath)) { throw new FileNotFoundException($"{itemPath} not found!"); } fileReader = new StreamReader(itemPath); Dictionary <Vector2i, List <MapObject> > itemDic = new Dictionary <Vector2i, List <MapObject> >(); while ((line = fileReader.ReadLine()) != null) { if (line.StartsWith("#") || line.Equals("")) { continue; } string[] rowValues = line.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); Vector2i position = new Vector2i(int.Parse(rowValues[0]), int.Parse(rowValues[1])); if (!itemDic.ContainsKey(position)) { itemDic.Add(position, new List <MapObject>()); } MapObject item = new MapObject(); item.BaseId = int.Parse(rowValues[2]); string name = null; string description = null; if (Game.Config.Language == Language.German) { name = rowValues.FirstOrDefault(x => x.StartsWith("nameDe")); description = rowValues.FirstOrDefault(x => x.StartsWith("descriptionDe")); } else { name = rowValues.FirstOrDefault(x => x.StartsWith("nameEn")); description = rowValues.FirstOrDefault(x => x.StartsWith("descriptionEn")); } if (name != null) { item.Name = name.Substring(7); } if (description != null) { item.Description = description.Substring(14); } itemDic[position].Add(item); } fileReader.Close(); Dictionary <Vector2i, MapObject[]> arrayItemDic = new Dictionary <Vector2i, MapObject[]>(itemDic.Count); foreach (var item in itemDic) { arrayItemDic.Add(item.Key, item.Value.ToArray()); } map.Items = arrayItemDic; // Get the corresponding warp data file and transform them into Vector3i string warpPath = String.Concat(mapFile.Substring(0, mapFile.Length - 9), "warps.txt"); if (!File.Exists(warpPath)) { throw new FileNotFoundException($"{warpPath} not found!"); } fileReader = new StreamReader(warpPath); Dictionary <Vector2i, Vector3i> warpDic = new Dictionary <Vector2i, Vector3i>(); while ((line = fileReader.ReadLine()) != null) { if (line.StartsWith("#") || line.Equals("")) { continue; } string[] rowValues = line.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); warpDic.Add(new Vector2i( int.Parse(rowValues[0]), int.Parse(rowValues[1]) ), new Vector3i( int.Parse(rowValues[2]), int.Parse(rowValues[3]), int.Parse(rowValues[4]) )); } fileReader.Close(); map.Warps = warpDic; return(map); }