Magic beanstalk bean only grows on soil
[minetest_magicbeans_w.git] / init.lua
blob446e71f0fc76c898eb1162ddb9457a725adada61
1 -- get intllib optionally
2 local S
3 if (minetest.get_modpath("intllib")) then
4 dofile(minetest.get_modpath("intllib").."/intllib.lua")
5 S = intllib.Getter(minetest.get_current_modname())
6 else
7 S = function ( s ) return s end
8 end
9 math.randomseed(os.time())
11 --Register beanstalk nodes
12 minetest.register_node("magicbeans_w:leaves", {
13 description = S("Magic beanstalk leaves"),
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 x_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 tiles = {"magicbeans_w_stem_topbottom.png", "magicbeans_w_stem_topbottom.png", "magicbeans_w_stem.png"},
45 paramtype2 = "facedir",
46 groups = {oddly_breakable_by_hand = 1, choppy = 3, flammable = 2},
47 sounds = default.node_sound_wood_defaults(),
48 on_place = minetest.rotate_node,
51 minetest.register_node("magicbeans_w:cloud", {
52 description = S("Thin Cloud"),
53 drawtype = "liquid",
54 paramtype = "light",
55 tiles = {"default_cloud.png"},
56 walkable = false,
57 climbable = true,
58 sunlight_propagates = true,
59 post_effect_color = "#FFFFFFA0",
60 alpha = 192,
61 groups = {dig_immediate = 3},
62 sounds = { dig = { name = "", gain = 0 }, footstep = { name = "", gain = 0 },
63 dug = { name = "", gain = 0 }, player = { name = "", gain = 0 } },
64 drop = "",
67 if(minetest.get_modpath("awards") ~= nil) then
68 awards.register_achievement("magicbeans_w_beanstalk", {
69 title = S("Sam and the Beanstalk"),
70 description = S("Climb up a magic beanstalk and touch the clouds."),
71 icon = "magicbeans_w_stem.png",
72 trigger = {
73 type = "dig",
74 node = "magicbeans_w:cloud",
75 target = 1,
78 end
80 local magicbeans_w_list = {
81 { S("Magic jumping bean"), S("Magic super jump"), "jumping", nil, 5, nil, {"jump"},
82 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."),
83 doc.sub.items.temp.eat, },
84 { S("Magic flying bean"), S("Magic flying"), "flying", 2, nil, 0.02, {"speed", "gravity"},
85 S("Eating a running bean will greatly increase your walking speed for 30 seconds. It also cancels out any previous walking speed effects."),
86 doc.sub.items.temp.eat, },
87 { S("Magic running bean"), S("Magic super speed"), "running", 3, nil, nil, {"speed"},
88 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."),
89 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."), },
90 { S("Magic beanstalk bean"), nil, "beanstalk", nil, nil, nil, nil,
91 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."),
92 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."),
96 for i in ipairs(magicbeans_w_list) do
98 local beandesc = magicbeans_w_list[i][1]
99 local effdesc = magicbeans_w_list[i][2]
100 local bean = magicbeans_w_list[i][3]
101 local beanspeed = magicbeans_w_list[i][4]
102 local beanjump = magicbeans_w_list[i][5]
103 local beangrav = magicbeans_w_list[i][6]
104 local beangroups = magicbeans_w_list[i][7]
105 local longdesc = magicbeans_w_list[i][8]
106 local usagehelp = magicbeans_w_list[i][9]
108 --Register effect types
109 if(beangroups ~= nil) then
110 playereffects.register_effect_type("bean_"..bean, effdesc, "magicbeans_w_"..bean.."16.png", beangroups,
111 function(player)
112 player:set_physics_override({speed=beanspeed, jump=beanjump, gravity=beangrav})
113 end,
114 function(effect)
115 local player = minetest.get_player_by_name(effect.playername)
116 local speed, jump, grav
117 if beanspeed ~= nil then speed = 1 end
118 if beanjump ~= nil then jump = 1 end
119 if beangrav ~= nil then grav = 1 end
120 player:set_physics_override({speed=speed, jump=jump, gravity=grav})
124 --Register beans
125 minetest.register_craftitem("magicbeans_w:"..bean, {
126 description = beandesc,
127 x_doc_items_longdesc = longdesc,
128 x_doc_items_usagehelp = usagehelp,
129 inventory_image = "magicbeans_w_"..bean..".png",
130 on_place = function(itemstack, placer, pointed_thing)
131 if pointed_thing.type == "node" then
132 if bean ~= "beanstalk" then
133 minetest.add_item(pointed_thing.above, {name="magicbeans_w:"..bean})
134 else
135 -- Grow Beanstalk
136 local stalk = pointed_thing.above
137 local floor = pointed_thing.under
138 if minetest.get_item_group(minetest.get_node(floor).name, "soil") == 0 then
139 return itemstack
141 stalk.x = stalk.x - 2
142 stalk.z = stalk.z - 2
143 local height = 127 - stalk.y
144 local c = {1, 1, 1, 1, 2, 1, 1, 1, 1}
145 local d = {1, 2, 3, 6}
146 local e = {9, 8, 7, 4}
147 local ex = 0
148 local zed = 1
149 local blank = 0
150 local why1, ex1, zed1, node
151 for why = 0,height do
152 blank = blank + 1
153 if blank > 4 then blank = 1 end
154 why1 = stalk.y + why
155 for i = 1,9 do
156 ex = ex + 1
157 if ex > 3 then
158 zed = zed + 1
159 ex = 1
161 if c[i] == 1 then
162 node = "magicbeans_w:leaves"
163 if i == d[blank] or i == e[blank] then node = "magicbeans_w:blank" end
164 else
165 node = "magicbeans_w:stem"
167 ex1 = stalk.x + ex
168 zed1 = stalk.z + zed
169 minetest.set_node({x=ex1, y=why1, z=zed1},{name=node})
171 zed = 0
173 -- Build cloud platform
174 for ex = -10,20 do
175 for zed = -10,20 do
176 ex1 = stalk.x + ex
177 zed1 = stalk.z + zed
178 minetest.set_node({x=ex1, y=why1, z=zed1},{name="magicbeans_w:cloud"})
179 end
180 end
182 itemstack:take_item()
184 return itemstack
185 end,
186 on_use = function(itemstack, user, pointed_thing)
187 if bean == "beanstalk" then
188 -- Beanstalk bean can't be eaten
189 return
191 playereffects.apply_effect_type("bean_"..bean, 30, user)
192 itemstack:take_item()
193 return itemstack
194 end,
199 -- Bean Spawning
200 minetest.register_abm(
201 {nodenames = {"default:dirt_with_grass"},
202 interval = 600,
203 chance = 3000,
204 action = function(pos)
205 pos.y = pos.y + 1
206 math.randomseed(os.time())
207 local j = math.random(4)
208 local bean = magicbeans_w_list[j][3]
209 minetest.add_item(pos, {name="magicbeans_w:"..bean})
210 end,
213 -- Remove beanstalk blanks
214 minetest.register_abm(
215 {nodenames = {"magicbeans_w:blank"},
216 interval = 5,
217 chance = 1,
218 action = function(pos)
219 local neighbors = {
220 { x=pos.x, y=pos.y, z=pos.z-1 },
221 { x=pos.x, y=pos.y, z=pos.z+1 },
222 { x=pos.x, y=pos.y-1, z=pos.z },
223 { x=pos.x, y=pos.y+1, z=pos.z },
224 { x=pos.x-1, y=pos.y, z=pos.z },
225 { x=pos.x+1, y=pos.y, z=pos.z },
227 local attached = false
228 for i=1,#neighbors do
229 local node = minetest.get_node_or_nil(neighbors[i])
230 if(node ~= nil) then
231 if node.name == "magicbeans_w:leaves" or node.name == "magicbeans_w:stem" then
232 attached = true
233 break
237 if(attached == false) then
238 minetest.remove_node(pos)