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.

76 lines
1.8 KiB

  1. -- seed the random number generator with the current time
  2. math.randomseed(os.clock())
  3. -- https://stackoverflow.com/a/20177466/12464892
  4. --local A1, A2 = 727595, 798405 -- 5^17=D20*A1+A2
  5. --local D20, D40 = 1048576, 1099511627776 -- 2^20, 2^40
  6. --local X1, X2 = 0, 1
  7. --local function rand()
  8. -- local U = X2*A2
  9. -- local V = (X1*A2 + X2*A1) % D20
  10. -- V = (V*D20 + U) % D40
  11. -- X1 = math.floor(V/D20)
  12. -- X2 = V - X1*D20
  13. -- return V/D40
  14. --end
  15. --
  16. --local SEED_BOUNDS = 2^20 - 1
  17. --math.randomseed = function(seed)
  18. -- local v = math.clamp(math.abs(seed), 0, SEED_BOUNDS)
  19. -- X1 = v
  20. -- X2 = v + 1
  21. --end
  22. -- to enable allowing the random number generator's state to be restored post-load (game-deserialize),
  23. -- we count the number of times we call math.random(), and on deserialize, seed the random
  24. -- number generator, and then discard |count| calls.
  25. local R = math.random
  26. RANDOM_CALLS_COUNT = 0
  27. local function random(n, m)
  28. RANDOM_CALLS_COUNT = RANDOM_CALLS_COUNT + 1
  29. if n then
  30. if m then
  31. return R(n, m)
  32. else
  33. return R(n)
  34. end
  35. else
  36. return R()
  37. end
  38. end
  39. -- whenever we refer to math.random, actually use the function 'random' above
  40. math.random = random
  41. function g_octave_noise(x, y, num_octaves, seed)
  42. local seed = seed or os.clock()
  43. local noise = 0
  44. for oct = 1, num_octaves do
  45. local f = 1/4^oct
  46. local l = 2^oct
  47. local pos = vec2(x + seed, y + seed)
  48. noise = noise + f * math.simplex(pos * l)
  49. end
  50. return noise
  51. end
  52. ---- @TODO test, fix
  53. --function poisson_knuth(lambda)
  54. -- local e = 2.71828
  55. --
  56. -- local L = e^-lambda
  57. -- local k = 0
  58. -- local p = 1
  59. --
  60. -- while p > L do
  61. -- k = k + 1
  62. -- p = p * math.random()
  63. -- end
  64. --
  65. -- return k - 1
  66. --end