interblog

City generator: cleaning up the river algorithm

Cities with rivers

(Originally posted on Tumblr on 2025-02-20)

back on the city generator again, I got distracted by the blog stuff. those posts are on the blog too in case you care..

this is a small update. the river algorithm was like 90% good, but occasionally we'd get some terrible instances where it would just end in the middle of the map for no reason, so I tightened up the algorithm and accounted for some end-caps not going entirely off the map. I don't have any of it misbehaving so you'll have to live with more pictures of it behaving correctly.

the previous algorithm was basically:

  1. pick a random edge polygon (north/east/south/west)
  2. set the goal as the opposite end of the map (no specific polygon)
  3. randomly choose a neighbor polygon (with an adjustable bias towards the other side of the map) and add that to your path
  4. repeat until you've reached the other side of the map

this is actually a little more complex than it needs to be. the main simplification that I added was to only start on the north or west, and then always go to the south and east respectively. it does the exact same thing since we're always going north/south east/west anyway. removing this cleaned up the code by a good bit since there was less to keep track of and whatever hidden bug that was causing the original issue appears to be squashed.

next, I think I need to start breaking these city districts up into blocks and placing some buildings down.