Everyone needs a home, even these mean old ghosts. In the Pacman games the ghost's home is usually located in the middle of the maze and is easily recognized as a big rectangular box. Pacman is never allowed to enter this region. The ghosts themselves don't go in unless they've been eaten by Pacman and they need to respawn. Three of the ghosts also start in this area when a level starts. Other than those situations this area remains pretty much empty. In the image on the right this area is the large empty rectangular area in the middle of the maze. The ghosts are going to have to sometimes enter this area so we'll need to add some more nodes so that they may do so.
There are some challenges to this that I should mention. As you can see in the image on the right, we need to add in 8 more nodes. Why didn't we just include these nodes in the first place? The main reason is because we want to center these 8 nodes horizontally. If you look at the maze1.txt file you'll see that it has an even number of columns, 28 to be exact. There is not integer that describes the middle since the middle has the value of 14.5. If we had an odd number of columns like 29, then that would be different since the middle would be 15. But Pacman mazes are meant to be symmetric horizontally so they should all have an even number of columns. The point is in a text file I can only show integer positions and not float positions so I can't have these nodes in the maze.txt file. So we have to add them in later. I'll show you how to do that now.
I wish there was a nicer way of doing this section. I admit that this isn't the most elegant way of doing things, but it's the best I can come up with at the moment.
The new method we're creating here is called createHomeNodes and it needs an xoffset and a yoffset. I'll explain that in a second. Notice the homedata, that should look familiar. It's similar to what we have in the maze1.txt file. It's the symbols for nodes and the paths between the nodes. We can place it in a text file and read it in, but it's so small I figured it's fine to just have it here. As you can see it defines 8 nodes. What's nice is that we can just call the same 3 methods we called earlier on the maze file, but this time we're passing in the offsets so that it can be properly positioned where we want it to be positioned.
The key that we're returning is the key to the top node of the homedata. We have to add the 2 because the offset describes where to position the top left corner of this array. We're also returning it because I want to use it for the next method which is described below.
The code above will create and correctly position the nodes as long as you give it the right offsets, but you just wouldn't be able to interact with it. It should be connected to the other nodes otherwise if you place any ghosts in there, they would never be able to escape and nothing would ever be able to enter. Pacman should never enter, but the ghosts need to enter when they are in SPAWN mode, for example.
So what this method does is it just connects the topmost node to whatever other node we want. We also need to specify a direction. For example, lets say there's a node to the right of the home node that we want to connect to. I need to specify the key to that node and also the RIGHT direction. This will connect the two nodes together in both directions. So the home node will connect to the other node on the LEFT. This will overwrite what was previously in the other nodes LEFT value.
We're going to add a few new lines here, with some more hard-coded values... We'll take care of that later. As you can see after we create the home nodes I call the connectHomeNodes twice because I want to connect the home node to the a node on the LEFT and a node on the RIGHT of the home node. How did I come up with these values? I just looked on the maze1.txt file and counted.
If you run this code you should see something similar to the image at the top of this page where the new nodes appear in the middle. You can direct Pacman to enter into these nodes and the ghost will also enter into these nodes. For now that's fine, but we'll have to restrict access to these nodes later.