Jump to content
  • Advertisement
Sign in to follow this  
Kaan Aksoy

XNA Enemy AI Help

This topic is 2273 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}

Share this post


Link to post
Share on other sites
Advertisement
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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!