mapgen.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. --Makes a stratus of rocks
  2. --name of the rock to generate
  3. --c_wherein id of node to replace, for example minetest.get_content_id("default:stone")
  4. --minp, maxp the corners of the map to be generated
  5. --seed random seed
  6. --stratus_chance inverse probability in a given radius 1:2, 1:3 etc
  7. --radius horizontal radius of the stratus
  8. --radius_y vertical radius of the stratus
  9. --deep how deep can be from the ground
  10. local function generate_stratus(data, area, name, c_wherein, ca_ceilin, minp, maxp, seed, stratus_chance, radius, radius_y, deep, y_min, y_max)
  11. if maxp.y < y_min
  12. or minp.y > y_max then
  13. return
  14. end
  15. local c_node = minetest.get_content_id(name)
  16. -- it will be only generate a stratus for every 100 m of area
  17. local stratus_per_volume=1
  18. local area_size = 45
  19. local y_min = math.max(minp.y, y_min)
  20. local y_max = math.min(maxp.y, y_max)
  21. local volume = ((maxp.x-minp.x+1)/area_size)*((y_max-y_min+1)/area_size)*((maxp.z-minp.z+1)/area_size)
  22. local pr = PseudoRandom(seed)
  23. local blocks = math.floor(stratus_per_volume*volume)
  24. if blocks == 0 then
  25. blocks = 1
  26. end
  27. for i = 1,blocks do
  28. if pr:next(1,stratus_chance) == 1 then
  29. -- TODO deep
  30. local y0=y_max-radius_y+1
  31. if y0 < y_min then
  32. y0=y_min
  33. else
  34. y0=pr:next(y_min, y0)
  35. end
  36. local x0 = maxp.x-radius+1
  37. if x0 < minp.x then
  38. x0 = minp.x
  39. else
  40. x0 = pr:next(minp.x, x0)
  41. end
  42. local z0 = maxp.z-radius+1
  43. if z0 < minp.z then
  44. x0 = minp.z
  45. else
  46. z0 = pr:next(minp.z, z0)
  47. end
  48. local n = data[area:index(x0, y0, z0)]
  49. local i = 0
  50. --print(" upper node "..n)
  51. local x
  52. for _,v in ipairs(ca_ceilin) do
  53. if n == v then
  54. x = true
  55. break
  56. end
  57. end
  58. if x then
  59. -- search for the node to replace
  60. --print(" Searching nodes to replace from "..dump(y0-1).." to "..dump(y_min))
  61. for y1 = y0-1,y_min,-1 do
  62. if data[area:index(x0, y1, z0)] == c_wherein then
  63. y0 = y1-deep
  64. if y0 < y_min then
  65. y0 = y_min
  66. end
  67. break
  68. end
  69. end
  70. local rx=pr:next(radius/2,radius)+1
  71. local rz=pr:next(radius/2,radius)+1
  72. local ry=pr:next(radius_y/2,radius_y)+1
  73. --print(" area of generation ("..dump(rx)..","..dump(rz)..","..dump(ry)..")")
  74. for x1=0,rx do
  75. rz = rz + 3 - pr:next(1,6)
  76. if rz < 1 then
  77. rz = 1
  78. end
  79. for z1=pr:next(1,3),rz do
  80. local ry0 = ry + pr:next(1,3)
  81. for y1 = pr:next(1,3),ry0 do
  82. local p2 = area:index(x0+x1, y0+y1, z0+z1)
  83. if data[p2] == c_wherein then
  84. data[p2] = c_node
  85. i = i +1
  86. end
  87. end
  88. end
  89. end
  90. end
  91. end
  92. end
  93. --print("generate_ore done")
  94. end
  95. local function generate_claylike(data, varea, name, minp, maxp, seed, chance, minh, maxh, dirt)
  96. if maxp.y >= maxh+1 and minp.y <= minh-1 then
  97. local c_ore = minetest.get_content_id(name)
  98. local c_sand = minetest.get_content_id("default:sand")
  99. local c_dirt = minetest.get_content_id("default:dirt")
  100. local c_lawn = minetest.get_content_id("default:dirt_with_grass")
  101. local c_water = minetest.get_content_id("default:water_source")
  102. local c_air = minetest.get_content_id("air")
  103. local pr = PseudoRandom(seed)
  104. local divlen = 4
  105. local divs = (maxp.x-minp.x)/divlen+1;
  106. for yy=minh,maxh do
  107. local x = pr:next(1,chance)
  108. if x == 1 then
  109. for divx=0+1,divs-1-1 do
  110. for divz=0+1,divs-1-1 do
  111. local cx = minp.x + math.floor((divx+0.5)*divlen)
  112. local cz = minp.z + math.floor((divz+0.5)*divlen)
  113. local up = data[varea:index(cx,yy,cz)]
  114. local down = data[varea:index(cx,yy-1,cz)]
  115. if ( up == c_water or up == c_air ) and ( down == c_sand or (dirt == 1 and (down == c_dirt or down == c_lawn ))) then
  116. local is_shallow = true
  117. local num_water_around = 0
  118. if data[varea:index(cx-divlen*2,yy,cz)] == c_water then
  119. num_water_around = num_water_around + 1
  120. end
  121. if data[varea:index(cx+divlen*2,yy,cz)] == c_water then
  122. num_water_around = num_water_around + 1
  123. end
  124. if data[varea:index(cx,yy,cz-divlen*2)] == c_water then
  125. num_water_around = num_water_around + 1
  126. end
  127. if data[varea:index(cx,yy,cz+divlen*2)] == c_water then
  128. num_water_around = num_water_around + 1
  129. end
  130. if num_water_around >= 3 then
  131. is_shallow = false
  132. end
  133. if is_shallow then
  134. for x1=-divlen,divlen do
  135. for z1=-divlen,divlen do
  136. local p={x=cx+x1,y=yy-1,z=cz+z1}
  137. down = data[varea:indexp(p)]
  138. if down == c_sand or (dirt == 1 and (down == c_dirt or down == c_lawn)) then
  139. data[varea:indexp(p)] = c_ore
  140. end
  141. end
  142. end
  143. end
  144. end
  145. end
  146. end
  147. end
  148. end
  149. end
  150. end
  151. local seed = minetest.get_mapgen_setting("seed")
  152. -- Generate desert stone with iron in derset.
  153. minetest.register_ore({
  154. ore_type = "scatter",
  155. ore = "darkage:desert_stone_with_iron",
  156. wherein = "default:desert_stone",
  157. clust_scarcity = 12 * 12 * 12,
  158. clust_num_ores = 3,
  159. clust_size = 2,
  160. y_min = -1,
  161. y_max = 200,
  162. })
  163. -- Generate chalk inside mountains
  164. minetest.register_ore({
  165. ore_type = "sheet",
  166. ore = "darkage:chalk",
  167. wherein = {"default:stone"},
  168. column_height_max = 30,
  169. column_height_min = 20,
  170. y_min = -20,
  171. y_max = 100,
  172. noise_threshold = 0.45,
  173. noise_params = {
  174. offset = 0.35,
  175. scale = 0.2,
  176. spread = {x = 30, y = 30, z = 30},
  177. octaves = 1,
  178. persistence = 0.6
  179. },
  180. })
  181. minetest.register_ore({
  182. ore_type = "sheet",
  183. ore = "darkage:tuff",
  184. wherein = {"default:stone", "default:dirt", "default:gravel", "default:stone_with_coal"},
  185. column_height_max = 20,
  186. column_height_min = 15,
  187. y_min = -400,
  188. y_max = -20,
  189. noise_threshold = 0.5,
  190. noise_params = {
  191. offset = 0.35,
  192. scale = 0.19,
  193. seed = seed+12,
  194. spread = {x = 45, y = 45, z = 45},
  195. octaves = 1,
  196. persistence = 0.6
  197. },
  198. })
  199. minetest.register_ore({
  200. ore_type = "sheet",
  201. ore = "darkage:rhyolitic_tuff",
  202. wherein = {"default:stone", "default:dirt", "default:gravel", "default:stone_with_coal"},
  203. column_height_max = 20,
  204. column_height_min = 15,
  205. y_min = -2000,
  206. y_max = -20,
  207. noise_threshold = 0.53,
  208. noise_params = {
  209. offset = 0.35,
  210. scale = 0.2,
  211. seed = seed+13,
  212. spread = {x = 100, y = 100, z = 100},
  213. octaves = 1,
  214. persistence = 0.6
  215. },
  216. })
  217. -- Generate strati
  218. local function generate_strati(minp, maxp, seed)
  219. local t1 = os.clock()
  220. local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
  221. local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
  222. local data = vm:get_data()
  223. local c_air = minetest.get_content_id("air")
  224. local c_stone = minetest.get_content_id("default:stone")
  225. local c_water = minetest.get_content_id("default:water_source")
  226. generate_claylike(data, area, "darkage:mud", minp, maxp, seed+1, 4, 0, 2, 0)
  227. generate_claylike(data, area, "darkage:silt", minp, maxp, seed+2, 4, -1, 1, 1)
  228. -- TODO: Maybe realize the following stuff with register ore. somehow.
  229. generate_stratus(data, area, "darkage:ors",
  230. c_stone,
  231. {c_stone, c_air, c_water},
  232. minp, maxp, seed+4, 4, 25, 7, 50, -200, 500)
  233. generate_stratus(data, area, "darkage:shale",
  234. c_stone,
  235. {c_stone, c_air},
  236. minp, maxp, seed+5, 4, 23, 7, 50, -50, 20)
  237. generate_stratus(data, area, "darkage:slate",
  238. c_stone,
  239. {c_stone, c_air},
  240. minp, maxp, seed+6, 6, 23, 5, 50, -500, 0)
  241. generate_stratus(data, area, "darkage:schist",
  242. c_stone,
  243. {c_stone, c_air},
  244. minp, maxp, seed+7, 6, 19, 6, 50, -31000, -10)
  245. generate_stratus(data, area, "darkage:basalt",
  246. c_stone,
  247. {c_stone, c_air},
  248. minp, maxp, seed+8, 5, 20, 5, 20, -31000, -50)
  249. generate_stratus(data, area, "darkage:gneiss",
  250. c_stone,
  251. {c_stone, c_air},
  252. minp, maxp, seed+11, 4, 15, 5, 50, -31000, -250)
  253. vm:set_data(data)
  254. vm:write_to_map()
  255. end
  256. minetest.register_on_generated(generate_strati)