void buildNeighbors(ASNeighborList neighbors, void *node, void *context) { int i, j; Node *p = (Node*)node; Node *next; int legal; NodeSet *nodes = (NodeSet*)context; if(p->id == nodes->refs[0]->id) { ASNeighborListAdd(neighbors, nodes->headnext, nodes->firstlen); } for (i = 0; i < p->outdegree; i++) { next = p->outedges[i]->tail; legal = 0; for (j = 0; j < nodes->count; j++) { if (next->id == nodes->refs[j]->id) { legal = 1; } } if(legal) { if( next->id == nodes->refs[1]->id && p->id != nodes->tailprev->id ) { continue; } ASNeighborListAdd(neighbors, next, p->outedges[i]->length); } } }
static void AddTileNeighbors( ASNeighborList neighbors, void *node, void *context) { Vec2i *v = node; int y; AStarContext *c = context; for (y = v->y - 1; y <= v->y + 1; y++) { int x; if (y < 0 || y >= c->Map->Size.y) { continue; } for (x = v->x - 1; x <= v->x + 1; x++) { float cost; Vec2i neighbor; neighbor.x = x; neighbor.y = y; if (x < 0 || x >= c->Map->Size.x) { continue; } if (x == v->x && y == v->y) { continue; } // if we're moving diagonally, // need to check the axis-aligned neighbours are also clear if (!c->IsTileOk(c->Map, Vec2iNew(x, y)) || !c->IsTileOk(c->Map, Vec2iNew(v->x, y)) || !c->IsTileOk(c->Map, Vec2iNew(x, v->y))) { continue; } // Calculate cost of direction // Note that there are different horizontal and vertical costs, // due to the tiles being non-square // Slightly prefer axes instead of diagonals if (x != v->x && y != v->y) { cost = TILE_WIDTH * 1.1f; } else if (x != v->x) { cost = TILE_WIDTH; } else { cost = TILE_HEIGHT; } ASNeighborListAdd(neighbors, &neighbor, cost); } } }
/* following three aims to test the third-party a* algorithm */ void t_buildNeighbors(ASNeighborList neighbors, void *node, void *context) { int p = *(int*)node; int nodes[9] = {1, 2, 3, 2, 5, 3, 5, 4, 5}; if (p == 0) { ASNeighborListAdd(neighbors, nodes + 0, 1); ASNeighborListAdd(neighbors, nodes + 1, 2); ASNeighborListAdd(neighbors, nodes + 2, 4); } else if (p == 1) { ASNeighborListAdd(neighbors, nodes + 3, 3); ASNeighborListAdd(neighbors, nodes + 4, 6); } else if (p == 2) { ASNeighborListAdd(neighbors, nodes + 5, 1); ASNeighborListAdd(neighbors, nodes + 6, 5); } else if (p == 3) { ASNeighborListAdd(neighbors, nodes + 7, 1); } else if (p == 4) { ASNeighborListAdd(neighbors, nodes + 8, 1); } }