Advertisement

XNA Enemy AI Help

Started by May 27, 2012 07:40 PM
2 comments, last by IADaveMark 12 years, 5 months ago
Hi. I am working on an enemy class in my tile-based 2d rpg game in xna, and I am having difficulty with the AI. I attempted to make my own custom method; It works sometimes but not always. Right now, I am testing it on a simple map with a river running through it and a bridge. When my player's X position is the same as the bridge's, the enemy is able to reach me. When it is not, the enemy simply walks towards the river until it can't move anymore (enemy's can't enter water). By the way, the enemy and the player are on opposite sides of the river. Before trying this method, I looked into the A* method. Unfortunately, I am only in 8th grade, so it is a bit too confusing for me. Could you please try to modify or explain to me why my code is not working. I do understand why it is not working, but do not understand how to fix the problem.

Here is the AI Method.:


public void Move()
{
if (movetimer > 0)
{
movetimer--;
}

if (movetimer == 0)
{
int TargetX;
int TargetY;

List<Vector2> possibleTiles = new List<Vector2>();
List<Vector2> movableTiles = new List<Vector2>();
List<Vector2> movableDesiredTiles = new List<Vector2>();
if (X < Main.map.grid.GetLength(1))
{
possibleTiles.Add(new Vector2(X + 1, Y));
}
possibleTiles.Add(new Vector2(X, Y + 1));
if (X > 0)
{
possibleTiles.Add(new Vector2(X - 1, Y));
}
possibleTiles.Add(new Vector2(X, Y - 1));

foreach (Vector2 tile in possibleTiles)
{
if (tile.X < Main.map.grid.GetLength(1) && tile.Y < Main.map.grid.GetLength(0))
{
if (Main.map.grid[(int)tile.Y, (int)tile.X] != 2)
{
movableTiles.Add(new Vector2(tile.X, tile.Y));
}
}
}

foreach (Vector2 tile in movableTiles)
{
if (Vector2.Distance(new Vector2(tile.X, tile.Y), new Vector2(Main.player.X, Main.player.Y)) < Vector2.Distance(new Vector2(X, Y), new Vector2(Main.player.X, Main.player.Y)))
{
movableDesiredTiles.Add(new Vector2(tile.X, tile.Y));
}
}
if (movableDesiredTiles.Count > 0)
{
Vector2 mostDesiredTile = new Vector2(X, Y);
int distanceX;
int distanceY;
int totalDistance = 100;
foreach (Vector2 tile in movableDesiredTiles)
{
int specificDistance;
distanceX = Main.player.X - X;
if (distanceX < 0)
{
distanceX *= -1;
}
distanceY = Main.player.Y - Y;
if (distanceY < 0)
{
distanceY *= -1;
}
specificDistance = distanceX + distanceY;
if (specificDistance < totalDistance)
{
mostDesiredTile = new Vector2(tile.X, tile.Y);
totalDistance = specificDistance;
}
}
TargetX = (int)mostDesiredTile.X;
TargetY = (int)mostDesiredTile.Y;
X = TargetX;
Y = TargetY;


}

movetimer = 60;
}
Hello,

From the way i look at your code, all this code does is check ahead only 1 square and then move in the most desired direction. The problem with this is that your code doesnt look ahead farther than one square and doesnt know whether the second square and all the other squares are movable. In my opinion (but i am not very experienced) the only way to solve this would be with A*, since that method looks ahead for all squares between the current and the target position.

If you dont want to learn A*, you could try with a depth-first search, although this is definitely not recommended, since it might take a lot of time to calculate for your AI and will not always find the optimal path, but at least it will find a path to the target (in most cases).

I hope this helps!

My personal blog on game development!

Black Wolf Game Development

Advertisement
Didn't look through all your code but if you need a simple A* tutorial then i reccomend this one.
http://www.policyalmanac.org/games/aStarTutorial.htm
Being in the 8th grade shouldn't stop you from learning it. Its fairly simple if you can visualize it. All you need to do is reread the parts that give you trouble and you should have no problem.
While I recommend learning A* since it is a staple of game AI, another cheap solution is to give options for what happens when your move is blocked. In your case, if you can't move the way you want because of the river (or whatever) why not move in a secondary direction? For example, towards the bridge?

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

This topic is closed to new replies.

Advertisement