changed biome to use pack instead of its own variable

This commit is contained in:
Zoltan 2019-11-12 10:56:50 -08:00
parent 23c9538b79
commit fe51098b16
10 changed files with 39 additions and 41 deletions

14
main.js
View file

@ -144,7 +144,7 @@ let seed, mapHistory = [], elSelected, modules = {}, notes = [];
let customization = 0; // 0 - no; 1 = heightmap draw; 2 - states draw; 3 - add state/burg; 4 - cultures draw
let mapCoordinates = {}; // map coordinates on globe
let winds = [225, 45, 225, 315, 135, 315]; // default wind directions
let biomesData = applyDefaultBiomesSystem();
applyDefaultBiomesSystem();
let nameBases = Names.getNameBases(); // cultures-related data
const fonts = ["Almendra+SC", "Georgia", "Arial", "Times+New+Roman", "Comic+Sans+MS", "Lucida+Sans+Unicode", "Courier+New"]; // default web-safe fonts
@ -328,7 +328,7 @@ function findBurgForMFCG(params) {
// apply default biomes data
function applyDefaultBiomesSystem() {
const biomeList = [
pack.biomes = [
new Biome ("Marine", "#53679f", 0, new Icons({}, 0), 10),
new Biome ("Hot desert", "#fbe79f", 2, new Icons({dune:3, cactus:6, deadTree:1}, 3), 200),
new Biome ("Cold desert", "#b5b887", 5, new Icons({dune:9, deadTree:1}, 2), 150),
@ -345,13 +345,13 @@ function applyDefaultBiomesSystem() {
];
//it is occasionally useful to have the "ID" be in the object
biomeList.forEach((e, i) => {e.id = i;});
pack.biomes.forEach((e, i) => {e.id = i;});
const MARINE = 0;
const PERMAFROST = 11;
const WETLANDS = 12;
const biomesMartix = [
pack.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]),
new Uint8Array([3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,9,9,9,9,9,10,10]),
@ -359,8 +359,6 @@ function applyDefaultBiomesSystem() {
new Uint8Array([5,6,6,6,6,6,6,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,10,10,10]),
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])
];
return {biomeList, biomesMartix};
}
function showWelcomeMessage() {
@ -1145,7 +1143,7 @@ function getBiomeId(moisture, temperature, height) {
if (moisture > 40 && height < 25 || moisture > 24 && height > 24) return 12; // wetland biome
const m = Math.min(moisture / 5 | 0, 4); // moisture band from 0 to 4
const t = Math.min(Math.max(20 - temperature, 0), 25); // temparature band from 0 to 25
return biomesData.biomesMartix[m][t];
return pack.biomesMartix[m][t];
}
// assess cells suitability to calculate population and rand cells for culture center and burgs placement
@ -1159,7 +1157,7 @@ function rankCells() {
const areaMean = d3.mean(cells.area); // to adjust population by cell area
for (const i of cells.i) {
let s = +biomesData.biomeList[cells.biome[i]].habitability; // base suitability derived from biome habitability
let s = +pack.biomes[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;

View file

@ -292,7 +292,7 @@
burgs.filter(b => b.i && !b.removed).forEach(b => b.state = cells.state[b.cell]); // assign state to burgs
function getBiomeCost(b, biome, type) {
const bio = biomesData.biomeList[biome];
const bio = pack.biomes[biome];
if (b === biome) return 10; // tiny penalty for native biome
if (type === "Hunting") return bio.cost * 2; // non-native biome penalty for hunters
if (type === "Nomadic" && biome > 4 && biome < 10) return bio.cost * 3; // forest biome penalty for nomads

View file

@ -391,7 +391,7 @@
}
function getBiomeCost(c, biome, type) {
const b = biomesData.biomeList[biome];
const b = pack.biomes[biome];
if (cells.biome[pack.cultures[c].center] === biome) return 10; // tiny penalty for native biome
if (type === "Hunting") return b.cost * 5; // non-native biome penalty for hunters
if (type === "Nomadic" && biome > 4 && biome < 10) return b.cost * 10; // forest biome penalty for nomads

View file

@ -17,7 +17,7 @@
const height = cells.h[i];
if (height < 20) continue; // no icons on water
if (cells.r[i]) continue; // no icons on rivers
const b = biomesData.biomeList[cells.biome[i]];
const b = pack.biomes[cells.biome[i]];
if (height < 50 && 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]);

View file

@ -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.biomeList[cells.biome[e]].cost;
const biomeCost = cells.road[e] ? 1 : pack.biomes[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.biomeList[cells.biome[e]].cost;
const biomeCost = cells.road[e] ? 0 : pack.biomes[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);

View file

@ -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.biomeList[cells.biome[c]].habitability, 0); // routes tend to lay within populated areas
const habitedCost = Math.max(100 - pack.biomes[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);

View file

@ -231,7 +231,7 @@ function getMapData() {
temperaturePoleOutput.value, precOutput.value, JSON.stringify(winds),
mapName.value].join("|");
const coords = JSON.stringify(mapCoordinates);
const b = biomesData.biomeList
const b = pack.biomes
const biomes = [b.map(i => i.color), b.map(i => i.habitability), b.map(i => i.name)].join("|");
const notesData = JSON.stringify(notes);
@ -588,8 +588,8 @@ function parseLoadedData(data) {
const habitabilityList = biomes[1].split(",").map(h => +h);
const namesList = biomes[2].split(",");
biomesData = applyDefaultBiomesSystem();
const initialBiomes = biomesData.biomeList.length;
applyDefaultBiomesSystem();
const initialBiomes = pack.biomes.length;
for (let i = 0; i < namesList.length; i++){
const name = namesList[i];
@ -597,13 +597,13 @@ function parseLoadedData(data) {
const habitability = habitabilityList[i];
let icons;
if (i < initialBiomes){
biomesData.biomeList[i].name = namesList[i];
biomesData.biomeList[i].color = colorsList[i];
biomesData.biomeList[i].habitability = habitabilityList[i];
pack.biomes[i].name = namesList[i];
pack.biomes[i].color = colorsList[i];
pack.biomes[i].habitability = habitabilityList[i];
}
else{
biomesData.biomeList.push(new Biome(name, color, habitability));
biomesData.biomeList[i].id = i; //don't forget the id!
pack.biomes.push(new Biome(name, color, habitability));
pack.biomes[i].id = i; //don't forget the id!
}
}
}()
@ -822,7 +822,8 @@ function parseLoadedData(data) {
});
// 1.0 added new biome - Wetland
biomesData.biomeList.push("Wetland", "#0b9131", 12);
pack.biomes.push(new Biome("Wetland", "#0b9131", 12, new Icons({swamp:1}, 150), 150));
pack.biomes[12].id = 12;
}
if (version < 1.1) {

View file

@ -55,19 +55,19 @@ function editBiomes() {
function biomesCollectStatistics() {
const cells = pack.cells;
biomesData.biomeList.forEach((biome) => {
pack.biomes.forEach((biome) => {
biome.resetStatistics();
});
for (const i of cells.i) {
if (cells.h[i] < 20) continue;
biomesData.biomeList[cells.biome[i]].addCell(i, cells);
pack.biomes[cells.biome[i]].addCell(i, cells);
}
}
function biomesEditorAddLines() {
const unit = areaUnit.value === "square" ? " " + distanceUnitInput.value + "²" : " " + areaUnit.value;
const l = biomesData.biomeList;
const l = pack.biomes;
let lines = "", totalArea = 0, totalPopulation = 0;;
for (const b of l) {
@ -117,19 +117,19 @@ function editBiomes() {
function biomeHighlightOn(event) {
if (customization === 6) return;
const biome = biomesData.biomeList[+event.target.dataset.id];
const biome = pack.biomes[+event.target.dataset.id];
biomes.select("#biome"+biome.id).raise().transition(animate).attr("stroke-width", 2).attr("stroke", "#cd4c11");
}
function biomeHighlightOff(event) {
if (customization === 6) return;
const biome = biomesData.biomeList[+event.target.dataset.id];
const biome = pack.biomes[+event.target.dataset.id];
biomes.select("#biome"+biome.id).transition().attr("stroke-width", .7).attr("stroke", biome.color);
}
function biomeChangeColor(el) {
const currentFill = el.getAttribute("fill");
const biome = biomesData.biomeList[+el.parentNode.parentNode.dataset.id];
const biome = pack.biomes[+el.parentNode.parentNode.dataset.id];
const callback = function(fill) {
el.setAttribute("fill", fill);
@ -141,13 +141,13 @@ function editBiomes() {
}
function biomeChangeName(el) {
const biome = biomesData.biomeList[+el.parentNode.dataset.id];
const biome = pack.biomes[+el.parentNode.dataset.id];
el.parentNode.dataset.name = el.value;
biome.name = el.value;
}
function biomeChangeHabitability(el) {
const biome = biomesData.biomeList[+el.parentNode.dataset.id];
const biome = pack.biomes[+el.parentNode.dataset.id];
const failed = isNaN(+el.value) || +el.value < 0 || +el.value > 9999;
if (failed) {
el.value = biome.habitability;
@ -184,8 +184,7 @@ function editBiomes() {
function toggleLegend() {
if (legend.selectAll("*").size()) {clearLegend(); return;}; // hide legend
const d = biomesData;
const data = Array.from(d.biomeList) //shallow copy existing array
const data = pack.biomes //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
@ -211,7 +210,7 @@ function editBiomes() {
}
function addCustomBiome() {
const b = biomesData.biomeList, i = b.length;
const b = pack.biomes, i = b.length;
b.push(new Biome("Custom", getRandomColor(), 50))
b[i].id = i; //don't forget the ID!
@ -236,7 +235,7 @@ function editBiomes() {
}
function removeCustomBiome(el) {
const biome = biomesData.biomeList[+el.parentNode.dataset.id];
const biome = pack.biomes[+el.parentNode.dataset.id];
el.parentNode.remove();
biome.name = "removed";
biomesFooterBiomes.innerHTML = +biomesFooterBiomes.innerHTML - 1;
@ -323,7 +322,7 @@ function editBiomes() {
const temp = biomes.select("#temp");
const selected = body.querySelector("div.selected");
const biomeNew = biomesData.biomeList[+selected.dataset.id];
const biomeNew = pack.biomes[+selected.dataset.id];
const color = biomeNew.color;
selection.forEach(function(i) {
@ -379,7 +378,7 @@ function editBiomes() {
}
function restoreInitialBiomes() {
biomesData = applyDefaultBiomesSystem();
applyDefaultBiomesSystem();
defineBiomes();
drawBiomes();
recalculatePopulation();

View file

@ -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.biomeList[pack.cells.biome[i]].name); else
if (layerIsOn("toggleBiomes") && pack.cells.biome[i]) tip("Biome: " + pack.biomes[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.biomeList[cells.biome[i]].name;
infoBiome.innerHTML = pack.biomes[cells.biome[i]].name;
}
// get user-friendly (real-world) height value from map data

View file

@ -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.biomeList.length).fill("");
const paths = new Array(pack.biomes.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.biomeList[i].color).attr("stroke", biomesData.biomeList[i].color).attr("id", "biome"+i);
biomes.append("path").attr("d", d).attr("fill", pack.biomes[i].color).attr("stroke", pack.biomes[i].color).attr("id", "biome"+i);
});
// connect vertices to chain