Loading

Paste #pubdptwfo

  1. /**
  2.  * Height map.
  3.  * The data structure is used at many places, it contains requirements from all its users.
  4.  * The tgp Perlin map generator is a more demanding user.
  5.  */
  6. struct HeightMap
  7. {
  8.     HeightMap();
  9.     ~HeightMap();
  10.  
  11.     /** Fixed point type for heights, required by Perlin map generator tgp. */
  12.     typedef int16 height_t;
  13.  
  14.     height_t *h;     //< Array of heights (#size_x + 1) * (#size_y + 1).
  15.     /* (from tgp) Even though the sizes are always positive, there are many cases where
  16.      * X and Y need to be signed integers due to subtractions. */
  17.     uint dim_x;      //< Line length #size_x + 1.
  18.     uint total_size; //< height map total size
  19.     uint map_size;   //< Number of tiles in the map.
  20.     uint size_x;     //< Size of the map in X direction.
  21.     uint size_y;     //< Size of the map in Y direction.
  22.     uint log_x;      //< 2log of #size_x.
  23.     uint log_y;      //< 2log of #size_y.
  24.  
  25.     /**
  26.      * Get the size of the map in X direction.
  27.      * @return The size of the map in X direction.
  28.      */
  29.     inline int MapSizeX() const
  30.     {
  31.         return this->size_x;
  32.     }
  33.  
  34.     /**
  35.      * Get the size of the map in Y direction.
  36.      * @return The size of the map in Y direction.
  37.      */
  38.     inline int MapSizeY() const
  39.     {
  40.         return this->size_y;
  41.     }
  42.  
  43.     /**
  44.      * Get the number of tiles in the map.
  45.      * @return Number of files in the map.
  46.      */
  47.     inline int MapSize() const
  48.     {
  49.         return this->map_size;
  50.     }
  51.  
  52.     /**
  53.      * Is the given coordinate valid as map coordinate?
  54.      * @param x X coordinate.
  55.      * @param y Y coordinate.
  56.      * @return Whether the indicated position in on the map.
  57.      */
  58.     inline bool IsValidXY(int x, int y) const
  59.     {
  60.         return x >= 0 && x < (int)this->MapSizeX() && y >= 0 && y < (int)this->MapSizeY();
  61.     }
  62.  
  63.     /**
  64.      * Get biggest coordinate of the map in X direction.
  65.      * @return The biggest coordinate of the map in X direction.
  66.      */
  67.     inline int MapMaxX() const
  68.     {
  69.         return this->size_x - 1;
  70.     }
  71.  
  72.     /**
  73.      * Get biggest coordinate of the map in Y direction.
  74.      * @return The biggest coordinate of the map in Y direction.
  75.      */
  76.     inline int MapMaxY() const
  77.     {
  78.         return this->size_y - 1;
  79.     }
  80.  
  81.     /**
  82.      * Get the number of bits needed for storing an X coordinate.
  83.      * @return Number of bits needed for storing an X coordinate.
  84.      */
  85.     inline int MapLogX() const
  86.     {
  87.         return this->log_x;
  88.     }
  89.  
  90.     /**
  91.      * Get the number of bits needed for storing an Y coordinate.
  92.      * @return Number of bits needed for storing an Y coordinate.
  93.      */
  94.     inline int MapLogY() const
  95.     {
  96.         return this->log_y;
  97.     }
  98.  
  99.     /**
  100.      * Scales the given value by the map size, where the given value is for a 256 by 256 map.
  101.      * @param n the value to scale.
  102.      * @return the scaled size.
  103.      */
  104.     inline uint ScaleByMapSize(uint n) const
  105.     {
  106.         /* Subtract 12 from shift in order to prevent integer overflow
  107.          * for large values of n. It's safe since the min mapsize is 64x64. */
  108.         return CeilDiv(n << (this->MapLogX() + this->MapLogY() - 12), 1 << 4);
  109.     }
  110.  
  111.     /**
  112.      * Height map access (both read and write).
  113.      * @param x X position.
  114.      * @param y Y position.
  115.      * @return height data.
  116.      */
  117.     inline height_t &Height(uint x, uint y)
  118.     {
  119.         return this->h[x + y * this->dim_x];
  120.     }
  121.  
  122.     /**
  123.      * Get height of the map at a given position.
  124.      * @param x X position.
  125.      * @param y Y position.
  126.      * @return height data.
  127.      */
  128.     inline height_t Height(uint x, uint y) const
  129.     {
  130.         return this->h[x + y * this->dim_x];
  131.     }
  132.  
  133.     void Setup(uint size_x, uint size_y, HeightMap::height_t initial_height);
  134.     void FixSlopes();
  135.     void CopyToGlobalMap();
  136. };

Comments