minihack.level_generator module

class minihack.level_generator.LevelGenerator(map=None, w=8, h=8, fill='.', lit=True, flags=('hardfloor'), solidfill=' ')[source]

Bases: object

LevelGenerator provides a convenient Python interface for quickly writing description files for MiniHack. The LevelGenerator class can be used to create MAZE-type levels with specified heights and widths, and can then fill those levels with objects, monsters and terrain, and specify the start point of the level.

Parameters
  • map (str or None) – The description of the map block of the environment. If None, the map will have a rectangle layout with the given height and width. Defaults to None.

  • w (int) – The width of map. Only used when map=None. Defaults to 8.

  • h (int) – The height of map. Only used when map=None. Defaults to 8.

  • fill (str) – A character describing the environment feature that fills the map. Only used when map=None. Defaults to “.”, which corresponds to floor.

  • lit (bool) – Whether the layout is lit or not. This affects the observations the agent will receive. If an area is not lit, the agent can only see directly adjacent grids. Defaults to True.

  • flags (tuple) – Flags of the environment. For the full list, see https://nethackwiki.com/wiki/Des-file_format#FLAGS. Defaults to (“hardfloor”,).

  • solidfill (str) – A character describing the environment feature used for filling solid / unspecified parts of the map. Defaults to ” “, which corresponds to solid wall.

__init__(map=None, w=8, h=8, fill='.', lit=True, flags=('hardfloor'), solidfill=' ')[source]

Initialize self. See help(type(self)) for accurate signature.

add_altar(place=None, align='random', type='random')[source]

Add an altar.

Parameters
  • place (None, tuple or str) – The place of the added object. If None, the location is selected randomly. Tuple values are used for providing exact (x, y) coordinates. String values are copied to des-file as is. Defaults to None.

  • align (str) – The alignment. Possible values are “noalign”, “law”, “neutral”, “chaos”, “coaligned”, “noncoaligned”, and “random”. Defaults to “random”.

  • type (str) – The type of the altar. Possible values are “sanctum”, “shrine”, “altar”, and “random”. Defaults to random.

add_boulder(place=None)[source]

Add a boulder to the floor.

Parameters
  • amount (int) – The amount of gold.

  • place (None, tuple or str) – The place of the added object. If None, the location is selected randomly. Tuple values are used for providing exact (x, y) coordinates. String values are copied to des-file as is. Defaults to None.

add_door(state, place=None)[source]

Add a door.

Parameters
  • state (str) – The state of the door. Possible values are “locked”, “closed”, “open”, “nodoor”, and “random”. Defaults to “random”.

  • place (None, tuple or str) – The place of the added object. If None, the location is selected randomly. Tuple values are used for providing exact (x, y) coordinates. String values are copied to des-file as is. Defaults to None.

add_fountain(place=None)[source]

Add a fountain.

Parameters

place (None, tuple or str) – The place of the added object. If None, the location is selected randomly. Tuple values are used for providing exact (x, y) coordinates. String values are copied to des-file as is. Defaults to None.

add_goal_pos(place=None)[source]

Add a goal at the given place. Same as add_stair_down.

add_gold(amount, place=None)[source]

Add gold on the floor.

Parameters
  • amount (int) – The amount of gold.

  • place (None, tuple or str) – The place of the added object. If None, the location is selected randomly. Tuple values are used for providing exact (x, y) coordinates. String values are copied to des-file as is. Defaults to None.

add_line(str)[source]

Add a custom string to the buttom of the description file.

Parameters

str (str) – The string to be concatenated to the des-file.

add_mazewalk(coord=None, dir='east')[source]

Creates a random maze, starting from the given coordinate.

Mazewalk turns map grids with solid stone into floor. From the starting position, it checks the mapgrid in the direction given, and if it’s solid stone, it will move there, and turn that place into floor. Then it will choose a random direction, jump over the nearest mapgrid in that direction, and check the next mapgrid for solid stone. If there is solid stone, mazewalk will move that direction, changing that place and the intervening mapgrid to floor. Normally the generated maze will not have any loops.

Pointing mazewalk at that will create a small maze of trees, but unless the map (at the place where it’s put into the level) is surrounded by something else than solid stone, mazewalk will get out of that MAP. Substituting floor characters for some of the trees “in the maze” will make loops in the maze, which are not otherwise possible. Substituting floor characters for some of the trees at the edges of the map will make maze entrances and exits at those places.

For more details see https://nethackwiki.com/wiki/Des-file_format#MAZEWALK.

Parameters

coord – A tuple with length two representing the (x, y) coordinates. If None is passed, the middle point of the map is selected. Defaults to None.

add_monster(name='random', symbol=None, place=None, args=())[source]

Add a monster to the map.

