Grain controls the clustering of things. High grain values makes things spread out more; low grain values clump them together more.
One example is with landmasses. A continent grain of 2 will generally produce continents, a grain of 3 will generally produce more continents that are smaller, a grain of 4 will generally produce archipelago, and a grain of 5 will generally produce many tiny islands.
Here's an old post of mine when I wasn't very knowledgeable of fractals or what "heights" exactly were:
http://forums.civfanatics.com/showthread.php?t=484233
A fractal is just a matrix of values, but I believe the numbers aren't just a random mess. Instead, they work off of one another and steadily decrease or increase relative to adjacent points (I believe), so that if you blanketed them all with a plane, you get an image like this:
I would imagine the way grain plays into these fractals is that it makes those increases and decreases between points more or less steep. So, a fractal with a low grain would be a smoother plane with ridges that have gradual slopes. A fractal with a high grain would be spikier with ridges spread-out and with steeper slopes.
In that old thread, the idea clicked when I thought of it as a 3D plane inside of an aquarium, lol. So when you obtain the height value you're looking for, you mark it on the side of the aquarium and fill it with an opaque liquid up to the line. Now, when you look straight down at the plane from above, you'll see the ridges which protrude out of the water and pass the height test. So, if it was a height value applied to a forest fractal, all those ridges that are still visible would be forest.
Though, there are other things to consider. Say that was marsh, for example. Marsh usually has a height of 92 (or 8% marsh on maps). But, it really isn't 8%. The fractal doesn't know that, and it surely isolates 8% of the relative height of the fractal values. But, when you apply it to a map under construction, other things play in. Such as, mountains and hills already placed on grassland tiles that could potentially pass the height test; they block marsh.
The same can be said for jungles and forests, which are placed on grassland tiles (note: all jungle is later converted to plains using a final adjustment function). Marsh is placed before them, so even though it's a small amount, it blocks-out some of the forests and jungle. If one were to dramatically increase marsh percentages, you would probably want to have them placed in a different order, behind forest and jungle; unless marshes are that important to one's script.
Hope that helps.
EDIT: Oh... and as for the fractal values, I believe they work off of a value range of 0 (or 1?) - 255 (or 256?). As I tinkered with some print outs to get information while testing things with my script and that seemed to be the general range (on Standard size anyway... not sure if it is relative to map size, but I very much doubt it). An example can be seen here which may prove this:
Code:
function TerrainGenerator:GetLatitudeAtPlot(iX, iY)
-- Terrain bands are governed by latitude.
-- Returns a latitude value between 0.0 (tropical) and 1.0 (polar).
local lat = math.abs((self.iHeight / 2) - iY) / (self.iHeight / 2);
-- Adjust latitude using self.variation fractal, to roughen the border between bands:
[COLOR="red"]lat = lat + (128 - self.variation:GetHeight(iX, iY))/(255.0 * 5.0);[/COLOR]
-- Limit to the range [0, 1]:
lat = math.clamp(lat, 0, 1);
return lat;
end
Terrain doesn't use a hard latitude calculation, or else it would look horrible and the band lines would be too straight and unnatural looking (I've tested things with no variation while reworking the default terrain bands, so I get them precisely the way I wanted before reapplying the variation again). Instead, it applies a variation to the lat so the terrain checks think the plot is actually higher or lower than it really is.
Using the formula, highlighted in red, one can see that it creates a variation value roughly between -0.1 and 0.1 (equal to 4 rows on a Huge map, since they're each 0.025).
Using the extreme ends of the fractal:
(128 -
0) / (255 * 5) = 0.1003
(128 -
255) / (255 * 5) = -0.0996
If you wanted to decrease the variation you could multiply the denominator by 10 instead of 5, which would halve the results to a range of only roughly -0.05 and 0.05.
I customized these calculations to multiple variation methods which apply to different terrain bands. Some will favor more blending towards the inner part of the map and restrict blending towards the outer parts, other terrains will favor blending towards the outer parts of the map and restrict inner blends more. It comes out really nice.
- - - - - - - - - - - - - - - - - - - - - - -
Here's another little hint, which I've applied to my script:
It's very minor, but latitude is off on all maps and latitude checks. Since all maps have even number heights and a single inner row set to 0, you never get horizontally symmetrical maps.
Using the default formula:
Code:
local lat = math.abs((self.iHeight / 2) - iY) / (self.iHeight / 2)
The bottommost row will always be 1.0 and the topmost row will never be 1.0. On a Huge map, the topmost row would be 0.975. On a Standard map, the topmost row would be only 0.963... it gets worse on smaller maps. You can see an example of this by looking at ice formations at the bottom of the map, they'll always have an additional row of ice. (It drove me crazy until I finally understood and figured this all out.)
I use this method when calculating latitude:
Code:
local lat = 0;
if (iY >= (self.iHeight/2)) then
lat = math.abs((self.iHeight/2) - iY)/(self.iHeight/2);
else
lat = math.abs((self.iHeight/2) - (iY + 1))/(self.iHeight/2);
end
With this, you get a horizontally symmetrical and balanced map. Within the equator, you'll have two rows equal to 0 instead of only one.
To be fair, the default method may be intentional due to the viewing angle of the game's world. But, I personally feel it isn't necessary and it looks just fine with my modded method (and I'm VERY picky!), especially since it's just ice down there. Plus, think about it, what would you rather have: another row of ice or another row of useful equatorial land?