always use day light level, more fixes

This commit is contained in:
kno10 2024-10-13 21:42:42 +02:00 committed by the-real-herowl
parent 78a958db4e
commit 540a070c59
2 changed files with 87 additions and 105 deletions

View file

@ -10,7 +10,7 @@ local floor = math.floor
local plant_lists = {} local plant_lists = {}
mcl_farming.plant_lists = plant_lists -- export 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 plant_step_from_name = {} -- map nodes to growth steps
local growth_factor = tonumber(minetest.settings:get("vl_plant_growth")) or 1.0 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 if time_multiplier == 0 then return 0 end
-- "wall clock time", so plants continue to grow while sleeping -- "wall clock time", so plants continue to grow while sleeping
local current_game_time = (minetest.get_day_count() + minetest.get_timeofday()) * time_multiplier 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 meta = minetest.get_meta(pos)
local last_game_time = meta:get_float("last_gametime") 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) 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 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 function get_moisture_level(pos)
local n = vector.offset(pos, 0, -1, 0) local n = vector.offset(pos, 0, -1, 0)
local totalm = 1 local totalm = 1
@ -43,44 +43,50 @@ local function get_moisture_level(pos)
for x = -1,1 do for x = -1,1 do
n.x = pos.x + x n.x = pos.x + x
local ndef = minetest.registered_nodes[minetest.get_node(n).name] local ndef = minetest.registered_nodes[minetest.get_node(n).name]
local soil = ndef and ndef.groups.soil or 0 local soil = ndef and ndef.groups.soil
local m = (soil == 2 and 2) or (soil >= 3 and 4) or 0 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 if x ~= 0 and z ~= 0 then m = m * 0.25 end
totalm = totalm + m totalm = totalm + m
end end
end end
end
return totalm return totalm
end end
-- moisture penalty function: -- moisture penalty function:
-- 0.5 if both on the x axis and the z axis the same plant growth -- 0.5 if both on the x axis and the z axis at least one of the same plants grows
-- 0.5 if one diagonal neighbor is the same -- 0.5 if at least one diagonal neighbor is the same
-- 1.0 otherwise -- 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 name = minetest.get_node(pos).name
local n, p = vector.copy(pos), 1 local plant = plant_nodename_to_id[name]
-- check adjacent points, avoid vector allocations and reduce node accesses 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 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 n.x = pos.x + 1
dx = dx or minetest.get_node(n).name == name dx = dx or plant_nodename_to_id[minetest.get_node(n).name] == plant
if dx then if dx then -- no need to check z otherwise
n.x = pos.x n.x = pos.x
n.z = pos.z - 1 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 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 if dz then return 0.5 end
end end
-- check diagonals, clockwise -- check diagonals, clockwise
n.x, n.z = pos.x - 1, pos.z - 1 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 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 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 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 return 1
end end
@ -92,7 +98,7 @@ function mcl_farming:add_plant(identifier, full_grown, names, interval, chance)
plant_info.interval = interval plant_info.interval = interval
plant_info.chance = chance plant_info.chance = chance
for _, nodename in pairs(names) do for _, nodename in pairs(names) do
plant_nodename_to_id_list[nodename] = identifier plant_nodename_to_id[nodename] = identifier
end end
for i, name in ipairs(names) do for i, name in ipairs(names) do
plant_step_from_name[name] = i plant_step_from_name[name] = i
@ -121,16 +127,15 @@ end
function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light_water) function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light_water)
stages = stages or 1 -- 0 when run from block loading stages = stages or 1 -- 0 when run from block loading
-- check light -- 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 -- number of missed interval ticks, for catch-up in block loading
local plant_info = plant_lists[identifier] local plant_info = plant_lists[identifier]
if plant_info then if not plant_info then return end
stages = stages + 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 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 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 if random() * odds >= 2 then stages = stages - 1 end
end end
end end
@ -307,7 +312,7 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s
interval = grow_interval, interval = grow_interval,
chance = grow_chance, chance = grow_chance,
action = function(stempos) 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 if not light or light < 9 then return end
-- Pick one neighbor and check if it can be used to grow -- Pick one neighbor and check if it can be used to grow
local dir = random(1, 4) -- pick direction at random 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 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 -- 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 -- 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] }) minetest.swap_node(stempos, { name = connected_stem_names[dir] })
if gourd_def.paramtype2 == "facedir" then if gourd_def.paramtype2 == "facedir" then
@ -374,13 +379,14 @@ minetest.register_lbm({
nodenames = { "group:plant" }, nodenames = { "group:plant" },
run_at_every_load = true, run_at_every_load = true,
action = function(pos, node, dtime_s) 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 if not identifier then return end
mcl_farming:grow_plant(identifier, pos, node, 0, false) mcl_farming:grow_plant(identifier, pos, node, 0, false)
end, end,
}) })
-- The average light levels were unreliable -- The average light levels were unreliable
-- LBM added in fall 2024
minetest.register_lbm({ minetest.register_lbm({
label = "Drop legacy average lighting data", label = "Drop legacy average lighting data",
name = "mcl_farming:drop_average_light_meta", name = "mcl_farming:drop_average_light_meta",

View file

@ -15,10 +15,6 @@ minetest.register_node("mcl_farming:soil", {
{-0.5, -0.5, -0.5, 0.5, 0.4375, 0.5}, {-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 }, 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(), sounds = mcl_sounds.node_sound_dirt_defaults(),
_mcl_blast_resistance = 0.6, _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}, {-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 }, 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(), sounds = mcl_sounds.node_sound_dirt_defaults(),
_mcl_blast_resistance = 0.6, _mcl_blast_resistance = 0.6,
@ -51,76 +43,60 @@ minetest.register_node("mcl_farming:soil_wet", {
minetest.register_abm({ minetest.register_abm({
label = "Farmland hydration", label = "Farmland hydration",
nodenames = {"mcl_farming:soil", "mcl_farming:soil_wet"}, nodenames = {"mcl_farming:soil", "mcl_farming:soil_wet"},
interval = 15, interval = 2.73,
chance = 4, chance = 25,
action = function(pos, node) 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 -- 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}) local above_node = minetest.get_node_or_nil(vector.offset(pos, 0, 1, 0))
if above_node then if above_node and minetest.get_item_group(above_node.name, "solid") ~= 0 then
if minetest.get_item_group(above_node.name, "solid") ~= 0 then
node.name = "mcl_core:dirt" node.name = "mcl_core:dirt"
minetest.set_node(pos, node) minetest.set_node(pos, node) -- also removes "wet" metadata
return return
end 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 end
-- Check an area of 9×2×9 around the node for nodename (9×9 on same level and 9×9 below) local meta = minetest.get_meta(pos)
local function check_surroundings(pos, nodename) local wet = meta:get_int("wet") or (node.name == "mcl_farming:soil" and 0 or 7)
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}) -- Hydrate by rain or water
return #nodes > 0 if raining or has_water then
end if node.name == "mcl_farming:soil" then
if check_surroundings(pos, "group:water") then
if node.name ~= "mcl_farming:soil_wet" then
-- Make it wet
node.name = "mcl_farming:soil_wet" node.name = "mcl_farming:soil_wet"
minetest.set_node(pos, node) minetest.set_node(pos, node) -- resets wetness
meta:set_int("wet", 7)
elseif wet < 7 then
meta:set_int("wet", 7)
end end
else -- No water nearby. return
-- The decay branch (make farmland dry or turn back to dirt) end
-- No decay near unloaded areas (ignore) since these might include water.
if not fully_loaded then return end
-- Don't decay while it's raining -- Decay: make farmland dry or turn back to dirt
if mcl_weather.rain.raining then if wet > 1 then
if mcl_weather.is_outdoor(pos) then if node.name == "mcl_farming:soil_wet" then -- change visual appearance to dry
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" node.name = "mcl_farming:soil"
minetest.swap_node(pos, node) minetest.set_node(pos, node)
end end
-- Slowly count down wetness
meta:set_int("wet", wet - 1) meta:set_int("wet", wet - 1)
return
end 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 end
end node.name = "mcl_core:dirt"
minetest.set_node(pos, node) -- also removes "wet" metadata
end, end,
}) })