Internship - Week 6 - Bigger roads
Hey there,
Today we will have a look at how we can make the roads that we created in week 3 and 4 bigger. Currently all the roads in ProDnD are one tile wide, It is about time this changed. We are going to make the roads wider depending on their length. Short roads will have a width of 1, medium roads a width of 2 and you guessed it, long roads will have a length of 3. Now it is our goal to find a need and quick way to calculate the width of a road.
Figure 1. How it used to look. How it looks after this week.
The first task would probably be, finding the start point of each road. This will be slightly more tricky than you might have though at first. The road, of which we are trying to find the length, could look something like this:
Figure 2. Road made out of segments.
In this case the road consists of multiple road segments that were created one after each other. The red tiles resemble the start point of a new road. If we would simply save the lengths of all the roads that we create to an array, we will run into some issues. The road could be part of a much larger road and we would like to have the the length of this larger road.
Instead of saving the road's properties, we are going to search the map until we find a road tile. When a road tile is encountered, we will work backwards from this tile until we discover the start point of the road. We have discovered the start point when the next tile in the backwards direction, is no-longer a road tile.
Figure 3. How to enlarge roads.
Depicted in picture 3-1 to 3-3, you can find the steps taken to find the start point.
After finding the start point, we want to calculate the length. We do this by finding the end point of the road and calculating the distance between the start and end point. The same steps that were used for finding the start point, will be used for finding the end point. The only difference is that we start from the start point. Now we are able to calculate the length.
Calculating the width is quite easy. We take the Size of the map and divide it by the max width our roads can have. In our case this is 3. Lets call the outcome LenghtPerWidth. Now we take the length of the road and divide it by the LengthPerWidth. This will give us a floating point number between 0 and 3, but tiles and floating point numbers do not get along very well, we would like the outcome to be an integer. This is why we take the outcome and round it up to the next integral number. In general, humans prefer their roads to be visible, and as I am quite certain we are all humans, the road should never have a width of zero. This is why we are rounding up the floating point number.
With the start point and width, we should be able to enlarge the roads. we will enlarge each road tile from the start point to the end point. When enlarging a road we look at the direction of a road and either change the tiles horizontally or vertically next to it. This can be seen in picture 3-5, in this picture the road is widened to the right.
When the algorithm is done with widening the roads, we will have a nice result were the longest roads are the widest and small roads are thin. This will look something like this:
Figure 4. Before. After enlarging.