Fix some farming stuff
authorBlockMen <nmuelll@web.de>
Thu, 21 Aug 2014 10:45:14 +0000 (12:45 +0200)
committerBlockMen <nmuelll@web.de>
Thu, 21 Aug 2014 14:48:48 +0000 (16:48 +0200)
mods/farming/api.lua
mods/farming/nodes.lua

index f1767d17dae828e9bcb5d3da5d02f3c2537ee102..e6d57f803fe0727bbe49642b63489c770669819c 100644 (file)
@@ -44,7 +44,10 @@ farming.hoe_on_use = function(itemstack, user, pointed_thing, uses)
                pos = pt.under,
                gain = 0.5,
        })
-       itemstack:add_wear(65535/(uses-1))
+       
+       if not minetest.setting_getbool("creative_mode") then
+               itemstack:add_wear(65535/(uses-1))
+       end
        return itemstack
 end
 
@@ -135,25 +138,27 @@ end
 farming.register_plant = function(name, def)
        local mname = name:split(":")[1]
        local pname = name:split(":")[2]
+
        -- Check def table
-       if def.description == nil then
+       if not def.description then
                def.description = "Seed"
        end
-       if def.inventory_image == nil then
+       if not def.inventory_image then
                def.inventory_image = "unknown_item.png"
        end
-       if def.steps == nil then
+       if not def.steps then
                return nil
        end
-       if def.minlight == nil then
+       if not def.minlight then
                def.minlight = 1
        end
-       if def.maxlight == nil then
+       if not def.maxlight then
                def.maxlight = 14
        end
        if not def.fertility then
                def.fertility = {}
        end
+
        -- Register seed
        local g = {seed = 1, snappy = 3, attached_node = 1}
        for k, v in pairs(def.fertility) do
@@ -179,40 +184,13 @@ farming.register_plant = function(name, def)
                        return farming.place_seed(itemstack, placer, pointed_thing, mname .. ":seed_" .. pname)
                end
        })
-       -- Seed -> plant
-       minetest.register_abm({
-               nodenames = {"group:seed"},
-               neighbors = {"group:soil"},
-               interval = 90,
-               chance = 2,
-               action = function(pos, node)
-                       local seedferts = minetest.registered_nodes[node.name].fertility
-                       local soilferts = {}
-                       -- Collect fertilities of soil
-                       for k, v in pairs(minetest.registered_nodes[minetest.get_node({x = pos.x, y = pos.y - 1, z = pos.z}).name].groups) do
-                               if k == "grassland" or k == "desert" then
-                                       soilferts[k] = k
-                               end
-                       end
-                       -- Cannot grow if no fertility match found
-                       local fertmatch = false
-                       for k, v in pairs(seedferts) do
-                               if soilferts[v] ~= nil then
-                                       fertmatch = true
-                                       break
-                               end
-                       end
-                       
-                       if fertmatch == true and minetest.get_item_group(minetest.get_node({x = pos.x, y = pos.y - 1, z = pos.z}).name, "wet") ~= 0 then
-                               minetest.set_node(pos, {name = node.name:gsub("seed_", "") .. "_1"})
-                       end
-               end
-       })
+
        -- Register harvest
        minetest.register_craftitem(":" .. mname .. ":" .. pname, {
                description = pname:gsub("^%l", string.upper),
                inventory_image = mname .. "_" .. pname .. ".png",
        })
+
        -- Register growing steps
        for i=1,def.steps do
                local drop = {
@@ -242,18 +220,38 @@ farming.register_plant = function(name, def)
                        sounds = default.node_sound_leaves_defaults(),
                })
        end
+
        -- Growing ABM
        minetest.register_abm({
-               nodenames = {"group:" .. pname},
+               nodenames = {"group:" .. pname, "group:seed"},
                neighbors = {"group:soil"},
                interval = 90,
                chance = 2,
                action = function(pos, node)
+                       local plant_height = minetest.get_item_group(node.name, pname)
+
                        -- return if already full grown
-                       if minetest.get_item_group(node.name, pname) == def.steps then
+                       if plant_height == def.steps then
+                               return
+                       end
+
+                       local node_def = minetest.registered_items[node.name] or nil
+
+                       -- grow seed
+                       if minetest.get_item_group(node.name, "seed") and node_def.fertility then
+                               local can_grow = false
+                               local soil_node = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
+                               for _, v in pairs(node_def.fertility) do
+                                       if minetest.get_item_group(soil_node.name, v) ~= 0 then
+                                               can_grow = true
+                                       end
+                               end
+                               if can_grow then
+                                       minetest.set_node(pos, {name = node.name:gsub("seed_", "") .. "_1"})
+                               end
                                return
                        end
-                       
+
                        -- check if on wet soil
                        pos.y = pos.y - 1
                        local n = minetest.get_node(pos)
@@ -261,20 +259,19 @@ farming.register_plant = function(name, def)
                                return
                        end
                        pos.y = pos.y + 1
-                       
+
                        -- check light
-                       if not minetest.get_node_light(pos) then
-                               return
-                       end
-                       if minetest.get_node_light(pos) < def.minlight or minetest.get_node_light(pos) > def.maxlight then
+                       local ll = minetest.get_node_light(pos)
+
+                       if not ll or ll < def.minlight or ll > def.maxlight then
                                return
                        end
-                       
+
                        -- grow
-                       local height = minetest.get_item_group(node.name, pname) + 1
-                       minetest.set_node(pos, {name = mname .. ":" .. pname .. "_" .. height})
+                       minetest.set_node(pos, {name = mname .. ":" .. pname .. "_" .. plant_height + 1})
                end
        })
