2014-11-09 20:06:28 +01:00
|
|
|
--[[
|
2017-08-14 19:42:03 +02:00
|
|
|
Farming Redo Mod
|
2014-11-09 20:06:28 +01:00
|
|
|
by TenPlus1
|
2015-05-20 11:22:04 +02:00
|
|
|
NEW growing routine by prestidigitator
|
2015-08-29 10:27:01 +02:00
|
|
|
auto-refill by crabman77
|
2014-11-09 20:06:28 +01:00
|
|
|
]]
|
|
|
|
|
2018-09-29 17:17:28 +02:00
|
|
|
farming = {
|
|
|
|
mod = "redo",
|
2022-03-09 10:19:43 +01:00
|
|
|
version = "20220309",
|
2018-09-29 17:17:28 +02:00
|
|
|
path = minetest.get_modpath("farming"),
|
|
|
|
select = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5}
|
|
|
|
},
|
2020-12-09 11:22:41 +01:00
|
|
|
registered_plants = {},
|
|
|
|
min_light = 12,
|
|
|
|
max_light = 15
|
2015-07-05 11:54:18 +02:00
|
|
|
}
|
2014-11-09 20:06:28 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
|
2018-01-04 10:33:24 +01:00
|
|
|
local creative_mode_cache = minetest.settings:get_bool("creative_mode")
|
2017-08-28 13:33:25 +02:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
function farming.is_creative(name)
|
2017-08-28 13:33:25 +02:00
|
|
|
return creative_mode_cache or minetest.check_player_privs(name, {creative = true})
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-05-17 10:25:16 +02:00
|
|
|
local statistics = dofile(farming.path .. "/statistics.lua")
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-05-30 13:30:00 +02:00
|
|
|
-- Intllib
|
2022-01-29 10:54:24 +01:00
|
|
|
local S
|
|
|
|
if minetest.get_translator ~= nil then
|
|
|
|
S = minetest.get_translator("farming") -- 5.x translation function
|
|
|
|
else
|
|
|
|
if minetest.get_modpath("intllib") then
|
|
|
|
dofile(minetest.get_modpath("intllib") .. "/init.lua")
|
|
|
|
if intllib.make_gettext_pair then
|
|
|
|
gettext, ngettext = intllib.make_gettext_pair() -- new gettext method
|
|
|
|
else
|
|
|
|
gettext = intllib.Getter() -- old text file method
|
|
|
|
end
|
|
|
|
S = gettext
|
|
|
|
else -- boilerplate function
|
|
|
|
S = function(str, ...)
|
|
|
|
local args = {...}
|
|
|
|
return str:gsub("@%d+", function(match)
|
|
|
|
return args[tonumber(match:sub(2))]
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-08-25 10:19:59 +02:00
|
|
|
|
2016-05-30 13:30:00 +02:00
|
|
|
farming.intllib = S
|
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
-- Utility Function
|
2017-10-09 12:32:00 +02:00
|
|
|
local time_speed = tonumber(minetest.settings:get("time_speed")) or 72
|
2018-05-17 10:25:16 +02:00
|
|
|
local SECS_PER_CYCLE = (time_speed > 0 and (24 * 60 * 60) / time_speed) or 0
|
2015-05-20 11:22:04 +02:00
|
|
|
local function clamp(x, min, max)
|
|
|
|
return (x < min and min) or (x > max and max) or x
|
|
|
|
end
|
|
|
|
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
-- return amount of day or night that has elapsed
|
|
|
|
-- dt is time elapsed, count_day if true counts day, otherwise night
|
|
|
|
local function day_or_night_time(dt, count_day)
|
2015-05-20 11:22:04 +02:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
local t_day = minetest.get_timeofday()
|
2015-12-08 15:47:22 +01:00
|
|
|
local t1_day = t_day - dt / SECS_PER_CYCLE
|
2015-05-20 11:22:04 +02:00
|
|
|
local t1_c, t2_c -- t1_c < t2_c and t2_c always in [0, 1)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
if count_day then
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
if t_day < 0.25 then
|
|
|
|
t1_c = t1_day + 0.75 -- Relative to sunup, yesterday
|
|
|
|
t2_c = t_day + 0.75
|
|
|
|
else
|
|
|
|
t1_c = t1_day - 0.25 -- Relative to sunup, today
|
|
|
|
t2_c = t_day - 0.25
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if t_day < 0.75 then
|
|
|
|
t1_c = t1_day + 0.25 -- Relative to sundown, yesterday
|
|
|
|
t2_c = t_day + 0.25
|
|
|
|
else
|
|
|
|
t1_c = t1_day - 0.75 -- Relative to sundown, today
|
|
|
|
t2_c = t_day - 0.75
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local dt_c = clamp(t2_c, 0, 0.5) - clamp(t1_c, 0, 0.5) -- this cycle
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
if t1_c < -0.5 then
|
|
|
|
local nc = math.floor(-t1_c)
|
|
|
|
t1_c = t1_c + nc
|
|
|
|
dt_c = dt_c + 0.5 * nc + clamp(-t1_c - 0.5, 0, 0.5)
|
|
|
|
end
|
|
|
|
|
|
|
|
return dt_c * SECS_PER_CYCLE
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Growth Logic
|
2020-04-26 16:41:39 +02:00
|
|
|
local STAGE_LENGTH_AVG = tonumber(
|
2020-07-02 15:31:12 +02:00
|
|
|
minetest.settings:get("farming_stage_length")) or 200 -- 160
|
2015-05-20 11:22:04 +02:00
|
|
|
local STAGE_LENGTH_DEV = STAGE_LENGTH_AVG / 6
|
|
|
|
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
-- return plant name and stage from node provided
|
2015-05-20 11:22:04 +02:00
|
|
|
local function plant_name_stage(node)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
local name
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2017-02-13 14:31:18 +01:00
|
|
|
if type(node) == "table" then
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
if node.name then
|
2016-03-10 18:45:55 +01:00
|
|
|
name = node.name
|
2015-05-20 11:22:04 +02:00
|
|
|
elseif node.x and node.y and node.z then
|
|
|
|
node = minetest.get_node_or_nil(node)
|
|
|
|
name = node and node.name
|
|
|
|
end
|
|
|
|
else
|
|
|
|
name = tostring(node)
|
|
|
|
end
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
if not name or name == "ignore" then
|
|
|
|
return nil
|
|
|
|
end
|
2015-05-20 11:22:04 +02:00
|
|
|
|
|
|
|
local sep_pos = name:find("_[^_]+$")
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
if sep_pos and sep_pos > 1 then
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
local stage = tonumber(name:sub(sep_pos + 1))
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
if stage and stage >= 0 then
|
|
|
|
return name:sub(1, sep_pos - 1), stage
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return name, 0
|
|
|
|
end
|
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
-- Map from node name to
|
|
|
|
-- { plant_name = ..., name = ..., stage = n, stages_left = { node_name, ... } }
|
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
local plant_stages = {}
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
farming.plant_stages = plant_stages
|
|
|
|
|
|
|
|
--- Registers the stages of growth of a (possible plant) node.
|
|
|
|
--
|
|
|
|
-- @param node
|
|
|
|
-- Node or position table, or node name.
|
|
|
|
-- @return
|
|
|
|
-- The (possibly zero) number of stages of growth the plant will go through
|
|
|
|
-- before being fully grown, or nil if not a plant.
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
local register_plant_node
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
-- Recursive helper
|
|
|
|
local function reg_plant_stages(plant_name, stage, force_last)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
local node_name = plant_name and plant_name .. "_" .. stage
|
|
|
|
local node_def = node_name and minetest.registered_nodes[node_name]
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
if not node_def then
|
|
|
|
return nil
|
|
|
|
end
|
2015-05-20 11:22:04 +02:00
|
|
|
|
|
|
|
local stages = plant_stages[node_name]
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
if stages then
|
|
|
|
return stages
|
|
|
|
end
|
2015-05-20 11:22:04 +02:00
|
|
|
|
|
|
|
if minetest.get_item_group(node_name, "growing") > 0 then
|
|
|
|
|
2015-12-08 15:47:22 +01:00
|
|
|
local ns = reg_plant_stages(plant_name, stage + 1, true)
|
2015-05-20 11:22:04 +02:00
|
|
|
local stages_left = (ns and { ns.name, unpack(ns.stages_left) }) or {}
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-07-05 11:54:18 +02:00
|
|
|
stages = {
|
|
|
|
plant_name = plant_name,
|
|
|
|
name = node_name,
|
|
|
|
stage = stage,
|
|
|
|
stages_left = stages_left
|
|
|
|
}
|
2015-05-20 11:22:04 +02:00
|
|
|
|
|
|
|
if #stages_left > 0 then
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
local old_constr = node_def.on_construct
|
|
|
|
local old_destr = node_def.on_destruct
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
minetest.override_item(node_name,
|
|
|
|
{
|
|
|
|
on_construct = function(pos)
|
2016-03-10 18:45:55 +01:00
|
|
|
|
|
|
|
if old_constr then
|
|
|
|
old_constr(pos)
|
|
|
|
end
|
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
farming.handle_growth(pos)
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_destruct = function(pos)
|
2016-03-10 18:45:55 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
minetest.get_node_timer(pos):stop()
|
2016-03-10 18:45:55 +01:00
|
|
|
|
|
|
|
if old_destr then
|
|
|
|
old_destr(pos)
|
|
|
|
end
|
2015-05-20 11:22:04 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
on_timer = function(pos, elapsed)
|
|
|
|
return farming.plant_growth_timer(pos, elapsed, node_name)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
elseif force_last then
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-07-05 11:54:18 +02:00
|
|
|
stages = {
|
|
|
|
plant_name = plant_name,
|
|
|
|
name = node_name,
|
|
|
|
stage = stage,
|
|
|
|
stages_left = {}
|
|
|
|
}
|
2015-05-20 11:22:04 +02:00
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
plant_stages[node_name] = stages
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
return stages
|
|
|
|
end
|
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
|
2018-03-22 16:57:36 +01:00
|
|
|
local register_plant_node = function(node)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
local plant_name, stage = plant_name_stage(node)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
if plant_name then
|
2016-03-10 18:45:55 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
local stages = reg_plant_stages(plant_name, stage, false)
|
|
|
|
return stages and #stages.stages_left
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
local function set_growing(pos, stages_left)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
if not stages_left then
|
|
|
|
return
|
|
|
|
end
|
2015-05-20 11:22:04 +02:00
|
|
|
|
|
|
|
local timer = minetest.get_node_timer(pos)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
if stages_left > 0 then
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
if not timer:is_started() then
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
local stage_length = statistics.normal(STAGE_LENGTH_AVG, STAGE_LENGTH_DEV)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
stage_length = clamp(stage_length, 0.5 * STAGE_LENGTH_AVG, 3.0 * STAGE_LENGTH_AVG)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
timer:set(stage_length, -0.5 * math.random() * STAGE_LENGTH_AVG)
|
|
|
|
end
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
elseif timer:is_started() then
|
|
|
|
timer:stop()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
-- detects a crop at given position, starting or stopping growth timer when needed
|
2015-05-20 11:22:04 +02:00
|
|
|
function farming.handle_growth(pos, node)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
if not pos then
|
|
|
|
return
|
|
|
|
end
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
local stages_left = register_plant_node(node or pos)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
if stages_left then
|
|
|
|
set_growing(pos, stages_left)
|
|
|
|
end
|
2015-05-20 11:22:04 +02:00
|
|
|
end
|
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
minetest.after(0, function()
|
|
|
|
|
2019-05-23 20:29:06 +02:00
|
|
|
for _, node_def in pairs(minetest.registered_nodes) do
|
2016-03-10 18:45:55 +01:00
|
|
|
register_plant_node(node_def)
|
|
|
|
end
|
|
|
|
end)
|
2015-05-20 11:22:04 +02:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
-- Just in case a growing type or added node is missed (also catches existing
|
|
|
|
-- nodes added to map before timers were incorporated).
|
2016-03-10 18:45:55 +01:00
|
|
|
minetest.register_abm({
|
2019-12-02 11:23:20 +01:00
|
|
|
nodenames = {"group:growing"},
|
2015-12-08 15:47:22 +01:00
|
|
|
interval = 300,
|
|
|
|
chance = 1,
|
2018-05-19 16:39:49 +02:00
|
|
|
catch_up = false,
|
|
|
|
action = function(pos, node)
|
|
|
|
farming.handle_growth(pos, node)
|
|
|
|
end
|
2015-12-08 15:47:22 +01:00
|
|
|
})
|
2015-05-20 11:22:04 +02:00
|
|
|
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
-- Plant timer function that grows plants under the right conditions.
|
2015-05-20 11:22:04 +02:00
|
|
|
function farming.plant_growth_timer(pos, elapsed, node_name)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
local stages = plant_stages[node_name]
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
if not stages then
|
|
|
|
return false
|
|
|
|
end
|
2015-05-20 11:22:04 +02:00
|
|
|
|
|
|
|
local max_growth = #stages.stages_left
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
if max_growth <= 0 then
|
|
|
|
return false
|
|
|
|
end
|
2015-05-20 11:22:04 +02:00
|
|
|
|
2018-06-17 10:55:26 +02:00
|
|
|
-- custom growth check
|
|
|
|
local chk = minetest.registered_nodes[node_name].growth_check
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2018-06-17 10:55:26 +02:00
|
|
|
if chk then
|
|
|
|
|
|
|
|
if chk(pos, node_name) then
|
2015-05-20 11:22:04 +02:00
|
|
|
return true
|
|
|
|
end
|
2018-06-17 10:55:26 +02:00
|
|
|
|
|
|
|
-- otherwise check for wet soil beneath crop
|
2015-05-20 11:22:04 +02:00
|
|
|
else
|
2020-07-02 15:31:12 +02:00
|
|
|
local under = minetest.get_node({x = pos.x, y = pos.y - 1, z = pos.z})
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-05-25 21:58:25 +02:00
|
|
|
if minetest.get_item_group(under.name, "soil") < 3 then
|
2016-03-10 18:45:55 +01:00
|
|
|
return true
|
|
|
|
end
|
2015-05-20 11:22:04 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local growth
|
2019-05-08 20:30:20 +02:00
|
|
|
local light_pos = {x = pos.x, y = pos.y, z = pos.z}
|
2015-05-20 11:22:04 +02:00
|
|
|
local lambda = elapsed / STAGE_LENGTH_AVG
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
if lambda < 0.1 then
|
|
|
|
return true
|
|
|
|
end
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2020-12-09 11:22:41 +01:00
|
|
|
local MIN_LIGHT = minetest.registered_nodes[node_name].minlight or farming.min_light
|
|
|
|
local MAX_LIGHT = minetest.registered_nodes[node_name].maxlight or farming.max_light
|
2017-08-29 14:59:04 +02:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
if max_growth == 1 or lambda < 2.0 then
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
local light = (minetest.get_node_light(light_pos) or 0)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
if light < MIN_LIGHT or light > MAX_LIGHT then
|
2016-03-10 18:45:55 +01:00
|
|
|
return true
|
|
|
|
end
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
growth = 1
|
|
|
|
else
|
2016-03-10 18:45:55 +01:00
|
|
|
local night_light = (minetest.get_node_light(light_pos, 0) or 0)
|
|
|
|
local day_light = (minetest.get_node_light(light_pos, 0.5) or 0)
|
2017-08-29 14:59:04 +02:00
|
|
|
local night_growth = night_light >= MIN_LIGHT and night_light <= MAX_LIGHT
|
|
|
|
local day_growth = day_light >= MIN_LIGHT and day_light <= MAX_LIGHT
|
2015-05-20 11:22:04 +02:00
|
|
|
|
|
|
|
if not night_growth then
|
2016-03-10 18:45:55 +01:00
|
|
|
|
|
|
|
if not day_growth then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
lambda = day_or_night_time(elapsed, true) / STAGE_LENGTH_AVG
|
2016-03-10 18:45:55 +01:00
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
elseif not day_growth then
|
2016-03-10 18:45:55 +01:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
lambda = day_or_night_time(elapsed, false) / STAGE_LENGTH_AVG
|
2015-05-20 11:22:04 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
growth = statistics.poisson(lambda, max_growth)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
if growth < 1 then
|
|
|
|
return true
|
|
|
|
end
|
2015-05-20 11:22:04 +02:00
|
|
|
end
|
|
|
|
|
2015-12-08 15:47:22 +01:00
|
|
|
if minetest.registered_nodes[stages.stages_left[growth]] then
|
2017-08-29 14:59:04 +02:00
|
|
|
|
|
|
|
local p2 = minetest.registered_nodes[stages.stages_left[growth] ].place_param2 or 1
|
|
|
|
|
|
|
|
minetest.swap_node(pos, {name = stages.stages_left[growth], param2 = p2})
|
2015-12-08 15:47:22 +01:00
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
2015-05-20 11:22:04 +02:00
|
|
|
|
|
|
|
return growth ~= max_growth
|
|
|
|
end
|
|
|
|
|
2015-08-29 10:27:01 +02:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
-- refill placed plant by crabman (26/08/2015) updated by TenPlus1
|
2015-08-29 10:27:01 +02:00
|
|
|
function farming.refill_plant(player, plantname, index)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2022-03-09 10:19:43 +01:00
|
|
|
if not player then return end
|
|
|
|
|
2015-08-29 10:27:01 +02:00
|
|
|
local inv = player:get_inventory()
|
|
|
|
local old_stack = inv:get_stack("main", index)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2016-03-10 18:45:55 +01:00
|
|
|
if old_stack:get_name() ~= "" then
|
|
|
|
return
|
|
|
|
end
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2017-02-13 14:31:18 +01:00
|
|
|
for i, stack in ipairs(inv:get_list("main")) do
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-08-29 10:27:01 +02:00
|
|
|
if stack:get_name() == plantname and i ~= index then
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-08-29 10:27:01 +02:00
|
|
|
inv:set_stack("main", index, stack)
|
|
|
|
stack:clear()
|
|
|
|
inv:set_stack("main", i, stack)
|
2017-08-29 14:59:04 +02:00
|
|
|
|
2015-08-29 10:27:01 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2016-03-10 18:45:55 +01:00
|
|
|
end
|
2015-08-29 10:27:01 +02:00
|
|
|
|
2014-11-09 20:06:28 +01:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
-- Place Seeds on Soil
|
2014-11-09 20:06:28 +01:00
|
|
|
function farming.place_seed(itemstack, placer, pointed_thing, plantname)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2014-11-09 20:06:28 +01:00
|
|
|
local pt = pointed_thing
|
|
|
|
|
|
|
|
-- check if pointing at a node
|
2015-08-26 16:05:17 +02:00
|
|
|
if not pt or pt.type ~= "node" then
|
2014-11-09 20:06:28 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local under = minetest.get_node(pt.under)
|
2016-10-14 15:48:27 +02:00
|
|
|
|
|
|
|
-- am I right-clicking on something that has a custom on_place set?
|
|
|
|
-- thanks to Krock for helping with this issue :)
|
|
|
|
local def = minetest.registered_nodes[under.name]
|
2019-10-02 20:13:55 +02:00
|
|
|
if placer and itemstack and def and def.on_rightclick then
|
2021-11-16 21:22:43 +01:00
|
|
|
return def.on_rightclick(pt.under, under, placer, itemstack, pt)
|
2016-10-14 15:48:27 +02:00
|
|
|
end
|
|
|
|
|
2014-11-09 20:06:28 +01:00
|
|
|
local above = minetest.get_node(pt.above)
|
|
|
|
|
|
|
|
-- check if pointing at the top of the node
|
2015-07-05 11:54:18 +02:00
|
|
|
if pt.above.y ~= pt.under.y + 1 then
|
2014-11-09 20:06:28 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- return if any of the nodes is not registered
|
|
|
|
if not minetest.registered_nodes[under.name]
|
|
|
|
or not minetest.registered_nodes[above.name] then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- can I replace above node, and am I pointing at soil
|
|
|
|
if not minetest.registered_nodes[above.name].buildable_to
|
2016-04-01 12:02:18 +02:00
|
|
|
or minetest.get_item_group(under.name, "soil") < 2
|
2015-08-26 16:05:17 +02:00
|
|
|
-- avoid multiple seed placement bug
|
|
|
|
or minetest.get_item_group(above.name, "plant") ~= 0 then
|
2014-11-09 20:06:28 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-06-19 19:36:00 +02:00
|
|
|
-- is player planting seed?
|
2018-06-19 19:42:13 +02:00
|
|
|
local name = placer and placer:get_player_name() or ""
|
2018-06-19 19:36:00 +02:00
|
|
|
|
2015-08-26 16:05:17 +02:00
|
|
|
-- if not protected then add node and remove 1 item from the itemstack
|
2018-06-19 19:36:00 +02:00
|
|
|
if not minetest.is_protected(pt.above, name) then
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
local p2 = minetest.registered_nodes[plantname].place_param2 or 1
|
|
|
|
|
|
|
|
minetest.set_node(pt.above, {name = plantname, param2 = p2})
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2018-05-19 16:39:49 +02:00
|
|
|
--minetest.get_node_timer(pt.above):start(1)
|
2019-01-24 12:38:21 +01:00
|
|
|
--farming.handle_growth(pt.above)--, node)
|
2018-05-19 16:39:49 +02:00
|
|
|
|
2016-04-02 14:42:14 +02:00
|
|
|
minetest.sound_play("default_place_node", {pos = pt.above, gain = 1.0})
|
2016-04-01 12:02:18 +02:00
|
|
|
|
2019-07-28 09:38:43 +02:00
|
|
|
if placer and itemstack
|
|
|
|
and not farming.is_creative(placer:get_player_name()) then
|
2017-08-29 14:59:04 +02:00
|
|
|
|
|
|
|
local name = itemstack:get_name()
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-03-01 15:28:51 +01:00
|
|
|
itemstack:take_item()
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-08-29 10:27:01 +02:00
|
|
|
-- check for refill
|
2017-08-29 14:59:04 +02:00
|
|
|
if itemstack:get_count() == 0 then
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2022-03-09 10:19:43 +01:00
|
|
|
minetest.after(0.2,
|
2015-08-29 10:27:01 +02:00
|
|
|
farming.refill_plant,
|
|
|
|
placer,
|
2017-08-29 14:59:04 +02:00
|
|
|
name,
|
2015-08-29 10:27:01 +02:00
|
|
|
placer:get_wield_index()
|
|
|
|
)
|
2015-12-08 15:47:22 +01:00
|
|
|
end
|
2015-03-01 15:28:51 +01:00
|
|
|
end
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2015-03-01 15:28:51 +01:00
|
|
|
return itemstack
|
2014-11-09 20:06:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
-- Function to register plants (default farming compatibility)
|
2014-11-09 20:06:28 +01:00
|
|
|
farming.register_plant = function(name, def)
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2014-11-09 20:06:28 +01:00
|
|
|
if not def.steps then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
local mname = name:split(":")[1]
|
|
|
|
local pname = name:split(":")[2]
|
2017-08-28 13:33:25 +02:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
-- Check def
|
|
|
|
def.description = def.description or S("Seed")
|
|
|
|
def.inventory_image = def.inventory_image or "unknown_item.png"
|
2019-05-08 20:42:33 +02:00
|
|
|
def.minlight = def.minlight or 12
|
2017-09-02 20:34:28 +02:00
|
|
|
def.maxlight = def.maxlight or 15
|
2017-08-28 13:33:25 +02:00
|
|
|
|
2014-11-09 20:06:28 +01:00
|
|
|
-- Register seed
|
|
|
|
minetest.register_node(":" .. mname .. ":seed_" .. pname, {
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2014-11-09 20:06:28 +01:00
|
|
|
description = def.description,
|
|
|
|
tiles = {def.inventory_image},
|
|
|
|
inventory_image = def.inventory_image,
|
|
|
|
wield_image = def.inventory_image,
|
|
|
|
drawtype = "signlike",
|
2018-05-19 16:39:49 +02:00
|
|
|
groups = {seed = 1, snappy = 3, attached_node = 1, flammable = 2},
|
2014-11-09 20:06:28 +01:00
|
|
|
paramtype = "light",
|
|
|
|
paramtype2 = "wallmounted",
|
|
|
|
walkable = false,
|
|
|
|
sunlight_propagates = true,
|
2015-07-05 11:54:18 +02:00
|
|
|
selection_box = farming.select,
|
2017-08-29 14:59:04 +02:00
|
|
|
place_param2 = def.place_param2 or nil,
|
|
|
|
next_plant = mname .. ":" .. pname .. "_1",
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2014-11-09 20:06:28 +01:00
|
|
|
on_place = function(itemstack, placer, pointed_thing)
|
2016-10-14 15:48:27 +02:00
|
|
|
return farming.place_seed(itemstack, placer,
|
|
|
|
pointed_thing, mname .. ":" .. pname .. "_1")
|
2016-04-01 12:02:18 +02:00
|
|
|
end,
|
2014-11-09 20:06:28 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
-- Register harvest
|
|
|
|
minetest.register_craftitem(":" .. mname .. ":" .. pname, {
|
|
|
|
description = pname:gsub("^%l", string.upper),
|
|
|
|
inventory_image = mname .. "_" .. pname .. ".png",
|
2018-03-31 17:32:17 +02:00
|
|
|
groups = def.groups or {flammable = 2},
|
2014-11-09 20:06:28 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
-- Register growing steps
|
2015-08-26 16:05:17 +02:00
|
|
|
for i = 1, def.steps do
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2017-02-13 14:31:18 +01:00
|
|
|
local base_rarity = 1
|
|
|
|
if def.steps ~= 1 then
|
|
|
|
base_rarity = 8 - (i - 1) * 7 / (def.steps - 1)
|
|
|
|
end
|
2014-11-09 20:06:28 +01:00
|
|
|
local drop = {
|
|
|
|
items = {
|
2017-02-13 14:31:18 +01:00
|
|
|
{items = {mname .. ":" .. pname}, rarity = base_rarity},
|
|
|
|
{items = {mname .. ":" .. pname}, rarity = base_rarity * 2},
|
|
|
|
{items = {mname .. ":seed_" .. pname}, rarity = base_rarity},
|
|
|
|
{items = {mname .. ":seed_" .. pname}, rarity = base_rarity * 2},
|
2014-11-09 20:06:28 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-13 14:31:18 +01:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
local g = {
|
|
|
|
snappy = 3, flammable = 2, plant = 1, growing = 1,
|
|
|
|
attached_node = 1, not_in_creative_inventory = 1,
|
|
|
|
}
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2014-11-09 20:06:28 +01:00
|
|
|
-- Last step doesn't need growing=1 so Abm never has to check these
|
|
|
|
if i == def.steps then
|
2016-06-04 16:00:53 +02:00
|
|
|
g.growing = 0
|
2014-11-09 20:06:28 +01:00
|
|
|
end
|
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
local node_name = mname .. ":" .. pname .. "_" .. i
|
2015-12-08 15:47:22 +01:00
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
local next_plant = nil
|
|
|
|
|
|
|
|
if i < def.steps then
|
|
|
|
next_plant = mname .. ":" .. pname .. "_" .. (i + 1)
|
|
|
|
end
|
|
|
|
|
2015-05-20 11:22:04 +02:00
|
|
|
minetest.register_node(node_name, {
|
2014-11-09 20:06:28 +01:00
|
|
|
drawtype = "plantlike",
|
|
|
|
waving = 1,
|
|
|
|
tiles = {mname .. "_" .. pname .. "_" .. i .. ".png"},
|
|
|
|
paramtype = "light",
|
2018-03-22 16:57:36 +01:00
|
|
|
paramtype2 = def.paramtype2,
|
|
|
|
place_param2 = def.place_param2,
|
2014-11-09 20:06:28 +01:00
|
|
|
walkable = false,
|
|
|
|
buildable_to = true,
|
2018-05-19 16:39:49 +02:00
|
|
|
sunlight_propagates = true,
|
2014-11-09 20:06:28 +01:00
|
|
|
drop = drop,
|
2015-07-05 11:54:18 +02:00
|
|
|
selection_box = farming.select,
|
2014-11-09 20:06:28 +01:00
|
|
|
groups = g,
|
|
|
|
sounds = default.node_sound_leaves_defaults(),
|
2017-08-28 13:33:25 +02:00
|
|
|
minlight = def.minlight,
|
|
|
|
maxlight = def.maxlight,
|
2020-07-02 15:31:12 +02:00
|
|
|
next_plant = next_plant
|
2014-11-09 20:06:28 +01:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2018-09-29 17:17:28 +02:00
|
|
|
-- add to farming.registered_plants
|
|
|
|
farming.registered_plants[mname .. ":" .. pname] = {
|
2018-09-29 17:53:18 +02:00
|
|
|
crop = mname .. ":" .. pname,
|
2018-09-29 17:17:28 +02:00
|
|
|
seed = mname .. ":seed_" .. pname,
|
|
|
|
steps = def.steps,
|
|
|
|
minlight = def.minlight,
|
|
|
|
maxlight = def.maxlight
|
|
|
|
}
|
2019-01-06 11:28:24 +01:00
|
|
|
--print(dump(farming.registered_plants[mname .. ":" .. pname]))
|
2014-11-09 20:06:28 +01:00
|
|
|
-- Return info
|
2017-08-29 14:59:04 +02:00
|
|
|
return {seed = mname .. ":seed_" .. pname, harvest = mname .. ":" .. pname}
|
2014-11-09 20:06:28 +01:00
|
|
|
end
|
|
|
|
|
2016-06-04 16:00:53 +02:00
|
|
|
|
2017-04-28 19:40:57 +02:00
|
|
|
-- default settings
|
2019-04-27 09:34:29 +02:00
|
|
|
farming.carrot = 0.001
|
|
|
|
farming.potato = 0.001
|
|
|
|
farming.tomato = 0.001
|
|
|
|
farming.cucumber = 0.001
|
|
|
|
farming.corn = 0.001
|
|
|
|
farming.coffee = 0.001
|
|
|
|
farming.melon = 0.001
|
|
|
|
farming.pumpkin = 0.001
|
2017-04-28 19:40:57 +02:00
|
|
|
farming.cocoa = true
|
2019-04-27 09:34:29 +02:00
|
|
|
farming.raspberry = 0.001
|
|
|
|
farming.blueberry = 0.001
|
|
|
|
farming.rhubarb = 0.001
|
|
|
|
farming.beans = 0.001
|
|
|
|
farming.grapes = 0.001
|
2017-04-28 19:40:57 +02:00
|
|
|
farming.barley = true
|
2019-06-25 16:08:08 +02:00
|
|
|
farming.chili = 0.003
|
|
|
|
farming.hemp = 0.003
|
2019-04-27 09:34:29 +02:00
|
|
|
farming.garlic = 0.001
|
|
|
|
farming.onion = 0.001
|
|
|
|
farming.pepper = 0.002
|
|
|
|
farming.pineapple = 0.001
|
|
|
|
farming.peas = 0.001
|
|
|
|
farming.beetroot = 0.001
|
2020-05-27 12:07:31 +02:00
|
|
|
farming.mint = 0.005
|
2020-07-01 20:32:38 +02:00
|
|
|
farming.cabbage = 0.001
|
2020-12-10 16:37:44 +01:00
|
|
|
farming.blackberry = 0.002
|
2020-12-13 11:37:08 +01:00
|
|
|
farming.soy = 0.001
|
|
|
|
farming.vanilla = 0.001
|
2020-12-10 16:37:44 +01:00
|
|
|
farming.lettuce = 0.001
|
2021-03-11 10:49:52 +01:00
|
|
|
farming.artichoke = 0.001
|
|
|
|
farming.parsley = 0.002
|
2021-09-28 15:03:49 +02:00
|
|
|
farming.sunflower = 0.001
|
2019-01-06 11:28:24 +01:00
|
|
|
farming.grains = true
|
2021-12-04 15:06:18 +01:00
|
|
|
farming.rice = true
|
2019-04-27 09:34:29 +02:00
|
|
|
farming.rarety = 0.002
|
2017-04-28 19:40:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- Load new global settings if found inside mod folder
|
|
|
|
local input = io.open(farming.path.."/farming.conf", "r")
|
|
|
|
if input then
|
|
|
|
dofile(farming.path .. "/farming.conf")
|
|
|
|
input:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- load new world-specific settings if found inside world folder
|
|
|
|
local worldpath = minetest.get_worldpath()
|
2019-01-11 11:23:22 +01:00
|
|
|
input = io.open(worldpath.."/farming.conf", "r")
|
2017-04-28 19:40:57 +02:00
|
|
|
if input then
|
|
|
|
dofile(worldpath .. "/farming.conf")
|
|
|
|
input:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-08-29 14:59:04 +02:00
|
|
|
-- important items
|
2016-06-04 16:00:53 +02:00
|
|
|
dofile(farming.path.."/soil.lua")
|
|
|
|
dofile(farming.path.."/hoes.lua")
|
|
|
|
dofile(farming.path.."/grass.lua")
|
2018-03-21 11:51:12 +01:00
|
|
|
dofile(farming.path.."/utensils.lua")
|
2017-08-29 14:59:04 +02:00
|
|
|
|
|
|
|
-- default crops
|
2018-06-09 17:47:38 +02:00
|
|
|
dofile(farming.path.."/crops/wheat.lua")
|
|
|
|
dofile(farming.path.."/crops/cotton.lua")
|
2017-08-29 14:59:04 +02:00
|
|
|
|
2018-06-09 17:47:38 +02:00
|
|
|
|
|
|
|
-- helper function
|
|
|
|
local function ddoo(file, check)
|
|
|
|
|
|
|
|
if check then
|
|
|
|
dofile(farming.path .. "/crops/" .. file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- add additional crops and food (if enabled)
|
|
|
|
ddoo("carrot.lua", farming.carrot)
|
|
|
|
ddoo("potato.lua", farming.potato)
|
|
|
|
ddoo("tomato.lua", farming.tomato)
|
|
|
|
ddoo("cucumber.lua", farming.cucumber)
|
|
|
|
ddoo("corn.lua", farming.corn)
|
|
|
|
ddoo("coffee.lua", farming.coffee)
|
|
|
|
ddoo("melon.lua", farming.melon)
|
|
|
|
ddoo("pumpkin.lua", farming.pumpkin)
|
|
|
|
ddoo("cocoa.lua", farming.cocoa)
|
|
|
|
ddoo("raspberry.lua", farming.raspberry)
|
|
|
|
ddoo("blueberry.lua", farming.blueberry)
|
|
|
|
ddoo("rhubarb.lua", farming.rhubarb)
|
|
|
|
ddoo("beans.lua", farming.beans)
|
|
|
|
ddoo("grapes.lua", farming.grapes)
|
|
|
|
ddoo("barley.lua", farming.barley)
|
|
|
|
ddoo("hemp.lua", farming.hemp)
|
|
|
|
ddoo("garlic.lua", farming.garlic)
|
|
|
|
ddoo("onion.lua", farming.onion)
|
|
|
|
ddoo("pepper.lua", farming.pepper)
|
|
|
|
ddoo("pineapple.lua", farming.pineapple)
|
|
|
|
ddoo("peas.lua", farming.peas)
|
|
|
|
ddoo("beetroot.lua", farming.beetroot)
|
|
|
|
ddoo("chili.lua", farming.chili)
|
2019-01-06 11:28:24 +01:00
|
|
|
ddoo("ryeoatrice.lua", farming.grains)
|
2021-12-04 15:06:18 +01:00
|
|
|
ddoo("rice.lua", farming.rice)
|
2020-05-27 12:07:31 +02:00
|
|
|
ddoo("mint.lua", farming.mint)
|
2020-07-01 20:32:38 +02:00
|
|
|
ddoo("cabbage.lua", farming.cabbage)
|
2020-12-10 16:37:44 +01:00
|
|
|
ddoo("blackberry.lua", farming.blackberry)
|
2020-12-13 11:37:08 +01:00
|
|
|
ddoo("soy.lua", farming.soy)
|
|
|
|
ddoo("vanilla.lua", farming.vanilla)
|
2020-12-10 16:37:44 +01:00
|
|
|
ddoo("lettuce.lua", farming.lettuce)
|
2021-03-11 10:49:52 +01:00
|
|
|
ddoo("artichoke.lua", farming.artichoke)
|
|
|
|
ddoo("parsley.lua", farming.parsley)
|
2021-09-28 15:03:49 +02:00
|
|
|
ddoo("sunflower.lua", farming.sunflower)
|
2018-06-09 17:47:38 +02:00
|
|
|
|
2020-07-02 15:31:12 +02:00
|
|
|
dofile(farming.path .. "/food.lua")
|
|
|
|
dofile(farming.path .. "/mapgen.lua")
|
|
|
|
dofile(farming.path .. "/compatibility.lua") -- Farming Plus compatibility
|
|
|
|
dofile(farming.path .. "/lucky_block.lua")
|