Initial commit
[minetest_tsm_chests_dungeon.git] / init.lua
bloba577bdafd953d6d2f6fc493e05682d8662ca51b8
1 -- Preciousness scaling numbers
2 local h_min = -256 -- Minimum height. At this height and below, the preciousness is at its peak
3 local h_max = -20 -- Max. height. At this height and above, the preciousness is at its lowest level
5 minetest.set_gen_notify("dungeon")
6 minetest.register_on_generated(function(minp, maxp, seed)
7 local g = minetest.get_mapgen_object("gennotify")
8 if g and g.dungeon and #g.dungeon > 3 then
9 minetest.after(3, function(d)
10 if d == nil or #d < 1 then
11 return
12 end
13 for i=1,2 do
14 local p = d[math.random(1, #d)]
15 if minetest.get_node({x=p.x, y =p.y-1, z=p.z}).name ~= "air" then
16 minetest.set_node(p, {name = "default:chest"})
18 -- calculate preciousness of treasures based on height. higher = more precious
19 local height = math.abs(h_min) - math.abs(h_max)
20 local y_norm = (p.y+1) - h_min
21 local scale = 1 - (y_norm/height)
23 -- We will select randomly 1-2 out of 5 possible treasure groups: light, crafting component, building block, and melee weapon
24 local groups = {
25 { 4, 0, scale*2+3, "light" }, -- precousness: 0..5
26 { 6, 0, 2, "crafting_component" }, -- 0..2
27 { 6, 0, 5, "building_block" }, -- 0..5
28 { 1, scale*2, scale*5, "melee_weapon" }, -- 0..5
29 { 3, 0, 3, "food" }, -- 0..3
32 local meta = minetest.get_meta(p)
33 local inv = meta:get_inventory()
34 local treasures_added = 0
35 for j=1, math.random(1, 2) do
36 local r = math.random(1, #groups)
37 local treasures = treasurer.select_random_treasures(groups[r][1], groups[r][2], groups[r][3], { groups[r][4] })
38 treasures_added = treasures_added + #treasures
39 for t=1,#treasures do
40 inv:add_item("main", treasures[t])
41 end
42 end
43 -- No treasures found? Maybe add one random treasure from default group
44 if treasures_added <= 0 then
45 if math.random(1,2) then
46 local treasures = treasurer.select_random_treasures(1, 0, 4)
47 for t=1,#treasures do
48 inv:add_item("main", treasures[t])
49 end
50 end
51 end
52 end
53 end
54 end, table.copy(g.dungeon))
55 end
56 end)