diff --git a/main.js b/main.js index 8693ad9a..e9444720 100644 --- a/main.js +++ b/main.js @@ -96,6 +96,47 @@ fogging.append("rect").attr("x", 0).attr("y", 0).attr("width", "100%").attr("hei scaleBar.on("mousemove", () => tip("Click to open Units Editor")); legend.on("mousemove", () => tip("Drag to change the position. Click to hide the legend")).on("click", () => clearLegend()); +class Icons { + constructor(iconList = {}, density = 0){ + this.density = density; + this.probability = []; + + for(var item in iconList) { + if ( iconList.hasOwnProperty(item) ) { // Safety + for( var i=0; i {e.id = i;}); + + const MARINE = 0; + const PERMAFROST = 11; + const WETLANDS = 12; + const biomesMartix = [ // hot ↔ cold; dry ↕ wet new Uint8Array([1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]), @@ -302,16 +360,7 @@ function applyDefaultBiomesSystem() { new Uint8Array([7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,10,10,10]) ]; - // parse icons weighted array into a simple array - for (let i=0; i < icons.length; i++) { - const parsed = []; - for (const icon in icons[i]) { - for (let j = 0; j < icons[i][icon]; j++) {parsed.push(icon);} - } - icons[i] = parsed; - } - - return {i:d3.range(0, name.length), name, color, biomesMartix, habitability, iconsDensity, icons, cost}; + return {biomeList, biomesMartix}; } function showWelcomeMessage() { @@ -1110,7 +1159,7 @@ function rankCells() { const areaMean = d3.mean(cells.area); // to adjust population by cell area for (const i of cells.i) { - let s = +biomesData.habitability[cells.biome[i]]; // base suitability derived from biome habitability + let s = +biomesData.biomeList[cells.biome[i]].habitability; // base suitability derived from biome habitability if (!s) continue; // uninhabitable biomes has 0 suitability s += normalize(cells.fl[i] + cells.conf[i], flMean, flMax) * 250; // big rivers and confluences are valued s -= (cells.h[i] - 50) / 5; // low elevation is valued, high is not; diff --git a/modules/burgs-and-states.js b/modules/burgs-and-states.js index 2d628a16..8e136b4c 100644 --- a/modules/burgs-and-states.js +++ b/modules/burgs-and-states.js @@ -293,9 +293,9 @@ function getBiomeCost(b, biome, type) { if (b === biome) return 10; // tiny penalty for native biome - if (type === "Hunting") return biomesData.cost[biome] * 2; // non-native biome penalty for hunters - if (type === "Nomadic" && biome > 4 && biome < 10) return biomesData.cost[biome] * 3; // forest biome penalty for nomads - return biomesData.cost[biome]; // general non-native biome penalty + if (type === "Hunting") return biomesData.biomeList[biome].cost * 2; // non-native biome penalty for hunters + if (type === "Nomadic" && biome > 4 && biome < 10) return biomesData.biomeList[biome].cost * 3; // forest biome penalty for nomads + return biomesData.biomeList[biome].cost; // general non-native biome penalty } function getHeightCost(f, h, type) { diff --git a/modules/cultures-generator.js b/modules/cultures-generator.js index 386ccc62..5caf11c7 100644 --- a/modules/cultures-generator.js +++ b/modules/cultures-generator.js @@ -392,9 +392,9 @@ function getBiomeCost(c, biome, type) { if (cells.biome[pack.cultures[c].center] === biome) return 10; // tiny penalty for native biome - if (type === "Hunting") return biomesData.cost[biome] * 5; // non-native biome penalty for hunters - if (type === "Nomadic" && biome > 4 && biome < 10) return biomesData.cost[biome] * 10; // forest biome penalty for nomads - return biomesData.cost[biome] * 2; // general non-native biome penalty + if (type === "Hunting") return biomesData.biomeList[biome].cost * 5; // non-native biome penalty for hunters + if (type === "Nomadic" && biome > 4 && biome < 10) return biomesData.biomeList[biome].cost * 10; // forest biome penalty for nomads + return biomesData.biomeList[biome].cost * 2; // general non-native biome penalty } function getHeightCost(i, h, type) { diff --git a/modules/relief-icons.js b/modules/relief-icons.js index bc73a389..238eff6d 100644 --- a/modules/relief-icons.js +++ b/modules/relief-icons.js @@ -18,7 +18,7 @@ if (height < 20) continue; // no icons on water if (cells.r[i]) continue; // no icons on rivers const b = cells.biome[i]; - if (height < 50 && biomesData.iconsDensity[b] === 0) continue; // no icons for this biome + if (height < 50 && biomesData.biomeList[b].icons.density === 0) continue; // no icons for this biome const polygon = getPackPolygon(i); const x = d3.extent(polygon, p => p[0]), y = d3.extent(polygon, p => p[1]); const e = [Math.ceil(x[0]), Math.ceil(y[0]), Math.floor(x[1]), Math.floor(y[1])]; // polygon box @@ -26,14 +26,14 @@ if (height < 50) placeBiomeIcons(i, b); else placeReliefIcons(i); function placeBiomeIcons() { - const iconsDensity = biomesData.iconsDensity[b] / 100; + const iconsDensity = biomesData.biomeList[b].icons.density / 100; const radius = 2 / iconsDensity / density; if (Math.random() > iconsDensity * 10) return; for (const [cx, cy] of poissonDiscSampler(e[0], e[1], e[2], e[3], radius)) { if (!d3.polygonContains(polygon, [cx, cy])) continue; let h = rn((4 + Math.random()) * size, 2); - const icon = getBiomeIcon(i, biomesData.icons[b]); + const icon = getBiomeIcon(i, biomesData.biomeList[b].icons.probability); if (icon === "#relief-grass-1") h *= 1.3; relief.push({i: icon, x: rn(cx-h, 2), y: rn(cy-h, 2), s: h*2}); } diff --git a/modules/religions-generator.js b/modules/religions-generator.js index 6308b83b..66c2d559 100644 --- a/modules/religions-generator.js +++ b/modules/religions-generator.js @@ -212,7 +212,7 @@ const cultureCost = c !== cells.culture[e] ? 10 : 0; const stateCost = s !== cells.state[e] ? 10 : 0; - const biomeCost = cells.road[e] ? 1 : biomesData.cost[cells.biome[e]]; + const biomeCost = cells.road[e] ? 1 : biomesData.biomeList[cells.biome[e]].cost; const populationCost = Math.max(rn(popCost - cells.pop[e]), 0); const heightCost = Math.max(cells.h[e], 20) - 20; const waterCost = cells.h[e] < 20 ? cells.road[e] ? 50 : 1000 : 0; @@ -248,7 +248,7 @@ cells.c[n].forEach(function(e) { const religionCost = cells.religion[e] === b ? 0 : 2000; - const biomeCost = cells.road[e] ? 0 : biomesData.cost[cells.biome[e]]; + const biomeCost = cells.road[e] ? 0 : biomesData.biomeList[cells.biome[e]].cost; const heightCost = Math.max(cells.h[e], 20) - 20; const waterCost = cells.h[e] < 20 ? cells.road[e] ? 50 : 1000 : 0; const totalCost = p + (religionCost + biomeCost + heightCost + waterCost) / Math.max(religions[r].expansionism, .1); diff --git a/modules/routes-generator.js b/modules/routes-generator.js index 47bdae05..8657ef13 100644 --- a/modules/routes-generator.js +++ b/modules/routes-generator.js @@ -173,7 +173,7 @@ for (const c of cells.c[n]) { if (cells.h[c] < 20) continue; // ignore water cells const stateChangeCost = cells.state && cells.state[c] !== cells.state[n] ? 400 : 0; // trails tend to lay within the same state - const habitedCost = Math.max(100 - biomesData.habitability[cells.biome[c]], 0); // routes tend to lay within populated areas + const habitedCost = Math.max(100 - biomesData.biomeList[cells.biome[c]].habitability, 0); // routes tend to lay within populated areas const heightChangeCost = Math.abs(cells.h[c] - cells.h[n]) * 10; // routes tend to avoid elevation changes const cellCoast = 10 + stateChangeCost + habitedCost + heightChangeCost; const totalCost = p + (cells.road[c] || cells.burg[c] ? cellCoast / 3 : cellCoast); diff --git a/modules/save-and-load.js b/modules/save-and-load.js index d8760d90..0c2a60bc 100644 --- a/modules/save-and-load.js +++ b/modules/save-and-load.js @@ -231,7 +231,8 @@ function getMapData() { temperaturePoleOutput.value, precOutput.value, JSON.stringify(winds), mapName.value].join("|"); const coords = JSON.stringify(mapCoordinates); - const biomes = [biomesData.color, biomesData.habitability, biomesData.name].join("|"); + const b = biomesData.biomeList + const biomes = [b.map(i => i.color), b.map(i => i.habitability), b.map(i => i.name)].join("|"); const notesData = JSON.stringify(notes); // set transform values to default @@ -583,18 +584,30 @@ function parseLoadedData(data) { if (data[4]) notes = JSON.parse(data[4]); const biomes = data[3].split("|"); - biomesData = applyDefaultBiomesSystem(); - biomesData.color = biomes[0].split(","); - biomesData.habitability = biomes[1].split(",").map(h => +h); - biomesData.name = biomes[2].split(","); + _biomesData = applyDefaultBiomesSystem(); - // push custom biomes if any - for (let i=biomesData.i.length; i < biomesData.name.length; i++) { - biomesData.i.push(biomesData.i.length); - biomesData.iconsDensity.push(0); - biomesData.icons.push([]); - biomesData.cost.push(50); + const colorsList = biomes[0].split(","); + const habitabilityList = biomes[1].split(",").map(h => +h); + const namesList = biomes[2].split(","); + + biomesData = []; + + for (let i = 0; i < namesList.length; i++){ + const name = namesList[i]; + const color = colorsList[i]; + const habitability = habitabilityList[i]; + let icons; + if (i < _biomesData.biomeList.length){ + icons = _biomesData.biomeList[i].icons; + cost = _biomesData.biomeList[i].cost; + } + else{ + icons = new Icons({}, 0); + cost = 50; + } + biomesData.push(new Biome(name, color, habitability, icons, cost)); } + }() void function replaceSVG() { @@ -811,9 +824,7 @@ function parseLoadedData(data) { }); // 1.0 added new biome - Wetland - biomesData.name.push("Wetland"); - biomesData.color.push("#0b9131"); - biomesData.habitability.push(12); + biomesData.biomeList.push("Wetland", "#0b9131", 12); } if (version < 1.1) { diff --git a/modules/ui/biomes-editor.js b/modules/ui/biomes-editor.js index f5b3f273..0d75c0c2 100644 --- a/modules/ui/biomes-editor.js +++ b/modules/ui/biomes-editor.js @@ -54,51 +54,46 @@ function editBiomes() { function biomesCollectStatistics() { const cells = pack.cells; - const array = new Uint8Array(biomesData.i.length); - biomesData.cells = Array.from(array); - biomesData.area = Array.from(array); - biomesData.rural = Array.from(array); - biomesData.urban = Array.from(array); + + biomesData.biomeList.forEach((biome) => { + biome.resetStatistics(); + }); for (const i of cells.i) { if (cells.h[i] < 20) continue; - const b = cells.biome[i]; - biomesData.cells[b] += 1; - biomesData.area[b] += cells.area[i]; - biomesData.rural[b] += cells.pop[i]; - if (cells.burg[i]) biomesData.urban[b] += pack.burgs[cells.burg[i]].population; + biomesData.biomeList[cells.biome[i]].addCell(i, cells); } } function biomesEditorAddLines() { const unit = areaUnit.value === "square" ? " " + distanceUnitInput.value + "²" : " " + areaUnit.value; - const b = biomesData; + const l = biomesData.biomeList; let lines = "", totalArea = 0, totalPopulation = 0;; - for (const i of b.i) { - if (!i || biomesData.name[i] === "removed") continue; // ignore water and removed biomes - const area = b.area[i] * distanceScaleInput.value ** 2; - const rural = b.rural[i] * populationRate.value; - const urban = b.urban[i] * populationRate.value * urbanization.value; + for (const b of l) { + if (!b.id || b.name === "removed") continue; // ignore water and removed biomes + const area = b.area * distanceScaleInput.value ** 2; + const rural = b.rural * populationRate.value; + const urban = b.urban * populationRate.value * urbanization.value; const population = rn(rural + urban); const populationTip = `Total population: ${si(population)}; Rural population: ${si(rural)}; Urban population: ${si(urban)}`; totalArea += area; totalPopulation += population; - lines += `
- - + lines += `
+ + % - + -
${b.cells[i]}
+
${b.cells}
${si(area) + unit}
${si(population)}
- ${i>12 && !b.cells[i] ? '' : ''} + ${b.id>12 && !b.cells ? '' : ''}
`; } body.innerHTML = lines; @@ -129,7 +124,7 @@ function editBiomes() { function biomeHighlightOff(event) { if (customization === 6) return; const biome = +event.target.dataset.id; - const color = biomesData.color[biome]; + const color = biomesData.biomeList[biome].color; biomes.select("#biome"+biome).transition().attr("stroke-width", .7).attr("stroke", color); } @@ -139,7 +134,7 @@ function editBiomes() { const callback = function(fill) { el.setAttribute("fill", fill); - biomesData.color[biome] = fill; + biomesData.biomeList[biome].color = fill; biomes.select("#biome"+biome).attr("fill", fill).attr("stroke", fill); } @@ -149,18 +144,18 @@ function editBiomes() { function biomeChangeName(el) { const biome = +el.parentNode.dataset.id; el.parentNode.dataset.name = el.value; - biomesData.name[biome] = el.value; + biomesData.biomeList[biome].name = el.value; } function biomeChangeHabitability(el) { const biome = +el.parentNode.dataset.id; const failed = isNaN(+el.value) || +el.value < 0 || +el.value > 9999; if (failed) { - el.value = biomesData.habitability[biome]; + el.value = biomesData.biomeList[biome].habitability; tip("Please provide a valid number in range 0-9999", false, "error"); return; } - biomesData.habitability[biome] = +el.value; + biomesData.biomeList[biome].habitability = +el.value; el.parentNode.dataset.habitability = el.value; recalculatePopulation(); refreshBiomesEditor(); @@ -191,7 +186,10 @@ function editBiomes() { function toggleLegend() { if (legend.selectAll("*").size()) {clearLegend(); return;}; // hide legend const d = biomesData; - const data = Array.from(d.i).filter(i => d.cells[i]).sort((a, b) => d.area[b] - d.area[a]).map(i => [i, d.color[i], d.name[i]]); + const data = Array.from(d.biomeList) //shallow copy existing array + .filter(i => i.cells) //remove biomes with 0 cells + .sort((a, b) => b.area - a.area) //sort by size + .map(i => [i.id, i.color, i.name]); //return index, color, and name drawLegend("Biomes", data); } @@ -214,28 +212,18 @@ function editBiomes() { } function addCustomBiome() { - const b = biomesData, i = biomesData.i.length; - b.i.push(i); - b.color.push(getRandomColor()); - b.habitability.push(50); - b.name.push("Custom"); - b.iconsDensity.push(0); - b.icons.push([]); - b.cost.push(50); - - b.rural.push(0); - b.urban.push(0); - b.cells.push(0); - b.area.push(0); + const b = biomesData.biomeList, i = b.length; + b.push(new Biome("Custom", getRandomColor(), 50)) + b[i].id = i; //don't forget the ID! const unit = areaUnit.value === "square" ? " " + distanceUnitInput.value + "²" : " " + areaUnit.value; - const line = `
- - + const line = `
+ + % - + -
${b.cells[i]}
+
${b[i].cells}
0 ${unit}
@@ -251,7 +239,7 @@ function editBiomes() { function removeCustomBiome(el) { const biome = +el.parentNode.dataset.id; el.parentNode.remove(); - biomesData.name[biome] = "removed"; + biomesData.biomeList[biome].name = "removed"; biomesFooterBiomes.innerHTML = +biomesFooterBiomes.innerHTML - 1; } @@ -337,7 +325,7 @@ function editBiomes() { const selected = body.querySelector("div.selected"); const biomeNew = selected.dataset.id; - const color = biomesData.color[biomeNew]; + const color = biomesData.biomeList[biomeNew].color; selection.forEach(function(i) { const exists = temp.select("polygon[data-cell='"+i+"']"); diff --git a/modules/ui/general.js b/modules/ui/general.js index b5c34aba..61714fe4 100644 --- a/modules/ui/general.js +++ b/modules/ui/general.js @@ -104,7 +104,7 @@ function showMapTooltip(point, e, i, g) { if (layerIsOn("togglePrec") && land) tip("Annual Precipitation: "+ getFriendlyPrecipitation(i)); else if (layerIsOn("togglePopulation")) tip(getPopulationTip(i)); else if (layerIsOn("toggleTemp")) tip("Temperature: " + convertTemperature(grid.cells.temp[g])); else - if (layerIsOn("toggleBiomes") && pack.cells.biome[i]) tip("Biome: " + biomesData.name[pack.cells.biome[i]]); else + if (layerIsOn("toggleBiomes") && pack.cells.biome[i]) tip("Biome: " + biomesData.biomeList[pack.cells.biome[i]].name); else if (layerIsOn("toggleReligions") && pack.cells.religion[i]) { const religion = pack.religions[pack.cells.religion[i]]; const type = religion.type === "Cult" || religion.type == "Heresy" ? religion.type : religion.type + " religion"; @@ -146,7 +146,7 @@ function updateCellInfo(point, i, g) { infoBurg.innerHTML = cells.burg[i] ? pack.burgs[cells.burg[i]].name + " (" + cells.burg[i] + ")" : "no"; const f = cells.f[i]; infoFeature.innerHTML = f ? pack.features[f].group + " (" + f + ")" : "n/a"; - infoBiome.innerHTML = biomesData.name[cells.biome[i]]; + infoBiome.innerHTML = biomesData.biomeList[cells.biome[i]].name; } // get user-friendly (real-world) height value from map data diff --git a/modules/ui/layers.js b/modules/ui/layers.js index 27201082..7c232035 100644 --- a/modules/ui/layers.js +++ b/modules/ui/layers.js @@ -333,7 +333,7 @@ function drawBiomes() { biomes.selectAll("path").remove(); const cells = pack.cells, vertices = pack.vertices, n = cells.i.length; const used = new Uint8Array(cells.i.length); - const paths = new Array(biomesData.i.length).fill(""); + const paths = new Array(biomesData.biomeList.length).fill(""); for (const i of cells.i) { if (!cells.biome[i]) continue; // no need to mark water @@ -350,7 +350,7 @@ function drawBiomes() { paths.forEach(function(d, i) { if (d.length < 10) return; - biomes.append("path").attr("d", d).attr("fill", biomesData.color[i]).attr("stroke", biomesData.color[i]).attr("id", "biome"+i); + biomes.append("path").attr("d", d).attr("fill", biomesData.biomeList[i].color).attr("stroke", biomesData.biomeList[i].color).attr("id", "biome"+i); }); // connect vertices to chain