Mountains from Pixels, Rain from Math

With terrain generation done, along with the segregation of land and water, we can move on to more complex matters. Here’s where things become more subjective and creative. What terrain types are necessary? How do we handle climate?

I decided to start with basic terrain types contingent on elevation. At the lowest level is water, which isn’t really a “terrain,” but it’s there for the sake of making the terrain map look sensible. Who wants to look at a black ocean, seriously? Yes, it really is that simple a decision.

The various terrain types are based strictly on elevation. Further nuances are defined later by climate and biomes. The elevation scale is in shades of gray, from 0 to 255, with 0 as water. The rest are:

  * Wetlands (shades 1-5)
  * Floodplains (6-25)
  * Plains (26-75)
  * Foothills (76-125)
  * Highlands (126-170)
  * Mountains (171-255)

I’ll admit those numbers are rather arbitrary, but they result in a reasonable terrain distribution. Here’s an example:

[caption id=“attachment_43” align=“alignnone” width=“600”]terrain map Example of a colored terrain map.[/caption]

[caption id=“attachment_44” align=“alignnone” width=“178”]terrain color legend Terrain Types[/caption]

As you can see, the whole gamut of terrains is covered.

The terrain definitions, like many features of SagaSim, are parameterized and customizable. It will therefore be possible for people to tweak the terrain definitions and do fairly wacky things, if they’re so inclined.

Next up is climate! In SagaSim, climate is a function of two things: elevation and latitude. Rather than completely reinvent the wheel here, I decided to make use of the Köppen climate classification system. SagaSim’s version is rather simplified, though I’ve left the door open to make it more realistic in the future. For now, the idea is to just have SagaSim generate climate zones for the world map so the later generation steps can be carried out.

The climate system is also parameterized. I’ve currently defined 14 different climates, as based on the Köppen system:

  * Af
  * Am
  * Aw
  * BWh
  * BSh
  * Csa
  * Cfa
  * Cfb
  * Cfc
  * Dfa
  * Dfb
  * Dfc
  * Dfd
  * EF

Each climate has four parameters: its minimum base latitude, its maximum base latitude, whether it’s considered a rainy climate (a simple true/false value), and the color to use for it on the map (same colors as those in the Wikipedia article.) I say the latitudes are “base” latitudes because the climate generation algorithm doesn’t have to precisely respect them. Instead, there is a fair amount of variation allowed.

Climate generation is done by making multiple passes over the map. On the first pass, there is no existing climate information, so the algorithm starts from scratch. On each land pixel, it looks at the 8 surrounding pixels to see what climate times are immediately adjacent. It also determines which climates are possible at the current pixel, based on latitude. Then, a probability is calculated to determine whether to choose an adjacent climate type or a new one. It is heavily weighted toward picking an adjacent type. The formula is:

1 - (1 / (height * width * 0.25)

This will normally generate a very small number, roughly 1% on most maps. Then a random decimal is chosen. If it falls below the probability threshold, then we will pick a new climate from the available types, rather than an adjacent one.

This is done for every land pixel on the map, starting at the top left and working our way toward the bottom right. Next, we do a few more passes, doing a very similar calculation: for a given pixel, look at the surrounding pixels and choose one at random to assign to this pixel if we meet the probability threshold (about 5% in subsequent passes.)

At the end of the process, we have something like this:

[caption id=“attachment_45” align=“alignnone” width=“600”]climate map Example climate map.[/caption]

The “banding” effect is a consequence of the latitude restrictions and the probability checks. This allows for substantial climate variation without having climate zones go too far out of their assigned latitudinal ranges.

One shortcoming of the current climate generator is that, unlike the perlin noise generator, it does not produce a seamless map. If you were to wrap the above image around a sphere, it would be discontinuous at the left and right edges of the map–the climate zones wouldn’t line up. I intend to fix that, though I’ve not yet gotten around to it.

It might seem like we haven’t done much so far, but it’s all a means to an end. Next time, I’ll get into how rivers and biomes are generated. They’re more complicated than (and dependent on) terrain and climate, so they may get split into two articles.