2
0

mapgen.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. minetest.set_gen_notify({dungeon = true, temple = true})
  2. local function noise3d_integer(noise, pos)
  3. return math.abs(math.floor(noise:get_3d(pos) * 0x7fffffff))
  4. end
  5. local function random_sample(rand, list, count)
  6. local ret = {}
  7. for n = 1, count do
  8. local idx = rand:next(1, #list)
  9. table.insert(ret, list[idx])
  10. table.remove(list, idx)
  11. end
  12. return ret
  13. end
  14. local function find_walls(cpos, is_temple)
  15. local is_wall = function(node)
  16. return node.name ~= "air" and node.name ~= "ignore"
  17. end
  18. local dirs = {{x=1, z=0}, {x=-1, z=0}, {x=0, z=1}, {x=0, z=-1}}
  19. local get_node = minetest.get_node
  20. local ret = {}
  21. local mindist = {x=0, z=0}
  22. local min = function(a, b) return a ~= 0 and math.min(a, b) or b end
  23. for _, dir in ipairs(dirs) do
  24. for i = 1, 9 do -- 9 = max room size / 2
  25. local pos = vector.add(cpos, {x=dir.x*i, y=0, z=dir.z*i})
  26. -- continue in that direction until we find a wall-like node
  27. local node = get_node(pos)
  28. if is_wall(node) then
  29. local front_below = vector.subtract(pos, {x=dir.x, y=1, z=dir.z})
  30. local above = vector.add(pos, {x=0, y=1, z=0})
  31. -- check that it:
  32. --- is at least 2 nodes high (not a staircase)
  33. --- has a floor
  34. if is_wall(get_node(front_below)) and is_wall(get_node(above)) then
  35. table.insert(ret, {pos = pos, facing = {x=-dir.x, y=0, z=-dir.z}})
  36. if dir.z == 0 then
  37. mindist.x = min(mindist.x, i-1)
  38. else
  39. mindist.z = min(mindist.z, i-1)
  40. end
  41. end
  42. -- abort even if it wasn't a wall cause something is in the way
  43. break
  44. end
  45. end
  46. end
  47. local biome = minetest.get_biome_data(cpos)
  48. biome = biome and minetest.get_biome_name(biome.biome) or ""
  49. local type = "normal"
  50. if is_temple or biome:find("desert") == 1 then
  51. type = "desert"
  52. elseif biome:find("sandstone_desert") == 1 then
  53. type = "sandstone"
  54. elseif biome:find("icesheet") == 1 then
  55. type = "ice"
  56. end
  57. return {
  58. walls = ret,
  59. size = {x=mindist.x*2, z=mindist.z*2},
  60. type = type,
  61. }
  62. end
  63. local function populate_chest(pos, rand, dungeontype)
  64. --minetest.chat_send_all("chest placed at " .. minetest.pos_to_string(pos) .. " [" .. dungeontype .. "]")
  65. --minetest.add_node(vector.add(pos, {x=0, y=1, z=0}), {name="default:torch", param2=1})
  66. local item_list = dungeon_loot._internal_get_loot(pos.y, dungeontype)
  67. -- take random (partial) sample of all possible items
  68. local sample_n = math.min(#item_list, dungeon_loot.STACKS_PER_CHEST_MAX)
  69. item_list = random_sample(rand, item_list, sample_n)
  70. -- apply chances / randomized amounts and collect resulting items
  71. local items = {}
  72. for _, loot in ipairs(item_list) do
  73. if rand:next(0, 1000) / 1000 <= loot.chance then
  74. local itemdef = minetest.registered_items[loot.name]
  75. local amount = 1
  76. if loot.count ~= nil then
  77. amount = rand:next(loot.count[1], loot.count[2])
  78. end
  79. if not itemdef then
  80. minetest.log("warning", "Registered loot item " .. loot.name .. " does not exist")
  81. elseif itemdef.tool_capabilities then
  82. for n = 1, amount do
  83. local wear = rand:next(0.20 * 65535, 0.75 * 65535) -- 20% to 75% wear
  84. table.insert(items, ItemStack({name = loot.name, wear = wear}))
  85. end
  86. elseif itemdef.stack_max == 1 then
  87. -- not stackable, add separately
  88. for n = 1, amount do
  89. table.insert(items, loot.name)
  90. end
  91. else
  92. table.insert(items, ItemStack({name = loot.name, count = amount}))
  93. end
  94. end
  95. end
  96. -- place items at random places in chest
  97. local inv = minetest.get_meta(pos):get_inventory()
  98. local listsz = inv:get_size("main")
  99. assert(listsz >= #items)
  100. for _, item in ipairs(items) do
  101. local index = rand:next(1, listsz)
  102. if inv:get_stack("main", index):is_empty() then
  103. inv:set_stack("main", index, item)
  104. else
  105. inv:add_item("main", item) -- space occupied, just put it anywhere
  106. end
  107. end
  108. end
  109. minetest.register_on_generated(function(minp, maxp, blockseed)
  110. local gennotify = minetest.get_mapgen_object("gennotify")
  111. local poslist = gennotify["dungeon"] or {}
  112. local n_dungeons = #poslist
  113. -- Add MGv6 desert temples to the list too
  114. for _, entry in ipairs(gennotify["temple"] or {}) do
  115. table.insert(poslist, entry)
  116. end
  117. if #poslist == 0 then return end
  118. local noise = minetest.get_perlin(10115, 4, 0.5, 1)
  119. local rand = PcgRandom(noise3d_integer(noise, poslist[1]))
  120. local candidates = {}
  121. -- process at most 8 rooms to keep runtime of this predictable
  122. local num_process = math.min(#poslist, 8)
  123. for i = 1, num_process do
  124. local room = find_walls(poslist[i], i > n_dungeons)
  125. -- skip small rooms and everything that doesn't at least have 3 walls
  126. if math.min(room.size.x, room.size.z) >= 4 and #room.walls >= 3 then
  127. table.insert(candidates, room)
  128. end
  129. end
  130. local num_chests = rand:next(dungeon_loot.CHESTS_MIN, dungeon_loot.CHESTS_MAX)
  131. num_chests = math.min(#candidates, num_chests)
  132. local rooms = random_sample(rand, candidates, num_chests)
  133. for _, room in ipairs(rooms) do
  134. -- choose place somewhere in front of any of the walls
  135. local wall = room.walls[rand:next(1, #room.walls)]
  136. local v, vi -- vector / axis that runs alongside the wall
  137. if wall.facing.x ~= 0 then
  138. v, vi = {x=0, y=0, z=1}, "z"
  139. else
  140. v, vi = {x=1, y=0, z=0}, "x"
  141. end
  142. local chestpos = vector.add(wall.pos, wall.facing)
  143. local off = rand:next(-room.size[vi]/2 + 1, room.size[vi]/2 - 1)
  144. chestpos = vector.add(chestpos, vector.multiply(v, off))
  145. if minetest.get_node(chestpos).name == "air" then
  146. -- make it face inwards to the room
  147. local facedir = minetest.dir_to_facedir(vector.multiply(wall.facing, -1))
  148. minetest.add_node(chestpos, {name = "default:chest", param2 = facedir})
  149. populate_chest(chestpos, PcgRandom(noise3d_integer(noise, chestpos)), room.type)
  150. end
  151. end
  152. end)