From 58f9f2f43463fefd12395f62b1fb95ea0682b55e Mon Sep 17 00:00:00 2001
From: Arkerthan <arkerthan@mailbox.org>
Date: Tue, 18 Mar 2025 09:34:06 +0100
Subject: [PATCH] Round Populations at the end after scaling to a useful range

---
 src/events/intro/customizeSlaveTrade.js | 29 ++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/src/events/intro/customizeSlaveTrade.js b/src/events/intro/customizeSlaveTrade.js
index 49ae5dabc87..8ef755929fd 100644
--- a/src/events/intro/customizeSlaveTrade.js
+++ b/src/events/intro/customizeSlaveTrade.js
@@ -444,6 +444,7 @@ App.Intro.CustomSlaveTrade = function() {
 			console.log(lon, lat);
 			populateFromCoordinates(lat, lon);
 			scalePopulations();
+			normalizePopulation();
 			refresh();
 		}
 	}
@@ -475,12 +476,30 @@ App.Intro.CustomSlaveTrade = function() {
 
 		// Apply pop scale
 		for (const n in V.nationalities) {
-			const pop = Math.round(V.nationalities[n] * cache[n]);
-			if (n === "Vatican") {
-				console.log(pop, V.nationalities[n]);
+			V.nationalities[n] *= cache[n];
+		}
+	}
+
+	/**
+	 * Normalize population numbers and round them.
+	 */
+	function normalizePopulation() {
+		// Find max
+		let max = 0;
+		for (const n in V.nationalities) {
+			if (V.nationalities[n] > max) {
+				max = V.nationalities[n];
 			}
-			if (pop > 0) {
-				V.nationalities[n] = pop;
+		}
+		// Scale everything relative to max
+		// relMax controls at which point small populations are cut off. The larger, the smaller allowed populations
+		// will be. This does not change their relative likelihood.
+		const relMax = 1000.0;
+		const scale = relMax / max;
+		for (const n in V.nationalities) {
+			const val = Math.round(V.nationalities[n] * scale);
+			if (val > 0) {
+				V.nationalities[n] = val;
 			} else {
 				delete V.nationalities[n];
 			}
-- 
GitLab