Pathfinding updates and Spamocalypse Objective system

Somebody found a few bugs in Spamocalypse, which turned out to be an issue with my pathfinding code. I had accidentally overwritten the navmesh when I generated one for a new level, which was caused by the meshes not being loaded beforehand. I’ve added a fix for this, which is to create a static constructor to the BuildMeshEditor class in my repository. That will call the GameManger.LoadNavMeshes method.

The way static constructors work in C# is that they are always called first, and apply to all instances of the class. The constructor for the BuildMeshEditor class is called as soon as you select the cube that defines its boundaries, so the meshes are loaded in the background. If you then build a mesh for a particular level, it will be added to the list of meshes, or update/overwrite the specific one for that level. This is what it should have been doing.

In the meantime, I finally have an objective system. That’s coming in a second post.

Pathfinding update

So, an update on my pathfinding system. I recently imported it into Spamocalypse, and there were a few things I had to change.

For starters, I can’t store the neighbours of a Node inside that particular Node, or else Unity tells me I’ve reached the serialisation limit. In hindsight, this isn’t a surprise: if each node stored references to their neighbours, then referring to another neighbour would have in turn referred to the neighbours of that neighbour…and so on. Recursion is not a good idea, so I’ve had to make finding the neighbours of a particular Node a run-time method, i.e. it’s calculated on the fly while the game runs. So far, I haven’t run into any problems when testing that, but I’ve only used a single agent to test it.

I’ve also added a few useful things to the editor script that builds the navigation meshes. For starters, I’ve added in options to specify the walkable layers in the project. These work fine, although in Spamocalypse it seems to reset the walkable layers to include the bots and walls when starting up again, but in spite of that the code works. I’ve also added sections to change the square footprint of a cell, along with it’s max and min height, and finally managed to clamp light intensity. Light intensity will be clamped to a range between 0 and a value set by the user, if they wish to enable this.

So, I have the core changes uploaded into the repository. For those who don’t have or don’t know how to use Git, I’ll probably make a Unity package that you can import directly into the editor at a later stage.

Path-finding for stealth

One of the problems I have with Spamocalypse is how to build light intensity into the player’s ability to hide. Most of the stealth-based games I’ve played include a mechanism like this, and I think it’s worth adding. My problem is how to do that, especially given that I’m using Unity Free and don’t have access to things like Light Probes.

The solution I’ve come up with is to add illumination to the pathfinding system. However, for that I’ll need to build one. This is something I did for my M.Sc. (see the Completed Projects page), but it had a few problems. Firstly, the mesh for each level was generated from scratch upon runtime, and the nodes were essentially limited to a two-dimensional array. Due to the level design, I also had a lot of unreachable nodes that were simply wasted space. While premature optimisation is a bad idea, I still hate the idea of useless data.

So far, I’ve managed to create a mechanism for serialising and deserialising the navigation meshes to the disc, allowing me to save and load navmeshes at runtime. I also have a template for adding nodes underneath obstacles, although it doesn’t quite work yet. Finally, I have managed to add an illumination variable to nodes, so that part of the system is done. At the moment, what I have left to do is to finish an A* implementation, and I should be fine.

I’ve decided to put this up as a GitHub repository, partly for storage, partly to track my changes, and partly so other people can use it, and it can be found here. When it’s finished, I will also add it to Dropbox as a Unity Package.