private void LoadRegions(string tmxFiles) { foreach (var mapFilePath in Directory.GetFiles(tmxFiles, "*.tmx", SearchOption.AllDirectories)) { using (var xmlReader = XmlReader.Create(File.OpenRead(mapFilePath), new XmlReaderSettings { DtdProcessing = DtdProcessing.Parse, XmlResolver = null })) { var document = new XPathDocument(xmlReader); var nav = document.CreateNavigator(); var nodeIter = nav.Select("/map/layer/data"); var region = new MapGenRegion(); //load tile info while (nodeIter.MoveNext()) { Debug.Assert(nodeIter.Current != null, "nodeIter.Current != null"); byte[] zippedMap = Convert.FromBase64String(nodeIter.Current.Value); using (var gzip = new GZipStream(new MemoryStream(zippedMap), CompressionMode.Decompress)) { var tmpMap = new byte[TMX_REGION_WIDTH * TMX_REGION_HEIGHT * sizeof(int)]; gzip.Read(tmpMap, 0, TMX_REGION_WIDTH * TMX_REGION_HEIGHT * sizeof(int)); int cnt = 0; for (int i = 0; i < tmpMap.Length; i += sizeof(int)) { var tileId = (ushort)(BitConverter.ToInt32(tmpMap, i) - 1); if (objectTypeFactory.IsTileType("CityStartTile", tileId)) { tileId = 0; } region.Map[cnt++] = tileId; } } break; //not supporting multi layer } regions.Add(region); } } if (regions.Count == 0) { Console.Out.WriteLine("No maps were found. They should be in the /map/ folder"); Environment.Exit(-1); } }
private void GenerateMapArray() { for (int row = 0; row < TMX_REGION_ROWS; ++row) { for (int col = 0; col < TMX_REGION_COLUMNS; ++col) { MapGenRegion region = regions[random.Next(regions.Count)]; var regionMap = new ushort[TMX_REGION_WIDTH * TMX_REGION_HEIGHT]; Buffer.BlockCopy(region.Map, 0, regionMap, 0, region.Map.Length * sizeof(ushort)); ushort[] data = regionMap; for (int y = 0; y < TMX_REGION_HEIGHT; ++y) { for (int x = 0; x < TMX_REGION_WIDTH; ++x) { map[TMX_REGION_WIDTH * col + x, TMX_REGION_HEIGHT *row + y] = data[y * TMX_REGION_WIDTH + x]; } } } } }