RogueSharp Documentation

repository·main·Indexed 20 days ago

https://github.com/faronbracy/roguesharp

A C# library for roguelike developers providing utility functions for map generation, field-of-view (FOV), pathfinding, and cell manipulation. It includes features such as the PathFinder class, GoalMap for weighted desirability, IMapCreationStrategy for map generation, and tools for circular cell selection using Bresenham's midpoint circle algorithm.

Tokens
1.9K
Snippets
7
Records
9
Agent score
21%

What's inside RogueSharp

  1. How Field-of-View (FOV) works in RogueSharp

    main

    RogueSharp provides efficient field-of-view calculations for a specified distance on a map. You can choose whether or not to light walls during the calculation.

    • ComputeFov: Performs a new FOV calculation.
    • AppendFov: Appends a new FOV calculation to any existing ones.
    /// Constructs a new FieldOfView objec for the specified Map
    public FieldOfView( IMap map )
    
    /// Performs a field-of-view calculation with the specified parameters.
    public ReadOnlyCollection<ICell> ComputeFov( int xOrigin, int yOrigin, int radius, bool lightWalls )
    
    /// Performs a field-of-view calculation with the specified parameters
    /// and appends it any existing field-of-view calculations.
    public ReadOnlyCollection<ICell> AppendFov( int xOrigin, int yOrigin, int radius, bool lightWalls )
  2. Understand the Map and Cell concepts

    main

    Most RogueSharp interactions revolve around the Map (an IMap implementation), which is a rectangular grid of Cell objects.

    Each Cell contains state information relevant to roguelike mechanics:

    • IsTransparent: Whether visibility extends through the cell.
    • IsWalkable: Whether the player/entities can traverse the cell.
    • IsExplored: Whether the player has ever had line-of-sight to the cell.
    • IsInFov: Whether the cell is currently within the player's field-of-view.
  3. How Weighted Goal Maps work

    main

    A GoalMap allows you to define desirability across a map by setting weights for specific locations. This is useful for pathfinding towards objectives while avoiding obstacles.

    • AddGoal(x, y, weight): Sets a desirability weight at a specific coordinate.
    • AddObstacle(x, y): Marks a coordinate as an obstacle that any found path must avoid.
    • allowDiagonalMovement: A boolean flag used during construction to determine if diagonal movement is valid.
    /// Constructs a new instance of a GoalMap for the specified Map 
    /// that will consider diagonal movements to be valid if allowDiagonalMovement is set to true.
    public GoalMap( IMap map, bool allowDiagonalMovement )
    
    /// Add a Goal at the specified location with the specified weight
    public void AddGoal( int x, int y, int weight )
    
    /// Add an Obstacle at the specified location. Any paths found must not go through Obstacles
    public void AddObstacle( int x, int y )
  4. How Pathfinding works in RogueSharp

    main

    RogueSharp provides a PathFinder class to calculate the shortest path between two cells on a map. You can configure whether the pathfinder considers diagonal movement by providing a diagonalCost during instantiation.

    To find a path, use the ShortestPath method, which returns a Path object containing a list of Cell objects from the source to the destination.

    /// Constructs a new PathFinder instance for the specified Map 
    /// that will consider diagonal movement by using the specified diagonalCost
    public PathFinder( IMap map, double diagonalCost )
    
    /// Returns a shortest Path containing a list of Cells 
    /// from a specified source Cell to a destination Cell
    public Path ShortestPath( ICell source, ICell destination )
  5. How Cell Selection works in RogueSharp

    main

    You can select groups of cells (rows, columns, circles, squares, or diamonds) using specific methods. For circular selections, RogueSharp uses Bresenham's midpoint circle algorithm.

    • GetCellsInCircle: Returns all cells within a circle up to a specified radius.
    • GetBorderCellsInCircle: Returns only the outermost border cells of a circle up to a specified radius.
    /// Get an IEnumerable of Cells in a circle around the center Cell up 
    /// to the specified radius using Bresenham's midpoint circle algorithm
    public IEnumerable<ICell> GetCellsInCircle( int xCenter, int yCenter, int radius )
      
    /// Get an IEnumerable of outermost border Cells in a circle around the center 
    /// Cell up to the specified radius using Bresenham's midpoint circle algorithm
    public IEnumerable<ICell> GetBorderCellsInCircle( int xCenter, int yCenter, int radius )
  6. Create a Map using an IMapCreationStrategy

    main

    Instead of a blank map, you can use the static Map.Create method combined with an IMapCreationStrategy. RogueSharp provides built-in strategies like RandomRoomsMapCreationStrategy. You can also implement your own custom strategy to define how maps are generated.

    IMapCreationStrategy<Map> mapCreationStrategy = new RandomRoomsMapCreationStrategy<Map>( 17, 10, 30, 5, 3 )
    IMap somewhatInterestingMap = Map.Create( mapCreationStrategy );
    Console.WriteLine( somewhatInterestingMap.ToString() );
  7. Create a basic Map

    main

    You can instantiate a new Map by providing a width and height. By default, all cell properties (transparency, walkability, etc.) are set to false.

    IMap boringMapOfSolidStone = new Map( 5, 3 );
    Console.WriteLine( boringMapOfSolidStone.ToString() );
  8. Reference: Map.ToString() visual symbols

    main

    The Map.ToString() method provides a text-based visual representation of the map. You can optionally pass a bool to ToString() to include field-of-view information (defaults to false).

    Symbols used:

    • %: Cell is not in field-of-view
    • .: Cell is transparent, walkable, and in field-of-view
    • s: Cell is walkable and in field-of-view (but not transparent)
    • o: Cell is transparent and in field-of-view (but not walkable)
    • #: Cell is in field-of-view (but not transparent or walkable)