From fd205e10fc38a121cefb23d63a7f5b14aa504507 Mon Sep 17 00:00:00 2001 From: tenplus1 Date: Mon, 11 Sep 2023 14:49:37 +0100 Subject: [PATCH] seeds grow on timer so players can wander --- crops/barley.lua | 3 +++ crops/cotton.lua | 3 +++ crops/hemp.lua | 3 +++ crops/mint.lua | 3 +++ crops/rice.lua | 3 +++ crops/sunflower.lua | 3 +++ crops/wheat.lua | 3 +++ init.lua | 36 +++++++++++++++++++++++++++++++++--- 8 files changed, 54 insertions(+), 3 deletions(-) diff --git a/crops/barley.lua b/crops/barley.lua index 51d6561..9114b17 100644 --- a/crops/barley.lua +++ b/crops/barley.lua @@ -18,6 +18,9 @@ minetest.register_node("farming:seed_barley", { selection_box = farming.select, on_place = function(itemstack, placer, pointed_thing) return farming.place_seed(itemstack, placer, pointed_thing, "farming:seed_barley") + end, + on_timer = function(pos, elapsed) + minetest.set_node(pos, {name = "farming:barley_1", param2 = 3}) end }) diff --git a/crops/cotton.lua b/crops/cotton.lua index 858eb10..c0dac62 100644 --- a/crops/cotton.lua +++ b/crops/cotton.lua @@ -47,6 +47,9 @@ minetest.register_node("farming:seed_cotton", { next_plant = "farming:cotton_1", on_place = function(itemstack, placer, pointed_thing) return farming.place_seed(itemstack, placer, pointed_thing, "farming:seed_cotton") + end, + on_timer = function(pos, elapsed) + minetest.set_node(pos, {name = "farming:cotton_1", param2 = 1}) end }) diff --git a/crops/hemp.lua b/crops/hemp.lua index cd575bf..ac2b1dc 100644 --- a/crops/hemp.lua +++ b/crops/hemp.lua @@ -18,6 +18,9 @@ minetest.register_node("farming:seed_hemp", { next_plant = "farming:hemp_1", on_place = function(itemstack, placer, pointed_thing) return farming.place_seed(itemstack, placer, pointed_thing, "farming:seed_hemp") + end, + on_timer = function(pos, elapsed) + minetest.set_node(pos, {name = "farming:hemp_1", param2 = 1}) end }) diff --git a/crops/mint.lua b/crops/mint.lua index 6dc78d6..eeed4c7 100644 --- a/crops/mint.lua +++ b/crops/mint.lua @@ -21,6 +21,9 @@ minetest.register_node("farming:seed_mint", { next_plant = "farming:mint_1", on_place = function(itemstack, placer, pointed_thing) return farming.place_seed(itemstack, placer, pointed_thing, "farming:seed_mint") + end, + on_timer = function(pos, elapsed) + minetest.set_node(pos, {name = "farming:mint_1", param2 = 1}) end }) diff --git a/crops/rice.lua b/crops/rice.lua index 01cc9ab..1e831f3 100644 --- a/crops/rice.lua +++ b/crops/rice.lua @@ -21,6 +21,9 @@ minetest.register_node("farming:seed_rice", { next_plant = "farming:rice_1", on_place = function(itemstack, placer, pointed_thing) return farming.place_seed(itemstack, placer, pointed_thing, "farming:seed_rice") + end, + on_timer = function(pos, elapsed) + minetest.set_node(pos, {name = "farming:rice_1", param2 = 3}) end }) diff --git a/crops/sunflower.lua b/crops/sunflower.lua index d302f19..df7f43a 100644 --- a/crops/sunflower.lua +++ b/crops/sunflower.lua @@ -28,6 +28,9 @@ minetest.register_node("farming:seed_sunflower", { next_plant = "farming:sunflower_1", on_place = function(itemstack, placer, pointed_thing) return farming.place_seed(itemstack, placer, pointed_thing, "farming:seed_sunflower") + end, + on_timer = function(pos, elapsed) + minetest.set_node(pos, {name = "farming:sunflower_1", param2 = 1}) end }) diff --git a/crops/wheat.lua b/crops/wheat.lua index 24fbd43..7d62db0 100644 --- a/crops/wheat.lua +++ b/crops/wheat.lua @@ -18,6 +18,9 @@ minetest.register_node("farming:seed_wheat", { next_plant = "farming:wheat_1", on_place = function(itemstack, placer, pointed_thing) return farming.place_seed(itemstack, placer, pointed_thing, "farming:seed_wheat") + end, + on_timer = function(pos, elapsed) + minetest.set_node(pos, {name = "farming:wheat_1", param2 = 3}) end }) diff --git a/init.lua b/init.lua index d41e7f8..f0ee7ed 100644 --- a/init.lua +++ b/init.lua @@ -7,7 +7,7 @@ farming = { mod = "redo", - version = "20230906", + version = "20230911", path = minetest.get_modpath("farming"), select = { type = "fixed", @@ -107,6 +107,16 @@ local STAGE_LENGTH_AVG = tonumber( local STAGE_LENGTH_DEV = STAGE_LENGTH_AVG / 6 +-- quick start seed timer +farming.start_seed_timer = function(pos) + + local timer = minetest.get_node_timer(pos) + local grow_time = math.floor(math.random(STAGE_LENGTH_DEV, STAGE_LENGTH_AVG)) + + timer:start(grow_time) +end + + -- return plant name and stage from node provided local function plant_name_stage(node) @@ -318,8 +328,15 @@ minetest.register_abm({ def = minetest.registered_nodes[next_stage] - -- change seed to stage_1 or crop - if def then + local timer = minetest.get_node_timer(pos):is_started() + + -- if seed has timer function that isn't started then start timer + if def and def.on_timer and not timer then + + farming.start_seed_timer(pos) + + -- otherwise switch seed to stage_1 of crop + elseif def then local p2 = def.place_param2 or 1 @@ -503,6 +520,7 @@ function farming.place_seed(itemstack, placer, pointed_thing, plantname) minetest.set_node(pt.above, {name = plantname, param2 = p2}) +farming.start_seed_timer(pt.above) --minetest.get_node_timer(pt.above):start(1) --farming.handle_growth(pt.above)--, node) @@ -568,6 +586,18 @@ farming.register_plant = function(name, def) place_param2 = 1, -- place seed flat next_plant = mname .. ":" .. pname .. "_1", + on_timer = function(pos, elapsed) + + local def = minetest.registered_nodes[mname .. ":" .. pname .. "_1"] + + if def then + minetest.swap_node(pos, { + name = def.next_plant, + param2 = def.place_param2 + }) + end + end, + on_place = function(itemstack, placer, pointed_thing) return farming.place_seed(itemstack, placer, pointed_thing, mname .. ":seed_" .. pname)