Towns
评价数不足
Towns Map Creation: Part 2: Water: Adding Lakes
由 Varsh 制作
Continuing on from Part 1 you will now be creating randomised lakes to your map to make things look a little more interesting.
   
奖励
收藏
已收藏
取消收藏
Notes
Creating lakes is the next part on the agenda in creating a more believable map. In this part you will understand how to create lakes at specific and random positions on the map using constraints and varying the size of the lakes.
Giving the map more varying layers
Now that we have our base map we need to make it a little more interesting, having just a plain isn't any fun, especially if there isn't anything to do but dig down! To make things a little more interesting we will layer the map with three different terrain types, we'll have the top layer as grass, mud below that, and then stone from then on.

Open up your gen_map.xml file and for the mainTerrain change it from “grass” to “grass,dirt,stone”. You should have the following:
<mainTerrain>grass,dirt,stone</mainTerrain>
Just to make sure that it has worked load the game and then run your map, start digging and you should say the layers change until you hit stone.
The map size
Before we start adding lakes and rivers we need to know a few things about the map in general. Firstly each map is made up of 40,000 tiles in a 200x200 (0 to 199) grid, essentially this will mean that the centre of the world is a set of 4 tiles which are 99x99, 99x100, 100x99, 100x100. If we wanted to create a lake at that specific location then we can start the lake at any of those 4 positions.
Creating the seed
After the map's initialisation phase all terrain is created via “seeds”, these seeds can be either randomly generated or be to a specific point on the map including having any number of them created on map randomly. For this tutorial we'll create a lakes that will vary between 1 and 3 within a close proximity to the centre of the map.

Firstly let's add a new seed, this see we will call “seedLake” so after the </init> on a new line we'll add the following:
<seed id=”seedLake”> <!-- This is where the lake information goes --> </seed>
This will create an empty seed ready for use, but without anything in there nothing is going to happen, so let's start adding our lakes then.
Adding the Water type
Some terrain in the game is hardcoded meaning that you cannot give a new terrain “type”, Lava and Water are two of these and they have specific ways for the type, as this is based on water we'll want to add our type as “_WATER_”.
<type>_WATER_</type>
Setting a random number of lakes
We've already decided that we were going to have a random number between 1 and 3 for the amount of lakes we are to have on the map, we can do this by rolling a virtual die, to keep things simple we will use a single 3-sided die to roll and give us the result. The way the game let's us do this is by the simple formula “1d3” where it means “use 1 die which has 3 sides”. Having “2d5” for instance would mean “use 2 dice which have 5 sides each” so you could potentially end up with anything between 2 and 10 lakes. For what we want we'll keep to a maximum of 3:
<num>1d3</num>
Which layer to put the lakes on
A few more things are required to show the lake on the map, the position and the level it is starting on. Having the water start on level 23 will, you guessed it, drown all of our citizens! We can't have that now can we so we'll make sure that the lakes start on the last layer on the outside. There are 24 layers on the outside so having the water start on level 24 will remove the grass tile at that location.
<level>24</level>
Randomising lake positions
To get the random position of the lakes we don't actually need to do anything, the game will automatically choose a random position on the map if no position is given. As we want the lakes to be away from the edge of the map and more towards the centre we'll use a position so that when the map is generated it will be away from the edges.
<pointx></pointx> <pointy></pointy>
Leaving out these tags will let the lake appear anywhere. Adding them though we can point it towards the centre as we know is 99 and 100, so we could have:
<pointx>99</pointx> <pointy>99</pointy>
But if we did that then the lake will always appear there, having 3 lakes on top of each other is a little pointless so to randomise things we'll add a die roll. Let's have the lakes so that they are at least 20 tiles away from the edge, this would mean that there are 40 tiles unused at the edges, 20 at one end and 20 at the other. To make sure we keep this in check we will always add 20 to the random number generated.

The question is what will we add for the random number?

Currently we have 160 tiles to play with so having a random number up to 160 is desirable so “1d160” would seem reasonable and you can use that, to add the 20 on the end you would have a formulae of “1d160+20”, this way you will have a tile used anywhere between 21 and 180 leaving at least 20 either side.
<pointx>1d160+20</pointx> <pointy>1d160+20</pointy>
Try running the map now and see what happens, you should see anything between 1 and 3 positions where a single tile of water will be, these are the starting points for where the size of the lakes will be created and what we will be sorting out now.
Sizes and percentages
From the initial point of creation you can create a new water tile by telling it to move in that direction, there are only 6 directions in which you can create new tiles: north, south, east, west, top and bottom, of course I would highly advise you not to create a new water tile upwards otherwise you will eventually flood your plain and drown your citizens!

So for the creation process we will only use north, south, east, west and down. In order to create these new tiles we need to have a set number of turns in order to go by to create the lake, so from the initial tile let's say we want the water to go 10 spaces to the north, we will set <turns> to 10 and have <northPCT> set to 100.

You probably have just noticed that north is <northPCT> where “PCT” means percentage. 100 means that it will always be true and 0 will mean that it will never happen, setting a value of 50 will mean that there's a 50/50 chance of a new tile to be created. To make sure that you can see what is happening though we will set it to 100.

For the other directions we will use <southPCT>, <eastPCT>, <westPCT> and <downPCT> each with 100 as their values so that it will always happen. You should have the following:
<turns>10</turns> <northPCT>100</northPCT> <southPCT>100</southPCT> <eastPCT>100</eastPCT> <westPCT>100</westPCT> <downPCT>100</downPCT>
Try out the map and you should now see lakes appearing on the map clearly this time around, not only this but each of the lakes are now 21 tiles in diameter (corner to corner). Play around with the settings for the lake to see what you can come up with. For the rest of the tutorials I will be keeping it at these values.
Code
Here's the code for the gen_map.xml file with the new additions:
<?xml version="1.0" encoding="UTF-8"?> <gen_map> <init id="init"> <numLevelsOutside>24</numLevelsOutside> <numLevelsUnderground>40</numLevelsUnderground> <mainTerrain>grass,dirt,stone</mainTerrain> <numCitizens>100</numCitizens> <startingLevel>23</startingLevel> </init> <seed id="seedLake"> <type>_WATER_</type> <num>1d3</num> <level>24</level> <pointx>1d160+20</pointx> <pointy>1d160+20</pointy> <turns>10</turns> <northPCT>100</northPCT> <southPCT>100</southPCT> <eastPCT>100</eastPCT> <westPCT>100</westPCT> <downPCT>100</downPCT> </seed> </gen_map>