Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: factions have at least one city #54

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading