mirror of
https://git.minetest.land/VoxeLibre/VoxeLibre.git
synced 2024-11-22 10:31:06 +01:00
always use day light level, more fixes
This commit is contained in:
parent
78a958db4e
commit
540a070c59
2 changed files with 87 additions and 105 deletions
|
@ -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",
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue