mapgen.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. minetest.set_gen_notify({dungeon = true, temple = true})
  2. local function noise3d_integer(noise, pos)
  3. return math.abs(math.floor(noise:get3d(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)
  15. local wall = minetest.registered_aliases["mapgen_cobble"]
  16. local wall_alt = minetest.registered_aliases["mapgen_mossycobble"]
  17. local wall_ss = minetest.registered_aliases["mapgen_sandstonebrick"]
  18. local wall_ds = minetest.registered_aliases["mapgen_desert_stone"]
  19. local is_wall = function(node)
  20. return table.indexof({wall, wall_alt, wall_ss, wall_ds}, node.name) ~= -1
  21. end
  22. local dirs = {{x=1, z=0}, {x=-1, z=0}, {x=0, z=1}, {x=0, z=-1}}
  23. local get_node = minetest.get_node
  24. local ret = {}
  25. local mindist = {x=0, z=0}
  26. local min = function(a, b) return a ~= 0 and math.min(a, b) or b end
  27. local wallnode
  28. for _, dir in ipairs(dirs) do
  29. for i = 1, 9 do -- 9 = max room size / 2
  30. local pos = vector.add(cpos, {x=dir.x*i, y=0, z=dir.z*i})
  31. -- continue in that direction until we find a wall-like node
  32. local node = get_node(pos)
  33. if is_wall(node) then
  34. local front_below = vector.subtract(pos, {x=dir.x, y=1, z=dir.z})
  35. local above = vector.add(pos, {x=0, y=1, z=0})
  36. -- check that it:
  37. --- is at least 2 nodes high (not a staircase)
  38. --- has a floor
  39. if is_wall(get_node(front_below)) and is_wall(get_node(above)) then
  40. table.insert(ret, {pos = pos, facing = {x=-dir.x, y=0, z=-dir.z}})
  41. if dir.z == 0 then
  42. mindist.x = min(mindist.x, i-1)
  43. else
  44. mindist.z = min(mindist.z, i-1)
  45. end
  46. wallnode = node.name
  47. end
  48. -- abort even if it wasn't a wall cause something is in the way
  49. break
  50. end
  51. end
  52. end
  53. local mapping = {
  54. [wall_ss] = "sandstone",
  55. [wall_ds] = "desert"
  56. }
  57. return {
  58. walls = ret,
  59. size = {x=mindist.x*2, z=mindist.z*2},
  60. type = mapping[wallnode] or "normal"
  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. assert(#item_list >= dungeon_loot.STACKS_PER_CHEST_MAX)
  69. item_list = random_sample(rand, item_list, dungeon_loot.STACKS_PER_CHEST_MAX)
  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 itemdef.tool_capabilities then
  80. for n = 1, amount do
  81. local wear = rand:next(0.20 * 65535, 0.75 * 65535) -- 20% to 75% wear
  82. table.insert(items, ItemStack({name = loot.name, wear = wear}))
  83. end
  84. elseif itemdef.stack_max == 1 then
  85. -- not stackable, add separately
  86. for n = 1, amount do
  87. table.insert(items, loot.name)
  88. end
  89. else
  90. table.insert(items, ItemStack({name = loot.name, count = amount}))
  91. end
  92. end
  93. end
  94. -- place items at random places in chest
  95. local inv = minetest.get_meta(pos):get_inventory()
  96. local listsz = inv:get_size("main")
  97. assert(listsz >= #items)
  98. for _, item in ipairs(items) do
  99. local index = rand:next(1, listsz)
  100. if inv:get_stack("main", index):is_empty() then
  101. inv:set_stack("main", index, item)
  102. else
  103. inv:add_item("main", item) -- space occupied, just put it anywhere
  104. end
  105. end
  106. end
  107. minetest.register_on_generated(function(minp, maxp, blockseed)
  108. local gennotify = minetest.get_mapgen_object("gennotify")
  109. local poslist = gennotify["dungeon"] or {}
  110. for _, entry in ipairs(gennotify["temple"] or {}) do
  111. table.insert(poslist, entry)
  112. end
  113. if #poslist == 0 then return end
  114. local noise = minetest.get_perlin(10115, 4, 0.5, 1)
  115. local rand = PcgRandom(noise3d_integer(noise, poslist[1]))
  116. local candidates = {}
  117. -- process at most 16 rooms to keep runtime of this predictable
  118. local num_process = math.min(#poslist, 16)
  119. for i = 1, num_process do
  120. local room = find_walls(poslist[i])
  121. -- skip small rooms and everything that doesn't at least have 3 walls
  122. if math.min(room.size.x, room.size.z) >= 4 and #room.walls >= 3 then
  123. table.insert(candidates, room)
  124. end
  125. end
  126. local num_chests = rand:next(dungeon_loot.CHESTS_MIN, dungeon_loot.CHESTS_MAX)
  127. num_chests = math.min(#candidates, num_chests)
  128. local rooms = random_sample(rand, candidates, num_chests)
  129. for _, room in ipairs(rooms) do
  130. -- choose place somewhere in front of any of the walls
  131. local wall = room.walls[rand:next(1, #room.walls)]
  132. local v, vi -- vector / axis that runs alongside the wall
  133. if wall.facing.x ~= 0 then
  134. v, vi = {x=0, y=0, z=1}, "z"
  135. else
  136. v, vi = {x=1, y=0, z=0}, "x"
  137. end
  138. local chestpos = vector.add(wall.pos, wall.facing)
  139. local off = rand:next(-room.size[vi]/2 + 1, room.size[vi]/2 - 1)
  140. chestpos = vector.add(chestpos, vector.multiply(v, off))
  141. if minetest.get_node(chestpos).name == "air" then
  142. -- make it face inwards to the room
  143. local facedir = minetest.dir_to_facedir(vector.multiply(wall.facing, -1))
  144. minetest.add_node(chestpos, {name = "default:chest", param2 = facedir})
  145. populate_chest(chestpos, PcgRandom(noise3d_integer(noise, chestpos)), room.type)
  146. end
  147. end
  148. end)