How I built a global treasure hunt on hexagonal map tiles
· 13 minute read
A one-ounce gold coin is hidden somewhere outdoors, and a camera is watching it. Treasure hunters look at a photo of the treasure and guess on a globe where it is. The globe is cut into 1.66 trillion hexagons, each guess checks one of them, and first to get it right is first to know where it is. Anyone can then go and pick it up. How hard could it be?

It came down to three problems: a map of the whole world that doesn’t bill me per tile, a treasure location that stays secret even if my server gets hacked, and a way to pace the guesses so the hunt lasts. As of writing, the treasure is still out there.
The map tile bill
First is the map itself. Last treasure hunt I built, we spent over $400 paying our provider to serve map tiles, and that hunt only ran for two weeks. That was not even a bad deal given that we had over 100,000 unique visitors. But every visitor on the site who doesn’t pay for the perks is slowly burning money on map tiles, and that’s money that could be going to the prize pool. Worst of all, our provider’s terms didn’t allow putting the tiles behind a CDN to save requests by caching.
So this time I would serve the tiles myself. I also wanted full creative control over every part of the map, to make it as cool as I wanted, and that meant building the whole stack myself.
Guessing in hexagons
The architecture depends on how guessing works. I knew about Uber’s H3 system, a way to tile the whole globe in hexagons, and that seemed ideal. Everyone would just guess small hexagons and chip away at the whole globe. The smaller the hexagons, the more guesses people can get, which makes for a more engaging experience. But make them too small and it gets tedious, and removes the physical search aspect if you know the exact pinpoint. I settled on H3 resolution 12. That makes each hexagon, or cell as H3 calls it, roughly 307 m², and 1.66 trillion hexagons to cover the whole globe.
Of course, it would take many orders of magnitude fewer guesses to actually find the treasure, you don’t need to sweep the whole globe. Hunters look at the images, and can narrow it down by the type of tree, and other clues in them. To steer the pace, I would simply limit how fast new guesses are handed out. More on that at the end.
Map tiles as hexagons
Since we are doing H3 hexagons for the guesses, I thought, why not make the whole map system based on H3? Surely that would be convenient as the system is built out, since it would match how our guesses are structured. Every guess would have exactly one parent from each coarser resolution, and you wouldn’t have a tile awkwardly in between two rectangular tiles.
Traditional map tiles are based on Mercator coordinates. You get a tile at a location, and if you zoom in, that tile turns into four smaller tiles, simple. But I wanted a 3D globe, not a distorted Mercator projection. When rendering a globe, libraries like MapLibre GL take the Mercator-shaped tiles and stretch them onto it. Usually, there is no special consideration for the Mercator distortion when creating the tiles, a process called baking. That means that a tile in Norway and one in Kenya will have the same content density, even though their physical size on the globe differs. In practice, the Norway tile will get a worse deal: it will have fewer details per pixel than a tile near the equator. Some client-side rendering rules punish Norway further: streets might be drawn thinner, and labels only show up at higher zooms.
This was something the H3-based tiles would fix, since they are built for a globe, not a flat map, so they don’t have any distortion towards the poles. I made sure to always match or exceed the detail level of traditional Mercator-based tiles. In the end, we get a cool effect where our map loads in large hexagonal tiles. I made sure to not curve them, keeping them flat, so the tiling itself is really prominent at globe scale.
But the costs are non-trivial: each parent tile contains seven child tiles, but you can’t actually make a hexagon out of seven smaller ones, so you get a hierarchy that’s not perfectly nesting. Each step down is also an awkward zoom factor of √7, about 1.4 Mercator zoom levels, and the whole grid turns by about 19° every time. You also can’t tile a whole globe in pure hexagons, there are actually 12 pentagons at each resolution.
I got around the nesting by never mixing resolutions. The renderer picks one resolution from the camera distance and covers the whole view with it. A Mercator renderer can patch a missing tile with its parent or its children, because they nest perfectly. My tiles don’t nest, so while new tiles load, whatever cached tiles I have from other resolutions stay drawn underneath.
Every tile is also cut to the exact shape of its cell. A road or river that crosses a border is baked into every tile it touches, plus a small margin past the edge. The renderer then draws only the part of each tile that’s inside its cell’s outline, so the overlaps are never drawn.
Each tile is its own small flat map, centered on its cell. It uses a gnomonic projection, in which the edges of an H3 cell come out as straight lines, so cutting a tile to its cell is straight-line math in both the baker and the renderer. A tile’s place on the globe comes from its H3 index, so it doesn’t store its own position. Pentagons are ordinary tiles with five edges.
Surprisingly, the baker worked quite well with these limitations, and some players even asked if we used a traditional map provider.
What’s in a tile
The tile format is my own byte format. It keeps the same model as Mapbox vector tiles, with layers, features, shared tag tables and delta-encoded varint coordinates, but without protobuf. The encoding is canonical: the same content always gives the same bytes, wherever on the globe it is. A tile that is nothing but ocean is the same everywhere, so it isn’t stored, and a missing tile means sea.
Tiles are compressed with zstd, with a custom zstd dictionary trained on the data, for Pareto optimal size and compression speed.
Baking the planet
To know if my tiles were any good, I needed something to compare against. I started by making a reference bake of Nevada, where the last treasure hunt ended, with Planetiler, a Java program that’s commonly used for baking traditional tiles. I then used MapLibre GL with the OSM Bright style to render it. Now, whenever I built my system, I could toggle back and forth between mine and the reference, and quickly spot issues.
My first baker was in Python and took 163 seconds to bake Nevada at a coarser resolution, only down to resolution 5 tiles. First I optimized the Python version to 77 seconds. Then I rewrote it in Rust, which took 30 seconds, and that was to bake everything down to resolution 8. Each resolution has seven times the tiles of the one before, so that’s about 343 times as many tiles. Further Rust optimization took it down to 7.93 seconds. Because wall-clock time had high variance, I compared each change by counting CPU instructions on one pinned thread.
The finished version was only 8,862 lines of Rust, tile server included, and the baker has zero crate dependencies. The only real dependencies are the C and C++ libraries zstd, tcmalloc and libdeflate. Initially I used GEOS, libosmium, H3, Spade triangulation, and other dependencies, but when I vendored them and cut them down to what I needed, I was able to shave down their 300,000 lines of code to just small enough where I could fold it into my code, improving performance at the same time.
Then I made it multithreaded. The globe is split into shards, each one a coarse H3 cell. A tile belongs to the shard that holds its center, geometry is routed to every shard it touches, and the shards bake in parallel. That got the runtime for Nevada down to 1.02 seconds and got the whole US West region baking in 24 seconds while staying within my 32 GB of memory. Memory wasn’t a major concern since my production server had 500 GB. The whole world bake ended up taking under 25 minutes and used about 400 GB of memory.
The hardest part
I could’ve never guessed that the most computationally complex and expensive part of generating the tiles would be the labels for bodies of water. Not the millions of houses and thousands of towns, not the complex layer geometry, oceans, streets, airports. One of the hardest problems in computational map making is how to place a label in an oddly shaped polygon efficiently, legibly, and curving it to fit the shape of the body without it looking awkward. There’s even research coming out on this as recently as 2020.
You would think the text could just be written left to right, plainly, like a city or region name. It could, but it would look ugly. You would have a blue river and then a blue label next to it, adding more blue to the screen and obscuring the shape of the already thin river. So you want the text to be inside the water. But if the river runs north to south and is very thin, you are forced to place the text vertically inside it. And if the river isn’t a straight line and it bends, you must bend the label too. In lakes, you want multiple labels so you can see the name from many different locations. Each label should be not too close to either shore, so it looks balanced, but it also must fit the shape of the lake.
The algorithm I used is to find the lake’s spine, the same idea that paper uses. I compute the Voronoi diagram of points sampled along the shore, and the edges that fall inside the water form a skeleton running down the middle of every arm and bay. Two rounds of Dijkstra find the longest route through that skeleton, which is then smoothed into a curve that the renderer sets the name along, letter by letter.
But after implementing, refactoring, and optimizing it, I decided I actually didn’t need water labels for my use case anyways. So I removed a solid thousand lines of code.
The renderer
I knew I wanted to use the latest and greatest APIs, so I started building it in WebGPU instead of WebGL. Who knows, maybe I would turn it into a native app some day, it would be pretty easy. On the client, a web worker decodes map tiles in its own thread using Wasm zstd, and builds the vertex buffers there too, ready for the main thread to upload.
The fragment shader works out which of the 1.66 trillion guess cells each pixel is in, so the cells are never baked into the tiles or sent as geometry. It projects the pixel onto the nearest face of the icosahedron that H3 is built on, and finds the nearest point of the hexagonal lattice there, which is how H3 itself divides the globe, pentagons included. Only a cell that is being hovered or checked is drawn as its own geometry, so that it can lift out of the ground and flip over.
Every wrong guess lights up its cell for everyone, so the globe slowly fills with light where people have searched. The lights are drawn per pixel too. Each map tile keeps its guesses in a small texture that holds how many there are in each patch, their average color and where they sit, and the shader reads that texture. Drawing a frame costs the same however many guesses the hunt has.
On a sphere the size of the Earth, a 32-bit float can only place a point to within about 0.76 m, which is coarser than the detail in my finest tiles, and shaders have no 64-bit floats. So the camera math runs in f64 on the CPU, each tile’s matrix is composed in f64 every frame, and the shaders work in small tile-local coordinates. Not something I foresaw when I first started making this, and there was probably a better way to do it.
Elevation tiles give every guess cell a height, so up close the terrain is made of small hexagonal facets. The heights are compressed with a predictor over the neighboring cells and an LZMA-style range coder, and decoded in the same worker.
The style started out as a copy of OSM Bright, to match the reference. The map that shipped is the Earth by moonlight: dark, with buildings as blocks, the guesses as the lights, and only place, state and country labels. The backdrop is the real sky from the Bright Star Catalogue, turned to the current sidereal time.
Serving and cost
A bake is two archive files, one for the map and one for elevation. The tile server is 110 lines of Rust. It memory-maps both, binary-searches their indexes, and returns a cell’s map and elevation in one response.
Tiles are cached immutably by Cloudflare’s CDN, so the total traffic hitting the origin is under 100 GB per day, which is essentially free on a Hetzner box. The server itself costs under $200 a month, where the last hunt’s tiles cost over $400 for two weeks, and I get an EPYC and half a terabyte of RAM to play with. It can probably handle 1,000 concurrent players, though we are not near that scale yet. It’s currently not under much load, and a $30 droplet with enough storage could likely have handled it.
Keeping the secret
The fun part was pacing the guesses and securing the treasure location. Worse than having your production server hacked would be having it also leak the treasure location. I wanted to design a system where the treasure location is somewhat safe even if the main server gets hacked.
With the main server being behind a Cloudflare tunnel, it’s already unlikely. For good measure, I hardened the server and all its services best I could.
Next, I put the actual treasure location on a separate server on a separate hosting provider. I call this the oracle. The oracle only talks to production over a WireGuard connection. A question is nine bytes, an opcode and a cell, and the answer is one byte. It answers guesses yes or no, and itself keeps track of how many guesses have been minted and how many have been answered. That way, a hacked production server couldn’t send a billion guesses to the oracle to get the treasure location by brute force. The oracle decides how many guesses are minted, and the production server has no say in it. Furthermore, it will only answer 100 guesses per second. The whole oracle is 94 lines of Python with zero dependencies.
A similar setup is used for the image receiver server, which talks to the production and backup cameras, but only passes on to our production server the things players are meant to see.
Pacing and pricing
The guesses themselves are minted at a rate set on the oracle, which started at one guess per second when the hunt began, and which I can adjust to steer pacing. Some go to free players, who are captcha-gated, while the rest go to a dynamically priced pool, so the price adjusts based on demand.
Whenever people buy, the price goes up, and then it falls back down during quiet periods, so timing when you buy is part of the game. The pool is something called a constant-product AMM (automatic market maker). It sets the price by having a pool of guesses and a pool of (virtual) dollars, and the product of the two always stays the same. So when I put in guesses, I pull out virtual dollars, and when you buy guesses, virtual dollars get added to the pool, and guesses get taken out, so that the product stays the same. As for the real dollars, half or more of sales go to growing the treasure.
Free guesses are all in a long line, and whenever a free player completes the captcha, they get assigned a place in line. The line then walks forward at a constant speed, so the player can be informed of the wait time. After their place in line comes, they get the free guess.
Why not just use MapLibre?
Throwing H3 hexagons on top of MapLibre GL would’ve saved a lot of time, but it wouldn’t have been as cool. This way the guesses, the lights and the tiles all sit on one grid, and the map loads in big flat hexagons.
The treasure is still out there. Have a go at needle.global and make your guess!