Tutorial: Map Generator - Algorithms & Transformations
This is the second part of the map generator tutorial. The first part can be found here, and should be completed before trying this part of the tutorial. Here two aspects of the map generator will be explained: algorithms and transformations. Both are attributes to the objects map and overlay, although using them in overlays makes more sense.
Algorithms can be specified within an overlay with the following syntax:
algo=<algorithm name>;
All algorithms available for the map generator can be found here in the documentation. An algorithm specifies which points of the overlay should be drawn, it provides a function which maps a point (x,y) to a boolean value. Stated more simply an algorithm cuts holes in the mask of its overlay, giving a certain fill pattern. Some algorithms have - up to two - parameters, which define their behaviour. These parameters are also attributes of the overlay, and are specified with the syntax:
a=<value>; b=<value>;
Some algorithm possess properties of randomness, others do not. In this part we will only consider non-random algorithms, so that the basics are not obfuscated. The next part of this tutorial however will explain random algorithms.
To adjust the fill pattern created by any of the algorithms to your specific needs, there are transformations. Transformations are attributes of an overlay and affect the algorithm in that overlay. There is the turbulence transformation, which shifts the points of a mask using a sine curve. This somewhat more complicated transformation will be discussed later, in the part we restrict ourself to the three affine transformations available:
- Zooming, specified by
zoomX
andzoomY
for the x- and y-directions respectively. - Translations,
ox
specifies the translation in the x-direction andoy
in the y-direction. - Rotation, specified by
rotate
, a positive value implies a counter clockwise rotation.
All these transformation will be discussed in detail in the following examples.
Alternative Checkers Board
We ended the previous part with an exercise about logical operators and set theory. The landscape shown in the figure had to be constructed, we will now see that there are alternative methods to construct such a landscape. Such an alternative is to use an algorithm. We need the checker
algorithm, which creates an overlay consisting of alternating boxes of size ten in standard zoom. Thus we have a 10x10 checkers board, which is a problem since we want a 5x5 one. So we want to reduce its size by a factor two. For this purpose we can use the zoom transformation, with attributes zoomX
and zoomY
. The zoom attributes take integer values from -100 to +99, where negative values zoom out and positive values zoom in. For the ones who want to know: the zoom factor is calculated as zoom factor = 100 / (100 - zoom value)
. All we need to know is that the value 50 results in zooming in by a factor of two. All together results in the following code.
// Checker algorithm at work. map ExampleChecker { mat=Gold; tex=gold; overlay { mat=Rock; tex=rock; algo=checker; zoomX=50; zoomY=50; }; };
Convince yourself that this produces the same landscape as in the exercise. Here we have encountered a major advantage of algorithms, they reduce the amount of overlays needed drastically. Play around with the zoom factors a little so that you get an understanding of how they work.
Bozo Algorithm & Rotation
Let's introduce another algorithm, therefore consider the following piece of map generator code.
// Golden specks. map ExampleBozo { mat=Rock; tex=rock; overlay { // A regular bozo algorithm on the left side. x=0; wdt=50; algo=bozo; } | overlay { // A modified bozo algorithm on the right side. mat=Gold; tex=gold; x=50; wdt=50; algo=bozo; a=10; rotate=45; }; };
In this example the bozo algorithm is used. This algorithm cuts out everything except some ellipse shaped specks, it will take one parameter a
. Higher values of a
will reduce the size of the specks. On the left side of the figure the regular bozo algorithm is shown, on the right side the specks were reduced in size and rotated. To apply the rotation one has the attribute rotate
, which takes values from -180 to 180. The value 45 like in this example means that the whole pattern is rotated counter clockwise over 45 degrees. Rotations form an essential tool in transforming algorithms the way you want them to work, and we will encounter them again in more elaborate examples.
Landscape With Hills
It is time to produce a first real landscape, we want to create a landscape with two hills separated by a lake. The following piece of map generator code achieves that.
// Two hills separated by a lake. map ExampleHills { // First raise the water to a certain level. overlay { mat=Water; tex=water; x=0; y=50; wdt=100; hgt=50; }; // Then overlay two hills using the sine algorithm. overlay { mat=Earth; tex=earth; algo=sin; zoomX=60; zoomY=35; ox=25; oy=35; }; };
So what did we do in here? First the bottom half of the map is filled with water, after that the two hills are overlaid with the sin
algorithm. If the sine curve is overlaid in such a way that a part of the water is not covered by the sine's overlay a lake arises. To achieve this the standard sine curve created by the sin
algorithm has to be transformed in the right way.
[TODO: Explanation here too]
Exercise: Goldmine
In this exercise you must try to create a simple map suited for a goldmine goal. Create a map with some hills and fill the earth with some golden specks. Try to construct something looking similar to the landscape in the figure.
[TODO: solution as some sort of spoiler]
// A simple goldmine scenario. map ExerciseGoldmine { // Use the sine algorithm for some small hills. overlay { mat=Earth; tex=earth; algo=sin; zoomX=20; zoomY=-20; ox=20; oy=40; // Use the bozo algorithm for some gold in the earth. overlay { mat=Gold; tex=gold; algo=bozo; a=10; }; }; };
Summary
In the previous tutorial you learned about overlays and combining them with operators. This part covered algorithms and transformation to manipulate them. So now you have all the tools needed to create landscapes with the map generator, but still there is an essential tool missing to create interesting and dynamic landscapes. These are random algorithms and turbulence and are covered in the next part of this tutorial.