engine/maps/source/MapUtils.cpp
// Add a creature randomly to the map
Add a creature randomly to the map
Add a creature randomly to the map
bool MapUtils::place_creature_randomly(MapPtr map, const string& creature_id)
{
  bool creatures_generated = false;
  Dimensions dim = map->size();
  int rows = dim.get_y();
  int cols = dim.get_x();
  Game& game = Game::instance();
  ActionManager& am = game.get_action_manager_ref();
  CreatureFactory cf;
  CreaturePtr creature = cf.create_by_creature_id(am, creature_id, map);
  if (creature != nullptr)
  {
    for (int attempts = 0; attempts < 200; attempts++)
    {
      int creature_row = RNG::range(0, rows - 1);
      int creature_col = RNG::range(0, cols - 1);
      TilePtr tile = map->at(creature_row, creature_col);
      if (MapUtils::is_tile_available_for_creature(creature, tile))
      {
        ostringstream ss;
        ss << "Placing creature " << creature->get_id() << " randomly at " << creature_row << "," << creature_col;
        Log::instance().debug(ss.str());
        Coordinate coords(creature_row, creature_col);
        MapUtils::add_or_update_location(map, creature, coords);
        creatures_generated = true;
        break;
      }
    }
  }
  return creatures_generated;
}
bool MapUtils :: place_creature_randomly ( MapPtr map , const string & creature_id ) { bool creatures_generated = false ; Dimensions dim = map -> size ( ) ; int rows = dim . get_y ( ) ; int cols = dim . get_x ( ) ; Game & game = Game :: instance ( ) ; ActionManager & am = game . get_action_manager_ref ( ) ; CreatureFactory cf ; CreaturePtr creature = cf . create_by_creature_id ( am , creature_id , map ) ; if ( creature != nullptr ) { for ( int attempts = 0 ; attempts < 200 ; attempts ++ ) { int creature_row = RNG :: range ( 0 , rows - 1 ) ; int creature_col = RNG :: range ( 0 , cols - 1 ) ; TilePtr tile = map -> at ( creature_row , creature_col ) ; if ( MapUtils :: is_tile_available_for_creature ( creature , tile ) ) { ostringstream ss ; ss << " " << creature -> get_id ( ) << " " << creature_row << " " << creature_col ; Log :: instance ( ) . debug ( ss . str ( ) ) ; Coordinate coords ( creature_row , creature_col ) ; MapUtils :: add_or_update_location ( map , creature , coords ) ; creatures_generated = true ; break ; } } } return creatures_generated ; }
Add a creature randomly to the map
Add a creature randomly to the map
// Generate the creature // Place the creature // Check to see if the spot is empty, and if a creature can be added there.