# text-game AI grid pathing

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

## Recommended Posts

Ive been working on a text-game with a 2d scrolling map and im almost finished. But i need to apply some AI stuff. I spent about an hour and a half trying to figure out an equation and checked a few threads about it. What im trying to do is make an AI mob chase my character. Basicly if i cast a spell at it, or get in a cirtain radius of it, a boolean variable 'isHostile' is set to 'true', making him come after me. But im stuck figuring out an equation for pathing on a grid map. Here an example: -----------***x10**** -01234567890123456789 0-------------------- 1-------------------- 2--@x---------------- 3----xxx------------- 4-------xxx---------- 5----------xxx------- 6-------------xxx---- 7----------------xM-- 8-------------------- 9-------------------- @ = my character (point: 2,2) M = the mob (point: 7,17) x = a square the mob should land on, starting from right to left PS: im pretty sure the x's are right, i mapped it out on grid paper Does anyone know an equation to solve this? Also if you could supply an example using the points about, that would be real helpful too. Thanks for reading, and i hope someone else will find this useful. EDIT: it took all the spaces in the graphs out, so '-' represents a space

##### Share on other sites
Hm, maybe you're looking for Bresenham's line algorithm? That is, assuming all movement is grid-based and you're only interested in straight-line paths.

If you're planning on adding obstacles at some point, you might also look into the A* algorithm.

##### Share on other sites
Thank you so much! Its a lot more work than i thaught lol. A lot of reading and note-taking, but will be worth it.

##### Share on other sites
Quote:
 Original post by javasircThank you so much! Its a lot more work than i thaught lol. A lot of reading and note-taking, but will be worth it.

You should be able to reuse a fast line rendering algorithm for anything that moves in a straight path, like projectiles, and possibly to compute visibility and illumination between map cells.
For the original application, however, computing a whole line is probably wasteful because while the mob is chasing the target moves and the straight shortest path is invalidated; you actually need to compute individual steps of the mob, choosing one of its 8 adjacent locations.
Without obstacles, you can avoid A* or even more complex approximate pathfinding and simply compare the distance between the target and the two adjacent cells in the appropriate octant (only one choice when the target lies on the same row, column or 45° diagonal as the mob).

##### Share on other sites
Google for roguelike, and line of sight algorithm. If someone would know this it would be them. IIRC they compute multiple lines of sight per one step. And yes there is a little catch called a creature shouldn't interrupt its pursuit just because it lost a line of sight of a character.

BTW term mob is from MUD development. If you'd like to write a something less primitive, you might like a idea of a self sufficient player independent creatures.

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 101
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009662
• ### Who's Online (See full list)

There are no registered users currently online

×