mapgen.lua 12 KB

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