Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions src/core/WorldManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,13 @@ export class WorldManager {
EventBus.subscribe(EVENTS.COMMAND_APPLY_SELECTED_INITIAL_STATE_TO_ALL, this._applySelectedInitialStateToAll);
EventBus.subscribe(EVENTS.COMMAND_RESET_INITIAL_STATES_TO_DEFAULT, this._resetStatesToDefault);
EventBus.subscribe(EVENTS.COMMAND_RESET_ALL_WORLDS_TO_INITIAL_DENSITIES, () => {
// --- MODIFICATION START ---
const baseSeed = Date.now();
this.worlds.forEach((proxy, idx) => {
if (this.worldSettings[idx]) {
const seed = this.deterministic ? baseSeed : baseSeed + idx;
proxy.resetWorld(this.worldSettings[idx].initialState, seed);
proxy.resetWorld(this.worldSettings[idx].initialState, this._getResetSeed(baseSeed, idx));
if (!this.isGloballyPaused && this.worldSettings[idx].enabled) proxy.startSimulation();
}
});
// --- MODIFICATION END ---
EventBus.dispatch(EVENTS.ALL_WORLDS_RESET);
});
EventBus.subscribe(EVENTS.COMMAND_RESET_WORLDS_WITH_CURRENT_RULESET, (data) => {
Expand All @@ -319,15 +316,15 @@ export class WorldManager {
return;
}
const indicesToReset = this._getAffectedWorldIndices(data.scope);
const baseSeed = Date.now();
indicesToReset.forEach(idx => {
if (data.copyPrimaryRuleset && idx !== this.selectedWorldIndex) {
const newRulesetBuffer = hexToRuleset(primaryRulesetHex).buffer.slice(0);
this.worlds[idx].setRuleset(newRulesetBuffer);
this.worldSettings[idx].rulesetHex = primaryRulesetHex;
}
if (this.worldSettings[idx]) {
// --- MODIFICATION ---
this.worlds[idx].resetWorld(this.worldSettings[idx].initialState);
this.worlds[idx].resetWorld(this.worldSettings[idx].initialState, this._getResetSeed(baseSeed, idx));
if (!this.isGloballyPaused && this.worldSettings[idx].enabled) this.worlds[idx].startSimulation();
}
});
Expand Down Expand Up @@ -432,6 +429,12 @@ export class WorldManager {
return [];
}

// In deterministic mode every world reseeds from the same base seed so
// worlds with identical initial-state configs produce identical grids.
_getResetSeed = (baseSeed, worldIndex) => {
return this.deterministic ? baseSeed : baseSeed + worldIndex;
}

#applyRulesetToWorlds = (rulesetHex, scope, shouldReset) => {
const newRulesetArray = hexToRuleset(rulesetHex);
if (rulesetHex === "Error" || newRulesetArray.length !== 128) {
Expand All @@ -440,18 +443,19 @@ export class WorldManager {
}

const indicesToAffect = this._getAffectedWorldIndices(scope);
const baseSeed = Date.now();

indicesToAffect.forEach(idx => {

this._addRulesetToHistory(idx, rulesetHex);

const newRulesetBuffer = newRulesetArray.buffer.slice(0);
this.worlds[idx].setRuleset(newRulesetBuffer);
this.worldSettings[idx].rulesetHex = rulesetHex;


if (shouldReset && this.worldSettings[idx]) {
this.worlds[idx].resetWorld(this.worldSettings[idx].initialState);
this.worlds[idx].resetWorld(this.worldSettings[idx].initialState, this._getResetSeed(baseSeed, idx));
}
});

Expand Down Expand Up @@ -565,6 +569,7 @@ export class WorldManager {
}

const generatedHexes = new Set([sourceRulesetHex]);
const baseSeed = Date.now();

this.worlds.forEach((proxy, idx) => {
let newHex = sourceRulesetHex;
Expand Down Expand Up @@ -603,18 +608,18 @@ export class WorldManager {
this.worldSettings[idx].rulesetHex = newHex;

if (this.worldSettings[idx]) {
proxy.resetWorld(this.worldSettings[idx].initialState, Date.now() + idx);
proxy.resetWorld(this.worldSettings[idx].initialState, this._getResetSeed(baseSeed, idx));

if (!this.isGloballyPaused) {
proxy.startSimulation();
}
}
});


PersistenceService.saveWorldSettings(this.worldSettings);
EventBus.dispatch(EVENTS.WORLD_SETTINGS_CHANGED, this.getWorldSettingsForUI());
EventBus.dispatch(EVENTS.ALL_WORLDS_RESET);
EventBus.dispatch(EVENTS.ALL_WORLDS_RESET);
}

_cloneRuleset = () => {
Expand All @@ -631,18 +636,20 @@ export class WorldManager {
return;
}

const baseSeed = Date.now();

this.worlds.forEach((proxy, idx) => {
if (idx !== this.selectedWorldIndex) {
const newRulesetBuffer = hexToRuleset(sourceRulesetHex).buffer.slice(0);
proxy.setRuleset(newRulesetBuffer);
}

this._addRulesetToHistory(idx, sourceRulesetHex);
this.worldSettings[idx].rulesetHex = sourceRulesetHex;

if (this.worldSettings[idx]) {
proxy.resetWorld(this.worldSettings[idx].initialState, Date.now() + idx);
proxy.resetWorld(this.worldSettings[idx].initialState, this._getResetSeed(baseSeed, idx));

if (!this.isGloballyPaused) {
proxy.startSimulation();
}
Expand All @@ -656,6 +663,7 @@ export class WorldManager {

_modifyRulesetForScope = (scope, modifierFunc, conditionalResetScope) => {
const indices = this._getAffectedWorldIndices(scope);
const baseSeed = Date.now();
indices.forEach(idx => {
const proxyStats = this.worlds[idx]?.getLatestStats();
let currentHex = (proxyStats?.rulesetHex && proxyStats.rulesetHex !== "Error")
Expand All @@ -679,7 +687,7 @@ export class WorldManager {
if (conditionalResetScope !== 'none') {
const resetTargetIndices = this._getAffectedWorldIndices(conditionalResetScope);
if (resetTargetIndices.includes(idx) && this.worldSettings[idx]) {
this.worlds[idx].resetWorld(this.worldSettings[idx].initialState, Date.now() + idx);
this.worlds[idx].resetWorld(this.worldSettings[idx].initialState, this._getResetSeed(baseSeed, idx));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const NEIGHBOR_DIRS_EVEN_R = [
* Useful for debugging the flow of information in the application.
*/
export const EVENT_BUS_LOGGING = {
enabled: true, // Set to true to enable console logging of events
enabled: false, // Set to true to enable console logging of events

/**
* An array of event prefixes to log. If empty, all events are logged (if enabled).
Expand Down
4 changes: 2 additions & 2 deletions src/ui/UIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ export class UIManager {
'clear-all': { icon: '🌍', title: 'Clear All', command: EVENTS.COMMAND_CLEAR_WORLDS, payload: { scope: 'all' } },
'reset-one': { icon: '🔄', title: 'Reset World', command: EVENTS.COMMAND_RESET_WORLDS_WITH_CURRENT_RULESET, payload: { scope: 'selected' } },
'reset-all': { icon: '♻️', title: 'Reset All', command: EVENTS.COMMAND_RESET_ALL_WORLDS_TO_INITIAL_DENSITIES, payload: {} },
'reset-densities': { icon: '🎨', title: 'Default Densities', command: EVENTS.COMMAND_RESET_DENSITIES_TO_DEFAULT, payload: {} },
'apply-density-all': { icon: '🎯', title: 'Apply Density', command: EVENTS.COMMAND_APPLY_SELECTED_DENSITY_TO_ALL, payload: {} }
'reset-densities': { icon: '🎨', title: 'Default Densities', command: EVENTS.COMMAND_RESET_INITIAL_STATES_TO_DEFAULT, payload: {} },
'apply-density-all': { icon: '🎯', title: 'Apply Density', command: EVENTS.COMMAND_APPLY_SELECTED_INITIAL_STATE_TO_ALL, payload: {} }
};

const fabSettings = PersistenceService.loadUISetting('fabSettings', { enabled: ['generate', 'clone-mutate', 'reset-all'], locked: true, order: [] });
Expand Down
4 changes: 2 additions & 2 deletions src/ui/controllers/WorldsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export class WorldsController {
}

applySelectedDensityToAll = () => {
EventBus.dispatch(EVENTS.COMMAND_APPLY_SELECTED_DENSITY_TO_ALL);
EventBus.dispatch(EVENTS.COMMAND_APPLY_SELECTED_INITIAL_STATE_TO_ALL);
}

resetDensitiesToDefault = () => {
EventBus.dispatch(EVENTS.COMMAND_RESET_DENSITIES_TO_DEFAULT);
EventBus.dispatch(EVENTS.COMMAND_RESET_INITIAL_STATES_TO_DEFAULT);
}

saveSelectedWorldState = () => {
Expand Down