Project Intro
This project is about influence maps and how they can be used in game artificial intelligence. My project shows influence maps in the Unity Engine. Using influence maps requires a good handle on memory usage and speed. There are many moving parts that need to be filled, refilled, and calculated resources on your computer can be easily used up.
Influence maps can be used for targeting systems, obstacle avoidance or even determining notable locations on the map.
Influence Maps
The start of Influence maps start with, well, Influence maps! An influence map requires a grid of some sort. The grid can either be 2D (x, y) or 3D (x, y, z). Once a grid is established you can begin creating influence maps. Each influence map you calculate will have the grid’s dimensions as its own. For example if you have a grid is that is five by five then you influence map will also be five by five.
Its also important that your maps have matching granularity. Granularity is the size of the tiles you are using, basically your metrics. Influence maps and the base grid should be of the same granularity. Have the influence map be more dense or less dense than the grid that exists will throw off the calculations and alter how values are calculated.
Once the map dimensions and granularity are uniform, you have to decide on the data structure you will be using to access the information. In my project I used a 2D array since it works well with a 2D grid, however you could also use a Dictionary have the Key type set to hold the grid position and have the Value hold the influence value.
An important thing to keep in note is that you influence map will need to be updated constantly so its best to create functionality for editing the values within your data structure.
Filling Influence Maps
After your influence map is set up correctly its time to populate the map with information. There are various ways to do this, but most methods will need you to keep track of units and where they are on map grid. Once you know where each unit is on the map you can then start filling it out. The formula used to calculate influence varies based on the project/game needs, but the core principle is that influence fades over distance. An example of this is that a unit would exert more influence on a tile they are next rather than a tile that is eight tiles away.
The formula I used is below:
influence = value - Mathf.Pow(value * (distance/(maxRange + 1)), 2.0f);
The formula takes three components into the account when calculating the influence. The first component is the unit’s max influence value. This value is the influence value that is exerted if were were to be standing on the same tile as the unit.
The second component is the max range that the unit can exert influence. once we’ve reached tiles outside the range no more calculations are made.
The final component is the actual distance a tile is from the unit’s current location. By using this distance we have a varied amount of influence that decreases as we get further away form the unit.
Map Stamps
Now that we have the information in the influence maps we should talk about how to get the information. You could go and grab the Influence map every time or you could use a map stamp. With a map stamp all you need to do is define a distance that the stamp will go out from.. Beyond that one parameter upon creation your map stamp should contain all the functions it needs to grab the information from the influence map.
Using a map stamp eliminates and excess information that would limit the efficiency of code. Additionally you can reuse the map stamp by simply having a reference to the different sizes of stamps and calling that reference when you need to get new information. Stamps should be able to grab information form any map that you have made so that units can access.
Using Map Stamps
When a unit uses a Map Stamp you want to grab a map stamp with a size that relates to the function you are grabbing it for. A simple example of this would be if you wanted to create an artillery unit which will fire at the highest concentration of enemy units. If the range of the unit is ten then you would want a map stamp with a size of 10 from the enemy unit influence map. With this stamp you could see the highest concentration of units by looking a the tile with the highest value on that stamp.
You can also do this for movement too. In this case you’d want to grab a map stamp that is equal in size to the unit’s movement speed. where you grab the from depends on the kind of movement you want. If you want the unit to avoid enemies then you would stamp the enemy influence map and pick the tile with the lowest value. If you want to create a separation like behavior you stamp an ally influence map and steer towards the lowest value tile (since it has the least influence from allies in range).