Use new doc_items field names
[minetest_tsm_surprise.git] / init.lua
blobf50574774bf52cd024d1c8e3efac4f21de03f6a7
1 --[[
2 TSM: Surprise blocks
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.
13 local S
14 if (minetest.get_modpath("intllib")) then
15 S = intllib.Getter()
16 else
17 S = function ( s ) return s end
18 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
26 -- generation code
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
31 water_level = 1
32 else
33 water_level = tonumber(water_level)
34 end
36 if(maxp.y < height_min or minp.y > height_max) then
37 return
38 end
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
45 -- Find ground level
46 local ground = nil
47 local top = y_max
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
52 top = y
53 break
54 end
55 end
56 end
57 for y=top,y_min,-1 do
58 local p = {x=pos.x,y=y,z=pos.z}
59 if env:get_node(p).name ~= "air" then
60 ground = y
61 break
62 end
63 end
65 if(ground~=nil) 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
68 if nn == "air" then
69 -- Place the question mark block
70 local block = {}
71 block.name = "tsm_surprise:question"
73 minetest.set_node(block_pos, block)
75 end
76 end
77 end
78 end)
80 --[[
81 Register the question mark node
85 local nodedef = {
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 } } },
90 drop = "",
91 is_ground_content = false,
92 groups = { dig_immediate=2, },
93 sounds = {
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
102 local count, name
103 if type(item) == "string" then
104 name, count = item:match("^([a-zA-Z0-9_:]*) ([0-9]*)$")
105 if not name then
106 name = item
108 if not count then
109 count = 1
111 else
112 count = item:get_count()
113 name = item:get_name()
115 if not inv or not inv:contains_item("main", ItemStack(name)) then
116 for i=1,count do
117 local obj = minetest.env:add_item(pos, name)
118 if obj ~= nil then
119 obj:get_luaentity().collect = true
120 local x = math.random(1, 5)
121 if math.random(1,2) == 1 then
122 x = -x
124 local z = math.random(1, 5)
125 if math.random(1,2) == 1 then
126 z = -z
128 obj:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z})
133 end,
135 minetest.register_node("tsm_surprise:question",nodedef)