• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Enemy AI for a Tactics game

Genetix

Member
I am trying to work on a grid based Tactics RPG - This is something like Final Fantasy Tactics, Fire Emblem, Disgaea, etc. If you have never played a game like that - essentially it an RPG where the battle takes place on a grid.

The player takes a turn - each of their units can move so many tiles on the grid (based on how much energy they have) and can also attack if they are in range of an enemy and have enough energy left. I have this system working great.

I am struggling to get my enemy AI working correctly though, and need some feedback on the logic behind how it should work.

I am using an mp_grid to break the battle field up into tiles - but have a number of questions if anyone can help:

- My enemies move to the nearest player unit, but they all end up on the same tile, how can I prevent this from happening?

- If the tile by the nearest player unit is not open (another enemy standing there) how can I have my enemies move to the next closest tile?

- How can I make my enemies move to a random tile that is open if they aren't within attacking range of a player unit?

- How would you make an enemy move 1 tile at a time towards the nearest player unit object, rather then setting a path directly to the nearest open tile by that player unit?

I hope this makes sense, i'll be glad to go into great details and share some code if that helps out. I have really been struggling with this recently, but need to grasp basic AI using grids and paths better.
 
Z

zircher

Guest
It sounds like you need to use mp_grid_add_cell to temporarily mark those occupied or claimed cells so that other enemies can choose a different path.
 

Genetix

Member
I have tried that out - but with no luck. I have an enemy choose the cell it wants to go to, and start a path towards that cell - If I mark the destination cell (Through mp_grid_add_cell()) the enemy moving towards it will stop in their tracks rather then completing the path. I didn't think that would be the outcome as their path is determined before the destination cell is marked, but that seems to be what happens.
 
Z

zircher

Guest
I was thinking something like an enemy control loop. A sets path and marks end, B sets path and marks end, C sets path and as the last unit clears the cells (so they can be entered.) That give A, B, and C three different end points. Would that work?
 
Z

zombieattack

Guest
- My enemies move to the nearest player unit, but they all end up on the same tile, how can I prevent this from happening?
You'll have to make your enemies move one at a time. Let the first enemy move to whatever closest tile he wants, then mark it as unavailable. The next enemy will have to choose a different tile and mark it as unavailable, and so on.

If these enemies must be adjacent to the player to attack him, you may want to create a priority list for tiles near the player. Have enemies fill in these high priority tiles first (so they can attack) and the other enemies will just have to stand behind them and wait.

- If the tile by the nearest player unit is not open (another enemy standing there) how can I have my enemies move to the next closest tile?
Mark the tile as unavailable in your mp_grid. You could also make a list of all tiles adjacent to the player, and the enemies will have to choose tiles from this list one at a time on a first-come first-served basis. Then you could have lower priority tiles around these where enemies will just have to stand behind their attacking friends and wait. You may want to allow the strongest enemies to move to the attacking positions first, or decide on some priority for enemies choosing their tiles (or just make it random, up to you).

- How can I make my enemies move to a random tile that is open if they aren't within attacking range of a player unit?
Find the shortest path to the player using mp_grid functions. Check the length of this path. If it's greater than the attacking range of the enemy, then choose some area in which the enemy will move to a random location. Since your game is tile based, you could use irandom() to select a random number of tiles in the x and y direction, starting from some reference tile at the top-left of the area you want your enemy to pick a tile in. Say the top-left tile was (60,50) and you want the furthest tile of the random area to be (80,70). You would use x = irandom(20) + 60 and y = irandom(20) + 50. Then say you get a random tile of (72, 64). Check if this tile is free, and if the enemy can find a path to this tile using mp_grid functions. If the tile is free and a path exists, move the enemy there. Otherwise keep looking for random tiles that are free and that the enemy can reach using a path. You could have the enemy give up after a certain number of tries to prevent an endless search (which could freeze your game and cause it to crash!).

- How would you make an enemy move 1 tile at a time towards the nearest player unit object, rather then setting a path directly to the nearest open tile by that player unit?
Easy, find a full path to the nearest player unit object, but then only move the enemy forward one point in the path. If you're trying to animate movement, then just have the enemy move through all the points in the path, but make him wait for a little bit of time every time he reaches a new point.

Good luck with your game!
 

Yal

šŸ§ *penguin noises*
GMC Elder
I am using an mp_grid to break the battle field up into tiles - but have a number of questions if anyone can help:
- My enemies move to the nearest player unit, but they all end up on the same tile, how can I prevent this from happening?
- If the tile by the nearest player unit is not open (another enemy standing there) how can I have my enemies move to the next closest tile?
- How can I make my enemies move to a random tile that is open if they aren't within attacking range of a player unit?
- How would you make an enemy move 1 tile at a time towards the nearest player unit object, rather then setting a path directly to the nearest open tile by that player unit?
I hope this makes sense, i'll be glad to go into great details and share some code if that helps out. I have really been struggling with this recently, but need to grasp basic AI using grids and paths better.
In order of appearance:
  • Assign target cells sequentially, and mark each found end cell as invalid.
  • Assign each cell a score based on distance to the nearest valid target, and have enemies move to the cell with the biggest score rather than a cell close to a player. (Giving a cell a zero score if it's invalid for one reason or another)
  • If every cell gets a score, STILL have them move to the cell with the highest score - they'll approach the players this way, even if they can't get close enough to attack.
  • Making my own movement system where units can move one tile at a time (using state machines or something), then giving each unit a list of 1-tile steps to move. Wait until all units have empty lists, and then proceed to the next phase.
 
Top