mapgen.lua 8.4 KB

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