civ/script.js
2026-05-12 23:31:17 +09:00

3335 lines
122 KiB
JavaScript

const Terrain = Object.freeze({
PLAINS: 0,
FOREST: 1,
MOUNTAIN: 2,
WATER: 3,
DESERT: 4,
FERTILE: 5,
MINERAL: 6
});
const terrainInfo = [
{ name: "Plains", color: [82, 102, 74], move: 1.0, fertility: 0.55, mineral: 0.05, regen: 0.025 },
{ name: "Forest", color: [58, 91, 68], move: 1.3, fertility: 0.62, mineral: 0.08, regen: 0.038 },
{ name: "Mountains", color: [104, 105, 101], move: 2.6, fertility: 0.18, mineral: 0.78, regen: 0.012 },
{ name: "Water", color: [52, 78, 103], move: 7.0, fertility: 0.04, mineral: 0.02, regen: 0.004 },
{ name: "Desert", color: [139, 123, 86], move: 1.55, fertility: 0.16, mineral: 0.16, regen: 0.014 },
{ name: "Fertile", color: [94, 122, 78], move: 0.9, fertility: 0.92, mineral: 0.04, regen: 0.055 },
{ name: "Mineral", color: [118, 101, 94], move: 1.5, fertility: 0.38, mineral: 0.95, regen: 0.018 }
];
const MONTHS_PER_YEAR = 12;
const SAVE_KEY = "civil-emergence-save";
const SAVE_VERSION = 3;
const MAX_SAVE_BYTES = 4_500_000;
const SimConfig = Object.freeze({
population: Object.freeze({
maxAgentsFloor: 8000,
maxAgentsScale: 1.15,
maxOffspringPerStep: 360,
carryingCapacityBase: 2.4,
carryingCapacityFertility: 7.8,
carryingCapacityMineral: 1.8,
carryingCapacityFarmland: 5.5,
pressurePenaltyBase: 0.2,
pressureReproductionPenalty: 0.16
}),
render: Object.freeze({
graphThrottleMs: 700,
statsThrottleMs: 350
}),
save: Object.freeze({
key: SAVE_KEY,
version: SAVE_VERSION,
maxBytes: MAX_SAVE_BYTES
}),
technology: Object.freeze({
cityDiffusion: 0.0022,
tradeDiffusion: 0.0032,
cityInnovation: 0.00045
}),
polity: Object.freeze({
minimumPerCapitaFood: 0.06,
logisticsDistance: 34
}),
culture: Object.freeze({
spreadRadius: 3,
cityWeight: 0.035,
routeWeight: 1.35,
minimumInfluence: 0.18
})
});
function years(value) {
return value * MONTHS_PER_YEAR;
}
const els = {
canvas: document.getElementById("world"),
sim: document.querySelector(".sim"),
toggleRun: document.getElementById("toggleRun"),
stepOnce: document.getElementById("stepOnce"),
resetWorld: document.getElementById("resetWorld"),
saveWorld: document.getElementById("saveWorld"),
loadWorld: document.getElementById("loadWorld"),
clearSave: document.getElementById("clearSave"),
speed: document.getElementById("speed"),
agentCount: document.getElementById("agentCount"),
worldSize: document.getElementById("worldSize"),
viewMode: document.getElementById("viewMode"),
year: document.getElementById("year"),
activeGroups: document.getElementById("activeGroups"),
urbanPopulation: document.getElementById("urbanPopulation"),
ethnicities: document.getElementById("ethnicities"),
cities: document.getElementById("cities"),
polities: document.getElementById("polities"),
routes: document.getElementById("routes"),
farmingKnowledge: document.getElementById("farmingKnowledge"),
metallurgyKnowledge: document.getElementById("metallurgyKnowledge"),
deaths: document.getElementById("deaths"),
frameCost: document.getElementById("frameCost"),
ethnicityList: document.getElementById("ethnicityList"),
legend: document.getElementById("legend"),
tooltip: document.getElementById("tooltip"),
stateGraph: document.getElementById("stateGraph"),
historyRange: document.getElementById("historyRange")
};
const ctx = els.canvas.getContext("2d", { alpha: false });
let sim;
let running = true;
let frame = 0;
let lastStatsAt = 0;
let lastGraphRenderAt = 0;
let hoverState = null;
let renderImage = null;
let renderImageSize = 0;
class Rng {
constructor(seed) {
this.seed = seed >>> 0;
}
next() {
this.seed = (1664525 * this.seed + 1013904223) >>> 0;
return this.seed / 4294967296;
}
range(min, max) {
return min + (max - min) * this.next();
}
int(max) {
return Math.floor(this.next() * max);
}
}
class World {
constructor(size, rng, generate = true) {
this.size = size;
this.count = size * size;
this.rng = rng;
this.terrain = new Uint8Array(this.count);
this.resource = new Float32Array(this.count);
this.regen = new Float32Array(this.count);
this.move = new Float32Array(this.count);
this.fertility = new Float32Array(this.count);
this.mineral = new Float32Array(this.count);
this.temperature = new Float32Array(this.count);
this.humidity = new Float32Array(this.count);
this.pheromone = new Float32Array(this.count);
this.tradeRoute = new Uint8Array(this.count);
this.farmland = new Float32Array(this.count);
this.cityPull = new Float32Array(this.count);
this.city = new Int32Array(this.count);
this.pressure = new Float32Array(this.count);
this.dominantEthnicity = new Int32Array(this.count);
this.cultureDiversity = new Float32Array(this.count);
this.city.fill(-1);
this.dominantEthnicity.fill(-1);
if (generate) this.generate();
}
static blank(size, rng) {
return new World(size, rng, false);
}
idx(x, y) {
return y * this.size + x;
}
generate() {
const s = this.size;
const centers = Array.from({ length: 18 }, () => ({
x: this.rng.range(0, s),
y: this.rng.range(0, s),
kind: this.rng.next()
}));
const height = new Float32Array(this.count);
const moistureMap = new Float32Array(this.count);
const mineralMap = new Float32Array(this.count);
for (let y = 0; y < s; y++) {
for (let x = 0; x < s; x++) {
const i = this.idx(x, y);
const nx = x / s - 0.5;
const ny = y / s - 0.5;
const latitude = Math.abs(ny) * 0.18;
let elevation = 0.48 - Math.hypot(nx, ny) * 0.62 + this.smoothNoise(x, y, 58) * 0.42 + this.smoothNoise(x + 900, y - 300, 31) * 0.12;
let moisture = this.smoothNoise(x + 500, y - 330, 54) * 0.68 + this.smoothNoise(x, y, 24) * 0.24 - latitude;
let minerals = this.smoothNoise(x - 290, y + 120, 20);
for (const c of centers) {
const d = Math.hypot(x - c.x, y - c.y) / s;
if (c.kind < 0.3) elevation += Math.max(0, 0.24 - d) * 0.85;
if (c.kind > 0.72) moisture += Math.max(0, 0.22 - d) * 1.0;
}
height[i] = elevation;
moistureMap[i] = moisture;
mineralMap[i] = minerals;
}
}
this.smoothField(height, 2);
this.smoothField(moistureMap, 1);
const seaLevel = this.percentile(height, 0.34);
for (let y = 0; y < s; y++) {
for (let x = 0; x < s; x++) {
const i = this.idx(x, y);
const latitude = Math.abs((y / (s - 1)) * 2 - 1);
const altitudeCooling = clamp((height[i] - seaLevel) * 0.25, 0, 0.18);
this.temperature[i] = clamp(1 - latitude * 0.92 - altitudeCooling + this.smoothNoise(x + 1700, y - 80, 70) * 0.08, 0, 1);
this.terrain[i] = height[i] < seaLevel ? Terrain.WATER : Terrain.PLAINS;
}
}
this.computeHumidityFromWater(moistureMap);
for (let i = 0; i < this.count; i++) {
const elevation = height[i];
const minerals = mineralMap[i];
const temp = this.temperature[i];
const humid = this.humidity[i];
let t = this.terrain[i];
if (t !== Terrain.WATER) {
if (elevation > seaLevel + 0.42) t = minerals > 0.56 ? Terrain.MINERAL : Terrain.MOUNTAIN;
else if (humid < 0.15 && temp > 0.58) t = Terrain.DESERT;
else if (humid > 0.72 && temp > 0.25 && temp < 0.88) t = Terrain.FERTILE;
else if (humid > 0.5 && temp > 0.18) t = Terrain.FOREST;
else t = Terrain.PLAINS;
}
const info = terrainInfo[t];
this.terrain[i] = t;
this.fertility[i] = clamp(info.fertility + humid * 0.16 - Math.abs(temp - 0.58) * 0.08 + this.rng.range(-0.03, 0.03), 0, 1);
this.mineral[i] = clamp(info.mineral + minerals * 0.16, 0, 1);
this.regen[i] = info.regen * (0.7 + this.fertility[i]);
this.move[i] = info.move;
this.resource[i] = this.terrain[i] === Terrain.WATER ? 0 : this.rng.range(4, 18) * (0.5 + this.fertility[i] + this.mineral[i] * 0.35);
}
this.smoothTerrainTypes(2);
this.ensureDesertPatches();
this.enrichWaterMargins();
}
enrichWaterMargins() {
const s = this.size;
const nextFertility = new Float32Array(this.fertility);
for (let y = 0; y < s; y++) {
for (let x = 0; x < s; x++) {
const i = this.idx(x, y);
if (this.terrain[i] === Terrain.WATER) continue;
let waterScore = 0;
for (let dy = -3; dy <= 3; dy++) {
for (let dx = -3; dx <= 3; dx++) {
const d = Math.abs(dx) + Math.abs(dy);
if (!d || d > 3) continue;
const tx = x + dx;
const ty = y + dy;
if (tx < 0 || ty < 0 || tx >= s || ty >= s) continue;
if (this.terrain[this.idx(tx, ty)] === Terrain.WATER) waterScore += (4 - d) / 4;
}
}
if (waterScore <= 0) continue;
const boost = Math.min(0.32, waterScore * 0.08);
nextFertility[i] = clamp(this.fertility[i] + boost, 0, 1);
if (this.terrain[i] === Terrain.DESERT && waterScore > 1.2) this.terrain[i] = Terrain.PLAINS;
if ((this.terrain[i] === Terrain.PLAINS || this.terrain[i] === Terrain.FOREST) && nextFertility[i] > 0.82) this.terrain[i] = Terrain.FERTILE;
}
}
for (let i = 0; i < this.count; i++) {
const info = terrainInfo[this.terrain[i]];
this.fertility[i] = nextFertility[i];
this.regen[i] = info.regen * (0.7 + this.fertility[i]);
this.move[i] = info.move;
if (this.terrain[i] === Terrain.WATER) {
this.resource[i] = 0;
} else {
this.resource[i] = Math.max(this.resource[i], this.rng.range(5, 20) * (0.45 + this.fertility[i] + this.mineral[i] * 0.25));
}
}
}
smoothNoise(x, y, scale) {
const x0 = Math.floor(x / scale);
const y0 = Math.floor(y / scale);
const fx = smoothstep((x / scale) - x0);
const fy = smoothstep((y / scale) - y0);
const a = this.gridNoise(x0, y0);
const b = this.gridNoise(x0 + 1, y0);
const c = this.gridNoise(x0, y0 + 1);
const d = this.gridNoise(x0 + 1, y0 + 1);
return lerp(lerp(a, b, fx), lerp(c, d, fx), fy);
}
gridNoise(x, y) {
const v = Math.sin(x * 127.1 + y * 311.7 + this.rng.seed * 0.000001) * 43758.5453123;
return v - Math.floor(v);
}
computeHumidityFromWater(moistureMap) {
const s = this.size;
const radius = 10;
for (let y = 0; y < s; y++) {
for (let x = 0; x < s; x++) {
const i = this.idx(x, y);
if (this.terrain[i] === Terrain.WATER) {
this.humidity[i] = 1;
continue;
}
let best = 0;
for (let dy = -radius; dy <= radius; dy++) {
for (let dx = -radius; dx <= radius; dx++) {
const d = Math.abs(dx) + Math.abs(dy);
if (!d || d > radius) continue;
const tx = x + dx;
const ty = y + dy;
if (tx < 0 || ty < 0 || tx >= s || ty >= s) continue;
if (this.terrain[this.idx(tx, ty)] === Terrain.WATER) best = Math.max(best, 1 - d / (radius + 1));
}
}
this.humidity[i] = clamp(best * 0.78 + moistureMap[i] * 0.22, 0, 1);
}
}
this.smoothField(this.humidity, 1);
}
smoothTerrainTypes(passes) {
const s = this.size;
let source = new Uint8Array(this.terrain);
let target = new Uint8Array(this.terrain.length);
for (let pass = 0; pass < passes; pass++) {
for (let y = 0; y < s; y++) {
for (let x = 0; x < s; x++) {
const counts = new Uint8Array(terrainInfo.length);
for (let dy = -1; dy <= 1; dy++) {
for (let dx = -1; dx <= 1; dx++) {
const tx = x + dx;
const ty = y + dy;
if (tx < 0 || ty < 0 || tx >= s || ty >= s) continue;
counts[source[this.idx(tx, ty)]]++;
}
}
let bestTerrain = source[this.idx(x, y)];
let bestCount = counts[bestTerrain];
for (let t = 0; t < counts.length; t++) {
if (counts[t] > bestCount) {
bestTerrain = t;
bestCount = counts[t];
}
}
target[this.idx(x, y)] = bestCount >= 4 ? bestTerrain : source[this.idx(x, y)];
}
}
const swap = source;
source = target;
target = swap;
}
this.terrain.set(source);
}
ensureDesertPatches() {
const candidates = [];
let desertCount = 0;
for (let i = 0; i < this.count; i++) {
if (this.terrain[i] === Terrain.DESERT) desertCount++;
if (this.terrain[i] !== Terrain.WATER && this.temperature[i] > 0.55 && this.humidity[i] < 0.28) {
candidates.push(i);
}
}
const target = Math.max(12, Math.floor(this.count * 0.012));
if (desertCount >= target || !candidates.length) return;
candidates.sort((a, b) => {
const dryA = this.temperature[a] * (1 - this.humidity[a]);
const dryB = this.temperature[b] * (1 - this.humidity[b]);
return dryB - dryA;
});
const needed = Math.min(target - desertCount, candidates.length);
for (let n = 0; n < needed; n++) {
const center = candidates[n];
const cx = center % this.size;
const cy = Math.floor(center / this.size);
for (let dy = -2; dy <= 2; dy++) {
for (let dx = -2; dx <= 2; dx++) {
if (Math.abs(dx) + Math.abs(dy) > 2) continue;
const x = cx + dx;
const y = cy + dy;
if (x < 0 || y < 0 || x >= this.size || y >= this.size) continue;
const i = this.idx(x, y);
if (this.terrain[i] !== Terrain.WATER && this.temperature[i] > 0.5 && this.humidity[i] < 0.34) {
this.terrain[i] = Terrain.DESERT;
}
}
}
}
}
smoothField(field, passes) {
const s = this.size;
let source = field;
let target = new Float32Array(field.length);
for (let pass = 0; pass < passes; pass++) {
for (let y = 0; y < s; y++) {
for (let x = 0; x < s; x++) {
let sum = 0;
let count = 0;
for (let dy = -1; dy <= 1; dy++) {
for (let dx = -1; dx <= 1; dx++) {
const tx = x + dx;
const ty = y + dy;
if (tx < 0 || ty < 0 || tx >= s || ty >= s) continue;
sum += source[this.idx(tx, ty)];
count++;
}
}
target[this.idx(x, y)] = sum / count;
}
}
const nextSource = target;
target = source === field ? new Float32Array(field.length) : field;
source = nextSource;
}
if (source !== field) field.set(source);
}
percentile(field, ratio) {
const values = Array.from(field).sort((a, b) => a - b);
return values[Math.floor(clamp(ratio, 0, 1) * (values.length - 1))];
}
}
class Simulation {
constructor(size, initialAgents, options = {}) {
this.rng = new Rng(Date.now());
this.world = options.blankWorld ? World.blank(size, this.rng) : new World(size, this.rng);
this.agents = [];
this.ethnicities = new Map();
this.cities = [];
this.polities = [];
this.polityHistory = new Map();
this.deadPolityHistories = [];
this.tradeLinks = [];
this.nextEthnicity = 1;
this.nextCity = 1;
this.nextPolity = 1;
this.year = 0;
this.deaths = 0;
this.maxAgents = Math.max(
Math.floor(initialAgents * SimConfig.population.maxAgentsScale),
SimConfig.population.maxAgentsFloor
);
this.tileEthnicities = new Map();
this.tileAgents = new Map();
if (!options.skipSpawn) {
this.spawnInitialAgents(initialAgents);
this.rebuildOccupancy();
this.updateEthnicStats();
}
}
spawnInitialAgents(count) {
const founders = Math.max(5, Math.min(16, Math.round(count / 180)));
const desertFounders = Math.max(2, Math.floor(founders * 0.25));
for (let e = 0; e < founders; e++) {
const desertFounder = e < desertFounders;
const origin = this.findHabitableTile(desertFounder ? Terrain.DESERT : null);
const originTile = this.world.idx(origin.x, origin.y);
const id = this.createEthnicity(0, {
temperature: this.world.temperature[originTile],
humidity: this.world.humidity[originTile]
});
const baseTraits = desertFounder ? this.desertFounderTraits() : this.randomTraits();
for (let n = 0; n < Math.floor(count / founders); n++) {
const spawn = this.findNearbySpawn(origin.x, origin.y, desertFounder ? Terrain.DESERT : null);
this.agents.push(this.makeAgent(
spawn.x,
spawn.y,
id,
mutateTraits(baseTraits, this.rng, 0.07),
desertFounder ? this.rng.range(18, 32) : this.rng.range(6, 15)
));
}
}
}
createEthnicity(parent, climate = null) {
const id = this.nextEthnicity++;
this.ethnicities.set(id, {
id,
parent,
born: this.year,
population: 0,
diversity: 0,
climateTemp: climate?.temperature ?? 0.55,
climateHumidity: climate?.humidity ?? 0.45,
color: hslToRgb((id * 0.61803398875) % 1, 0.55, 0.56),
centroidX: 0,
centroidY: 0
});
return id;
}
randomTraits() {
return {
mobility: this.rng.range(0.18, 0.85),
resourceAttraction: this.rng.range(0.55, 1),
assimilation: this.rng.range(0.03, 0.34),
ethnocentrism: this.rng.range(0.12, 0.78),
reproductionThreshold: this.rng.range(19, 34),
sedentary: this.rng.range(0.08, 0.82)
};
}
desertFounderTraits() {
const traits = this.randomTraits();
traits.mobility = this.rng.range(0.48, 0.9);
traits.resourceAttraction = this.rng.range(0.82, 1.12);
traits.reproductionThreshold = this.rng.range(24, 38);
traits.sedentary = this.rng.range(0.18, 0.58);
return traits;
}
makeAgent(x, y, ethnicity, traits, resources) {
return {
x,
y,
resources,
ethnicity,
traits,
alive: true,
foreignContact: 0,
contactEthnicity: ethnicity,
settled: 0,
movedThisStep: false,
tech: {
farming: 0,
metallurgy: 0
},
farmingWork: 0,
lastFarmTile: -1
};
}
findNearbySpawn(originX, originY, preferredTerrain = null) {
const w = this.world;
let best = { x: originX, y: originY };
let bestScore = -Infinity;
for (let tries = 0; tries < 24; tries++) {
const x = clamp(originX + this.rng.int(9) - 4, 0, w.size - 1);
const y = clamp(originY + this.rng.int(9) - 4, 0, w.size - 1);
const i = w.idx(x, y);
if (w.terrain[i] === Terrain.WATER) continue;
const preferred = preferredTerrain !== null && w.terrain[i] === preferredTerrain ? 1.5 : 0;
const score = preferred + w.resource[i] * 0.04 + w.fertility[i] * 0.8 + w.mineral[i] * 0.25 - w.move[i] * 0.16;
if (score > bestScore) {
bestScore = score;
best = { x, y };
}
}
return best;
}
findHabitableTile(preferredTerrain = null) {
if (preferredTerrain !== null) {
const exact = [];
let best = null;
let bestScore = -Infinity;
for (let i = 0; i < this.world.count; i++) {
const x = i % this.world.size;
const y = Math.floor(i / this.world.size);
if (this.world.terrain[i] === preferredTerrain) {
exact.push({ x, y });
continue;
}
if (preferredTerrain === Terrain.DESERT && this.world.terrain[i] !== Terrain.WATER) {
const score = this.world.temperature[i] * (1 - this.world.humidity[i]);
if (score > bestScore) {
bestScore = score;
best = { x, y };
}
}
}
if (exact.length) return exact[this.rng.int(exact.length)];
if (best) return best;
}
for (let tries = 0; tries < 5000; tries++) {
const x = this.rng.int(this.world.size);
const y = this.rng.int(this.world.size);
const i = this.world.idx(x, y);
if (this.world.terrain[i] !== Terrain.WATER && this.world.fertility[i] + this.world.mineral[i] > 0.5) {
return { x, y };
}
}
return { x: this.world.size >> 1, y: this.world.size >> 1 };
}
step() {
for (const city of this.cities) city.activeVisitors = 0;
this.rebuildOccupancy();
const offspring = [];
for (const a of this.agents) {
if (!a.alive) continue;
this.moveAgent(a);
}
this.rebuildOccupancy();
for (const a of this.agents) {
if (!a.alive) continue;
this.updateAgentTechnology(a);
this.gatherConsumeReproduce(a, offspring);
if (a.alive) this.payTechnologyCostOrForget(a);
this.resolveAssimilation(a);
}
this.agents = this.agents.filter(a => a.alive);
const acceptedOffspring = this.agents.length + offspring.length < this.maxAgents
? offspring
: offspring.slice(0, Math.max(0, this.maxAgents - this.agents.length));
this.agents.push(...acceptedOffspring);
for (const child of acceptedOffspring) this.addAgentToOccupancy(child);
this.updateWorldFields();
if (this.year % years(1) === 0) {
this.updateCities();
this.updateRegionalCultures();
}
if (this.year % years(5) === 0) {
this.updateTradeRoutes();
}
this.updatePolities();
this.updateEthnicStats();
if (this.year % years(45) === 0) {
this.splitDivergentEthnicities();
this.updateEthnicStats();
}
this.year++;
}
rebuildOccupancy() {
const w = this.world;
w.pressure.fill(0);
this.tileEthnicities.clear();
this.tileAgents.clear();
for (const a of this.agents) {
if (!a.alive) continue;
const i = w.idx(a.x, a.y);
w.pressure[i]++;
let agents = this.tileAgents.get(i);
if (!agents) {
agents = [];
this.tileAgents.set(i, agents);
}
agents.push(a);
let counts = this.tileEthnicities.get(i);
if (!counts) {
counts = new Map();
this.tileEthnicities.set(i, counts);
}
counts.set(a.ethnicity, (counts.get(a.ethnicity) || 0) + 1);
}
for (const tile of this.tileEthnicities.keys()) this.updateCultureTile(tile);
}
updateCultureTile(tile) {
const counts = this.tileEthnicities.get(tile);
if (!counts || !counts.size) {
this.world.cultureDiversity[tile] *= 0.98;
return;
}
let total = 0;
let dominant = -1;
let dominantCount = 0;
for (const [id, count] of counts) {
total += count;
if (count > dominantCount) {
dominant = id;
dominantCount = count;
}
}
this.world.dominantEthnicity[tile] = dominant;
this.world.cultureDiversity[tile] = total > 0 ? 1 - dominantCount / total : 0;
}
updateRegionalCultures() {
const w = this.world;
const nextDominant = new Int32Array(w.dominantEthnicity);
const nextDiversity = new Float32Array(w.cultureDiversity);
const radius = SimConfig.culture.spreadRadius;
const cityById = new Map(this.cities.map(city => [city.id, city]));
for (let y = 0; y < w.size; y++) {
for (let x = 0; x < w.size; x++) {
const tile = w.idx(x, y);
if (w.terrain[tile] === Terrain.WATER) continue;
const influence = new Map();
let total = 0;
for (let dy = -radius; dy <= radius; dy++) {
for (let dx = -radius; dx <= radius; dx++) {
const distance = Math.abs(dx) + Math.abs(dy);
if (distance > radius) continue;
const tx = x + dx;
const ty = y + dy;
if (tx < 0 || ty < 0 || tx >= w.size || ty >= w.size) continue;
const source = w.idx(tx, ty);
const id = w.dominantEthnicity[source];
if (id < 0) continue;
const pressureWeight = Math.sqrt(Math.max(0, w.pressure[source]));
const cityWeight = w.city[source] >= 0
? Math.sqrt(Math.max(1, cityById.get(w.city[source])?.population || 1)) * SimConfig.culture.cityWeight
: 0;
const routeWeight = w.tradeRoute[source] ? SimConfig.culture.routeWeight : 1;
const weight = (pressureWeight + cityWeight) * routeWeight / (1 + distance);
if (weight <= 0) continue;
influence.set(id, (influence.get(id) || 0) + weight);
total += weight;
}
}
let bestId = nextDominant[tile];
let bestWeight = 0;
for (const [id, weight] of influence) {
if (weight > bestWeight) {
bestId = id;
bestWeight = weight;
}
}
if (bestWeight >= SimConfig.culture.minimumInfluence) {
nextDominant[tile] = bestId;
nextDiversity[tile] = total > 0 ? clamp(1 - bestWeight / total, 0, 1) : 0;
}
}
}
w.dominantEthnicity.set(nextDominant);
w.cultureDiversity.set(nextDiversity);
}
addAgentToOccupancy(a) {
if (!a.alive) return;
const w = this.world;
const i = w.idx(a.x, a.y);
w.pressure[i]++;
let agents = this.tileAgents.get(i);
if (!agents) {
agents = [];
this.tileAgents.set(i, agents);
}
agents.push(a);
let counts = this.tileEthnicities.get(i);
if (!counts) {
counts = new Map();
this.tileEthnicities.set(i, counts);
}
counts.set(a.ethnicity, (counts.get(a.ethnicity) || 0) + 1);
this.updateCultureTile(i);
}
removeAgentFromOccupancy(a) {
const w = this.world;
const i = w.idx(a.x, a.y);
w.pressure[i] = Math.max(0, w.pressure[i] - 1);
const agents = this.tileAgents.get(i);
if (agents) {
const index = agents.indexOf(a);
if (index >= 0) agents.splice(index, 1);
if (!agents.length) this.tileAgents.delete(i);
}
const counts = this.tileEthnicities.get(i);
if (counts) {
const next = (counts.get(a.ethnicity) || 0) - 1;
if (next > 0) counts.set(a.ethnicity, next);
else counts.delete(a.ethnicity);
if (!counts.size) this.tileEthnicities.delete(i);
}
this.updateCultureTile(i);
}
changeAgentEthnicity(a, nextEthnicity) {
if (a.ethnicity === nextEthnicity) return;
const w = this.world;
const i = w.idx(a.x, a.y);
const counts = this.tileEthnicities.get(i);
if (counts) {
const prev = (counts.get(a.ethnicity) || 0) - 1;
if (prev > 0) counts.set(a.ethnicity, prev);
else counts.delete(a.ethnicity);
counts.set(nextEthnicity, (counts.get(nextEthnicity) || 0) + 1);
}
a.ethnicity = nextEthnicity;
this.updateCultureTile(i);
}
moveAgent(a) {
const w = this.world;
const s = w.size;
const current = w.idx(a.x, a.y);
const localPressure = w.pressure[current];
let bestX = a.x;
let bestY = a.y;
let bestScore = -Infinity;
const sedentary = getSedentary(a.traits);
const ethnocentrism = getEthnocentrism(a.traits);
const ethnicClimate = this.ethnicities.get(a.ethnicity);
const waterAdaptation = ethnicClimate?.climateHumidity ?? 0.45;
const localCapacity = this.carryingCapacityAt(current);
const localOverCapacity = Math.max(0, localPressure - localCapacity);
if (localOverCapacity <= 0.5 && localPressure < 7 && this.rng.next() < sedentary * 0.58) {
a.settled++;
a.movedThisStep = false;
return;
}
const radius = a.traits.mobility > 0.62 && sedentary < 0.52 && this.rng.next() < 0.16 ? 2 : 1;
for (let dy = -radius; dy <= radius; dy++) {
for (let dx = -radius; dx <= radius; dx++) {
const x = clamp(a.x + dx, 0, s - 1);
const y = clamp(a.y + dy, 0, s - 1);
const i = w.idx(x, y);
const isWater = w.terrain[i] === Terrain.WATER;
const canSail = waterAdaptation >= 0.82 && a.traits.mobility > 0.45;
const waterAccess = canSail ? 0.82 + (w.tradeRoute[i] ? 0.08 : 0) : waterAdaptation * 0.18 + a.traits.mobility * 0.04 + (w.tradeRoute[i] ? 0.08 : 0);
if (isWater && this.rng.next() > waterAccess) continue;
const ethnicDensity = this.ethnicDensityNear(x, y, a.ethnicity);
const cityPull = w.city[i] >= 0 ? 1.45 : w.cityPull[i];
const routePull = (w.tradeRoute[i] ? 1.35 : clamp(w.pheromone[i] / 8, 0, 1)) * sedentary;
const resourceScore = w.resource[i] * 0.12 + w.fertility[i] * 1.2 + w.mineral[i] * 0.42 + (isWater ? waterAdaptation * 0.35 : 0);
const destinationCapacity = this.carryingCapacityAt(i);
const destinationOverCapacity = Math.max(0, w.pressure[i] - destinationCapacity);
const capacitySpace = clamp((destinationCapacity - w.pressure[i]) / Math.max(1, destinationCapacity), -1, 1);
const crowdPenalty = destinationOverCapacity * (0.42 + a.traits.mobility * 0.85);
const routeBonus = w.tradeRoute[i] ? 1.25 : 0;
const waterPenalty = isWater ? (canSail ? 0.22 : 2.45 - waterAdaptation * 0.9) : 0;
const terrainPenalty = w.move[i] * (0.8 - a.traits.mobility * 0.35) + waterPenalty - routeBonus;
const inertia = dx === 0 && dy === 0 ? sedentary * 3.2 : 0;
const pressurePush = localOverCapacity > 0 ? a.traits.mobility * (1.2 + localOverCapacity * 0.18 - sedentary * 0.45) : 0;
const capacityPull = capacitySpace * (0.72 + a.traits.mobility * 0.45);
const score =
resourceScore * a.traits.resourceAttraction +
w.pheromone[i] * 0.021 +
ethnicDensity.same * ethnocentrism +
cityPull * sedentary +
routePull +
capacityPull +
inertia -
terrainPenalty -
crowdPenalty +
pressurePush -
ethnicDensity.foreign * ethnocentrism * 0.72 +
this.rng.range(-0.55, 0.55);
if (score > bestScore) {
bestScore = score;
bestX = x;
bestY = y;
}
}
}
const from = w.idx(a.x, a.y);
a.x = bestX;
a.y = bestY;
const to = w.idx(a.x, a.y);
if (from !== to) {
a.movedThisStep = true;
a.farmingWork = 0;
a.lastFarmTile = to;
const depositScale = w.terrain[to] === Terrain.WATER ? 0.35 : 1;
w.pheromone[from] += (w.tradeRoute[from] ? 0.14 : 0.42) * depositScale;
w.pheromone[to] += (w.tradeRoute[to] ? 0.10 : 0.30) * depositScale;
a.settled = Math.max(0, a.settled - 1);
} else {
a.movedThisStep = false;
a.settled++;
}
}
ensureAgentTech(a) {
a.tech ??= { farming: 0, metallurgy: 0 };
a.tech.farming ??= 0;
a.tech.metallurgy ??= 0;
a.tech.farming = clamp(a.tech.farming, 0, 1);
a.tech.metallurgy = clamp(a.tech.metallurgy, 0, 1);
a.farmingWork ??= 0;
a.lastFarmTile ??= -1;
a.movedThisStep ??= false;
}
farmabilityAt(tile) {
const t = this.world.terrain[tile];
if (t === Terrain.FERTILE) return 1.0;
if (t === Terrain.PLAINS) return 0.75;
if (t === Terrain.FOREST) return 0.55;
if (t === Terrain.DESERT) return 0.25;
return 0.0;
}
carryingCapacityAt(tile) {
const w = this.world;
if (w.terrain[tile] === Terrain.WATER) return 1.2;
return SimConfig.population.carryingCapacityBase +
w.fertility[tile] * SimConfig.population.carryingCapacityFertility +
w.mineral[tile] * SimConfig.population.carryingCapacityMineral +
w.farmland[tile] * SimConfig.population.carryingCapacityFarmland +
(w.city[tile] >= 0 ? 3.5 : 0);
}
updateAgentTechnology(agent, payMaintenance = false) {
if (!agent.alive) return;
this.ensureAgentTech(agent);
this.updateFarmingWork(agent);
this.tryInventTechnology(agent);
this.spreadTechnology(agent);
this.learnTechnologyFromCity(agent);
this.improveTechnologyFromDensity(agent);
if (payMaintenance) this.payTechnologyCostOrForget(agent);
}
updateFarmingWork(agent) {
const w = this.world;
const tile = w.idx(agent.x, agent.y);
if (agent.lastFarmTile !== tile) {
agent.farmingWork = 0;
agent.lastFarmTile = tile;
}
if (agent.movedThisStep) return;
const farming = agent.tech?.farming || 0;
if (farming <= 0.02) return;
if (agent.settled > 0) {
agent.farmingWork = clamp((agent.farmingWork || 0) + farming * 0.015, 0, 1);
agent.tech.farming = clamp(agent.tech.farming + farming * this.farmabilityAt(tile) * 0.00018, 0, 1);
}
}
tryInventTechnology(agent) {
const w = this.world;
const tile = w.idx(agent.x, agent.y);
const settledFactor = clamp((agent.settled || 0) / 12, 0, 1);
if ((agent.tech?.farming || 0) <= 0.02) {
const farmingBase = 0.00016;
const farmability = this.farmabilityAt(tile);
const sedentary = getSedentary(agent.traits);
const farmingChance =
farmingBase *
(0.25 + farmability) *
(0.4 + sedentary) *
(0.3 + settledFactor);
if (this.rng.next() < farmingChance) this.grantTechnologyAround(agent, "farming", 0.32);
}
if ((agent.tech?.metallurgy || 0) <= 0.02) {
const metallurgyBase = 0.00011;
const mineral = clamp(w.mineral[tile], 0, 1);
const mineralTerrainBonus =
w.terrain[tile] === Terrain.MINERAL || w.terrain[tile] === Terrain.MOUNTAIN
? 1.8
: 1.0;
const metallurgyChance =
metallurgyBase *
(0.2 + mineral) *
mineralTerrainBonus *
(0.5 + settledFactor);
if (this.rng.next() < metallurgyChance) this.grantTechnologyAround(agent, "metallurgy", 0.28);
}
}
grantTechnologyAround(agent, techName, amount) {
this.ensureAgentTech(agent);
this.forEachLocalAgentNear(agent.x, agent.y, 2, other => {
if (!other.alive) return true;
this.ensureAgentTech(other);
const distance = Math.abs(agent.x - other.x) + Math.abs(agent.y - other.y);
if (distance > 2) return true;
const gain = amount * (other === agent ? 1 : 0.55);
other.tech[techName] = clamp(Math.max(other.tech[techName] || 0, gain), 0, 1);
return true;
});
}
payTechnologyCostOrForget(agent) {
if (!agent.alive) return;
this.ensureAgentTech(agent);
const farming = agent.tech.farming || 0;
const metallurgy = agent.tech.metallurgy || 0;
const cost = farming * 0.004 + metallurgy * 0.010;
if (cost <= 0) return;
if (agent.resources >= cost) {
agent.resources -= cost;
return;
}
const shortage = cost - Math.max(0, agent.resources);
agent.resources = Math.max(0, agent.resources - cost);
const decay = clamp(0.006 + shortage * 0.025, 0.006, 0.06);
agent.tech.farming = Math.max(0, farming - decay);
agent.tech.metallurgy = Math.max(0, metallurgy - decay * 1.15);
if (agent.tech.farming < 0.02) agent.tech.farming = 0;
if (agent.tech.metallurgy < 0.02) agent.tech.metallurgy = 0;
if (agent.tech.farming <= 0) agent.farmingWork = 0;
}
spreadTechnology(agent) {
const w = this.world;
const tile = w.idx(agent.x, agent.y);
const assimilation = agent.traits.assimilation || 0;
const ethnocentrism = agent.traits.ethnocentrism || 0;
let checked = 0;
this.forEachLocalAgentNear(agent.x, agent.y, 1, other => {
if (other === agent || !other.alive) return true;
if (Math.abs(agent.x - other.x) + Math.abs(agent.y - other.y) > 1) return true;
if (++checked > 12) return false;
this.ensureAgentTech(other);
const sameEthnicity = agent.ethnicity === other.ethnicity;
let chance =
0.0035 +
assimilation * 0.006 -
ethnocentrism * 0.0012;
if (sameEthnicity) chance += 0.004;
if (w.tradeRoute[tile]) chance += 0.006;
if (w.city[tile] >= 0) chance += 0.003;
chance = clamp(chance, 0.0008, 0.028);
this.learnTechnologyFrom(agent, other, "farming", chance);
this.learnTechnologyFrom(agent, other, "metallurgy", chance);
return true;
});
}
learnTechnologyFrom(agent, other, techName, chance) {
const current = agent.tech[techName] || 0;
const otherLevel = other.tech?.[techName] || 0;
if (otherLevel > current + 0.04 && this.rng.next() < chance) {
agent.tech[techName] = clamp(current + (otherLevel - current) * 0.14, 0, 1);
}
}
learnTechnologyFromCity(agent) {
const tile = this.world.idx(agent.x, agent.y);
const city = this.world.city[tile] >= 0 ? this.getCityById(this.world.city[tile]) : null;
if (!city?.knowledge) return;
const routeBonus = this.world.tradeRoute[tile] ? SimConfig.technology.tradeDiffusion : 0;
const chance = SimConfig.technology.cityDiffusion + routeBonus + clamp(city.population / 1200, 0, 0.006);
if (this.rng.next() < chance) {
agent.tech.farming = clamp(Math.max(agent.tech.farming, city.knowledge.farming * 0.72), 0, 1);
agent.tech.metallurgy = clamp(Math.max(agent.tech.metallurgy, city.knowledge.metallurgy * 0.68), 0, 1);
}
}
improveTechnologyFromDensity(agent) {
let farmingCount = 0;
let metallurgyCount = 0;
let farmingSum = 0;
let metallurgySum = 0;
this.forEachLocalAgentNear(agent.x, agent.y, 2, other => {
if (!other.alive) return true;
this.ensureAgentTech(other);
const distance = Math.abs(agent.x - other.x) + Math.abs(agent.y - other.y);
if (distance > 2) return true;
const farming = other.tech.farming || 0;
const metallurgy = other.tech.metallurgy || 0;
if (farming > 0.1) {
farmingCount++;
farmingSum += farming;
}
if (metallurgy > 0.1) {
metallurgyCount++;
metallurgySum += metallurgy;
}
return true;
});
if (farmingCount >= 2) {
const avgFarming = farmingSum / farmingCount;
agent.tech.farming = clamp(agent.tech.farming + 0.0024 * farmingCount * avgFarming, 0, 1);
}
if (metallurgyCount >= 2) {
const avgMetallurgy = metallurgySum / metallurgyCount;
agent.tech.metallurgy = clamp(agent.tech.metallurgy + 0.0018 * metallurgyCount * avgMetallurgy, 0, 1);
}
}
forEachLocalAgentNear(x, y, radius, callback) {
const w = this.world;
for (let dy = -radius; dy <= radius; dy++) {
for (let dx = -radius; dx <= radius; dx++) {
const tx = x + dx;
const ty = y + dy;
if (tx < 0 || ty < 0 || tx >= w.size || ty >= w.size) continue;
const agents = this.tileAgents.get(w.idx(tx, ty));
if (!agents) continue;
for (const agent of agents) {
if (callback(agent) === false) return;
}
}
}
}
farmingGatherMultiplier(agent, tile) {
const farmingLevel = agent.tech?.farming || 0;
const farmingWork = agent.farmingWork || 0;
const farmability = this.farmabilityAt(tile);
return clamp(1 + farmingLevel * farmingWork * farmability * 2, 1, 3);
}
metallurgyGatherMultiplier(agent, tile) {
const metallurgyLevel = agent.tech?.metallurgy || 0;
if (metallurgyLevel > 0.02) {
agent.tech.metallurgy = clamp(agent.tech.metallurgy + metallurgyLevel * clamp(this.world.mineral[tile], 0, 1) * 0.00012, 0, 1);
}
return 1 + metallurgyLevel * clamp(this.world.mineral[tile], 0, 1);
}
gatherConsumeReproduce(a, offspring) {
const w = this.world;
const i = w.idx(a.x, a.y);
this.ensureAgentTech(a);
const ethnicity = this.ethnicities.get(a.ethnicity);
const climateMismatch = this.climateMismatch(a.ethnicity, i);
const climateFit = clamp(1 - climateMismatch * 1.35, 0.18, 1);
const cityMarket = w.city[i] >= 0 ? 0.28 : 0;
const drylandAdapted = this.isDrylandAdapted(ethnicity);
const desertForage = drylandAdapted && w.terrain[i] === Terrain.DESERT ? 0.55 : 0;
const productivity = 0.45 + w.fertility[i] * 1.35 + w.mineral[i] * 0.45 + w.farmland[i] * 0.72 + cityMarket + desertForage;
const carryingCapacity = this.carryingCapacityAt(i);
const overCapacity = Math.max(0, w.pressure[i] - carryingCapacity);
const pressurePenalty = 1 / (1 + overCapacity * SimConfig.population.pressurePenaltyBase);
const baseGatherAmount = productivity * climateFit * pressurePenalty * this.rng.range(0.45, 1.2);
const gathered = Math.min(
w.resource[i],
baseGatherAmount * this.farmingGatherMultiplier(a, i) * this.metallurgyGatherMultiplier(a, i)
);
w.resource[i] -= gathered;
a.resources += gathered;
const waterAdaptation = ethnicity?.climateHumidity ?? 0.45;
const waterCost = w.terrain[i] === Terrain.WATER ? (waterAdaptation >= 0.82 ? 0.1 : 0.4 - waterAdaptation * 0.12) : 0;
const climateCost = Math.max(0, climateMismatch - 0.22) * 2.4;
const drylandUpkeep = drylandAdapted && w.terrain[i] === Terrain.DESERT ? 0.72 : 1;
const sedentary = getSedentary(a.traits);
const mobileOverhead = (1 - sedentary) * 0.18 + a.traits.mobility * 0.08;
a.resources -= ((0.72 + w.move[i] * 0.06 + waterCost + climateCost) * drylandUpkeep) + mobileOverhead;
if (a.resources <= 0) {
this.removeAgentFromOccupancy(a);
a.alive = false;
this.deaths++;
return;
}
const settlementBonus = sedentary * (w.farmland[i] * 0.18 + cityMarket * 0.12);
const mobilityPenalty = (1 - sedentary) * 0.45;
const capacityPenalty = overCapacity * SimConfig.population.pressureReproductionPenalty;
const reproductionThreshold = a.traits.reproductionThreshold * clamp(1 + mobilityPenalty + capacityPenalty - settlementBonus, 0.82, 2.35);
if (a.resources > reproductionThreshold && offspring.length < SimConfig.population.maxOffspringPerStep) {
const childShare = 0.36 + sedentary * 0.08;
const childResources = a.resources * childShare;
a.resources -= childResources;
const childTraits = mutateTraits(a.traits, this.rng, 0.035);
const child = this.makeAgent(a.x, a.y, a.ethnicity, childTraits, childResources);
child.tech.farming = clamp((a.tech?.farming || 0) * this.rng.range(0.75, 0.95), 0, 1);
child.tech.metallurgy = clamp((a.tech?.metallurgy || 0) * this.rng.range(0.70, 0.92), 0, 1);
offspring.push(child);
}
}
climateMismatch(ethnicityId, tile) {
const ethnicity = this.ethnicities.get(ethnicityId);
if (!ethnicity) return 0;
const tempDiff = Math.abs(this.world.temperature[tile] - ethnicity.climateTemp);
const humidDiff = Math.abs(this.world.humidity[tile] - ethnicity.climateHumidity);
return tempDiff * 0.58 + humidDiff * 0.42;
}
isDrylandAdapted(ethnicity) {
return !!ethnicity && ethnicity.climateTemp > 0.5 && ethnicity.climateHumidity < 0.38;
}
ethnicDensityNear(x, y, ethnicity) {
let same = 0;
let foreign = 0;
const w = this.world;
for (let dy = -2; dy <= 2; dy++) {
for (let dx = -2; dx <= 2; dx++) {
if (Math.abs(dx) + Math.abs(dy) > 2) continue;
const tx = x + dx;
const ty = y + dy;
if (tx < 0 || ty < 0 || tx >= w.size || ty >= w.size) continue;
const counts = this.tileEthnicities.get(w.idx(tx, ty));
if (!counts) continue;
for (const [id, value] of counts) {
if (id === ethnicity) same += value;
else foreign += value;
}
if (same > 6 && foreign > 6) return { same: same * 0.22, foreign: foreign * 0.16 };
}
}
return { same: same * 0.22, foreign: foreign * 0.16 };
}
resolveAssimilation(a) {
const dominant = this.dominantEthnicityNear(a.x, a.y, a.ethnicity);
if (!dominant || dominant.id === a.ethnicity) {
a.foreignContact = Math.max(0, a.foreignContact - 1);
return;
}
if (a.contactEthnicity !== dominant.id) {
a.contactEthnicity = dominant.id;
a.foreignContact = 0;
}
a.foreignContact++;
const pressure = dominant.count / Math.max(1, dominant.total);
const chance = a.traits.assimilation * pressure * Math.min(1, a.foreignContact / 30);
if (this.rng.next() < chance * 0.14) {
this.changeAgentEthnicity(a, dominant.id);
a.traits = blendTraits(a.traits, dominant.averageTraits, 0.08 + a.traits.assimilation * 0.22);
a.foreignContact = 0;
}
}
dominantEthnicityNear(x, y, self) {
const counts = new Map();
let total = 0;
const w = this.world;
for (let dy = -3; dy <= 3; dy++) {
for (let dx = -3; dx <= 3; dx++) {
if (Math.abs(dx) + Math.abs(dy) > 3) continue;
const tx = x + dx;
const ty = y + dy;
if (tx < 0 || ty < 0 || tx >= w.size || ty >= w.size) continue;
const tileCounts = this.tileEthnicities.get(w.idx(tx, ty));
if (!tileCounts) continue;
for (const [id, count] of tileCounts) {
total += count;
counts.set(id, (counts.get(id) || 0) + count);
}
}
}
for (const city of this.cities) {
const distance = Math.abs(city.x - x) + Math.abs(city.y - y);
if (distance > 5 || !city.ethnicityComposition?.size) continue;
const influence = clamp((6 - distance) / 6, 0, 1) * clamp(Math.sqrt(city.population) / 8, 0.5, 8);
for (const [id, count] of city.ethnicityComposition) {
const weighted = Math.max(1, Math.round(count * influence * 0.08));
total += weighted;
counts.set(id, (counts.get(id) || 0) + weighted);
}
}
let best = null;
for (const [id, count] of counts) {
if (id !== self && (!best || count > best.count)) best = { id, count, total };
}
if (best) best.averageTraits = this.ethnicities.get(best.id)?.averageTraits || this.randomTraits();
return best;
}
updateWorldFields() {
const w = this.world;
for (let i = 0; i < w.count; i++) {
w.resource[i] = Math.min(58, w.resource[i] + w.regen[i] * (1 + w.farmland[i] * 1.15));
w.pheromone[i] *= 0.996;
}
}
updateCities() {
const w = this.world;
w.city.fill(-1);
w.farmland.fill(0);
w.cityPull.fill(0);
const candidates = new Map();
for (const a of this.agents) {
if (!a.alive) continue;
const local = w.idx(a.x, a.y);
if (a.settled < 3 && w.pressure[local] < 2) continue;
if (w.terrain[local] === Terrain.WATER || w.fertility[local] < 0.24) continue;
const cx = clamp(Math.round(a.x / 4) * 4, 0, w.size - 1);
const cy = clamp(Math.round(a.y / 4) * 4, 0, w.size - 1);
const i = w.idx(cx, cy);
let group = candidates.get(i);
if (!group) {
group = { count: 0, resources: 0, sedentary: 0, farming: 0, metallurgy: 0, techCount: 0, ethnicities: new Map() };
candidates.set(i, group);
}
this.ensureAgentTech(a);
group.count++;
group.resources += Math.max(0, a.resources);
group.sedentary += getSedentary(a.traits);
group.farming += a.tech.farming || 0;
group.metallurgy += a.tech.metallurgy || 0;
group.techCount++;
group.ethnicities.set(a.ethnicity, (group.ethnicities.get(a.ethnicity) || 0) + 1);
}
let foundedThisTick = 0;
const canFoundCities = this.year >= years(80) && this.year % years(10) === 0;
const foundingLimit = canFoundCities ? 1 + Math.floor(this.year / years(600)) : 0;
const candidateEntries = [...candidates].sort((a, b) => {
const sedentaryA = a[1].sedentary / Math.max(1, a[1].count);
const sedentaryB = b[1].sedentary / Math.max(1, b[1].count);
return (b[1].count * (0.7 + sedentaryB) + b[1].resources * 0.02) - (a[1].count * (0.7 + sedentaryA) + a[1].resources * 0.02);
});
for (const [i, group] of candidateEntries) {
if (group.count < 3) continue;
const avgSedentary = group.sedentary / group.count;
let city = this.cities.find(c => Math.abs(c.x - (i % w.size)) + Math.abs(c.y - Math.floor(i / w.size)) < 7);
if (!city && canFoundCities && foundedThisTick < foundingLimit && this.cities.length < 80) {
const foundingChance = clamp((avgSedentary - 0.18) * 2.4 * 3, 0.04, 0.98);
if (avgSedentary < 0.22 || this.rng.next() > foundingChance) continue;
city = this.createCity(i % w.size, Math.floor(i / w.size), group);
this.cities.push(city);
foundedThisTick++;
}
if (city) {
city.activeVisitors += group.count;
city.storedResources += group.resources * 0.08;
city.sedentaryCulture = city.sedentaryCulture * 0.98 + avgSedentary * 0.02;
city.knowledge ??= { farming: 0, metallurgy: 0 };
city.knowledge.farming = Math.max(city.knowledge.farming * 0.998, group.farming / Math.max(1, group.techCount));
city.knowledge.metallurgy = Math.max(city.knowledge.metallurgy * 0.998, group.metallurgy / Math.max(1, group.techCount));
const urbanWeight = clamp((avgSedentary - 0.18) * 1.45, 0.08, 1);
for (const [id, count] of group.ethnicities) {
const urbanCount = this.weightedUrbanContribution(count * 0.2, urbanWeight);
if (urbanCount > 0) city.ethnicityComposition.set(id, (city.ethnicityComposition.get(id) || 0) + urbanCount);
}
city.strength = city.strength * 0.96 + group.count * 0.05;
}
}
this.absorbUrbanPopulation();
this.rebuildOccupancy();
this.processCityEconomies();
this.cities = this.cities.filter(c => {
c.age++;
c.strength *= 0.992;
const foodPerCapita = c.storedResources / Math.max(1, c.population);
if (c.age > 20) {
if (c.activeVisitors < 2 && foodPerCapita < 0.04) c.strength -= 0.035;
else if (c.activeVisitors < 5 && foodPerCapita < 0.02) c.strength -= 0.015;
if (c.population < 25 && foodPerCapita < 0.05) c.strength -= 0.018;
}
if (c.population <= 0 || c.strength <= 0.06) return false;
const radius = c.agriculturalRadius;
for (let dy = -radius; dy <= radius; dy++) {
for (let dx = -radius; dx <= radius; dx++) {
const x = clamp(c.x + dx, 0, w.size - 1);
const y = clamp(c.y + dy, 0, w.size - 1);
const d = Math.abs(dx) + Math.abs(dy);
if (d <= radius) {
const tile = w.idx(x, y);
w.city[tile] = c.id;
if (w.terrain[tile] !== Terrain.WATER) {
w.farmland[tile] = Math.max(w.farmland[tile], (radius - d + 1) / (radius + 1));
w.cityPull[tile] = Math.max(w.cityPull[tile], (radius - d + 1) / (radius + 1));
}
}
}
}
return true;
});
}
createCity(x, y, seedGroup = null) {
const seedPopulation = seedGroup ? Math.max(14, seedGroup.count * 5) : 10;
const seedSedentary = seedGroup ? seedGroup.sedentary / Math.max(1, seedGroup.count) : 0.5;
const composition = new Map();
if (seedGroup) {
const urbanWeight = clamp((seedSedentary - 0.18) * 1.45, 0.08, 1);
for (const [id, count] of seedGroup.ethnicities) {
const urbanCount = this.weightedUrbanContribution(count * 3, urbanWeight);
if (urbanCount > 0) composition.set(id, urbanCount);
}
}
return {
id: this.nextCity++,
x,
y,
population: seedPopulation,
storedResources: 34 + (seedGroup?.resources || 0) * 0.55,
ethnicityComposition: composition,
pheromoneOutput: 0,
agriculturalRadius: 2,
tradeLinks: new Set(),
activeVisitors: 0,
age: 0,
strength: 3,
sedentaryCulture: seedSedentary,
knowledge: { farming: 0, metallurgy: 0 },
supplyStress: 0,
polityId: null,
loyalty: 0.5,
receivedAid: false
};
}
absorbUrbanPopulation() {
if (!this.cities.length) return;
const absorbed = [];
for (const a of this.agents) {
if (!a.alive || a.settled < 5) {
absorbed.push(a);
continue;
}
const city = this.findCityNear(a.x, a.y, 7);
const sedentary = getSedentary(a.traits);
if (!city || this.rng.next() > sedentary * 0.85) {
absorbed.push(a);
continue;
}
const migrants = 1 + Math.floor(Math.min(8, a.resources / 8));
const urbanWeight = clamp((sedentary - 0.14) * 1.5, 0.08, 1);
city.population += migrants;
city.storedResources += Math.max(0, a.resources) * 0.65;
const urbanCount = this.weightedUrbanContribution(migrants, urbanWeight);
if (urbanCount > 0) city.ethnicityComposition.set(a.ethnicity, (city.ethnicityComposition.get(a.ethnicity) || 0) + urbanCount);
city.sedentaryCulture = city.sedentaryCulture * 0.985 + sedentary * 0.015;
city.strength += 0.04;
}
this.agents = absorbed;
}
weightedUrbanContribution(amount, weight) {
const value = amount * weight;
const whole = Math.floor(value);
return whole + (this.rng.next() < value - whole ? 1 : 0);
}
processCityEconomies() {
const w = this.world;
for (const city of this.cities) {
city.agriculturalRadius = clamp(Math.floor(1 + Math.sqrt(city.population) / 4.5), 2, 14);
city.pheromoneOutput = clamp(Math.log2(city.population + 1) * 0.09, 0.15, 1.8);
city.knowledge ??= { farming: 0, metallurgy: 0 };
let harvested = 0;
const radius = city.agriculturalRadius;
for (let dy = -radius; dy <= radius; dy++) {
for (let dx = -radius; dx <= radius; dx++) {
if (Math.abs(dx) + Math.abs(dy) > radius) continue;
const x = clamp(city.x + dx, 0, w.size - 1);
const y = clamp(city.y + dy, 0, w.size - 1);
const i = w.idx(x, y);
if (w.terrain[i] === Terrain.WATER) continue;
const pull = (radius - Math.abs(dx) - Math.abs(dy) + 1) / (radius + 1);
const farmingYield = 1 + city.knowledge.farming * 0.85;
const metallurgyYield = 1 + city.knowledge.metallurgy * w.mineral[i] * 0.35;
const extraction = Math.min(w.resource[i], (0.07 + w.fertility[i] * 0.18 * farmingYield + w.mineral[i] * 0.045 * metallurgyYield) * pull);
w.resource[i] -= extraction;
w.farmland[i] = Math.max(w.farmland[i], pull);
w.pheromone[i] += city.pheromoneOutput * pull * 0.09;
harvested += extraction;
}
}
city.storedResources += harvested;
const supportRatio = city.activeVisitors / Math.max(1, city.population);
const knowledgeMaintenance = city.population * (city.knowledge.farming * 0.0008 + city.knowledge.metallurgy * 0.0012);
const upkeep = city.population * (0.010 + Math.max(0, 0.025 - supportRatio) * 0.09) + knowledgeMaintenance;
city.storedResources -= upkeep;
const foodPerCapita = city.storedResources / Math.max(1, city.population);
city.supplyStress = clamp((0.11 - foodPerCapita) * 8 + Math.max(0, 0.02 - supportRatio) * 8, 0, 1.8);
this.innovateCityKnowledge(city, harvested);
if (city.storedResources > city.population * 0.16 && city.population > 0) {
const prosperity = clamp(city.storedResources / Math.max(1, city.population) - 0.12, 0, 0.8);
const births = Math.max(1, Math.floor(city.population * (0.012 + prosperity * 0.012)));
city.population += births;
city.storedResources -= births * 0.55;
addBirthsToComposition(city.ethnicityComposition, births);
}
if (city.age > 20 && city.activeVisitors < 2 && city.storedResources < city.population * 0.03 && city.population > 0) {
const attrition = Math.max(1, Math.ceil(city.population * (city.activeVisitors === 0 ? 0.025 : 0.010)));
city.population -= attrition;
removeFromComposition(city.ethnicityComposition, attrition);
city.strength -= city.activeVisitors === 0 ? 0.030 : 0.012;
}
if (city.storedResources < 0) {
const deficit = Math.abs(city.storedResources);
const dominant = dominantComposition(city.ethnicityComposition);
const loss = Math.min(city.population, Math.ceil(deficit * 2.2 + city.population * 0.035));
city.population -= loss;
city.storedResources = 0;
removeFromComposition(city.ethnicityComposition, loss);
city.strength -= Math.min(0.4, 0.03 + deficit * 0.01);
if (loss > 0 && this.agents.length < this.maxAgents) {
this.spawnUrbanRefugees(city, Math.min(24, Math.max(2, Math.ceil(loss / 8))), dominant);
}
}
city.population = Math.max(0, Math.floor(city.population));
city.storedResources = clamp(city.storedResources, 0, Math.max(30, city.population * 1.4));
}
}
innovateCityKnowledge(city, harvested) {
city.knowledge ??= { farming: 0, metallurgy: 0 };
const scale = SimConfig.technology.cityInnovation;
const density = clamp(Math.sqrt(city.population) / 20, 0, 1.6);
const foodSurplus = clamp(city.storedResources / Math.max(1, city.population) - 0.08, 0, 0.5);
city.knowledge.farming = clamp(
city.knowledge.farming + scale * density * (0.4 + foodSurplus * 4) + harvested * 0.000015,
0,
1
);
city.knowledge.metallurgy = clamp(
city.knowledge.metallurgy + scale * density * clamp(city.pheromoneOutput, 0.1, 1.8) * 0.35,
0,
1
);
if (city.supplyStress > 0.8) {
city.knowledge.farming *= 0.998;
city.knowledge.metallurgy *= 0.997;
}
}
spawnUrbanRefugees(city, count, ethnicity = null) {
const dominant = ethnicity || dominantComposition(city.ethnicityComposition) || 1;
const template = this.ethnicities.get(dominant)?.averageTraits || this.randomTraits();
for (let i = 0; i < count; i++) {
this.agents.push(this.makeAgent(
clamp(city.x + this.rng.int(7) - 3, 0, this.world.size - 1),
clamp(city.y + this.rng.int(7) - 3, 0, this.world.size - 1),
dominant,
mutateTraits(template, this.rng, 0.05),
this.rng.range(4, 11)
));
}
}
findCityNear(x, y, radius) {
let best = null;
let bestDistance = Infinity;
for (const c of this.cities) {
const d = Math.abs(c.x - x) + Math.abs(c.y - y);
if (d <= radius && d < bestDistance) {
best = c;
bestDistance = d;
}
}
return best;
}
cityInfluence(city) {
if (!city || city.population <= 0 || city.strength <= 0) return 0;
const sedentaryFactor = clamp(0.45 + (city.sedentaryCulture ?? 0.5) * 0.9, 0.45, 1.35);
return (Math.sqrt(city.population) * 1.4 + Math.sqrt(Math.max(0, city.storedResources))) * sedentaryFactor;
}
getCityById(id) {
return this.cities.find(c => c.id === id) || null;
}
getPolityById(id) {
return this.polities.find(p => p.id === id) || null;
}
getPolityCities(polity) {
const living = [];
for (const id of [...polity.cityIds]) {
const city = this.getCityById(id);
if (!city || city.population <= 0) {
polity.cityIds.delete(id);
} else {
living.push(city);
}
}
return living;
}
dominantCityEthnicity(city) {
if (!city || !city.ethnicityComposition || !city.ethnicityComposition.size) return null;
return dominantComposition(city.ethnicityComposition) || null;
}
sameDominantEthnicity(cityA, cityB) {
const a = this.dominantCityEthnicity(cityA);
const b = this.dominantCityEthnicity(cityB);
return a !== null && b !== null && a === b;
}
distanceBetweenCities(cityA, cityB) {
return Math.abs(cityA.x - cityB.x) + Math.abs(cityA.y - cityB.y);
}
hasDirectTradeConnection(cityA, cityB) {
return !!cityA?.tradeLinks?.has(cityB?.id) || !!cityB?.tradeLinks?.has(cityA?.id);
}
effectiveDistance(cityA, cityB) {
let distance = this.distanceBetweenCities(cityA, cityB);
if (this.hasDirectTradeConnection(cityA, cityB)) distance *= 0.55;
return distance;
}
createPolity(centerCity) {
const id = this.nextPolity++;
const polity = {
id,
centerCityId: centerCity.id,
cityIds: new Set([centerCity.id]),
treasury: Math.max(0, centerCity.storedResources * 0.12),
color: hslToRgb((id * 0.38196601125) % 1, 0.58, 0.62),
founded: this.year,
legitimacy: this.rng.range(0.68, 0.94),
cohesion: this.rng.range(0.58, 0.9),
crisis: 0,
lastCrisisYear: this.year
};
centerCity.polityId = id;
centerCity.loyalty = 1;
centerCity.receivedAid = false;
this.polities.push(polity);
this.ensurePolityHistory(polity);
this.samplePolityHistory(polity);
return polity;
}
ensurePolityHistory(polity) {
if (!polity) return null;
if (!this.polityHistory.has(polity.id)) {
this.polityHistory.set(polity.id, {
id: polity.id,
color: polity.color || hslToRgb((polity.id * 0.38196601125) % 1, 0.58, 0.62),
founded: polity.founded ?? this.year,
ended: null,
active: true,
centerCityId: polity.centerCityId ?? null,
fate: null,
samples: []
});
}
return this.polityHistory.get(polity.id);
}
averagePolityLoyalty(polity) {
const cities = this.getPolityCities(polity);
const subordinates = cities.filter(city => city.id !== polity.centerCityId);
if (!subordinates.length) return 1;
return subordinates.reduce((sum, city) => sum + city.loyalty, 0) / subordinates.length;
}
samplePolityHistory(polity) {
const history = this.ensurePolityHistory(polity);
if (!history) return;
const cities = this.getPolityCities(polity);
history.centerCityId = polity.centerCityId ?? history.centerCityId;
history.samples.push({
year: this.year,
cities: cities.length,
population: cities.reduce((sum, city) => sum + city.population, 0),
treasury: polity.treasury || 0,
avgLoyalty: this.averagePolityLoyalty(polity)
});
while (history.samples.length > 160) history.samples.shift();
}
samplePolityHistories() {
for (const polity of this.polities) this.samplePolityHistory(polity);
}
markPolityEnded(polity, reason = "dissolved") {
if (!polity) return;
const history = this.ensurePolityHistory(polity);
if (!history) return;
this.samplePolityHistory(polity);
history.ended = this.year;
history.active = false;
history.fate = reason;
history.centerCityId = polity.centerCityId ?? history.centerCityId;
this.polityHistory.delete(polity.id);
this.deadPolityHistories.push(history);
while (this.deadPolityHistories.length > 80) this.deadPolityHistories.shift();
}
getAllPolityHistories() {
return [...this.deadPolityHistories, ...this.polityHistory.values()]
.sort((a, b) => (a.founded - b.founded) || (a.id - b.id));
}
addCityToPolity(city, polity, initialLoyalty = 0.5) {
if (!city || !polity) return;
if (city.polityId !== null && city.polityId !== polity.id) this.removeCityFromPolity(city);
city.polityId = polity.id;
city.loyalty = clamp(initialLoyalty, 0, 1);
city.receivedAid = false;
polity.cityIds.add(city.id);
}
removeCityFromPolity(city) {
if (!city || city.polityId === null) return;
const oldPolity = this.getPolityById(city.polityId);
if (oldPolity) oldPolity.cityIds.delete(city.id);
city.polityId = null;
city.loyalty = 0.45;
city.receivedAid = false;
}
polityAge(polity) {
return (this.year - (polity?.founded ?? this.year)) / MONTHS_PER_YEAR;
}
polityAgePressure(polity) {
const age = this.polityAge(polity);
if (age < 80) return 0;
const x = (age - 80) / 420;
return clamp(1 - Math.exp(-x), 0, 1.35);
}
polityOverextension(polity) {
const cities = this.getPolityCities(polity);
const center = this.getCityById(polity.centerCityId);
if (!center || cities.length <= 1) return 0;
const cityCountPressure = Math.max(0, cities.length - 5) * 0.08;
let distanceSum = 0;
let count = 0;
for (const city of cities) {
if (city.id === center.id) continue;
distanceSum += this.effectiveDistance(center, city);
count++;
}
const avgDistance = count ? distanceSum / count : 0;
const distancePressure = Math.max(0, avgDistance - 28) * 0.008;
return clamp(cityCountPressure + distancePressure, 0, 2);
}
polityResourceStress(polity) {
const cities = this.getPolityCities(polity);
if (!cities.length) return 1;
let poor = 0;
let supplyStress = 0;
for (const city of cities) {
const perCapita = city.storedResources / Math.max(1, city.population);
if (perCapita < 0.06) poor++;
supplyStress += city.supplyStress || 0;
}
const povertyRate = poor / cities.length;
const avgSupplyStress = supplyStress / cities.length;
const treasuryPerCity = (polity.treasury || 0) / Math.max(1, cities.length);
const treasuryStress = treasuryPerCity < 2 ? (2 - treasuryPerCity) / 2 : 0;
return clamp(povertyRate * 0.55 + avgSupplyStress * 0.35 + treasuryStress * 0.28, 0, 1.5);
}
polityLogisticsStress(polity) {
const cities = this.getPolityCities(polity);
const center = this.getCityById(polity.centerCityId);
if (!center || cities.length <= 1) return 0;
let stress = 0;
let count = 0;
for (const city of cities) {
if (city.id === center.id) continue;
const distance = this.effectiveDistance(center, city);
const food = city.storedResources / Math.max(1, city.population);
const distanceStress = Math.max(0, distance - SimConfig.polity.logisticsDistance) * 0.012;
const foodStress = Math.max(0, SimConfig.polity.minimumPerCapitaFood - food) * 4.5;
const routeRelief = this.hasDirectTradeConnection(center, city) ? 0.72 : 1;
stress += (distanceStress + foodStress + (city.supplyStress || 0) * 0.35) * routeRelief;
count++;
}
return clamp(stress / Math.max(1, count), 0, 1.8);
}
polityEthnicFragmentation(polity) {
const cities = this.getPolityCities(polity);
if (cities.length <= 1) return 0;
const counts = new Map();
for (const city of cities) {
const e = this.dominantCityEthnicity(city);
if (e !== null) counts.set(e, (counts.get(e) || 0) + 1);
}
if (!counts.size) return 0;
let max = 0;
for (const v of counts.values()) max = Math.max(max, v);
const dominantShare = max / cities.length;
return clamp(1 - dominantShare, 0, 1);
}
polityInstability(polity) {
const agePressure = this.polityAgePressure(polity);
const overextension = this.polityOverextension(polity);
const resourceStress = this.polityResourceStress(polity);
const logisticsStress = this.polityLogisticsStress(polity);
const fragmentation = this.polityEthnicFragmentation(polity);
const legitimacyBuffer = (polity.legitimacy ?? 0.7) * 0.85;
const cohesionBuffer = (polity.cohesion ?? 0.6) * 0.62;
return clamp(
agePressure * 0.35 +
overextension * 0.24 +
resourceStress * 0.38 +
logisticsStress * 0.30 +
fragmentation * 0.22 +
(polity.crisis || 0) * 0.42 -
legitimacyBuffer -
cohesionBuffer,
0,
2.5
);
}
foundPolities() {
for (const city of this.cities) {
if (city.polityId !== null || city.population < 55 || city.storedResources < 18) continue;
const centerInfluence = this.cityInfluence(city);
let absorbed = 0;
for (const other of this.cities) {
if (absorbed >= 3) break;
if (other === city || other.polityId !== null) continue;
if (this.distanceBetweenCities(city, other) > 34) continue;
const targetInfluence = this.cityInfluence(other);
if (centerInfluence <= targetInfluence * 1.08) continue;
const proximity = 1 / (1 + this.distanceBetweenCities(city, other) * 0.08);
const routeBonus = this.hasDirectTradeConnection(city, other) ? 1.5 : 1.0;
const dominanceScore = (centerInfluence / (targetInfluence + 1)) * proximity * routeBonus;
if (dominanceScore > 0.68 && this.rng.next() < 0.32) {
const polity = city.polityId === null ? this.createPolity(city) : this.getPolityById(city.polityId);
this.addCityToPolity(other, polity, 0.68);
absorbed++;
}
}
}
}
expandPolities() {
for (const polity of this.polities) {
const center = this.getCityById(polity.centerCityId);
if (!center) continue;
const centerInfluence = this.cityInfluence(center);
let absorbed = 0;
for (const city of this.cities) {
if (absorbed >= 2) break;
if (city.polityId !== null || city.id === center.id) continue;
const distance = this.effectiveDistance(center, city);
if (distance > 44) continue;
const targetInfluence = this.cityInfluence(city);
if (centerInfluence <= targetInfluence * 0.9) continue;
const dominanceScore =
(centerInfluence / (targetInfluence + 1)) *
(1 / (1 + distance * 0.08)) *
(this.hasDirectTradeConnection(center, city) ? 1.5 : 1.0);
if (dominanceScore > 0.62 && this.rng.next() < 0.24) {
this.addCityToPolity(city, polity, 0.62);
absorbed++;
}
}
}
}
collectAndRedistributeResources() {
for (const polity of this.polities) {
const cities = this.getPolityCities(polity);
const centerId = polity.centerCityId;
for (const city of cities) city.receivedAid = false;
for (const city of cities) {
if (city.id === centerId) continue;
const tax = city.storedResources * 0.035;
city.storedResources -= tax;
polity.treasury += tax;
}
const agePressure = this.polityAgePressure(polity);
const overextension = this.polityOverextension(polity);
const adminCost =
cities.length * 0.24 +
overextension * 1.1 +
agePressure * cities.length * 0.18;
polity.treasury -= adminCost;
if (polity.treasury < 0) {
const deficit = Math.abs(polity.treasury);
polity.treasury = 0;
polity.crisis = clamp((polity.crisis || 0) + deficit * 0.012, 0, 1.5);
}
const maintenance = Math.pow(cities.length, 1.12) * 0.10;
if (maintenance > 0) {
const center = this.getCityById(centerId);
const treasuryPayment = Math.min(polity.treasury, maintenance);
polity.treasury -= treasuryPayment;
const unpaid = maintenance - treasuryPayment;
if (unpaid > 0 && center) {
const paidByCenter = this.drainCityResources(center, unpaid);
if (paidByCenter < unpaid) {
for (const city of cities) {
if (city.id !== centerId) city.loyalty = clamp(city.loyalty - 0.012, 0, 1);
}
}
}
}
const poorCount = Math.ceil(cities.length * 0.1);
const poorest = [...cities]
.sort((a, b) => (a.storedResources / Math.max(1, a.population)) - (b.storedResources / Math.max(1, b.population)))
.slice(0, poorCount);
for (const city of poorest) {
const target = city.population * 0.08;
const need = target - city.storedResources;
if (need > 0 && polity.treasury > 0) {
const aid = Math.min(need, polity.treasury);
city.storedResources += aid;
polity.treasury -= aid;
city.receivedAid = true;
city.loyalty = clamp(city.loyalty + 0.05, 0, 1);
}
}
}
}
erodePolityLegitimacy() {
for (const polity of this.polities) {
const agePressure = this.polityAgePressure(polity);
const resourceStress = this.polityResourceStress(polity);
const overextension = this.polityOverextension(polity);
const erosion =
0.0015 +
agePressure * 0.003 +
resourceStress * 0.003 +
overextension * 0.0015;
polity.legitimacy = clamp((polity.legitimacy ?? 0.7) - erosion, 0, 1);
polity.cohesion = clamp((polity.cohesion ?? 0.6) - erosion * 0.45, 0, 1);
const cities = this.getPolityCities(polity);
if (this.polityAge(polity) < 80 && polity.treasury > cities.length * 10) {
polity.legitimacy = clamp(polity.legitimacy + 0.008, 0, 1);
polity.cohesion = clamp(polity.cohesion + 0.005, 0, 1);
}
polity.crisis = clamp((polity.crisis || 0) * 0.88, 0, 1.5);
}
}
triggerPolityCrises() {
for (const polity of this.polities) {
const age = this.polityAge(polity);
if (age < 140) continue;
if ((this.year - (polity.lastCrisisYear ?? 0)) / MONTHS_PER_YEAR < 160) continue;
const instability = this.polityInstability(polity);
const agePressure = this.polityAgePressure(polity);
const chance = 0.006 + agePressure * 0.018 + instability * 0.014;
if (this.rng.next() >= chance) continue;
polity.lastCrisisYear = this.year;
const severity = clamp(
0.08 +
agePressure * this.rng.range(0.08, 0.20) +
instability * this.rng.range(0.05, 0.16),
0.05,
0.36
);
polity.crisis = clamp((polity.crisis || 0) + severity, 0, 1.5);
polity.legitimacy = clamp((polity.legitimacy ?? 0.7) - severity * 0.30, 0, 1);
polity.cohesion = clamp((polity.cohesion ?? 0.6) - severity * 0.18, 0, 1);
const center = this.getCityById(polity.centerCityId);
for (const city of this.getPolityCities(polity)) {
if (city.id === polity.centerCityId) continue;
const distance = center ? this.effectiveDistance(center, city) : 40;
const distanceFactor = clamp(distance / 40, 0.25, 1);
city.loyalty = clamp(city.loyalty - severity * distanceFactor, 0, 1);
}
}
}
applyOldStateStress() {
for (const polity of this.polities) {
const age = this.polityAge(polity);
if (age < 1000) continue;
const cities = this.getPolityCities(polity);
if (cities.length <= 1) continue;
const oldAge = clamp((age - 1000) / 1000, 0, 1);
polity.legitimacy = clamp((polity.legitimacy ?? 0.7) - oldAge * 0.018, 0, 1);
polity.cohesion = clamp((polity.cohesion ?? 0.6) - oldAge * 0.012, 0, 1);
polity.crisis = clamp((polity.crisis || 0) + oldAge * 0.035, 0, 1.5);
const center = this.getCityById(polity.centerCityId);
for (const city of cities) {
if (city.id === polity.centerCityId) continue;
const distance = center ? this.effectiveDistance(center, city) : 40;
const distanceFactor = clamp(distance / 35, 0.2, 1);
city.loyalty = clamp(city.loyalty - oldAge * distanceFactor * 0.045, 0, 1);
}
}
}
updateCityLoyalty() {
for (const polity of this.polities) {
const center = this.getCityById(polity.centerCityId);
if (!center) continue;
center.loyalty = 1;
const instability = this.polityInstability(polity);
const agePressure = this.polityAgePressure(polity);
const overextension = this.polityOverextension(polity);
const logisticsStress = this.polityLogisticsStress(polity);
for (const city of this.getPolityCities(polity)) {
if (city.id === center.id) continue;
const perCapita = city.storedResources / Math.max(1, city.population);
const distance = this.effectiveDistance(city, center);
let delta = 0;
delta += clamp((perCapita - 0.10) * 0.07, -0.025, 0.045);
delta += this.sameDominantEthnicity(city, center) ? 0.03 : -0.012;
delta += clamp(0.045 - distance * 0.0011, -0.025, 0.045);
if (this.hasDirectTradeConnection(city, center)) delta += 0.025;
if (city.receivedAid) delta += 0.04;
delta -= 0.004;
if (city.storedResources < city.population * 0.05) delta -= 0.035;
delta -= (city.supplyStress || 0) * 0.018;
delta -= instability * 0.020;
delta -= agePressure * 0.010;
delta -= overextension * clamp(distance / 48, 0, 1) * 0.014;
delta -= logisticsStress * clamp(distance / 42, 0.25, 1) * 0.018;
if ((polity.legitimacy ?? 0.7) < 0.25) delta -= 0.020;
if ((polity.crisis || 0) > 0.6) delta -= 0.014;
city.loyalty = clamp(city.loyalty + delta, 0, 1);
}
}
}
splitUnloyalCities() {
for (const polity of this.polities) {
const instability = this.polityInstability(polity);
const agePressure = this.polityAgePressure(polity);
const threshold = clamp(0.14 + instability * 0.05 + agePressure * 0.03, 0.14, 0.28);
for (const city of this.getPolityCities(polity)) {
if (city.id === polity.centerCityId || city.loyalty >= threshold) continue;
const chance =
(threshold - city.loyalty) * 0.28 +
instability * 0.012 +
agePressure * 0.010;
if (this.rng.next() < chance) this.removeCityFromPolity(city);
}
}
}
cleanupPolities() {
const survivors = [];
for (const polity of this.polities) {
const cities = this.getPolityCities(polity);
if (!cities.length) {
this.markPolityEnded(polity, "collapsed");
continue;
}
let center = this.getCityById(polity.centerCityId);
if (!center) {
center = cities.reduce((best, city) => this.cityInfluence(city) > this.cityInfluence(best) ? city : best, cities[0]);
polity.centerCityId = center.id;
center.loyalty = 1;
polity.treasury *= 0.5;
polity.legitimacy = clamp((polity.legitimacy ?? 0.7) - 0.22, 0, 1);
polity.cohesion = clamp((polity.cohesion ?? 0.6) - 0.16, 0, 1);
polity.crisis = clamp((polity.crisis || 0) + 0.35, 0, 1.5);
polity.lastCrisisYear = this.year;
for (const city of cities) {
if (city.id !== center.id) city.loyalty = clamp(city.loyalty - 0.18, 0, 1);
}
const history = this.ensurePolityHistory(polity);
if (history) history.centerCityId = center.id;
}
if (cities.length === 1) {
this.markPolityEnded(polity, "fragmented");
cities[0].polityId = null;
cities[0].loyalty = 0.45;
cities[0].receivedAid = false;
continue;
}
survivors.push(polity);
}
this.polities = survivors;
const validPolities = new Set(this.polities.map(p => p.id));
for (const city of this.cities) {
if (city.polityId !== null && !validPolities.has(city.polityId)) {
city.polityId = null;
city.loyalty = 0.45;
city.receivedAid = false;
}
}
}
updatePolities() {
if (this.year % years(1) !== 0) return;
this.cleanupPolities();
this.foundPolities();
this.expandPolities();
if (this.reinforcePolityTradeRoutes) this.reinforcePolityTradeRoutes();
this.collectAndRedistributeResources();
this.erodePolityLegitimacy();
this.triggerPolityCrises();
this.applyOldStateStress();
this.updateCityLoyalty();
this.splitUnloyalCities();
this.cleanupPolities();
if (this.samplePolityHistories) this.samplePolityHistories();
}
reinforcePolityTradeRoutes() {
const w = this.world;
for (const polity of this.polities) {
if (this.rng.next() > 0.48) continue;
const center = this.getCityById(polity.centerCityId);
if (!center) continue;
const candidates = this.getPolityCities(polity)
.filter(city => city.id !== center.id && !this.hasDirectTradeConnection(center, city))
.filter(city => this.distanceBetweenCities(center, city) <= 40)
.sort((a, b) => this.effectiveDistance(center, a) - this.effectiveDistance(center, b));
if (!candidates.length) continue;
const target = candidates[Math.min(candidates.length - 1, this.rng.int(Math.min(3, candidates.length)))];
const path = this.findTerrainRoute(center.x, center.y, target.x, target.y);
if (!this.isValidRoutePath(path, target.x, target.y)) continue;
const cities = this.getPolityCities(polity);
const roadCost = 8 + cities.length * 1.5 + this.polityOverextension(polity) * 3;
if ((polity.treasury || 0) < roadCost) {
polity.crisis = clamp((polity.crisis || 0) + 0.025, 0, 1.5);
continue;
}
polity.treasury -= roadCost;
if (!this.payRouteConstructionCost(center, target, path, polity)) continue;
center.tradeLinks.add(target.id);
target.tradeLinks.add(center.id);
this.tradeLinks.push({ from: center.id, to: target.id, strength: 0.22, path });
for (const tile of path) {
w.tradeRoute[tile] = Math.min(255, w.tradeRoute[tile] + 34);
w.pheromone[tile] += 2.4;
}
}
}
updateTradeRoutes() {
const w = this.world;
for (let i = 0; i < w.count; i++) {
const invalidTerrain = w.terrain[i] === Terrain.WATER || w.move[i] > 2.2;
if (invalidTerrain) w.tradeRoute[i] = 0;
else if (w.tradeRoute[i] > 0 && this.year % years(4) === 0) w.tradeRoute[i] = Math.max(0, w.tradeRoute[i] - 2);
}
this.tradeLinks = [];
for (const city of this.cities) city.tradeLinks.clear();
const candidates = [];
for (let a = 0; a < this.cities.length; a++) {
for (let b = a + 1; b < this.cities.length; b++) {
const c1 = this.cities[a];
const c2 = this.cities[b];
const d = Math.abs(c1.x - c2.x) + Math.abs(c1.y - c2.y);
if (d > 44) continue;
const path = this.findTerrainRoute(c1.x, c1.y, c2.x, c2.y);
if (!this.isValidRoutePath(path, c2.x, c2.y)) continue;
const strength = this.routeStrengthForPath(path);
if (strength < 0.08) continue;
if (!this.canPayRouteConstructionCost(c1, c2, path)) continue;
candidates.push({ c1, c2, strength, path });
}
}
candidates.sort((a, b) => b.strength - a.strength);
const maxLinks = Math.max(1, Math.floor(this.cities.length / 2));
const supportedRoutes = new Set();
for (const candidate of candidates) {
if (this.tradeLinks.length >= maxLinks) break;
const { c1, c2, strength, path } = candidate;
const c1Limit = c1.population > 90 ? 3 : 2;
const c2Limit = c2.population > 90 ? 3 : 2;
if (c1.tradeLinks.size >= c1Limit || c2.tradeLinks.size >= c2Limit) continue;
if (!this.payRouteConstructionCost(c1, c2, path)) continue;
c1.tradeLinks.add(c2.id);
c2.tradeLinks.add(c1.id);
this.tradeLinks.push({ from: c1.id, to: c2.id, strength, path });
this.exchangeCityResources(c1, c2, strength, path);
for (const tile of path) {
supportedRoutes.add(tile);
w.tradeRoute[tile] = Math.min(255, w.tradeRoute[tile] + 18);
}
}
for (let i = 0; i < w.count; i++) {
if (w.tradeRoute[i] && !supportedRoutes.has(i)) w.tradeRoute[i] = Math.max(0, w.tradeRoute[i] - 4);
}
this.diffuseCityKnowledgeThroughTrade();
}
diffuseCityKnowledgeThroughTrade() {
for (const link of this.tradeLinks) {
const a = this.getCityById(link.from);
const b = this.getCityById(link.to);
if (!a || !b) continue;
a.knowledge ??= { farming: 0, metallurgy: 0 };
b.knowledge ??= { farming: 0, metallurgy: 0 };
const rate = clamp(SimConfig.technology.tradeDiffusion * (0.5 + link.strength), 0, 0.012);
const farmingDelta = (a.knowledge.farming - b.knowledge.farming) * rate;
const metallurgyDelta = (a.knowledge.metallurgy - b.knowledge.metallurgy) * rate;
a.knowledge.farming = clamp(a.knowledge.farming - farmingDelta, 0, 1);
b.knowledge.farming = clamp(b.knowledge.farming + farmingDelta, 0, 1);
a.knowledge.metallurgy = clamp(a.knowledge.metallurgy - metallurgyDelta, 0, 1);
b.knowledge.metallurgy = clamp(b.knowledge.metallurgy + metallurgyDelta, 0, 1);
}
}
findTerrainRoute(x1, y1, x2, y2) {
const w = this.world;
const path = [];
const visited = new Set();
let x = x1;
let y = y1;
const maxSteps = Math.min(w.size * 2, Math.abs(x1 - x2) + Math.abs(y1 - y2) + 60);
const directions = [
[1, 0],
[-1, 0],
[0, 1],
[0, -1]
];
for (let step = 0; step < maxSteps; step++) {
const current = w.idx(x, y);
path.push(current);
visited.add(current);
if (x === x2 && y === y2) break;
let bestX = x;
let bestY = y;
let bestScore = Infinity;
for (const [dx, dy] of directions) {
const nx = x + dx;
const ny = y + dy;
if (nx < 0 || ny < 0 || nx >= w.size || ny >= w.size) continue;
const i = w.idx(nx, ny);
const terrainCost = w.move[i] + (w.terrain[i] === Terrain.WATER ? 8 : 0) + (w.terrain[i] === Terrain.MOUNTAIN ? 2.2 : 0);
const revisitCost = visited.has(i) ? 5 : 0;
const routeEase = w.tradeRoute[i] ? -2 : 0;
const pheromoneEase = -clamp(w.pheromone[i] / 12, 0, 1.4);
const distance = Math.abs(nx - x2) + Math.abs(ny - y2);
const score = distance * 1.4 + terrainCost * 1.65 + revisitCost + routeEase + pheromoneEase;
if (score < bestScore) {
bestScore = score;
bestX = nx;
bestY = ny;
}
}
if (bestX === x && bestY === y) break;
x = bestX;
y = bestY;
}
return path;
}
routeStrengthForPath(path) {
const w = this.world;
if (!path.length) return 0;
let route = 0;
let pheromone = 0;
let terrainEase = 0;
for (const i of path) {
route += w.tradeRoute[i] > 0 ? 1 : 0;
pheromone += clamp(w.pheromone[i] / 9, 0, 1);
terrainEase += 1 / Math.max(1, w.move[i]);
}
return route / path.length * 0.45 + pheromone / path.length * 0.35 + terrainEase / path.length * 0.2;
}
isValidRoutePath(path, targetX, targetY) {
const w = this.world;
if (!path.length) return false;
const last = path[path.length - 1];
if (last % w.size !== targetX || Math.floor(last / w.size) !== targetY) return false;
if (path.length > 42) return false;
let totalCost = 0;
let hardTiles = 0;
const seen = new Set();
for (const tile of path) {
if (seen.has(tile)) return false;
seen.add(tile);
if (w.terrain[tile] === Terrain.WATER) return false;
totalCost += w.move[tile];
if (w.move[tile] > 1.8) hardTiles++;
}
return totalCost / path.length <= 1.55 && hardTiles / path.length <= 0.12;
}
exchangeCityResources(a, b, strength, path) {
const delta = (a.storedResources - b.storedResources) * 0.018 * strength;
a.storedResources -= delta;
b.storedResources += delta;
const traffic = Math.min(0.8, strength * 0.26);
this.depositRoutePheromone(path, traffic);
}
routeConstructionCost(path, polityBacked = false) {
let weakTiles = 0;
for (const tile of path) {
if (this.world.tradeRoute[tile] < 24) weakTiles++;
}
return weakTiles * (polityBacked ? 0.22 : 0.16);
}
canPayRouteConstructionCost(cityA, cityB, path, polity = null) {
const cost = this.routeConstructionCost(path, !!polity);
if (cost <= 0) return true;
return cityA.storedResources + cityB.storedResources + (polity?.treasury || 0) >= cost;
}
payRouteConstructionCost(cityA, cityB, path, polity = null) {
const cost = this.routeConstructionCost(path, !!polity);
if (cost <= 0) return true;
if (!this.canPayRouteConstructionCost(cityA, cityB, path, polity)) return false;
let remaining = cost;
if (polity) {
const treasuryPayment = Math.min(polity.treasury, cost * 0.65);
polity.treasury -= treasuryPayment;
remaining -= treasuryPayment;
}
const half = remaining * 0.5;
const paidA = this.drainCityResources(cityA, half);
const paidB = this.drainCityResources(cityB, half);
remaining -= paidA + paidB;
if (remaining > 0) remaining -= this.drainCityResources(cityA.storedResources >= cityB.storedResources ? cityA : cityB, remaining);
if (remaining > 0 && polity) {
const treasuryPayment = Math.min(polity.treasury, remaining);
polity.treasury -= treasuryPayment;
remaining -= treasuryPayment;
}
return remaining <= 0.001;
}
drainCityResources(city, amount) {
const paid = Math.min(city.storedResources, amount);
city.storedResources -= paid;
return paid;
}
depositRoutePheromone(path, amount) {
const w = this.world;
for (const i of path) {
w.pheromone[i] += amount;
}
}
updateEthnicStats() {
for (const e of this.ethnicities.values()) {
e.population = 0;
e.diversity = 0;
e.centroidX = 0;
e.centroidY = 0;
e.activeTraitPopulation = 0;
e.traitSums = emptyTraitSums();
}
for (const a of this.agents) {
const e = this.ethnicities.get(a.ethnicity);
if (!e) continue;
e.population++;
e.centroidX += a.x;
e.centroidY += a.y;
e.activeTraitPopulation++;
addTraits(e.traitSums, a.traits);
}
for (const city of this.cities) {
for (const [id, count] of city.ethnicityComposition) {
const e = this.ethnicities.get(id);
if (!e) continue;
e.population += count;
e.centroidX += city.x * count;
e.centroidY += city.y * count;
}
}
for (const e of this.ethnicities.values()) {
if (!e.population) continue;
e.centroidX /= e.population;
e.centroidY /= e.population;
const activeTraitCount = Math.max(1, e.activeTraitPopulation);
e.averageTraits = averageTraits(e.traitSums, activeTraitCount);
}
for (const a of this.agents) {
const e = this.ethnicities.get(a.ethnicity);
if (e?.averageTraits) e.diversity += traitDistance(a.traits, e.averageTraits);
}
for (const e of this.ethnicities.values()) {
if (e.population) e.diversity /= e.population;
}
}
splitDivergentEthnicities() {
for (const e of this.ethnicities.values()) {
if (e.activeTraitPopulation < 24 || e.population < 40 || e.diversity < 0.14) continue;
const candidates = [];
let tempSum = 0;
let humidSum = 0;
for (const a of this.agents) {
if (a.ethnicity !== e.id) continue;
const far = traitDistance(a.traits, e.averageTraits) > e.diversity * 0.95;
const spatial = Math.hypot(a.x - e.centroidX, a.y - e.centroidY) > this.world.size * 0.09;
const tile = this.world.idx(a.x, a.y);
const climate = this.climateMismatch(e.id, tile) > 0.18;
if ((far || spatial || climate) && this.rng.next() < 0.72) {
candidates.push(a);
tempSum += this.world.temperature[tile];
humidSum += this.world.humidity[tile];
}
}
if (candidates.length >= 6) {
const newId = this.createEthnicity(e.id, {
temperature: tempSum / candidates.length,
humidity: humidSum / candidates.length
});
for (const a of candidates) a.ethnicity = newId;
}
}
}
toJSON() {
return {
version: SimConfig.save.version,
rngSeed: this.rng.seed,
year: this.year,
deaths: this.deaths,
nextEthnicity: this.nextEthnicity,
nextCity: this.nextCity,
nextPolity: this.nextPolity,
maxAgents: this.maxAgents,
world: {
size: this.world.size,
terrain: packArray(this.world.terrain),
resource: packArray(this.world.resource),
regen: packArray(this.world.regen),
move: packArray(this.world.move),
fertility: packArray(this.world.fertility),
mineral: packArray(this.world.mineral),
temperature: packArray(this.world.temperature),
humidity: packArray(this.world.humidity),
pheromone: packArray(this.world.pheromone),
tradeRoute: packArray(this.world.tradeRoute),
farmland: packArray(this.world.farmland),
cityPull: packArray(this.world.cityPull),
city: packArray(this.world.city),
pressure: packArray(this.world.pressure),
dominantEthnicity: packArray(this.world.dominantEthnicity),
cultureDiversity: packArray(this.world.cultureDiversity)
},
agents: this.agents,
ethnicities: [...this.ethnicities.values()].map(e => ({
id: e.id,
parent: e.parent,
born: e.born,
climateTemp: e.climateTemp,
climateHumidity: e.climateHumidity,
color: e.color
})),
cities: this.cities.map(c => ({
...c,
ethnicityComposition: [...c.ethnicityComposition],
tradeLinks: [...c.tradeLinks]
})),
polities: this.polities.map(p => ({
id: p.id,
centerCityId: p.centerCityId,
cityIds: [...p.cityIds],
treasury: p.treasury,
color: p.color,
founded: p.founded,
legitimacy: p.legitimacy,
cohesion: p.cohesion,
crisis: p.crisis,
lastCrisisYear: p.lastCrisisYear
})),
polityHistory: [...this.polityHistory.values()],
deadPolityHistories: this.deadPolityHistories
};
}
static fromJSON(state) {
if (!state?.world?.size) throw new Error("Invalid save payload");
const sim = new Simulation(state.world.size, 0, { blankWorld: true, skipSpawn: true });
sim.rng.seed = state.rngSeed >>> 0;
sim.year = state.year || 0;
sim.deaths = state.deaths || 0;
sim.nextEthnicity = state.nextEthnicity || 1;
sim.nextCity = state.nextCity || 1;
sim.nextPolity = state.nextPolity || 1;
sim.maxAgents = state.maxAgents || 30000;
sim.world.terrain.set(unpackArray(state.world.terrain, Uint8Array));
sim.world.resource.set(unpackArray(state.world.resource, Float32Array));
sim.world.regen.set(unpackArray(state.world.regen, Float32Array));
sim.world.move.set(unpackArray(state.world.move, Float32Array));
sim.world.fertility.set(unpackArray(state.world.fertility, Float32Array));
sim.world.mineral.set(unpackArray(state.world.mineral, Float32Array));
sim.world.temperature.set(unpackArray(state.world.temperature, Float32Array));
sim.world.humidity.set(unpackArray(state.world.humidity, Float32Array));
sim.world.pheromone.set(unpackArray(state.world.pheromone, Float32Array));
sim.world.tradeRoute.set(unpackArray(state.world.tradeRoute, Uint8Array));
sim.world.farmland.set(unpackArray(state.world.farmland, Float32Array));
sim.world.cityPull.set(unpackArray(state.world.cityPull, Float32Array));
sim.world.city.set(unpackArray(state.world.city, Int32Array));
if (state.world.pressure) sim.world.pressure.set(unpackArray(state.world.pressure, Float32Array));
if (state.world.dominantEthnicity) sim.world.dominantEthnicity.set(unpackArray(state.world.dominantEthnicity, Int32Array));
if (state.world.cultureDiversity) sim.world.cultureDiversity.set(unpackArray(state.world.cultureDiversity, Float32Array));
sim.agents = state.agents || [];
for (const a of sim.agents) sim.ensureAgentTech(a);
sim.ethnicities = new Map((state.ethnicities || []).map(e => [e.id, {
id: e.id,
parent: e.parent,
born: e.born,
population: 0,
diversity: 0,
climateTemp: e.climateTemp,
climateHumidity: e.climateHumidity,
color: e.color,
centroidX: 0,
centroidY: 0
}]));
sim.cities = (state.cities || []).map(c => ({
id: c.id,
x: c.x,
y: c.y,
population: c.population || 0,
storedResources: c.storedResources || 0,
ethnicityComposition: new Map(c.ethnicityComposition || []),
pheromoneOutput: c.pheromoneOutput || 0,
agriculturalRadius: c.agriculturalRadius || 2,
tradeLinks: new Set(c.tradeLinks || []),
activeVisitors: 0,
age: c.age || 0,
strength: c.strength || 1,
sedentaryCulture: c.sedentaryCulture ?? 0.5,
knowledge: {
farming: clamp(c.knowledge?.farming ?? 0, 0, 1),
metallurgy: clamp(c.knowledge?.metallurgy ?? 0, 0, 1)
},
supplyStress: c.supplyStress ?? 0,
polityId: c.polityId ?? null,
loyalty: c.loyalty ?? 0.5,
receivedAid: c.receivedAid ?? false
}));
sim.polities = (state.polities || []).map(p => ({
id: p.id,
centerCityId: p.centerCityId,
cityIds: new Set(p.cityIds || []),
treasury: p.treasury || 0,
color: p.color || hslToRgb((p.id * 0.38196601125) % 1, 0.58, 0.62),
founded: p.founded || 0,
legitimacy: p.legitimacy ?? 0.7,
cohesion: p.cohesion ?? 0.6,
crisis: p.crisis ?? 0,
lastCrisisYear: p.lastCrisisYear ?? sim.year
}));
sim.polityHistory = new Map((state.polityHistory || []).map(h => [h.id, {
id: h.id,
color: h.color || hslToRgb((h.id * 0.38196601125) % 1, 0.58, 0.62),
founded: h.founded || 0,
ended: h.ended ?? null,
active: h.active !== false,
centerCityId: h.centerCityId ?? null,
fate: h.fate || null,
samples: h.samples || []
}]));
sim.deadPolityHistories = (state.deadPolityHistories || []).map(h => ({
id: h.id,
color: h.color || hslToRgb((h.id * 0.38196601125) % 1, 0.58, 0.62),
founded: h.founded || 0,
ended: h.ended ?? null,
active: false,
centerCityId: h.centerCityId ?? null,
fate: h.fate || null,
samples: h.samples || []
}));
for (const polity of sim.polities) sim.ensurePolityHistory(polity);
sim.tradeLinks = [];
sim.rebuildOccupancy();
sim.updateTradeRoutes();
sim.cleanupPolities();
sim.updateEthnicStats();
return sim;
}
}
function render() {
const start = performance.now();
const w = sim.world;
const size = w.size;
if (!renderImage || renderImageSize !== size) {
renderImage = ctx.createImageData(size, size);
renderImageSize = size;
}
const image = renderImage;
const data = image.data;
const mode = els.viewMode.value;
for (let i = 0; i < w.count; i++) {
let color;
if (mode === "resources") {
const v = clamp(w.resource[i] / 30, 0, 1);
color = mix([28, 36, 40], [107, 188, 85], v);
} else if (mode === "ethnicity") {
const id = w.dominantEthnicity[i];
const ethnicity = id >= 0 ? sim.ethnicities.get(id) : null;
color = ethnicity
? mix(ethnicity.color, [178, 184, 177], clamp(w.cultureDiversity[i] * 0.85, 0, 0.65))
: mix(terrainInfo[w.terrain[i]].color, [18, 21, 23], 0.55);
} else if (mode === "pressure") {
const v = clamp(w.pressure[i] / 12, 0, 1);
color = mix(terrainInfo[w.terrain[i]].color, [210, 73, 67], v);
} else if (mode === "cities") {
color = w.city[i] >= 0 ? mix([74, 105, 58], [226, 198, 121], clamp(w.farmland[i], 0, 1)) : terrainInfo[w.terrain[i]].color;
} else if (mode === "polities") {
color = terrainInfo[w.terrain[i]].color;
if (w.city[i] >= 0) {
const city = sim.getCityById(w.city[i]);
const polity = city && city.polityId !== null ? sim.getPolityById(city.polityId) : null;
if (polity) color = mix(color, polity.color, 0.5);
}
} else if (mode === "technology") {
color = mix(terrainInfo[w.terrain[i]].color, [44, 42, 48], 0.35);
} else {
color = terrainInfo[w.terrain[i]].color;
}
const p = i * 4;
data[p] = color[0];
data[p + 1] = color[1];
data[p + 2] = color[2];
data[p + 3] = 255;
}
ctx.putImageData(image, 0, 0);
drawTradeLinks();
drawFarmlandRings(mode);
drawAgentsAndCities(mode);
maybeRenderStateGraph();
els.frameCost.textContent = `${Math.round(performance.now() - start)}ms`;
}
function drawTradeLinks() {
const w = sim.world;
ctx.save();
ctx.globalAlpha = 0.24;
ctx.fillStyle = "#d9b650";
for (let i = 0; i < w.count; i++) {
if (w.tradeRoute[i]) ctx.fillRect(i % w.size, Math.floor(i / w.size), 1, 1);
}
if (!sim.tradeLinks.length) {
ctx.restore();
return;
}
for (const link of sim.tradeLinks) {
ctx.globalAlpha = clamp(0.12 + link.strength * 0.35, 0.16, 0.42);
ctx.fillStyle = "#ffd75a";
for (const tile of link.path || []) {
ctx.fillRect(tile % w.size, Math.floor(tile / w.size), 1, 1);
}
}
ctx.restore();
}
function drawFarmlandRings(mode) {
if (mode !== "cities") return;
const w = sim.world;
ctx.save();
ctx.globalAlpha = 0.78;
ctx.fillStyle = "#d9c45f";
for (const city of sim.cities) {
const radius = Math.max(1, city.agriculturalRadius);
for (let dy = -radius; dy <= radius; dy++) {
for (let dx = -radius; dx <= radius; dx++) {
const d = Math.abs(dx) + Math.abs(dy);
if (d !== radius) continue;
const x = city.x + dx;
const y = city.y + dy;
if (x < 0 || y < 0 || x >= w.size || y >= w.size) continue;
if (w.terrain[w.idx(x, y)] !== Terrain.WATER) ctx.fillRect(x, y, 1, 1);
}
}
}
ctx.restore();
}
function drawAgentsAndCities(mode) {
ctx.save();
for (const city of sim.cities) {
const radius = cityRenderRadius(city);
const color = cityDisplayColor(city, mode);
ctx.fillStyle = `rgba(${color[0]}, ${color[1]}, ${color[2]}, 0.72)`;
ctx.globalAlpha = 0.88;
ctx.fillRect(city.x - radius, city.y - radius, radius * 2 + 1, radius * 2 + 1);
}
ctx.globalAlpha = 1;
for (const a of sim.agents) {
if (mode === "ethnicity") {
const e = sim.ethnicities.get(a.ethnicity);
if (!e) continue;
ctx.fillStyle = `rgb(${e.color[0]},${e.color[1]},${e.color[2]})`;
ctx.fillRect(a.x, a.y, 1, 1);
} else if (mode === "technology") {
sim.ensureAgentTech(a);
const farming = a.tech.farming || 0;
const metallurgy = a.tech.metallurgy || 0;
const tech = clamp(Math.max(farming, metallurgy), 0, 1);
if (tech <= 0.01) continue;
const color = mix([95, 171, 91], [202, 169, 102], metallurgy / Math.max(0.001, farming + metallurgy));
ctx.fillStyle = `rgba(${color[0]},${color[1]},${color[2]},${clamp(0.32 + tech * 0.68, 0.32, 1)})`;
ctx.fillRect(a.x, a.y, 1, 1);
} else {
ctx.fillStyle = "#eeeccf";
ctx.fillRect(a.x, a.y, 1, 1);
}
}
for (const city of sim.cities) {
const color = cityDisplayColor(city, mode);
ctx.fillStyle = `rgb(${Math.min(255, color[0] + 55)}, ${Math.min(255, color[1] + 55)}, ${Math.min(255, color[2] + 55)})`;
ctx.fillRect(city.x, city.y, 1, 1);
}
ctx.restore();
}
function cityRenderRadius(city) {
return clamp(Math.floor(Math.sqrt(city.population) / 12), 1, 7);
}
function cityMajorityColor(city) {
const id = dominantComposition(city.ethnicityComposition);
return sim.ethnicities.get(id)?.color || [242, 215, 134];
}
function cityDisplayColor(city, mode) {
if (mode === "polities") {
if (city.polityId !== null) {
const polity = sim.getPolityById(city.polityId);
if (polity) return polity.color;
}
return [218, 205, 154];
}
return cityMajorityColor(city);
}
function showTooltip(event) {
const rect = els.canvas.getBoundingClientRect();
const simRect = els.sim.getBoundingClientRect();
const x = Math.floor((event.clientX - rect.left) / rect.width * sim.world.size);
const y = Math.floor((event.clientY - rect.top) / rect.height * sim.world.size);
hoverState = {
x,
y,
left: event.clientX - simRect.left + 16,
top: event.clientY - simRect.top + 16,
width: simRect.width,
height: simRect.height
};
renderTooltip();
}
function renderTooltip() {
if (!hoverState) return;
const x = hoverState.x;
const y = hoverState.y;
if (x < 0 || y < 0 || x >= sim.world.size || y >= sim.world.size) {
hideTooltip();
return;
}
const w = sim.world;
const i = w.idx(x, y);
const agent = findAgentAt(x, y);
const city = w.city[i] >= 0 ? sim.cities.find(c => c.id === w.city[i]) : null;
const terrain = terrainInfo[w.terrain[i]];
const ethnicity = agent ? sim.ethnicities.get(agent.ethnicity) : null;
const waterInfluence = waterInfluenceAt(w, x, y).toFixed(2);
const cityEthnicity = city ? dominantComposition(city.ethnicityComposition) : null;
const mismatch = agent ? sim.climateMismatch(agent.ethnicity, i) : 0;
const polity = city && city.polityId !== null ? sim.getPolityById(city.polityId) : null;
const regionalEthnicity = w.dominantEthnicity[i] >= 0 ? `E${w.dominantEthnicity[i]}` : "-";
els.tooltip.innerHTML = `
<strong>${agent ? "Agent group" : terrain.name}</strong>
<span><b>Tile</b><em>${x}, ${y}</em></span>
<span><b>Terrain</b><em>${terrain.name}</em></span>
<span><b>Resources</b><em>${w.resource[i].toFixed(1)}</em></span>
<span><b>Fertility</b><em>${w.fertility[i].toFixed(2)}</em></span>
<span><b>Temp / humid</b><em>${w.temperature[i].toFixed(2)} / ${w.humidity[i].toFixed(2)}</em></span>
<span><b>Water influence</b><em>${waterInfluence}</em></span>
<span><b>Minerals</b><em>${w.mineral[i].toFixed(2)}</em></span>
<span><b>Pheromone</b><em>${w.pheromone[i].toFixed(1)}</em></span>
<span><b>Pressure</b><em>${w.pressure[i].toFixed(0)}</em></span>
<span><b>Local culture</b><em>${regionalEthnicity} / ${w.cultureDiversity[i].toFixed(2)}</em></span>
${w.tradeRoute[i] ? `<span><b>Route</b><em>${w.tradeRoute[i]}</em></span>` : ""}
${city ? `<span><b>City</b><em>#${city.id}</em></span>` : ""}
${city ? `<span><b>Urban pop</b><em>${city.population.toLocaleString()}</em></span>` : ""}
${city ? `<span><b>Food stock</b><em>${city.storedResources.toFixed(1)}</em></span>` : ""}
${city ? `<span><b>Supply stress</b><em>${(city.supplyStress || 0).toFixed(2)}</em></span>` : ""}
${city ? `<span><b>Farmland radius</b><em>${city.agriculturalRadius}</em></span>` : ""}
${city ? `<span><b>Trade links</b><em>${city.tradeLinks.size}</em></span>` : ""}
${city ? `<span><b>City knowledge</b><em>${(city.knowledge?.farming || 0).toFixed(2)} / ${(city.knowledge?.metallurgy || 0).toFixed(2)}</em></span>` : ""}
${cityEthnicity ? `<span><b>City majority</b><em>E${cityEthnicity}</em></span>` : ""}
${city ? `<span><b>State</b><em>${polity ? `#${polity.id}` : "Independent"}</em></span>` : ""}
${city ? `<span><b>Loyalty</b><em>${city.loyalty.toFixed(2)}</em></span>` : ""}
${polity ? `<span><b>Treasury</b><em>${polity.treasury.toFixed(1)}</em></span>` : ""}
${polity ? `<span><b>Center</b><em>${polity.centerCityId === city.id ? "yes" : "no"}</em></span>` : ""}
${agent ? `<span><b>Ethnicity</b><em>E${agent.ethnicity}</em></span>` : ""}
${ethnicity ? `<span><b>Climate pref</b><em>${ethnicity.climateTemp.toFixed(2)} / ${ethnicity.climateHumidity.toFixed(2)}</em></span>` : ""}
${agent ? `<span><b>Climate mismatch</b><em>${mismatch.toFixed(2)}</em></span>` : ""}
${agent ? `<span><b>Sedentary</b><em>${getSedentary(agent.traits).toFixed(2)}</em></span>` : ""}
${agent ? `<span><b>Ethnocentrism</b><em>${getEthnocentrism(agent.traits).toFixed(2)}</em></span>` : ""}
${agent ? `<span><b>Farming</b><em>${(agent.tech?.farming || 0).toFixed(3)} / work ${(agent.farmingWork || 0).toFixed(2)}</em></span>` : ""}
${agent ? `<span><b>Metallurgy</b><em>${(agent.tech?.metallurgy || 0).toFixed(3)}</em></span>` : ""}
${agent ? `<span><b>Stored</b><em>${agent.resources.toFixed(1)}</em></span>` : ""}
${ethnicity ? `<span><b>Lineage pop</b><em>${ethnicity.population}</em></span>` : ""}
`;
els.tooltip.hidden = false;
const margin = 8;
const width = els.tooltip.offsetWidth;
const height = els.tooltip.offsetHeight;
let left = hoverState.left;
let top = hoverState.top;
if (left + width + margin > hoverState.width) left = hoverState.left - width - 32;
if (top + height + margin > hoverState.height) top = hoverState.height - height - margin;
els.tooltip.style.left = `${clamp(left, margin, Math.max(margin, hoverState.width - width - margin))}px`;
els.tooltip.style.top = `${clamp(top, margin, Math.max(margin, hoverState.height - height - margin))}px`;
}
function hideTooltip() {
hoverState = null;
els.tooltip.hidden = true;
}
function findAgentAt(x, y) {
const directIndex = sim.world.idx(x, y);
const directCounts = sim.tileEthnicities.get(directIndex);
if (!directCounts) return null;
for (const a of sim.agents) {
if (a.x === x && a.y === y) return a;
}
return null;
}
function waterInfluenceAt(world, x, y) {
let score = 0;
for (let dy = -3; dy <= 3; dy++) {
for (let dx = -3; dx <= 3; dx++) {
const d = Math.abs(dx) + Math.abs(dy);
if (!d || d > 3) continue;
const tx = x + dx;
const ty = y + dy;
if (tx < 0 || ty < 0 || tx >= world.size || ty >= world.size) continue;
if (world.terrain[world.idx(tx, ty)] === Terrain.WATER) score += (4 - d) / 4;
}
}
return score;
}
function updateStats(force = false) {
const now = performance.now();
if (!force && now - lastStatsAt < SimConfig.render.statsThrottleMs) return;
lastStatsAt = now;
const livingEthnicities = [...sim.ethnicities.values()].filter(e => e.population > 0);
const urbanPopulation = sim.cities.reduce((sum, c) => sum + c.population, 0);
let farmingTotal = 0;
let metallurgyTotal = 0;
let farmingHolders = 0;
let metallurgyHolders = 0;
for (const a of sim.agents) {
sim.ensureAgentTech(a);
farmingTotal += a.tech.farming || 0;
metallurgyTotal += a.tech.metallurgy || 0;
if ((a.tech.farming || 0) > 0.02) farmingHolders++;
if ((a.tech.metallurgy || 0) > 0.02) metallurgyHolders++;
}
const agentCount = Math.max(1, sim.agents.length);
els.year.textContent = formatSimDate(sim.year);
els.activeGroups.textContent = sim.agents.length.toLocaleString();
els.urbanPopulation.textContent = Math.floor(urbanPopulation).toLocaleString();
els.ethnicities.textContent = livingEthnicities.length.toLocaleString();
els.cities.textContent = sim.cities.length.toLocaleString();
els.polities.textContent = sim.polities.length.toLocaleString();
els.routes.textContent = sim.tradeLinks.length.toLocaleString();
els.farmingKnowledge.textContent = `${(farmingTotal / agentCount).toFixed(4)} (${farmingHolders})`;
els.metallurgyKnowledge.textContent = `${(metallurgyTotal / agentCount).toFixed(4)} (${metallurgyHolders})`;
els.deaths.textContent = sim.deaths.toLocaleString();
const top = livingEthnicities.sort((a, b) => b.population - a.population).slice(0, 9);
els.ethnicityList.innerHTML = top.map(e => (
`<li><span class="lineage-chip" style="background: rgb(${e.color.join(",")})"></span>` +
`E${e.id} pop ${e.population.toLocaleString()} div ${e.diversity.toFixed(2)} parent ${e.parent || "-"}</li>`
)).join("");
maybeRenderStateGraph();
}
function setLegend() {
const mode = els.viewMode.value;
if (mode === "terrain") {
els.legend.innerHTML = terrainInfo.map(t => `<span><i style="background: rgb(${t.color.join(",")})"></i>${t.name}</span>`).join("");
} else if (mode === "ethnicity") {
els.legend.innerHTML = "<span>Color = dominant regional lineage. Mixed tiles brighten toward gray.</span>";
} else if (mode === "pressure") {
els.legend.innerHTML = "<span><i style=\"background:#d24943\"></i>High local population pressure</span>";
} else if (mode === "cities") {
els.legend.innerHTML = "<span><i style=\"background:#e2c679\"></i>City nodes, farmland radius, and stored urban population</span>";
} else if (mode === "polities") {
els.legend.innerHTML = "<span>Color = city-centered state. Uncolored cities are independent.</span>";
} else if (mode === "technology") {
els.legend.innerHTML = "<span><i style=\"background:#5fab5b\"></i>Farming knowledge</span><span><i style=\"background:#caa966\"></i>Metallurgy knowledge</span>";
} else {
els.legend.innerHTML = "<span><i style=\"background:#6bbc55\"></i>Regenerating local resource stock</span>";
}
}
function maybeRenderStateGraph() {
const now = performance.now();
if (now - lastGraphRenderAt < SimConfig.render.graphThrottleMs) return;
lastGraphRenderAt = now;
renderStateGraph();
}
function renderStateGraph() {
const canvas = els.stateGraph;
if (!canvas || !sim) return;
const rect = canvas.getBoundingClientRect();
if (!rect.width || !rect.height) return;
const dpr = window.devicePixelRatio || 1;
const width = Math.max(1, Math.floor(rect.width * dpr));
const height = Math.max(1, Math.floor(rect.height * dpr));
if (canvas.width !== width || canvas.height !== height) {
canvas.width = width;
canvas.height = height;
}
const g = canvas.getContext("2d");
g.setTransform(dpr, 0, 0, dpr, 0, 0);
const w = rect.width;
const h = rect.height;
g.clearRect(0, 0, w, h);
g.fillStyle = "#101315";
g.fillRect(0, 0, w, h);
const historyWindowStart = Math.max(0, sim.year - years(10000));
const histories = sim.getAllPolityHistories()
.filter(history => (history.ended ?? sim.year) >= historyWindowStart)
.map(history => ({
...history,
visibleFounded: Math.max(history.founded ?? 0, historyWindowStart),
visibleSamples: (history.samples || []).filter(sample => sample.year >= historyWindowStart)
}))
.filter(history => history.visibleSamples.length > 0 || (history.ended ?? sim.year) >= historyWindowStart);
if (!histories.length) {
if (els.historyRange) els.historyRange.textContent = "-";
g.fillStyle = "#7f8984";
g.font = "12px ui-sans-serif, system-ui, sans-serif";
g.fillText("No state history yet", 16, 28);
return;
}
let minYear = Infinity;
let maxYear = sim.year;
for (const history of histories) {
minYear = Math.min(minYear, history.visibleFounded);
maxYear = Math.max(maxYear, history.ended ?? 0, history.visibleSamples[history.visibleSamples.length - 1]?.year ?? 0);
}
if (!Number.isFinite(minYear)) minYear = 0;
if (els.historyRange) els.historyRange.textContent = `${formatGraphYear(minYear)}-${formatGraphYear(maxYear)}`;
const paddingLeft = 34;
const paddingRight = 10;
const paddingTop = 18;
const paddingBottom = 22;
const plotWidth = Math.max(1, w - paddingLeft - paddingRight);
const rowHeight = 22;
const maxRows = Math.max(1, Math.floor((h - paddingTop - paddingBottom) / rowHeight));
const selected = histories
.map(history => {
const samples = history.visibleSamples.length ? history.visibleSamples : [{ population: 0, cities: 0 }];
const peakPopulation = Math.max(...samples.map(sample => sample.population || 0));
const peakCities = Math.max(...samples.map(sample => sample.cities || 0));
const lifespan = (history.ended ?? sim.year) - history.visibleFounded;
return { history, peakPopulation, peakCities, importance: peakPopulation + peakCities * 100 + lifespan * 2 };
})
.sort((a, b) => b.importance - a.importance)
.slice(0, maxRows)
.sort((a, b) => (a.history.founded - b.history.founded) || (a.history.id - b.history.id));
const yearToX = year => paddingLeft + ((year - minYear) / Math.max(1, maxYear - minYear)) * plotWidth;
const axisY = h - paddingBottom + 4;
g.strokeStyle = "rgba(168, 177, 170, 0.22)";
g.lineWidth = 1;
g.beginPath();
g.moveTo(paddingLeft, axisY);
g.lineTo(w - paddingRight, axisY);
g.stroke();
g.fillStyle = "#8c9690";
g.font = "10px ui-sans-serif, system-ui, sans-serif";
g.textAlign = "left";
g.fillText(formatGraphYear(minYear), paddingLeft, h - 5);
g.textAlign = "center";
g.fillText(formatGraphYear((minYear + maxYear) / 2), paddingLeft + plotWidth / 2, h - 5);
g.textAlign = "right";
g.fillText(formatGraphYear(maxYear), w - paddingRight, h - 5);
selected.forEach((item, rowIndex) => {
const history = item.history;
const y = paddingTop + rowIndex * rowHeight + rowHeight * 0.5;
const color = history.color || hslToRgb((history.id * 0.38196601125) % 1, 0.58, 0.62);
const startX = yearToX(history.visibleFounded);
const endX = yearToX(history.ended ?? sim.year);
const alpha = history.active ? 0.82 : 0.4;
const thickness = clamp(2 + Math.sqrt(item.peakCities) * 1.2, 2, 9);
g.textAlign = "left";
g.fillStyle = "#a8b1aa";
g.font = "10px ui-sans-serif, system-ui, sans-serif";
g.fillText(`S${history.id}`, 4, y + 3);
g.strokeStyle = "rgba(168, 177, 170, 0.12)";
g.lineWidth = 1;
g.beginPath();
g.moveTo(paddingLeft, y);
g.lineTo(w - paddingRight, y);
g.stroke();
g.strokeStyle = `rgba(${color[0]}, ${color[1]}, ${color[2]}, ${alpha})`;
g.lineWidth = thickness;
g.lineCap = "round";
g.beginPath();
g.moveTo(startX, y);
g.lineTo(endX, y);
g.stroke();
g.lineCap = "butt";
for (const sample of history.visibleSamples) {
const sx = yearToX(sample.year);
const radius = clamp(1 + Math.sqrt(sample.cities || 0) * 0.5, 1.5, 4);
const opacity = 0.35 + clamp(sample.avgLoyalty ?? 0.5, 0, 1) * 0.55;
g.fillStyle = `rgba(${color[0]}, ${color[1]}, ${color[2]}, ${opacity})`;
g.beginPath();
g.arc(sx, y, radius, 0, Math.PI * 2);
g.fill();
}
if (history.ended !== null) {
g.strokeStyle = "rgba(224, 105, 94, 0.8)";
g.lineWidth = 1.5;
g.beginPath();
g.moveTo(endX - 3, y - 3);
g.lineTo(endX + 3, y + 3);
g.moveTo(endX + 3, y - 3);
g.lineTo(endX - 3, y + 3);
g.stroke();
} else {
g.fillStyle = `rgb(${color[0]}, ${color[1]}, ${color[2]})`;
g.beginPath();
g.arc(endX, y, 3, 0, Math.PI * 2);
g.fill();
}
});
}
function formatGraphYear(month) {
return `${Math.floor(month / MONTHS_PER_YEAR).toLocaleString()}y`;
}
function formatSimDate(month) {
const year = Math.floor(month / MONTHS_PER_YEAR);
const monthOfYear = month % MONTHS_PER_YEAR + 1;
return `${year.toLocaleString()}y ${monthOfYear}m`;
}
function loop() {
const steps = running ? Number(els.speed.value) : 0;
for (let i = 0; i < steps; i++) sim.step();
if (running || frame % 8 === 0) {
render();
updateStats();
renderTooltip();
}
frame++;
requestAnimationFrame(loop);
}
function reset() {
const size = Number(els.worldSize.value);
const count = Number(els.agentCount.value);
els.canvas.width = size;
els.canvas.height = size;
renderImage = null;
renderImageSize = 0;
sim = new Simulation(size, count);
setLegend();
render();
updateStats(true);
renderStateGraph();
}
function clamp(v, min, max) {
return Math.max(min, Math.min(max, v));
}
function smoothstep(t) {
return t * t * (3 - 2 * t);
}
function getSedentary(traits) {
return traits.sedentary;
}
function getEthnocentrism(traits) {
return traits.ethnocentrism;
}
function mutateTraits(traits, rng, amount) {
return {
mobility: clamp(traits.mobility + rng.range(-amount, amount), 0.02, 1),
resourceAttraction: clamp(traits.resourceAttraction + rng.range(-amount, amount), 0.05, 1.2),
assimilation: clamp(traits.assimilation + rng.range(-amount, amount), 0, 0.75),
ethnocentrism: clamp(getEthnocentrism(traits) + rng.range(-amount, amount), 0, 1.2),
reproductionThreshold: clamp(traits.reproductionThreshold + rng.range(-amount * 16, amount * 16), 12, 48),
sedentary: clamp(getSedentary(traits) + rng.range(-amount, amount), 0, 1)
};
}
function blendTraits(a, b, t) {
return {
mobility: lerp(a.mobility, b.mobility, t),
resourceAttraction: lerp(a.resourceAttraction, b.resourceAttraction, t),
assimilation: lerp(a.assimilation, b.assimilation, t),
ethnocentrism: lerp(getEthnocentrism(a), getEthnocentrism(b), t),
reproductionThreshold: lerp(a.reproductionThreshold, b.reproductionThreshold, t),
sedentary: lerp(getSedentary(a), getSedentary(b), t)
};
}
function emptyTraitSums() {
return { mobility: 0, resourceAttraction: 0, assimilation: 0, ethnocentrism: 0, reproductionThreshold: 0, sedentary: 0 };
}
function addTraits(sum, traits) {
sum.mobility += traits.mobility;
sum.resourceAttraction += traits.resourceAttraction;
sum.assimilation += traits.assimilation;
sum.ethnocentrism += getEthnocentrism(traits);
sum.reproductionThreshold += traits.reproductionThreshold / 48;
sum.sedentary += getSedentary(traits);
}
function averageTraits(sum, count) {
return {
mobility: sum.mobility / count,
resourceAttraction: sum.resourceAttraction / count,
assimilation: sum.assimilation / count,
ethnocentrism: sum.ethnocentrism / count,
reproductionThreshold: (sum.reproductionThreshold / count) * 48,
sedentary: sum.sedentary / count
};
}
function addBirthsToComposition(composition, births) {
const dominant = dominantComposition(composition);
if (!dominant) return;
composition.set(dominant, (composition.get(dominant) || 0) + births);
}
function removeFromComposition(composition, loss) {
let remaining = loss;
const total = [...composition.values()].reduce((sum, value) => sum + value, 0);
if (!total) return;
for (const [id, count] of [...composition]) {
const removed = Math.min(count, Math.ceil(loss * (count / total)));
composition.set(id, Math.max(0, count - removed));
remaining -= removed;
if (composition.get(id) <= 0) composition.delete(id);
if (remaining <= 0) break;
}
}
function dominantComposition(composition) {
let bestId = null;
let bestCount = 0;
for (const [id, count] of composition) {
if (count > bestCount) {
bestId = id;
bestCount = count;
}
}
return bestId;
}
function traitDistance(a, b) {
return Math.abs(a.mobility - b.mobility) +
Math.abs(a.resourceAttraction - b.resourceAttraction) +
Math.abs(a.assimilation - b.assimilation) +
Math.abs(getEthnocentrism(a) - getEthnocentrism(b)) +
Math.abs(a.reproductionThreshold - b.reproductionThreshold) / 48 +
Math.abs(getSedentary(a) - getSedentary(b));
}
function hslToRgb(h, s, l) {
const hue = (p, q, t) => {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
};
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
return [
Math.round(hue(p, q, h + 1 / 3) * 255),
Math.round(hue(p, q, h) * 255),
Math.round(hue(p, q, h - 1 / 3) * 255)
];
}
function mix(a, b, t) {
return [
Math.round(lerp(a[0], b[0], t)),
Math.round(lerp(a[1], b[1], t)),
Math.round(lerp(a[2], b[2], t))
];
}
function lerp(a, b, t) {
return a + (b - a) * t;
}
function packArray(typedArray) {
const bytes = new Uint8Array(typedArray.buffer);
let binary = "";
const chunkSize = 8192;
for (let i = 0; i < bytes.length; i += chunkSize) {
binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
}
return {
type: typedArray.constructor.name,
length: typedArray.length,
data: btoa(binary)
};
}
function unpackArray(payload, TypedArray) {
const binary = atob(payload.data);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
return new TypedArray(bytes.buffer, 0, payload.length);
}
const Persistence = Object.freeze({
save(currentSim) {
const raw = JSON.stringify(currentSim);
if (raw.length > SimConfig.save.maxBytes) {
throw new Error(`Save is too large (${raw.length.toLocaleString()} bytes)`);
}
localStorage.setItem(SimConfig.save.key, raw);
},
load() {
const raw = localStorage.getItem(SimConfig.save.key);
if (!raw) return null;
if (raw.length > SimConfig.save.maxBytes * 1.25) {
throw new Error("Saved world is too large to load safely");
}
const state = JSON.parse(raw);
if (!state || !state.world || !state.agents) throw new Error("Saved world is missing required fields");
return Simulation.fromJSON(state);
},
clear() {
localStorage.removeItem(SimConfig.save.key);
}
});
function saveWorld() {
try {
Persistence.save(sim);
} catch (error) {
console.warn("Save failed", error);
}
}
function loadWorld() {
try {
const loaded = Persistence.load();
if (!loaded) return;
sim = loaded;
els.worldSize.value = String(sim.world.size);
els.canvas.width = sim.world.size;
els.canvas.height = sim.world.size;
renderImage = null;
renderImageSize = 0;
setLegend();
render();
updateStats(true);
renderStateGraph();
} catch (error) {
console.warn("Load failed", error);
}
}
els.toggleRun.addEventListener("click", () => {
running = !running;
els.toggleRun.textContent = running ? "Pause" : "Run";
});
els.stepOnce.addEventListener("click", () => {
sim.step();
render();
updateStats(true);
renderStateGraph();
renderTooltip();
});
els.resetWorld.addEventListener("click", reset);
els.saveWorld.addEventListener("click", saveWorld);
els.loadWorld.addEventListener("click", loadWorld);
els.clearSave.addEventListener("click", () => Persistence.clear());
els.worldSize.addEventListener("change", reset);
els.agentCount.addEventListener("change", reset);
els.viewMode.addEventListener("change", () => {
setLegend();
render();
});
els.canvas.addEventListener("mousemove", showTooltip);
els.canvas.addEventListener("mouseleave", hideTooltip);
reset();
loop();