From c4c775a7787beb376ae5059382838f4b118f063c Mon Sep 17 00:00:00 2001 From: Aaron T Date: Wed, 3 Jan 2024 19:31:08 +0000 Subject: [PATCH] fix: factions have at least one city (#54) --- RTWLibPlus/map/voronoi.cs | 30 ++++++++++++++++++++++++++++++ RTWLibPlus/randomiser/randDS.cs | 8 +++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/RTWLibPlus/map/voronoi.cs b/RTWLibPlus/map/voronoi.cs index 2e044c6..ca2dd38 100644 --- a/RTWLibPlus/map/voronoi.cs +++ b/RTWLibPlus/map/voronoi.cs @@ -1,6 +1,7 @@ namespace RTWLibPlus.map; using RTWLibPlus.helpers; using RTWLibPlus.randomiser; +using System; using System.Collections.Generic; using System.Numerics; @@ -30,8 +31,37 @@ public static List GetVoronoiGroups(Dictionary coords groups[closest] = groups[closest].Add(c.Key); } + CheckGroupsNotEmpty(groups); + return groups; + } + + private static void CheckGroupsNotEmpty(List groups) + { + for (int i = 0; i < groups.Count; i++) + { + string[] arr = groups[i]; + if (groups[i].Length == 0) + { + StealSettlement(groups, i); + } + } + } + private static void StealSettlement(List groups, int emptyGroupInd) + { + for (int i = 0; i < groups.Count; i++) + { + if (groups[i].Length > 2) + { + string temp = groups[i][^1]; + Console.WriteLine("city to add " + temp); + groups[emptyGroupInd] = groups[emptyGroupInd].Add(temp); + Console.WriteLine(groups[emptyGroupInd][0]); + groups[i] = groups[i].Remove(groups[i].Length - 1); + break; + } + } } public static int GetClosestPoint(Vector2[] points, Vector2 coord) diff --git a/RTWLibPlus/randomiser/randDS.cs b/RTWLibPlus/randomiser/randDS.cs index e0aaf00..dffeed9 100644 --- a/RTWLibPlus/randomiser/randDS.cs +++ b/RTWLibPlus/randomiser/randDS.cs @@ -54,11 +54,13 @@ public static string RandCitiesVoronoi(SMF smf, RandWrap rnd, DS ds = null, DR d Vector2[] vp = Voronoi.GetVoronoiPoints(factions.Count, cm.Width, cm.Height, rnd); List gh = Voronoi.GetVoronoiGroups(cm.CityCoordinates, vp); - // gh.Shuffle(TWRand.rnd); - // factions.Shuffle(TWRand.rnd); - // function to get missing settlements and add them to the pool for (int i = 0; i < factions.Count; i++) { + if (gh[i].Length == 0) + { + Console.WriteLine("no settlements in group"); + } + foreach (string region in gh[i]) { IBaseObj city = ds.GetItemByValue(settlements, region);