Replace minetest.env: with minetest.
[oweals/minetest_game.git] / mods / farming / init.lua
1 -- Minetest 0.4 mod: farming
2 -- See README.txt for licensing and other information.
3
4 --
5 -- Soil
6 --
7 minetest.register_node("farming:soil", {
8         description = "Soil",
9         tiles = {"farming_soil.png", "default_dirt.png"},
10         drop = "default:dirt",
11         is_ground_content = true,
12         groups = {crumbly=3, not_in_creative_inventory=1, soil=2},
13         sounds = default.node_sound_dirt_defaults(),
14 })
15
16 minetest.register_node("farming:soil_wet", {
17         description = "Wet Soil",
18         tiles = {"farming_soil_wet.png", "farming_soil_wet_side.png"},
19         drop = "default:dirt",
20         is_ground_content = true,
21         groups = {crumbly=3, not_in_creative_inventory=1, soil=3},
22         sounds = default.node_sound_dirt_defaults(),
23 })
24
25 minetest.register_abm({
26         nodenames = {"farming:soil", "farming:soil_wet"},
27         interval = 15,
28         chance = 4,
29         action = function(pos, node)
30                 pos.y = pos.y+1
31                 local nn = minetest.get_node(pos).name
32                 pos.y = pos.y-1
33                 if minetest.registered_nodes[nn] and minetest.registered_nodes[nn].walkable then
34                         minetest.set_node(pos, {name="default:dirt"})
35                 end
36                 -- check if there is water nearby
37                 if minetest.find_node_near(pos, 3, {"group:water"}) then
38                         -- if it is dry soil turn it into wet soil
39                         if node.name == "farming:soil" then
40                                 minetest.set_node(pos, {name="farming:soil_wet"})
41                         end
42                 else
43                         -- turn it back into dirt if it is already dry
44                         if node.name == "farming:soil" then
45                                 -- only turn it back if there is no plant on top of it
46                                 if minetest.get_item_group(nn, "plant") == 0 then
47                                         minetest.set_node(pos, {name="default:dirt"})
48                                 end
49                                 
50                         -- if its wet turn it back into dry soil
51                         elseif node.name == "farming:soil_wet" then
52                                 minetest.set_node(pos, {name="farming:soil"})
53                         end
54                 end
55         end,
56 })
57
58 --
59 -- Hoes
60 --
61 -- turns nodes with group soil=1 into soil; drop seeds if plowing grass
62 local function hoe_on_use(itemstack, user, pointed_thing, uses)
63         local pt = pointed_thing
64         -- check if pointing at a node
65         if not pt then
66                 return
67         end
68         if pt.type ~= "node" then
69                 return
70         end
71         
72         local under = minetest.get_node(pt.under)
73         local p = {x=pt.under.x, y=pt.under.y+1, z=pt.under.z}
74         local above = minetest.get_node(p)
75         
76         -- return if any of the nodes is not registered
77         if not minetest.registered_nodes[under.name] then
78                 return
79         end
80         if not minetest.registered_nodes[above.name] then
81                 return
82         end
83         
84         -- check if the node above the pointed thing is air
85         if above.name ~= "air" then
86                 return
87         end
88         
89         -- check if pointing at dirt
90         if minetest.get_item_group(under.name, "soil") ~= 1 then
91                 return
92         end
93         
94         -- turn the node into soil, wear out item and play sound
95         minetest.set_node(pt.under, {name="farming:soil"})
96         minetest.sound_play("default_dig_crumbly", {
97                 pos = pt.under,
98                 gain = 0.5,
99         })
100         itemstack:add_wear(65535/(uses-1))
101         return itemstack
102 end
103
104 minetest.register_tool("farming:hoe_wood", {
105         description = "Wooden Hoe",
106         inventory_image = "farming_tool_woodhoe.png",
107         
108         on_use = function(itemstack, user, pointed_thing)
109                 return hoe_on_use(itemstack, user, pointed_thing, 30)
110         end,
111 })
112
113 minetest.register_tool("farming:hoe_stone", {
114         description = "Stone Hoe",
115         inventory_image = "farming_tool_stonehoe.png",
116         
117         on_use = function(itemstack, user, pointed_thing)
118                 return hoe_on_use(itemstack, user, pointed_thing, 90)
119         end,
120 })
121
122 minetest.register_tool("farming:hoe_steel", {
123         description = "Steel Hoe",
124         inventory_image = "farming_tool_steelhoe.png",
125         
126         on_use = function(itemstack, user, pointed_thing)
127                 return hoe_on_use(itemstack, user, pointed_thing, 200)
128         end,
129 })
130
131 minetest.register_tool("farming:hoe_bronze", {
132         description = "Bronze Hoe",
133         inventory_image = "farming_tool_bronzehoe.png",
134         
135         on_use = function(itemstack, user, pointed_thing)
136                 return hoe_on_use(itemstack, user, pointed_thing, 220)
137         end,
138 })
139
140 minetest.register_craft({
141         output = "farming:hoe_wood",
142         recipe = {
143                 {"group:wood", "group:wood"},
144                 {"", "default:stick"},
145                 {"", "default:stick"},
146         }
147 })
148
149 minetest.register_craft({
150         output = "farming:hoe_stone",
151         recipe = {
152                 {"group:stone", "group:stone"},
153                 {"", "default:stick"},
154                 {"", "default:stick"},
155         }
156 })
157
158 minetest.register_craft({
159         output = "farming:hoe_steel",
160         recipe = {
161                 {"default:steel_ingot", "default:steel_ingot"},
162                 {"", "default:stick"},
163                 {"", "default:stick"},
164         }
165 })
166
167 minetest.register_craft({
168         output = "farming:hoe_bronze",
169         recipe = {
170                 {"default:bronze_ingot", "default:bronze_ingot"},
171                 {"", "default:stick"},
172                 {"", "default:stick"},
173         }
174 })
175
176 --
177 -- Override grass for drops
178 --
179 minetest.register_node(":default:grass_1", {
180         description = "Grass",
181         drawtype = "plantlike",
182         tiles = {"default_grass_1.png"},
183         -- use a bigger inventory image
184         inventory_image = "default_grass_3.png",
185         wield_image = "default_grass_3.png",
186         paramtype = "light",
187         walkable = false,
188         buildable_to = true,
189         drop = {
190                 max_items = 1,
191                 items = {
192                         {items = {'farming:seed_wheat'},rarity = 20},
193                         {items = {'default:grass_1'}},
194                 }
195         },
196         groups = {snappy=3,flammable=3,flora=1,attached_node=1},
197         sounds = default.node_sound_leaves_defaults(),
198         selection_box = {
199                 type = "fixed",
200                 fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
201         },
202         on_place = function(itemstack, placer, pointed_thing)
203                 -- place a random grass node
204                 local stack = ItemStack("default:grass_"..math.random(1,5))
205                 local ret = minetest.item_place(stack, placer, pointed_thing)
206                 return ItemStack("default:grass_1 "..itemstack:get_count()-(1-ret:get_count()))
207         end,
208 })
209
210 for i=2,5 do
211         minetest.register_node(":default:grass_"..i, {
212                 description = "Grass",
213                 drawtype = "plantlike",
214                 tiles = {"default_grass_"..i..".png"},
215                 inventory_image = "default_grass_"..i..".png",
216                 wield_image = "default_grass_"..i..".png",
217                 paramtype = "light",
218                 walkable = false,
219                 buildable_to = true,
220                 is_ground_content = true,
221                 drop = {
222                         max_items = 1,
223                         items = {
224                                 {items = {'farming:seed_wheat'},rarity = 20},
225                                 {items = {'default:grass_1'}},
226                         }
227                 },
228                 groups = {snappy=3,flammable=3,flora=1,attached_node=1,not_in_creative_inventory=1},
229                 sounds = default.node_sound_leaves_defaults(),
230                 selection_box = {
231                         type = "fixed",
232                         fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
233                 },
234         })
235 end
236
237 minetest.register_node(":default:junglegrass", {
238         description = "Jungle Grass",
239         drawtype = "plantlike",
240         visual_scale = 1.3,
241         tiles = {"default_junglegrass.png"},
242         inventory_image = "default_junglegrass.png",
243         wield_image = "default_junglegrass.png",
244         paramtype = "light",
245         walkable = false,
246         buildable_to = true,
247         is_ground_content = true,
248         drop = {
249                 max_items = 1,
250                 items = {
251                         {items = {'farming:seed_cotton'},rarity = 20},
252                         {items = {'default:junglegrass'}},
253                 }
254         },
255         groups = {snappy=3,flammable=2,flora=1,attached_node=1},
256         sounds = default.node_sound_leaves_defaults(),
257         selection_box = {
258                 type = "fixed",
259                 fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
260         },
261 })
262
263 --
264 -- Place seeds
265 --
266 local function place_seed(itemstack, placer, pointed_thing, plantname)
267         local pt = pointed_thing
268         -- check if pointing at a node
269         if not pt then
270                 return
271         end
272         if pt.type ~= "node" then
273                 return
274         end
275         
276         local under = minetest.get_node(pt.under)
277         local above = minetest.get_node(pt.above)
278         
279         -- return if any of the nodes is not registered
280         if not minetest.registered_nodes[under.name] then
281                 return
282         end
283         if not minetest.registered_nodes[above.name] then
284                 return
285         end
286         
287         -- check if pointing at the top of the node
288         if pt.above.y ~= pt.under.y+1 then
289                 return
290         end
291         
292         -- check if you can replace the node above the pointed node
293         if not minetest.registered_nodes[above.name].buildable_to then
294                 return
295         end
296         
297         -- check if pointing at soil
298         if minetest.get_item_group(under.name, "soil") <= 1 then
299                 return
300         end
301         
302         -- add the node and remove 1 item from the itemstack
303         minetest.add_node(pt.above, {name=plantname})
304         if not minetest.setting_getbool("creative_mode") then
305                 itemstack:take_item()
306         end
307         return itemstack
308 end
309
310 --
311 -- Wheat
312 --
313 minetest.register_craftitem("farming:seed_wheat", {
314         description = "Wheat Seed",
315         inventory_image = "farming_wheat_seed.png",
316         on_place = function(itemstack, placer, pointed_thing)
317                 return place_seed(itemstack, placer, pointed_thing, "farming:wheat_1")
318         end,
319 })
320
321 minetest.register_craftitem("farming:wheat", {
322         description = "Wheat",
323         inventory_image = "farming_wheat.png",
324 })
325
326 minetest.register_craftitem("farming:flour", {
327         description = "Flour",
328         inventory_image = "farming_flour.png",
329 })
330
331 minetest.register_craftitem("farming:bread", {
332         description = "Bread",
333         inventory_image = "farming_bread.png",
334         on_use = minetest.item_eat(4),
335 })
336
337 minetest.register_craft({
338         type = "shapeless",
339         output = "farming:flour",
340         recipe = {"farming:wheat", "farming:wheat", "farming:wheat", "farming:wheat"}
341 })
342
343 minetest.register_craft({
344         type = "cooking",
345         cooktime = 15,
346         output = "farming:bread",
347         recipe = "farming:flour"
348 })
349
350 for i=1,8 do
351         local drop = {
352                 items = {
353                         {items = {'farming:wheat'},rarity=9-i},
354                         {items = {'farming:wheat'},rarity=18-i*2},
355                         {items = {'farming:seed_wheat'},rarity=9-i},
356                         {items = {'farming:seed_wheat'},rarity=18-i*2},
357                 }
358         }
359         minetest.register_node("farming:wheat_"..i, {
360                 drawtype = "plantlike",
361                 tiles = {"farming_wheat_"..i..".png"},
362                 paramtype = "light",
363                 walkable = false,
364                 buildable_to = true,
365                 is_ground_content = true,
366                 drop = drop,
367                 selection_box = {
368                         type = "fixed",
369                         fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
370                 },
371                 groups = {snappy=3,flammable=2,plant=1,wheat=i,not_in_creative_inventory=1,attached_node=1},
372                 sounds = default.node_sound_leaves_defaults(),
373         })
374 end
375
376 minetest.register_abm({
377         nodenames = {"group:wheat"},
378         neighbors = {"group:soil"},
379         interval = 90,
380         chance = 2,
381         action = function(pos, node)
382                 -- return if already full grown
383                 if minetest.get_item_group(node.name, "wheat") == 8 then
384                         return
385                 end
386                 
387                 -- check if on wet soil
388                 pos.y = pos.y-1
389                 local n = minetest.get_node(pos)
390                 if minetest.get_item_group(n.name, "soil") < 3 then
391                         return
392                 end
393                 pos.y = pos.y+1
394                 
395                 -- check light
396                 if not minetest.get_node_light(pos) then
397                         return
398                 end
399                 if minetest.get_node_light(pos) < 13 then
400                         return
401                 end
402                 
403                 -- grow
404                 local height = minetest.get_item_group(node.name, "wheat") + 1
405                 minetest.set_node(pos, {name="farming:wheat_"..height})
406         end
407 })
408
409 --
410 -- Cotton
411 --
412 minetest.register_craftitem("farming:seed_cotton", {
413         description = "Cotton Seed",
414         inventory_image = "farming_cotton_seed.png",
415         on_place = function(itemstack, placer, pointed_thing)
416                 return place_seed(itemstack, placer, pointed_thing, "farming:cotton_1")
417         end,
418 })
419
420 minetest.register_craftitem("farming:string", {
421         description = "String",
422         inventory_image = "farming_string.png",
423 })
424
425 minetest.register_craft({
426         output = "wool:white",
427         recipe = {
428                 {"farming:string", "farming:string"},
429                 {"farming:string", "farming:string"},
430         }
431 })
432
433 for i=1,8 do
434         local drop = {
435                 items = {
436                         {items = {'farming:string'},rarity=9-i},
437                         {items = {'farming:string'},rarity=18-i*2},
438                         {items = {'farming:string'},rarity=27-i*3},
439                         {items = {'farming:seed_cotton'},rarity=9-i},
440                         {items = {'farming:seed_cotton'},rarity=18-i*2},
441                         {items = {'farming:seed_cotton'},rarity=27-i*3},
442                 }
443         }
444         minetest.register_node("farming:cotton_"..i, {
445                 drawtype = "plantlike",
446                 tiles = {"farming_cotton_"..i..".png"},
447                 paramtype = "light",
448                 walkable = false,
449                 buildable_to = true,
450                 is_ground_content = true,
451                 drop = drop,
452                 selection_box = {
453                         type = "fixed",
454                         fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
455                 },
456                 groups = {snappy=3,flammable=2,plant=1,cotton=i,not_in_creative_inventory=1,attached_node=1},
457                 sounds = default.node_sound_leaves_defaults(),
458         })
459 end
460
461 minetest.register_abm({
462         nodenames = {"group:cotton"},
463         neighbors = {"group:soil"},
464         interval = 80,
465         chance = 2,
466         action = function(pos, node)
467                 -- return if already full grown
468                 if minetest.get_item_group(node.name, "cotton") == 8 then
469                         return
470                 end
471                 
472                 -- check if on wet soil
473                 pos.y = pos.y-1
474                 local n = minetest.get_node(pos)
475                 if minetest.get_item_group(n.name, "soil") < 3 then
476                         return
477                 end
478                 pos.y = pos.y+1
479                 
480                 -- check light
481                 if not minetest.get_node_light(pos) then
482                         return
483                 end
484                 if minetest.get_node_light(pos) < 13 then
485                         return
486                 end
487                 
488                 -- grow
489                 local height = minetest.get_item_group(node.name, "cotton") + 1
490                 minetest.set_node(pos, {name="farming:cotton_"..height})
491         end
492 })