diamondsquare.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright (C) 2015 Marien Raat
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #pragma once
  15. #include <random>
  16. #include <iostream>
  17. // The map array is a one dimensional array used to describe a two
  18. // dimensional array. The array is layed out with the rows next to
  19. // each other, so (x, y) kan be accesed on the index x + y * width.
  20. // To ease the use of this array, the simple macro TO_ARRAY_INDEX
  21. // is provided.
  22. #define TO_ARRAY_INDEX(x, y, width) (x + y * width)
  23. enum DiamondSquareMapType {
  24. MAPTYPE_LAND,
  25. MAPTYPE_ISLAND,
  26. MAPTYPE_ARCHIPELAGO
  27. };
  28. struct DiamondSquareMapSettings{
  29. int size; // Size of the square, should be a power of 2 plus 1
  30. double h; // The displacement will be decreased by 2 ^ (- h) each pass
  31. // A high h-value will result in a smooth map and a low h-value
  32. // will result in a rough maps
  33. DiamondSquareMapType mapType;
  34. bool overwriteOldMap; // If false, new space will be allocated in the
  35. // heap for the new map.
  36. // The user of the class is responsible for cleaning
  37. // up all maps and repeatedly running with this as
  38. // false will leak memory.
  39. double (*modifierFunction)(int, double, double *);
  40. double *modifiers; // An array of modifiers, can be NULL or
  41. // undefined if it isn't used in the modifier
  42. // function.
  43. };
  44. struct PassSettings {
  45. double *map; // Map to run the pass on
  46. double displacementModifier; // Displacement from drand() is multiplied
  47. // by this value. Given by 2 ^ (- (h * pass))
  48. int passSize; // Length of the sides of the square
  49. int mapSize; // Size of each side of the map
  50. };
  51. class DiamondSquareGenerator {
  52. public:
  53. DiamondSquareGenerator(std::mt19937 *mt);
  54. ~DiamondSquareGenerator();
  55. void createNewMap(DiamondSquareMapSettings settings);
  56. void pass(); // Apply the next pass to the map if it is made and
  57. // it is not done
  58. void finishMap(); // Calls pass() until isMapDone
  59. bool isMapDone();
  60. PassSettings currentPassSettings;
  61. double *map;
  62. private:
  63. DiamondSquareMapSettings currentMapSettings;
  64. bool mapInitialized, firstMap;
  65. std::mt19937 *mt;
  66. std::uniform_real_distribution<double> *stdDist;
  67. void passDiamondSquare(PassSettings *settings);
  68. void diamond(int x, int y, PassSettings *settings);
  69. void square(int x, int y, PassSettings *settings);
  70. bool isValid(int x, int y, int size);
  71. double drand(); // Returns a random double between -1.0 and 1.0
  72. // using the given mt and the stdDist
  73. void fillSidesWith(double *map, double cornerValue, int size);
  74. };
  75. // Simple functions to use as modifier functions
  76. double logaritmicDisplacementModifier(int pass, double h, double *modifiers);
  77. double customDisplacementModifier(int pass, double h, double *modifiers);