2 ms·
I saw this post about 2 hours ago, and I was fascinated by it and curious how it worked. So I decided to de-obfuscate it and add comments. The code was too lar
by bheadmaster 3y ago
I saw this post about 2 hours ago, and I was fascinated by it and curious how it worked. So I decided to de-obfuscate it and add comments.
The code was too large for a comment, so here's the gist:
https://gist.github.com/paskozdilar/48d7532733ccd11144bb43fed953c334 https://gist.github.com/paskozdilar/48d7532733ccd11144bb43fe...
The approach is quite interesting - it (ab)uses the preprocessor to shorten the syntax of for loops:
#define l(a, b, c, d) for (int y = a; y < b; y++) for (int x = c; x < d; x++)
And then it does the heavy lifting by iterating over the whole map thousand times, randomly selecting a rectangle on the map and checking if the selected area is adjacent to an already-generated cave. It's a probabilistic algorithm, so there's a tiny chance that it will just print a single cave in the middle and nothing else.
EDIT: Then I found that many other people also de-obfuscated it in the gist comments. :') This guy did a much better job than me:
https://gist.github.com/Joker-vD/cc5372a349559b9d1a3b220d5eaf2b01 https://gist.github.com/Joker-vD/cc5372a349559b9d1a3b220d5ea...
- munificent 3y ago> And then it does the heavy lifting by iterating over the whole map thousand times, randomly selecting a rectangle on the map and checking if the selected area is adjacent to an already-generated cave. It's a probabilistic algorithm, so there's a tiny chance that it will just print a single cave in the middle and nothing else. Yeah, it's surprising how well this approach works. Performance is actually decent. There is a chance of "dud" levels that only have a couple of rooms, but it's rare. The nice thing about this approach is that it guarantees by construction that every room in the dungeon will be reachable from every other one.