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
36 changes: 36 additions & 0 deletions apps/micropolis/src/lib/cityLoadDeterminism.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, it, expect } from 'vitest';
import { loadMicropolisMainModule } from './wasm/node';
import { createNoopJsCallback } from './wasm/callbacks';
import { createMapMopViews } from './wasm/views';
import type { MainModule, Micropolis } from '../types/micropolisengine.d.js';

const CITY = '/cities/kobe.cty';

const loadWithSeed = (engine: MainModule, seed: number): Micropolis => {
const micropolis = new engine.Micropolis();
micropolis.setCallback(createNoopJsCallback(engine), {});
micropolis.init();
micropolis.seedRandom(seed);
expect(micropolis.loadCity(CITY)).toBe(true);
return micropolis;
};

const mapHash = (engine: MainModule, micropolis: Micropolis): number => {
let hash = 0;
for (const tile of createMapMopViews(engine, micropolis)!.mapData) hash = (Math.imul(hash, 31) + tile) | 0;
return hash >>> 0;
};

describe('city load determinism', () => {
it('same seed gives the same map, a different seed gives another', async () => {
const engine = await loadMicropolisMainModule();
const first = loadWithSeed(engine, 42);
const second = loadWithSeed(engine, 42);
const other = loadWithSeed(engine, 7);

expect(mapHash(engine, second)).toBe(mapHash(engine, first));
expect(mapHash(engine, other)).not.toBe(mapHash(engine, first));

for (const micropolis of [first, second, other]) micropolis.delete();
});
});
2 changes: 0 additions & 2 deletions packages/micropolis-engine/src/fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,6 @@ bool Micropolis::loadCity(const std::string &filename)
std::string newCityName = cityFileName.substr(pos, last - pos);
setCityName(newCityName);

doSimInit();

didLoadCity(filename);

return true;
Expand Down
1 change: 0 additions & 1 deletion packages/micropolis-engine/src/initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
/** Reset many game state variables */
void Micropolis::initWillStuff()
{
randomlySeedRandom();
initGraphMax();
destroyAllSprites();

Expand Down
1 change: 1 addition & 0 deletions packages/micropolis-engine/src/micropolis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ void Micropolis::simInit()
resetMapState();
resetEditorState();
clearMap();
randomlySeedRandom();
initWillStuff();
setFunds(5000);
setGameLevelFunds(LEVEL_EASY);
Expand Down