init.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. cw = cw or {}
  2. cw.modpath = minetest.get_modpath("cw")
  3. cw.worldpath = minetest.get_worldpath()
  4. if not cw.jungletree_registered then
  5. local _ = {name = "air", prob = 0}
  6. local L = {name = "default:jungleleaves", prob = 255}
  7. local N = {name = "default:jungleleaves", prob = 223}
  8. local M = {name = "default:jungleleaves", prob = 191}
  9. local B = {name = "default:jungletree", prob = 255, force_place = true}
  10. local Y = {name = "default:jungletree", prob = 191, force_place = true}
  11. local U = {name = "default:jungletree", prob = 127, force_place = true}
  12. local I = {name = "default:jungletree", prob = 255}
  13. do
  14. local jungletree_data = {
  15. size = {x = 5, y = 17, z = 5},
  16. data = {
  17. _, _, _, _, _,
  18. _, _, _, _, _,
  19. _, _, _, _, _,
  20. _, _, _, _, _,
  21. _, _, _, _, _,
  22. _, _, _, _, _,
  23. _, _, _, _, _,
  24. _, _, _, _, _,
  25. _, _, _, _, _,
  26. _, _, _, _, _,
  27. N, L, N, _, _,
  28. _, _, N, L, N,
  29. _, _, _, _, _,
  30. _, _, _, _, _,
  31. M, N, N, N, M,
  32. M, N, N, N, M,
  33. _, _, _, _, _,
  34. _, _, B, _, _,
  35. _, _, B, _, _,
  36. _, _, U, _, _,
  37. _, _, _, _, _,
  38. _, _, _, _, _,
  39. _, _, _, _, _,
  40. _, _, _, _, _,
  41. _, _, _, _, _,
  42. _, _, _, _, _,
  43. _, _, _, _, _,
  44. L, B, L, _, _,
  45. _, _, L, B, L,
  46. _, _, _, _, _,
  47. _, _, _, _, _,
  48. N, B, L, B, N,
  49. N, L, L, L, N,
  50. _, N, N, N, _,
  51. _, B, B, B, _,
  52. _, B, B, B, _,
  53. _, U, B, U, _,
  54. _, _, B, _, _,
  55. _, _, B, _, _,
  56. _, _, B, _, _,
  57. _, _, B, _, _,
  58. _, _, B, _, _,
  59. _, _, B, L, N,
  60. N, L, B, _, _,
  61. N, L, B, _, _,
  62. _, _, B, L, N,
  63. _, _, B, L, N,
  64. _, _, B, _, _,
  65. N, L, L, L, N,
  66. N, L, L, L, N,
  67. _, N, L, N, _,
  68. _, _, B, _, _,
  69. _, _, B, _, _,
  70. _, _, U, _, _,
  71. _, _, _, _, _,
  72. _, _, _, _, _,
  73. _, _, _, _, _,
  74. _, _, _, _, _,
  75. _, _, _, _, _,
  76. _, _, L, B, L,
  77. L, B, L, _, _,
  78. _, _, _, _, _,
  79. _, _, _, _, _,
  80. _, _, L, B, L,
  81. _, _, _, _, _,
  82. N, B, L, B, N,
  83. N, L, L, L, N,
  84. _, N, N, N, _,
  85. _, _, _, _, _,
  86. _, _, _, _, _,
  87. _, _, _, _, _,
  88. _, _, _, _, _,
  89. _, _, _, _, _,
  90. _, _, _, _, _,
  91. _, _, _, _, _,
  92. _, _, _, _, _,
  93. _, _, N, L, N,
  94. N, L, N, _, _,
  95. _, _, _, _, _,
  96. _, _, _, _, _,
  97. _, _, N, L, N,
  98. _, _, _, _, _,
  99. M, N, N, N, M,
  100. M, N, N, N, M,
  101. _, _, _, _, _,
  102. },
  103. yslice_prob = {
  104. {ypos=6, prob=191},
  105. {ypos=7, prob=191},
  106. {ypos=8, prob=191},
  107. {ypos=9, prob=191},
  108. {ypos=10, prob=191},
  109. },
  110. }
  111. local data = minetest.serialize_schematic(jungletree_data, "mts", {})
  112. local file = io.open(cw.worldpath .. "/cw_jungletree_base.mts", "w")
  113. file:write(data)
  114. file:close()
  115. end
  116. do
  117. -- Main difference is the trunk base doesn't have extra nodes around it.
  118. local jungletree_data = {
  119. size = {x = 5, y = 17, z = 5},
  120. data = {
  121. _, _, _, _, _,
  122. _, _, _, _, _,
  123. _, _, _, _, _,
  124. _, _, _, _, _,
  125. _, _, _, _, _,
  126. _, _, _, _, _,
  127. _, _, _, _, _,
  128. _, _, _, _, _,
  129. _, _, _, _, _,
  130. _, _, _, _, _,
  131. N, L, N, _, _,
  132. _, _, N, L, N,
  133. _, _, _, _, _,
  134. _, _, _, _, _,
  135. M, N, N, N, M,
  136. M, N, N, N, M,
  137. _, _, _, _, _,
  138. _, _, _, _, _,
  139. _, _, _, _, _,
  140. _, _, _, _, _,
  141. _, _, _, _, _,
  142. _, _, _, _, _,
  143. _, _, _, _, _,
  144. _, _, _, _, _,
  145. _, _, _, _, _,
  146. _, _, _, _, _,
  147. _, _, _, _, _,
  148. L, B, L, _, _,
  149. _, _, L, B, L,
  150. _, _, _, _, _,
  151. _, _, _, _, _,
  152. N, B, L, B, N,
  153. N, L, L, L, N,
  154. _, N, N, N, _,
  155. _, _, B, _, _,
  156. _, _, B, _, _,
  157. _, _, B, _, _,
  158. _, _, B, _, _,
  159. _, _, B, _, _,
  160. _, _, B, _, _,
  161. _, _, B, _, _,
  162. _, _, B, _, _,
  163. _, _, B, L, N,
  164. N, L, B, _, _,
  165. N, L, B, _, _,
  166. _, _, B, L, N,
  167. _, _, B, L, N,
  168. _, _, B, _, _,
  169. N, L, L, L, N,
  170. N, L, L, L, N,
  171. _, N, L, N, _,
  172. _, _, _, _, _,
  173. _, _, _, _, _,
  174. _, _, _, _, _,
  175. _, _, _, _, _,
  176. _, _, _, _, _,
  177. _, _, _, _, _,
  178. _, _, _, _, _,
  179. _, _, _, _, _,
  180. _, _, L, B, L,
  181. L, B, L, _, _,
  182. _, _, _, _, _,
  183. _, _, _, _, _,
  184. _, _, L, B, L,
  185. _, _, _, _, _,
  186. N, B, L, B, N,
  187. N, L, L, L, N,
  188. _, N, N, N, _,
  189. _, _, _, _, _,
  190. _, _, _, _, _,
  191. _, _, _, _, _,
  192. _, _, _, _, _,
  193. _, _, _, _, _,
  194. _, _, _, _, _,
  195. _, _, _, _, _,
  196. _, _, _, _, _,
  197. _, _, N, L, N,
  198. N, L, N, _, _,
  199. _, _, _, _, _,
  200. _, _, _, _, _,
  201. _, _, N, L, N,
  202. _, _, _, _, _,
  203. M, N, N, N, M,
  204. M, N, N, N, M,
  205. _, _, _, _, _,
  206. },
  207. yslice_prob = {
  208. {ypos=6, prob=191},
  209. {ypos=7, prob=191},
  210. {ypos=8, prob=191},
  211. {ypos=9, prob=191},
  212. {ypos=10, prob=191},
  213. },
  214. }
  215. local data = minetest.serialize_schematic(jungletree_data, "mts", {})
  216. local file = io.open(cw.worldpath .. "/cw_jungletree_top.mts", "w")
  217. file:write(data)
  218. file:close()
  219. end
  220. cw.jungletree_registered = true
  221. end
  222. -- A Channelwood-like realm. Endless, shallow water in all directions, with
  223. -- trees growing out of the ocean. Trees are huge and extremely tall. Water is
  224. -- dangerious, filled with flesh-eating fish! Trees do not burn (too wet).
  225. -- Register deadly water.
  226. if not cw.registered then
  227. -- Basically just a copy of regular water, with damage_per_second.
  228. local sdef = table.copy(minetest.registered_nodes["default:water_source"])
  229. local fdef = table.copy(minetest.registered_nodes["default:water_flowing"])
  230. sdef.damage_per_second = 1
  231. fdef.damage_per_second = 1
  232. sdef.liquid_alternative_flowing = "cw:water_flowing"
  233. sdef.liquid_alternative_source = "cw:water_source"
  234. fdef.liquid_alternative_flowing = "cw:water_flowing"
  235. fdef.liquid_alternative_source = "cw:water_source"
  236. minetest.register_node("cw:water_source", sdef)
  237. minetest.register_node("cw:water_flowing", fdef)
  238. end
  239. cw.REALM_START = 3050
  240. cw.BEDROCK_DEPTH = 4
  241. cw.OCEAN_DEPTH = 16
  242. cw.GROUND_DEPTH = 11
  243. cw.GROUND_HEIGHT_VARIATION = 4
  244. -- Controls land height.
  245. cw.noise1param2d = {
  246. offset = 0,
  247. scale = 1,
  248. spread = {x=128, y=128, z=128},
  249. seed = 3717,
  250. octaves = 5,
  251. persist = 0.5,
  252. lacunarity = 2,
  253. }
  254. cw.noise2param2d = {
  255. offset = 0,
  256. scale = 1,
  257. spread = {x=32, y=32, z=32},
  258. seed = 5817,
  259. octaves = 4,
  260. persist = 0.8,
  261. lacunarity = 2,
  262. }
  263. -- Content IDs used with the voxel manipulator.
  264. local c_air = minetest.get_content_id("air")
  265. local c_ignore = minetest.get_content_id("ignore")
  266. local c_stone = minetest.get_content_id("default:stone")
  267. local c_bedrock = minetest.get_content_id("bedrock:bedrock")
  268. local c_water = minetest.get_content_id("cw:water_source")
  269. local c_dirt = minetest.get_content_id("darkage:darkdirt")
  270. local c_silt = minetest.get_content_id("darkage:silt")
  271. local c_mud = minetest.get_content_id("darkage:mud")
  272. local c_clay = minetest.get_content_id("default:clay")
  273. -- Externally located tables for performance.
  274. local data = {}
  275. local noisemap1 = {}
  276. local noisemap2 = {}
  277. local JUNGLETREE_REPLACEMENTS = {
  278. ["default:jungletree"] = "basictrees:jungletree_cube",
  279. ["default:jungleleaves"] = "basictrees:jungletree_leaves2",
  280. }
  281. local RANDPOS = {
  282. {x=1, y=0, z=0},
  283. {x=-1, y=0, z=0},
  284. {x=0, y=0, z=1},
  285. {x=0, y=0, z=-1},
  286. -- 3 times to increase probability.
  287. {x=0, y=0, z=0},
  288. {x=0, y=0, z=0},
  289. {x=0, y=0, z=0},
  290. }
  291. cw.generate_realm = function(minp, maxp, seed)
  292. local nstart = cw.REALM_START
  293. -- Don't run for out-of-bounds mapchunks.
  294. local nfinish = nstart + 100
  295. if minp.y > nfinish or maxp.y < nstart then
  296. return
  297. end
  298. -- Grab the voxel manipulator.
  299. -- Read current map data.
  300. local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
  301. vm:get_data(data)
  302. local area = VoxelArea:new {MinEdge=emin, MaxEdge=emax}
  303. local pr = PseudoRandom(seed + 381)
  304. local x1 = maxp.x
  305. local y1 = maxp.y
  306. local z1 = maxp.z
  307. local x0 = minp.x
  308. local y0 = minp.y
  309. local z0 = minp.z
  310. -- Compute side lengths.
  311. local side_len_x = ((x1-x0)+1)
  312. local side_len_z = ((z1-z0)+1)
  313. local sides2D = {x=side_len_x, y=side_len_z}
  314. local bp2d = {x=x0, y=z0}
  315. -- Get noisemaps.
  316. local perlin1 = minetest.get_perlin_map(cw.noise1param2d, sides2D)
  317. perlin1:get2dMap_flat(bp2d, noisemap1)
  318. local perlin2 = minetest.get_perlin_map(cw.noise2param2d, sides2D)
  319. perlin2:get2dMap_flat(bp2d, noisemap2)
  320. -- Localize commonly used functions.
  321. local floor = math.floor
  322. local ceil = math.ceil
  323. local abs = math.abs
  324. local od = cw.OCEAN_DEPTH
  325. local bd = cw.BEDROCK_DEPTH
  326. local gd = cw.GROUND_DEPTH
  327. local ghv = cw.GROUND_HEIGHT_VARIATION
  328. local tree_positions1 = {}
  329. -- First mapgen pass.
  330. for z = z0, z1 do
  331. for x = x0, x1 do
  332. -- Get index into 2D noise arrays.
  333. local nx = (x-x0)
  334. local nz = (z-z0)
  335. local ni2 = (side_len_z*nz+nx)
  336. -- Lua arrays start indexing at 1, not 0. Urrrrgh.
  337. ni2 = ni2 + 1
  338. local n1 = noisemap1[ni2]
  339. local n2 = noisemap2[ni2]
  340. -- Randomize height of the bedrock a bit.
  341. local bedrock_adjust = (nstart + bd + pr:next(0, pr:next(1, 2)))
  342. local clay_depth = (nstart + bd + pr:next(1, 2))
  343. -- Fixed ocean height.
  344. local ocean_depth = (nstart + od)
  345. -- Ground height.
  346. local an1 = abs(n1)
  347. local ground_depth = (nstart + gd + floor(an1 * ghv))
  348. local water_depth = (ocean_depth - ground_depth)
  349. if water_depth <= 2 then
  350. if pr:next(1, 16) == 1 then
  351. tree_positions1[#tree_positions1+1] = {x=x, y=ground_depth, z=z, w=an1}
  352. end
  353. elseif water_depth == 3 then
  354. if pr:next(1, 40) == 1 then
  355. tree_positions1[#tree_positions1+1] = {x=x, y=ground_depth, z=z, w=an1}
  356. end
  357. elseif water_depth == 4 then
  358. if pr:next(1, 300) == 1 then
  359. tree_positions1[#tree_positions1+1] = {x=x, y=ground_depth, z=z, w=an1}
  360. end
  361. end
  362. -- First pass through column.
  363. for y = y0, y1 do
  364. local vp = area:index(x, y, z)
  365. if y >= nstart and y <= nfinish then
  366. -- Get what's already here.
  367. local cid = data[vp]
  368. if cid == c_air or cid == c_ignore then
  369. -- Place bedrock layer.
  370. if y <= bedrock_adjust then
  371. data[vp] = c_bedrock
  372. elseif y <= clay_depth and water_depth >= 5 then
  373. data[vp] = c_clay
  374. elseif y < ground_depth then
  375. data[vp] = c_dirt
  376. elseif y == ground_depth then
  377. -- Mud appears when silt rises to water surface and above.
  378. if ground_depth >= (ocean_depth - 1) then
  379. data[vp] = c_mud
  380. else
  381. data[vp] = c_silt
  382. end
  383. elseif y <= ocean_depth then
  384. data[vp] = c_water
  385. end
  386. end
  387. end
  388. end
  389. end
  390. end
  391. -- Finalize voxel manipulator.
  392. -- Note: we specifically do not generate ores! The value of this realm is in
  393. -- its trees.
  394. vm:set_data(data)
  395. vm:set_lighting({day=0, night=0})
  396. vm:calc_lighting()
  397. vm:update_liquids()
  398. vm:write_to_map()
  399. for k, v in ipairs(tree_positions1) do
  400. local w = v.w
  401. if w > 1.0 then
  402. w = 1.0
  403. end
  404. local h = 13 * w
  405. v.w = nil
  406. -- Schematic horizontal offset.
  407. v.x = v.x - 2
  408. v.z = v.z - 2
  409. local path = cw.worldpath .. "/cw_jungletree_base.mts"
  410. local path2 = cw.worldpath .. "/cw_jungletree_top.mts"
  411. local path3 = path2
  412. if h > 10 then
  413. path2 = path
  414. end
  415. local force_place = false
  416. minetest.place_schematic(v, path, "random", JUNGLETREE_REPLACEMENTS, force_place)
  417. if pr:next(1, 5) <= 4 then
  418. v.y = v.y + h
  419. if h > 10 then
  420. minetest.place_schematic(vector.add(v, RANDPOS[math.random(1, #RANDPOS)]), path2, "random", JUNGLETREE_REPLACEMENTS, force_place)
  421. else
  422. minetest.place_schematic(v, path2, "random", JUNGLETREE_REPLACEMENTS, force_place)
  423. end
  424. if pr:next(1, 3) <= 2 then
  425. v.y = v.y + h
  426. if h > 10 then
  427. minetest.place_schematic(vector.add(v, RANDPOS[math.random(1, #RANDPOS)]), path2, "random", JUNGLETREE_REPLACEMENTS, force_place)
  428. else
  429. minetest.place_schematic(v, path2, "random", JUNGLETREE_REPLACEMENTS, force_place)
  430. end
  431. if h >= 10 then
  432. if pr:next(1, 2) == 1 then
  433. v.y = v.y + 13
  434. minetest.place_schematic(v, path3, "random", JUNGLETREE_REPLACEMENTS, force_place)
  435. if h > 10 and pr:next(1, 3) == 1 then
  436. v.y = v.y + 13
  437. minetest.place_schematic(v, path3, "random", JUNGLETREE_REPLACEMENTS, force_place)
  438. end
  439. end
  440. elseif h >= 8 then
  441. if pr:next(1, 3) == 1 then
  442. v.y = v.y + 12
  443. minetest.place_schematic(v, path3, "random", JUNGLETREE_REPLACEMENTS, force_place)
  444. end
  445. end
  446. end
  447. end
  448. end
  449. end
  450. if not cw.registered then
  451. -- Register the mapgen callback.
  452. minetest.register_on_generated(function(...)
  453. cw.generate_realm(...)
  454. end)
  455. local c = "cw:core"
  456. local f = cw.modpath .. "/init.lua"
  457. reload.register_file(c, f, false)
  458. cw.registered = true
  459. end