astar_2d_example.c
lpc-astar-1.5.3/astar_2d_example.c · 3.7 KB
From the archive lpc-astar-1.5.3.tar.gz
Download the archive « Back to the archive contents
// Sample implementation of A* search on a simple 2D Cartesian space
//
// v1.0 2007-10-06, Chaos of Lost Souls MUD, http://lostsouls.org/
#include <astar.h>
inherit "/mod/algorithm/astar";
#define Map_Min_X 1
#define Map_Max_X 20
#define Map_Min_Y 1
#define Map_Max_Y 10
#define X 0
#define Y 1
// Neighbors rule produces loci and deltas for the points adjacent to us.
mixed * astar_neighbors_rule(mixed * pathfind) {
int * locus = pathfind[Astar_Pathfind_Active_Locus];
mixed * out = ({});
if(locus[X] > Map_Min_X)
out += ({({
({ locus[X] - 1, locus[Y] }), // Adjacent locus
({ -1, 0 }), // Delta to adjacent locus
1, // Cost of delta
})});
if(locus[X] < Map_Max_X)
out += ({({
({ locus[X] + 1, locus[Y] }), // Adjacent locus
({ 1, 0 }), // Delta to adjacent locus
1, // Cost of delta
})});
if(locus[Y] > Map_Min_Y)
out += ({({
({ locus[X], locus[Y] - 1 }), // Adjacent locus
({ 0, -1 }), // Delta to adjacent locus
1, // Cost of delta
})});
if(locus[Y] < Map_Max_Y)
out += ({({
({ locus[X], locus[Y] + 1 }), // Adjacent locus
({ 0, 1 }), // Delta to adjacent locus
1, // Cost of delta
})});
return out;
}
// Distance rule calculates the distance from a point to the target
// locus. Using the Pythagorean Theorem, yo.
float astar_distance_rule(mixed * pathfind) {
int * a = pathfind[Astar_Pathfind_Active_Locus];
int * b = pathfind[Astar_Pathfind_To];
int dx = a[X] - b[X];
int dy = a[Y] - b[Y];
return sqrt(dx * dx + dy * dy);
}
// Locus key rule gives us a unique value corresponding to a locus that
// can be reliably used as a mapping key. Since we're using arbitrary
// two-element arrays as loci, and arrays are pointers that will give
// unpredictable behavior when used as mapping keys, we need to use this
// so the algorithm can tell where it's been.
int astar_locus_key_rule(int * locus) {
return (locus[X] << 16) | locus[Y];
}
// Run limit rule tells the algorithm when to stop running and continue
// on a call_out.
int astar_run_limit_rule(mixed * pathfind) {
return get_eval_cost() < __MAX_EVAL_COST__ / 2;
}
// Set up the A* module.
void create() {
set_astar_neighbors_rule(#'astar_neighbors_rule);
set_astar_distance_rule(#'astar_distance_rule);
set_astar_locus_key_rule(#'astar_locus_key_rule);
set_astar_run_limit_rule(#'astar_run_limit_rule);
set_astar_caching(1);
}
// This is our callback when pathfinding completes (for better or worse);
// it displays the path to this_player().
void end_random_pathfind(mixed * path) {
if(path) {
mixed * loci = path[Astar_Path_Loci];
write("Path from " + loci[0][X] + "," + loci[0][Y] + " to " +
loci[<1][X] + "," + loci[<1][Y] + ":\n");
foreach(mixed * locus : loci)
write(" " + locus[X] + "," + locus[Y] + "\n");
} else {
write("Cannot find path.\n");
}
}
// Picks two random points in our coordinate space and pathfinds between
// them.
void start_random_pathfind() {
int * start = ({ Map_Min_X + random(Map_Max_X - Map_Min_X + 1),
Map_Min_Y + random(Map_Max_Y - Map_Min_Y + 1) });
int * target = ({ Map_Min_X + random(Map_Max_X - Map_Min_X + 1),
Map_Min_Y + random(Map_Max_Y - Map_Min_Y + 1) });
astar_find_path(start, target, 0, #'end_random_pathfind);
}