mirror of
https://git.minetest.land/VoxeLibre/VoxeLibre.git
synced 2024-11-22 10:31:06 +01:00
Fix growth logic, clean up mcl_farming/shared_functions (#4640)
Reviewed-on: https://git.minetest.land/VoxeLibre/VoxeLibre/pulls/4640 Reviewed-by: teknomunk <teknomunk@protonmail.com> Co-authored-by: kno10 <erich.schubert@gmail.com> Co-committed-by: kno10 <erich.schubert@gmail.com>
This commit is contained in:
parent
513413afc7
commit
d264ba70d8
2 changed files with 201 additions and 283 deletions
|
@ -283,7 +283,7 @@ local function apply_bone_meal(pointed_thing, user)
|
||||||
if n.name == "mcl_farming:sweet_berry_bush_3" then
|
if n.name == "mcl_farming:sweet_berry_bush_3" then
|
||||||
return minetest.add_item(vector.offset(pos,math.random()-0.5,math.random()-0.5,math.random()-0.5),"mcl_farming:sweet_berry")
|
return minetest.add_item(vector.offset(pos,math.random()-0.5,math.random()-0.5,math.random()-0.5),"mcl_farming:sweet_berry")
|
||||||
else
|
else
|
||||||
return mcl_farming:grow_plant("plant_sweet_berry_bush", pos, n, 0, true)
|
return mcl_farming:grow_plant("plant_sweet_berry_bush", pos, n, 1, true)
|
||||||
end
|
end
|
||||||
elseif n.name == "mcl_cocoas:cocoa_1" or n.name == "mcl_cocoas:cocoa_2" then
|
elseif n.name == "mcl_cocoas:cocoa_1" or n.name == "mcl_cocoas:cocoa_2" then
|
||||||
mcl_dye.add_bone_meal_particle(pos)
|
mcl_dye.add_bone_meal_particle(pos)
|
||||||
|
|
|
@ -1,81 +1,79 @@
|
||||||
|
-- Possible future improvements:
|
||||||
|
-- * rewrite to use node timers instead of ABMs, but needs benchmarking
|
||||||
|
-- * redesign the catch-up logic
|
||||||
|
-- * switch to exponentially-weighted moving average for light instead using a single variable to conserve IO
|
||||||
|
--
|
||||||
local math = math
|
local math = math
|
||||||
local tostring = tostring
|
local vector = vector
|
||||||
|
|
||||||
mcl_farming.plant_lists = {}
|
|
||||||
|
|
||||||
local plant_lists = {}
|
local plant_lists = {}
|
||||||
|
mcl_farming.plant_lists = plant_lists -- export
|
||||||
local plant_nodename_to_id_list = {}
|
local plant_nodename_to_id_list = {}
|
||||||
|
|
||||||
local function get_intervals_counter(pos, interval, chance)
|
local time_speed = tonumber(minetest.settings:get("time_speed")) or 72
|
||||||
local meta = minetest.get_meta(pos)
|
local time_multiplier = time_speed > 0 and (86400 / time_speed) or 0
|
||||||
local time_speed = tonumber(minetest.settings:get("time_speed") or 72)
|
|
||||||
local current_game_time
|
|
||||||
if time_speed == nil then
|
|
||||||
return 1
|
|
||||||
end
|
|
||||||
if (time_speed < 0.1) then
|
|
||||||
return 1
|
|
||||||
end
|
|
||||||
local time_multiplier = 86400 / time_speed
|
|
||||||
current_game_time = .0 + ((minetest.get_day_count() + minetest.get_timeofday()) * time_multiplier)
|
|
||||||
|
|
||||||
|
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 approx_interval = math.max(interval, 1) * math.max(chance, 1)
|
||||||
|
|
||||||
local last_game_time = meta:get_string("last_gametime")
|
local meta = minetest.get_meta(pos)
|
||||||
if last_game_time then
|
local last_game_time = meta:get_float("last_gametime")
|
||||||
last_game_time = tonumber(last_game_time)
|
if last_game_time < 1 then
|
||||||
end
|
last_game_time = current_game_time - approx_interval * 0.5
|
||||||
if not last_game_time or last_game_time < 1 then
|
|
||||||
last_game_time = current_game_time - approx_interval / 10
|
|
||||||
elseif last_game_time == current_game_time then
|
elseif last_game_time == current_game_time then
|
||||||
current_game_time = current_game_time + approx_interval
|
current_game_time = current_game_time + approx_interval
|
||||||
end
|
end
|
||||||
|
meta:set_float("last_gametime", current_game_time)
|
||||||
local elapsed_game_time = .0 + current_game_time - last_game_time
|
return (current_game_time - last_game_time) / approx_interval
|
||||||
|
|
||||||
meta:set_string("last_gametime", tostring(current_game_time))
|
|
||||||
|
|
||||||
return elapsed_game_time / approx_interval
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_avg_light_level(pos)
|
local function get_avg_light_level(pos)
|
||||||
local node_light = tonumber(minetest.get_node_light(pos) or 0)
|
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
local counter = meta:get_int("avg_light_count")
|
-- 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 summary = meta:get_int("avg_light_summary")
|
||||||
|
local counter = meta:get_int("avg_light_count")
|
||||||
if counter > 99 then
|
if counter > 99 then
|
||||||
counter = 51
|
summary, counter = math.ceil(summary * 0.5), 50
|
||||||
summary = math.ceil((summary + 0.0) / 2.0)
|
|
||||||
else
|
|
||||||
counter = counter + 1
|
|
||||||
end
|
end
|
||||||
summary = summary + node_light
|
local node_light = minetest.get_node_light(pos)
|
||||||
meta:set_int("avg_light_count", counter)
|
if node_light ~= nil then
|
||||||
|
summary, counter = summary + node_light, counter + 1
|
||||||
meta:set_int("avg_light_summary", summary)
|
meta:set_int("avg_light_summary", summary)
|
||||||
return math.ceil((summary + 0.0) / counter)
|
meta:set_int("avg_light_count", counter)
|
||||||
|
end
|
||||||
|
return math.ceil(summary / counter)
|
||||||
end
|
end
|
||||||
|
|
||||||
function mcl_farming:add_plant(identifier, full_grown, names, interval, chance)
|
function mcl_farming:add_plant(identifier, full_grown, names, interval, chance)
|
||||||
mcl_farming.plant_lists[identifier] = {}
|
local plant_info = {}
|
||||||
mcl_farming.plant_lists[identifier].full_grown = full_grown
|
plant_info.full_grown = full_grown
|
||||||
mcl_farming.plant_lists[identifier].names = names
|
plant_info.names = names
|
||||||
mcl_farming.plant_lists[identifier].interval = interval
|
plant_info.interval = interval
|
||||||
mcl_farming.plant_lists[identifier].chance = chance
|
plant_info.chance = chance
|
||||||
plant_lists = mcl_farming.plant_lists --provide local copy of plant lists (performances)
|
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
|
||||||
|
end
|
||||||
|
plant_lists[identifier] = plant_info
|
||||||
minetest.register_abm({
|
minetest.register_abm({
|
||||||
label = string.format("Farming plant growth (%s)", identifier),
|
label = string.format("Farming plant growth (%s)", identifier),
|
||||||
nodenames = names,
|
nodenames = names,
|
||||||
interval = interval,
|
interval = interval,
|
||||||
chance = chance,
|
chance = chance,
|
||||||
action = function(pos, node)
|
action = function(pos, node)
|
||||||
local low_speed = minetest.get_node({ x = pos.x, y = pos.y - 1, z = pos.z }).name ~= "mcl_farming:soil_wet"
|
local low_speed = minetest.get_node(vector.offset(pos, 0, -1, 0)).name ~= "mcl_farming:soil_wet"
|
||||||
mcl_farming:grow_plant(identifier, pos, node, false, false, low_speed)
|
mcl_farming:grow_plant(identifier, pos, node, 1, false, low_speed)
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
for _, nodename in pairs(names) do
|
|
||||||
plant_nodename_to_id_list[nodename] = identifier
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Attempts to advance a plant at pos by one or more growth stages (if possible)
|
-- Attempts to advance a plant at pos by one or more growth stages (if possible)
|
||||||
|
@ -84,56 +82,36 @@ end
|
||||||
-- node: Node table
|
-- node: Node table
|
||||||
-- stages: Number of stages to advance (optional, defaults to 1)
|
-- stages: Number of stages to advance (optional, defaults to 1)
|
||||||
-- ignore_light: if true, ignore light requirements for growing
|
-- ignore_light: if true, ignore light requirements for growing
|
||||||
|
-- low_speed: grow more slowly (not wet), default false
|
||||||
-- Returns true if plant has been grown by 1 or more stages.
|
-- Returns true if plant has been grown by 1 or more stages.
|
||||||
-- Returns false if nothing changed.
|
-- Returns false if nothing changed.
|
||||||
function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light, low_speed)
|
function mcl_farming:grow_plant(identifier, pos, node, stages, ignore_light, low_speed)
|
||||||
local average_light_level = get_avg_light_level(pos)
|
stages = stages or 1
|
||||||
local plant_info = plant_lists[identifier]
|
local plant_info = plant_lists[identifier]
|
||||||
local intervals_counter = get_intervals_counter(pos, plant_info.interval, plant_info.chance)
|
local intervals_counter = get_intervals_counter(pos, plant_info.interval, plant_info.chance)
|
||||||
local low_speed = low_speed or false
|
if stages > 0 then intervals_counters = intervals_counter - 1 end
|
||||||
if low_speed then
|
if low_speed then -- 10% speed approximately
|
||||||
if intervals_counter < 1.01 and math.random(0, 9) > 0 then
|
if intervals_counter < 1.01 and math.random(0, 9) > 0 then return false end
|
||||||
return
|
|
||||||
else
|
|
||||||
intervals_counter = intervals_counter / 10
|
intervals_counter = intervals_counter / 10
|
||||||
end
|
end
|
||||||
end
|
if not ignore_light and intervals_counter < 1.5 then
|
||||||
if not minetest.get_node_light(pos) and not ignore_light and intervals_counter < 1.5 then
|
local light = minetest.get_node_light(pos)
|
||||||
return false
|
if not light or light < 10 then return false end
|
||||||
end
|
|
||||||
if minetest.get_node_light(pos) < 10 and not ignore_light and intervals_counter < 1.5 then
|
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if intervals_counter >= 1.5 then
|
if intervals_counter >= 1.5 then
|
||||||
if average_light_level < 0.1 then
|
local average_light_level = get_avg_light_level(pos)
|
||||||
return false
|
if average_light_level < 0.1 then return false end
|
||||||
end
|
|
||||||
if average_light_level < 10 then
|
if average_light_level < 10 then
|
||||||
intervals_counter = intervals_counter * average_light_level / 10
|
intervals_counter = intervals_counter * average_light_level / 10
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local step = nil
|
local step = plant_info.step_from_name[node.name]
|
||||||
|
if step == nil then return false end
|
||||||
for i, name in ipairs(plant_info.names) do
|
stages = stages + math.floor(intervals_counter)
|
||||||
if name == node.name then
|
if stages == 0 then return false end
|
||||||
step = i
|
local new_node = { name = plant_info.names[step + stages] or plant_info.full_grown }
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if step == nil then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
if not stages then
|
|
||||||
stages = 1
|
|
||||||
end
|
|
||||||
stages = stages + math.ceil(intervals_counter)
|
|
||||||
local new_node = { name = plant_info.names[step + stages] }
|
|
||||||
if new_node.name == nil then
|
|
||||||
new_node.name = plant_info.full_grown
|
|
||||||
end
|
|
||||||
new_node.param = node.param
|
new_node.param = node.param
|
||||||
new_node.param2 = node.param2
|
new_node.param2 = node.param2
|
||||||
minetest.set_node(pos, new_node)
|
minetest.set_node(pos, new_node)
|
||||||
|
@ -142,12 +120,7 @@ end
|
||||||
|
|
||||||
function mcl_farming:place_seed(itemstack, placer, pointed_thing, plantname)
|
function mcl_farming:place_seed(itemstack, placer, pointed_thing, plantname)
|
||||||
local pt = pointed_thing
|
local pt = pointed_thing
|
||||||
if not pt then
|
if not pt or pt.type ~= "node" then return end
|
||||||
return
|
|
||||||
end
|
|
||||||
if pt.type ~= "node" then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Use pointed node's on_rightclick function first, if present
|
-- Use pointed node's on_rightclick function first, if present
|
||||||
local node = minetest.get_node(pt.under)
|
local node = minetest.get_node(pt.under)
|
||||||
|
@ -157,22 +130,13 @@ function mcl_farming:place_seed(itemstack, placer, pointed_thing, plantname)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local pos = { x = pt.above.x, y = pt.above.y - 1, z = pt.above.z }
|
if minetest.get_node(pt.above).name ~= "air" then return end
|
||||||
local farmland = minetest.get_node(pos)
|
local farmland = minetest.registered_nodes[minetest.get_node(vector.offset(pt.above, 0, -1, 0)).name]
|
||||||
pos = { x = pt.above.x, y = pt.above.y, z = pt.above.z }
|
if not farmland or (farmland.groups.soil or 0) < 2 then return end
|
||||||
local place_s = minetest.get_node(pos)
|
minetest.sound_play(minetest.registered_nodes[plantname].sounds.place, { pos = pt.above }, true)
|
||||||
|
minetest.add_node(pt.above, { name = plantname, param2 = minetest.registered_nodes[plantname].place_param2 })
|
||||||
|
|
||||||
if string.find(farmland.name, "mcl_farming:soil") and string.find(place_s.name, "air") then
|
if not minetest.is_creative_enabled(placer:get_player_name()) then itemstack:take_item() end
|
||||||
minetest.sound_play(minetest.registered_nodes[plantname].sounds.place, { pos = pos }, true)
|
|
||||||
minetest.add_node(pos, { name = plantname, param2 = minetest.registered_nodes[plantname].place_param2 })
|
|
||||||
--local intervals_counter = get_intervals_counter(pos, 1, 1)
|
|
||||||
else
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if not minetest.is_creative_enabled(placer:get_player_name()) then
|
|
||||||
itemstack:take_item()
|
|
||||||
end
|
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -189,48 +153,41 @@ end
|
||||||
- grow_interval: Will attempt to grow a gourd periodically at this interval in seconds
|
- grow_interval: Will attempt to grow a gourd periodically at this interval in seconds
|
||||||
- grow_chance: Chance of 1/grow_chance to grow a gourd next to the full unconnected stem after grow_interval has passed. Must be a natural number
|
- grow_chance: Chance of 1/grow_chance to grow a gourd next to the full unconnected stem after grow_interval has passed. Must be a natural number
|
||||||
- connected_stem_texture: Texture of the connected stem
|
- connected_stem_texture: Texture of the connected stem
|
||||||
- gourd_on_construct_extra: Custom on_construct extra function for the gourd. Will be called after the stem check code
|
|
||||||
]]
|
]]
|
||||||
|
|
||||||
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, gourd_on_construct_extra)
|
|
||||||
|
|
||||||
|
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)
|
||||||
local connected_stem_names = {
|
local connected_stem_names = {
|
||||||
connected_stem_basename .. "_r",
|
connected_stem_basename .. "_r",
|
||||||
connected_stem_basename .. "_l",
|
connected_stem_basename .. "_l",
|
||||||
connected_stem_basename .. "_t",
|
connected_stem_basename .. "_t",
|
||||||
connected_stem_basename .. "_b",
|
connected_stem_basename .. "_b" }
|
||||||
}
|
|
||||||
|
|
||||||
local neighbors = {
|
|
||||||
{ x = -1, y = 0, z = 0 },
|
|
||||||
{ x = 1, y = 0, z = 0 },
|
|
||||||
{ x = 0, y = 0, z = -1 },
|
|
||||||
{ x = 0, y = 0, z = 1 },
|
|
||||||
}
|
|
||||||
|
|
||||||
-- Connect the stem at stempos to the first neighboring gourd block.
|
-- Connect the stem at stempos to the first neighboring gourd block.
|
||||||
-- No-op if not a stem or no gourd block found
|
-- No-op if not a stem or no gourd block found
|
||||||
local function try_connect_stem(stempos)
|
local function try_connect_stem(stempos)
|
||||||
local stem = minetest.get_node(stempos)
|
local stem = minetest.get_node(stempos)
|
||||||
if stem.name ~= full_unconnected_stem then
|
if stem.name ~= full_unconnected_stem then return false end
|
||||||
return false
|
-- four directions, but avoid table allocation
|
||||||
end
|
local neighbor = vector.offset(stempos, 1, 0, 0)
|
||||||
for n = 1, #neighbors do
|
if minetest.get_node(neighbor).name == gourd_itemstring then
|
||||||
local offset = neighbors[n]
|
minetest.swap_node(stempos, { name = connected_stem_names[1] })
|
||||||
local blockpos = vector.add(stempos, offset)
|
|
||||||
local block = minetest.get_node(blockpos)
|
|
||||||
if block.name == gourd_itemstring then
|
|
||||||
if offset.x == 1 then
|
|
||||||
minetest.set_node(stempos, { name = connected_stem_names[1] })
|
|
||||||
elseif offset.x == -1 then
|
|
||||||
minetest.set_node(stempos, { name = connected_stem_names[2] })
|
|
||||||
elseif offset.z == 1 then
|
|
||||||
minetest.set_node(stempos, { name = connected_stem_names[3] })
|
|
||||||
elseif offset.z == -1 then
|
|
||||||
minetest.set_node(stempos, { name = connected_stem_names[4] })
|
|
||||||
end
|
|
||||||
return true
|
return true
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -238,29 +195,37 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s
|
||||||
if not gourd_def.after_destruct then
|
if not gourd_def.after_destruct then
|
||||||
gourd_def.after_destruct = function(blockpos, oldnode)
|
gourd_def.after_destruct = function(blockpos, oldnode)
|
||||||
-- Disconnect any connected stems, turning them back to normal stems
|
-- Disconnect any connected stems, turning them back to normal stems
|
||||||
for n = 1, #neighbors do
|
-- four directions, but avoid using a table
|
||||||
local offset = neighbors[n]
|
-- opposite directions to above, as we go from groud to stem now!
|
||||||
local expected_stem = connected_stem_names[n]
|
local stempos = vector.offset(blockpos, -1, 0, 0)
|
||||||
local stempos = vector.add(blockpos, offset)
|
if minetest.get_node(stempos).name == connected_stem_names[1] then
|
||||||
local stem = minetest.get_node(stempos)
|
minetest.swap_node(stempos, { name = full_unconnected_stem })
|
||||||
if stem.name == expected_stem then
|
|
||||||
minetest.add_node(stempos, { name = full_unconnected_stem })
|
|
||||||
try_connect_stem(stempos)
|
try_connect_stem(stempos)
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
if not gourd_def.on_construct then
|
if not gourd_def.on_construct then
|
||||||
function gourd_def.on_construct(blockpos)
|
function gourd_def.on_construct(blockpos)
|
||||||
-- Connect all unconnected stems at full size
|
-- Connect all unconnected stems at full size
|
||||||
for n = 1, #neighbors do
|
try_connect_stem(vector.offset(blockpos, 1, 0, 0))
|
||||||
local stempos = vector.add(blockpos, neighbors[n])
|
try_connect_stem(vector.offset(blockpos, -1, 0, 0))
|
||||||
try_connect_stem(stempos)
|
try_connect_stem(vector.offset(blockpos, 0, 0, 1))
|
||||||
end
|
try_connect_stem(vector.offset(blockpos, 0, 0, -1))
|
||||||
-- Call custom on_construct
|
|
||||||
if gourd_on_construct_extra then
|
|
||||||
gourd_on_construct_extra(blockpos)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
minetest.register_node(gourd_itemstring, gourd_def)
|
minetest.register_node(gourd_itemstring, gourd_def)
|
||||||
|
@ -269,72 +234,47 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s
|
||||||
|
|
||||||
-- Default values for the stem definition
|
-- Default values for the stem definition
|
||||||
if not stem_def.selection_box then
|
if not stem_def.selection_box then
|
||||||
stem_def.selection_box = {
|
stem_def.selection_box = { type = "fixed", fixed = { { -0.15, -0.5, -0.15, 0.15, 0.5, 0.15 } } }
|
||||||
type = "fixed",
|
|
||||||
fixed = {
|
|
||||||
{ -0.15, -0.5, -0.15, 0.15, 0.5, 0.15 }
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end
|
|
||||||
if not stem_def.paramtype then
|
|
||||||
stem_def.paramtype = "light"
|
|
||||||
end
|
|
||||||
if not stem_def.drawtype then
|
|
||||||
stem_def.drawtype = "plantlike"
|
|
||||||
end
|
|
||||||
if stem_def.walkable == nil then
|
|
||||||
stem_def.walkable = false
|
|
||||||
end
|
|
||||||
if stem_def.sunlight_propagates == nil then
|
|
||||||
stem_def.sunlight_propagates = true
|
|
||||||
end
|
|
||||||
if stem_def.drop == nil then
|
|
||||||
stem_def.drop = stem_drop
|
|
||||||
end
|
|
||||||
if stem_def.groups == nil then
|
|
||||||
stem_def.groups = { dig_immediate = 3, not_in_creative_inventory = 1, plant = 1, attached_node = 1, dig_by_water = 1, destroy_by_lava_flow = 1, }
|
|
||||||
end
|
|
||||||
if stem_def.sounds == nil then
|
|
||||||
stem_def.sounds = mcl_sounds.node_sound_leaves_defaults()
|
|
||||||
end
|
|
||||||
|
|
||||||
if not stem_def.on_construct then
|
|
||||||
function stem_def.on_construct(stempos)
|
|
||||||
-- Connect stem to gourd (if possible)
|
|
||||||
try_connect_stem(stempos)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
stem_def.paramtype = stem_def.paramtype or "light"
|
||||||
|
stem_def.drawtype = stem_def.drawtype or "plantlike"
|
||||||
|
stem_def.walkable = stem_def.walkable or false
|
||||||
|
stem_def.sunlight_propagates = stem_def.sunlight_propagates == nil or stem_def.sunlight_propagates
|
||||||
|
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)
|
minetest.register_node(stem_itemstring, stem_def)
|
||||||
|
|
||||||
-- Register connected stems
|
-- Register connected stems
|
||||||
|
|
||||||
local connected_stem_tiles = {
|
local connected_stem_tiles = {
|
||||||
{ "blank.png", --top
|
{ "blank.png", -- top
|
||||||
"blank.png", -- bottom
|
"blank.png", -- bottom
|
||||||
"blank.png", -- right
|
"blank.png", -- right
|
||||||
"blank.png", -- left
|
"blank.png", -- left
|
||||||
connected_stem_texture, -- back
|
connected_stem_texture, -- back
|
||||||
connected_stem_texture .. "^[transformFX" --front
|
connected_stem_texture .. "^[transformFX" -- front
|
||||||
},
|
},
|
||||||
{ "blank.png", --top
|
{ "blank.png", -- top
|
||||||
"blank.png", -- bottom
|
"blank.png", -- bottom
|
||||||
"blank.png", -- right
|
"blank.png", -- right
|
||||||
"blank.png", -- left
|
"blank.png", -- left
|
||||||
connected_stem_texture .. "^[transformFX", --back
|
connected_stem_texture .. "^[transformFX", -- back
|
||||||
connected_stem_texture, -- front
|
connected_stem_texture, -- front
|
||||||
},
|
},
|
||||||
{ "blank.png", --top
|
{ "blank.png", -- top
|
||||||
"blank.png", -- bottom
|
"blank.png", -- bottom
|
||||||
connected_stem_texture .. "^[transformFX", -- right
|
connected_stem_texture .. "^[transformFX", -- right
|
||||||
connected_stem_texture, -- left
|
connected_stem_texture, -- left
|
||||||
"blank.png", --back
|
"blank.png", -- back
|
||||||
"blank.png", -- front
|
"blank.png", -- front
|
||||||
},
|
},
|
||||||
{ "blank.png", --top
|
{ "blank.png", -- top
|
||||||
"blank.png", -- bottom
|
"blank.png", -- bottom
|
||||||
connected_stem_texture, -- right
|
connected_stem_texture, -- right
|
||||||
connected_stem_texture .. "^[transformFX", -- left
|
connected_stem_texture .. "^[transformFX", -- left
|
||||||
"blank.png", --back
|
"blank.png", -- back
|
||||||
"blank.png", -- front
|
"blank.png", -- front
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -359,17 +299,11 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s
|
||||||
walkable = false,
|
walkable = false,
|
||||||
drop = stem_drop,
|
drop = stem_drop,
|
||||||
drawtype = "nodebox",
|
drawtype = "nodebox",
|
||||||
node_box = {
|
node_box = { type = "fixed", fixed = connected_stem_nodebox[i] },
|
||||||
type = "fixed",
|
selection_box = { type = "fixed", fixed = connected_stem_selectionbox[i] },
|
||||||
fixed = connected_stem_nodebox[i]
|
|
||||||
},
|
|
||||||
selection_box = {
|
|
||||||
type = "fixed",
|
|
||||||
fixed = connected_stem_selectionbox[i]
|
|
||||||
},
|
|
||||||
tiles = connected_stem_tiles[i],
|
tiles = connected_stem_tiles[i],
|
||||||
use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "clip" or true,
|
use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "clip" or true,
|
||||||
groups = { dig_immediate = 3, not_in_creative_inventory = 1, plant = 1, attached_node = 1, dig_by_water = 1, destroy_by_lava_flow = 1, },
|
groups = { dig_immediate = 3, not_in_creative_inventory = 1, plant = 1, attached_node = 1, dig_by_water = 1, destroy_by_lava_flow = 1 },
|
||||||
sounds = mcl_sounds.node_sound_leaves_defaults(),
|
sounds = mcl_sounds.node_sound_leaves_defaults(),
|
||||||
_mcl_blast_resistance = 0,
|
_mcl_blast_resistance = 0,
|
||||||
})
|
})
|
||||||
|
@ -379,6 +313,16 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s
|
||||||
end
|
end
|
||||||
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({
|
minetest.register_abm({
|
||||||
label = "Grow gourd stem to gourd (" .. full_unconnected_stem .. " → " .. gourd_itemstring .. ")",
|
label = "Grow gourd stem to gourd (" .. full_unconnected_stem .. " → " .. gourd_itemstring .. ")",
|
||||||
nodenames = { full_unconnected_stem },
|
nodenames = { full_unconnected_stem },
|
||||||
|
@ -387,67 +331,50 @@ function mcl_farming:add_gourd(full_unconnected_stem, connected_stem_basename, s
|
||||||
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)
|
||||||
if light and light > 10 then
|
if not light or light <= 10 then return end
|
||||||
-- Check the four neighbors and filter out neighbors where gourds can't grow
|
-- Check the four neighbors and filter out neighbors where gourds can't grow
|
||||||
local neighbors = {
|
local neighbor, dir, nchance = nil, -1, 1 -- reservoir sampling
|
||||||
{ x = -1, y = 0, z = 0 },
|
if nchance == 1 or math.random(1, nchance) == 1 then
|
||||||
{ x = 1, y = 0, z = 0 },
|
local blockpos = vector.offset(stempos, 1, 0, 0)
|
||||||
{ x = 0, y = 0, z = -1 },
|
if check_neighbor_soil(blockpos) then
|
||||||
{ x = 0, y = 0, z = 1 },
|
neighbor, dir, nchance = blockpos, 1, nchance + 1
|
||||||
}
|
end
|
||||||
local floorpos, floor
|
end
|
||||||
for n = #neighbors, 1, -1 do
|
if nchance == 1 or math.random(1, nchance) == 1 then
|
||||||
local offset = neighbors[n]
|
local blockpos = vector.offset(stempos, -1, 0, 0)
|
||||||
local blockpos = vector.add(stempos, offset)
|
if check_neighbor_soil(blockpos) then
|
||||||
floorpos = vector.offset (blockpos, 0, -1,0) -- replaces { x = blockpos.x, y = blockpos.y - 1, z = blockpos.z }
|
neighbor, dir, nchance = blockpos, 2, nchance + 1
|
||||||
floor = minetest.get_node(floorpos)
|
end
|
||||||
local block = minetest.get_node(blockpos)
|
end
|
||||||
local soilgroup = minetest.get_item_group(floor.name, "soil")
|
if nchance == 1 or math.random(1, nchance) == 1 then
|
||||||
if not ((minetest.get_item_group(floor.name, "grass_block") == 1 or floor.name == "mcl_core:dirt" or soilgroup == 2 or soilgroup == 3) and block.name == "air") then
|
local blockpos = vector.offset(stempos, 0, 0, 1)
|
||||||
table.remove(neighbors, n)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Gourd needs at least 1 free neighbor to grow
|
-- Gourd needs at least 1 free neighbor to grow
|
||||||
if #neighbors > 0 then
|
if not neighbor then return end
|
||||||
-- From the remaining neighbors, grow randomly
|
minetest.swap_node(stempos, { name = connected_stem_names[dir] })
|
||||||
local r = math.random(1, #neighbors)
|
|
||||||
local offset = neighbors[r]
|
|
||||||
local blockpos = vector.add(stempos, offset)
|
|
||||||
local p2
|
|
||||||
if offset.x == 1 then
|
|
||||||
minetest.set_node(stempos, { name = connected_stem_names[1] })
|
|
||||||
p2 = 3
|
|
||||||
elseif offset.x == -1 then
|
|
||||||
minetest.set_node(stempos, { name = connected_stem_names[2] })
|
|
||||||
p2 = 1
|
|
||||||
elseif offset.z == 1 then
|
|
||||||
minetest.set_node(stempos, { name = connected_stem_names[3] })
|
|
||||||
p2 = 2
|
|
||||||
elseif offset.z == -1 then
|
|
||||||
minetest.set_node(stempos, { name = connected_stem_names[4] })
|
|
||||||
p2 = 0
|
|
||||||
end
|
|
||||||
-- Place the gourd
|
-- Place the gourd
|
||||||
if gourd_def.paramtype2 == "facedir" then
|
if gourd_def.paramtype2 == "facedir" then
|
||||||
minetest.add_node(blockpos, { name = gourd_itemstring, param2 = p2 })
|
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 })
|
||||||
else
|
else
|
||||||
minetest.add_node(blockpos, { name = gourd_itemstring })
|
minetest.add_node(neighbor, { name = gourd_itemstring })
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Reset farmland, etc. to dirt when the gourd grows on top
|
-- Reset farmland, etc. to dirt when the gourd grows on top
|
||||||
|
local floorpos = vector.offset(neighbor, 0, -1, 0)
|
||||||
-- FIXED: The following 2 lines were missing, and wasn't being set (outside of the above loop that
|
if minetest.get_item_group(minetest.get_node(floorpos).name, "dirtifies_below_solid") == 1 then
|
||||||
-- finds the neighbors.)
|
|
||||||
-- FYI - don't factor this out thinking that the loop above is setting the positions correctly.
|
|
||||||
floorpos = vector.offset (blockpos, 0, -1,0) -- replaces { x = blockpos.x, y = blockpos.y - 1, z = blockpos.z }
|
|
||||||
floor = minetest.get_node(floorpos)
|
|
||||||
-- END OF FIX -------------------------------------
|
|
||||||
if minetest.get_item_group(floor.name, "dirtifies_below_solid") == 1 then
|
|
||||||
minetest.set_node(floorpos, { name = "mcl_core:dirt" })
|
minetest.set_node(floorpos, { name = "mcl_core:dirt" })
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
@ -458,15 +385,11 @@ end
|
||||||
-- * step: The nth growth step. Counting starts at 1
|
-- * step: The nth growth step. Counting starts at 1
|
||||||
-- * step_count: The number of total growth steps
|
-- * step_count: The number of total growth steps
|
||||||
function mcl_farming:stem_color(startcolor, endcolor, step, step_count)
|
function mcl_farming:stem_color(startcolor, endcolor, step, step_count)
|
||||||
local color = {}
|
local mix = (step - 1) / (step_count - 1)
|
||||||
local function get_component(startt, endd, step, step_count)
|
return string.format("#%02X%02X%02X",
|
||||||
return math.floor(math.max(0, math.min(255, (startt + (((step - 1) / step_count) * endd)))))
|
math.max(0, math.min(255, math.round((1 - mix) * startcolor.r + mix * endcolor.r))),
|
||||||
end
|
math.max(0, math.min(255, math.round((1 - mix) * startcolor.g + mix * endcolor.g))),
|
||||||
color.r = get_component(startcolor.r, endcolor.r, step, step_count)
|
math.max(0, math.min(255, math.round((1 - mix) * startcolor.b + mix * endcolor.b))))
|
||||||
color.g = get_component(startcolor.g, endcolor.g, step, step_count)
|
|
||||||
color.b = get_component(startcolor.b, endcolor.b, step, step_count)
|
|
||||||
local colorstring = string.format("#%02X%02X%02X", color.r, color.g, color.b)
|
|
||||||
return colorstring
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--[[Get a callback that either eats the item or plants it.
|
--[[Get a callback that either eats the item or plants it.
|
||||||
|
@ -475,12 +398,8 @@ Used for on_place callbacks for craft items which are seeds that can also be con
|
||||||
]]
|
]]
|
||||||
function mcl_farming:get_seed_or_eat_callback(plantname, hp_change)
|
function mcl_farming:get_seed_or_eat_callback(plantname, hp_change)
|
||||||
return function(itemstack, placer, pointed_thing)
|
return function(itemstack, placer, pointed_thing)
|
||||||
local new = mcl_farming:place_seed(itemstack, placer, pointed_thing, plantname)
|
return mcl_farming:place_seed(itemstack, placer, pointed_thing, plantname)
|
||||||
if new then
|
or minetest.do_item_eat(hp_change, nil, itemstack, placer, pointed_thing)
|
||||||
return new
|
|
||||||
else
|
|
||||||
return minetest.do_item_eat(hp_change, nil, itemstack, placer, pointed_thing)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -489,12 +408,11 @@ minetest.register_lbm({
|
||||||
name = "mcl_farming:growth",
|
name = "mcl_farming:growth",
|
||||||
nodenames = { "group:plant" },
|
nodenames = { "group:plant" },
|
||||||
run_at_every_load = true,
|
run_at_every_load = true,
|
||||||
action = function(pos, node)
|
action = function(pos, node, dtime_s)
|
||||||
local identifier = plant_nodename_to_id_list[node.name]
|
local identifier = plant_nodename_to_id_list[node.name]
|
||||||
if not identifier then
|
if not identifier then return end
|
||||||
return
|
local low_speed = minetest.get_node(vector.offset(pos, 0, -1, 0)).name ~= "mcl_farming:soil_wet"
|
||||||
end
|
mcl_farming:grow_plant(identifier, pos, node, 0, false, low_speed)
|
||||||
local low_speed = minetest.get_node({ x = pos.x, y = pos.y - 1, z = pos.z }).name ~= "mcl_farming:soil_wet"
|
|
||||||
mcl_farming:grow_plant(identifier, pos, node, false, false, low_speed)
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue