hexyz is tower defense game, and a lua library for dealing with hexagonal grids
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. RANDOM_CALLS_COUNT = 0
  2. -- https://stackoverflow.com/a/32387452/12464892
  3. local function bitwise_and(a, b)
  4. local result = 0
  5. local bit = 1
  6. while a > 0 and b > 0 do
  7. if a % 2 == 1 and b % 2 == 1 then
  8. result = result + bit
  9. end
  10. bit = bit * 2 -- shift left
  11. a = math.floor(a/2) -- shift-right
  12. b = math.floor(b/2)
  13. end
  14. return result
  15. end
  16. -- https://stackoverflow.com/a/20177466/12464892
  17. local A1, A2 = 727595, 798405 -- 5^17=D20*A1+A2
  18. local D20, D40 = 1048576, 1099511627776 -- 2^20, 2^40
  19. local X1, X2 = 0, 1
  20. local function rand()
  21. local U = X2*A2
  22. local V = (X1*A2 + X2*A1) % D20
  23. V = (V*D20 + U) % D40
  24. X1 = math.floor(V/D20)
  25. X2 = V - X1*D20
  26. return V/D40
  27. end
  28. local SEED_BOUNDS = 2^20 - 1
  29. local function randomseed2(seed)
  30. -- 0 <= X1 <= 2^20-1, 1 <= X2 <= 2^20-1 (must be odd!)
  31. -- ensure the number is odd, and within bounds of the generator
  32. local seed = bitwise_and(seed, 1)
  33. local v = math.clamp(math.abs(seed), 0, SEED_BOUNDS)
  34. X1 = v
  35. X2 = v + 1
  36. end
  37. local R = math.random
  38. local RS = math.randomseed
  39. local function random(n, m)
  40. RANDOM_CALLS_COUNT = RANDOM_CALLS_COUNT + 1
  41. local r
  42. if n then
  43. -- @TODO there are some bugs with calling math.random or am.rand
  44. -- with n or m values, so usually we should just avoid it... not sure what's it about yet
  45. log('calling random with "n" %g', n)
  46. if m then
  47. r = math.floor(R() * (m - n) + n)
  48. else
  49. r = math.floor(R() * n)
  50. end
  51. else
  52. r = R()
  53. end
  54. return r
  55. end
  56. math.randomseed = function(seed)
  57. RANDOM_CALLS_COUNT = 0
  58. R = am.rand(seed)
  59. math.random = random
  60. end
  61. -- whenever we refer to math.random, actually use the function 'random' above
  62. math.random = random
  63. function g_octave_noise(x, y, num_octaves, seed)
  64. local seed = seed or os.time()
  65. local noise = 0
  66. for oct = 1, num_octaves do
  67. local f = 1/4^oct
  68. local l = 2^oct
  69. local pos = vec2(x + seed, y + seed)
  70. noise = noise + f * math.simplex(pos * l)
  71. end
  72. return noise
  73. end
  74. -- @TODO test, fix
  75. function poisson_knuth(lambda)
  76. local e = 2.71828
  77. local L = e^-lambda
  78. local k = 0
  79. local p = 1
  80. while p > L do
  81. k = k + 1
  82. p = p * math.random()
  83. end
  84. return k - 1
  85. end
  86. -- seed the random number generator with the current time
  87. -- os.clock() is better if the program has been running for a little bit.
  88. math.randomseed(os.time())