refactor(generation): cultures completion

This commit is contained in:
max 2022-07-29 01:03:23 +03:00
parent 6d70458aaf
commit a832354b48
12 changed files with 679 additions and 378 deletions

View file

@ -2,7 +2,14 @@ import * as d3 from "d3";
import FlatQueue from "flatqueue";
import {cultureSets, TCultureSetName} from "config/cultureSets";
import {DISTANCE_FIELD, ELEVATION, HUNTING_BIOMES, NOMADIC_BIOMES} from "config/generation";
import {
DISTANCE_FIELD,
ELEVATION,
FOREST_BIOMES,
HUNTING_BIOMES,
MIN_LAND_HEIGHT,
NOMADIC_BIOMES
} from "config/generation";
import {ERROR, TIME, WARN} from "config/logging";
import {getColors} from "utils/colorUtils";
import {abbreviate} from "utils/languageUtils";
@ -10,8 +17,9 @@ import {getInputNumber, getInputValue, getSelectedOption} from "utils/nodeUtils"
import {minmax, rn} from "utils/numberUtils";
import {biased, P, rand} from "utils/probabilityUtils";
import {byId} from "utils/shorthands";
import {defaultNameBases} from "config/namebases";
const {COA, Names} = window;
const {COA} = window;
const cultureTypeBaseExpansionism: {[key in TCultureType]: number} = {
Generic: 1,
@ -33,17 +41,16 @@ export const generateCultures = function (
"p" | "i" | "g" | "t" | "h" | "haven" | "harbor" | "f" | "r" | "fl" | "s" | "pop" | "biome"
>,
temp: Int8Array
): {cultureIds: Uint16Array; cultures: TCultures} {
): TCultures {
TIME && console.time("generateCultures");
const wildlands: IWilderness = {name: "Wildlands", i: 0, base: 1, origins: [null], shield: "round"};
const cultureIds = new Uint16Array(cells.i.length); // cell cultures
const populatedCellIds = cells.i.filter(cellId => cells.pop[cellId] > 0);
const maxSuitability = d3.max(cells.s)!;
const culturesNumber = getCulturesNumber(populatedCellIds.length);
if (!culturesNumber) return {cultureIds, cultures: [wildlands]};
if (!culturesNumber) return [wildlands];
const culturesData = selectCulturesData(culturesNumber);
const colors = getColors(culturesNumber);
@ -69,13 +76,12 @@ export const generateCultures = function (
centers.add(cells.p[center]);
codes.push(code);
cultureIds[center] = index + 1;
return {i: index + 1, name, base, center, color, type, expansionism, origins, code, shield};
});
TIME && console.timeEnd("generateCultures");
return {cultureIds, cultures: [wildlands, ...definedCultures]};
return [wildlands, ...definedCultures];
function getCulturesNumber(populatedCells: number) {
const culturesDesired = getInputNumber("culturesInput");
@ -117,7 +123,7 @@ export const generateCultures = function (
}
function selectCulturesData(culturesNumber: number) {
let defaultCultures = getDefault(culturesNumber);
let defaultCultures = getDefaultCultures(culturesNumber);
if (defaultCultures.length <= culturesNumber) return defaultCultures;
const cultures = [];
@ -127,7 +133,7 @@ export const generateCultures = function (
do {
rnd = rand(defaultCultures.length - 1);
culture = defaultCultures[rnd];
} while (!P(culture.odd));
} while (!P(culture.chance));
cultures.push(culture);
defaultCultures.splice(rnd, 1);
}
@ -135,6 +141,15 @@ export const generateCultures = function (
return cultures;
}
function getDefaultCultures(culturesNumber: number) {
const cultureSet = getInputValue("culturesSet") as TCultureSetName;
if (cultureSet in cultureSets) {
return cultureSets[cultureSet](culturesNumber);
}
throw new Error(`Unsupported culture set: ${cultureSet}`);
}
function placeCenter(sortingString: string) {
let spacing = (graphWidth + graphHeight) / 2 / culturesNumber;
@ -158,28 +173,30 @@ export const generateCultures = function (
let cellId: number;
const sortingMethods = {
n: () => Math.ceil((cells.s[cellId] / maxSuitability) * 3), // normalized cell score
// normalized cell score
score: () => Math.ceil((cells.s[cellId] / maxSuitability) * 3),
td: (goalTemp: number) => {
// temperature delta
temp: (goalTemp: number) => {
const tempDelta = Math.abs(temp[cells.g[cellId]] - goalTemp);
return tempDelta ? tempDelta + 1 : 1;
},
bd: (biomes: number[], fee = 4) => {
// biome delta
biome: (biomes: number[], fee = 4) => {
return biomes.includes(cells.biome[cellId]) ? 1 : fee;
},
sf: (fee = 4) => {
// fee if for an ocean coast
oceanCoast: (fee = 4) => {
const haven = cells.haven[cellId];
const havenHeature = features[haven];
return haven && havenHeature && havenHeature.type !== "lake" ? 1 : fee;
const havenFeature = features[haven];
return haven && havenFeature && havenFeature.type === "ocean" ? 1 : fee;
},
t: () => cells.t[cellId],
h: () => cells.h[cellId],
s: () => cells.s[cellId]
coastDist: () => cells.t[cellId],
height: () => cells.h[cellId],
suitability: () => cells.s[cellId]
};
const allSortingMethods = `{${Object.keys(sortingMethods).join(", ")}}`;
@ -245,7 +262,7 @@ export const generateCultures = function (
// make sure namesbase exists in nameBases
if (!nameBases.length) {
ERROR && console.error("Name base is empty, default nameBases will be applied");
nameBases = Names.getNameBases();
nameBases = [...defaultNameBases];
}
// check if base is in nameBases
@ -256,48 +273,47 @@ export const generateCultures = function (
}
};
export const getDefault = function (culturesNumber: number) {
const cultureSet = getInputValue("culturesSet") as TCultureSetName;
if (cultureSet in cultureSets) {
return cultureSets[cultureSet](culturesNumber);
}
throw new Error(`Unsupported culture set: ${cultureSet}`);
};
// expand cultures across the map (Dijkstra-like algorithm)
export const expand = function () {
export const expandCultures = function (
cultures: TCultures,
features: TPackFeatures,
cells: Pick<IPack["cells"], "c" | "area" | "h" | "t" | "f" | "r" | "fl" | "biome" | "pop">
) {
TIME && console.time("expandCultures");
const queue = new FlatQueue();
pack.cultures.forEach(culture => {
if (!culture.i || culture.removed) return;
const cultureIds = new Uint16Array(cells.h.length); // cell cultures
const isWilderness = (culture: ICulture | IWilderness): culture is IWilderness => culture.i === 0;
const queue = new FlatQueue<{cellId: number; cultureId: number}>();
cultures.forEach(culture => {
if (isWilderness(culture) || culture.removed) return;
queue.push({cellId: culture.center, cultureId: culture.i}, 0);
});
const neutral = (cells.i.length / 5000) * 3000 * neutralInput.value; // limit cost for culture growth
const cost = [];
const cellsNumberFactor = cells.h.length / 1.6;
const neutral = cellsNumberFactor * getInputNumber("neutralInput"); // limit cost for culture growth
const cost: number[] = [];
while (queue.length) {
const priority = queue.peekValue();
const {cellId, cultureId} = queue.pop();
const priority = queue.peekValue()!;
const {cellId, cultureId} = queue.pop()!;
if (cultureId === 0) throw new Error("Wilderness culture should not expand");
const {type, expansionism} = cultures[cultureId] as ICulture;
const cultureBiome = cells.biome[cellId];
const type = pack.cultures[cultureId].type;
cells.c[cellId].forEach(neibCellId => {
const biome = cells.biome[neibCellId];
const biomeCost = getBiomeCost(cultureId, biome, type);
const biomeChangeCost = biome === cells.biome[cellId] ? 0 : 20; // penalty on biome change
const biomeCost = getBiomeCost(biome, cultureBiome, type);
const heightCost = getHeightCost(neibCellId, cells.h[neibCellId], type);
const riverCost = getRiverCost(cells.r[neibCellId], neibCellId, type);
const typeCost = getTypeCost(cells.t[neibCellId], type);
const totalCost =
priority +
(biomeCost + biomeChangeCost + heightCost + riverCost + typeCost) / pack.cultures[cultureId].expansionism;
const totalCost = priority + (biomeCost + heightCost + riverCost + typeCost) / expansionism;
if (totalCost > neutral) return;
if (!cost[neibCellId] || totalCost < cost[neibCellId]) {
if (cells.s[neibCellId] > 0) cells.culture[neibCellId] = cultureId; // assign culture to populated cell
if (cells.pop[neibCellId] > 0) cultureIds[neibCellId] = cultureId; // assign culture to populated cell
cost[neibCellId] = totalCost;
queue.push({cellId: neibCellId, cultureId}, totalCost);
}
@ -305,87 +321,47 @@ export const expand = function () {
}
TIME && console.timeEnd("expandCultures");
};
return cultureIds;
function getBiomeCost(cultureId: number, biome: number, type: TCultureType) {
const center = cultureId && (pack.cultures[cultureId] as ICulture).center;
const cultureBiome = cells.biome[center];
if (cultureBiome === biome) return 10; // tiny penalty for native biome
if (type === "Hunting") return biomesData.cost[biome] * 5; // non-native biome penalty for hunters
if (type === "Nomadic" && biome > 4 && biome < 10) return biomesData.cost[biome] * 10; // forest biome penalty for nomads
return biomesData.cost[biome] * 2; // general non-native biome penalty
}
function getHeightCost(cellId: number, height: number, type: TCultureType) {
const feature = pack.features[cells.f[cellId]];
const area = cells.area[cellId];
if (type === "Lake" && feature && feature.type === "lake") return 10; // no lake crossing penalty for Lake cultures
if (type === "Naval" && height < 20) return area * 2; // low sea/lake crossing penalty for Naval cultures
if (type === "Nomadic" && height < 20) return area * 50; // giant sea/lake crossing penalty for Nomads
if (height < 20) return area * 6; // general sea/lake crossing penalty
if (type === "Highland" && height < 44) return 3000; // giant penalty for highlanders on lowlands
if (type === "Highland" && height < 62) return 200; // giant penalty for highlanders on lowhills
if (type === "Highland") return 0; // no penalty for highlanders on highlands
if (height >= 67) return 200; // general mountains crossing penalty
if (height >= 44) return 30; // general hills crossing penalty
return 0;
}
function getRiverCost(riverId: number, cellId: number, type: TCultureType) {
if (type === "River") return riverId ? 0 : 100; // penalty for river cultures
if (!riverId) return 0; // no penalty for others if there is no river
return minmax(cells.fl[cellId] / 10, 20, 100); // river penalty from 20 to 100 based on flux
}
function getTypeCost(t: number, type: TCultureType) {
if (t === LAND_COAST) return type === "Naval" || type === "Lake" ? 0 : type === "Nomadic" ? 60 : 20; // penalty for coastline
if (t === LANDLOCKED) return type === "Naval" || type === "Nomadic" ? 30 : 0; // low penalty for land level 2 for Navals and nomads
if (t !== WATER_COAST) return type === "Naval" || type === "Lake" ? 100 : 0; // penalty for mainland for navals
return 0;
}
export const add = function (center: number) {
const defaultCultures = getDefault();
let culture, base, name;
if (pack.cultures.length < defaultCultures.length) {
// add one of the default cultures
culture = pack.cultures.length;
base = defaultCultures[culture].base;
name = defaultCultures[culture].name;
} else {
// add random culture besed on one of the current ones
culture = rand(pack.cultures.length - 1);
name = Names.getCulture(culture, 5, 8, "");
base = pack.cultures[culture].base;
function getBiomeCost(biome: number, cultureBiome: number, type: TCultureType) {
if (cultureBiome === biome) return 10; // tiny penalty for native biome
if (type === "Hunting") return biomesData.cost[biome] * 5; // non-native biome penalty for hunters
if (type === "Nomadic" && FOREST_BIOMES.includes(biome)) return biomesData.cost[biome] * 10; // forest biome penalty for nomads
return biomesData.cost[biome] * 2; // general non-native biome penalty
}
const code = abbreviate(
name,
pack.cultures.map(c => c.code)
);
const i = pack.cultures.length;
const color = d3.color(d3.scaleSequential(d3.interpolateRainbow)(Math.random())).hex();
// define emblem shape
let shield = culture.shield;
const emblemShape = document.getElementById("emblemShape").value;
if (emblemShape === "random") shield = COA.getRandomShield();
function getHeightCost(cellId: number, height: number, type: TCultureType) {
if (height < MIN_LAND_HEIGHT) {
const feature = features[cells.f[cellId]];
const area = cells.area[cellId];
pack.cultures.push({
name,
color,
base,
center,
i,
expansionism: 1,
type: "Generic",
cells: 0,
area: 0,
rural: 0,
urban: 0,
origins: [0],
code,
shield
});
if (type === "Lake" && feature && feature.type === "lake") return 10; // almost lake crossing penalty for Lake cultures
if (type === "Naval") return area * 2; // low sea or lake crossing penalty for Naval cultures
if (type === "Nomadic") return area * 50; // giant sea or lake crossing penalty for Nomads
return area * 6; // general sea or lake crossing penalty
}
if (type === "Highland") {
if (height >= MOUNTAINS) return 0; // no penalty for highlanders on highlands
if (height < HILLS) return 3000; // giant penalty for highlanders on lowlands
return 100; // penalty for highlanders on hills
}
if (height >= MOUNTAINS) return 200; // general mountains crossing penalty
if (height >= HILLS) return 30; // general hills crossing penalty
return 0;
}
function getRiverCost(riverId: number, cellId: number, type: TCultureType) {
if (type === "River") return riverId ? 0 : 100; // penalty for river cultures
if (!riverId) return 0; // no penalty for others if there is no river
return minmax(cells.fl[cellId] / 10, 20, 100); // river penalty from 20 to 100 based on flux
}
function getTypeCost(t: number, type: TCultureType) {
if (t === LAND_COAST) return type === "Naval" || type === "Lake" ? 0 : type === "Nomadic" ? 60 : 20; // penalty for coastline
if (t === LANDLOCKED) return type === "Naval" || type === "Nomadic" ? 30 : 0; // low penalty for land level 2 for Navals and nomads
if (t !== WATER_COAST) return type === "Naval" || type === "Lake" ? 100 : 0; // penalty for mainland for navals
return 0;
}
};

View file

@ -9,7 +9,7 @@ import {rankCells} from "scripts/generation/pack/rankCells";
import {createTypedArray} from "utils/arrayUtils";
import {pick} from "utils/functionUtils";
import {rn} from "utils/numberUtils";
import {generateCultures} from "./cultures";
import {generateCultures, expandCultures} from "./cultures";
import {generateRivers} from "./rivers";
const {LAND_COAST, WATER_COAST, DEEPER_WATER} = DISTANCE_FIELD;
@ -39,7 +39,7 @@ export function createPack(grid: IGrid): IPack {
temp
);
const biome: IPack["cells"]["biome"] = Biomes.define({
const biome: Uint8Array = Biomes.define({
temp,
prec,
flux,
@ -62,7 +62,7 @@ export function createPack(grid: IGrid): IPack {
harbor
});
const {cultureIds, cultures} = generateCultures(
const cultures = generateCultures(
mergedFeatures,
{
p: cells.p,
@ -82,7 +82,18 @@ export function createPack(grid: IGrid): IPack {
temp
);
// Cultures.expand();
const cultureIds = expandCultures(cultures, mergedFeatures, {
c: cells.c,
area: cells.area,
h: heights,
t: distanceField,
f: featureIds,
r: riverIds,
fl: flux,
biome,
pop: population
});
// BurgsAndStates.generate();
// Religions.generate();
// BurgsAndStates.defineStateForms();