mapgen.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. -- mods/default/mapgen.lua
  2. --
  3. -- Aliases for map generator outputs
  4. --
  5. minetest.register_alias("mapgen_stone", "default:stone")
  6. minetest.register_alias("mapgen_tree", "default:tree")
  7. minetest.register_alias("mapgen_leaves", "default:leaves")
  8. minetest.register_alias("mapgen_jungletree", "default:jungletree")
  9. minetest.register_alias("mapgen_jungleleaves", "default:jungleleaves")
  10. minetest.register_alias("mapgen_apple", "default:apple")
  11. minetest.register_alias("mapgen_water_source", "default:water_source")
  12. minetest.register_alias("mapgen_dirt", "default:dirt")
  13. minetest.register_alias("mapgen_sand", "default:sand")
  14. minetest.register_alias("mapgen_gravel", "default:gravel")
  15. minetest.register_alias("mapgen_clay", "default:clay")
  16. minetest.register_alias("mapgen_lava_source", "default:lava_source")
  17. minetest.register_alias("mapgen_cobble", "default:cobble")
  18. minetest.register_alias("mapgen_mossycobble", "default:mossycobble")
  19. minetest.register_alias("mapgen_dirt_with_grass", "default:dirt_with_grass")
  20. minetest.register_alias("mapgen_junglegrass", "default:junglegrass")
  21. minetest.register_alias("mapgen_stone_with_coal", "default:stone_with_coal")
  22. minetest.register_alias("mapgen_stone_with_iron", "default:stone_with_iron")
  23. minetest.register_alias("mapgen_mese", "default:mese")
  24. minetest.register_alias("mapgen_desert_sand", "default:desert_sand")
  25. minetest.register_alias("mapgen_desert_stone", "default:desert_stone")
  26. minetest.register_alias("mapgen_stair_cobble", "stairs:stair_cobble")
  27. --
  28. -- Ore generation
  29. --
  30. minetest.register_ore({
  31. ore_type = "scatter",
  32. ore = "default:stone_with_coal",
  33. wherein = "default:stone",
  34. clust_scarcity = 8*8*8,
  35. clust_num_ores = 8,
  36. clust_size = 3,
  37. height_min = -31000,
  38. height_max = 64,
  39. })
  40. minetest.register_ore({
  41. ore_type = "scatter",
  42. ore = "default:stone_with_coal",
  43. wherein = "default:stone",
  44. clust_scarcity = 24*24*24,
  45. clust_num_ores = 27,
  46. clust_size = 6,
  47. height_min = -31000,
  48. height_max = 0,
  49. flags = "absheight",
  50. })
  51. minetest.register_ore({
  52. ore_type = "scatter",
  53. ore = "default:stone_with_iron",
  54. wherein = "default:stone",
  55. clust_scarcity = 12*12*12,
  56. clust_num_ores = 3,
  57. clust_size = 2,
  58. height_min = -15,
  59. height_max = 2,
  60. })
  61. minetest.register_ore({
  62. ore_type = "scatter",
  63. ore = "default:stone_with_iron",
  64. wherein = "default:stone",
  65. clust_scarcity = 9*9*9,
  66. clust_num_ores = 5,
  67. clust_size = 3,
  68. height_min = -63,
  69. height_max = -16,
  70. })
  71. minetest.register_ore({
  72. ore_type = "scatter",
  73. ore = "default:stone_with_iron",
  74. wherein = "default:stone",
  75. clust_scarcity = 7*7*7,
  76. clust_num_ores = 5,
  77. clust_size = 3,
  78. height_min = -31000,
  79. height_max = -64,
  80. flags = "absheight",
  81. })
  82. minetest.register_ore({
  83. ore_type = "scatter",
  84. ore = "default:stone_with_iron",
  85. wherein = "default:stone",
  86. clust_scarcity = 24*24*24,
  87. clust_num_ores = 27,
  88. clust_size = 6,
  89. height_min = -31000,
  90. height_max = -64,
  91. flags = "absheight",
  92. })
  93. minetest.register_ore({
  94. ore_type = "scatter",
  95. ore = "default:stone_with_mese",
  96. wherein = "default:stone",
  97. clust_scarcity = 18*18*18,
  98. clust_num_ores = 3,
  99. clust_size = 2,
  100. height_min = -255,
  101. height_max = -64,
  102. flags = "absheight",
  103. })
  104. minetest.register_ore({
  105. ore_type = "scatter",
  106. ore = "default:stone_with_mese",
  107. wherein = "default:stone",
  108. clust_scarcity = 14*14*14,
  109. clust_num_ores = 5,
  110. clust_size = 3,
  111. height_min = -31000,
  112. height_max = -256,
  113. flags = "absheight",
  114. })
  115. minetest.register_ore({
  116. ore_type = "scatter",
  117. ore = "default:mese",
  118. wherein = "default:stone",
  119. clust_scarcity = 36*36*36,
  120. clust_num_ores = 3,
  121. clust_size = 2,
  122. height_min = -31000,
  123. height_max = -1024,
  124. flags = "absheight",
  125. })
  126. minetest.register_ore({
  127. ore_type = "scatter",
  128. ore = "default:stone_with_gold",
  129. wherein = "default:stone",
  130. clust_scarcity = 15*15*15,
  131. clust_num_ores = 3,
  132. clust_size = 2,
  133. height_min = -255,
  134. height_max = -64,
  135. flags = "absheight",
  136. })
  137. minetest.register_ore({
  138. ore_type = "scatter",
  139. ore = "default:stone_with_gold",
  140. wherein = "default:stone",
  141. clust_scarcity = 13*13*13,
  142. clust_num_ores = 5,
  143. clust_size = 3,
  144. height_min = -31000,
  145. height_max = -256,
  146. flags = "absheight",
  147. })
  148. minetest.register_ore({
  149. ore_type = "scatter",
  150. ore = "default:stone_with_diamond",
  151. wherein = "default:stone",
  152. clust_scarcity = 17*17*17,
  153. clust_num_ores = 4,
  154. clust_size = 3,
  155. height_min = -255,
  156. height_max = -128,
  157. flags = "absheight",
  158. })
  159. minetest.register_ore({
  160. ore_type = "scatter",
  161. ore = "default:stone_with_diamond",
  162. wherein = "default:stone",
  163. clust_scarcity = 15*15*15,
  164. clust_num_ores = 4,
  165. clust_size = 3,
  166. height_min = -31000,
  167. height_max = -256,
  168. flags = "absheight",
  169. })
  170. minetest.register_ore({
  171. ore_type = "scatter",
  172. ore = "default:stone_with_copper",
  173. wherein = "default:stone",
  174. clust_scarcity = 12*12*12,
  175. clust_num_ores = 4,
  176. clust_size = 3,
  177. height_min = -63,
  178. height_max = -16,
  179. })
  180. minetest.register_ore({
  181. ore_type = "scatter",
  182. ore = "default:stone_with_copper",
  183. wherein = "default:stone",
  184. clust_scarcity = 9*9*9,
  185. clust_num_ores = 5,
  186. clust_size = 3,
  187. height_min = -31000,
  188. height_max = -64,
  189. flags = "absheight",
  190. })
  191. if minetest.setting_get("mg_name") == "indev" then
  192. -- Floatlands and high mountains springs
  193. minetest.register_ore({
  194. ore_type = "scatter",
  195. ore = "default:water_source",
  196. ore_param2 = 128,
  197. wherein = "default:stone",
  198. clust_scarcity = 40*40*40,
  199. clust_num_ores = 8,
  200. clust_size = 3,
  201. height_min = 100,
  202. height_max = 31000,
  203. })
  204. minetest.register_ore({
  205. ore_type = "scatter",
  206. ore = "default:lava_source",
  207. ore_param2 = 128,
  208. wherein = "default:stone",
  209. clust_scarcity = 50*50*50,
  210. clust_num_ores = 5,
  211. clust_size = 2,
  212. height_min = 10000,
  213. height_max = 31000,
  214. })
  215. minetest.register_ore({
  216. ore_type = "scatter",
  217. ore = "default:sand",
  218. wherein = "default:stone",
  219. clust_scarcity = 20*20*20,
  220. clust_num_ores = 5*5*3,
  221. clust_size = 5,
  222. height_min = 500,
  223. height_max = 31000,
  224. })
  225. -- Underground springs
  226. minetest.register_ore({
  227. ore_type = "scatter",
  228. ore = "default:water_source",
  229. ore_param2 = 128,
  230. wherein = "default:stone",
  231. clust_scarcity = 25*25*25,
  232. clust_num_ores = 8,
  233. clust_size = 3,
  234. height_min = -10000,
  235. height_max = -10,
  236. })
  237. minetest.register_ore({
  238. ore_type = "scatter",
  239. ore = "default:lava_source",
  240. ore_param2 = 128,
  241. wherein = "default:stone",
  242. clust_scarcity = 35*35*35,
  243. clust_num_ores = 5,
  244. clust_size = 2,
  245. height_min = -31000,
  246. height_max = -100,
  247. })
  248. end
  249. minetest.register_ore({
  250. ore_type = "scatter",
  251. ore = "default:clay",
  252. wherein = "default:sand",
  253. clust_scarcity = 15*15*15,
  254. clust_num_ores = 64,
  255. clust_size = 5,
  256. height_max = 0,
  257. height_min = -10,
  258. })
  259. function default.generate_ore(name, wherein, minp, maxp, seed, chunks_per_volume, chunk_size, ore_per_chunk, height_min, height_max)
  260. minetest.log('action', "WARNING: default.generate_ore is deprecated")
  261. if maxp.y < height_min or minp.y > height_max then
  262. return
  263. end
  264. local y_min = math.max(minp.y, height_min)
  265. local y_max = math.min(maxp.y, height_max)
  266. if chunk_size >= y_max - y_min + 1 then
  267. return
  268. end
  269. local volume = (maxp.x-minp.x+1)*(y_max-y_min+1)*(maxp.z-minp.z+1)
  270. local pr = PseudoRandom(seed)
  271. local num_chunks = math.floor(chunks_per_volume * volume)
  272. local inverse_chance = math.floor(chunk_size*chunk_size*chunk_size / ore_per_chunk)
  273. --print("generate_ore num_chunks: "..dump(num_chunks))
  274. for i=1,num_chunks do
  275. local y0 = pr:next(y_min, y_max-chunk_size+1)
  276. if y0 >= height_min and y0 <= height_max then
  277. local x0 = pr:next(minp.x, maxp.x-chunk_size+1)
  278. local z0 = pr:next(minp.z, maxp.z-chunk_size+1)
  279. local p0 = {x=x0, y=y0, z=z0}
  280. for x1=0,chunk_size-1 do
  281. for y1=0,chunk_size-1 do
  282. for z1=0,chunk_size-1 do
  283. if pr:next(1,inverse_chance) == 1 then
  284. local x2 = x0+x1
  285. local y2 = y0+y1
  286. local z2 = z0+z1
  287. local p2 = {x=x2, y=y2, z=z2}
  288. if minetest.get_node(p2).name == wherein then
  289. minetest.set_node(p2, {name=name})
  290. end
  291. end
  292. end
  293. end
  294. end
  295. end
  296. end
  297. --print("generate_ore done")
  298. end
  299. function default.make_papyrus(pos, size)
  300. for y=0,size-1 do
  301. local p = {x=pos.x, y=pos.y+y, z=pos.z}
  302. local nn = minetest.get_node(p).name
  303. if minetest.registered_nodes[nn] and
  304. minetest.registered_nodes[nn].buildable_to then
  305. minetest.set_node(p, {name="default:papyrus"})
  306. else
  307. return
  308. end
  309. end
  310. end
  311. function default.make_cactus(pos, size)
  312. for y=0,size-1 do
  313. local p = {x=pos.x, y=pos.y+y, z=pos.z}
  314. local nn = minetest.get_node(p).name
  315. if minetest.registered_nodes[nn] and
  316. minetest.registered_nodes[nn].buildable_to then
  317. minetest.set_node(p, {name="default:cactus"})
  318. else
  319. return
  320. end
  321. end
  322. end
  323. -- facedir: 0/1/2/3 (head node facedir value)
  324. -- length: length of rainbow tail
  325. function default.make_nyancat(pos, facedir, length)
  326. local tailvec = {x=0, y=0, z=0}
  327. if facedir == 0 then
  328. tailvec.z = 1
  329. elseif facedir == 1 then
  330. tailvec.x = 1
  331. elseif facedir == 2 then
  332. tailvec.z = -1
  333. elseif facedir == 3 then
  334. tailvec.x = -1
  335. else
  336. --print("default.make_nyancat(): Invalid facedir: "+dump(facedir))
  337. facedir = 0
  338. tailvec.z = 1
  339. end
  340. local p = {x=pos.x, y=pos.y, z=pos.z}
  341. minetest.set_node(p, {name="default:nyancat", param2=facedir})
  342. for i=1,length do
  343. p.x = p.x + tailvec.x
  344. p.z = p.z + tailvec.z
  345. minetest.set_node(p, {name="default:nyancat_rainbow", param2=facedir})
  346. end
  347. end
  348. function generate_nyancats(seed, minp, maxp)
  349. local height_min = -31000
  350. local height_max = -32
  351. if maxp.y < height_min or minp.y > height_max then
  352. return
  353. end
  354. local y_min = math.max(minp.y, height_min)
  355. local y_max = math.min(maxp.y, height_max)
  356. local volume = (maxp.x-minp.x+1)*(y_max-y_min+1)*(maxp.z-minp.z+1)
  357. local pr = PseudoRandom(seed + 9324342)
  358. local max_num_nyancats = math.floor(volume / (16*16*16))
  359. for i=1,max_num_nyancats do
  360. if pr:next(0, 1000) == 0 then
  361. local x0 = pr:next(minp.x, maxp.x)
  362. local y0 = pr:next(minp.y, maxp.y)
  363. local z0 = pr:next(minp.z, maxp.z)
  364. local p0 = {x=x0, y=y0, z=z0}
  365. default.make_nyancat(p0, pr:next(0,3), pr:next(3,15))
  366. end
  367. end
  368. end
  369. minetest.register_on_generated(function(minp, maxp, seed)
  370. if maxp.y >= 2 and minp.y <= 0 then
  371. -- Generate papyrus
  372. local perlin1 = minetest.get_perlin(354, 3, 0.7, 100)
  373. -- Assume X and Z lengths are equal
  374. local divlen = 8
  375. local divs = (maxp.x-minp.x)/divlen+1;
  376. for divx=0,divs-1 do
  377. for divz=0,divs-1 do
  378. local x0 = minp.x + math.floor((divx+0)*divlen)
  379. local z0 = minp.z + math.floor((divz+0)*divlen)
  380. local x1 = minp.x + math.floor((divx+1)*divlen)
  381. local z1 = minp.z + math.floor((divz+1)*divlen)
  382. -- Determine papyrus amount from perlin noise
  383. local papyrus_amount = math.floor(perlin1:get2d({x=x0, y=z0}) * 45 - 20)
  384. -- Find random positions for papyrus based on this random
  385. local pr = PseudoRandom(seed+1)
  386. for i=0,papyrus_amount do
  387. local x = pr:next(x0, x1)
  388. local z = pr:next(z0, z1)
  389. if minetest.get_node({x=x,y=1,z=z}).name == "default:dirt_with_grass" and
  390. minetest.find_node_near({x=x,y=1,z=z}, 1, "default:water_source") then
  391. default.make_papyrus({x=x,y=2,z=z}, pr:next(2, 4))
  392. end
  393. end
  394. end
  395. end
  396. -- Generate cactuses
  397. local perlin1 = minetest.get_perlin(230, 3, 0.6, 100)
  398. -- Assume X and Z lengths are equal
  399. local divlen = 16
  400. local divs = (maxp.x-minp.x)/divlen+1;
  401. for divx=0,divs-1 do
  402. for divz=0,divs-1 do
  403. local x0 = minp.x + math.floor((divx+0)*divlen)
  404. local z0 = minp.z + math.floor((divz+0)*divlen)
  405. local x1 = minp.x + math.floor((divx+1)*divlen)
  406. local z1 = minp.z + math.floor((divz+1)*divlen)
  407. -- Determine cactus amount from perlin noise
  408. local cactus_amount = math.floor(perlin1:get2d({x=x0, y=z0}) * 6 - 3)
  409. -- Find random positions for cactus based on this random
  410. local pr = PseudoRandom(seed+1)
  411. for i=0,cactus_amount do
  412. local x = pr:next(x0, x1)
  413. local z = pr:next(z0, z1)
  414. -- Find ground level (0...15)
  415. local ground_y = nil
  416. for y=30,0,-1 do
  417. if minetest.get_node({x=x,y=y,z=z}).name ~= "air" then
  418. ground_y = y
  419. break
  420. end
  421. end
  422. -- If desert sand, make cactus
  423. if ground_y and minetest.get_node({x=x,y=ground_y,z=z}).name == "default:desert_sand" then
  424. default.make_cactus({x=x,y=ground_y+1,z=z}, pr:next(3, 4))
  425. end
  426. end
  427. end
  428. end
  429. -- Generate grass
  430. local perlin1 = minetest.get_perlin(329, 3, 0.6, 100)
  431. -- Assume X and Z lengths are equal
  432. local divlen = 16
  433. local divs = (maxp.x-minp.x)/divlen+1;
  434. for divx=0,divs-1 do
  435. for divz=0,divs-1 do
  436. local x0 = minp.x + math.floor((divx+0)*divlen)
  437. local z0 = minp.z + math.floor((divz+0)*divlen)
  438. local x1 = minp.x + math.floor((divx+1)*divlen)
  439. local z1 = minp.z + math.floor((divz+1)*divlen)
  440. -- Determine grass amount from perlin noise
  441. local grass_amount = math.floor(perlin1:get2d({x=x0, y=z0}) ^ 3 * 9)
  442. -- Find random positions for grass based on this random
  443. local pr = PseudoRandom(seed+1)
  444. for i=0,grass_amount do
  445. local x = pr:next(x0, x1)
  446. local z = pr:next(z0, z1)
  447. -- Find ground level (0...15)
  448. local ground_y = nil
  449. for y=30,0,-1 do
  450. if minetest.get_node({x=x,y=y,z=z}).name ~= "air" then
  451. ground_y = y
  452. break
  453. end
  454. end
  455. if ground_y then
  456. local p = {x=x,y=ground_y+1,z=z}
  457. local nn = minetest.get_node(p).name
  458. -- Check if the node can be replaced
  459. if minetest.registered_nodes[nn] and
  460. minetest.registered_nodes[nn].buildable_to then
  461. nn = minetest.get_node({x=x,y=ground_y,z=z}).name
  462. -- If desert sand, add dry shrub
  463. if nn == "default:desert_sand" then
  464. minetest.set_node(p,{name="default:dry_shrub"})
  465. -- If dirt with grass, add grass
  466. elseif nn == "default:dirt_with_grass" then
  467. minetest.set_node(p,{name="default:grass_"..pr:next(1, 5)})
  468. end
  469. end
  470. end
  471. end
  472. end
  473. end
  474. end
  475. -- Generate nyan cats
  476. generate_nyancats(seed, minp, maxp)
  477. end)