Class SoundMap

java.lang.Object
squidpony.squidgrid.SoundMap

public class SoundMap
extends Object
This class is used to determine when a sound is audible on a map and at what positions. Created by Tommy Ettinger on 4/4/2015.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    OrderedMap<Coord,​Double> alerted
    The latest results of findAlerted(), with Coord keys representing the positions of creatures that were alerted and Double values representing how loud the sound was when it reached them.
    double[][] gradientMap
    The frequently-changing values that are often the point of using this class; cells producing sound will have a value greater than 0, cells that cannot possibly be reached by a sound will have a value of exactly 0, and walls will have a value equal to the WALL constant (a negative number).
    int height
    Height of the map.
    Measurement measurement
    This affects how sound travels on diagonal directions vs.
    double[][] physicalMap
    Stores which parts of the map are accessible and which are not.
    IRNG rng
    The RNG used to decide which one of multiple equally-short paths to take.
    static double SILENT
    Cells with no sound are always marked with 0.
    OrderedMap<Coord,​Double> sounds
    Sources of sound on the map; keys are positions, values are how loud the noise is (10.0 should spread 10 cells away, with diminishing values assigned to further positions).
    static double WALL
    Walls, which are solid no-entry cells, are marked with a significant negative number equal to -999500.0 .
    int width
    Width of the map.
  • Constructor Summary

    Constructors 
    Constructor Description
    SoundMap()
    Construct a SoundMap without a level to actually scan.
    SoundMap​(char[][] level)
    Constructor meant to take a char[][] returned by DungeonBoneGen.generate(), or any other char[][] where '#' means a wall and anything else is a walkable tile.
    SoundMap​(char[][] level, char alternateWall)
    Constructor meant to take a char[][] returned by DungeonBoneGen.generate(), or any other char[][] where one char means a wall and anything else is a walkable tile.
    SoundMap​(char[][] level, Measurement measurement)
    Constructor meant to take a char[][] returned by DungeonBoneGen.generate(), or any other char[][] where '#' means a wall and anything else is a walkable tile.
    SoundMap​(double[][] level)
    Used to construct a SoundMap from the output of another.
    SoundMap​(double[][] level, Measurement measurement)
    Used to construct a DijkstraMap from the output of another, specifying a distance calculation.
    SoundMap​(IRNG random)
    Construct a SoundMap without a level to actually scan.
  • Method Summary

    Modifier and Type Method Description
    void clearSounds()
    Used to remove all sounds.
    OrderedMap<Coord,​Double> findAlerted​(Set<Coord> creatures, Map<Coord,​Double> extraSounds)
    Scans the dungeon using SoundMap.scan, adding any positions in extraSounds to the group of known sounds before scanning.
    SoundMap initialize​(char[][] level)
    Used to initialize or re-initialize a SoundMap that needs a new PhysicalMap because it either wasn't given one when it was constructed, or because the contents of the terrain have changed permanently.
    SoundMap initialize​(char[][] level, char alternateWall)
    Used to initialize or re-initialize a SoundMap that needs a new PhysicalMap because it either wasn't given one when it was constructed, or because the contents of the terrain have changed permanently.
    SoundMap initialize​(double[][] level)
    Used to initialize or re-initialize a SoundMap that needs a new PhysicalMap because it either wasn't given one when it was constructed, or because the contents of the terrain have changed permanently.
    void removeSound​(int x, int y)
    If a sound is being produced at a given (x, y) location, this removes it.
    void removeSound​(Coord pt)
    If a sound is being produced at a given location (a Coord), this removes it.
    void reset()
    Resets this SoundMap to a state with no sounds, no alerted creatures, and no changes made to gradientMap relative to physicalMap.
    void resetCell​(int x, int y)
    Reverts a cell to the value stored in the original state of the level as known by physicalMap.
    void resetCell​(Coord pt)
    Reverts a cell to the value stored in the original state of the level as known by physicalMap.
    void resetMap()
    Resets the gradientMap to its original value from physicalMap.
    double[][] scan()
    Recalculate the sound map and return it.
    protected void setFresh​(int x, int y, double counter)  
    protected void setFresh​(Coord pt, double counter)  
    void setOccupied​(int x, int y)
    Marks a specific cell in gradientMap as a wall, which makes sounds potentially unable to pass through it.
    void setSound​(int x, int y, double loudness)
    Marks a cell as producing a sound with the given loudness; this can be placed on a wall or unreachable area, but that may cause the sound to be un-hear-able.
    void setSound​(Coord pt, double loudness)
    Marks a cell as producing a sound with the given loudness; this can be placed on a wall or unreachable area, but that may cause the sound to be un-hear-able.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • measurement

      This affects how sound travels on diagonal directions vs. orthogonal directions. MANHATTAN should form a diamond shape on a featureless map, while CHEBYSHEV will form a square.
    • physicalMap

      public double[][] physicalMap
      Stores which parts of the map are accessible and which are not. Should not be changed unless the actual physical terrain has changed. You should call initialize() with a new map instead of changing this directly.
    • gradientMap

      public double[][] gradientMap
      The frequently-changing values that are often the point of using this class; cells producing sound will have a value greater than 0, cells that cannot possibly be reached by a sound will have a value of exactly 0, and walls will have a value equal to the WALL constant (a negative number).
    • height

      public int height
      Height of the map. Exciting stuff. Don't change this, instead call initialize().
    • width

      public int width
      Width of the map. Exciting stuff. Don't change this, instead call initialize().
    • alerted

      The latest results of findAlerted(), with Coord keys representing the positions of creatures that were alerted and Double values representing how loud the sound was when it reached them.
    • SILENT

      public static final double SILENT
      Cells with no sound are always marked with 0.
      See Also:
      Constant Field Values
    • WALL

      public static final double WALL
      Walls, which are solid no-entry cells, are marked with a significant negative number equal to -999500.0 .
      See Also:
      Constant Field Values
    • sounds

      Sources of sound on the map; keys are positions, values are how loud the noise is (10.0 should spread 10 cells away, with diminishing values assigned to further positions).
    • rng

      public IRNG rng
      The RNG used to decide which one of multiple equally-short paths to take.
  • Constructor Details

    • SoundMap

      public SoundMap()
      Construct a SoundMap without a level to actually scan. If you use this constructor, you must call an initialize() method before using this class.
    • SoundMap

      public SoundMap​(IRNG random)
      Construct a SoundMap without a level to actually scan. This constructor allows you to specify an RNG before it is used. If you use this constructor, you must call an initialize() method before using this class.
    • SoundMap

      public SoundMap​(double[][] level)
      Used to construct a SoundMap from the output of another. Any sounds will need to be assigned again.
      Parameters:
      level -
    • SoundMap

      public SoundMap​(double[][] level, Measurement measurement)
      Used to construct a DijkstraMap from the output of another, specifying a distance calculation.
      Parameters:
      level -
      measurement -
    • SoundMap

      public SoundMap​(char[][] level)
      Constructor meant to take a char[][] returned by DungeonBoneGen.generate(), or any other char[][] where '#' means a wall and anything else is a walkable tile. If you only have a map that uses box-drawing characters, use DungeonUtility.linesToHashes() to get a map that can be used here.
      Parameters:
      level -
    • SoundMap

      public SoundMap​(char[][] level, char alternateWall)
      Constructor meant to take a char[][] returned by DungeonBoneGen.generate(), or any other char[][] where one char means a wall and anything else is a walkable tile. If you only have a map that uses box-drawing characters, use DungeonUtility.linesToHashes() to get a map that can be used here. You can specify the character used for walls.
      Parameters:
      level -
    • SoundMap

      public SoundMap​(char[][] level, Measurement measurement)
      Constructor meant to take a char[][] returned by DungeonBoneGen.generate(), or any other char[][] where '#' means a wall and anything else is a walkable tile. If you only have a map that uses box-drawing characters, use DungeonUtility.linesToHashes() to get a map that can be used here. This constructor specifies a distance measurement.
      Parameters:
      level -
      measurement -
  • Method Details

    • initialize

      public SoundMap initialize​(double[][] level)
      Used to initialize or re-initialize a SoundMap that needs a new PhysicalMap because it either wasn't given one when it was constructed, or because the contents of the terrain have changed permanently.
      Parameters:
      level -
      Returns:
    • initialize

      public SoundMap initialize​(char[][] level)
      Used to initialize or re-initialize a SoundMap that needs a new PhysicalMap because it either wasn't given one when it was constructed, or because the contents of the terrain have changed permanently.
      Parameters:
      level -
      Returns:
    • initialize

      public SoundMap initialize​(char[][] level, char alternateWall)
      Used to initialize or re-initialize a SoundMap that needs a new PhysicalMap because it either wasn't given one when it was constructed, or because the contents of the terrain have changed permanently. This initialize() method allows you to specify an alternate wall char other than the default character, '#' .
      Parameters:
      level -
      alternateWall -
      Returns:
    • resetMap

      public void resetMap()
      Resets the gradientMap to its original value from physicalMap. Does not remove sounds (they will still affect scan() normally).
    • reset

      public void reset()
      Resets this SoundMap to a state with no sounds, no alerted creatures, and no changes made to gradientMap relative to physicalMap.
    • setSound

      public void setSound​(int x, int y, double loudness)
      Marks a cell as producing a sound with the given loudness; this can be placed on a wall or unreachable area, but that may cause the sound to be un-hear-able. A sound emanating from a cell on one side of a 2-cell-thick wall will only radiate sound on one side, which can be used for certain effects. A sound emanating from a cell in a 1-cell-thick wall will radiate on both sides.
      Parameters:
      x -
      y -
      loudness - The number of cells the sound should spread away using the current measurement.
    • setSound

      public void setSound​(Coord pt, double loudness)
      Marks a cell as producing a sound with the given loudness; this can be placed on a wall or unreachable area, but that may cause the sound to be un-hear-able. A sound emanating from a cell on one side of a 2-cell-thick wall will only radiate sound on one side, which can be used for certain effects. A sound emanating from a cell in a 1-cell-thick wall will radiate on both sides.
      Parameters:
      pt -
      loudness - The number of cells the sound should spread away using the current measurement.
    • removeSound

      public void removeSound​(int x, int y)
      If a sound is being produced at a given (x, y) location, this removes it.
      Parameters:
      x -
      y -
    • removeSound

      public void removeSound​(Coord pt)
      If a sound is being produced at a given location (a Coord), this removes it.
      Parameters:
      pt -
    • setOccupied

      public void setOccupied​(int x, int y)
      Marks a specific cell in gradientMap as a wall, which makes sounds potentially unable to pass through it.
      Parameters:
      x -
      y -
    • resetCell

      public void resetCell​(int x, int y)
      Reverts a cell to the value stored in the original state of the level as known by physicalMap.
      Parameters:
      x -
      y -
    • resetCell

      public void resetCell​(Coord pt)
      Reverts a cell to the value stored in the original state of the level as known by physicalMap.
      Parameters:
      pt -
    • clearSounds

      public void clearSounds()
      Used to remove all sounds.
    • setFresh

      protected void setFresh​(int x, int y, double counter)
    • setFresh

      protected void setFresh​(Coord pt, double counter)
    • scan

      public double[][] scan()
      Recalculate the sound map and return it. Cells that were marked as goals with setSound will have a value greater than 0 (higher numbers are louder sounds), the cells adjacent to sounds will have a value 1 less than the loudest adjacent cell, and cells progressively further from sounds will have a value equal to the loudness of the nearest sound minus the distance from it, to a minimum of 0. The exceptions are walls, which will have a value defined by the WALL constant in this class. Like sound itself, the sound map allows some passage through walls; specifically, 1 cell thick of wall can be passed through, with reduced loudness, before the fill cannot go further. This uses the current measurement.
      Returns:
      A 2D double[width][height] using the width and height of what this knows about the physical map.
    • findAlerted

      public OrderedMap<Coord,​Double> findAlerted​(Set<Coord> creatures, Map<Coord,​Double> extraSounds)
      Scans the dungeon using SoundMap.scan, adding any positions in extraSounds to the group of known sounds before scanning. The creatures passed to this function as a Set of Points will have the loudness of all sounds at their position put as the value in alerted corresponding to their Coord position.
      Parameters:
      creatures -
      extraSounds -
      Returns: