Remove the climbable blank beanstalk blocks if the beanstalk around it is gone
[minetest_magicbeans_w.git] / init.lua
blob7b58eb316911e26ebb23a55ddfb1f31100cac158
1 -- get intllib optionally
2 if (minetest.get_modpath("intllib")) then
3 dofile(minetest.get_modpath("intllib").."/intllib.lua")
4 S = intllib.Getter(minetest.get_current_modname())
5 else
6 S = function ( s ) return s end
7 end
9 --Register beanstalk nodes
10 minetest.register_node("magicbeans_w:leaves", {
11 description = S("magic beanstalk leaves"),
12 tiles = {"magicbeans_w_leaves.png"},
13 inventory_image = "magicbeans_w_leaves.png",
14 drawtype = "plantlike",
15 walkable = false,
16 climbable = true,
17 paramtype = "light",
18 groups = {leaves = 1, snappy = 3, flammable = 2},
21 minetest.register_node("magicbeans_w:blank", {
22 description = S("magic beanstalk climbable blank"),
23 drawtype = "airlike",
24 walkable = false,
25 climbable = true,
26 paramtype = "light",
27 buildable_to = true,
28 pointable = false,
29 diggable = false,
30 drop = "",
31 groups = {not_in_creative_inventory = 1},
34 minetest.register_node("magicbeans_w:stem", {
35 description = S("magic beanstalk stem"),
36 tiles = {"magicbeans_w_stem.png"},
37 paramtype = "light",
38 groups = {oddly_breakable_by_hand = 1, choppy = 3, flammable = 2},
41 minetest.register_node("magicbeans_w:cloud", {
42 description = S("cloud"),
43 tiles = {"default_cloud.png"},
44 walkable = false,
45 climbable = true,
46 paramtype = "light",
47 sunlight_propagates = true,
48 groups = {dig_immediate = 3},
51 magicbeans_w_list = {
52 { S("magic jumping bean"), S("Magic super jump"), "jumping", nil, 5, nil, {"jump"}},
53 { S("magic flying bean"), S("Magic flying"), "flying", 2, nil, 0.02, {"speed", "gravity"}},
54 { S("magic running bean"), S("Magic super speed"), "running", 3, nil, nil, {"speed"}},
55 { S("magic beanstalk bean"), nil, "beanstalk", nil, nil, nil, nil},
58 for i in ipairs(magicbeans_w_list) do
60 local beandesc = magicbeans_w_list[i][1]
61 local effdesc = magicbeans_w_list[i][2]
62 local bean = magicbeans_w_list[i][3]
63 local beanspeed = magicbeans_w_list[i][4]
64 local beanjump = magicbeans_w_list[i][5]
65 local beangrav = magicbeans_w_list[i][6]
66 local beangroups = magicbeans_w_list[i][7]
68 --Register effect types
69 if(beangroups ~= nil) then
70 playereffects.register_effect_type("bean_"..bean, effdesc, beangroups,
71 function(player)
72 player:set_physics_override({speed=beanspeed, jump=beanjump, gravity=beangrav})
73 end,
74 function(effect)
75 local player = minetest.get_player_by_name(effect.playername)
76 local speed, jump, grav
77 if beanspeed ~= nil then speed = 1 end
78 if beanjump ~= nil then jump = 1 end
79 if beangrav ~= nil then grav = 1 end
80 player:set_physics_override({speed=speed, jump=jump, gravity=grav})
81 end
83 end
84 --Register beans
85 minetest.register_craftitem("magicbeans_w:"..bean, {
86 description = beandesc,
87 inventory_image = "magicbeans_w_"..bean..".png",
88 on_place = function(itemstack, placer, pointed_thing)
89 if pointed_thing.above then
90 if bean ~= "beanstalk" then
91 minetest.env:add_item(pointed_thing.above, {name="magicbeans_w:"..bean})
92 else
93 -- Grow Beanstalk
94 minetest.chat_send_player(placer:get_player_name(),S("It's gonna grow - wait for it!"))
95 math.randomseed(os.time())
96 local stalk = pointed_thing.above
97 stalk.x = stalk.x - 2
98 stalk.z = stalk.z - 2
99 local height = 127 - stalk.y
100 local c = {1, 1, 1, 1, 2, 1, 1, 1, 1}
101 local d = {1, 2, 3, 6}
102 local e = {9, 8, 7, 4}
103 local ex = 0
104 local zed = 1
105 local blank = 0
106 for why = 0,height do
107 blank = blank + 1
108 if blank > 4 then blank = 1 end
109 why1 = stalk.y + why
110 for i = 1,9 do
111 ex = ex + 1
112 if ex > 3 then
113 zed = zed + 1
114 ex = 1
116 if c[i] == 1 then
117 node = "magicbeans_w:leaves"
118 if i == d[blank] or i == e[blank] then node = "magicbeans_w:blank" end
119 else
120 node = "magicbeans_w:stem"
122 ex1 = stalk.x + ex
123 zed1 = stalk.z + zed
124 minetest.set_node({x=ex1, y=why1, z=zed1},{name=node})
126 zed = 0
128 -- Build cloud platform
129 for ex = -10,20 do
130 for zed = -10,20 do
131 ex1 = stalk.x + ex
132 zed1 = stalk.z + zed
133 minetest.set_node({x=ex1, y=why1, z=zed1},{name="magicbeans_w:cloud"})
134 end
135 end
137 itemstack:take_item()
139 return itemstack
140 end,
141 on_use = function(itemstack, user, pointed_thing)
142 if bean == "beanstalk" then
143 minetest.chat_send_player(user:get_player_name(),S("You can't eat magic beanstalk beans - you have to plant them."))
144 return
146 playereffects.apply_effect_type("bean_"..bean, 30, user)
147 itemstack:take_item()
148 return itemstack
149 end,
154 -- Bean Spawning
155 minetest.register_abm(
156 {nodenames = {"default:dirt_with_grass"},
157 interval = 600,
158 chance = 3000,
159 action = function(pos)
160 pos.y = pos.y + 1
161 math.randomseed(os.time())
162 local j = math.random(4)
163 local bean = magicbeans_w_list[j][3]
164 minetest.env:add_item(pos, {name="magicbeans_w:"..bean})
165 end,
168 -- Remove beanstalk blanks
169 minetest.register_abm(
170 {nodenames = {"magicbeans_w:blank"},
171 interval = 5,
172 chance = 1,
173 action = function(pos)
174 local neighbors = {
175 { x=pos.x, y=pos.y, z=pos.z-1 },
176 { x=pos.x, y=pos.y, z=pos.z+1 },
177 { x=pos.x, y=pos.y-1, z=pos.z },
178 { x=pos.x, y=pos.y+1, z=pos.z },
179 { x=pos.x-1, y=pos.y, z=pos.z },
180 { x=pos.x+1, y=pos.y, z=pos.z },
182 local attached = false
183 for i=1,#neighbors do
184 local node = minetest.get_node_or_nil(neighbors[i])
185 if(node ~= nil) then
186 if node.name == "magicbeans_w:leaves" or node.name == "magicbean_w:stem" then
187 attached = true
188 break
192 if(attached == false) then
193 minetest.remove_node(pos)