Version 0.31.0
[MineClone/MineClone2.git] / mods / MAPGEN / mcl_dungeons / init.lua
blobb18d30e5fa6623dd1c1e3b7f15428d0e6452486a
1 -- FIXME: Chests may appear at openings
3 -- TODO: Add monster spawner
5 local mg_name = minetest.get_mapgen_setting("mg_name")
6 local pr = PseudoRandom(os.time())
8 -- Get loot for dungeon chests
9 local get_loot = function()
10 local loottable = {
12 stacks_min = 1,
13 stacks_max = 3,
14 items = {
15 { itemstring = "mobs:nametag", weight = 20 },
16 { itemstring = "mcl_mobitems:saddle", weight = 20 },
17 { itemstring = "mcl_jukebox:record_1", weight = 15 },
18 { itemstring = "mcl_jukebox:record_4", weight = 15 },
19 { itemstring = "mobs_mc:iron_horse_armor", weight = 15 },
20 { itemstring = "mcl_core:apple_gold", weight = 15 },
21 -- TODO: Enchanted Book
22 { itemstring = "mcl_books:book", weight = 10 },
23 { itemstring = "mobs_mc:gold_horse_armor", weight = 10 },
24 { itemstring = "mobs_mc:diamond_horse_armor", weight = 5 },
25 -- TODO: Enchanted Golden Apple
26 { itemstring = "mcl_core:apple_gold", weight = 2 },
30 stacks_min = 1,
31 stacks_max = 4,
32 items = {
33 { itemstring = "mcl_farming:wheat_item", weight = 20, amount_min = 1, amount_max = 4 },
34 { itemstring = "mcl_farming:bread", weight = 20 },
35 { itemstring = "mcl_core:coal_lump", weight = 15, amount_min = 1, amount_max = 4 },
36 { itemstring = "mesecons:redstone", weight = 15, amount_min = 1, amount_max = 4 },
37 { itemstring = "mcl_farming:beetroot_seeds", weight = 10, amount_min = 2, amount_max = 4 },
38 { itemstring = "mcl_farming:melon_seeds", weight = 10, amount_min = 2, amount_max = 4 },
39 { itemstring = "mcl_farming:pumpkin_seeds", weight = 10, amount_min = 2, amount_max = 4 },
40 { itemstring = "mcl_core:iron_ingot", weight = 10, amount_min = 1, amount_max = 4 },
41 { itemstring = "mcl_buckets:bucket_empty", weight = 10 },
42 { itemstring = "mcl_core:gold_ingot", weight = 5, amount_min = 1, amount_max = 4 },
46 stacks_min = 3,
47 stacks_max = 3,
48 items = {
49 { itemstring = "mcl_mobitems:bone", weight = 10, amount_min = 1, amount_max = 8 },
50 { itemstring = "mcl_mobitems:gunpowder", weight = 10, amount_min = 1, amount_max = 8 },
51 { itemstring = "mcl_mobitems:rotten_flesh", weight = 10, amount_min = 1, amount_max = 8 },
52 { itemstring = "mcl_mobitems:string", weight = 10, amount_min = 1, amount_max = 8 },
57 -- Bonus loot for v6 mapgen: Otherwise unobtainable saplings.
58 if mg_name == "v6" then
59 table.insert(loottable, {
60 stacks_min = 1,
61 stacks_max = 1,
62 items = {
63 { itemstring = "mcl_core:birchsapling", weight = 1, amount_min = 1, amount_max = 2 },
64 { itemstring = "mcl_core:acaciasapling", weight = 1, amount_min = 1, amount_max = 2 },
65 { itemstring = "", weight = 11 },
68 end
69 local items = mcl_loot.get_multi_loot(loottable, pr)
71 return items
72 end
75 -- Buffer for LuaVoxelManip
76 local lvm_buffer = {}
77 local lvm_buffer2 = {} -- for param2
79 -- Below the bedrock, generate air/void
80 minetest.register_on_generated(function(minp, maxp)
81 if maxp.y < mcl_vars.mg_overworld_min or minp.y > mcl_vars.mg_overworld_max then
82 return
83 end
85 local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
86 local data = vm:get_data(lvm_buffer)
87 local area = VoxelArea:new({MinEdge=emin, MaxEdge=emax})
88 local lvm_used = false
90 local c_air = minetest.get_content_id("air")
91 local c_cobble = minetest.get_content_id("mcl_core:cobble")
92 local c_mossycobble = minetest.get_content_id("mcl_core:mossycobble")
93 local c_chest = minetest.get_content_id("mcl_chests:chest")
95 -- Remember spawner chest positions to set metadata later
96 local chest_posses = {}
97 local spawner_posses = {}
99 -- Calculate the number of dungeon spawn attempts
100 local sizevector = vector.subtract(maxp, minp)
101 sizevector = vector.add(sizevector, 1)
102 local chunksize = sizevector.x * sizevector.y * sizevector.z
104 -- In Minecraft, there 8 dungeon spawn attempts Minecraft chunk (16*256*16 = 65536 blocks).
105 -- Minetest chunks don't have this size, so scale the number accordingly.
106 local attempts = math.ceil(chunksize / 65536 * 8)
108 for a=1, attempts do
109 local x, y, z
110 local b = 7 -- buffer
111 x = math.random(minp.x+b, maxp.x-b)
113 local ymin = math.min(mcl_vars.mg_overworld_max, math.max(minp.y, mcl_vars.mg_bedrock_overworld_max) + 7)
114 local ymax = math.min(mcl_vars.mg_overworld_max, math.max(maxp.y, mcl_vars.mg_bedrock_overworld_max) - 4)
116 y = math.random(ymin, ymax)
117 z = math.random(minp.z+b, maxp.z-b)
119 local dungeonsizes = {
120 { x=5, y=4, z=5},
121 { x=5, y=4, z=7},
122 { x=7, y=4, z=5},
123 { x=7, y=4, z=7},
125 local dim = dungeonsizes[math.random(1, #dungeonsizes)]
127 -- Check floor and ceiling: Must be *completely* solid
128 local ceilingfloor_ok = true
129 for tx = x, x+dim.x do
130 for tz = z, z+dim.z do
131 local floor = minetest.get_name_from_content_id(data[area:index(tx, y, tz)])
132 local ceiling = minetest.get_name_from_content_id(data[area:index(tx, y+dim.y+1, tz)])
133 if (not minetest.registered_nodes[floor].walkable) or (not minetest.registered_nodes[ceiling].walkable) then
134 ceilingfloor_ok = false
135 break
138 if not ceilingfloor_ok then break end
141 -- Check for air openings (2 stacked air at ground level) in wall positions
142 local openings_counter = 0
143 -- Store positions of openings; walls will not be generated here
144 local openings = {}
145 -- Corners are stored because a corner-only opening needs to be increased,
146 -- so entities can get through.
147 local corners = {}
148 if ceilingfloor_ok then
150 local walls = {
151 -- walls along x axis (contain corners)
152 { x, x+dim.x+1, "x", "z", z },
153 { x, x+dim.x+1, "x", "z", z+dim.z+1 },
154 -- walls along z axis (exclude corners)
155 { z+1, z+dim.z, "z", "x", x },
156 { z+1, z+dim.z, "z", "x", x+dim.x+1 },
159 for w=1, #walls do
160 local wall = walls[w]
161 for iter = wall[1], wall[2] do
162 local pos = {}
163 pos[wall[3]] = iter
164 pos[wall[4]] = wall[5]
165 pos.y = y+1
167 if openings[pos.x] == nil then openings[pos.x] = {} end
169 local door1 = area:index(pos.x, pos.y, pos.z)
170 pos.y = y+2
171 local door2 = area:index(pos.x, pos.y, pos.z)
172 local doorname1 = minetest.get_name_from_content_id(data[door1])
173 local doorname2 = minetest.get_name_from_content_id(data[door2])
174 if doorname1 == "air" and doorname2 == "air" then
175 openings_counter = openings_counter + 1
176 openings[pos.x][pos.z] = true
178 -- Record corners
179 if wall[3] == "x" and (iter == wall[1] or iter == wall[2]) then
180 table.insert(corners, {x=pos.x, z=pos.z})
188 -- If all openings are only at corners, the dungeon can't be accessed yet.
189 -- This code extends the openings of corners so they can be entered.
190 if openings_counter >= 1 and openings_counter == #corners then
191 for c=1, #corners do
192 -- Prevent creating too many openings because this would lead to dungeon rejection
193 if openings_counter >= 5 then
194 break
196 -- A corner is widened by adding openings to both neighbors
197 local cx, cz = corners[c].x, corners[c].z
198 local cxn, czn = cx, cz
199 if x == cx then
200 cxn = cxn + 1
201 else
202 cxn = cxn - 1
204 if z == cz then
205 czn = czn + 1
206 else
207 czn = czn - 1
209 openings[cx][czn] = true
210 openings_counter = openings_counter + 1
211 if openings_counter < 5 then
212 openings[cxn][cz] = true
213 openings_counter = openings_counter + 1
218 -- Check conditions. If okay, start generating
219 if ceilingfloor_ok and openings_counter >= 1 and openings_counter <= 5 then
220 -- Okay! Spawning starts!
222 -- First prepare random chest positions.
223 -- Chests spawn at wall
225 -- We assign each position at the wall a number and each chest gets one of these numbers randomly
226 local totalChests = 2 -- this code strongly relies on this number being 2
227 local totalChestSlots = (dim.x-1) * (dim.z-1)
228 local chestSlots = {}
229 -- There is a small chance that both chests have the same slot.
230 -- In that case, we give a 2nd chance for the 2nd chest to get spawned.
231 -- If it failed again, tough luck! We stick with only 1 chest spawned.
232 local lastRandom
233 local secondChance = true -- second chance is still available
234 for i=1, totalChests do
235 local r = math.random(1, totalChestSlots)
236 if r == lastRandom and secondChance then
237 -- Oops! Same slot selected. Try again.
238 r = math.random(1, totalChestSlots)
239 secondChance = false
241 lastRandom = r
242 table.insert(chestSlots, r)
244 table.sort(chestSlots)
245 local currentChest = 1
247 -- Calculate the monster spawner position, to be re-used for later
248 local spawner_pos = {x = x + math.ceil(dim.x/2), y = y+1, z = z + math.ceil(dim.z/2)}
249 table.insert(spawner_posses, spawner_pos)
251 -- Generate walls and floor
252 local maxx, maxy, maxz = x+dim.x+1, y+dim.y, z+dim.z+1
253 local chestSlotCounter = 1
254 for tx = x, maxx do
255 for tz = z, maxz do
256 for ty = y, maxy do
257 local p_pos = area:index(tx, ty, tz)
259 -- Do not overwrite nodes with is_ground_content == false (e.g. bedrock)
260 -- Exceptions: cobblestone and moss stone so neighborings dungeons nicely connect to each other
261 local name = minetest.get_name_from_content_id(data[p_pos])
262 if name == "mcl_core:cobble" or name == "mcl_core:mossycobble" or minetest.registered_nodes[name].is_ground_content then
263 -- Floor
264 if ty == y then
265 if math.random(1,4) == 1 then
266 data[p_pos] = c_cobble
267 else
268 data[p_pos] = c_mossycobble
271 -- Generate walls
272 --[[ Note: No additional cobblestone ceiling is generated. This is intentional.
273 The solid blocks above the dungeon are considered as the “ceiling”.
274 It is possible (but rare) for a dungeon to generate below sand or gravel. ]]
276 elseif ty > y and (tx == x or tx == maxx or (tz == z or tz == maxz)) then
277 -- Check if it's an opening first
278 if (not openings[tx][tz]) or ty == maxy then
279 -- Place wall or ceiling
280 data[p_pos] = c_cobble
281 elseif ty < maxy - 1 then
282 -- Normally the openings are already clear, but not if it is a corner
283 -- widening. Make sure to clear at least the bottom 2 nodes of an opening.
284 data[p_pos] = c_air
285 elseif ty == maxy - 1 and data[p_pos] ~= c_air then
286 -- This allows for variation between 2-node and 3-node high openings.
287 data[p_pos] = c_cobble
289 -- If it was an opening, the lower 3 blocks are not touched at all
291 -- Room interiour
292 else
293 local forChest = ty==y+1 and (tx==x+1 or tx==maxx-1 or tz==z+1 or tz==maxz-1)
295 -- Place next chest at the wall (if it was its chosen wall slot)
296 if forChest and (currentChest < totalChests + 1) and (chestSlots[currentChest] == chestSlotCounter) then
297 local p2
299 -- Select rotation so the chest faces away from wall
300 if (tx==x+1) then p2 = 3
301 elseif (tx==maxx-1) then p2 = 1
302 elseif (tz==z+1) then p2 = 2
303 else p2 = 0 end
304 table.insert(chest_posses, {x=tx, y=ty, z=tz})
305 currentChest = currentChest + 1
306 else
307 data[p_pos] = c_air
309 if forChest then
310 chestSlotCounter = chestSlotCounter + 1
320 lvm_used = true
323 if lvm_used then
324 local chest_param2 = {}
325 -- Determine correct chest rotation (must pointi outwards)
326 for c=1, #chest_posses do
327 local cpos = chest_posses[c]
329 -- Check surroundings of chest to determine correct rotation
330 local surround_vectors = {
331 { x=-1, y=0, z=0 },
332 { x=1, y=0, z=0 },
333 { x=0, y=0, z=-1 },
334 { x=0, y=0, z=1 },
336 local surroundings = {}
338 for s=1, #surround_vectors do
339 -- Detect the 4 horizontal neighbors
340 local spos = vector.add(cpos, surround_vectors[s])
341 local wpos = vector.subtract(cpos, surround_vectors[s])
342 local p_pos = area:index(spos.x, spos.y, spos.z)
343 local p_pos2 = area:index(wpos.x, wpos.y, wpos.z)
345 local nodename = minetest.get_name_from_content_id(data[p_pos])
346 local nodename2 = minetest.get_name_from_content_id(data[p_pos2])
347 local nodedef = minetest.registered_nodes[nodename]
348 local nodedef2 = minetest.registered_nodes[nodename2]
349 -- The chest needs an open space in front of it and a walkable node (except chest) behind it
350 if nodedef and nodedef.walkable == false and nodedef2 and nodedef2.walkable == true and nodename2 ~= "mcl_chests:chest" then
351 table.insert(surroundings, spos)
354 -- Set param2 (=facedir) of this chest
355 local facedir
356 if #surroundings <= 0 then
357 -- Fallback if chest ended up in the middle of a room for some reason
358 facedir = math.random(0, 0)
359 else
360 -- 1 or multiple possible open directions: Choose random facedir
361 local face_to = surroundings[math.random(1, #surroundings)]
362 facedir = minetest.dir_to_facedir(vector.subtract(cpos, face_to))
364 chest_param2[c] = facedir
367 -- Finally generate the dungeons all at once (except the chests and the spawners)
368 vm:set_data(data)
369 vm:calc_lighting()
370 vm:update_liquids()
371 vm:write_to_map()
373 -- Chests are placed seperately
374 for c=1, #chest_posses do
375 local cpos = chest_posses[c]
376 minetest.set_node(cpos, {name="mcl_chests:chest", param2=chest_param2[c]})
377 local meta = minetest.get_meta(cpos)
378 local inv = meta:get_inventory()
379 local items = get_loot()
380 for i=1, math.min(#items, inv:get_size("main")) do
381 inv:set_stack("main", i, ItemStack(items[i]))
385 -- Monster spawners are placed seperately, too
386 -- We don't want to destroy non-ground nodes
387 for s=1, #spawner_posses do
388 local sp = spawner_posses[s]
389 local n = minetest.get_name_from_content_id(data[area:index(sp.x,sp.y,sp.z)])
390 if minetest.registered_nodes[n].is_ground_content then
392 -- ... and place it and select a random mob
393 minetest.set_node(sp, {name = "mcl_mobspawners:spawner"})
394 local mobs = {
395 "mobs_mc:zombie",
396 "mobs_mc:zombie",
397 "mobs_mc:spider",
398 "mobs_mc:skeleton",
400 local spawner_mob = mobs[math.random(1, #mobs)]
402 mcl_mobspawners.setup_spawner(sp, spawner_mob)
408 end)