refactor: specify burgs

This commit is contained in:
max 2022-08-07 00:24:04 +03:00
parent aa6cefb683
commit 7f57c9af65
14 changed files with 151 additions and 45 deletions

View file

@ -21,11 +21,12 @@ export function createStates(capitals: TCapitals, cultures: TCultures) {
const id = index + 1;
const name = getStateName(cellId, capitalName, cultureId, cultures);
const color = colors[index];
const type = (cultures[cultureId] as ICulture).type;
const {type, shield: cultureShield} = cultures[cultureId] as ICulture;
const expansionism = rn(Math.random() * powerInput + 1, 1);
const shield = COA.getShield(cultureId, null, cultures);
const coa = {...COA.generate(null, null, null, type), shield};
const shield = COA.getShield(cultureShield, null);
const coa: ICoa = {...COA.generate(null, null, null, type), shield};
return {i: id, center: cellId, type, name, color, expansionism, capital: capitalId, culture: cultureId, coa};
});
@ -34,7 +35,7 @@ export function createStates(capitals: TCapitals, cultures: TCultures) {
return [NEUTRALS, ...states];
}
function getStateName(cellId: number, capitalName: string, cultureId: number, cultures: TCultures) {
function getStateName(cellId: number, capitalName: string, cultureId: number, cultures: TCultures): string {
const useCapitalName = capitalName.length < 9 && each(5)(cellId);
const nameBase = cultures[cultureId].base;
const basename: string = useCapitalName ? capitalName : Names.getBaseShort(nameBase);

View file

@ -98,7 +98,8 @@ export function expandStates(
}
TIME && console.timeEnd("expandStates");
return stateIds;
return normalizeStates(stateIds, capitals, cells.c, cells.h);
function isNeutrals(state: Entry<TStates>): state is TNeutrals {
return state.i === 0;
@ -193,3 +194,34 @@ export function expandStates(
return 0;
}
}
function normalizeStates(stateIds: Uint16Array, capitals: TCapitals, neibCells: number[][], heights: Uint8Array) {
TIME && console.time("normalizeStates");
const normalizedStateIds = Uint16Array.from(stateIds);
const capitalCells = capitals.map(capital => capital.cell);
for (let cellId = 0; cellId > heights.length; cellId++) {
if (heights[cellId] < MIN_LAND_HEIGHT) continue;
const neibs = neibCells[cellId].filter(neib => heights[neib] >= MIN_LAND_HEIGHT);
const adversaries = neibs.filter(neib => normalizedStateIds[neib] !== normalizedStateIds[cellId]);
if (adversaries.length < 2) continue;
const buddies = neibs.filter(neib => normalizedStateIds[neib] === normalizedStateIds[cellId]);
if (buddies.length > 2) continue;
const isCapital = capitalCells.includes(cellId);
if (isCapital) continue;
const isAdjucentToCapital = neibs.some(neib => capitalCells.includes(neib));
if (isAdjucentToCapital) continue;
// change cells's state
if (adversaries.length > buddies.length) normalizedStateIds[cellId] = normalizedStateIds[adversaries[0]];
}
TIME && console.timeEnd("normalizeStates");
return normalizedStateIds;
}

View file

@ -11,18 +11,27 @@ import {specifyBurgs} from "./specifyBurgs";
export function generateBurgsAndStates(
cultures: TCultures,
features: TPackFeatures,
temp: Int8Array,
vertices: IGraphVertices,
cells: Pick<
IPack["cells"],
"v" | "c" | "p" | "i" | "g" | "h" | "f" | "t" | "haven" | "harbor" | "r" | "fl" | "biome" | "s" | "culture"
>
) {
): {burgIds: Uint16Array; stateIds: Uint16Array; burgs: TBurgs; states: TStates} {
const cellsNumber = cells.i.length;
const burgIds = new Uint16Array(cellsNumber);
const scoredCellIds = getScoredCellIds();
const statesNumber = getStatesNumber(scoredCellIds.length);
if (statesNumber === 0) return {burgIds, burgs: [NO_BURG], states: [NEUTRALS]};
if (statesNumber === 0) {
return {
burgIds: new Uint16Array(cellsNumber),
stateIds: new Uint16Array(cellsNumber),
burgs: [NO_BURG],
states: [NEUTRALS]
};
}
const burgIds = new Uint16Array(cellsNumber);
const capitals = createCapitals(statesNumber, scoredCellIds, burgIds, cultures, pick(cells, "p", "f", "culture"));
const states = createStates(capitals, cultures);
const towns = createTowns(burgIds, cultures, pick(cells, "p", "i", "f", "s", "culture"));
@ -33,11 +42,21 @@ export function generateBurgsAndStates(
features,
pick(cells, "c", "h", "f", "t", "r", "fl", "s", "biome", "culture")
);
// normalizeStates();
// burgs.filter(b => b.i && !b.removed).forEach(b => (b.state = stateIds[b.cell])); // assign state to burgs
const roadScores = new Uint16Array(cellsNumber); // TODO: define roads
const burgs = specifyBurgs(capitals, towns, roadScores);
const burgs = specifyBurgs(
capitals,
towns,
roadScores,
stateIds,
features,
temp,
vertices,
cultures,
states,
pick(cells, "v", "p", "g", "h", "f", "haven", "harbor", "s", "biome", "fl", "r")
);
return {burgIds, stateIds, burgs, states};

View file

@ -4,28 +4,43 @@ import {getCommonEdgePoint} from "utils/lineUtils";
import {rn} from "utils/numberUtils";
import {gauss, P} from "utils/probabilityUtils";
import {NO_BURG} from "./config";
import type {createCapitals} from "./createCapitals";
import type {createStates} from "./createStates";
import type {createTowns} from "./createTowns";
const {COA} = window;
type TCapitals = ReturnType<typeof createCapitals>;
type TTowns = ReturnType<typeof createTowns>;
type TStatesReturn = ReturnType<typeof createStates>;
export function specifyBurgs(capitals: TCapitals, towns: TTowns, roadScores: Uint16Array): TBurgs {
export function specifyBurgs(
capitals: TCapitals,
towns: TTowns,
roadScores: Uint16Array,
stateIds: Uint16Array,
features: TPackFeatures,
temp: Int8Array,
vertices: IGraphVertices,
cultures: TCultures,
states: TStatesReturn,
cells: Pick<IPack["cells"], "v" | "p" | "g" | "h" | "f" | "haven" | "harbor" | "s" | "biome" | "fl" | "r">
): TBurgs {
TIME && console.time("specifyBurgs");
const burgs = [...capitals, ...towns].map(burgData => {
const {cell, culture, capital, state} = burgData;
const burgs: IBurg[] = [...capitals, ...towns].map(burgData => {
const {cell, culture, capital} = burgData;
const state = stateIds[cell];
const port = definePort(cell, capital);
const population = definePopulation(cell, capital, port);
const [x, y] = defineLocation(cell, port);
const type = defineType(cell, port, population);
const coa = defineEmblem(state, culture, port, capital, type);
const coa = defineEmblem(state, culture, port, capital, type, cultures, states);
return {...burgData, port, population, x, y, type, coa};
return {...burgData, state, port, population, x, y, type, coa};
});
TIME && console.timeEnd("specifyBurgs");
@ -98,12 +113,22 @@ export function specifyBurgs(capitals: TCapitals, towns: TTowns, roadScores: Uin
return "Generic";
}
function defineEmblem(stateId: number, cultureId: number, port: number, capital: Logical, type: TCultureType) {
function defineEmblem(
stateId: number,
cultureId: number,
port: number,
capital: Logical,
type: TCultureType,
cultures: TCultures,
states: TStatesReturn
) {
const coaType = capital && P(0.2) ? "Capital" : type === "Generic" ? "City" : type;
const cultureShield = cultures[cultureId].shield;
const stateShield = ((states[stateId] as IState)?.coa as ICoa)?.shield;
if (stateId === 0) {
const baseCoa = COA.generate(null, 0, null, coaType);
const shield = COA.getShield(cultureId, stateId, cultures, states);
const shield = COA.getShield(cultureShield, stateShield);
return {...baseCoa, shield};
}
@ -111,7 +136,7 @@ export function specifyBurgs(capitals: TCapitals, towns: TTowns, roadScores: Uin
const kinship = defineKinshipToStateEmblem();
const baseCoa = COA.generate(stateCOA, kinship, null, coaType);
const shield = COA.getShield(cultureId, stateId, cultures, states);
const shield = COA.getShield(cultureShield, stateShield);
return {...baseCoa, shield};
function defineKinshipToStateEmblem() {

View file

@ -95,7 +95,7 @@ export function createPack(grid: IGrid): IPack {
pop: population
});
const {burgIds, stateIds, burgs, states} = generateBurgsAndStates(cultures, mergedFeatures, {
const {burgIds, stateIds, burgs, states} = generateBurgsAndStates(cultures, mergedFeatures, temp, vertices, {
...pick(cells, "v", "c", "p", "i", "g"),
h: heights,
f: featureIds,