init.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. if not minetest.global_exists("oregen") then oregen = {} end
  2. oregen.modpath = minetest.get_modpath("oregen")
  3. -- Localize for performance.
  4. local math_floor = math.floor
  5. -- Settings. These affect all ores registered through this mod.
  6. oregen.scarcity_multiplier = 1.0
  7. oregen.scarcity_blob_multiplier = 4.0
  8. oregen.count_multiplier = 1.0
  9. oregen.size_multiplier = 1.0
  10. oregen.depth_offset = -20
  11. oregen.noise_threshold_offset = 0.2
  12. local noise_params = {
  13. offset = 0.0,
  14. scale = 1.0,
  15. spread = {x=256, y=256, z=256},
  16. seed = 0, -- Note: the code changes this at will.
  17. octaves = 1,
  18. persist = 0.0,
  19. }
  20. -- Defaults, so that registrations can omit values they don't care about.
  21. local default = {
  22. algorithm = "scatter",
  23. ore = "air",
  24. wherein = "default:stone",
  25. scarcity = 20*20*20,
  26. count = 3,
  27. diameter = 2,
  28. miny = -32000,
  29. maxy = 32000,
  30. noise_threshold = 0.0,
  31. noise_params = noise_params,
  32. random_factor = 1.0,
  33. }
  34. local get_hash = function(name)
  35. local num = 0
  36. local cnt = string.len(name)
  37. if cnt > 64 then cnt = 64 end
  38. for i = 1, cnt, 1 do
  39. num = num + string.byte(name, i)
  40. end
  41. return num
  42. end
  43. -- Public API function.
  44. oregen.register_ore = function(oredef)
  45. -- Name compatibility.
  46. if oredef.ore_type ~= nil then oredef.algorithm = oredef.ore_type end
  47. if oredef.clust_scarcity ~= nil then oredef.scarcity = oredef.clust_scarcity end
  48. if oredef.clust_num_ores ~= nil then oredef.count = oredef.clust_num_ores end
  49. if oredef.clust_size ~= nil then oredef.diameter = oredef.clust_size end
  50. if oredef.y_min ~= nil then oredef.miny = oredef.y_min end
  51. if oredef.y_max ~= nil then oredef.maxy = oredef.y_max end
  52. -- Get params or defaults.
  53. local algorithm = oredef.algorithm or default.algorithm
  54. local ore = oredef.ore or default.ore
  55. local wherein = oredef.wherein or default.wherein
  56. local scarcity = oredef.scarcity or default.scarcity
  57. local count = oredef.count or default.count
  58. local diameter = oredef.diameter or default.diameter
  59. local miny = oredef.miny or default.miny
  60. local maxy = oredef.maxy or default.maxy
  61. local random_factor = oredef.random_factor or default.random_factor
  62. -- Note: this triggers orefull/oreless regions for all scatter type ores!
  63. local noise_threshold = oredef.noise_threshold or default.noise_threshold
  64. local noise_params = oredef.noise_params or default.noise_params
  65. count = math_floor(count * oregen.count_multiplier + 0.5)
  66. if count <= 1 then count = 1 end
  67. if algorithm == "blob" then
  68. scarcity = math_floor(scarcity * oregen.scarcity_blob_multiplier + 0.5)
  69. if scarcity <= 1 then scarcity = 1 end
  70. else
  71. scarcity = math_floor(scarcity * oregen.scarcity_multiplier + 0.5)
  72. if scarcity <= 1 then scarcity = 1 end
  73. end
  74. if algorithm == "standard_blob" then
  75. algorithm = "blob"
  76. end
  77. diameter = math_floor(diameter * oregen.size_multiplier + 0.5)
  78. if diameter <= 1 then diameter = 1 end
  79. miny = miny + oregen.depth_offset
  80. maxy = maxy + oregen.depth_offset
  81. noise_threshold = noise_threshold + oregen.noise_threshold_offset
  82. local seed = get_hash(ore)
  83. --print("OREGEN: '" .. ore .. "' == " .. seed .. "!")
  84. noise_params.seed = seed
  85. assert(maxy > miny)
  86. local tbr = {
  87. ore_type = algorithm,
  88. ore = ore,
  89. wherein = wherein,
  90. clust_scarcity = scarcity,
  91. clust_num_ores = count,
  92. clust_size = diameter,
  93. y_min = miny,
  94. y_max = maxy,
  95. noise_threshold = noise_threshold,
  96. noise_params = noise_params,
  97. random_factor = random_factor,
  98. }
  99. --minetest.log(dump(tbr))
  100. minetest.register_ore(tbr)
  101. end