/// <summary>Recalculates tile system. </summary> public static void FixTiles(TileLayer tiles, TileCollection collection) { Logger.Info("Recalculating tile LAT system"); // change all CLAT tiles to their corresponding LAT tiles foreach (MapTile t in tiles) { // If this tile comes from a CLAT (connecting lat) set, // then replace it's set and tilenr by corresponding LAT sets' t.SetNum = collection.GetSetNum(t.TileNum); if (collection.IsCLAT(t.SetNum)) { t.SetNum = collection.GetLAT(t.SetNum); t.TileNum = collection.GetTileNumFromSet(t.SetNum); } } // apply autolat foreach (MapTile t in tiles) { // If this tile is a LAT tile, we might have to connect it if (collection.IsLAT(t.SetNum)) { // Which tile to use from CLAT tileset byte transitionTile = 0; MapTile tileTopRight = tiles.GetNeighbourTile(t, TileLayer.TileDirection.TopRight); MapTile tileBottomRight = tiles.GetNeighbourTile(t, TileLayer.TileDirection.BottomRight); MapTile tileBottomLeft = tiles.GetNeighbourTile(t, TileLayer.TileDirection.BottomLeft); MapTile tileTopLeft = tiles.GetNeighbourTile(t, TileLayer.TileDirection.TopLeft); // Find out setnums of adjacent cells if (tileTopRight != null && collection.ConnectTiles(t.SetNum, tileTopRight.SetNum)) { transitionTile += 1; } if (tileBottomRight != null && collection.ConnectTiles(t.SetNum, tileBottomRight.SetNum)) { transitionTile += 2; } if (tileBottomLeft != null && collection.ConnectTiles(t.SetNum, tileBottomLeft.SetNum)) { transitionTile += 4; } if (tileTopLeft != null && collection.ConnectTiles(t.SetNum, tileTopLeft.SetNum)) { transitionTile += 8; } // Crystal LAT tile connects to specific tiles in CrystalCliff if (collection.IsCrystalLAT(t.SetNum)) { if (tileTopRight != null && collection.IsCrystalCliff(tileTopRight.SetNum) && tileTopRight.TileNum == collection.GetTileNumFromSet(tileTopRight.SetNum, 1)) { transitionTile = 0; } if (tileBottomRight != null && collection.IsCrystalCliff(tileBottomRight.SetNum) && tileBottomRight.TileNum == collection.GetTileNumFromSet(tileBottomRight.SetNum, 4)) { transitionTile = 0; } if (tileBottomLeft != null && collection.IsCrystalCliff(tileBottomLeft.SetNum) && tileBottomLeft.TileNum == collection.GetTileNumFromSet(tileBottomLeft.SetNum, 0)) { transitionTile = 0; } if (tileTopLeft != null && collection.IsCrystalCliff(tileTopLeft.SetNum) && tileTopLeft.TileNum == collection.GetTileNumFromSet(tileTopLeft.SetNum, 5)) { transitionTile = 0; } } // Swamp has TilesInSet=9 instead of 1 for LAT tilesets // which doubles as a normal set for remaining tiles. if (collection.IsSwampLAT(t.SetNum) && t.TileNum > collection.GetTileNumFromSet(t.SetNum, 0)) { transitionTile = 0; } if (transitionTile > 0) { // Find Tileset that contains the connecting pieces short clatSet = collection.GetCLATSet(t.SetNum); // Do not change this setnum, as then we could recognize it as // a different tileset for later tiles around this one. // (T->SetNum = clatSet;) t.TileNum = collection.GetTileNumFromSet(clatSet, transitionTile); t.Drawable = collection.GetDrawable(t); } } // apply ramp fixup else if (t.SetNum == collection.RampBase) { var ti = t.GetTileImage(); if (ti.RampType < 1 || 4 < ti.TerrainType) { continue; } int fixup = -1; MapTile tileTopRight = tiles.GetNeighbourTile(t, TileLayer.TileDirection.TopRight); MapTile tileBottomRight = tiles.GetNeighbourTile(t, TileLayer.TileDirection.BottomRight); MapTile tileBottomLeft = tiles.GetNeighbourTile(t, TileLayer.TileDirection.BottomLeft); MapTile tileTopLeft = tiles.GetNeighbourTile(t, TileLayer.TileDirection.TopLeft); switch (ti.RampType) { case 1: // northwest facing if (tileTopLeft != null && tileTopLeft.GetTileImage().RampType == 0) { fixup++; } if (tileBottomRight != null && tileBottomRight.GetTileImage().RampType == 0) { fixup += 2; } break; case 2: // northeast facing if (tileTopRight != null && tileTopRight.GetTileImage().RampType == 0) { fixup++; } if (tileBottomLeft != null && tileBottomLeft.GetTileImage().RampType == 0) { fixup += 2; } break; case 3: // southeast facing if (tileBottomRight != null && tileBottomRight.GetTileImage().RampType == 0) { fixup++; } if (tileTopLeft != null && tileTopLeft.GetTileImage().RampType == 0) { fixup += 2; } break; case 4: // southwest facing if (tileBottomLeft != null && tileBottomLeft.GetTileImage().RampType == 0) { fixup++; } if (tileTopRight != null && tileTopRight.GetTileImage().RampType == 0) { fixup += 2; } break; } if (fixup != -1) { t.TileNum = collection.GetTileNumFromSet(collection.RampSmooth, (byte)((ti.RampType - 1) * 3 + fixup)); // update drawable too t.Drawable = collection.GetDrawable(t); } } } }