public void DecomposeCells(SerializableMapData mapData) { Grid.Build(mapData.CellCountX, mapData.CellCountZ); foreach (var cellData in mapData.HexCells) { var cellToModify = Grid.GetCellAtCoordinates(cellData.Coordinates); CellModificationLogic.ChangeTerrainOfCell(cellToModify, cellData.Terrain); CellModificationLogic.ChangeShapeOfCell(cellToModify, cellData.Shape); CellModificationLogic.ChangeVegetationOfCell(cellToModify, cellData.Vegetation); CellModificationLogic.ChangeFeatureOfCell(cellToModify, cellData.Feature); CellModificationLogic.ChangeHasRoadsOfCell(cellToModify, cellData.HasRoads); cellToModify.SuppressSlot = cellData.SuppressSlot; //Converging rivers (where two rivers combine and flow into a third) have //order-sensitive creation, since attempting to place both of the inflow //rivers before the outflow river has been created is invalid. To account for //this, we delay the creation of any invalid rivers (since those represent //an inflow being attached to another inflow) until after all other rivers //have been placed. var delayedRivers = new List <System.Tuple <IHexCell, HexDirection, RiverFlow> >(); for (int i = 0; i < 6; i++) { var edge = (HexDirection)i; if (cellData.HasRiverAtEdge[i] && !RiverCanon.HasRiverAlongEdge(cellToModify, edge)) { if (RiverCanon.CanAddRiverToCell(cellToModify, edge, cellData.DirectionOfRiverAtEdge[i])) { RiverCanon.AddRiverToCell(cellToModify, edge, cellData.DirectionOfRiverAtEdge[i]); } else { delayedRivers.Add(new System.Tuple <IHexCell, HexDirection, RiverFlow>( cellToModify, edge, cellData.DirectionOfRiverAtEdge[i] )); } } } foreach (var river in delayedRivers) { if (RiverCanon.CanAddRiverToCell(river.Item1, river.Item2, river.Item3)) { RiverCanon.AddRiverToCell(river.Item1, river.Item2, river.Item3); } else { throw new InvalidOperationException(string.Format( "Failed to decompose river ({0}, {1}, {2})", river.Item1, river.Item2, river.Item3 )); } } cellToModify.WorkerSlot.IsOccupied = cellData.IsSlotOccupied; cellToModify.WorkerSlot.IsLocked = cellData.IsSlotLocked; } foreach (var chunk in Grid.Chunks) { chunk.Refresh(MapRendering.TerrainRefreshType.All); } }