+
        -- Return
        local r = {
                seed = mname .. ":seed_" .. pname,
index d8c1d2d230d4f24e5e905dd727e9efafe98d9a97..ba7aed47c6c5e951b88e1307968f3c2cf1e632c1 100644 (file)
@@ -21,7 +21,7 @@ minetest.register_node("farming:soil", {
        tiles = {"farming_soil.png", "default_dirt.png"},
        drop = "default:dirt",
        is_ground_content = true,
-       groups = {crumbly=3, not_in_creative_inventory=1, soil=2, grassland = 1},
+       groups = {crumbly=3, not_in_creative_inventory=1, soil=2, grassland = 1, field = 1},
        sounds = default.node_sound_dirt_defaults(),
        soil = {
                base = "default:dirt",
@@ -35,7 +35,7 @@ minetest.register_node("farming:soil_wet", {
        tiles = {"farming_soil_wet.png", "farming_soil_wet_side.png"},
        drop = "default:dirt",
        is_ground_content = true,
-       groups = {crumbly=3, not_in_creative_inventory=1, soil=3, wet = 1, grassland = 1},
+       groups = {crumbly=3, not_in_creative_inventory=1, soil=3, wet = 1, grassland = 1, field = 1},
        sounds = default.node_sound_dirt_defaults(),
        soil = {
                base = "default:dirt",
@@ -57,7 +57,7 @@ minetest.register_node("farming:desert_sand_soil", {
        drop = "default:desert_sand",
        tiles = {"farming_desert_sand_soil.png", "default_desert_sand.png"},
        is_ground_content = true,
-       groups = {crumbly=3, not_in_creative_inventory = 1, falling_node=1, sand=1, soil = 2, desert = 1},
+       groups = {crumbly=3, not_in_creative_inventory = 1, falling_node=1, sand=1, soil = 2, desert = 1, field = 1},
        sounds = default.node_sound_sand_defaults(),
        soil = {
                base = "default:desert_sand",
@@ -71,7 +71,7 @@ minetest.register_node("farming:desert_sand_soil_wet", {
        drop = "default:desert_sand",
        tiles = {"farming_desert_sand_soil_wet.png", "farming_desert_sand_soil_wet_side.png"},
        is_ground_content = true,
-       groups = {crumbly=3, falling_node=1, sand=1, not_in_creative_inventory=1, soil=3, wet = 1, desert = 1},
+       groups = {crumbly=3, falling_node=1, sand=1, not_in_creative_inventory=1, soil=3, wet = 1, desert = 1, field = 1},
        sounds = default.node_sound_sand_defaults(),
        soil = {
                base = "default:desert_sand",
@@ -81,39 +81,48 @@ minetest.register_node("farming:desert_sand_soil_wet", {
 })
 
 minetest.register_abm({
-       nodenames = {"group:soil", "group:wet"},
-       interval = 5,
-       chance = 10,
+       nodenames = {"group:field"},
+       interval = 15,
+       chance = 4,
        action = function(pos, node)
-               pos.y = pos.y+1
-               local nn = minetest.get_node(pos).name
-               node = minetest.registered_nodes[node.name]
-               pos.y = pos.y-1
-               
-               if node.soil == nil or node.soil.wet == nil or node.soil.base == nil or node.soil.dry == nil then
+               local n_def = minetest.registered_nodes[node.name] or nil
+               local wet = n_def.soil.wet or nil
+               local base = n_def.soil.base or nil
+               local dry = n_def.soil.dry or nil
+               if not n_def or not n_def.soil or not wet or not base or not dry then
                        return
                end
+
+               pos.y = pos.y + 1
+               local nn = minetest.get_node_or_nil(pos)
+               if not nn or not nn.name then
+                       return
+               end
+               local nn_def = minetest.registered_nodes[nn.name] or nil
+               pos.y = pos.y - 1
                
-               if minetest.registered_nodes[nn] and minetest.registered_nodes[nn].walkable and minetest.get_item_group(nn, "plant") == 0 and node.name ~= node.soil.base then
-                       minetest.set_node(pos, {name = node.soil.base})
+               if nn_def and nn_def.walkable and minetest.get_item_group(nn.name, "plant") == 0 then
+                       minetest.set_node(pos, {name = base})
+                       return
                end
                -- check if there is water nearby
                if minetest.find_node_near(pos, 3, {"group:water"}) then
+                       local wet_lvl = minetest.get_item_group(node.name, "wet")
                        -- if it is dry soil and not base node, turn it into wet soil
-                       if node.name ~= node.soil.base and minetest.get_item_group(node.name, "wet") == 0 then
-                               minetest.set_node(pos, {name = node.soil.wet})
+                       if wet_lvl == 0 then
+                               minetest.set_node(pos, {name = wet})
                        end
                else
                        -- turn it back into base if it is already dry
-                       if minetest.get_item_group(node.name, "wet") == 0 then
+                       if wet_lvl == 0 then
                                -- only turn it back if there is no plant/seed on top of it
-                               if minetest.get_item_group(nn, "plant") == 0 and minetest.get_item_group(nn, "seed") == 0 then
-                                       minetest.set_node(pos, {name = node.soil.base})
+                               if minetest.get_item_group(nn.name, "plant") == 0 and minetest.get_item_group(nn.name, "seed") == 0 then
+                                       minetest.set_node(pos, {name = base})
                                end
                                
                        -- if its wet turn it back into dry soil
-                       elseif minetest.get_item_group(node.name, "wet") == 1 then
-                               minetest.set_node(pos, {name = node.soil.dry})
+                       elseif wet_lvl == 1 then
+                               minetest.set_node(pos, {name = dry})
                        end
                end
        end,