use _doc_items_*
[minetest_magicbeans_w.git] / init.lua
blob6bad158b5ef41aea12a759b83ece29a908815cba
1 -- get intllib optionally
2 local S
3 if (minetest.get_modpath("intllib")) then
4 S = intllib.Getter()
5 else
6 S = function ( s ) return s end
7 end
8 math.randomseed(os.time())
10 --Register beanstalk nodes
11 minetest.register_node("magicbeans_w:leaves", {
12 description = S("Magic beanstalk leaves"),
13 tiles = {"magicbeans_w_leaves.png"},
14 inventory_image = "magicbeans_w_leaves.png",
15 drawtype = "plantlike",
16 is_ground_content = false,
17 walkable = false,
18 climbable = true,
19 paramtype = "light",
20 groups = {leaves = 1, snappy = 3, flammable = 2},
21 sounds = default.node_sound_leaves_defaults()
24 minetest.register_node("magicbeans_w:blank", {
25 description = S("Magic beanstalk climbable blank"),
26 _doc_items_create_entry = false,
27 drawtype = "airlike",
28 inventory_image = "unknown_node.png",
29 wield_image = "unknown_node.png",
30 walkable = false,
31 climbable = true,
32 paramtype = "light",
33 buildable_to = true,
34 pointable = false,
35 diggable = false,
36 drop = "",
37 groups = {not_in_creative_inventory = 1},
40 minetest.register_node("magicbeans_w:stem", {
41 description = S("Magic beanstalk stem"),
42 is_ground_content = false,
43 tiles = {"magicbeans_w_stem_topbottom.png", "magicbeans_w_stem_topbottom.png", "magicbeans_w_stem.png"},
44 paramtype2 = "facedir",
45 groups = {oddly_breakable_by_hand = 1, choppy = 3, flammable = 2},
46 sounds = default.node_sound_wood_defaults(),
47 on_place = minetest.rotate_node,
50 minetest.register_craft({
51 type="fuel",
52 recipe = "magicbeans_w:stem",
53 burntime = 21,
56 minetest.register_node("magicbeans_w:cloud", {
57 description = S("Thin Cloud"),
58 drawtype = "liquid",
59 paramtype = "light",
60 tiles = {"default_cloud.png"},
61 walkable = false,
62 climbable = true,
63 sunlight_propagates = true,
64 post_effect_color = "#FFFFFFA0",
65 alpha = 192,
66 groups = {dig_immediate = 3, not_in_creative_inventory = 1},
67 sounds = { dig = { name = "", gain = 0 }, footstep = { name = "", gain = 0 },
68 dug = { name = "", gain = 0 }, player = { name = "", gain = 0 } },
69 drop = "",
72 if(minetest.get_modpath("awards") ~= nil) then
73 awards.register_achievement("magicbeans_w_beanstalk", {
74 title = S("Sam and the Beanstalk"),
75 description = S("Climb up a magic beanstalk and touch the clouds."),
76 icon = "magicbeans_w_stem.png",
77 trigger = {
78 type = "dig",
79 node = "magicbeans_w:cloud",
80 target = 1,
83 end
85 local eat
86 if minetest.get_modpath("doc_items") then
87 eat = doc.sub.items.temp.eat
88 end
90 local magicbeans_w_list = {
91 { S("Magic jumping bean"), S("Magic super jump"), "jumping", nil, 5, nil, {"jump"},
92 S("Eating this bean will make your jumps much, much higher for 30 seconds. In fact, you can jump so high that you may hurt yourself when you fall down again. Use with caution. It also cancels any previous jumping effects."),
93 eat, },
94 { S("Magic flying bean"), S("Magic flying"), "flying", 2, nil, 0.02, {"speed", "gravity"},
95 S("Eating a running bean will greatly increase your walking speed for 30 seconds. It also cancels out any previous walking speed effects."),
96 eat, },
97 { S("Magic running bean"), S("Magic super speed"), "running", 3, nil, nil, {"speed"},
98 S("Eating a flying bean will allow you to fly for 30 seconds, after which your flight will apruptly end. Use with caution. It also cancels out any gravity and walking speed effects you have."),
99 S("Hold it in your hand, then leftclick to eat the bean. To initiate the flight, just jump. You will very slowly rise high in the skies and eventually sink again. Plan your flights carefully as falling down in mid-flight can be very harmful."), },
100 { S("Magic beanstalk bean"), nil, "beanstalk", nil, nil, nil, nil,
101 S("This magic bean will grow into a giant beanstalk when planted. The beanstalk can grop up to a height of about 100 blocks, easily reaching into the clouds."),
102 S("Rightclick with it on dirt with grass (or any other block which allows saplings to grow) to plant it, then wait for a short while for the giant beanstalk to appear."),
106 for i in ipairs(magicbeans_w_list) do
108 local beandesc = magicbeans_w_list[i][1]
109 local effdesc = magicbeans_w_list[i][2]
110 local bean = magicbeans_w_list[i][3]
111 local beanspeed = magicbeans_w_list[i][4]
112 local beanjump = magicbeans_w_list[i][5]
113 local beangrav = magicbeans_w_list[i][6]
114 local beangroups = magicbeans_w_list[i][7]
115 local longdesc = magicbeans_w_list[i][8]
116 local usagehelp = magicbeans_w_list[i][9]
118 --Register effect types
119 if(beangroups ~= nil) then
120 playereffects.register_effect_type("bean_"..bean, effdesc, "magicbeans_w_"..bean.."16.png", beangroups,
121 function(player)
122 player:set_physics_override({speed=beanspeed, jump=beanjump, gravity=beangrav})
123 end,
124 function(effect)
125 local player = minetest.get_player_by_name(effect.playername)
126 local speed, jump, grav
127 if beanspeed ~= nil then speed = 1 end
128 if beanjump ~= nil then jump = 1 end
129 if beangrav ~= nil then grav = 1 end
130 player:set_physics_override({speed=speed, jump=jump, gravity=grav})
135 local on_use = nil
136 if bean ~= "beanstalk" then
137 on_use = function(itemstack, user, pointed_thing)
138 playereffects.apply_effect_type("bean_"..bean, 30, user)
139 itemstack:take_item()
140 return itemstack
143 --Register beans
144 minetest.register_craftitem("magicbeans_w:"..bean, {
145 description = beandesc,
146 _doc_items_longdesc = longdesc,
147 _doc_items_usagehelp = usagehelp,
148 inventory_image = "magicbeans_w_"..bean..".png",
149 on_place = function(itemstack, placer, pointed_thing)
150 if pointed_thing.type == "node" then
151 if bean ~= "beanstalk" then
152 minetest.add_item(pointed_thing.above, {name="magicbeans_w:"..bean})
153 else
154 -- Grow Beanstalk
155 local stalk = pointed_thing.above
156 local floor = pointed_thing.under
157 if minetest.get_item_group(minetest.get_node(floor).name, "soil") == 0 then
158 return itemstack
160 stalk.x = stalk.x - 2
161 stalk.z = stalk.z - 2
162 local height = 127 - stalk.y
163 local c = {1, 1, 1, 1, 2, 1, 1, 1, 1}
164 local d = {1, 2, 3, 6}
165 local e = {9, 8, 7, 4}
166 local ex = 0
167 local zed = 1
168 local blank = 0
169 local why1, ex1, zed1, node
171 local can_replace = function(pos)
172 local node = minetest.get_node(pos).name
173 if minetest.registered_nodes[node] then
174 return minetest.registered_nodes[node].buildable_to
176 return false
179 for why = 0,height do
180 blank = blank + 1
181 if blank > 4 then blank = 1 end
182 why1 = stalk.y + why
183 for i = 1,9 do
184 ex = ex + 1
185 if ex > 3 then
186 zed = zed + 1
187 ex = 1
189 if c[i] == 1 then
190 node = "magicbeans_w:leaves"
191 if i == d[blank] or i == e[blank] then node = "magicbeans_w:blank" end
192 else
193 node = "magicbeans_w:stem"
195 ex1 = stalk.x + ex
196 zed1 = stalk.z + zed
197 local pos = {x=ex1, y=why1, z=zed1}
198 if can_replace(pos) then
199 minetest.set_node(pos,{name=node})
200 elseif node == "magicbeans_w:stem" then
201 -- Stop growing if stem hits an obstacle
202 return itemstack
205 zed = 0
207 -- Build cloud platform
208 for ex = -10,20 do
209 for zed = -10,20 do
210 ex1 = stalk.x + ex
211 zed1 = stalk.z + zed
212 local pos = {x=ex1, y=why1, z=zed1}
213 if can_replace(pos) then
214 minetest.set_node(pos,{name="magicbeans_w:cloud"})
216 end
217 end
219 itemstack:take_item()
221 return itemstack
222 end,
223 on_use = on_use,
228 -- Bean Spawning
229 minetest.register_abm(
230 {nodenames = {"default:dirt_with_grass"},
231 interval = 600,
232 chance = 3000,
233 action = function(pos)
234 pos.y = pos.y + 1
235 math.randomseed(os.time())
236 local j = math.random(4)
237 local bean = magicbeans_w_list[j][3]
238 minetest.add_item(pos, {name="magicbeans_w:"..bean})
239 end,
242 -- Remove beanstalk blanks
243 minetest.register_abm(
244 {nodenames = {"magicbeans_w:blank"},
245 interval = 5,
246 chance = 1,
247 action = function(pos)
248 local neighbors = {
249 { x=pos.x, y=pos.y, z=pos.z-1 },
250 { x=pos.x, y=pos.y, z=pos.z+1 },
251 { x=pos.x, y=pos.y-1, z=pos.z },
252 { x=pos.x, y=pos.y+1, z=pos.z },
253 { x=pos.x-1, y=pos.y, z=pos.z },
254 { x=pos.x+1, y=pos.y, z=pos.z },
256 local attached = false
257 for i=1,#neighbors do
258 local node = minetest.get_node_or_nil(neighbors[i])
259 if(node ~= nil) then
260 if node.name == "magicbeans_w:leaves" or node.name == "magicbeans_w:stem" then
261 attached = true
262 break
266 if(attached == false) then
267 minetest.remove_node(pos)