4 This mod adds surprise blocks to Minetest.
5 When destroyed, they drop one or more random items.
7 The item is selected with the “treasurer” mod.
8 Therefore, this is a treasure spawning mod.
9 You also need to install at least one treasure registraton mod,
10 otherwise, the questinon mark blocks stay empty.
11 Refer to the documentation of the “treasurer” mod to learn more.
14 if (minetest
.get_modpath("intllib")) then
17 S
= function ( s
) return s
end
20 --[[ here are some configuration variables ]]
21 local blocks_per_chunk
= 1 -- number of blocks per chunk.
22 local height_min
= -30000 -- minimum spawning height
23 local height_max
= 30000 -- maximum spawning height
24 local height_above_ground
= 4
27 minetest
.register_on_generated(function(minp
, maxp
, seed
)
28 -- get the water level and convert it to a number
29 local water_level
= minetest
.setting_get("water_level")
30 if water_level
== nil or type(water_level
) ~= "number" then
33 water_level
= tonumber(water_level
)
36 if(maxp
.y
< height_min
or minp
.y
> height_max
) then
39 local y_min
= math
.max(minp
.y
, height_min
)
40 local y_max
= math
.min(maxp
.y
, height_max
)
41 for i
=1, blocks_per_chunk
do
42 local pos
= {x
=math
.random(minp
.x
,maxp
.x
),z
=math
.random(minp
.z
,maxp
.z
), y
=minp
.y
}
43 local env
= minetest
.env
48 if env
:get_node({x
=pos
.x
,y
=y_max
,z
=pos
.z
}).name
~= "air" then
49 for y
=y_max
,y_min
,-1 do
50 local p
= {x
=pos
.x
,y
=y
,z
=pos
.z
}
51 if env
:get_node(p
).name
== "air" then
58 local p
= {x
=pos
.x
,y
=y
,z
=pos
.z
}
59 if env
:get_node(p
).name
~= "air" then
66 local block_pos
= {x
=pos
.x
,y
=ground
+height_above_ground
, z
=pos
.z
}
67 local nn
= minetest
.get_node(block_pos
).name
69 -- Place the question mark block
71 block
.name
= "tsm_surprise:question"
73 minetest
.set_node(block_pos
, block
)
81 Register the question mark node
86 description
= S("Surprise block"),
87 _doc_items_longdesc
= S("If this block is mined, a few surprise items may drop out of it."),
88 tiles
= { { name
= "tsm_surprise_question_anim.png", animation
= {
89 type = "vertical_frames", aspect_w
=16, aspect_h
=16, length
=3.0 } } },
91 is_ground_content
= false,
92 groups
= { dig_immediate
=2, },
94 place
= { name
= "tsm_surprise_question_dig", gain
= 1 },
95 dug
= { name
= "tsm_surprise_question_break", gain
= 0.8 },
96 dig
= { name
= "tsm_surprise_question_dig", gain
= 0.4 },
97 footstep
= { name
= "tsm_surprise_question_dig", gain
= 0.1 },
99 after_destruct
= function (pos
, oldnode
)
100 local drops
= treasurer
.select_random_treasures(1,0,3)
101 for _
,item
in ipairs(drops
) do
103 if type(item
) == "string" then
104 name
, count
= item
:match("^([a-zA-Z0-9_:]*) ([0-9]*)$")
112 count
= item
:get_count()
113 name
= item
:get_name()
115 if not inv
or not inv
:contains_item("main", ItemStack(name
)) then
117 local obj
= minetest
.env
:add_item(pos
, name
)
119 obj
:get_luaentity().collect
= true
120 local x
= math
.random(1, 5)
121 if math
.random(1,2) == 1 then
124 local z
= math
.random(1, 5)
125 if math
.random(1,2) == 1 then
128 obj
:setvelocity({x
=1/x
, y
=obj
:getvelocity().y
, z
=1/z
})
135 minetest
.register_node("tsm_surprise:question",nodedef
)