To Infinity... and Beyond!

Posted at 8:39 AM on Jan. 8, 2007

There's a bit of code I wrote for UltraCorps that generates star systems: a single homeworld, around which 9-12 unowned worlds orbit, in varying distances from the homeworld. In a solo game, there are supposed to be exactly 12 unowned worlds to capture.

The map builder starts by picking a random point around the circle, then starts filling in worlds, depending on how far they should be from the homeworld. The one requirement is that a world not be closer than 20 units to any other world (this prevents cases where worlds appear to overlap on top of each other). If it finds another world closer than 20 units away, it discards the point and loops back to the beginning to choose another point. Repeat until a usable point is found...

In the original code, I had a recursion limiter; after 30 iterations, it gave up and skipped the world. This threw a wrench into the "exactly 12 worlds" requirement, though. So I came up with what I thought was a good solution: if it couldn't find a point at one distance, push back to the next farther-out distance. I was so sure that this would solve the infinite-loop problem that I did the unthinkable: I removed the recursion limiter.

Big mistake. Never assume that code with the potential to run away in an infinite loop will not actually do so. And, judging by the near-meltdown of the server several times in the last two days, I'd say that was indeed the case.

So, back to square one. Actually, I think I have another workaround for the problem... but this time I'll be sure to leave the infinity-stopper in place. Just in case.

Home Permalink No comments