Adding more nodes
Ok, we've got a working map generator now, but the maps it generates aren't very exciting yet. So we're going to make it a bit more interesting by adding some more nodes to the graph.
Creating a cave-like map
Start by opening up the Map Graph editor again.
First, we're going to add a Cellular Automata Smoothing node. This is a great node for creating cave-like patterns:
- Right-click inside the editor and select Add Node and Textures/Cellular Automata Smoothing.
- Place the new node between the Randomly Fill Texture node and the Texture To Tilemap Data node.
- Connect the Texture output port of the Randomly Fill Texture node to the Texture input port on the Cellular Automata Smoothing node.
- Connect the Cellular Automata Smoothing node's Texture output port to the Texture input port of the Texture To Tilemap Data node.
Create a constant integer node:
- Open the node creation menu and select Constant/int.
- Set the node's value to 5.
- Connect the constant node's output port to the Number of Passes port.
The number of passes determines how smooth the result is going to be, at the cost of performance. Having just 1 pass takes the least amount of time to run, but it's also going to give the least smooth result.
We're also going to need to change the fill percentage, as the current 10% will get completely smoothed away:
- Change the value of the float constant node connected to Fill Percentage to 50.
Add two more Named Color nodes:
- Open the node creation menu and select Colors/Named Color.
- Set its value to Wall.
- Connect it to the Fill Color port on the Cellular Automata Smoothing node.
- Create another Named Color node.
- Set its value to Floor.
- Connect it to the Empty Color port on the Cellular Automata Smoothing node.
Go back and to the scene view and run the graph runner again.
Now it's beginning to look like a real map!
Run the graph a couple of more times and you might notice two obvious issues with the maps it generates.
- It creates rooms that aren't connected to each other with floor tiles, meaning that some spaces would be unreachable.
- The outer edge can contain floor tiles meaning that once you have a player character running around, they could walk off of the map.
Drawing a border
To fix the latter issue, we're going to draw a border of wall tiles:
- In the graph editor, open the node creation menu and select Add Node and Drawing/Draw Border.
- Connect the Texture input port to the Randomly Fill Texture node's Texture output port.
- Connect the Texture output port to the Cellular Automata Smoothing node's Texture input port.
- Add a constant integer node, by opening the node creation menu and selecting Constants/int
- Give it a value of 1.
- Connect it to the Width input port on the Draw Border node.
- Add another Named Color node and set its value to Wall.
- Connect it to the Border Color port.
If you run the graph again, you'll notice that the map will always have a border of wall tiles on the outer edge of it.
Drawing connections
That's one issue solved! Now to make sure that all the floor tiles are always connected to each other:
- Add an Extract Areas node, by selecting Areas/Extract Areas.
- Connect its Texture input port to the Cellular Automata Smoothing node's Texture output port.
- Add a Named Color node and set its value to Floor.
- Connect it to the Color To Extract port on the Extract Areas node.
- Add a Generate Area Graph node, by selecting Areas/Generate Area Graph.
- Connect the Area ports of the Generate Area Graph node and the Extract Area node to each other.
If you run the graph now, you won't see any difference for the generated maps yet (as we don't do anything yet with the output of the new nodes), but you can see the preview images on the nodes in the Map Graph editor.
The Extract Areas node's preview will display the areas it has extracted. White pixels represent the inside of each area and the borders are represented by gray pixels.
The Generate Area Graph node's preview displays a visual representation of the area graph that it generates by showing the edges between the areas. We can use these edges to create connections between all the areas:
Next:
- Add a Connect Area Graph (Closest Border Points), by selecting Areas/Connect Area Graph (Closest Border Points).
There are a couple of different Connect Area Graph nodes. This particular one, connects each pair of areas by finding the border point on each area that result in the shortest possible connection between the two areas.
- Connect the Area Graph ports of the Connect Area Graph (Closest Border Points) and Generate Area Graph nodes to each other.
- Add a Draw Connections (Shortest Path) node, by selecting Drawing/Draw Connections (Shortest Path).
Connect Area Graph creates pairs of points that should connect areas to each other. The Draw Connections node does the actual drawing of those connections onto the texture.
- Connect the Connected Points ports on the Draw Connections (Shortest Path) node and the Connect Area Graph (Closest Border Points) node to each other.
- Connect the Cellular Automata Smoothing node's Texture output port to the Draw Connections (Shortest Path) node's Texture input port.
- Connect the Draw Connections (Shortest Path) node's Texture output port to the Texture input port of the Texture To Tilemap Data node.
- Add a Named Color node and set its value to Floor
- Connect it to Draw Color input port on the Draw Connections (Shortest Path) node.
- Add a constant integer node, by selecting Constant/int and set its value to 3.
- Connect the constant int node's output port to the Width input port on the Draw Connections (Shortest Path) node
This determines how wide the connections will be between each area. Setting it to a value of 3 makes the connections 3 pixels wide.
Run the graph again. And you'll see that everything is now connected!
However, you might notice that the number of connections is more than what is required to connect everything together, which might not be what you want, depending on your use case.
If you want the least possible amount of connections possible, you can add another node. Namely, the generate area graph (MST) node:
MST (Minimum Spanning Tree) basically means that it will connect the areas together in the least amount of connections required.
- Add a Generate Area Graph (Minimum) node, by selecting Areas/Generate Area Graph (Minimum).
- Place it between and connect it to the Generate Area Graph and the Connect Area Graph (Closest Border Points) nodes.
Many of these nodes can be replaced by subgraph nodes instead, such as a Extract Areas & Draw Connections or a Connected Caves nodes. You can also create your own subgraphs to reuse a specific sequence of node, without having to recreate that sequence each time. You can find more about subgraphs on the "Running a graph within a graph (Sub graphs)" page.
Next steps
Nice! The generated maps are a lot more interesting now.
They're still a bit empty though, so the next tutorial will show how to handle random placement of things like player characters, enemies and items.