コード例 #1
0
ファイル: creature.cpp プロジェクト: smarmy/HellRogue
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;
}
コード例 #2
0
ファイル: generate.cpp プロジェクト: smarmy/HellRogue
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;
}