text-game AI grid pathing

Started by
3 comments, last by Raghar 17 years ago
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
Advertisement
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.
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.
Quote:Original post by javasirc
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.

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).

Omae Wa Mou Shindeiru

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.

This topic is closed to new replies.

Advertisement