mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 09:41:24 +01:00
v1.5.7 - cultures selection loop fix
This commit is contained in:
parent
7a94ab632f
commit
31e0ed7c94
2 changed files with 18 additions and 10 deletions
|
|
@ -38,7 +38,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const cultures = pack.cultures = getRandomCultures(count);
|
const cultures = pack.cultures = selectCultures(count);
|
||||||
const centers = d3.quadtree();
|
const centers = d3.quadtree();
|
||||||
const colors = getColors(count);
|
const colors = getColors(count);
|
||||||
const emblemShape = document.getElementById("emblemShape").value;
|
const emblemShape = document.getElementById("emblemShape").value;
|
||||||
|
|
@ -77,16 +77,21 @@
|
||||||
|
|
||||||
cultures.forEach(c => c.base = c.base % nameBases.length);
|
cultures.forEach(c => c.base = c.base % nameBases.length);
|
||||||
|
|
||||||
function getRandomCultures(c) {
|
function selectCultures(c) {
|
||||||
const d = getDefault(c), n = d.length-1;
|
let def = getDefault(c);
|
||||||
const count = Math.min(c, d.length);
|
if (c === def.length) return def;
|
||||||
|
if (def.every(d => d.odd === 1)) return def.splice(0, c);
|
||||||
|
|
||||||
|
const count = Math.min(c, def.length);
|
||||||
const cultures = [];
|
const cultures = [];
|
||||||
while (cultures.length < count) {
|
|
||||||
let culture = d[rand(n)];
|
for (let culture, rnd, i=0; cultures.length < count && i < 200; i++) {
|
||||||
do {
|
do {
|
||||||
culture = d[rand(n)];
|
rnd = rand(def.length-1);
|
||||||
} while (!P(culture.odd) || cultures.find(c => c.name === culture.name))
|
culture = def[rnd];
|
||||||
|
} while (!P(culture.odd))
|
||||||
cultures.push(culture);
|
cultures.push(culture);
|
||||||
|
def.splice(rnd, 1);
|
||||||
}
|
}
|
||||||
return cultures;
|
return cultures;
|
||||||
}
|
}
|
||||||
|
|
@ -304,9 +309,10 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
if (culturesSet.value === "random") {
|
if (culturesSet.value === "random") {
|
||||||
return d3.range(count).map(i => {
|
return d3.range(count).map(function() {
|
||||||
const rnd = rand(nameBases.length-1);
|
const rnd = rand(nameBases.length-1);
|
||||||
return {name:Names.getBaseShort(rnd), base:rnd, odd:1, shield:getRandomShield()}
|
const name = Names.getBaseShort(rnd);
|
||||||
|
return {name, base:rnd, odd:1, shield:getRandomShield()}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -224,6 +224,8 @@ function rand(min, max) {
|
||||||
|
|
||||||
// probability shorthand
|
// probability shorthand
|
||||||
function P(probability) {
|
function P(probability) {
|
||||||
|
if (probability >= 1) return true;
|
||||||
|
if (probability <= 0) return false;
|
||||||
return Math.random() < probability;
|
return Math.random() < probability;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue