From e1ace4ad01d12dad95b4e2b37919d644292da013 Mon Sep 17 00:00:00 2001 From: kno10 Date: Wed, 9 Oct 2024 18:01:20 +0200 Subject: [PATCH 01/12] pumpkin/melon growth only tests one neighbor every time --- mods/ITEMS/mcl_farming/shared_functions.lua | 99 ++++----------------- 1 file changed, 15 insertions(+), 84 deletions(-) diff --git a/mods/ITEMS/mcl_farming/shared_functions.lua b/mods/ITEMS/mcl_farming/shared_functions.lua index 241e61c56..3264287ae 100644 --- a/mods/ITEMS/mcl_farming/shared_functions.lua +++ b/mods/ITEMS/mcl_farming/shared_functions.lua @@ -89,7 +89,7 @@ function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light, low stages = stages or 1 local plant_info = plant_lists[identifier] local intervals_counter = get_intervals_counter(pos, plant_info.interval, plant_info.chance) - if stages > 0 then intervals_counters = intervals_counter - 1 end + if stages > 0 then intervals_counter = intervals_counter - 1 end if low_speed then -- 10% speed approximately if intervals_counter < 1.01 and math.random(0, 9) > 0 then return false end intervals_counter = intervals_counter / 10 @@ -163,34 +163,6 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s connected_stem_basename .. "_t", connected_stem_basename .. "_b" } - -- Connect the stem at stempos to the first neighboring gourd block. - -- No-op if not a stem or no gourd block found - local function try_connect_stem(stempos) - local stem = minetest.get_node(stempos) - if stem.name ~= full_unconnected_stem then return false end - -- four directions, but avoid table allocation - local neighbor = vector.offset(stempos, 1, 0, 0) - if minetest.get_node(neighbor).name == gourd_itemstring then - minetest.swap_node(stempos, { name = connected_stem_names[1] }) - return true - end - local neighbor = vector.offset(stempos, -1, 0, 0) - if minetest.get_node(neighbor).name == gourd_itemstring then - minetest.swap_node(stempos, { name = connected_stem_names[2] }) - return true - end - local neighbor = vector.offset(stempos, 0, 0, 1) - if minetest.get_node(neighbor).name == gourd_itemstring then - minetest.swap_node(stempos, { name = connected_stem_names[3] }) - return true - end - local neighbor = vector.offset(stempos, 0, 0, -1) - if minetest.get_node(neighbor).name == gourd_itemstring then - minetest.swap_node(stempos, { name = connected_stem_names[4] }) - return true - end - end - -- Register gourd if not gourd_def.after_destruct then gourd_def.after_destruct = function(blockpos, oldnode) @@ -200,34 +172,21 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s local stempos = vector.offset(blockpos, -1, 0, 0) if minetest.get_node(stempos).name == connected_stem_names[1] then minetest.swap_node(stempos, { name = full_unconnected_stem }) - try_connect_stem(stempos) end local stempos = vector.offset(blockpos, 1, 0, 0) if minetest.get_node(stempos).name == connected_stem_names[2] then minetest.swap_node(stempos, { name = full_unconnected_stem }) - try_connect_stem(stempos) end local stempos = vector.offset(blockpos, 0, 0, -1) if minetest.get_node(stempos).name == connected_stem_names[3] then minetest.swap_node(stempos, { name = full_unconnected_stem }) - try_connect_stem(stempos) end local stempos = vector.offset(blockpos, 0, 0, 1) if minetest.get_node(stempos).name == connected_stem_names[4] then minetest.swap_node(stempos, { name = full_unconnected_stem }) - try_connect_stem(stempos) end end end - if not gourd_def.on_construct then - function gourd_def.on_construct(blockpos) - -- Connect all unconnected stems at full size - try_connect_stem(vector.offset(blockpos, 1, 0, 0)) - try_connect_stem(vector.offset(blockpos, -1, 0, 0)) - try_connect_stem(vector.offset(blockpos, 0, 0, 1)) - try_connect_stem(vector.offset(blockpos, 0, 0, -1)) - end - end minetest.register_node(gourd_itemstring, gourd_def) -- Register unconnected stem @@ -243,7 +202,6 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s stem_def.drop = stem_def.drop or stem_drop stem_def.groups = stem_def.groups or { dig_immediate = 3, not_in_creative_inventory = 1, plant = 1, attached_node = 1, dig_by_water = 1, destroy_by_lava_flow = 1 } stem_def.sounds = stem_def.sounds or mcl_sounds.node_sound_leaves_defaults() - stem_def.on_construct = stem_def.on_construct or try_connect_stem minetest.register_node(stem_itemstring, stem_def) -- Register connected stems @@ -313,16 +271,6 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s end end - -- Check for a suitable spot to grow - local function check_neighbor_soil(blockpos) - if minetest.get_node(blockpos).name ~= "air" then return false end - local floorpos = vector.offset(blockpos, 0, -1, 0) - local floorname = minetest.get_node(floorpos).name - if floorname == "mcl_core:dirt" then return true end - local floordef = minetest.registered_nodes[floorname] - return floordef.groups.grass_block or floordef.groups.dirt or (floordef.groups.soil or 0) >= 2 - end - minetest.register_abm({ label = "Grow gourd stem to gourd (" .. full_unconnected_stem .. " → " .. gourd_itemstring .. ")", nodenames = { full_unconnected_stem }, @@ -332,37 +280,21 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s action = function(stempos) local light = minetest.get_node_light(stempos) if not light or light <= 10 then return end - -- Check the four neighbors and filter out neighbors where gourds can't grow - local neighbor, dir, nchance = nil, -1, 1 -- reservoir sampling - if nchance == 1 or math.random(1, nchance) == 1 then - local blockpos = vector.offset(stempos, 1, 0, 0) - if check_neighbor_soil(blockpos) then - neighbor, dir, nchance = blockpos, 1, nchance + 1 - end - end - if nchance == 1 or math.random(1, nchance) == 1 then - local blockpos = vector.offset(stempos, -1, 0, 0) - if check_neighbor_soil(blockpos) then - neighbor, dir, nchance = blockpos, 2, nchance + 1 - end - end - if nchance == 1 or math.random(1, nchance) == 1 then - local blockpos = vector.offset(stempos, 0, 0, 1) - if check_neighbor_soil(blockpos) then - neighbor, dir, nchance = blockpos, 3, nchance + 1 - end - end - if nchance == 1 or math.random(1, nchance) == 1 then - local blockpos = vector.offset(stempos, 0, 0, -1) - if check_neighbor_soil(blockpos) then - neighbor, dir, nchance = blockpos, 4, nchance + 1 - end - end + -- Pick one neighbor and check if it can be used to grow + local dir = math.random(1, 4) -- pick direction at random + local neighbor = (dir == 1 and vector.offset(stempos, 1, 0, 0)) + or (dir == 2 and vector.offset(stempos, -1, 0, 0)) + or (dir == 3 and vector.offset(stempos, 0, 0, 1)) + or vector.offset(stempos, 0, 0, -1) + if minetest.get_node(neighbor).name ~= "air" then return end -- occupied + -- check for suitable floor: grass, dirt, or soil + local floorpos = vector.offset(neighbor, 0, -1, 0) + local floorname = minetest.get_node(floorpos).name + local floordef = minetest.registered_nodes[floorname] + if not floordef then return end + if (floordef.groups.grass_block or 0) == 0 and (floordef.groups.dirt or 0) == 0 and (floordef.groups.soil or 0) < 2 then return end -- not suitable for growing - -- Gourd needs at least 1 free neighbor to grow - if not neighbor then return end minetest.swap_node(stempos, { name = connected_stem_names[dir] }) - -- Place the gourd if gourd_def.paramtype2 == "facedir" then local p2 = (dir == 1 and 3) or (dir == 2 and 1) or (dir == 3 and 2) or 0 minetest.add_node(neighbor, { name = gourd_itemstring, param2 = p2 }) @@ -371,8 +303,7 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s end -- Reset farmland, etc. to dirt when the gourd grows on top - local floorpos = vector.offset(neighbor, 0, -1, 0) - if minetest.get_item_group(minetest.get_node(floorpos).name, "dirtifies_below_solid") == 1 then + if (floordef.groups.dirtifies_below_solid or 0) > 0 then minetest.set_node(floorpos, { name = "mcl_core:dirt" }) end end, From c4030115c4ecfb599a4596d7a597c941a960aa9a Mon Sep 17 00:00:00 2001 From: kno10 Date: Wed, 9 Oct 2024 22:15:30 +0200 Subject: [PATCH 02/12] improve moisture logic --- mods/ITEMS/mcl_farming/shared_functions.lua | 72 ++++++++++++++++++--- 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/mods/ITEMS/mcl_farming/shared_functions.lua b/mods/ITEMS/mcl_farming/shared_functions.lua index 3264287ae..5d4d5d49e 100644 --- a/mods/ITEMS/mcl_farming/shared_functions.lua +++ b/mods/ITEMS/mcl_farming/shared_functions.lua @@ -50,6 +50,55 @@ local function get_avg_light_level(pos) return math.ceil(summary / counter) end +local function get_moisture_level(pos) + local n = vector.offset(pos, 0, -1, 0) + local totalm = 1 + for z = -1,1 do + n.z = pos.z + z + for x = -1,1 do + n.x = pos.x + x + local ndef = minetest.registered_nodes[minetest.get_node(n).name] + local soil = ndef and ndef.groups.soil or 0 + local m = (soil == 2 and 2) or (soil >= 3 and 4) or 0 + if x ~= 0 and z ~= 0 then m = m * 0.25 end + totalm = totalm + m + end + end + return totalm +end + +-- moisture penalty function: +-- 0.5 if both on the x axis and the z axis the same plant growth +-- 0.5 if one diagonal neighbor is the same +-- 1.0 otherwise +local function get_moisture_penalty(pos) + local name = minetest.get_node(pos).name + local n, p = vector.copy(pos), 1 + -- check adjacent points, avoid vector allocations and reduce node accesses + n.x = pos.x - 1 + local dx = minetest.get_node(n).name == name + n.x = pos.x + 1 + dx = dx or minetest.get_node(n).name == name + if dx then + n.x = pos.x + n.z = pos.z - 1 + local dz = minetest.get_node(n).name == name + n.z = pos.z + 1 + dz = dz or minetest.get_node(n).name == name + if dz then return 0.5 end + end + -- check diagonals, clockwise + n.x, n.z = pos.x - 1, pos.z - 1 + if minetest.get_node(n).name == name then return 0.5 end + n.x = pos.x + 1 + if minetest.get_node(n).name == name then return 0.5 end + n.z = pos.z + 1 + if minetest.get_node(n).name == name then return 0.5 end + n.x = pos.x - 1 + if minetest.get_node(n).name == name then return 0.5 end + return 1 +end + function mcl_farming:add_plant(identifier, full_grown, names, interval, chance) local plant_info = {} plant_info.full_grown = full_grown @@ -70,8 +119,7 @@ function mcl_farming:add_plant(identifier, full_grown, names, interval, chance) interval = interval, chance = chance, action = function(pos, node) - local low_speed = minetest.get_node(vector.offset(pos, 0, -1, 0)).name ~= "mcl_farming:soil_wet" - mcl_farming:grow_plant(identifier, pos, node, 1, false, low_speed) + mcl_farming:grow_plant(identifier, pos, node, 1, false) end, }) end @@ -82,7 +130,7 @@ end -- node: Node table -- stages: Number of stages to advance (optional, defaults to 1) -- ignore_light: if true, ignore light requirements for growing --- low_speed: grow more slowly (not wet), default false +-- low_speed: grow more slowly (not wet), default false -- OBSOLETE -- Returns true if plant has been grown by 1 or more stages. -- Returns false if nothing changed. function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light, low_speed) @@ -96,17 +144,22 @@ function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light, low end if not ignore_light and intervals_counter < 1.5 then local light = minetest.get_node_light(pos) - if not light or light < 10 then return false end + if not light or light < 9 then return false end end if intervals_counter >= 1.5 then local average_light_level = get_avg_light_level(pos) if average_light_level < 0.1 then return false end - if average_light_level < 10 then + if average_light_level < 9 then intervals_counter = intervals_counter * average_light_level / 10 end end + if low_speed == nil then + local moisture = get_moisture_level(pos) * get_moisture_penalty(pos) + if math.random(1, math.floor(25 / moisture) + 1) ~= 1 then return end + end + local step = plant_info.step_from_name[node.name] if step == nil then return false end stages = stages + math.floor(intervals_counter) @@ -279,7 +332,7 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s chance = grow_chance, action = function(stempos) local light = minetest.get_node_light(stempos) - if not light or light <= 10 then return end + if not light or light < 9 then return end -- Pick one neighbor and check if it can be used to grow local dir = math.random(1, 4) -- pick direction at random local neighbor = (dir == 1 and vector.offset(stempos, 1, 0, 0)) @@ -294,6 +347,10 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s if not floordef then return end if (floordef.groups.grass_block or 0) == 0 and (floordef.groups.dirt or 0) == 0 and (floordef.groups.soil or 0) < 2 then return end -- not suitable for growing + -- check moisture level + local moisture = get_moisture_level(stempos) * get_moisture_penalty(stempos) + if math.random(1, math.floor(25 / moisture) + 1) ~= 1 then return end + minetest.swap_node(stempos, { name = connected_stem_names[dir] }) if gourd_def.paramtype2 == "facedir" then local p2 = (dir == 1 and 3) or (dir == 2 and 1) or (dir == 3 and 2) or 0 @@ -342,8 +399,7 @@ minetest.register_lbm({ action = function(pos, node, dtime_s) local identifier = plant_nodename_to_id_list[node.name] if not identifier then return end - local low_speed = minetest.get_node(vector.offset(pos, 0, -1, 0)).name ~= "mcl_farming:soil_wet" - mcl_farming:grow_plant(identifier, pos, node, 0, false, low_speed) + mcl_farming:grow_plant(identifier, pos, node, 0, false) end, }) From 9376cf92b134ad48f82d97daa02ad6768f217f4c Mon Sep 17 00:00:00 2001 From: kno10 Date: Thu, 10 Oct 2024 14:37:01 +0200 Subject: [PATCH 03/12] Adjust growth speeds --- mods/ITEMS/mcl_farming/beetroot.lua | 3 ++- mods/ITEMS/mcl_farming/carrots.lua | 2 +- mods/ITEMS/mcl_farming/melon.lua | 4 ++-- mods/ITEMS/mcl_farming/potatoes.lua | 2 +- mods/ITEMS/mcl_farming/pumpkin.lua | 4 ++-- mods/ITEMS/mcl_farming/sweet_berry.lua | 2 +- mods/ITEMS/mcl_farming/wheat.lua | 2 +- 7 files changed, 10 insertions(+), 9 deletions(-) diff --git a/mods/ITEMS/mcl_farming/beetroot.lua b/mods/ITEMS/mcl_farming/beetroot.lua index 962fb100e..01a572355 100644 --- a/mods/ITEMS/mcl_farming/beetroot.lua +++ b/mods/ITEMS/mcl_farming/beetroot.lua @@ -171,7 +171,8 @@ minetest.register_craft({ }, }) -mcl_farming:add_plant("plant_beetroot", "mcl_farming:beetroot", {"mcl_farming:beetroot_0", "mcl_farming:beetroot_1", "mcl_farming:beetroot_2"}, 68, 3) +-- beetroots grow 1/3rd of the default speed +mcl_farming:add_plant("plant_beetroot", "mcl_farming:beetroot", {"mcl_farming:beetroot_0", "mcl_farming:beetroot_1", "mcl_farming:beetroot_2"}, 13.512, 15) if minetest.get_modpath("doc") then for i = 1, 2 do diff --git a/mods/ITEMS/mcl_farming/carrots.lua b/mods/ITEMS/mcl_farming/carrots.lua index e8e295d8b..fe68767f2 100644 --- a/mods/ITEMS/mcl_farming/carrots.lua +++ b/mods/ITEMS/mcl_farming/carrots.lua @@ -118,7 +118,7 @@ minetest.register_craft({ } }) -mcl_farming:add_plant("plant_carrot", "mcl_farming:carrot", {"mcl_farming:carrot_1", "mcl_farming:carrot_2", "mcl_farming:carrot_3", "mcl_farming:carrot_4", "mcl_farming:carrot_5", "mcl_farming:carrot_6", "mcl_farming:carrot_7"}, 25, 20) +mcl_farming:add_plant("plant_carrot", "mcl_farming:carrot", {"mcl_farming:carrot_1", "mcl_farming:carrot_2", "mcl_farming:carrot_3", "mcl_farming:carrot_4", "mcl_farming:carrot_5", "mcl_farming:carrot_6", "mcl_farming:carrot_7"}, 13.513, 5) if minetest.get_modpath("doc") then for i=2,7 do diff --git a/mods/ITEMS/mcl_farming/melon.lua b/mods/ITEMS/mcl_farming/melon.lua index d62288333..b8242f112 100644 --- a/mods/ITEMS/mcl_farming/melon.lua +++ b/mods/ITEMS/mcl_farming/melon.lua @@ -123,10 +123,10 @@ local stem_def = { } -- Register stem growth -mcl_farming:add_plant("plant_melon_stem", "mcl_farming:melontige_unconnect", {"mcl_farming:melontige_1", "mcl_farming:melontige_2", "mcl_farming:melontige_3", "mcl_farming:melontige_4", "mcl_farming:melontige_5", "mcl_farming:melontige_6", "mcl_farming:melontige_7"}, 30, 5) +mcl_farming:add_plant("plant_melon_stem", "mcl_farming:melontige_unconnect", {"mcl_farming:melontige_1", "mcl_farming:melontige_2", "mcl_farming:melontige_3", "mcl_farming:melontige_4", "mcl_farming:melontige_5", "mcl_farming:melontige_6", "mcl_farming:melontige_7"}, 13.514, 5) -- Register actual melon, connected stems and stem-to-melon growth -mcl_farming:add_gourd("mcl_farming:melontige_unconnect", "mcl_farming:melontige_linked", "mcl_farming:melontige_unconnect", stem_def, stem_drop, "mcl_farming:melon", melon_base_def, 25, 15, "mcl_farming_melon_stem_connected.png^[colorize:#FFA800:127") +mcl_farming:add_gourd("mcl_farming:melontige_unconnect", "mcl_farming:melontige_linked", "mcl_farming:melontige_unconnect", stem_def, stem_drop, "mcl_farming:melon", melon_base_def, 13.515, 5, "mcl_farming_melon_stem_connected.png^[colorize:#FFA800:127") -- Items and crafting minetest.register_craftitem("mcl_farming:melon_item", { diff --git a/mods/ITEMS/mcl_farming/potatoes.lua b/mods/ITEMS/mcl_farming/potatoes.lua index 66c5169c4..a19070d1e 100644 --- a/mods/ITEMS/mcl_farming/potatoes.lua +++ b/mods/ITEMS/mcl_farming/potatoes.lua @@ -135,7 +135,7 @@ minetest.register_craft({ cooktime = 10, }) -mcl_farming:add_plant("plant_potato", "mcl_farming:potato", {"mcl_farming:potato_1", "mcl_farming:potato_2", "mcl_farming:potato_3", "mcl_farming:potato_4", "mcl_farming:potato_5", "mcl_farming:potato_6", "mcl_farming:potato_7"}, 19.75, 20) +mcl_farming:add_plant("plant_potato", "mcl_farming:potato", {"mcl_farming:potato_1", "mcl_farming:potato_2", "mcl_farming:potato_3", "mcl_farming:potato_4", "mcl_farming:potato_5", "mcl_farming:potato_6", "mcl_farming:potato_7"}, 13.516, 5) minetest.register_on_item_eat(function (hp_change, replace_with_item, itemstack, user, pointed_thing) diff --git a/mods/ITEMS/mcl_farming/pumpkin.lua b/mods/ITEMS/mcl_farming/pumpkin.lua index 5bcf5e8a3..d33e73f16 100644 --- a/mods/ITEMS/mcl_farming/pumpkin.lua +++ b/mods/ITEMS/mcl_farming/pumpkin.lua @@ -180,10 +180,10 @@ if minetest.get_modpath("mcl_armor") then end -- Register stem growth -mcl_farming:add_plant("plant_pumpkin_stem", "mcl_farming:pumpkintige_unconnect", {"mcl_farming:pumpkin_1", "mcl_farming:pumpkin_2", "mcl_farming:pumpkin_3", "mcl_farming:pumpkin_4", "mcl_farming:pumpkin_5", "mcl_farming:pumpkin_6", "mcl_farming:pumpkin_7"}, 30, 5) +mcl_farming:add_plant("plant_pumpkin_stem", "mcl_farming:pumpkintige_unconnect", {"mcl_farming:pumpkin_1", "mcl_farming:pumpkin_2", "mcl_farming:pumpkin_3", "mcl_farming:pumpkin_4", "mcl_farming:pumpkin_5", "mcl_farming:pumpkin_6", "mcl_farming:pumpkin_7"}, 13.517, 5) -- Register actual pumpkin, connected stems and stem-to-pumpkin growth -mcl_farming:add_gourd("mcl_farming:pumpkintige_unconnect", "mcl_farming:pumpkintige_linked", "mcl_farming:pumpkintige_unconnect", stem_def, stem_drop, "mcl_farming:pumpkin", pumpkin_base_def, 30, 15, "mcl_farming_pumpkin_stem_connected.png^[colorize:#FFA800:127") +mcl_farming:add_gourd("mcl_farming:pumpkintige_unconnect", "mcl_farming:pumpkintige_linked", "mcl_farming:pumpkintige_unconnect", stem_def, stem_drop, "mcl_farming:pumpkin", pumpkin_base_def, 13.518, 5, "mcl_farming_pumpkin_stem_connected.png^[colorize:#FFA800:127") -- Steal function to properly disconnect a carved pumpkin pumpkin_face_base_def.after_destruct = minetest.registered_nodes["mcl_farming:pumpkin"].after_destruct diff --git a/mods/ITEMS/mcl_farming/sweet_berry.lua b/mods/ITEMS/mcl_farming/sweet_berry.lua index fdcc460df..328339f01 100644 --- a/mods/ITEMS/mcl_farming/sweet_berry.lua +++ b/mods/ITEMS/mcl_farming/sweet_berry.lua @@ -100,7 +100,7 @@ minetest.register_craftitem("mcl_farming:sweet_berry", { minetest.register_alias("mcl_sweet_berry:sweet_berry", "mcl_farming:sweet_berry") -- TODO: Find proper interval and chance values for sweet berry bushes. Current interval and chance values are copied from mcl_farming:beetroot which has similar growth stages. -mcl_farming:add_plant("plant_sweet_berry_bush", "mcl_farming:sweet_berry_bush_3", {"mcl_farming:sweet_berry_bush_0", "mcl_farming:sweet_berry_bush_1", "mcl_farming:sweet_berry_bush_2"}, 68, 3) +mcl_farming:add_plant("plant_sweet_berry_bush", "mcl_farming:sweet_berry_bush_3", {"mcl_farming:sweet_berry_bush_0", "mcl_farming:sweet_berry_bush_1", "mcl_farming:sweet_berry_bush_2"}, 13.519, 15) local function berry_damage_check(obj) local p = obj:get_pos() diff --git a/mods/ITEMS/mcl_farming/wheat.lua b/mods/ITEMS/mcl_farming/wheat.lua index 051c5a8d6..d30cc77a7 100644 --- a/mods/ITEMS/mcl_farming/wheat.lua +++ b/mods/ITEMS/mcl_farming/wheat.lua @@ -99,7 +99,7 @@ minetest.register_node("mcl_farming:wheat", { } }) -mcl_farming:add_plant("plant_wheat", "mcl_farming:wheat", {"mcl_farming:wheat_1", "mcl_farming:wheat_2", "mcl_farming:wheat_3", "mcl_farming:wheat_4", "mcl_farming:wheat_5", "mcl_farming:wheat_6", "mcl_farming:wheat_7"}, 25, 20) +mcl_farming:add_plant("plant_wheat", "mcl_farming:wheat", {"mcl_farming:wheat_1", "mcl_farming:wheat_2", "mcl_farming:wheat_3", "mcl_farming:wheat_4", "mcl_farming:wheat_5", "mcl_farming:wheat_6", "mcl_farming:wheat_7"}, 13.520, 5) minetest.register_craftitem("mcl_farming:wheat_item", { description = S("Wheat"), From e9453d62103e2e61389092adac1dd14980c765e0 Mon Sep 17 00:00:00 2001 From: kno10 Date: Thu, 10 Oct 2024 16:23:54 +0200 Subject: [PATCH 04/12] Add plant growth speed option, drop average light level Closes: #4683 by removal --- mods/ITEMS/mcl_farming/shared_functions.lua | 94 +++++++++------------ settingtypes.txt | 3 + 2 files changed, 43 insertions(+), 54 deletions(-) diff --git a/mods/ITEMS/mcl_farming/shared_functions.lua b/mods/ITEMS/mcl_farming/shared_functions.lua index 5d4d5d49e..f31272dc9 100644 --- a/mods/ITEMS/mcl_farming/shared_functions.lua +++ b/mods/ITEMS/mcl_farming/shared_functions.lua @@ -8,7 +8,10 @@ local vector = vector local plant_lists = {} mcl_farming.plant_lists = plant_lists -- export -local plant_nodename_to_id_list = {} +local plant_nodename_to_id_list = {} -- map nodes to plants +local plant_step_from_name = {} -- map nodes to growth steps + +local growth_factor = tonumber(minetest.settings:get("vl_plant_growth")) or 1.0 local time_speed = tonumber(minetest.settings:get("time_speed")) or 72 local time_multiplier = time_speed > 0 and (86400 / time_speed) or 0 @@ -30,26 +33,6 @@ local function get_intervals_counter(pos, interval, chance) return (current_game_time - last_game_time) / approx_interval end -local function get_avg_light_level(pos) - local meta = minetest.get_meta(pos) - -- EWMA would use a single variable: - -- local avg = meta:get_float("avg_light") - -- avg = avg + (node_light - avg) * 0.985 - -- meta.set_float("avg_light", avg) - local summary = meta:get_int("avg_light_summary") - local counter = meta:get_int("avg_light_count") - if counter > 99 then - summary, counter = math.ceil(summary * 0.5), 50 - end - local node_light = minetest.get_node_light(pos) - if node_light ~= nil then - summary, counter = summary + node_light, counter + 1 - meta:set_int("avg_light_summary", summary) - meta:set_int("avg_light_count", counter) - end - return math.ceil(summary / counter) -end - local function get_moisture_level(pos) local n = vector.offset(pos, 0, -1, 0) local totalm = 1 @@ -100,6 +83,7 @@ local function get_moisture_penalty(pos) end function mcl_farming:add_plant(identifier, full_grown, names, interval, chance) + interval = growth_factor > 0 and (interval / growth_factor) or 0 local plant_info = {} plant_info.full_grown = full_grown plant_info.names = names @@ -108,11 +92,11 @@ function mcl_farming:add_plant(identifier, full_grown, names, interval, chance) for _, nodename in pairs(names) do plant_nodename_to_id_list[nodename] = identifier end - plant_info.step_from_name = {} for i, name in ipairs(names) do - plant_info.step_from_name[name] = i + plant_step_from_name[name] = i end plant_lists[identifier] = plant_info + if interval == 0 then return end -- growth disabled minetest.register_abm({ label = string.format("Farming plant growth (%s)", identifier), nodenames = names, @@ -129,45 +113,32 @@ end -- pos: Position -- node: Node table -- stages: Number of stages to advance (optional, defaults to 1) --- ignore_light: if true, ignore light requirements for growing --- low_speed: grow more slowly (not wet), default false -- OBSOLETE +-- ignore_light_water: if true, ignore light and water requirements for growing -- Returns true if plant has been grown by 1 or more stages. -- Returns false if nothing changed. -function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light, low_speed) - stages = stages or 1 +function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light_water) + stages = stages or 1 -- 0 when run from block loading + -- check light + if not ignore_light_water and (minetest.get_node_light(pos) or 0) < 0 then return false end + -- number of missed interval ticks, for catch-up in block loading local plant_info = plant_lists[identifier] - local intervals_counter = get_intervals_counter(pos, plant_info.interval, plant_info.chance) - if stages > 0 then intervals_counter = intervals_counter - 1 end - if low_speed then -- 10% speed approximately - if intervals_counter < 1.01 and math.random(0, 9) > 0 then return false end - intervals_counter = intervals_counter / 10 + if plant_info then + stages = stages + math.floor(get_intervals_counter(pos, plant_info.interval, plant_info.chance)) end - if not ignore_light and intervals_counter < 1.5 then - local light = minetest.get_node_light(pos) - if not light or light < 9 then return false end - end - - if intervals_counter >= 1.5 then - local average_light_level = get_avg_light_level(pos) - if average_light_level < 0.1 then return false end - if average_light_level < 9 then - intervals_counter = intervals_counter * average_light_level / 10 + if not ignore_light_water then + local odds = math.floor(25 / (get_moisture_level(pos) * get_moisture_penalty(pos))) + 1 + for i = 1,stages do + if math.random(1, odds) ~= 1 then stages = stages - 1 end end end - if low_speed == nil then - local moisture = get_moisture_level(pos) * get_moisture_penalty(pos) - if math.random(1, math.floor(25 / moisture) + 1) ~= 1 then return end - end - - local step = plant_info.step_from_name[node.name] - if step == nil then return false end - stages = stages + math.floor(intervals_counter) if stages == 0 then return false end - local new_node = { name = plant_info.names[step + stages] or plant_info.full_grown } - new_node.param = node.param - new_node.param2 = node.param2 - minetest.set_node(pos, new_node) + local step = plant_step_from_name[node.name] + if step == nil then return false end + minetest.set_node(pos, { + name = plant_info.names[step + stages] or plant_info.full_grown, + param = node.param, param2 = node.param2, + }) return true end @@ -210,6 +181,7 @@ end function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, stem_itemstring, stem_def, stem_drop, gourd_itemstring, gourd_def, grow_interval, grow_chance, connected_stem_texture) + grow_interval = growth_factor > 0 and (grow_interval / growth_factor) or 0 local connected_stem_names = { connected_stem_basename .. "_r", connected_stem_basename .. "_l", @@ -324,6 +296,7 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s end end + if grow_interval == 0 then return end minetest.register_abm({ label = "Grow gourd stem to gourd (" .. full_unconnected_stem .. " → " .. gourd_itemstring .. ")", nodenames = { full_unconnected_stem }, @@ -403,3 +376,16 @@ minetest.register_lbm({ end, }) +-- The average light levels were unreliable +minetest.register_lbm({ + label = "Drop legacy average lighting data", + name = "mcl_farming:drop_average_light_meta", + nodenames = { "group:plant" }, + run_at_every_load = false, -- only convert once + action = function(pos, node, dtime_s) + local meta = minetest.get_meta(pos) + meta:set_string("avg_light_summary", "") -- drop + meta:set_string("avg_light_count", "") -- drop + end, +}) + diff --git a/settingtypes.txt b/settingtypes.txt index 7cfa03ce2..86c635582 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -45,6 +45,9 @@ mcl_disabled_structures (Disabled structures) string # Comma separated list of disabled event names mcl_disabled_events (Disabled events) string +# Control the relative plant growth speed (default: 1) +vl_plant_growth (Plant growth factor) float 1.0 0 100 + [Players] # If enabled, players respawn at the bed they last lay on instead of normal # spawn. From 78a958db4e548b03618aaa9b2dd604d7a07a55c7 Mon Sep 17 00:00:00 2001 From: kno10 Date: Thu, 10 Oct 2024 17:32:36 +0200 Subject: [PATCH 05/12] Double the odds, to halve the ABM frequencies. --- mods/ITEMS/mcl_farming/beetroot.lua | 2 +- mods/ITEMS/mcl_farming/carrots.lua | 2 +- mods/ITEMS/mcl_farming/melon.lua | 4 ++-- mods/ITEMS/mcl_farming/potatoes.lua | 2 +- mods/ITEMS/mcl_farming/pumpkin.lua | 4 ++-- mods/ITEMS/mcl_farming/shared_functions.lua | 14 +++++++++----- mods/ITEMS/mcl_farming/sweet_berry.lua | 4 ++-- mods/ITEMS/mcl_farming/wheat.lua | 2 +- 8 files changed, 19 insertions(+), 15 deletions(-) diff --git a/mods/ITEMS/mcl_farming/beetroot.lua b/mods/ITEMS/mcl_farming/beetroot.lua index 01a572355..904274b8e 100644 --- a/mods/ITEMS/mcl_farming/beetroot.lua +++ b/mods/ITEMS/mcl_farming/beetroot.lua @@ -172,7 +172,7 @@ minetest.register_craft({ }) -- beetroots grow 1/3rd of the default speed -mcl_farming:add_plant("plant_beetroot", "mcl_farming:beetroot", {"mcl_farming:beetroot_0", "mcl_farming:beetroot_1", "mcl_farming:beetroot_2"}, 13.512, 15) +mcl_farming:add_plant("plant_beetroot", "mcl_farming:beetroot", {"mcl_farming:beetroot_0", "mcl_farming:beetroot_1", "mcl_farming:beetroot_2"}, 16.2012, 25) if minetest.get_modpath("doc") then for i = 1, 2 do diff --git a/mods/ITEMS/mcl_farming/carrots.lua b/mods/ITEMS/mcl_farming/carrots.lua index fe68767f2..580e4da5a 100644 --- a/mods/ITEMS/mcl_farming/carrots.lua +++ b/mods/ITEMS/mcl_farming/carrots.lua @@ -118,7 +118,7 @@ minetest.register_craft({ } }) -mcl_farming:add_plant("plant_carrot", "mcl_farming:carrot", {"mcl_farming:carrot_1", "mcl_farming:carrot_2", "mcl_farming:carrot_3", "mcl_farming:carrot_4", "mcl_farming:carrot_5", "mcl_farming:carrot_6", "mcl_farming:carrot_7"}, 13.513, 5) +mcl_farming:add_plant("plant_carrot", "mcl_farming:carrot", {"mcl_farming:carrot_1", "mcl_farming:carrot_2", "mcl_farming:carrot_3", "mcl_farming:carrot_4", "mcl_farming:carrot_5", "mcl_farming:carrot_6", "mcl_farming:carrot_7"}, 5.4013, 25) if minetest.get_modpath("doc") then for i=2,7 do diff --git a/mods/ITEMS/mcl_farming/melon.lua b/mods/ITEMS/mcl_farming/melon.lua index b8242f112..74c95e686 100644 --- a/mods/ITEMS/mcl_farming/melon.lua +++ b/mods/ITEMS/mcl_farming/melon.lua @@ -123,10 +123,10 @@ local stem_def = { } -- Register stem growth -mcl_farming:add_plant("plant_melon_stem", "mcl_farming:melontige_unconnect", {"mcl_farming:melontige_1", "mcl_farming:melontige_2", "mcl_farming:melontige_3", "mcl_farming:melontige_4", "mcl_farming:melontige_5", "mcl_farming:melontige_6", "mcl_farming:melontige_7"}, 13.514, 5) +mcl_farming:add_plant("plant_melon_stem", "mcl_farming:melontige_unconnect", {"mcl_farming:melontige_1", "mcl_farming:melontige_2", "mcl_farming:melontige_3", "mcl_farming:melontige_4", "mcl_farming:melontige_5", "mcl_farming:melontige_6", "mcl_farming:melontige_7"}, 5.4014, 25) -- Register actual melon, connected stems and stem-to-melon growth -mcl_farming:add_gourd("mcl_farming:melontige_unconnect", "mcl_farming:melontige_linked", "mcl_farming:melontige_unconnect", stem_def, stem_drop, "mcl_farming:melon", melon_base_def, 13.515, 5, "mcl_farming_melon_stem_connected.png^[colorize:#FFA800:127") +mcl_farming:add_gourd("mcl_farming:melontige_unconnect", "mcl_farming:melontige_linked", "mcl_farming:melontige_unconnect", stem_def, stem_drop, "mcl_farming:melon", melon_base_def, 5.4015, 25, "mcl_farming_melon_stem_connected.png^[colorize:#FFA800:127") -- Items and crafting minetest.register_craftitem("mcl_farming:melon_item", { diff --git a/mods/ITEMS/mcl_farming/potatoes.lua b/mods/ITEMS/mcl_farming/potatoes.lua index a19070d1e..455befa7d 100644 --- a/mods/ITEMS/mcl_farming/potatoes.lua +++ b/mods/ITEMS/mcl_farming/potatoes.lua @@ -135,7 +135,7 @@ minetest.register_craft({ cooktime = 10, }) -mcl_farming:add_plant("plant_potato", "mcl_farming:potato", {"mcl_farming:potato_1", "mcl_farming:potato_2", "mcl_farming:potato_3", "mcl_farming:potato_4", "mcl_farming:potato_5", "mcl_farming:potato_6", "mcl_farming:potato_7"}, 13.516, 5) +mcl_farming:add_plant("plant_potato", "mcl_farming:potato", {"mcl_farming:potato_1", "mcl_farming:potato_2", "mcl_farming:potato_3", "mcl_farming:potato_4", "mcl_farming:potato_5", "mcl_farming:potato_6", "mcl_farming:potato_7"}, 5.4016, 25) minetest.register_on_item_eat(function (hp_change, replace_with_item, itemstack, user, pointed_thing) diff --git a/mods/ITEMS/mcl_farming/pumpkin.lua b/mods/ITEMS/mcl_farming/pumpkin.lua index d33e73f16..e06ce7774 100644 --- a/mods/ITEMS/mcl_farming/pumpkin.lua +++ b/mods/ITEMS/mcl_farming/pumpkin.lua @@ -180,10 +180,10 @@ if minetest.get_modpath("mcl_armor") then end -- Register stem growth -mcl_farming:add_plant("plant_pumpkin_stem", "mcl_farming:pumpkintige_unconnect", {"mcl_farming:pumpkin_1", "mcl_farming:pumpkin_2", "mcl_farming:pumpkin_3", "mcl_farming:pumpkin_4", "mcl_farming:pumpkin_5", "mcl_farming:pumpkin_6", "mcl_farming:pumpkin_7"}, 13.517, 5) +mcl_farming:add_plant("plant_pumpkin_stem", "mcl_farming:pumpkintige_unconnect", {"mcl_farming:pumpkin_1", "mcl_farming:pumpkin_2", "mcl_farming:pumpkin_3", "mcl_farming:pumpkin_4", "mcl_farming:pumpkin_5", "mcl_farming:pumpkin_6", "mcl_farming:pumpkin_7"}, 5.4017, 25) -- Register actual pumpkin, connected stems and stem-to-pumpkin growth -mcl_farming:add_gourd("mcl_farming:pumpkintige_unconnect", "mcl_farming:pumpkintige_linked", "mcl_farming:pumpkintige_unconnect", stem_def, stem_drop, "mcl_farming:pumpkin", pumpkin_base_def, 13.518, 5, "mcl_farming_pumpkin_stem_connected.png^[colorize:#FFA800:127") +mcl_farming:add_gourd("mcl_farming:pumpkintige_unconnect", "mcl_farming:pumpkintige_linked", "mcl_farming:pumpkintige_unconnect", stem_def, stem_drop, "mcl_farming:pumpkin", pumpkin_base_def, 5.4018, 25, "mcl_farming_pumpkin_stem_connected.png^[colorize:#FFA800:127") -- Steal function to properly disconnect a carved pumpkin pumpkin_face_base_def.after_destruct = minetest.registered_nodes["mcl_farming:pumpkin"].after_destruct diff --git a/mods/ITEMS/mcl_farming/shared_functions.lua b/mods/ITEMS/mcl_farming/shared_functions.lua index f31272dc9..80c1ce1fa 100644 --- a/mods/ITEMS/mcl_farming/shared_functions.lua +++ b/mods/ITEMS/mcl_farming/shared_functions.lua @@ -5,6 +5,8 @@ -- local math = math local vector = vector +local random = math.random +local floor = math.floor local plant_lists = {} mcl_farming.plant_lists = plant_lists -- export @@ -123,12 +125,13 @@ function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light_wate -- number of missed interval ticks, for catch-up in block loading local plant_info = plant_lists[identifier] if plant_info then - stages = stages + math.floor(get_intervals_counter(pos, plant_info.interval, plant_info.chance)) + stages = stages + floor(get_intervals_counter(pos, plant_info.interval, plant_info.chance)) end if not ignore_light_water then - local odds = math.floor(25 / (get_moisture_level(pos) * get_moisture_penalty(pos))) + 1 + local odds = floor(25 / (get_moisture_level(pos) * get_moisture_penalty(pos))) + 1 for i = 1,stages do - if math.random(1, odds) ~= 1 then stages = stages - 1 end + -- we double the odds, and rather call the ABM less often + if random() * odds >= 2 then stages = stages - 1 end end end @@ -307,7 +310,7 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s local light = minetest.get_node_light(stempos) if not light or light < 9 then return end -- Pick one neighbor and check if it can be used to grow - local dir = math.random(1, 4) -- pick direction at random + local dir = random(1, 4) -- pick direction at random local neighbor = (dir == 1 and vector.offset(stempos, 1, 0, 0)) or (dir == 2 and vector.offset(stempos, -1, 0, 0)) or (dir == 3 and vector.offset(stempos, 0, 0, 1)) @@ -322,7 +325,8 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s -- check moisture level local moisture = get_moisture_level(stempos) * get_moisture_penalty(stempos) - if math.random(1, math.floor(25 / moisture) + 1) ~= 1 then return end + -- we double the odds, and rather call the ABM less often + if random() * (math.floor(25 / moisture) + 1) >= 2 then return end minetest.swap_node(stempos, { name = connected_stem_names[dir] }) if gourd_def.paramtype2 == "facedir" then diff --git a/mods/ITEMS/mcl_farming/sweet_berry.lua b/mods/ITEMS/mcl_farming/sweet_berry.lua index 328339f01..7f565e97b 100644 --- a/mods/ITEMS/mcl_farming/sweet_berry.lua +++ b/mods/ITEMS/mcl_farming/sweet_berry.lua @@ -99,8 +99,8 @@ minetest.register_craftitem("mcl_farming:sweet_berry", { }) minetest.register_alias("mcl_sweet_berry:sweet_berry", "mcl_farming:sweet_berry") --- TODO: Find proper interval and chance values for sweet berry bushes. Current interval and chance values are copied from mcl_farming:beetroot which has similar growth stages. -mcl_farming:add_plant("plant_sweet_berry_bush", "mcl_farming:sweet_berry_bush_3", {"mcl_farming:sweet_berry_bush_0", "mcl_farming:sweet_berry_bush_1", "mcl_farming:sweet_berry_bush_2"}, 13.519, 15) +-- TODO: Find proper interval and chance values for sweet berry bushes. Current interval and chance values are copied from mcl_farming:beetroot which has similar growth stages, 1/3rd of the default. +mcl_farming:add_plant("plant_sweet_berry_bush", "mcl_farming:sweet_berry_bush_3", {"mcl_farming:sweet_berry_bush_0", "mcl_farming:sweet_berry_bush_1", "mcl_farming:sweet_berry_bush_2"}, 16.2019, 25) local function berry_damage_check(obj) local p = obj:get_pos() diff --git a/mods/ITEMS/mcl_farming/wheat.lua b/mods/ITEMS/mcl_farming/wheat.lua index d30cc77a7..b68ac4ce4 100644 --- a/mods/ITEMS/mcl_farming/wheat.lua +++ b/mods/ITEMS/mcl_farming/wheat.lua @@ -99,7 +99,7 @@ minetest.register_node("mcl_farming:wheat", { } }) -mcl_farming:add_plant("plant_wheat", "mcl_farming:wheat", {"mcl_farming:wheat_1", "mcl_farming:wheat_2", "mcl_farming:wheat_3", "mcl_farming:wheat_4", "mcl_farming:wheat_5", "mcl_farming:wheat_6", "mcl_farming:wheat_7"}, 13.520, 5) +mcl_farming:add_plant("plant_wheat", "mcl_farming:wheat", {"mcl_farming:wheat_1", "mcl_farming:wheat_2", "mcl_farming:wheat_3", "mcl_farming:wheat_4", "mcl_farming:wheat_5", "mcl_farming:wheat_6", "mcl_farming:wheat_7"}, 5.4020, 25) minetest.register_craftitem("mcl_farming:wheat_item", { description = S("Wheat"), From 540a070c596bdac9bcf70adff6f7198a5206b3d5 Mon Sep 17 00:00:00 2001 From: kno10 Date: Sun, 13 Oct 2024 21:42:42 +0200 Subject: [PATCH 06/12] always use day light level, more fixes --- mods/ITEMS/mcl_farming/shared_functions.lua | 80 +++++++------- mods/ITEMS/mcl_farming/soil.lua | 112 ++++++++------------ 2 files changed, 87 insertions(+), 105 deletions(-) diff --git a/mods/ITEMS/mcl_farming/shared_functions.lua b/mods/ITEMS/mcl_farming/shared_functions.lua index 80c1ce1fa..94450acbd 100644 --- a/mods/ITEMS/mcl_farming/shared_functions.lua +++ b/mods/ITEMS/mcl_farming/shared_functions.lua @@ -10,7 +10,7 @@ local floor = math.floor local plant_lists = {} mcl_farming.plant_lists = plant_lists -- export -local plant_nodename_to_id_list = {} -- map nodes to plants +local plant_nodename_to_id = {} -- map nodes to plants local plant_step_from_name = {} -- map nodes to growth steps local growth_factor = tonumber(minetest.settings:get("vl_plant_growth")) or 1.0 @@ -22,19 +22,19 @@ local function get_intervals_counter(pos, interval, chance) if time_multiplier == 0 then return 0 end -- "wall clock time", so plants continue to grow while sleeping local current_game_time = (minetest.get_day_count() + minetest.get_timeofday()) * time_multiplier - local approx_interval = math.max(interval, 1) * math.max(chance, 1) local meta = minetest.get_meta(pos) local last_game_time = meta:get_float("last_gametime") - if last_game_time < 1 then - last_game_time = current_game_time - approx_interval * 0.5 - elseif last_game_time == current_game_time then - current_game_time = current_game_time + approx_interval - end meta:set_float("last_gametime", current_game_time) - return (current_game_time - last_game_time) / approx_interval + if last_game_time < 1 then return 0 end + return (current_game_time - last_game_time) / (interval * chance) end +-- wetness of the surroundings +-- dry farmland = 1 point +-- wet farmland = 3 points +-- diagonal neighbors only 25% +-- center point gives + 1 point local function get_moisture_level(pos) local n = vector.offset(pos, 0, -1, 0) local totalm = 1 @@ -43,44 +43,50 @@ local function get_moisture_level(pos) for x = -1,1 do n.x = pos.x + x local ndef = minetest.registered_nodes[minetest.get_node(n).name] - local soil = ndef and ndef.groups.soil or 0 - local m = (soil == 2 and 2) or (soil >= 3 and 4) or 0 - if x ~= 0 and z ~= 0 then m = m * 0.25 end - totalm = totalm + m + local soil = ndef and ndef.groups.soil + if soil and soil >= 2 then + local m = (soil > 2 or soil == 2 and (minetest.get_meta(n):get_int("wet") or 0) > 0) and 3 or 1 + -- corners have less weight + if x ~= 0 and z ~= 0 then m = m * 0.25 end + totalm = totalm + m + end end end return totalm end -- moisture penalty function: --- 0.5 if both on the x axis and the z axis the same plant growth --- 0.5 if one diagonal neighbor is the same +-- 0.5 if both on the x axis and the z axis at least one of the same plants grows +-- 0.5 if at least one diagonal neighbor is the same -- 1.0 otherwise -local function get_moisture_penalty(pos) +-- we cannot use the names directly, because growth is encoded in the names +local function get_same_crop_penalty(pos) local name = minetest.get_node(pos).name - local n, p = vector.copy(pos), 1 - -- check adjacent points, avoid vector allocations and reduce node accesses + local plant = plant_nodename_to_id[name] + if not plant then return "unregistered plant" end + local n = vector.copy(pos) + -- check adjacent positions, avoid vector allocations and reduce node accesses n.x = pos.x - 1 - local dx = minetest.get_node(n).name == name + local dx = plant_nodename_to_id[minetest.get_node(n).name] == plant n.x = pos.x + 1 - dx = dx or minetest.get_node(n).name == name - if dx then + dx = dx or plant_nodename_to_id[minetest.get_node(n).name] == plant + if dx then -- no need to check z otherwise n.x = pos.x n.z = pos.z - 1 - local dz = minetest.get_node(n).name == name + local dz = plant_nodename_to_id[minetest.get_node(n).name] == plant n.z = pos.z + 1 - dz = dz or minetest.get_node(n).name == name + dz = dz or plant_nodename_to_id[minetest.get_node(n).name] == plant if dz then return 0.5 end end -- check diagonals, clockwise n.x, n.z = pos.x - 1, pos.z - 1 - if minetest.get_node(n).name == name then return 0.5 end + if plant_nodename_to_id[minetest.get_node(n).name] == plant then return 0.5 end n.x = pos.x + 1 - if minetest.get_node(n).name == name then return 0.5 end + if plant_nodename_to_id[minetest.get_node(n).name] == plant then return 0.5 end n.z = pos.z + 1 - if minetest.get_node(n).name == name then return 0.5 end + if plant_nodename_to_id[minetest.get_node(n).name] == plant then return 0.5 end n.x = pos.x - 1 - if minetest.get_node(n).name == name then return 0.5 end + if plant_nodename_to_id[minetest.get_node(n).name] == plant then return 0.5 end return 1 end @@ -92,7 +98,7 @@ function mcl_farming:add_plant(identifier, full_grown, names, interval, chance) plant_info.interval = interval plant_info.chance = chance for _, nodename in pairs(names) do - plant_nodename_to_id_list[nodename] = identifier + plant_nodename_to_id[nodename] = identifier end for i, name in ipairs(names) do plant_step_from_name[name] = i @@ -121,16 +127,15 @@ end function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light_water) stages = stages or 1 -- 0 when run from block loading -- check light - if not ignore_light_water and (minetest.get_node_light(pos) or 0) < 0 then return false end + if not ignore_light_water and (minetest.get_node_light(pos, 0.5) or 0) < 0 then return false end -- number of missed interval ticks, for catch-up in block loading local plant_info = plant_lists[identifier] - if plant_info then - stages = stages + floor(get_intervals_counter(pos, plant_info.interval, plant_info.chance)) - end + if not plant_info then return end + stages = stages + floor(get_intervals_counter(pos, plant_info.interval, plant_info.chance)) if not ignore_light_water then - local odds = floor(25 / (get_moisture_level(pos) * get_moisture_penalty(pos))) + 1 + local odds = floor(25 / (get_moisture_level(pos) * get_same_crop_penalty(pos))) + 1 for i = 1,stages do - -- we double the odds, and rather call the ABM less often + -- compared to MC, our ABM runs half as often, hence we use double the chance if random() * odds >= 2 then stages = stages - 1 end end end @@ -307,7 +312,7 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s interval = grow_interval, chance = grow_chance, action = function(stempos) - local light = minetest.get_node_light(stempos) + local light = minetest.get_node_light(stempos, 0.5) if not light or light < 9 then return end -- Pick one neighbor and check if it can be used to grow local dir = random(1, 4) -- pick direction at random @@ -324,9 +329,9 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s if (floordef.groups.grass_block or 0) == 0 and (floordef.groups.dirt or 0) == 0 and (floordef.groups.soil or 0) < 2 then return end -- not suitable for growing -- check moisture level - local moisture = get_moisture_level(stempos) * get_moisture_penalty(stempos) + local odds = floor(25 / (get_moisture_level(stempos) * get_same_crop_penalty(stempos))) + 1 -- we double the odds, and rather call the ABM less often - if random() * (math.floor(25 / moisture) + 1) >= 2 then return end + if random() * odds >= 2 then return end minetest.swap_node(stempos, { name = connected_stem_names[dir] }) if gourd_def.paramtype2 == "facedir" then @@ -374,13 +379,14 @@ minetest.register_lbm({ nodenames = { "group:plant" }, run_at_every_load = true, action = function(pos, node, dtime_s) - local identifier = plant_nodename_to_id_list[node.name] + local identifier = plant_nodename_to_id[node.name] if not identifier then return end mcl_farming:grow_plant(identifier, pos, node, 0, false) end, }) -- The average light levels were unreliable +-- LBM added in fall 2024 minetest.register_lbm({ label = "Drop legacy average lighting data", name = "mcl_farming:drop_average_light_meta", diff --git a/mods/ITEMS/mcl_farming/soil.lua b/mods/ITEMS/mcl_farming/soil.lua index e91feb1e8..67ea1bd3c 100644 --- a/mods/ITEMS/mcl_farming/soil.lua +++ b/mods/ITEMS/mcl_farming/soil.lua @@ -15,10 +15,6 @@ minetest.register_node("mcl_farming:soil", { {-0.5, -0.5, -0.5, 0.5, 0.4375, 0.5}, } }, - on_construct = function(pos) - local meta = minetest.get_meta(pos) - meta:set_int("wet", 0) - end, groups = {handy=1,shovely=1, dirtifies_below_solid=1, dirtifier=1, soil=2, soil_sapling=1, deco_block=1 }, sounds = mcl_sounds.node_sound_dirt_defaults(), _mcl_blast_resistance = 0.6, @@ -38,10 +34,6 @@ minetest.register_node("mcl_farming:soil_wet", { {-0.5, -0.5, -0.5, 0.5, 0.4375, 0.5}, } }, - on_construct = function(pos) - local meta = minetest.get_meta(pos) - meta:set_int("wet", 7) - end, groups = {handy=1,shovely=1, not_in_creative_inventory=1, dirtifies_below_solid=1, dirtifier=1, soil=3, soil_sapling=1 }, sounds = mcl_sounds.node_sound_dirt_defaults(), _mcl_blast_resistance = 0.6, @@ -51,76 +43,60 @@ minetest.register_node("mcl_farming:soil_wet", { minetest.register_abm({ label = "Farmland hydration", nodenames = {"mcl_farming:soil", "mcl_farming:soil_wet"}, - interval = 15, - chance = 4, + interval = 2.73, + chance = 25, action = function(pos, node) - -- Get wetness value - local meta = minetest.get_meta(pos) - local wet = meta:get_int("wet") - if not wet then - if node.name == "mcl_farming:soil" then - wet = 0 - else - wet = 7 - end - end - -- Turn back into dirt when covered by solid node - local above_node = minetest.get_node_or_nil({x=pos.x,y=pos.y+1,z=pos.z}) - if above_node then - if minetest.get_item_group(above_node.name, "solid") ~= 0 then - node.name = "mcl_core:dirt" - minetest.set_node(pos, node) - return - end + local above_node = minetest.get_node_or_nil(vector.offset(pos, 0, 1, 0)) + if above_node and minetest.get_item_group(above_node.name, "solid") ~= 0 then + node.name = "mcl_core:dirt" + minetest.set_node(pos, node) -- also removes "wet" metadata + return end - -- Check an area of 9×2×9 around the node for nodename (9×9 on same level and 9×9 below) - local function check_surroundings(pos, nodename) - local nodes = minetest.find_nodes_in_area({x=pos.x-4,y=pos.y,z=pos.z-4}, {x=pos.x+4,y=pos.y+1,z=pos.z+4}, {nodename}) - return #nodes > 0 + local raining = mcl_weather and mcl_weather.rain.raining and mcl_weather.is_outdoor(pos) + local has_water, fully_loaded = false, true + if not raining then + -- Check an area of 9×2×9 around the node for nodename (9×9 on same level and 9×9 above) + -- include "ignore" to detect unloaded blocks + local nodes, counts = minetest.find_nodes_in_area(vector.offset(pos, -4, 0, -4), vector.offset(pos, 4, 1, 4), {"group:water", "ignore"}) + local ignore = counts.ignore or 0 + has_water, fully_loaded = #nodes - ignore > 0, ignore == 0 end - if check_surroundings(pos, "group:water") then - if node.name ~= "mcl_farming:soil_wet" then - -- Make it wet + local meta = minetest.get_meta(pos) + local wet = meta:get_int("wet") or (node.name == "mcl_farming:soil" and 0 or 7) + -- Hydrate by rain or water + if raining or has_water then + if node.name == "mcl_farming:soil" then node.name = "mcl_farming:soil_wet" + minetest.set_node(pos, node) -- resets wetness + meta:set_int("wet", 7) + elseif wet < 7 then + meta:set_int("wet", 7) + end + return + end + -- No decay near unloaded areas (ignore) since these might include water. + if not fully_loaded then return end + + -- Decay: make farmland dry or turn back to dirt + if wet > 1 then + if node.name == "mcl_farming:soil_wet" then -- change visual appearance to dry + node.name = "mcl_farming:soil" minetest.set_node(pos, node) end - else -- No water nearby. - -- The decay branch (make farmland dry or turn back to dirt) - - -- Don't decay while it's raining - if mcl_weather.rain.raining then - if mcl_weather.is_outdoor(pos) then - return - end - end - -- No decay near unloaded areas since these might include water. - if not check_surroundings(pos, "ignore") then - if wet <= 0 then - --local n_def = minetest.registered_nodes[node.name] or nil - local nn = minetest.get_node_or_nil({x=pos.x,y=pos.y+1,z=pos.z}) - if not nn or not nn.name then - return - end - local nn_def = minetest.registered_nodes[nn.name] or nil - - if nn_def and minetest.get_item_group(nn.name, "plant") == 0 then - node.name = "mcl_core:dirt" - minetest.set_node(pos, node) - return - end - else - if wet == 7 then - node.name = "mcl_farming:soil" - minetest.swap_node(pos, node) - end - -- Slowly count down wetness - meta:set_int("wet", wet-1) - end - end + meta:set_int("wet", wet - 1) + return end + -- Revert to dirt if wetness is 0, and no plant above + local nn = minetest.get_node_or_nil(vector.offset(pos, 0, 1, 0)) + local nn_def = nn and minetest.registered_nodes[nn.name] or nil + if nn_def and (nn_def.groups.plant or 0) > 0 then + return + end + node.name = "mcl_core:dirt" + minetest.set_node(pos, node) -- also removes "wet" metadata end, }) From 220a7b06e67506313e3c004a17fce00fb0fbfc68 Mon Sep 17 00:00:00 2001 From: kno10 Date: Tue, 15 Oct 2024 10:18:22 +0200 Subject: [PATCH 07/12] code review feedback --- mods/ITEMS/mcl_farming/shared_functions.lua | 9 +++++---- mods/ITEMS/mcl_farming/soil.lua | 6 ++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/mods/ITEMS/mcl_farming/shared_functions.lua b/mods/ITEMS/mcl_farming/shared_functions.lua index 94450acbd..f7d5c9584 100644 --- a/mods/ITEMS/mcl_farming/shared_functions.lua +++ b/mods/ITEMS/mcl_farming/shared_functions.lua @@ -45,7 +45,7 @@ local function get_moisture_level(pos) local ndef = minetest.registered_nodes[minetest.get_node(n).name] local soil = ndef and ndef.groups.soil if soil and soil >= 2 then - local m = (soil > 2 or soil == 2 and (minetest.get_meta(n):get_int("wet") or 0) > 0) and 3 or 1 + local m = (soil > 2 or (soil == 2 and minetest.get_meta(n):get_int("wet") > 0)) and 3 or 1 -- corners have less weight if x ~= 0 and z ~= 0 then m = m * 0.25 end totalm = totalm + m @@ -63,7 +63,7 @@ end local function get_same_crop_penalty(pos) local name = minetest.get_node(pos).name local plant = plant_nodename_to_id[name] - if not plant then return "unregistered plant" end + if not plant then return end local n = vector.copy(pos) -- check adjacent positions, avoid vector allocations and reduce node accesses n.x = pos.x - 1 @@ -135,7 +135,7 @@ function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light_wate if not ignore_light_water then local odds = floor(25 / (get_moisture_level(pos) * get_same_crop_penalty(pos))) + 1 for i = 1,stages do - -- compared to MC, our ABM runs half as often, hence we use double the chance + -- compared to info from the MC wiki, our ABM runs half as often, hence we use double the chance if random() * odds >= 2 then stages = stages - 1 end end end @@ -145,7 +145,8 @@ function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light_wate if step == nil then return false end minetest.set_node(pos, { name = plant_info.names[step + stages] or plant_info.full_grown, - param = node.param, param2 = node.param2, + param = node.param, + param2 = node.param2, }) return true end diff --git a/mods/ITEMS/mcl_farming/soil.lua b/mods/ITEMS/mcl_farming/soil.lua index 67ea1bd3c..d943a7000 100644 --- a/mods/ITEMS/mcl_farming/soil.lua +++ b/mods/ITEMS/mcl_farming/soil.lua @@ -91,10 +91,8 @@ minetest.register_abm({ end -- Revert to dirt if wetness is 0, and no plant above local nn = minetest.get_node_or_nil(vector.offset(pos, 0, 1, 0)) - local nn_def = nn and minetest.registered_nodes[nn.name] or nil - if nn_def and (nn_def.groups.plant or 0) > 0 then - return - end + local nn_def = nn and minetest.registered_nodes[nn.name] + if nn_def and (nn_def.groups.plant or 0) > 0 then return end node.name = "mcl_core:dirt" minetest.set_node(pos, node) -- also removes "wet" metadata end, From c097c652626ba466a2e062e0fc1dafecc55e8be4 Mon Sep 17 00:00:00 2001 From: kno10 Date: Wed, 16 Oct 2024 00:00:05 +0200 Subject: [PATCH 08/12] adjust growth rates again --- mods/ITEMS/mcl_farming/beetroot.lua | 4 ++-- mods/ITEMS/mcl_farming/carrots.lua | 2 +- mods/ITEMS/mcl_farming/melon.lua | 4 ++-- mods/ITEMS/mcl_farming/potatoes.lua | 2 +- mods/ITEMS/mcl_farming/pumpkin.lua | 4 ++-- mods/ITEMS/mcl_farming/sweet_berry.lua | 4 ++-- mods/ITEMS/mcl_farming/wheat.lua | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/mods/ITEMS/mcl_farming/beetroot.lua b/mods/ITEMS/mcl_farming/beetroot.lua index 904274b8e..4c77f3dad 100644 --- a/mods/ITEMS/mcl_farming/beetroot.lua +++ b/mods/ITEMS/mcl_farming/beetroot.lua @@ -171,8 +171,8 @@ minetest.register_craft({ }, }) --- beetroots grow 1/3rd of the default speed -mcl_farming:add_plant("plant_beetroot", "mcl_farming:beetroot", {"mcl_farming:beetroot_0", "mcl_farming:beetroot_1", "mcl_farming:beetroot_2"}, 16.2012, 25) +-- beetroots grow at 2/3rd of the default speed +mcl_farming:add_plant("plant_beetroot", "mcl_farming:beetroot", {"mcl_farming:beetroot_0", "mcl_farming:beetroot_1", "mcl_farming:beetroot_2"}, 8.7012, 35) if minetest.get_modpath("doc") then for i = 1, 2 do diff --git a/mods/ITEMS/mcl_farming/carrots.lua b/mods/ITEMS/mcl_farming/carrots.lua index 580e4da5a..6f7d7a6aa 100644 --- a/mods/ITEMS/mcl_farming/carrots.lua +++ b/mods/ITEMS/mcl_farming/carrots.lua @@ -118,7 +118,7 @@ minetest.register_craft({ } }) -mcl_farming:add_plant("plant_carrot", "mcl_farming:carrot", {"mcl_farming:carrot_1", "mcl_farming:carrot_2", "mcl_farming:carrot_3", "mcl_farming:carrot_4", "mcl_farming:carrot_5", "mcl_farming:carrot_6", "mcl_farming:carrot_7"}, 5.4013, 25) +mcl_farming:add_plant("plant_carrot", "mcl_farming:carrot", {"mcl_farming:carrot_1", "mcl_farming:carrot_2", "mcl_farming:carrot_3", "mcl_farming:carrot_4", "mcl_farming:carrot_5", "mcl_farming:carrot_6", "mcl_farming:carrot_7"}, 5.8013, 35) if minetest.get_modpath("doc") then for i=2,7 do diff --git a/mods/ITEMS/mcl_farming/melon.lua b/mods/ITEMS/mcl_farming/melon.lua index 74c95e686..205150b6e 100644 --- a/mods/ITEMS/mcl_farming/melon.lua +++ b/mods/ITEMS/mcl_farming/melon.lua @@ -123,10 +123,10 @@ local stem_def = { } -- Register stem growth -mcl_farming:add_plant("plant_melon_stem", "mcl_farming:melontige_unconnect", {"mcl_farming:melontige_1", "mcl_farming:melontige_2", "mcl_farming:melontige_3", "mcl_farming:melontige_4", "mcl_farming:melontige_5", "mcl_farming:melontige_6", "mcl_farming:melontige_7"}, 5.4014, 25) +mcl_farming:add_plant("plant_melon_stem", "mcl_farming:melontige_unconnect", {"mcl_farming:melontige_1", "mcl_farming:melontige_2", "mcl_farming:melontige_3", "mcl_farming:melontige_4", "mcl_farming:melontige_5", "mcl_farming:melontige_6", "mcl_farming:melontige_7"}, 5.8014, 35) -- Register actual melon, connected stems and stem-to-melon growth -mcl_farming:add_gourd("mcl_farming:melontige_unconnect", "mcl_farming:melontige_linked", "mcl_farming:melontige_unconnect", stem_def, stem_drop, "mcl_farming:melon", melon_base_def, 5.4015, 25, "mcl_farming_melon_stem_connected.png^[colorize:#FFA800:127") +mcl_farming:add_gourd("mcl_farming:melontige_unconnect", "mcl_farming:melontige_linked", "mcl_farming:melontige_unconnect", stem_def, stem_drop, "mcl_farming:melon", melon_base_def, 5.8015, 35, "mcl_farming_melon_stem_connected.png^[colorize:#FFA800:127") -- Items and crafting minetest.register_craftitem("mcl_farming:melon_item", { diff --git a/mods/ITEMS/mcl_farming/potatoes.lua b/mods/ITEMS/mcl_farming/potatoes.lua index 455befa7d..2dc4a3522 100644 --- a/mods/ITEMS/mcl_farming/potatoes.lua +++ b/mods/ITEMS/mcl_farming/potatoes.lua @@ -135,7 +135,7 @@ minetest.register_craft({ cooktime = 10, }) -mcl_farming:add_plant("plant_potato", "mcl_farming:potato", {"mcl_farming:potato_1", "mcl_farming:potato_2", "mcl_farming:potato_3", "mcl_farming:potato_4", "mcl_farming:potato_5", "mcl_farming:potato_6", "mcl_farming:potato_7"}, 5.4016, 25) +mcl_farming:add_plant("plant_potato", "mcl_farming:potato", {"mcl_farming:potato_1", "mcl_farming:potato_2", "mcl_farming:potato_3", "mcl_farming:potato_4", "mcl_farming:potato_5", "mcl_farming:potato_6", "mcl_farming:potato_7"}, 5.8016, 35) minetest.register_on_item_eat(function (hp_change, replace_with_item, itemstack, user, pointed_thing) diff --git a/mods/ITEMS/mcl_farming/pumpkin.lua b/mods/ITEMS/mcl_farming/pumpkin.lua index e06ce7774..1befef8a8 100644 --- a/mods/ITEMS/mcl_farming/pumpkin.lua +++ b/mods/ITEMS/mcl_farming/pumpkin.lua @@ -180,10 +180,10 @@ if minetest.get_modpath("mcl_armor") then end -- Register stem growth -mcl_farming:add_plant("plant_pumpkin_stem", "mcl_farming:pumpkintige_unconnect", {"mcl_farming:pumpkin_1", "mcl_farming:pumpkin_2", "mcl_farming:pumpkin_3", "mcl_farming:pumpkin_4", "mcl_farming:pumpkin_5", "mcl_farming:pumpkin_6", "mcl_farming:pumpkin_7"}, 5.4017, 25) +mcl_farming:add_plant("plant_pumpkin_stem", "mcl_farming:pumpkintige_unconnect", {"mcl_farming:pumpkin_1", "mcl_farming:pumpkin_2", "mcl_farming:pumpkin_3", "mcl_farming:pumpkin_4", "mcl_farming:pumpkin_5", "mcl_farming:pumpkin_6", "mcl_farming:pumpkin_7"}, 5.8017, 35) -- Register actual pumpkin, connected stems and stem-to-pumpkin growth -mcl_farming:add_gourd("mcl_farming:pumpkintige_unconnect", "mcl_farming:pumpkintige_linked", "mcl_farming:pumpkintige_unconnect", stem_def, stem_drop, "mcl_farming:pumpkin", pumpkin_base_def, 5.4018, 25, "mcl_farming_pumpkin_stem_connected.png^[colorize:#FFA800:127") +mcl_farming:add_gourd("mcl_farming:pumpkintige_unconnect", "mcl_farming:pumpkintige_linked", "mcl_farming:pumpkintige_unconnect", stem_def, stem_drop, "mcl_farming:pumpkin", pumpkin_base_def, 5.8018, 35, "mcl_farming_pumpkin_stem_connected.png^[colorize:#FFA800:127") -- Steal function to properly disconnect a carved pumpkin pumpkin_face_base_def.after_destruct = minetest.registered_nodes["mcl_farming:pumpkin"].after_destruct diff --git a/mods/ITEMS/mcl_farming/sweet_berry.lua b/mods/ITEMS/mcl_farming/sweet_berry.lua index 7f565e97b..a03eee012 100644 --- a/mods/ITEMS/mcl_farming/sweet_berry.lua +++ b/mods/ITEMS/mcl_farming/sweet_berry.lua @@ -99,8 +99,8 @@ minetest.register_craftitem("mcl_farming:sweet_berry", { }) minetest.register_alias("mcl_sweet_berry:sweet_berry", "mcl_farming:sweet_berry") --- TODO: Find proper interval and chance values for sweet berry bushes. Current interval and chance values are copied from mcl_farming:beetroot which has similar growth stages, 1/3rd of the default. -mcl_farming:add_plant("plant_sweet_berry_bush", "mcl_farming:sweet_berry_bush_3", {"mcl_farming:sweet_berry_bush_0", "mcl_farming:sweet_berry_bush_1", "mcl_farming:sweet_berry_bush_2"}, 16.2019, 25) +-- TODO: Find proper interval and chance values for sweet berry bushes. Current interval and chance values are copied from mcl_farming:beetroot which has similar growth stages, 2/3rd of the default. +mcl_farming:add_plant("plant_sweet_berry_bush", "mcl_farming:sweet_berry_bush_3", {"mcl_farming:sweet_berry_bush_0", "mcl_farming:sweet_berry_bush_1", "mcl_farming:sweet_berry_bush_2"}, 8.7019, 35) local function berry_damage_check(obj) local p = obj:get_pos() diff --git a/mods/ITEMS/mcl_farming/wheat.lua b/mods/ITEMS/mcl_farming/wheat.lua index b68ac4ce4..4ddbb156a 100644 --- a/mods/ITEMS/mcl_farming/wheat.lua +++ b/mods/ITEMS/mcl_farming/wheat.lua @@ -99,7 +99,7 @@ minetest.register_node("mcl_farming:wheat", { } }) -mcl_farming:add_plant("plant_wheat", "mcl_farming:wheat", {"mcl_farming:wheat_1", "mcl_farming:wheat_2", "mcl_farming:wheat_3", "mcl_farming:wheat_4", "mcl_farming:wheat_5", "mcl_farming:wheat_6", "mcl_farming:wheat_7"}, 5.4020, 25) +mcl_farming:add_plant("plant_wheat", "mcl_farming:wheat", {"mcl_farming:wheat_1", "mcl_farming:wheat_2", "mcl_farming:wheat_3", "mcl_farming:wheat_4", "mcl_farming:wheat_5", "mcl_farming:wheat_6", "mcl_farming:wheat_7"}, 5.8020, 35) minetest.register_craftitem("mcl_farming:wheat_item", { description = S("Wheat"), From fa7a7f4e8159ba2bac9109ce19b4d94f06f4a671 Mon Sep 17 00:00:00 2001 From: kno10 Date: Wed, 16 Oct 2024 00:24:07 +0200 Subject: [PATCH 09/12] more fixes to plant growth --- mods/ITEMS/mcl_farming/shared_functions.lua | 26 +++++++++++---------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/mods/ITEMS/mcl_farming/shared_functions.lua b/mods/ITEMS/mcl_farming/shared_functions.lua index f7d5c9584..61a0df2f2 100644 --- a/mods/ITEMS/mcl_farming/shared_functions.lua +++ b/mods/ITEMS/mcl_farming/shared_functions.lua @@ -15,14 +15,13 @@ local plant_step_from_name = {} -- map nodes to growth steps local growth_factor = tonumber(minetest.settings:get("vl_plant_growth")) or 1.0 +-- note: does not support /set time_speed! local time_speed = tonumber(minetest.settings:get("time_speed")) or 72 local time_multiplier = time_speed > 0 and (86400 / time_speed) or 0 local function get_intervals_counter(pos, interval, chance) - if time_multiplier == 0 then return 0 end - -- "wall clock time", so plants continue to grow while sleeping + -- in-game days, so plants continue to grow while sleeping, and we try to catch up local current_game_time = (minetest.get_day_count() + minetest.get_timeofday()) * time_multiplier - local meta = minetest.get_meta(pos) local last_game_time = meta:get_float("last_gametime") meta:set_float("last_gametime", current_game_time) @@ -33,8 +32,8 @@ end -- wetness of the surroundings -- dry farmland = 1 point -- wet farmland = 3 points --- diagonal neighbors only 25% --- center point gives + 1 point +-- center point gives + 1 point, so 2 resp. 4 +-- neighbors only 25% local function get_moisture_level(pos) local n = vector.offset(pos, 0, -1, 0) local totalm = 1 @@ -47,7 +46,7 @@ local function get_moisture_level(pos) if soil and soil >= 2 then local m = (soil > 2 or (soil == 2 and minetest.get_meta(n):get_int("wet") > 0)) and 3 or 1 -- corners have less weight - if x ~= 0 and z ~= 0 then m = m * 0.25 end + if x ~= 0 or z ~= 0 then m = m * 0.25 end totalm = totalm + m end end @@ -63,7 +62,7 @@ end local function get_same_crop_penalty(pos) local name = minetest.get_node(pos).name local plant = plant_nodename_to_id[name] - if not plant then return end + if not plant then return 1 end local n = vector.copy(pos) -- check adjacent positions, avoid vector allocations and reduce node accesses n.x = pos.x - 1 @@ -97,6 +96,7 @@ function mcl_farming:add_plant(identifier, full_grown, names, interval, chance) plant_info.names = names plant_info.interval = interval plant_info.chance = chance + plant_nodename_to_id[full_grown] = identifier for _, nodename in pairs(names) do plant_nodename_to_id[nodename] = identifier end @@ -131,12 +131,12 @@ function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light_wate -- number of missed interval ticks, for catch-up in block loading local plant_info = plant_lists[identifier] if not plant_info then return end - stages = stages + floor(get_intervals_counter(pos, plant_info.interval, plant_info.chance)) + stages = floor(stages + get_intervals_counter(pos, plant_info.interval, plant_info.chance) - 0.45) if not ignore_light_water then local odds = floor(25 / (get_moisture_level(pos) * get_same_crop_penalty(pos))) + 1 for i = 1,stages do - -- compared to info from the MC wiki, our ABM runs half as often, hence we use double the chance - if random() * odds >= 2 then stages = stages - 1 end + -- compared to info from the MC wiki, our ABM runs a third as often, hence we use triple the chance + if random() * odds >= 3 then stages = stages - 1 end end end @@ -237,6 +237,7 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s stem_def.groups = stem_def.groups or { dig_immediate = 3, not_in_creative_inventory = 1, plant = 1, attached_node = 1, dig_by_water = 1, destroy_by_lava_flow = 1 } stem_def.sounds = stem_def.sounds or mcl_sounds.node_sound_leaves_defaults() minetest.register_node(stem_itemstring, stem_def) + plant_nodename_to_id[stem_itemstring] = stem_itemstring -- Register connected stems @@ -299,6 +300,7 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s sounds = mcl_sounds.node_sound_leaves_defaults(), _mcl_blast_resistance = 0, }) + plant_nodename_to_id[connected_stem_names[i]] = stem_itemstring if minetest.get_modpath("doc") then doc.add_entry_alias("nodes", full_unconnected_stem, "nodes", connected_stem_names[i]) @@ -331,8 +333,8 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s -- check moisture level local odds = floor(25 / (get_moisture_level(stempos) * get_same_crop_penalty(stempos))) + 1 - -- we double the odds, and rather call the ABM less often - if random() * odds >= 2 then return end + -- we triple the odds, and rather call the ABM less often + if random() * odds >= 3 then return end minetest.swap_node(stempos, { name = connected_stem_names[dir] }) if gourd_def.paramtype2 == "facedir" then From a8318f66006a053064cbb6e7370d518dc414e679 Mon Sep 17 00:00:00 2001 From: kno10 Date: Wed, 16 Oct 2024 21:19:38 +0200 Subject: [PATCH 10/12] simplify catch-up LBM logic --- mods/ITEMS/mcl_farming/shared_functions.lua | 47 ++++++--------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/mods/ITEMS/mcl_farming/shared_functions.lua b/mods/ITEMS/mcl_farming/shared_functions.lua index 61a0df2f2..a33e414ed 100644 --- a/mods/ITEMS/mcl_farming/shared_functions.lua +++ b/mods/ITEMS/mcl_farming/shared_functions.lua @@ -15,20 +15,6 @@ local plant_step_from_name = {} -- map nodes to growth steps local growth_factor = tonumber(minetest.settings:get("vl_plant_growth")) or 1.0 --- note: does not support /set time_speed! -local time_speed = tonumber(minetest.settings:get("time_speed")) or 72 -local time_multiplier = time_speed > 0 and (86400 / time_speed) or 0 - -local function get_intervals_counter(pos, interval, chance) - -- in-game days, so plants continue to grow while sleeping, and we try to catch up - local current_game_time = (minetest.get_day_count() + minetest.get_timeofday()) * time_multiplier - local meta = minetest.get_meta(pos) - local last_game_time = meta:get_float("last_gametime") - meta:set_float("last_gametime", current_game_time) - if last_game_time < 1 then return 0 end - return (current_game_time - last_game_time) / (interval * chance) -end - -- wetness of the surroundings -- dry farmland = 1 point -- wet farmland = 3 points @@ -125,14 +111,11 @@ end -- Returns true if plant has been grown by 1 or more stages. -- Returns false if nothing changed. function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light_water) - stages = stages or 1 -- 0 when run from block loading - -- check light - if not ignore_light_water and (minetest.get_node_light(pos, 0.5) or 0) < 0 then return false end -- number of missed interval ticks, for catch-up in block loading local plant_info = plant_lists[identifier] if not plant_info then return end - stages = floor(stages + get_intervals_counter(pos, plant_info.interval, plant_info.chance) - 0.45) if not ignore_light_water then + if (minetest.get_node_light(pos, 0.5) or 0) < 0 then return false end -- day light local odds = floor(25 / (get_moisture_level(pos) * get_same_crop_penalty(pos))) + 1 for i = 1,stages do -- compared to info from the MC wiki, our ABM runs a third as often, hence we use triple the chance @@ -384,21 +367,19 @@ minetest.register_lbm({ action = function(pos, node, dtime_s) local identifier = plant_nodename_to_id[node.name] if not identifier then return end - mcl_farming:grow_plant(identifier, pos, node, 0, false) - end, -}) - --- The average light levels were unreliable --- LBM added in fall 2024 -minetest.register_lbm({ - label = "Drop legacy average lighting data", - name = "mcl_farming:drop_average_light_meta", - nodenames = { "group:plant" }, - run_at_every_load = false, -- only convert once - action = function(pos, node, dtime_s) - local meta = minetest.get_meta(pos) - meta:set_string("avg_light_summary", "") -- drop - meta:set_string("avg_light_count", "") -- drop + + local plant_info = plant_lists[identifier] + if not plant_info then return end + local rolls = floor(dtime_s / plant_info.interval) + if rolls <= 0 then return end + -- simulate how often the block will be ticked + local stages = 0 + for i = 1,rolls do + if random(1, plant_info.chance) == 1 then stages = stages + 1 end + end + if stages > 0 then + mcl_farming:grow_plant(identifier, pos, node, stages, false) + end end, }) From ebf6cf32e89fe1d402e1859a81c4f361153beb27 Mon Sep 17 00:00:00 2001 From: kno10 Date: Sat, 2 Nov 2024 21:05:57 +0100 Subject: [PATCH 11/12] meta:set_private("wet"), require only walkable nodes --- mods/ITEMS/mcl_farming/shared_functions.lua | 5 ++--- mods/ITEMS/mcl_farming/soil.lua | 6 +++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/mods/ITEMS/mcl_farming/shared_functions.lua b/mods/ITEMS/mcl_farming/shared_functions.lua index a33e414ed..6ebf659ca 100644 --- a/mods/ITEMS/mcl_farming/shared_functions.lua +++ b/mods/ITEMS/mcl_farming/shared_functions.lua @@ -307,12 +307,11 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s or (dir == 3 and vector.offset(stempos, 0, 0, 1)) or vector.offset(stempos, 0, 0, -1) if minetest.get_node(neighbor).name ~= "air" then return end -- occupied - -- check for suitable floor: grass, dirt, or soil + -- check for suitable floor -- in contrast to MC, we think everything solid is fine local floorpos = vector.offset(neighbor, 0, -1, 0) local floorname = minetest.get_node(floorpos).name local floordef = minetest.registered_nodes[floorname] - if not floordef then return end - if (floordef.groups.grass_block or 0) == 0 and (floordef.groups.dirt or 0) == 0 and (floordef.groups.soil or 0) < 2 then return end -- not suitable for growing + if not floordef or not floordef.walkable then return end -- check moisture level local odds = floor(25 / (get_moisture_level(stempos) * get_same_crop_penalty(stempos))) + 1 diff --git a/mods/ITEMS/mcl_farming/soil.lua b/mods/ITEMS/mcl_farming/soil.lua index d943a7000..32f624c3c 100644 --- a/mods/ITEMS/mcl_farming/soil.lua +++ b/mods/ITEMS/mcl_farming/soil.lua @@ -72,6 +72,7 @@ minetest.register_abm({ node.name = "mcl_farming:soil_wet" minetest.set_node(pos, node) -- resets wetness meta:set_int("wet", 7) + meta:mark_as_private("wet") elseif wet < 7 then meta:set_int("wet", 7) end @@ -85,8 +86,11 @@ minetest.register_abm({ if node.name == "mcl_farming:soil_wet" then -- change visual appearance to dry node.name = "mcl_farming:soil" minetest.set_node(pos, node) + meta:set_int("wet", wet - 1) + meta:mark_as_private("wet") -- after set_int + else + meta:set_int("wet", wet - 1) end - meta:set_int("wet", wet - 1) return end -- Revert to dirt if wetness is 0, and no plant above From b5afa34469673766dd11969e9fada9494326b0dc Mon Sep 17 00:00:00 2001 From: kno10 Date: Sat, 2 Nov 2024 23:41:38 +0100 Subject: [PATCH 12/12] Remove "wet" metadata altogether --- mods/ITEMS/mcl_farming/shared_functions.lua | 2 +- mods/ITEMS/mcl_farming/soil.lua | 63 +++++++++------------ 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/mods/ITEMS/mcl_farming/shared_functions.lua b/mods/ITEMS/mcl_farming/shared_functions.lua index 6ebf659ca..2cf5514a2 100644 --- a/mods/ITEMS/mcl_farming/shared_functions.lua +++ b/mods/ITEMS/mcl_farming/shared_functions.lua @@ -30,7 +30,7 @@ local function get_moisture_level(pos) local ndef = minetest.registered_nodes[minetest.get_node(n).name] local soil = ndef and ndef.groups.soil if soil and soil >= 2 then - local m = (soil > 2 or (soil == 2 and minetest.get_meta(n):get_int("wet") > 0)) and 3 or 1 + local m = soil > 2 and 3 or 1 -- corners have less weight if x ~= 0 or z ~= 0 then m = m * 0.25 end totalm = totalm + m diff --git a/mods/ITEMS/mcl_farming/soil.lua b/mods/ITEMS/mcl_farming/soil.lua index 32f624c3c..6196f5286 100644 --- a/mods/ITEMS/mcl_farming/soil.lua +++ b/mods/ITEMS/mcl_farming/soil.lua @@ -50,55 +50,48 @@ minetest.register_abm({ local above_node = minetest.get_node_or_nil(vector.offset(pos, 0, 1, 0)) if above_node and minetest.get_item_group(above_node.name, "solid") ~= 0 then node.name = "mcl_core:dirt" - minetest.set_node(pos, node) -- also removes "wet" metadata + minetest.set_node(pos, node) return end - local raining = mcl_weather and mcl_weather.rain.raining and mcl_weather.is_outdoor(pos) - local has_water, fully_loaded = false, true - if not raining then - -- Check an area of 9×2×9 around the node for nodename (9×9 on same level and 9×9 above) - -- include "ignore" to detect unloaded blocks - local nodes, counts = minetest.find_nodes_in_area(vector.offset(pos, -4, 0, -4), vector.offset(pos, 4, 1, 4), {"group:water", "ignore"}) - local ignore = counts.ignore or 0 - has_water, fully_loaded = #nodes - ignore > 0, ignore == 0 - end - - local meta = minetest.get_meta(pos) - local wet = meta:get_int("wet") or (node.name == "mcl_farming:soil" and 0 or 7) - -- Hydrate by rain or water - if raining or has_water then + -- in rain, become wet, do not decay + if mcl_weather and mcl_weather.rain.raining and mcl_weather.is_outdoor(pos) then if node.name == "mcl_farming:soil" then node.name = "mcl_farming:soil_wet" - minetest.set_node(pos, node) -- resets wetness - meta:set_int("wet", 7) - meta:mark_as_private("wet") - elseif wet < 7 then - meta:set_int("wet", 7) + minetest.set_node(pos, node) + end + return + end + + -- Check an area of 9x2x9 around the node for nodename (9x9 on same level and 9x9 above) + -- include "ignore" to detect unloaded blocks + local nodes, counts = minetest.find_nodes_in_area(vector.offset(pos, -4, 0, -4), vector.offset(pos, 4, 1, 4), {"group:water", "ignore"}) + local ignore = counts.ignore or 0 + local has_water, fully_loaded = #nodes > ignore, ignore == 0 + + -- Hydrate by rain or water, do not decay + if has_water then + if node.name == "mcl_farming:soil" then + node.name = "mcl_farming:soil_wet" + minetest.set_node(pos, node) end return end -- No decay near unloaded areas (ignore) since these might include water. if not fully_loaded then return end - -- Decay: make farmland dry or turn back to dirt - if wet > 1 then - if node.name == "mcl_farming:soil_wet" then -- change visual appearance to dry - node.name = "mcl_farming:soil" - minetest.set_node(pos, node) - meta:set_int("wet", wet - 1) - meta:mark_as_private("wet") -- after set_int - else - meta:set_int("wet", wet - 1) - end + -- Decay: make wet farmland dry up + if node.name == "mcl_farming:soil_wet" then + node.name = "mcl_farming:soil" + minetest.set_node(pos, node) return end -- Revert to dirt if wetness is 0, and no plant above - local nn = minetest.get_node_or_nil(vector.offset(pos, 0, 1, 0)) - local nn_def = nn and minetest.registered_nodes[nn.name] - if nn_def and (nn_def.groups.plant or 0) > 0 then return end - node.name = "mcl_core:dirt" - minetest.set_node(pos, node) -- also removes "wet" metadata + local above = minetest.get_node_or_nil(vector.offset(pos, 0, 1, 0)) + if minetest.get_item_group(above.name, "plant") == 0 then + node.name = "mcl_core:dirt" + minetest.set_node(pos, node) + end end, })