返回资讯中心
外部精选
软件工程
#开发者实时流

开发者自建全球寻宝游戏:金币藏户外,靠猜1.66万亿个六边形定位

来源标题:Building a global interactive treasure hunt

一名开发者搭建了全球互动寻宝游戏:一枚一盎司金币藏在户外并由摄像头看管,玩家看照片在全球1.66万亿个六边形网格中猜位置,最先猜中者即可去取走。为省下地图瓦片费用(上次两周寻宝花了超400美元),他这次自建地图服务,并解决了位置保密和猜测节奏控制两个问题。截至目前金币仍未被找到。

Hacker News 最新jazzzooo2026/11/27 · 今天30 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

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.

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.

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.

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.

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.

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.

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.

Lake Label

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.

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 Brig

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →