Skip to main content
← All Posts

My terrain score went up because I deleted all the detail

Wave function collapse has been sitting in the TileForge codebase since March, unused. It’s the algorithm that generates tile layouts by constraint — you tell it which tiles may sit next to which and it solves the grid. In November it finally got wired into the forest and ruin generators, at high quality only, opt-in, failing safe if it can’t find a solution.

Then it taught me something about trusting a number.

Coherence 0.964, and nothing left

WFC needs cells it’s allowed to re-solve. For the ruin generator I freed the grass cells, which seemed obviously safe — grass is walkable, valid ground, and not part of the ruin.

Except a ruin sits in a field. So freeing grass freed 1,105 of 1,200 cells, and WFC cheerfully re-solved nearly the entire map. The cracked-floor tiles that make a ruin read as ruined went from 153 down to one.

And the metric I was using to evaluate the change — terrain coherence, the fraction of tiles matching their neighbours — went up. To 0.964, the best score I’d seen.

Which is correct. A uniform field of one tile is perfectly coherent. The metric measured exactly what it claimed to measure, and what it measures turns out to be maximised by deleting everything interesting. I had a number that improved as the output got worse, and if I’d been reading only that number I’d have shipped it as a win and written a commit message about better terrain.

The raw tile histogram caught it. 153 to 1 isn’t a subtle signal.

So there’s a rule now, written down: free a minority of cells, never the dominant background. And a guard that fails safe if a zone loses more than three-quarters of its cells to a re-solve. Where it works properly, the forest generator changes 17–27% of ground cells per seed and coherence moves from about 0.893 to 0.923 — a real improvement, small, with the variety still there.

I’d rather have a modest honest number than a great one I can’t explain.

A tree that was the letter D

The other good one. TileForge ships several bundled Kenney tile packs, and the generator needs to know which tile in a sheet is grass, which is wall, which is a tree.

It was guessing by position. Grass in the first quarter of the sheet, water in the second, wall in the third, tree in the fourth. That works if every sheet is laid out the same way, and sheets are not laid out the same way.

On the Tiny Ski pack the “tree” role resolved to tile index 99, which is the letter D from an alphabet font row. So the forest generator scattered letters across the terrain. On Tiny Battle, “wall” came out as a building and “tree” as a stretch of road.

The fix wasn’t a better heuristic. There’s no heuristic that reads a tilesheet’s intent from the position of its tiles. I made the guess visible — the metadata records that a role came from a guess rather than real data — and then did the boring work of hand-tagging all seven bundled packs.

Sometimes the answer to a bad inference is to stop inferring.

Undo that wedged itself

A data-loss bug came back this month, and the way it came back is the interesting part.

The line tool could push something onto the undo stack that wasn’t a valid undo entry. The undo handler had no error handling, so when it hit that entry it threw before advancing its index, which means it would hit the same bad entry next time, and the time after. The undo stack wedged permanently, stranding every edit recorded before it. Not “undo doesn’t work for that action” — undo doesn’t work again, for anything, for the rest of your session.

I’d fixed it in July. On a branch that never got merged, including its test file, which also went missing. So in November I rediscovered it, debugged it again, and fixed it again.

This time the history object structurally rejects anything that isn’t a valid entry, so the bad push can’t happen rather than being handled. And the fill, line and select tools all write tiles through one batched API instead of touching the layer data directly.

An unmerged fix isn’t a fix. I knew that and I still spent the same day twice.

Ten source maps I nearly published

Last thing, and it’s a near miss rather than a bug. I found ten source map files staged in the build output, ready to go out with the next deploy.

Source maps with embedded content reconstruct your original source, so publishing them means publishing the whole codebase. The same class of leak had already turned up live on a sibling project of mine a couple of weeks earlier, which is the only reason I thought to look in the build directory at all.

Source map generation is off now. And TileForge’s TMX export had two XML injection holes — an unescaped attribute, and about 25 numeric attribute positions accepting unvalidated strings from imported files — both closed.

668 tests to 900 in the first week of the month. The tripwire I most wanted — detect when tiles changed without a corresponding undo entry being recorded — is built and not yet wired in, because none of the obvious places to put it worked. That’s the honest state of it.

— Bruno