Parameters
  • name (str) – The name of the monster. Defaults to random.

  • symbol (str or None) – The symbol of the monster. The symbol should correspond to the family of the specified mosnter. For example, “d” symbol corresponds to canine monsters, so the name of the object should also correspond to canines (e.g. jackal). Not used when name is “random”. Defaults to None.

  • place (None, tuple or str) – The place of the added object. If None, the location is selected randomly. Tuple values are used for providing exact (x, y) coordinates. String values are copied to des-file as is. Defaults to None.

  • args (tuple) – Additional monster arguments, e.g. “hostile” or “peaceful”, “asleep” or “awake”, etc. For more details, see https://nethackwiki.com/wiki/Des-file_format#MONSTER.

add_object(name='random', symbol='%', place=None, cursestate=None)[source]

Add an object to the map.

Parameters
  • name (str) – The name of the object. Defaults to random.

  • symbol (str) – The symbol of the object. The symbol should correspond to the given object name. For example, “%” symbol corresponds to comestibles, so the name of the object should also correspond to commestibles (e.g. apple). Not used when name is “random”. Defaults to “%”.

  • place (None, tuple or str) – The place of the added object. If None, the location is selected randomly. Tuple values are used for providing exact (x, y) coordinates. String values are copied to des-file as is. Defaults to None.

  • cursetstate (str or None) – The cursed state of the object. Can be “blessed”, “uncursed”, “cursed” or “random”. Defaults to None (not used).

add_object_area(area_name, name='random', symbol='%', cursestate=None)[source]

Add an object in an area of the map defined by area_name variable. See add_object for more details.

add_sink(place=None)[source]

Add a sink.

Parameters

place (None, tuple or str) – The place of the added object. If None, the location is selected randomly. Tuple values are used for providing exact (x, y) coordinates. String values are copied to des-file as is. Defaults to None.

add_stair_down(place=None)[source]

Add a stair down at the given place.

Parameters

place (None, tuple or str) – The place of the added object. If None, the location is selected randomly. Tuple values are used for providing exact (x, y) coordinates. String values are copied to des-file as is. Defaults to None.

add_terrain(coord, flag, in_footer=False)[source]

Add terrain features to the map.

Parameters
  • coord (tuple) – A tuple with length two representing the (x, y) coordinates.

  • flag (str) – The flag corresponding to the desired terrain feature. Should belong to minihack.level_generator.MAP_CHARS. For more details, see https://nethackwiki.com/wiki/Des-file_format#Map_characters

  • in_footer (bool) – Whether to define the terrain feature as an additional line in the description file (True) or directly modify the map block with the given flag (False). Defaults to False.

add_trap(name='teleport', place=None)[source]

Add a trap.

Parameters
  • name (str) – The name of the trap. For possible values, see minihack.level_generator.TRAP_NAMES. Defaults to “teleport”.

  • place (None, tuple or str) – The place of the added object. If None, the location is selected randomly. Tuple values are used for providing exact (x, y) coordinates. String values are copied to des-file as is. Defaults to None.

fill_terrain(type, flag, x1, y1, x2, y2)[source]

Fill the areas between (x1, y1) and (x2, y2) with the given dungeon feature:

Parameters
  • type (str) – The type of filling. “rect” indicates an unfilled rectangle, containing just the edges and none of the interior points. “fillrect” denotes filled rectangle containing the edges and all of the interior points. “line” is used for a straight line drawn from one pair of coordinates to the other using Bresenham’s line algorithm.

  • flag (str) – The flag corresponding to the desired terrain feature. Should belong to minihack.level_generator.MAP_CHARS. For more details, see https://nethackwiki.com/wiki/Des-file_format#Map_characters

  • x1 (int) – x coordinate of point 1.

  • y1 (int) – y coordinate of point 1.

  • x2 (int) – x coordinate of point 2.

  • y2 (int) – y coordinate of point 2.

get_des()[source]

Returns the description file.

Returns

the description file as a string.

Return type

str

get_map_array()[source]

Returns the map as a two-dimensional numpy array.

get_map_str()[source]

Returns the map as a string.

init_map(map=None, x=8, y=8, fill='.')[source]

Initialise the map block of the des-file.

set_area_variable(var_name, type, x1, y1, x2, y2)[source]

Set a variable representing an area on the map.

Parameters
  • var_name (str) – The name of the variable.

  • type (str) – The type of filling. “rect” indicates an unfilled rectangle, containing just the edges and none of the interior points. “fillrect” denotes filled rectangle containing the edges and all of the interior points. “line” is used for a straight line drawn from one pair of coordinates to the other using Bresenham’s line algorithm.

  • x1 (int) – x coordinate of point 1.

  • y1 (int) – y coordinate of point 1.

  • x2 (int) – x coordinate of point 2.

  • y2 (int) – y coordinate of point 2.

set_start_pos(coord)[source]

Set the starting position of the agent.

Parameters

coord (tuple) – A tuple with length two representing the (x, y) coordinates.

set_start_rect(p1, p2)[source]

Set the starting position of the agent.

Parameters

coord (tuple) – A tuple with length two representing the (x, y) coordinates.

wallify()[source]

Wallify the map. Turns walls completely surrounded by other walls into solid stone ‘ ‘.