mapgen.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. local set_node = minetest.set_node
  2. local get_node = minetest.get_node
  3. local swap_node = minetest.swap_node
  4. local ipairs = ipairs
  5. -- Content IDs used with the voxel manipulator.
  6. local c_air = minetest.get_content_id("air")
  7. local c_ignore = minetest.get_content_id("ignore")
  8. local c_stone = minetest.get_content_id("default:stone")
  9. -- These 2 types are placed at and below the level of the brimstone ocean.
  10. -- They disallow the C++ cavegen from running.
  11. local c_redrack = minetest.get_content_id("rackstone:redrack")
  12. local c_rackstone = minetest.get_content_id("rackstone:rackstone")
  13. -- These 2 types are placed above the brimstone ocean.
  14. -- The C++ cavegen is allowed to run in these nodes.
  15. local c_mgredrack = minetest.get_content_id("rackstone:mg_redrack")
  16. local c_mgrackstone = minetest.get_content_id("rackstone:mg_rackstone")
  17. local c_whitestone = minetest.get_content_id("whitestone:stone")
  18. local c_mossycobble = minetest.get_content_id("default:mossycobble")
  19. local c_cobble = minetest.get_content_id("default:cobble")
  20. local c_netherbrick = minetest.get_content_id("rackstone:brick")
  21. local c_cobblestair = minetest.get_content_id("stairs:stair_cobble")
  22. local c_netherstair = minetest.get_content_id("stairs:stair_rackstone_brick")
  23. local c_water = minetest.get_content_id("default:water_source")
  24. local c_waterflow = minetest.get_content_id("default:water_flowing")
  25. local c_lava = minetest.get_content_id("default:lava_source")
  26. local c_nlava = minetest.get_content_id("lbrim:lava_source")
  27. local c_lavaflow = minetest.get_content_id("default:lava_flowing")
  28. local c_obsidian = minetest.get_content_id("default:obsidian")
  29. local c_vine = minetest.get_content_id("nethervine:vine")
  30. local c_bedrock = minetest.get_content_id("bedrock:bedrock")
  31. -- Externally located tables for performance.
  32. local data = {}
  33. local noisemap1 = {}
  34. local noisemap2 = {}
  35. local noisemap3 = {}
  36. local noisemap4 = {}
  37. local noisemap5 = {}
  38. --[[
  39. Notes on mapgen function. IMPORTANT!
  40. The code loops through all positions in the mapchunk being generated *twice*, once to generate the nether,
  41. and the second time to repair problems at chunk borders caused by caves overgenerating in the C++ mapgen.
  42. These two loops work together, and must be understood together, since the loop which overgenerates can touch
  43. chunks previously generated by the first 'normal' loop.
  44. This 'hack' would not be necessary if we were using Singlenode for the C++ mapgen.
  45. However, we're using Valleys, so the Lua mapgen must work nicely with it.
  46. --]]
  47. nethermapgen.generate_realm = function(minp, maxp, seed)
  48. local nstart = nethermapgen.NETHER_START
  49. local bstart = nethermapgen.BRIMSTONE_OCEAN
  50. local rstart = nethermapgen.BEDROCK_DEPTH
  51. -- Don't run for out-of-bounds mapchunks.
  52. local mindepth = nstart + 100
  53. if minp.y > mindepth and maxp.y > mindepth then return end
  54. -- Grab the voxel manipulator.
  55. local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
  56. vm:get_data(data) -- Read current map data.
  57. local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
  58. local area2 = VoxelArea:new{MinEdge=minp, MaxEdge=maxp}
  59. local pr = PseudoRandom(seed + 65)
  60. local x1 = maxp.x
  61. local y1 = maxp.y
  62. local z1 = maxp.z
  63. local x0 = minp.x
  64. local y0 = minp.y
  65. local z0 = minp.z
  66. -- Compute side lengths.
  67. local side_len_x = ((x1-x0)+1)
  68. local side_len_y = ((y1-y0)+1)
  69. local side_len_z = ((z1-z0)+1)
  70. local sides3D = {x=side_len_x, y=side_len_y, z=side_len_z}
  71. local sides2D = {x=side_len_x, y=side_len_z}
  72. local bp3d = {x=x0, y=y0, z=z0}
  73. local bp2d = {x=x0, y=z0}
  74. -- Get noisemaps.
  75. local perlin1 = minetest.get_perlin_map(nethermapgen.noise1param2d, sides2D)
  76. perlin1:get2dMap_flat(bp2d, noisemap1)
  77. local perlin2 = minetest.get_perlin_map(nethermapgen.noise2param2d, sides2D)
  78. perlin2:get2dMap_flat(bp2d, noisemap2)
  79. local perlin3 = minetest.get_perlin_map(nethermapgen.noise3param2d, sides2D)
  80. perlin3:get2dMap_flat(bp2d, noisemap3)
  81. local perlin4 = minetest.get_perlin_map(nethermapgen.noise4param2d, sides2D)
  82. perlin4:get2dMap_flat(bp2d, noisemap4)
  83. local perlin5 = minetest.get_perlin_map(nethermapgen.noise5param3d, sides3D)
  84. perlin5:get3dMap_flat(bp3d, noisemap5)
  85. -- Localize commonly used functions.
  86. local floor = math.floor
  87. local ceil = math.ceil
  88. local abs = math.abs
  89. -- First mapgen pass.
  90. for z = z0, z1 do
  91. for x = x0, x1 do
  92. -- Get index into 2D noise arrays.
  93. local nx = (x-x0)
  94. local nz = (z-z0)
  95. local ni2 = (side_len_z*nz+nx)
  96. ni2 = ni2 + 1 -- Lua arrays start indexing at 1, not 0. Urrrrgh. >:(
  97. local n1 = noisemap1[ni2]
  98. local n2 = noisemap2[ni2]
  99. local n3 = noisemap3[ni2]
  100. local n4 = noisemap4[ni2]
  101. local rs_top = ceil(nstart+abs(n1)*64)
  102. local rs_bot = floor(nstart-abs(n1)*64)
  103. local lakecel = bstart + 30
  104. local lakelav = bstart
  105. local lakebed = bstart - 2
  106. local islandi = bstart - 2
  107. if n4 > -0.3 then
  108. if abs(n2) > 0.6 then
  109. islandi = bstart + 5
  110. islandi = islandi + pr:next(0, pr:next(1, 2))
  111. islandi = islandi + floor(n1*4)
  112. -- Create random holes.
  113. if pr:next(1, 100) == 1 then islandi = bstart - 2 end
  114. elseif abs(n2) > 0.5 then
  115. islandi = islandi + pr:next(3, 5)
  116. end
  117. end
  118. lakecel = lakecel - pr:next(0, 1+floor(abs(n3) * 3.5))
  119. lakecel = lakecel + floor(abs(n1) * 6)
  120. -- First pass through column.
  121. for y = y0, y1 do
  122. local vp = area:index(x, y, z)
  123. local vu = area:index(x, y+1, z)
  124. local vd = area:index(x, y-1, z)
  125. -- 3D noise.
  126. local np = area2:index(x, y, z)
  127. local n5 = noisemap5[np]
  128. if y <= rs_top then
  129. -- Get the type already generated at this position.
  130. local ip = data[vp]
  131. -- Using mapdata already generated by the C++ mapgen, convert regular nodes to nether types.
  132. if ip == c_cobble or ip == c_mossycobble then
  133. data[vp] = c_netherbrick
  134. elseif ip == c_cobblestair then
  135. data[vp] = c_netherstair
  136. elseif ip == c_water or ip == c_waterflow then
  137. data[vp] = c_lava
  138. elseif ip ~= c_air and ip ~= c_vine then -- Vines can cross chunk borders. Don't replace them.
  139. -- Generate rackstone layer between nether and underworld.
  140. if y >= rs_bot and y <= rs_top then
  141. data[vp] = c_mgrackstone
  142. elseif y < rs_bot then
  143. if abs(n5) < 0.015 then
  144. data[vp] = c_mgrackstone
  145. else
  146. data[vp] = c_mgredrack
  147. end
  148. else
  149. data[vp] = c_stone
  150. end
  151. end
  152. -- Generate the endless nether lava ocean, with islands.
  153. if y <= lakebed or y <= islandi then
  154. -- Fill in map below brimstone ocean & create islands.
  155. data[vp] = c_redrack
  156. elseif y <= lakelav then
  157. data[vp] = c_nlava -- Special lava for brimstone ocean.
  158. elseif y <= lakecel then
  159. -- Carve out horizontal cavern above brimstone ocean.
  160. data[vp] = c_air
  161. elseif ip == c_air and y > lakecel and y <= lakecel+5 then
  162. -- Fill in huge ceiling holes created when huge caves
  163. -- intersect with the brimstone ocean realm.
  164. data[vp] = c_whitestone
  165. end
  166. end -- If Y coord is below nether start.
  167. end -- For all in Y coordinates.
  168. end -- For all in X coordinates.
  169. end -- For all in Z coordinates.
  170. -- Second mapgen pass, for things that need overgenerating.
  171. local ex1 = emax.x
  172. local ey1 = emax.y
  173. local ez1 = emax.z
  174. local ex0 = emin.x
  175. local ey0 = emin.y
  176. local ez0 = emin.z
  177. for z = ez0, ez1 do
  178. for x = ex0, ex1 do
  179. for y = ey0, ey1 do
  180. -- Get voxelmap indice.
  181. local vp = area:index(x, y, z)
  182. local ip = data[vp]
  183. -- Transform dungeons (over)-generated by the C++ mapgen.
  184. -- Also remove water.
  185. if y <= nstart then
  186. if ip == c_cobble or ip == c_mossycobble then
  187. data[vp] = c_netherbrick
  188. elseif ip == c_cobblestair then
  189. data[vp] = c_netherstair
  190. elseif ip == c_water or ip == c_waterflow then
  191. data[vp] = c_lava
  192. end
  193. end
  194. -- Bedrock layer.
  195. if y <= rstart+pr:next(1,pr:next(2, 3)) then
  196. data[vp] = c_bedrock
  197. end
  198. end
  199. end
  200. end
  201. -- These tables will be filled with positions for decorations,
  202. -- to be placed after the voxel manipulator code finishes.
  203. local chnk_pos_flame = {}
  204. local chnk_pos_crystal = {}
  205. local chnk_pos_tree = {}
  206. local chnk_pos_shroom = {}
  207. local chnk_pos_vine = {}
  208. local chnk_pos_grass = {}
  209. local chnk_pos_flower = {}
  210. local is_netherock = function(ip)
  211. if ip == c_redrack or
  212. ip == c_rackstone or
  213. ip == c_mgredrack or
  214. ip == c_mgrackstone then
  215. return true
  216. else
  217. return false
  218. end
  219. end
  220. -- Final mapgen pass, for finding floors and ceilings.
  221. -- Note: this has to be a seperate pass because otherwise the code
  222. -- will find floors and ceilings where there aren't any. Sad but true. :(
  223. for x = x0, x1 do
  224. for z = z0, z1 do
  225. for y = y0, y1 do
  226. local vp = area:index(x, y, z)
  227. local vu = area:index(x, y+1, z)
  228. local vd = area:index(x, y-1, z)
  229. local ip = data[vp]
  230. local iu = data[vu]
  231. local id = data[vd]
  232. -- Found floor.
  233. if is_netherock(ip) and iu == c_air then
  234. if pr:next(0, 2000) == 0 then
  235. chnk_pos_flame[#chnk_pos_flame+1] = {x=x, y=y+1, z=z}
  236. elseif pr:next(0, 5000) == 0 then
  237. chnk_pos_tree[#chnk_pos_tree+1] = {x=x, y=y+1, z=z}
  238. elseif pr:next(0, 3000) == 0 then
  239. chnk_pos_shroom[#chnk_pos_shroom+1] = {x=x, y=y+1, z=z}
  240. elseif pr:next(0, 200) == 0 then
  241. chnk_pos_grass[#chnk_pos_grass+1] = {x=x, y=y+1, z=z}
  242. elseif pr:next(0, 400) == 0 and y > -30750 then
  243. chnk_pos_flower[#chnk_pos_flower+1] = {x=x, y=y+1, z=z}
  244. end
  245. end
  246. -- Found roof.
  247. if is_netherock(ip) and id == c_air then
  248. if pr:next(0, 4000) == 0 then
  249. chnk_pos_crystal[#chnk_pos_crystal+1] = {x=x, y=y-1, z=z}
  250. elseif pr:next(0, 10) == 0 then
  251. chnk_pos_vine[#chnk_pos_vine+1] = {x=x, y=y-1, z=z}
  252. end
  253. end
  254. end
  255. end
  256. end
  257. -- Finalize voxel manipulator.
  258. vm:set_data(data)
  259. minetest.generate_ores(vm, minp, maxp)
  260. vm:set_lighting({day=0, night=0})
  261. vm:calc_lighting()
  262. vm:update_liquids()
  263. vm:write_to_map()
  264. for k, v in ipairs(chnk_pos_flame) do
  265. nethermapgen.scatter_flames(v)
  266. end
  267. for k, v in ipairs(chnk_pos_tree) do
  268. local p = table.copy(v)
  269. p.y = p.y - 1
  270. set_node(p, {name="rackstone:dauthsand"})
  271. firetree.create_firetree({x=p.x, y=p.y+1, z=p.z})
  272. end
  273. for k, v in ipairs(chnk_pos_shroom) do
  274. local p = table.copy(v)
  275. p.y = p.y - 1
  276. set_node(p, {name="rackstone:dauthsand"})
  277. redshroom.create_shroom({x=p.x, y=p.y+1, z=p.z})
  278. end
  279. for k, v in ipairs(chnk_pos_crystal) do
  280. nethermapgen.place_crystal(v)
  281. end
  282. for k, v in ipairs(chnk_pos_vine) do
  283. local p = table.copy(v)
  284. local r = pr:next(2, 7)
  285. for i = 1, r, 1 do
  286. if get_node(p).name ~= "air" then
  287. break
  288. end
  289. set_node(p, {name="nethervine:vine"})
  290. p.y = p.y - 1
  291. end
  292. end
  293. for k, v in ipairs(chnk_pos_grass) do
  294. -- Use swap_node to avoid triggering the construction timer.
  295. swap_node(v, {name="nether:grass_" .. math.random(1, 3)})
  296. end
  297. for k, v in ipairs(chnk_pos_flower) do
  298. -- Use swap_node to avoid triggering the construction timer.
  299. swap_node(v, {name="nether:glowflower"})
  300. end
  301. end