init.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. caverealms = {} --create a container for functions and constants
  2. --grab a shorthand for the filepath of the mod
  3. local modpath = minetest.get_modpath(minetest.get_current_modname())
  4. --[[
  5. -- debug privileges
  6. minetest.register_on_joinplayer(function(player)
  7. local name = player:get_player_name()
  8. local privs = minetest.get_player_privs(name)
  9. privs.fly = true
  10. privs.fast = true
  11. privs.teleport = true
  12. privs.noclip = true
  13. minetest.set_player_privs(name, privs)
  14. local p = player:get_pos()
  15. if p.y > -100 then
  16. player:set_pos({x=0, y=-20000, z= 0})
  17. end
  18. end)
  19. --]]
  20. --load companion lua files
  21. dofile(modpath.."/config.lua") --configuration file; holds various constants
  22. dofile(modpath.."/crafting.lua") --crafting recipes
  23. dofile(modpath.."/nodes.lua") --node definitions
  24. dofile(modpath.."/functions.lua") --function definitions
  25. dofile(modpath.."/plants.lua")
  26. dofile(modpath.."/hotsprings.lua")
  27. if minetest.get_modpath("mobs_monster") then
  28. if caverealms.config.dm_spawn == true then
  29. dofile(modpath.."/dungeon_master.lua") --special DMs for DM's Lair biome
  30. end
  31. end
  32. -- Parameters
  33. local YMIN = caverealms.config.ymin -- Approximate realm limits.
  34. local YMAX = caverealms.config.ymax
  35. local TCAVE = caverealms.config.tcave --0.5 -- Cave threshold. 1 = small rare caves, 0.5 = 1/3rd ground volume, 0 = 1/2 ground volume
  36. local BLEND = 128 -- Cave blend distance near YMIN, YMAX
  37. local STAGCHA = caverealms.config.stagcha --0.002 --chance of stalagmites
  38. local STALCHA = caverealms.config.stalcha --0.003 --chance of stalactites
  39. local CRYSTAL = caverealms.config.crystal --0.0004 --chance of glow crystal formations
  40. local SALTCRYCHA = caverealms.config.salt_crystal --0.007 --chance of salt crystal cubes
  41. local GEMCHA = caverealms.config.gemcha --0.03 --chance of small glow gems
  42. local HOTSCHA = 0.009 --chance of hotsprings
  43. local MUSHCHA = caverealms.config.mushcha --0.04 --chance of mushrooms
  44. local MYCCHA = caverealms.config.myccha --0.03 --chance of mycena mushrooms
  45. local WORMCHA = caverealms.config.wormcha --0.03 --chance of glow worms
  46. local GIANTCHA = caverealms.config.giantcha --0.001 -- chance of giant mushrooms
  47. local ICICHA = caverealms.config.icicha --0.035 -- chance of icicles
  48. local FLACHA = caverealms.config.flacha --0.04 --chance of constant flames
  49. local DM_TOP = caverealms.config.dm_top -- -4000 --level at which Dungeon Master Realms start to appear
  50. local DM_BOT = caverealms.config.dm_bot -- -5000 --level at which "" ends
  51. local DEEP_CAVE = caverealms.config.deep_cave -- -7000 --level at which deep cave biomes take over
  52. -- 2D noise for biome
  53. local np_biome_evil = {
  54. offset = 0,
  55. scale = 1,
  56. spread = {x=200, y=200, z=200},
  57. seed = 9130,
  58. octaves = 3,
  59. persist = 0.5
  60. }
  61. local np_biome_wonder = {
  62. offset = 0,
  63. scale = 1,
  64. spread = {x=400, y=400, z=400},
  65. seed = 8943,
  66. octaves = 2,
  67. persist = 0.45
  68. }
  69. -- Stuff
  70. subterrain = {}
  71. -- On generated function
  72. minetest.register_on_generated(function(minp, maxp, seed)
  73. --if out of range of caverealms limits
  74. if minp.y > YMAX or maxp.y < YMIN then
  75. return --quit; otherwise, you'd have stalagmites all over the place
  76. end
  77. --easy reference to commonly used values
  78. local t1 = os.clock()
  79. local x1 = maxp.x
  80. local y1 = maxp.y
  81. local z1 = maxp.z
  82. local x0 = minp.x
  83. local y0 = minp.y
  84. local z0 = minp.z
  85. --print ("[caverealms] chunk minp ("..x0.." "..y0.." "..z0..")") --tell people you are generating a chunk
  86. local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
  87. local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
  88. local data = vm:get_data()
  89. --grab content IDs
  90. local c_air = minetest.get_content_id("air")
  91. local c_stone = minetest.get_content_id("default:stone")
  92. local c_desertstone = minetest.get_content_id("default:desert_stone")
  93. local c_sandstone = minetest.get_content_id("default:sandstone")
  94. local c_obsidian = minetest.get_content_id("default:obsidian")
  95. local c_sand = minetest.get_content_id("default:sand")
  96. local c_water = minetest.get_content_id("default:water_source")
  97. local c_lava = minetest.get_content_id("default:lava_source")
  98. local c_ice = minetest.get_content_id("default:ice")
  99. local c_ice = minetest.get_content_id("default:sand")
  100. local c_ice = minetest.get_content_id("default:silver_sand")
  101. local c_thinice = minetest.get_content_id("caverealms:thin_ice")
  102. local c_crystal = minetest.get_content_id("caverealms:glow_crystal")
  103. local c_gem = minetest.get_content_id("caverealms:glow_gem")
  104. local c_saltgem = minetest.get_content_id("caverealms:salt_gem")
  105. local c_spike = minetest.get_content_id("caverealms:spike")
  106. local c_moss = minetest.get_content_id("caverealms:stone_with_moss")
  107. local c_lichen = minetest.get_content_id("caverealms:stone_with_lichen")
  108. local c_algae = minetest.get_content_id("caverealms:stone_with_algae")
  109. local c_salt = minetest.get_content_id("caverealms:stone_with_salt")
  110. local c_hcobble = minetest.get_content_id("caverealms:hot_cobble")
  111. local c_gobsidian = minetest.get_content_id("caverealms:glow_obsidian")
  112. local c_gobsidian2 = minetest.get_content_id("caverealms:glow_obsidian_2")
  113. local c_coalblock = minetest.get_content_id("default:coalblock")
  114. local c_desand = minetest.get_content_id("default:desert_sand")
  115. local c_coaldust = minetest.get_content_id("caverealms:coal_dust")
  116. local c_fungus = minetest.get_content_id("caverealms:fungus")
  117. local c_mycena = minetest.get_content_id("caverealms:mycena")
  118. local c_worm_blue = minetest.get_content_id("caverealms:glow_worm")
  119. local c_worm_green = minetest.get_content_id("caverealms:glow_worm_green")
  120. local c_worm_red = minetest.get_content_id("caverealms:glow_worm_red")
  121. local c_fire_vine = minetest.get_content_id("caverealms:fire_vine")
  122. local c_iciu = minetest.get_content_id("caverealms:icicle_up")
  123. local c_icid = minetest.get_content_id("caverealms:icicle_down")
  124. local c_flame = minetest.get_content_id("caverealms:constant_flame")
  125. local c_bflame = minetest.get_content_id("caverealms:constant_flame_blue")
  126. local c_firefly = minetest.get_content_id("fireflies:firefly")
  127. local c_bluefly = minetest.get_content_id("caverealms:butterfly_blue")
  128. -- crystals
  129. local c_crystore = minetest.get_content_id("caverealms:glow_ore")
  130. local c_emerald = minetest.get_content_id("caverealms:glow_emerald")
  131. local c_emore = minetest.get_content_id("caverealms:glow_emerald_ore")
  132. local c_mesecry = minetest.get_content_id("caverealms:glow_mese")
  133. local c_meseore = minetest.get_content_id("default:stone_with_mese")
  134. local c_ruby = minetest.get_content_id("caverealms:glow_ruby")
  135. local c_rubore = minetest.get_content_id("caverealms:glow_ruby_ore")
  136. local c_citrine = minetest.get_content_id("caverealms:glow_citrine")
  137. local c_citore = minetest.get_content_id("caverealms:glow_citrine_ore")
  138. local c_ameth = minetest.get_content_id("caverealms:glow_amethyst")
  139. local c_amethore = minetest.get_content_id("caverealms:glow_amethyst_ore")
  140. local c_hotspring = minetest.get_content_id("caverealms:hotspring_water_source")
  141. local stone_nodes = {
  142. [c_stone] = 1,
  143. [c_desertstone] = 1,
  144. [c_sandstone] = 1,
  145. [c_coalblock] = 1,
  146. [c_sand] = 1,
  147. [c_desand] = 1,
  148. [c_obsidian] = 1,
  149. }
  150. if nil ~= minetest.get_modpath("geology") then
  151. stone_nodes[minetest.get_content_id("geology:gneiss")] = 1
  152. stone_nodes[minetest.get_content_id("geology:slate")] = 1
  153. stone_nodes[minetest.get_content_id("geology:jade")] = 1
  154. stone_nodes[minetest.get_content_id("geology:granite")] = 1
  155. stone_nodes[minetest.get_content_id("geology:marble")] = 1
  156. stone_nodes[minetest.get_content_id("geology:basalt")] = 1
  157. stone_nodes[minetest.get_content_id("geology:chalk")] = 1
  158. stone_nodes[minetest.get_content_id("geology:ors")] = 1
  159. stone_nodes[minetest.get_content_id("geology:serpentine")] = 1
  160. stone_nodes[minetest.get_content_id("geology:shale")] = 1
  161. stone_nodes[minetest.get_content_id("geology:schist")] = 1
  162. stone_nodes[minetest.get_content_id("geology:anthracite")] = 1
  163. end
  164. --mandatory values
  165. local sidelen = x1 - x0 + 1 --length of a mapblock
  166. local chulens = {x=sidelen, y=sidelen, z=sidelen} --table of chunk edges
  167. local chulens2D = {x=sidelen, y=sidelen, z=1}
  168. local minposxyz = {x=x0, y=y0, z=z0} --bottom corner
  169. local minposxz = {x=x0, y=z0} --2D bottom corner
  170. local nvals_biome_e = minetest.get_perlin_map(np_biome_evil, chulens2D):get2dMap_flat({x=x0+150, y=z0+50}) --2D noise for biomes (will be 3D humidity/temp later)
  171. local nvals_biome_w = minetest.get_perlin_map(np_biome_wonder, chulens2D):get2dMap_flat({x=x0+150, y=z0+50}) --2D noise for biomes (will be 3D humidity/temp later)
  172. local nixyz = 1 --3D node index
  173. local nixz = 1 --2D node index
  174. local nixyz2 = 1 --second 3D index for second loop
  175. for z = z0, z1 do -- for each xy plane progressing northwards
  176. --increment indices
  177. nixyz = nixyz + 1
  178. --decoration loop
  179. for y = y0, y1 do -- for each x row progressing upwards
  180. local is_deep = false
  181. if y < DEEP_CAVE then
  182. is_deep = true
  183. end
  184. local vi = area:index(x0, y, z)
  185. for x = x0, x1 do -- for each node do
  186. local ai = area:index(x,y+1,z) --above index
  187. local bi = area:index(x,y-1,z) --below index
  188. local mode = 0 -- nothing, 1 = ground, 2 = ceiling
  189. if data[vi] == c_air then
  190. if stone_nodes[data[bi]] ~= nil then --ground
  191. mode = 1
  192. elseif stone_nodes[data[bi]] ~= nil and y < y1 then -- ceiling
  193. mode = 2
  194. end
  195. end
  196. if mode > 0 then
  197. local a2i = area:index(x,y+2,z) --above index
  198. --determine biome
  199. local biome = 0 --preliminary declaration
  200. local n_biome_e = nvals_biome_e[nixz] --make an easier reference to the noise
  201. local n_biome_w = nvals_biome_w[nixz] --make an easier reference to the noise
  202. local n_biome = (n_biome_e + n_biome_w) / 2
  203. local floor = c_hcobble
  204. local floor_depth = 1
  205. local worms = {}
  206. local worm_max_len = 1
  207. local no_mites = false
  208. local no_tites = false
  209. local decos = {}
  210. local decos2 = {}
  211. local deco_mul = 1
  212. local wiggle = (math.random() - 0.5) / 20
  213. n_biome_e = n_biome_e + wiggle
  214. n_biome_w = n_biome_w + wiggle
  215. if n_biome_e < -0.33 then
  216. if n_biome_w < -0.33 then -- algae
  217. floor = c_algae
  218. worms = {c_worm_green}
  219. worm_max_len = 3
  220. decos = {c_mycena}
  221. if mode == 1 and data[ai] == c_air and math.random() < 0.03 then
  222. data[ai] = c_firefly
  223. end
  224. elseif n_biome_w < 0.33 then -- moss
  225. floor = c_moss
  226. worms = {c_worm_green, c_worm_blue}
  227. worm_max_len = 3
  228. decos = {c_mycena}
  229. deco_mul = 2.0
  230. if mode == 1 and data[ai] == c_air and math.random() < 0.001 then
  231. caverealms:grow_green_mushroom(x,y-1,z, area, data)
  232. end
  233. else -- lichen
  234. floor = c_lichen
  235. worms = {c_worm_blue}
  236. worm_max_len = 3
  237. decos = {c_mycena, c_fungus, c_fungus}
  238. deco_mul = 3.3
  239. if mode == 1 and data[ai] == c_air and math.random() < 0.003 then
  240. data[ai] = c_bluefly
  241. end
  242. end
  243. elseif n_biome_e < 0.33 then
  244. if n_biome_w < -0.33 then -- desert
  245. if math.random() < 0.05 then
  246. floor = c_coalblock
  247. elseif math.random() < 0.15 then
  248. floor = c_coaldust
  249. else
  250. floor = c_desand
  251. end
  252. floor_depth = 2
  253. worms = {c_worm_red}
  254. worm_max_len = 1
  255. decos = {c_flame, c_spike}
  256. elseif n_biome_w < 0.33 then -- salt
  257. floor = c_salt
  258. floor_depth = 2
  259. worms = {c_icid}
  260. worm_max_len = 1
  261. no_mites = true
  262. decos = {c_saltgem}
  263. else -- glacial
  264. floor = c_thinice
  265. floor_depth = 2
  266. worms = {c_icid}
  267. worm_max_len = 1
  268. decos = {c_gem}
  269. end
  270. else
  271. if n_biome_w < -0.33 then -- hotspring
  272. floor = c_hcobble
  273. worms = {c_icid}
  274. worm_max_len = 1
  275. if mode == 1 and math.random() < 0.005 then
  276. caverealms:spawn_hotspring(x,y,z, area, data, math.random(4) + 2)
  277. end
  278. decos = {c_fire_vine}
  279. deco_mul = 0.7
  280. elseif n_biome_w < 0.33 then -- dungeon
  281. if math.random() < 0.5 then
  282. floor = c_gobsidian
  283. else
  284. floor = c_gobsidian2
  285. end
  286. worms = {c_worm_red}
  287. worm_max_len = 4
  288. decos = {c_flame, c_flame, c_fire_vine}
  289. else -- deep glacial
  290. floor = c_ice
  291. floor_depth = 3
  292. worms = {c_icid}
  293. worm_max_len = 1
  294. decos = {c_bflame}
  295. end
  296. end
  297. -- place floor
  298. if mode == 1 then --ground
  299. for i = 1,floor_depth do
  300. local ii = area:index(x,y-i,z)
  301. if stone_nodes[data[bi]] ~= nil then
  302. data[ii] = floor
  303. end
  304. end
  305. -- decorations
  306. if math.random() < ICICHA * deco_mul and data[bi] ~= c_hotspring then
  307. data[vi] = decos[math.random(1, #decos)]
  308. end
  309. -- salt crystals
  310. if floor == c_salt and math.random() < SALTCRYCHA then
  311. caverealms:salt_stalagmite(x,y-1,z, area, data)
  312. end
  313. -- stone stalagmites
  314. if math.random() < STAGCHA then
  315. caverealms:stalagmite(x,y,z, area, data)
  316. end
  317. -- crystal stalagmites
  318. if not no_mites and math.random() < CRYSTAL then
  319. local ore
  320. local cry
  321. if n_biome_e < 0 then -- non-evil
  322. if n_biome_w < -0.33 then
  323. ore = c_crystore
  324. cry = c_crystal
  325. elseif n_biome_w < 0.33 then
  326. ore = c_emore
  327. cry = c_emerald
  328. else
  329. ore = c_amethore
  330. cry = c_ameth
  331. end
  332. elseif n_biome_e < 0.4 then -- moderately evil
  333. if n_biome_w < 0 then
  334. ore = c_meseore
  335. cry = c_mesecry
  336. else
  337. ore = c_citore
  338. cry = c_citrine
  339. end
  340. else -- very evil
  341. ore = c_rubore
  342. cry = c_ruby
  343. end
  344. local base = floor
  345. caverealms:crystal_stalagmite(x,y-1,z, area, data, ore, cry, base)
  346. end
  347. if n_biome_w > 0.5 and n_biome_e < -0.33 and math.random() < GIANTCHA then --giant mushrooms
  348. caverealms:giant_shroom(x, y, z, area, data)
  349. end
  350. elseif mode == 2 then -- place ceiling
  351. if math.random() < ICICHA then
  352. local worm = worms[math.random(1,#worms)]
  353. local wdepth = math.random(1, worm_max_len)
  354. for i = 0,wdepth-1 do
  355. local ii = area:index(x,y-i,z)
  356. if data[ii] == c_air then
  357. data[ii] = worm
  358. end
  359. end
  360. end
  361. -- stalactites
  362. if not no_tites and math.random() < CRYSTAL then
  363. local ore
  364. local cry
  365. if n_biome_e < 0 then -- non-evil
  366. if n_biome_w < -0.33 then
  367. ore = c_crystore
  368. cry = c_crystal
  369. elseif n_biome_w < 0.33 then
  370. ore = c_emore
  371. cry = c_emerald
  372. else
  373. ore = c_amethore
  374. cry = c_ameth
  375. end
  376. elseif n_biome_e < 0.4 then -- moderately evil
  377. if n_biome_w < 0 then
  378. ore = c_meseore
  379. cry = c_mesecry
  380. else
  381. ore = c_citore
  382. cry = c_citrine
  383. end
  384. else -- very evil
  385. ore = c_rubore
  386. cry = c_ruby
  387. end
  388. local base = c_stone
  389. caverealms:crystal_stalactite(x,y,z, area, data, ore, cry, base)
  390. end
  391. end
  392. end
  393. nixyz2 = nixyz2 + 1
  394. nixz = nixz + 1
  395. vi = vi + 1
  396. end
  397. nixz = nixz - sidelen --shift the 2D index back
  398. end
  399. nixz = nixz + sidelen --shift the 2D index up a layer
  400. end
  401. --send data back to voxelmanip
  402. vm:set_data(data)
  403. --calc lighting
  404. vm:set_lighting({day=0, night=0})
  405. vm:calc_lighting()
  406. --write it to world
  407. vm:write_to_map(data)
  408. --local chugent = math.ceil((os.clock() - t1) * 1000) --grab how long it took
  409. --print ("[caverealms] "..chugent.." ms") --tell people how long
  410. end)
  411. print("[caverealms] loaded!")