Generalise gamesettings pickRandomItems' loop

Comments By: wraitii, Stan, Silier
Suggested By: elexis
Differential Revision: D4231
This was SVN commit r26535.
This commit is contained in:
bb 2022-03-03 16:13:35 +00:00
parent bc59f0c40b
commit bd4b7d4bb6
5 changed files with 20 additions and 29 deletions

View File

@ -88,21 +88,21 @@ class GameSettings
*/
pickRandomItems()
{
let components = Object.keys(this);
let i = 0;
while (components.length && i < 100)
const components = Object.keys(this);
// When we have looped components.length + 1 times, we are considered stuck.
for (let i = 0; i <= components.length; ++i)
{
// Re-pick if any random setting was unrandomised,
// to make sure dependencies are cleared.
// TODO: there's probably a better way to handle this.
components = components.filter(comp => this[comp].pickRandomItems ?
!!this[comp].pickRandomItems() : false);
++i;
}
if (i === 100)
{
throw new Error("Infinite loop picking random items, remains : " + uneval(components));
// Re-pick if any random setting was unrandomised, to make sure dependencies are cleared.
let rePick = false;
for (const comp in this)
if (this[comp].pickRandomItems)
rePick = this[comp].pickRandomItems() || rePick;
if (!rePick)
return;
}
throw new Error("Infinite loop picking random items detected, components: " + uneval(components));
}
/**

View File

@ -82,11 +82,9 @@ GameSettings.prototype.Attributes.Biome = class Biome extends GameSetting
pickRandomItems()
{
// If the map is random, we need to wait until it selects to know if we need to pick a biome.
if (this.settings.map.map === "random")
return true;
if (this.biome !== "random")
if (this.settings.map.map === "random" || this.biome !== "random")
return false;
this.biome = pickRandom(Array.from(this.available));
return true;
}

View File

@ -44,11 +44,9 @@ GameSettings.prototype.Attributes.Daytime = class Daytime extends GameSetting
pickRandomItems()
{
// If the map is random, we need to wait until it is selected.
if (this.settings.map.map === "random")
return true;
if (this.value !== "random")
if (this.settings.map.map === "random" || this.value !== "random")
return false;
this.value = pickRandom(this.data).Id;
return true;
}

View File

@ -57,10 +57,7 @@ GameSettings.prototype.Attributes.Landscape = class Landscape extends GameSettin
pickRandomItems()
{
// If the map is random, we need to wait until it is selected.
if (this.settings.map.map === "random")
return true;
if (!this.value || !this.value.startsWith("random"))
if (this.settings.map.map === "random" || !this.value || !this.value.startsWith("random"))
return false;
let items = [];

View File

@ -40,11 +40,9 @@ GameSettings.prototype.Attributes.TeamPlacement = class TeamPlacement extends Ga
pickRandomItems()
{
// If the map is random, we need to wait until it is selected.
if (this.settings.map.map === "random")
return true;
if (this.value !== "random")
if (this.settings.map.map === "random" || this.value !== "random")
return false;
this.value = pickRandom(this.available).Id;
return true;
}