v 0.8.31b

This commit is contained in:
Azgaar 2019-06-05 20:23:06 +03:00
parent ddb30d1ff2
commit 83585c3081
4 changed files with 168 additions and 111 deletions

View file

@ -1012,6 +1012,8 @@
<button id="regenerateRoutes" data-tip="Click to regenerate all routes">Routes</button> <button id="regenerateRoutes" data-tip="Click to regenerate all routes">Routes</button>
<button id="regenerateRivers" data-tip="Click to regenerate all rivers. Please note map heights can be changed by water flow">Rivers</button> <button id="regenerateRivers" data-tip="Click to regenerate all rivers. Please note map heights can be changed by water flow">Rivers</button>
<button id="regeneratePopulation" data-tip="Click to recalculate rural and urban population">Population</button> <button id="regeneratePopulation" data-tip="Click to recalculate rural and urban population">Population</button>
<button id="regenerateBurgs" data-tip="Click to regenerate all burgs and routes">Burgs</button>
<!-- <button id="regenerateStates" data-tip="Click to regenerate states">States</button> -->
</div> </div>
<div id="addFeature"> <div id="addFeature">

View file

@ -6,8 +6,7 @@
const generate = function() { const generate = function() {
console.time("generateBurgsAndStates"); console.time("generateBurgsAndStates");
const cells = pack.cells, cultures = pack.cultures, n = cells.i.length;
const cells = pack.cells, vertices = pack.vertices, features = pack.features, cultures = pack.cultures, n = cells.i.length;
cells.burg = new Uint16Array(n); // cell burg cells.burg = new Uint16Array(n); // cell burg
cells.road = new Uint16Array(n); // cell road power cells.road = new Uint16Array(n); // cell road power
@ -19,13 +18,14 @@
placeTowns(); placeTowns();
const townRoutes = Routes.getTrails(); const townRoutes = Routes.getTrails();
specifyBurgs(); specifyBurgs();
const oceanRoutes = Routes.getSearoutes(); const oceanRoutes = Routes.getSearoutes();
expandStates(); expandStates();
normalizeStates(); normalizeStates();
Routes.draw(capitalRoutes, townRoutes, oceanRoutes); Routes.draw(capitalRoutes, townRoutes, oceanRoutes);
drawBurgsWithLabels(); drawBurgs();
function placeCapitals() { function placeCapitals() {
console.time('placeCapitals'); console.time('placeCapitals');
@ -134,11 +134,15 @@
console.timeEnd('placeTowns'); console.timeEnd('placeTowns');
} }
// define burg coordinates and define details console.timeEnd("generateBurgsAndStates");
function specifyBurgs() { }
console.time("specifyBurgs");
for (const b of burgs) { // define burg coordinates and define details
const specifyBurgs = function() {
console.time("specifyBurgs");
const cells = pack.cells, vertices = pack.vertices;
for (const b of pack.burgs) {
if (!b.i) continue; if (!b.i) continue;
const i = b.cell; const i = b.cell;
@ -168,9 +172,9 @@
} }
// de-assign port status if it's the only one on feature // de-assign port status if it's the only one on feature
for (const f of features) { for (const f of pack.features) {
if (!f.i || f.land) continue; if (!f.i || f.land) continue;
const onFeature = burgs.filter(b => b.port === f.i); const onFeature = pack.burgs.filter(b => b.port === f.i);
if (onFeature.length === 1) { if (onFeature.length === 1) {
onFeature[0].port = 0; onFeature[0].port = 0;
} }
@ -179,7 +183,7 @@
console.timeEnd("specifyBurgs"); console.timeEnd("specifyBurgs");
} }
function drawBurgsWithLabels() { const drawBurgs = function() {
console.time("drawBurgs"); console.time("drawBurgs");
// remove old data // remove old data
@ -188,7 +192,7 @@
icons.selectAll("use").remove(); icons.selectAll("use").remove();
// capitals // capitals
const capitals = burgs.filter(b => b.capital); const capitals = pack.burgs.filter(b => b.capital);
const capitalIcons = burgIcons.select("#cities"); const capitalIcons = burgIcons.select("#cities");
const capitalLabels = burgLabels.select("#cities"); const capitalLabels = burgLabels.select("#cities");
const capitalSize = capitalIcons.attr("size") || 1; const capitalSize = capitalIcons.attr("size") || 1;
@ -209,7 +213,7 @@
.attr("width", caSize).attr("height", caSize); .attr("width", caSize).attr("height", caSize);
// towns // towns
const towns = burgs.filter(b => b.capital === false); const towns = pack.burgs.filter(b => b.capital === false);
const townIcons = burgIcons.select("#towns"); const townIcons = burgIcons.select("#towns");
const townLabels = burgLabels.select("#towns"); const townLabels = burgLabels.select("#towns");
const townSize = townIcons.attr("size") || 0.5; const townSize = townIcons.attr("size") || 0.5;
@ -232,9 +236,6 @@
console.timeEnd("drawBurgs"); console.timeEnd("drawBurgs");
} }
console.timeEnd("generateBurgsAndStates");
}
// growth algorithm to assign cells to states like we did for cultures // growth algorithm to assign cells to states like we did for cultures
const expandStates = function() { const expandStates = function() {
console.time("expandStates"); console.time("expandStates");
@ -481,6 +482,6 @@
console.timeEnd("drawStateLabels"); console.timeEnd("drawStateLabels");
} }
return {generate, expandStates, normalizeStates, drawStateLabels}; return {generate, expandStates, normalizeStates, drawBurgs, specifyBurgs, drawStateLabels};
}))); })));

View file

@ -19,13 +19,10 @@ toolsContent.addEventListener("click", function(event) {
if (button === "regenerateStateLabels") {BurgsAndStates.drawStateLabels(); if (!layerIsOn("toggleLabels")) toggleLabels();} else if (button === "regenerateStateLabels") {BurgsAndStates.drawStateLabels(); if (!layerIsOn("toggleLabels")) toggleLabels();} else
if (button === "regenerateReliefIcons") {ReliefIcons(); if (!layerIsOn("toggleRelief")) toggleRelief();} else if (button === "regenerateReliefIcons") {ReliefIcons(); if (!layerIsOn("toggleRelief")) toggleRelief();} else
if (button === "regenerateRoutes") {Routes.regenerate(); if (!layerIsOn("toggleRoutes")) toggleRoutes();} else if (button === "regenerateRoutes") {Routes.regenerate(); if (!layerIsOn("toggleRoutes")) toggleRoutes();} else
if (button === "regenerateRivers") { if (button === "regenerateRivers") regenerateRivers(); else
const heights = new Uint8Array(pack.cells.h); if (button === "regeneratePopulation") recalculatePopulation(); else
Rivers.generate(); if (button === "regenerateBurgs") regenerateBurgs(); else
pack.cells.h = new Uint8Array(heights); if (button === "regenerateStates") regenerateStates();
if (!layerIsOn("toggleRivers")) toggleRivers();
} else
if (button === "regeneratePopulation") recalculatePopulation();
// Click to Add buttons // Click to Add buttons
if (button === "addLabel") toggleAddLabel(); else if (button === "addLabel") toggleAddLabel(); else
@ -35,6 +32,13 @@ toolsContent.addEventListener("click", function(event) {
if (button === "addMarker") toggleAddMarker(); if (button === "addMarker") toggleAddMarker();
}); });
function regenerateRivers() {
const heights = new Uint8Array(pack.cells.h);
Rivers.generate();
pack.cells.h = new Uint8Array(heights);
if (!layerIsOn("toggleRivers")) toggleRivers();
}
function recalculatePopulation() { function recalculatePopulation() {
rankCells(); rankCells();
pack.burgs.forEach(b => { pack.burgs.forEach(b => {
@ -47,6 +51,56 @@ function recalculatePopulation() {
}); });
} }
function regenerateBurgs() {
const cells = pack.cells, states = pack.states;
rankCells();
cells.burg = new Uint16Array(cells.i.length);
const burgs = pack.burgs = [0]; // clear burgs array
states.filter(s => s.i).forEach(s => s.capital = 0); // clear capitals
const burgsTree = d3.quadtree();
const score = new Int16Array(cells.s.map(s => s * Math.random())); // cell score for capitals placement
const sorted = cells.i.filter(i => score[i] > 0 && cells.culture[i]).sort((a, b) => score[b] - score[a]); // filtered and sorted array of indexes
const burgsCount = manorsInput.value == 1000 ? rn(sorted.length / 10 / densityInput.value ** .8) + states.length : +manorsInput.value + states.length;
const spacing = (graphWidth + graphHeight) * 9 / burgsCount; // base min distance between towns
for (let i=0; i < sorted.length && burgs.length < burgsCount; i++) {
const id = burgs.length;
const cell = sorted[i];
const x = cells.p[cell][0], y = cells.p[cell][1];
const s = spacing * Math.random() + 0.5; // randomize to make the placement not uniform
if (burgsTree.find(x, y, s) !== undefined) continue; // to close to existing burg
const state = cells.state[cell];
const capital = !states[state].capital; // if state doesn't have capital, make this burg a capital
if (capital) {states[state].capital = id; states[state].cell = cell;}
const culture = cells.culture[cell];
const name = Names.getCulture(culture);
burgs.push({cell, x, y, state, i: id, culture, name, capital, feature: cells.f[cell]});
burgsTree.add([x, y]);
cells.burg[cell] = id;
}
// add a capital at former place for states without added capitals
states.filter(s => s.i && !s.removed && !s.capital).forEach(s => {
const burg = addBurg([cells.p[s.center][0], cells.p[s.center][1]]); // add new burg
s.capital = burg;
pack.burgs[burg].capital = true;
pack.burgs[burg].state = s.i;
moveBurgToGroup(burg, "cities");
});
BurgsAndStates.specifyBurgs();
BurgsAndStates.drawBurgs();
Routes.regenerate();
}
function regenerateStates() {
}
function unpressClickToAddButton() { function unpressClickToAddButton() {
addFeature.querySelectorAll("button.pressed").forEach(b => b.classList.remove("pressed")); addFeature.querySelectorAll("button.pressed").forEach(b => b.classList.remove("pressed"));
restoreDefaultEvents(); restoreDefaultEvents();