View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

# XNA Enemy AI Help

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Kaandimandimanbopbobop  Members

Posted 27 May 2012 - 01:40 PM

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


### #2Jaap85  Members

Posted 28 May 2012 - 02:17 AM

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 first game: Tycoon!

Please let me know what you think about it!

### #3LittleBigPhil  Members

Posted 28 May 2012 - 07:09 AM

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.

### #4IADaveMark  Moderators

Posted 28 May 2012 - 07:57 AM

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-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
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!"

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.