init.lua 4.1 KB

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