Skip to content

Commit

Permalink
fix: factions have at least one city (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
sargeantPig authored Jan 3, 2024
1 parent 4a95543 commit c4c775a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
30 changes: 30 additions & 0 deletions RTWLibPlus/map/voronoi.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace RTWLibPlus.map;
using RTWLibPlus.helpers;
using RTWLibPlus.randomiser;
using System;
using System.Collections.Generic;
using System.Numerics;

Expand Down Expand Up @@ -30,8 +31,37 @@ public static List<string[]> GetVoronoiGroups(Dictionary<string, Vector2> coords
groups[closest] = groups[closest].Add(c.Key);
}

CheckGroupsNotEmpty(groups);

return groups;
}

private static void CheckGroupsNotEmpty(List<string[]> 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<string[]> 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)
Expand Down
8 changes: 5 additions & 3 deletions RTWLibPlus/randomiser/randDS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> 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);
Expand Down

0 comments on commit c4c775a

Please sign in to comment.