Add optional compability with [playerphysics]
[minetest_magicbeans_w.git] / init.lua
blob0679048be24f58c9cb7e1112c3288817a06e50d1
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 _doc_items_longdesc = S("Leaves from a giant magic beanstalk, a plant which grows high into the sky. It is grown from a magic beanstalk bean. Can you climb to the top?"),
14 tiles = {"magicbeans_w_leaves.png"},
15 inventory_image = "magicbeans_w_leaves.png",
16 drawtype = "plantlike",
17 is_ground_content = false,
18 walkable = false,
19 climbable = true,
20 paramtype = "light",
21 groups = {leaves = 1, snappy = 3, flammable = 2},
22 sounds = default.node_sound_leaves_defaults()
25 minetest.register_node("magicbeans_w:blank", {
26 description = S("Magic beanstalk climbable blank"),
27 _doc_items_create_entry = false,
28 drawtype = "airlike",
29 inventory_image = "unknown_node.png",
30 wield_image = "unknown_node.png",
31 walkable = false,
32 climbable = true,
33 paramtype = "light",
34 buildable_to = true,
35 pointable = false,
36 diggable = false,
37 drop = "",
38 groups = {not_in_creative_inventory = 1},
41 minetest.register_node("magicbeans_w:stem", {
42 description = S("Magic beanstalk stem"),
43 is_ground_content = false,
44 _doc_items_longdesc = S("A stem from a giant magic beanstalk, a plant which grows high into the sky. It is grown from a magic beanstalk bean."),
45 tiles = {"magicbeans_w_stem_topbottom.png", "magicbeans_w_stem_topbottom.png", "magicbeans_w_stem.png"},
46 paramtype2 = "facedir",
47 groups = {oddly_breakable_by_hand = 1, choppy = 3, flammable = 2},
48 sounds = default.node_sound_wood_defaults(),
49 on_place = minetest.rotate_node,
52 minetest.register_craft({
53 type="fuel",
54 recipe = "magicbeans_w:stem",
55 burntime = 21,
58 minetest.register_node("magicbeans_w:cloud", {
59 description = S("Thin Cloud"),
60 _doc_items_longdesc = S("Thin clouds are rather unstable, are destroyed by a single punch and can't be collected. You can walk, climb and build on them, but be careful not to accidentaly fall through, especially when you drop on these."),
61 paramtype = "light",
62 tiles = {"default_cloud.png"},
63 walkable = false,
64 climbable = true,
65 sunlight_propagates = true,
66 post_effect_color = "#FFFFFFA0",
67 groups = {dig_immediate = 3, not_in_creative_inventory = 1},
68 sounds = { dig = { name = "", gain = 0 }, footstep = { name = "", gain = 0 },
69 dug = { name = "", gain = 0 }, player = { name = "", gain = 0 } },
70 drop = "",
73 if(minetest.get_modpath("awards") ~= nil) then
74 awards.register_achievement("magicbeans_w_beanstalk", {
75 title = S("Sam and the Beanstalk"),
76 description = S("Climb up a magic beanstalk and touch the clouds."),
77 icon = "magicbeans_w_stem.png",
78 trigger = {
79 type = "dig",
80 node = "magicbeans_w:cloud",
81 target = 1,
84 end
86 local eat
87 if minetest.get_modpath("doc_items") then
88 eat = doc.sub.items.temp.eat
89 end
91 local magicbeans_w_list = {
92 { S("Magic jumping bean"), S("Magic super jump"), "jumping", nil, 5, nil, {"jump"},
93 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."),
94 eat, },
95 { S("Magic flying bean"), S("Magic flying"), "flying", 2, nil, 0.02, {"speed", "gravity"},
96 S("Eating a flying bean will allow you to fly for 30 seconds, after which your flight will abruptly end. Use with caution. It also cancels out any gravity and walking speed effects you have."),
97 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."), },
98 { S("Magic running bean"), S("Magic super speed"), "running", 3, nil, nil, {"speed"},
99 S("Eating a running bean will greatly increase your walking speed for 30 seconds. It also cancels out any previous walking speed effects."),
100 eat, },
101 { S("Magic beanstalk bean"), nil, "beanstalk", nil, nil, nil, nil,
102 S("This magic bean will grow into a giant beanstalk when planted. The beanstalk can grow up to a height of about 100 blocks, easily reaching into the clouds."),
103 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."),
107 local pp_mod = minetest.get_modpath("playerphysics")
109 for i in ipairs(magicbeans_w_list) do
111 local beandesc = magicbeans_w_list[i][1]
112 local effdesc = magicbeans_w_list[i][2]
113 local bean = magicbeans_w_list[i][3]
114 local beanspeed = magicbeans_w_list[i][4]
115 local beanjump = magicbeans_w_list[i][5]
116 local beangrav = magicbeans_w_list[i][6]
117 local beangroups = magicbeans_w_list[i][7]
118 local longdesc = magicbeans_w_list[i][8]
119 local usagehelp = magicbeans_w_list[i][9]
121 --Register effect types
122 if(beangroups ~= nil) then
123 playereffects.register_effect_type("bean_"..bean, effdesc, "magicbeans_w_"..bean.."16.png", beangroups,
124 function(player)
125 if pp_mod then
126 if beanspeed ~= nil then
127 playerphysics.add_physics_factor(player, "speed", "magicbeans_w:speed", beanspeed)
129 if beanjump ~= nil then
130 playerphysics.add_physics_factor(player, "jump", "magicbeans_w:jump", beanjump)
132 if beangrav ~= nil then
133 playerphysics.add_physics_factor(player, "gravity", "magicbeans_w:gravity", beangrav)
135 else
136 player:set_physics_override({speed=beanspeed, jump=beanjump, gravity=beangrav})
138 end,
139 function(effect)
140 local player = minetest.get_player_by_name(effect.playername)
141 if pp_mod then
142 playerphysics.remove_physics_factor(player, "speed", "magicbeans_w:speed")
143 playerphysics.remove_physics_factor(player, "jump", "magicbeans_w:jump")
144 playerphysics.remove_physics_factor(player, "gravity", "magicbeans_w:gravity")
145 else
146 local speed, jump, grav
147 if beanspeed ~= nil then speed = 1 end
148 if beanjump ~= nil then jump = 1 end
149 if beangrav ~= nil then grav = 1 end
150 player:set_physics_override({speed=speed, jump=jump, gravity=grav})
156 local on_use = nil
157 if bean ~= "beanstalk" then
158 on_use = function(itemstack, user, pointed_thing)
159 playereffects.apply_effect_type("bean_"..bean, 30, user)
160 itemstack:take_item()
161 return itemstack
164 --Register beans
165 minetest.register_craftitem("magicbeans_w:"..bean, {
166 description = beandesc,
167 _doc_items_longdesc = longdesc,
168 _doc_items_usagehelp = usagehelp,
169 inventory_image = "magicbeans_w_"..bean..".png",
170 on_place = function(itemstack, placer, pointed_thing)
171 if pointed_thing.type == "node" then
172 if bean ~= "beanstalk" then
173 minetest.add_item(pointed_thing.above, {name="magicbeans_w:"..bean})
174 else
175 -- Grow Beanstalk
176 local stalk = pointed_thing.above
177 local floor = pointed_thing.under
178 if minetest.get_item_group(minetest.get_node(floor).name, "soil") == 0 then
179 return itemstack
181 stalk.x = stalk.x - 2
182 stalk.z = stalk.z - 2
183 local height = 127 - stalk.y
184 local c = {1, 1, 1, 1, 2, 1, 1, 1, 1}
185 local d = {1, 2, 3, 6}
186 local e = {9, 8, 7, 4}
187 local ex = 0
188 local zed = 1
189 local blank = 0
190 local why1, ex1, zed1, node
192 local can_replace = function(pos)
193 local node = minetest.get_node(pos).name
194 if minetest.registered_nodes[node] then
195 return minetest.registered_nodes[node].buildable_to
197 return false
200 for why = 0,height do
201 blank = blank + 1
202 if blank > 4 then blank = 1 end
203 why1 = stalk.y + why
204 for i = 1,9 do
205 ex = ex + 1
206 if ex > 3 then
207 zed = zed + 1
208 ex = 1
210 if c[i] == 1 then
211 node = "magicbeans_w:leaves"
212 if i == d[blank] or i == e[blank] then node = "magicbeans_w:blank" end
213 else
214 node = "magicbeans_w:stem"
216 ex1 = stalk.x + ex
217 zed1 = stalk.z + zed
218 local pos = {x=ex1, y=why1, z=zed1}
219 if can_replace(pos) then
220 minetest.set_node(pos,{name=node})
221 elseif node == "magicbeans_w:stem" then
222 -- Stop growing if stem hits an obstacle
223 return itemstack
226 zed = 0
228 -- Build cloud platform
229 for ex = -10,20 do
230 for zed = -10,20 do
231 ex1 = stalk.x + ex
232 zed1 = stalk.z + zed
233 local pos = {x=ex1, y=why1, z=zed1}
234 if can_replace(pos) then
235 minetest.set_node(pos,{name="magicbeans_w:cloud"})
237 end
238 end
240 itemstack:take_item()
242 return itemstack
243 end,
244 on_use = on_use,
249 -- Bean Spawning
250 minetest.register_abm(
251 {nodenames = {"default:dirt_with_grass"},
252 interval = 600,
253 chance = 3000,
254 action = function(pos)
255 pos.y = pos.y + 1
256 math.randomseed(os.time())
257 local j = math.random(4)
258 local bean = magicbeans_w_list[j][3]
259 minetest.add_item(pos, {name="magicbeans_w:"..bean})
260 end,
263 -- Remove beanstalk blanks
264 minetest.register_abm(
265 {nodenames = {"magicbeans_w:blank"},
266 interval = 5,
267 chance = 1,
268 action = function(pos)
269 local neighbors = {
270 { x=pos.x, y=pos.y, z=pos.z-1 },
271 { x=pos.x, y=pos.y, z=pos.z+1 },
272 { x=pos.x, y=pos.y-1, z=pos.z },
273 { x=pos.x, y=pos.y+1, z=pos.z },
274 { x=pos.x-1, y=pos.y, z=pos.z },
275 { x=pos.x+1, y=pos.y, z=pos.z },
277 local attached = false
278 for i=1,#neighbors do
279 local node = minetest.get_node_or_nil(neighbors[i])
280 if(node ~= nil) then
281 if node.name == "magicbeans_w:leaves" or node.name == "magicbeans_w:stem" then
282 attached = true
283 break
287 if(attached == false) then
288 minetest.remove_node(pos)