mapgen.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. darkgen = {}
  2. darkgen.modpath = minetest.get_modpath("darkgen")
  3. darkgen.SHEET_HEIGHT = 20
  4. dofile(darkgen.modpath .. "/noise.lua")
  5. -- Content IDs used with the voxel manipulator.
  6. local c_rock = minetest.get_content_id("default:stone")
  7. local c_water = minetest.get_content_id("default:water_source")
  8. local c_silt = minetest.get_content_id("darkage:silt")
  9. local c_mud = minetest.get_content_id("darkage:mud")
  10. local c_ore1 = minetest.get_content_id("darkage:ors")
  11. local c_ore2 = minetest.get_content_id("darkage:shale")
  12. local c_ore3 = minetest.get_content_id("darkage:slate")
  13. local c_ore4 = minetest.get_content_id("darkage:basaltic")
  14. local c_ore5 = minetest.get_content_id("darkage:marble")
  15. local c_ore6 = minetest.get_content_id("darkage:gneiss")
  16. local c_ore7 = minetest.get_content_id("darkage:schist")
  17. local c_ore8 = minetest.get_content_id("darkage:chalk")
  18. local c_ore10 = minetest.get_content_id("darkage:rhyolitic_tuff")
  19. local c_ore11 = minetest.get_content_id("darkage:tuff")
  20. -- Both of these are craftable from existing rock types.
  21. -- darkage:rhyolitic_tuff
  22. -- darkage:tuff
  23. -- These generate at ocean bottoms.
  24. -- darkage:silt
  25. -- darkage:mud
  26. -- Already craftable.
  27. -- darkage:darkdirt
  28. -- Externally located tables for performance.
  29. local data = {}
  30. local noisemap1 = {}
  31. local noisemap2 = {}
  32. local noisemap3 = {}
  33. local noisemap4 = {}
  34. local noisemap5 = {}
  35. local noisemap6 = {}
  36. local noisemap7 = {}
  37. local noisemap8 = {}
  38. local noisemap9 = {}
  39. local noisemap10= {}
  40. local noisemap11= {}
  41. darkgen.generate_realm = function(vm, minp, maxp, seed)
  42. local nstart = darkgen.SHEET_HEIGHT
  43. -- Don't run for out-of-bounds mapchunks.
  44. -- Need -100 in order to include ocean basins under the ice.
  45. if minp.y > (nstart + 200) or maxp.y < (nstart - 100) then return end
  46. -- Grab the voxel manipulator.
  47. local emin, emax = vm:get_emerged_area()
  48. vm:get_data(data) -- Read current map data.
  49. local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
  50. local area2 = VoxelArea:new{MinEdge=minp, MaxEdge=maxp}
  51. local pr = PseudoRandom(seed + 71)
  52. local x1 = maxp.x
  53. local y1 = maxp.y
  54. local z1 = maxp.z
  55. local x0 = minp.x
  56. local y0 = minp.y
  57. local z0 = minp.z
  58. -- Compute side lengths.
  59. local side_len_x = ((x1-x0)+1)
  60. local side_len_y = ((y1-y0)+1)
  61. local side_len_z = ((z1-z0)+1)
  62. local sides3D = {x=side_len_x, y=side_len_y, z=side_len_z}
  63. local sides2D = {x=side_len_x, y=side_len_z}
  64. local bp3d = {x=x0, y=y0, z=z0}
  65. local bp2d = {x=x0, y=z0}
  66. -- Get noisemaps.
  67. local perlin1 = minetest.get_perlin_map(darkgen.noise1param2d, sides2D)
  68. perlin1:get_2d_map_flat(bp2d, noisemap1)
  69. local perlin2 = minetest.get_perlin_map(darkgen.noise2param2d, sides2D)
  70. perlin2:get_2d_map_flat(bp2d, noisemap2)
  71. local perlin3 = minetest.get_perlin_map(darkgen.noise3param2d, sides2D)
  72. perlin3:get_2d_map_flat(bp2d, noisemap3)
  73. local perlin4 = minetest.get_perlin_map(darkgen.noise4param2d, sides2D)
  74. perlin4:get_2d_map_flat(bp2d, noisemap4)
  75. local perlin5 = minetest.get_perlin_map(darkgen.noise5param2d, sides2D)
  76. perlin5:get_2d_map_flat(bp2d, noisemap5)
  77. local perlin6 = minetest.get_perlin_map(darkgen.noise6param2d, sides2D)
  78. perlin6:get_2d_map_flat(bp2d, noisemap6)
  79. local perlin7 = minetest.get_perlin_map(darkgen.noise7param2d, sides2D)
  80. perlin7:get_2d_map_flat(bp2d, noisemap7)
  81. local perlin8 = minetest.get_perlin_map(darkgen.noise8param2d, sides2D)
  82. perlin8:get_2d_map_flat(bp2d, noisemap8)
  83. local perlin9 = minetest.get_perlin_map(darkgen.noise9param2d, sides2D)
  84. perlin9:get_2d_map_flat(bp2d, noisemap9)
  85. local perlin10 = minetest.get_perlin_map(darkgen.noise10param2d, sides2D)
  86. perlin10:get_2d_map_flat(bp2d, noisemap10)
  87. local perlin11 = minetest.get_perlin_map(darkgen.noise11param2d, sides2D)
  88. perlin11:get_2d_map_flat(bp2d, noisemap11)
  89. -- Localize commonly used functions.
  90. local floor = math.floor
  91. local ceil = math.ceil
  92. local abs = math.abs
  93. --[[
  94. -- Pre-pass: clean up C++ mapgen flat-slab leftovers.
  95. for z = emin.z, emax.z do
  96. for x = emin.x, emax.x do
  97. for y = emin.y, emax.y do
  98. local vp = area:index(x, y, z)
  99. -- Get the type already generated at this position.
  100. local ip = data[vp]
  101. -- Note: sometimes these will be nil, because of accessing outside the array.
  102. -- (We are scanning through the emin/emax range.)
  103. local iu = data[area:index(x, y+1, z)]
  104. local id = data[area:index(x, y-1, z)]
  105. -- HACK:
  106. -- Get rid of the <BEEP> flat horizontal slabs that appear at chunk top/bot edges
  107. -- whenever emerge threads are more than 1. We have to do this *indiscriminately*,
  108. -- which unfortunately modifies the terrain shape more than is actually necessary.
  109. if ip == c_stone then
  110. if (id == c_air or id == c_ignore) and (iu == c_air or iu == c_ignore) then
  111. data[vp] = c_air
  112. end
  113. end
  114. end
  115. end
  116. end
  117. --]]
  118. local function apply_ore(v, y, n, f, m, o)
  119. local bot = (nstart + f) - (n*m)
  120. local top = (nstart + f) + (n*m)
  121. if y >= bot and y <= top then
  122. if data[v] == c_rock then
  123. data[v] = o
  124. end
  125. end
  126. end
  127. -- First mapgen pass.
  128. for z = z0, z1 do
  129. for x = x0, x1 do
  130. -- Get index into 2D noise arrays.
  131. local nx = (x-x0)
  132. local nz = (z-z0)
  133. local ni2 = (side_len_z*nz+nx)
  134. ni2 = ni2 + 1 -- Lua arrays start indexing at 1, not 0. Urrrrgh. >:(
  135. local n1 = noisemap1[ni2]
  136. local n2 = noisemap2[ni2]
  137. local n3 = noisemap3[ni2]
  138. local n4 = noisemap4[ni2]
  139. local n5 = noisemap5[ni2]
  140. local n6 = noisemap6[ni2]
  141. local n7 = noisemap7[ni2]
  142. local n8 = noisemap8[ni2]
  143. local n9 = noisemap9[ni2]
  144. local n10= noisemap10[ni2]
  145. local n11= noisemap11[ni2]
  146. -- First pass through column.
  147. for y = y0, y1 do
  148. local vp = area:index(x, y, z)
  149. local vu = area:index(x, y+1, z)
  150. local vd = area:index(x, y-1, z)
  151. local vd2 = area:index(x, y-2, z)
  152. -- Layer silt at ocean bottoms.
  153. if data[vp] == c_rock and data[vu] == c_water then
  154. data[vp] = c_silt
  155. data[vd] = c_silt
  156. data[vd2] = c_mud
  157. end
  158. local mult = 40
  159. local off = n9 * 16
  160. for k = 1, 1, 1 do
  161. apply_ore(vp, y, n1, 0 + (k*mult) + off, 2, c_ore1)
  162. apply_ore(vp, y, n2, 4 + (k*mult) + off, 2, c_ore2)
  163. apply_ore(vp, y, n3, -4 + (k*mult) + off, 2, c_ore3)
  164. apply_ore(vp, y, n4, -8 + (k*mult) + off, 2, c_ore4)
  165. apply_ore(vp, y, n5, -12 + (k*mult) + off, 2, c_ore5)
  166. apply_ore(vp, y, n6, 8 + (k*mult) + off, 2, c_ore6)
  167. apply_ore(vp, y, n7, 12 + (k*mult) + off, 2, c_ore7)
  168. apply_ore(vp, y, n8, 18 + (k*mult) + off, 2, c_ore8)
  169. end
  170. apply_ore(vp, y, n10, 80 + off, 4, c_ore10)
  171. apply_ore(vp, y, n11, 90 + off, 4, c_ore11)
  172. end -- For all in Y coordinates.
  173. end -- For all in X coordinates.
  174. end -- For all in Z coordinates.
  175. -- Finalize voxel manipulator.
  176. vm:set_data(data)
  177. --vm:set_lighting({day=0, night=0})
  178. --vm:calc_lighting()
  179. --vm:update_liquids()
  180. --vm:write_to_map()
  181. end
  182. -- Register the mapgen callback.
  183. minetest.register_on_generated(function(...)
  184. darkgen.generate_realm(...)
  185. end)