static void HexifyFrontiersMenuOption(MenuCommand command) { WMSK_Editor editor = (WMSK_Editor)command.context; if (!editor.map.showGrid) { EditorUtility.DisplayDialog("Hexify Frontiers", "Grid must be enabled in WMSK inspector.", "Ok"); return; } editor.ClearSelection(); if (editor.editingMode == EDITING_MODE.COUNTRIES) { if (!EditorUtility.DisplayDialog("Hexify Frontiers", "This command will adjust COUNTRY frontiers (NOT PROVINCES) to match grid shape. If you want to include province borders, switch 'Show Layers' setting to Country + Provinces.\n\nBefore continuing, make sure the grid dimensions are fine.", "Ok", "Cancel")) { return; } HexifyOpContext cc = new HexifyOpContext { title = "Hexifying Countries...", progress = hexifyProgress, finish = hexifyFinished }; EditorCoroutines.Start(editor.HexifyCountries(cc)); } else { if (!EditorUtility.DisplayDialog("Hexify Frontiers", "This command will adjust COUNTRY and PROVINCE borders to match grid shape. Before continuing, make sure the grid dimensions are fine.", "Ok", "Cancel")) { return; } EditorCoroutines.Start(editor.HexifyAll(hexifyProgress, hexifyFinished)); } }
/// <summary> /// Adjusts all countries frontiers to match the hexagonal grid /// </summary> public IEnumerator HexifyAll(HexifyOperationProgress progressOp, HexifyOperationFinish finishOp) { HexifyOpContext cc = new HexifyOpContext { title = "Hexifying Countries...", progress = progressOp, finish = null }; yield return(HexifyCountries(cc)); cc.title = "Hexifying Provinces..."; cc.finish = finishOp; yield return(HexifyProvinces(cc)); }
/// <summary> /// Adjusts all countries frontiers to match the hexagonal grid /// </summary> public IEnumerator HexifyCountries(HexifyOpContext context) { Cell[] cells = _map.cells; if (cells == null) { yield break; } // Initialization cancelled = false; hexifyContext = context; if (procCells == null || procCells.Length < cells.Length) { procCells = new RegionCell[cells.Length]; } for (int k = 0; k < cells.Length; k++) { procCells [k].entityIndex = -1; } // Compute area of a single cell; for optimization purposes we'll ignore all regions whose surface is smaller than a 20% the size of a cell float minArea = 0; Region templateCellRegion = null; for (int k = 0; k < cells.Length; k++) { if (cells [k] != null) { templateCellRegion = new Region(null, 0); templateCellRegion.UpdatePointsAndRect(cells [k].points, true); minArea = templateCellRegion.rect2DArea * 0.2f; break; } } if (templateCellRegion == null) { yield break; } if (hexagonPoints == null || hexagonPoints.Length != 6) { hexagonPoints = new Vector2[6]; } for (int k = 0; k < 6; k++) { hexagonPoints [k] = templateCellRegion.points [k] - templateCellRegion.center; } // Pass 1: remove minor regions yield return(RemoveSmallRegions(minArea, _map.countries)); // Pass 2: assign all region centers to each country from biggest country to smallest country if (!cancelled) { yield return(AssignRegionCenters(_map.countries)); } // Pass 3: add cells to target countries if (!cancelled) { yield return(AddHexagons(_map.countries)); } // Pass 4: merge adjacent regions if (!cancelled) { yield return (MergeAdjacentRegions(_map.countries)); } // Pass 5: remove cells from other countries if (!cancelled) { yield return(RemoveHexagons(_map.countries)); } // Pass 6: update geometry of resulting countries if (!cancelled) { yield return(UpdateCountries()); } if (!cancelled) { _map.OptimizeFrontiers(); _map.Redraw(true); } hexifyContext.progress(1f, hexifyContext.title, ""); // hide progress bar yield return(null); if (hexifyContext.finish != null) { hexifyContext.finish(cancelled); } }