int BasicMonster::navigateTo(Point target) { // TODO: One centralized dijkstra should improve performance significantly TCODPath path = TCODPath(level->getWidth(), level->getHeight(), new PathFindingCallback(), level); path.compute(position.x, position.y, target.x, target.y); Point step; if (!path.isEmpty() && path.walk(&step.x, &step.y, true)) { Creature* c = level->creatureAt(step); if (c != NULL) { // TODO: more elegant solution? return 10; } else if (getStatusStrength(STATUS_BEARTRAP) > 0) { // TODO: resolve bear trap and other statuses somewhere else if (rng->getInt(0,9) == 0) endStatus(STATUS_BEARTRAP); return 10; } else { float diagonal = (step.x - position.x != 0 && step.y - position.y != 0) ? std::sqrt(2.f) : 1.f; moveTo(step); return static_cast<int>(getWalkingSpeed() * diagonal); } } else { return 0; } }
void creature_t::step_toward(int px, int py, creature_t* exclude) { TCODMap* map = new TCODMap(current_dungeon->width, current_dungeon->height); for (int y = 0; y < current_dungeon->height; y++) { for (int x = 0; x < current_dungeon->width; x++) { tile_t* tile = get_tile_at(current_dungeon, x, y); bool walkable = tile_is_walkable_by(*tile, this); if (flag & CF_INTELLIGENT) { // Intelligent monsters know how to open doors. if (tile->id == TILE_DOOR_CLOSED) { walkable = true; } } map->setProperties(x, y, tile->properties & TILE_PROP_TRANSPARENT, walkable); } } for (size_t i = 0; i < current_dungeon->creatures.size(); i++) { creature_t* c = current_dungeon->creatures.at(i); if (c == this || c == exclude) continue; if (sees(c)) { // If this creature can see another creature, then consider that // square as unwalkable. map->setProperties(c->pos.x, c->pos.y, 1, 0); } } TCODPath* path = new TCODPath(map, 1.0f); if (path->compute(pos.x, pos.y, px, py)) { if (path->size() > 0) { int move_x, move_y; path->get(0, &move_x, &move_y); try_move(move_x, move_y); } else { // No path found, walk around confused. random_walk(); } } else { // No path found, walk around confused. random_walk(); } delete map; delete path; }
static bool _dig_corridor(dungeon_t* dungeon, int sx, int sy, int dx, int dy, bool ignore_walkable_rules) { bool result = true; tile_t* start_tile = get_tile_at(dungeon, sx, sy); tile_t* end_tile = get_tile_at(dungeon, dx, dy); set_tile_at(dungeon, sx, sy, TILES[TILE_STONE_FLOOR]); set_tile_at(dungeon, dx, dy, TILES[TILE_STONE_FLOOR]); // Initialize pathfinding using A*. TCODMap* map = new TCODMap(dungeon->width, dungeon->height); _setup_pathfinding_map(dungeon, map, ignore_walkable_rules); TCODPath* path = new TCODPath(map, 0); if (path->compute(sx, sy, dx, dy)) { int i; for (i = 0; i < path->size(); i++) { int px, py; path->get(i, &px, &py); set_tile_at(dungeon, px, py, TILES[TILE_STONE_FLOOR]); } TRACE("Found a path between: [%d, %d] and [%d, %d]", sx, dy, dx, dy); } else { // No path found, handle. TRACE("No path found between: [%d, %d] and [%d, %d]", sx, sy, dx, dy); set_tile_at(dungeon, sx, sy, *start_tile); set_tile_at(dungeon, dx, dy, *end_tile); result = false; } if (result) { _maybe_place_door(dungeon, sx, sy); _maybe_place_door(dungeon, dx, dy); } delete map; delete path; return result; }
void Dungeon::drawPath(int y0, int y1, int x0, int x1, int width, char *tiles, void (*tileCallback)(char *)) { TCODMap *map = new TCODMap(m_width, m_height); for(int y=0;y<m_height;y++){ for(int x=0;x<m_width;x++){ map->setProperties(x,y,true,tiles[x+y*width] != 127); } } TCODPath *path = new TCODPath(map,0.0f); bool success = path->compute(x0,y0,x1,y1); if(success){ int x; int y; while(!path->isEmpty()){ path->walk(&x,&y,false); tileCallback(&(tiles[x+y*width])); } } else{ drawLine(y0,y1,x0,x1,width,tiles,tileCallback); } }