1 --plant growth time contants (in seconds)
5 minetest.register_plant = function(name,def)
13 nodename = "farming:"..name.."_"..i
15 nodename = "farming:"..name
23 local after_place_node
24 --do custom functions for each node
25 --wether growing in place or up
26 if def.grows == "up" then
27 after_dig_node = function(pos, node, metadata, digger)
28 if digger == nil then return end
29 local np = {x = pos.x, y = pos.y + 1, z = pos.z}
30 local nn = minetest.get_node(np)
31 if nn.name == node.name then
32 minetest.node_dig(np, nn, digger)
33 minetest.sound_play("dirt",{pos=pos,gain=0.2})
37 on_abm = function(pos)
38 if minetest.get_node_light(pos, nil) < 10 then
39 --print("failed to grow at "..dump(pos))
43 local found = minetest.find_node_near(pos, 3, {"main:water","main:waterflow"})
45 local noder = minetest.get_node(pos).name
46 local found_soil = minetest.get_item_group(noder, "soil") > 0
47 local found_self--[[this is deep]]= (noder == nodename)
49 if found and (found_soil or found_self) then
51 if minetest.get_node(pos).name == "air" then
52 minetest.set_node(pos,{name="farming:"..name})
54 elseif not found_self then
56 minetest.dig_node(pos)
57 minetest.sound_play("dirt",{pos=pos,gain=0.2})
61 after_place_node = function(pos, placer, itemstack, pointed_thing)
63 local noder = minetest.get_node(pos).name
64 local found = minetest.get_node_group(noder, "soil") > 0
67 minetest.dig_node(pos)
71 --for plants that grow in place
72 elseif def.grows == "in_place" then
73 on_abm = function(pos)
74 if minetest.get_node_light(pos, nil) < 10 then
75 minetest.dig_node(pos)
76 minetest.sound_play("dirt",{pos=pos,gain=0.2})
77 --print("failed to grow at "..dump(pos))
82 local found = minetest.get_node_group(minetest.get_node(pos).name, "farmland") > 0
83 --if found farmland below
87 minetest.set_node(pos,{name="farming:"..name.."_"..(i+1)})
89 --if not found farmland
91 minetest.dig_node(pos)
92 minetest.sound_play("dirt",{pos=pos,gain=0.2})
95 after_place_node = function(pos, placer, itemstack, pointed_thing)
97 local noder = minetest.get_node(pos).name
98 local found = minetest.get_node_group(noder, "farmland") > 0
101 minetest.dig_node(pos)
104 elseif def.grows == "in_place_yields" then
105 on_abm = function(pos)
106 if minetest.get_node_light(pos, nil) < 10 then
107 minetest.dig_node(pos)
108 minetest.sound_play("dirt",{pos=pos,gain=0.2})
109 --print("failed to grow at "..dump(pos))
113 local found = minetest.get_node_group(minetest.get_node(pos).name, "farmland") > 0
114 --if found farmland below
118 minetest.set_node(pos,{name="farming:"..name.."_"..(i+1)})
124 if found == false then
126 if math.abs(x)+math.abs(z) == 1 then
127 local node_get = minetest.get_node(vector.new(pos.x-x,pos.y,pos.z-z)).name == "air"
129 add_node = vector.new(pos.x-x,pos.y,pos.z-z)
137 if found == true and add_node then
138 local param2 = minetest.dir_to_facedir(vector.direction(pos,add_node))
139 minetest.add_node(add_node,{name=def.grown_node,param2=param2})
141 local facedir = minetest.facedir_to_dir(param2)
143 local inverted_facedir = vector.multiply(facedir,-1)
144 minetest.set_node(vector.add(inverted_facedir,add_node), {name="farming:"..name.."_complete", param2=minetest.dir_to_facedir(facedir)})
147 --if not found farmland
149 minetest.dig_node(pos)
150 minetest.sound_play("dirt",{pos=pos,gain=0.2})
153 after_place_node = function(pos, placer, itemstack, pointed_thing)
155 local noder = minetest.get_node(pos).name
156 local found = minetest.get_node_group(noder, "farmland") > 0
159 minetest.dig_node(pos)
164 --allow plants to only drop item at max stage
166 if i == max and def.grows ~= "in_place_yields" then
176 tiles = {def.tiles[1].."_"..i..".png"}
181 def.groups.plants = 1
183 minetest.register_node(nodename, {
184 description = def.description,
185 drawtype = def.drawtype,
187 inventory_image = def.inventory_image,
188 walkable = def.walkable,
189 climbable = def.climbable,
190 paramtype = def.paramtype,
192 paramtype2 = def.paramtype2,
193 buildable_to = def.buildable_to,
196 selection_box = def.selection_box,
198 sunlight_propagates = def.sunlight_propagates,
199 node_box = def.node_box,
200 node_placement_prediction = "",
201 is_ground_content = false,
205 on_flood = function(pos, oldnode, newnode)
206 minetest.dig_node(pos)
209 after_dig_node = after_dig_node,
210 after_place_node = after_place_node,
211 on_construct = on_construct,
212 after_destruct = after_destruct,
215 minetest.register_abm({
216 label = nodename.." Grow",
217 nodenames = {nodename},
222 action = function(pos)
229 --create final stage for grow in place plant stems that create food
230 if def.grows == "in_place_yields" then
231 minetest.register_node("farming:"..name.."_complete", {
232 description = def.stem_description,
233 tiles = def.stem_tiles,
234 drawtype = def.stem_drawtype,
235 walkable = def.stem_walkable,
236 sunlight_propagates = def.stem_sunlight_propagates,
237 paramtype = def.stem_paramtype,
238 drop = def.stem_drop,
239 groups = def.stem_groups,
240 sounds = def.stem_sounds,
241 node_box = def.stem_node_box,
242 selection_box = def.stem_selection_box,
243 paramtype2 = "facedir",
245 minetest.register_node("farming:"..def.fruit_name, {
246 description = def.fruit_description,
247 tiles = def.fruit_tiles,
248 groups = def.fruit_groups,
249 sounds = def.fruit_sounds,
250 drop = def.fruit_drop,
251 --this is hardcoded to work no matter what
252 paramtype2 = "facedir",
253 after_destruct = function(pos,oldnode)
254 local facedir = oldnode.param2
255 facedir = minetest.facedir_to_dir(facedir)
256 local dir = vector.multiply(facedir,-1)
257 local stem_pos = vector.add(dir,pos)
259 if minetest.get_node(stem_pos).name == "farming:"..name.."_complete" then
260 minetest.set_node(stem_pos, {name = "farming:"..name.."_1"})
266 if def.seed_name then
267 minetest.register_craftitem("farming:"..def.seed_name.."_seeds", {
268 description = def.seed_description,
269 inventory_image = def.seed_inventory_image,
270 on_place = function(itemstack, placer, pointed_thing)
271 if pointed_thing.type ~= "node" then
274 local pointed_thing_diff = pointed_thing.above.y - pointed_thing.under.y
276 if pointed_thing_diff < 1 then return end
278 if minetest.get_node(pointed_thing.above).name ~= "air" then return end
279 local pb = pointed_thing.above
280 if minetest.get_node_group(minetest.get_node(vector.new(pb.x,pb.y-1,pb.z)).name, "farmland") == 0 or minetest.get_node(pointed_thing.above).name ~= "air" then
284 local wdir = minetest.dir_to_wallmounted(vector.subtract(pointed_thing.under,pointed_thing.above))
286 local fakestack = itemstack
289 retval = fakestack:set_name(def.seed_plants)
294 itemstack, retval = minetest.item_place(fakestack, placer, pointed_thing, wdir)
295 itemstack:set_name("farming:"..def.seed_name.."_seeds")
298 minetest.sound_play("leaves", {pos=pointed_thing.above, gain = 1.0})