l_noise.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irr_v3d.h"
  18. #include "lua_api/l_base.h"
  19. #include "noise.h"
  20. /*
  21. LuaPerlinNoise
  22. */
  23. class LuaPerlinNoise : public ModApiBase
  24. {
  25. private:
  26. NoiseParams np;
  27. static const char className[];
  28. static luaL_Reg methods[];
  29. // Exported functions
  30. // garbage collector
  31. static int gc_object(lua_State *L);
  32. static int l_get_2d(lua_State *L);
  33. static int l_get_3d(lua_State *L);
  34. public:
  35. LuaPerlinNoise(NoiseParams *params);
  36. ~LuaPerlinNoise() = default;
  37. // LuaPerlinNoise(seed, octaves, persistence, scale)
  38. // Creates an LuaPerlinNoise and leaves it on top of stack
  39. static int create_object(lua_State *L);
  40. static LuaPerlinNoise *checkobject(lua_State *L, int narg);
  41. static void Register(lua_State *L);
  42. };
  43. /*
  44. LuaPerlinNoiseMap
  45. */
  46. class LuaPerlinNoiseMap : public ModApiBase
  47. {
  48. NoiseParams np;
  49. Noise *noise;
  50. bool m_is3d;
  51. static const char className[];
  52. static luaL_Reg methods[];
  53. // Exported functions
  54. // garbage collector
  55. static int gc_object(lua_State *L);
  56. static int l_get_2d_map(lua_State *L);
  57. static int l_get_2d_map_flat(lua_State *L);
  58. static int l_get_3d_map(lua_State *L);
  59. static int l_get_3d_map_flat(lua_State *L);
  60. static int l_calc_2d_map(lua_State *L);
  61. static int l_calc_3d_map(lua_State *L);
  62. static int l_get_map_slice(lua_State *L);
  63. public:
  64. LuaPerlinNoiseMap(NoiseParams *np, s32 seed, v3s16 size);
  65. ~LuaPerlinNoiseMap();
  66. // LuaPerlinNoiseMap(np, size)
  67. // Creates an LuaPerlinNoiseMap and leaves it on top of stack
  68. static int create_object(lua_State *L);
  69. static LuaPerlinNoiseMap *checkobject(lua_State *L, int narg);
  70. static void Register(lua_State *L);
  71. };
  72. /*
  73. LuaPseudoRandom
  74. */
  75. class LuaPseudoRandom : public ModApiBase
  76. {
  77. private:
  78. PseudoRandom m_pseudo;
  79. static const char className[];
  80. static const luaL_Reg methods[];
  81. // Exported functions
  82. // garbage collector
  83. static int gc_object(lua_State *L);
  84. // next(self, min=0, max=32767) -> get next value
  85. static int l_next(lua_State *L);
  86. public:
  87. LuaPseudoRandom(s32 seed) : m_pseudo(seed) {}
  88. // LuaPseudoRandom(seed)
  89. // Creates an LuaPseudoRandom and leaves it on top of stack
  90. static int create_object(lua_State *L);
  91. static LuaPseudoRandom *checkobject(lua_State *L, int narg);
  92. static void Register(lua_State *L);
  93. };
  94. /*
  95. LuaPcgRandom
  96. */
  97. class LuaPcgRandom : public ModApiBase
  98. {
  99. private:
  100. PcgRandom m_rnd;
  101. static const char className[];
  102. static const luaL_Reg methods[];
  103. // Exported functions
  104. // garbage collector
  105. static int gc_object(lua_State *L);
  106. // next(self, min=-2147483648, max=2147483647) -> get next value
  107. static int l_next(lua_State *L);
  108. // rand_normal_dist(self, min=-2147483648, max=2147483647, num_trials=6) ->
  109. // get next normally distributed random value
  110. static int l_rand_normal_dist(lua_State *L);
  111. public:
  112. LuaPcgRandom(u64 seed) : m_rnd(seed) {}
  113. LuaPcgRandom(u64 seed, u64 seq) : m_rnd(seed, seq) {}
  114. // LuaPcgRandom(seed)
  115. // Creates an LuaPcgRandom and leaves it on top of stack
  116. static int create_object(lua_State *L);
  117. static LuaPcgRandom *checkobject(lua_State *L, int narg);
  118. static void Register(lua_State *L);
  119. };
  120. /*
  121. LuaSecureRandom
  122. */
  123. class LuaSecureRandom : public ModApiBase
  124. {
  125. private:
  126. static const size_t RAND_BUF_SIZE = 2048;
  127. static const char className[];
  128. static const luaL_Reg methods[];
  129. u32 m_rand_idx;
  130. char m_rand_buf[RAND_BUF_SIZE];
  131. // Exported functions
  132. // garbage collector
  133. static int gc_object(lua_State *L);
  134. // next_bytes(self, count) -> get count many bytes
  135. static int l_next_bytes(lua_State *L);
  136. public:
  137. bool fillRandBuf();
  138. // LuaSecureRandom()
  139. // Creates an LuaSecureRandom and leaves it on top of stack
  140. static int create_object(lua_State *L);
  141. static LuaSecureRandom *checkobject(lua_State *L, int narg);
  142. static void Register(lua_State *L);
  143. };