init.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. --[[
  2. Farming Redo Mod
  3. by TenPlus1
  4. NEW growing routine by prestidigitator
  5. auto-refill by crabman77
  6. ]]
  7. farming = {
  8. mod = "redo",
  9. version = "20210311",
  10. path = minetest.get_modpath("farming"),
  11. select = {
  12. type = "fixed",
  13. fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5}
  14. },
  15. registered_plants = {},
  16. min_light = 12,
  17. max_light = 15
  18. }
  19. local creative_mode_cache = minetest.settings:get_bool("creative_mode")
  20. function farming.is_creative(name)
  21. return creative_mode_cache or minetest.check_player_privs(name, {creative = true})
  22. end
  23. local statistics = dofile(farming.path .. "/statistics.lua")
  24. -- Intllib
  25. local S = minetest.get_translator and minetest.get_translator("farming") or
  26. dofile(farming.path .. "/intllib.lua")
  27. farming.intllib = S
  28. -- Utility Function
  29. local time_speed = tonumber(minetest.settings:get("time_speed")) or 72
  30. local SECS_PER_CYCLE = (time_speed > 0 and (24 * 60 * 60) / time_speed) or 0
  31. local function clamp(x, min, max)
  32. return (x < min and min) or (x > max and max) or x
  33. end
  34. -- return amount of day or night that has elapsed
  35. -- dt is time elapsed, count_day if true counts day, otherwise night
  36. local function day_or_night_time(dt, count_day)
  37. local t_day = minetest.get_timeofday()
  38. local t1_day = t_day - dt / SECS_PER_CYCLE
  39. local t1_c, t2_c -- t1_c < t2_c and t2_c always in [0, 1)
  40. if count_day then
  41. if t_day < 0.25 then
  42. t1_c = t1_day + 0.75 -- Relative to sunup, yesterday
  43. t2_c = t_day + 0.75
  44. else
  45. t1_c = t1_day - 0.25 -- Relative to sunup, today
  46. t2_c = t_day - 0.25
  47. end
  48. else
  49. if t_day < 0.75 then
  50. t1_c = t1_day + 0.25 -- Relative to sundown, yesterday
  51. t2_c = t_day + 0.25
  52. else
  53. t1_c = t1_day - 0.75 -- Relative to sundown, today
  54. t2_c = t_day - 0.75
  55. end
  56. end
  57. local dt_c = clamp(t2_c, 0, 0.5) - clamp(t1_c, 0, 0.5) -- this cycle
  58. if t1_c < -0.5 then
  59. local nc = math.floor(-t1_c)
  60. t1_c = t1_c + nc
  61. dt_c = dt_c + 0.5 * nc + clamp(-t1_c - 0.5, 0, 0.5)
  62. end
  63. return dt_c * SECS_PER_CYCLE
  64. end
  65. -- Growth Logic
  66. local STAGE_LENGTH_AVG = tonumber(
  67. minetest.settings:get("farming_stage_length")) or 200 -- 160
  68. local STAGE_LENGTH_DEV = STAGE_LENGTH_AVG / 6
  69. -- return plant name and stage from node provided
  70. local function plant_name_stage(node)
  71. local name
  72. if type(node) == "table" then
  73. if node.name then
  74. name = node.name
  75. elseif node.x and node.y and node.z then
  76. node = minetest.get_node_or_nil(node)
  77. name = node and node.name
  78. end
  79. else
  80. name = tostring(node)
  81. end
  82. if not name or name == "ignore" then
  83. return nil
  84. end
  85. local sep_pos = name:find("_[^_]+$")
  86. if sep_pos and sep_pos > 1 then
  87. local stage = tonumber(name:sub(sep_pos + 1))
  88. if stage and stage >= 0 then
  89. return name:sub(1, sep_pos - 1), stage
  90. end
  91. end
  92. return name, 0
  93. end
  94. -- Map from node name to
  95. -- { plant_name = ..., name = ..., stage = n, stages_left = { node_name, ... } }
  96. local plant_stages = {}
  97. farming.plant_stages = plant_stages
  98. --- Registers the stages of growth of a (possible plant) node.
  99. --
  100. -- @param node
  101. -- Node or position table, or node name.
  102. -- @return
  103. -- The (possibly zero) number of stages of growth the plant will go through
  104. -- before being fully grown, or nil if not a plant.
  105. local register_plant_node
  106. -- Recursive helper
  107. local function reg_plant_stages(plant_name, stage, force_last)
  108. local node_name = plant_name and plant_name .. "_" .. stage
  109. local node_def = node_name and minetest.registered_nodes[node_name]
  110. if not node_def then
  111. return nil
  112. end
  113. local stages = plant_stages[node_name]
  114. if stages then
  115. return stages
  116. end
  117. if minetest.get_item_group(node_name, "growing") > 0 then
  118. local ns = reg_plant_stages(plant_name, stage + 1, true)
  119. local stages_left = (ns and { ns.name, unpack(ns.stages_left) }) or {}
  120. stages = {
  121. plant_name = plant_name,
  122. name = node_name,
  123. stage = stage,
  124. stages_left = stages_left
  125. }
  126. if #stages_left > 0 then
  127. local old_constr = node_def.on_construct
  128. local old_destr = node_def.on_destruct
  129. minetest.override_item(node_name,
  130. {
  131. on_construct = function(pos)
  132. if old_constr then
  133. old_constr(pos)
  134. end
  135. farming.handle_growth(pos)
  136. end,
  137. on_destruct = function(pos)
  138. minetest.get_node_timer(pos):stop()
  139. if old_destr then
  140. old_destr(pos)
  141. end
  142. end,
  143. on_timer = function(pos, elapsed)
  144. return farming.plant_growth_timer(pos, elapsed, node_name)
  145. end,
  146. })
  147. end
  148. elseif force_last then
  149. stages = {
  150. plant_name = plant_name,
  151. name = node_name,
  152. stage = stage,
  153. stages_left = {}
  154. }
  155. else
  156. return nil
  157. end
  158. plant_stages[node_name] = stages
  159. return stages
  160. end
  161. local register_plant_node = function(node)
  162. local plant_name, stage = plant_name_stage(node)
  163. if plant_name then
  164. local stages = reg_plant_stages(plant_name, stage, false)
  165. return stages and #stages.stages_left
  166. else
  167. return nil
  168. end
  169. end
  170. local function set_growing(pos, stages_left)
  171. if not stages_left then
  172. return
  173. end
  174. local timer = minetest.get_node_timer(pos)
  175. if stages_left > 0 then
  176. if not timer:is_started() then
  177. local stage_length = statistics.normal(STAGE_LENGTH_AVG, STAGE_LENGTH_DEV)
  178. stage_length = clamp(stage_length, 0.5 * STAGE_LENGTH_AVG, 3.0 * STAGE_LENGTH_AVG)
  179. timer:set(stage_length, -0.5 * math.random() * STAGE_LENGTH_AVG)
  180. end
  181. elseif timer:is_started() then
  182. timer:stop()
  183. end
  184. end
  185. -- detects a crop at given position, starting or stopping growth timer when needed
  186. function farming.handle_growth(pos, node)
  187. if not pos then
  188. return
  189. end
  190. local stages_left = register_plant_node(node or pos)
  191. if stages_left then
  192. set_growing(pos, stages_left)
  193. end
  194. end
  195. minetest.after(0, function()
  196. for _, node_def in pairs(minetest.registered_nodes) do
  197. register_plant_node(node_def)
  198. end
  199. end)
  200. -- Just in case a growing type or added node is missed (also catches existing
  201. -- nodes added to map before timers were incorporated).
  202. minetest.register_abm({
  203. nodenames = {"group:growing"},
  204. interval = 300,
  205. chance = 1,
  206. catch_up = false,
  207. action = function(pos, node)
  208. farming.handle_growth(pos, node)
  209. end
  210. })
  211. -- Plant timer function that grows plants under the right conditions.
  212. function farming.plant_growth_timer(pos, elapsed, node_name)
  213. local stages = plant_stages[node_name]
  214. if not stages then
  215. return false
  216. end
  217. local max_growth = #stages.stages_left
  218. if max_growth <= 0 then
  219. return false
  220. end
  221. -- custom growth check
  222. local chk = minetest.registered_nodes[node_name].growth_check
  223. if chk then
  224. if chk(pos, node_name) then
  225. return true
  226. end
  227. -- otherwise check for wet soil beneath crop
  228. else
  229. local under = minetest.get_node({x = pos.x, y = pos.y - 1, z = pos.z})
  230. if minetest.get_item_group(under.name, "soil") < 3 then
  231. return true
  232. end
  233. end
  234. local growth
  235. local light_pos = {x = pos.x, y = pos.y, z = pos.z}
  236. local lambda = elapsed / STAGE_LENGTH_AVG
  237. if lambda < 0.1 then
  238. return true
  239. end
  240. local MIN_LIGHT = minetest.registered_nodes[node_name].minlight or farming.min_light
  241. local MAX_LIGHT = minetest.registered_nodes[node_name].maxlight or farming.max_light
  242. if max_growth == 1 or lambda < 2.0 then
  243. local light = (minetest.get_node_light(light_pos) or 0)
  244. --print ("light level:", light)
  245. if light < MIN_LIGHT or light > MAX_LIGHT then
  246. return true
  247. end
  248. growth = 1
  249. else
  250. local night_light = (minetest.get_node_light(light_pos, 0) or 0)
  251. local day_light = (minetest.get_node_light(light_pos, 0.5) or 0)
  252. local night_growth = night_light >= MIN_LIGHT and night_light <= MAX_LIGHT
  253. local day_growth = day_light >= MIN_LIGHT and day_light <= MAX_LIGHT
  254. if not night_growth then
  255. if not day_growth then
  256. return true
  257. end
  258. lambda = day_or_night_time(elapsed, true) / STAGE_LENGTH_AVG
  259. elseif not day_growth then
  260. lambda = day_or_night_time(elapsed, false) / STAGE_LENGTH_AVG
  261. end
  262. growth = statistics.poisson(lambda, max_growth)
  263. if growth < 1 then
  264. return true
  265. end
  266. end
  267. if minetest.registered_nodes[stages.stages_left[growth]] then
  268. local p2 = minetest.registered_nodes[stages.stages_left[growth] ].place_param2 or 1
  269. minetest.swap_node(pos, {name = stages.stages_left[growth], param2 = p2})
  270. else
  271. return true
  272. end
  273. return growth ~= max_growth
  274. end
  275. -- refill placed plant by crabman (26/08/2015) updated by TenPlus1
  276. function farming.refill_plant(player, plantname, index)
  277. local inv = player:get_inventory()
  278. local old_stack = inv:get_stack("main", index)
  279. if old_stack:get_name() ~= "" then
  280. return
  281. end
  282. for i, stack in ipairs(inv:get_list("main")) do
  283. if stack:get_name() == plantname and i ~= index then
  284. inv:set_stack("main", index, stack)
  285. stack:clear()
  286. inv:set_stack("main", i, stack)
  287. return
  288. end
  289. end
  290. end
  291. -- Place Seeds on Soil
  292. function farming.place_seed(itemstack, placer, pointed_thing, plantname)
  293. local pt = pointed_thing
  294. -- check if pointing at a node
  295. if not pt or pt.type ~= "node" then
  296. return
  297. end
  298. local under = minetest.get_node(pt.under)
  299. -- am I right-clicking on something that has a custom on_place set?
  300. -- thanks to Krock for helping with this issue :)
  301. local def = minetest.registered_nodes[under.name]
  302. if placer and itemstack and def and def.on_rightclick then
  303. return def.on_rightclick(pt.under, under, placer, itemstack)
  304. end
  305. local above = minetest.get_node(pt.above)
  306. -- check if pointing at the top of the node
  307. if pt.above.y ~= pt.under.y + 1 then
  308. return
  309. end
  310. -- return if any of the nodes is not registered
  311. if not minetest.registered_nodes[under.name]
  312. or not minetest.registered_nodes[above.name] then
  313. return
  314. end
  315. -- can I replace above node, and am I pointing at soil
  316. if not minetest.registered_nodes[above.name].buildable_to
  317. or minetest.get_item_group(under.name, "soil") < 2
  318. -- avoid multiple seed placement bug
  319. or minetest.get_item_group(above.name, "plant") ~= 0 then
  320. return
  321. end
  322. -- is player planting seed?
  323. local name = placer and placer:get_player_name() or ""
  324. -- if not protected then add node and remove 1 item from the itemstack
  325. if not minetest.is_protected(pt.above, name) then
  326. local p2 = minetest.registered_nodes[plantname].place_param2 or 1
  327. minetest.set_node(pt.above, {name = plantname, param2 = p2})
  328. --minetest.get_node_timer(pt.above):start(1)
  329. --farming.handle_growth(pt.above)--, node)
  330. minetest.sound_play("default_place_node", {pos = pt.above, gain = 1.0})
  331. if placer and itemstack
  332. and not farming.is_creative(placer:get_player_name()) then
  333. local name = itemstack:get_name()
  334. itemstack:take_item()
  335. -- check for refill
  336. if itemstack:get_count() == 0 then
  337. minetest.after(0.10,
  338. farming.refill_plant,
  339. placer,
  340. name,
  341. placer:get_wield_index()
  342. )
  343. end
  344. end
  345. return itemstack
  346. end
  347. end
  348. -- Function to register plants (default farming compatibility)
  349. farming.register_plant = function(name, def)
  350. if not def.steps then
  351. return nil
  352. end
  353. local mname = name:split(":")[1]
  354. local pname = name:split(":")[2]
  355. -- Check def
  356. def.description = def.description or S("Seed")
  357. def.inventory_image = def.inventory_image or "unknown_item.png"
  358. def.minlight = def.minlight or 12
  359. def.maxlight = def.maxlight or 15
  360. -- Register seed
  361. minetest.register_node(":" .. mname .. ":seed_" .. pname, {
  362. description = def.description,
  363. tiles = {def.inventory_image},
  364. inventory_image = def.inventory_image,
  365. wield_image = def.inventory_image,
  366. drawtype = "signlike",
  367. groups = {seed = 1, snappy = 3, attached_node = 1, flammable = 2},
  368. paramtype = "light",
  369. paramtype2 = "wallmounted",
  370. walkable = false,
  371. sunlight_propagates = true,
  372. selection_box = farming.select,
  373. place_param2 = def.place_param2 or nil,
  374. next_plant = mname .. ":" .. pname .. "_1",
  375. on_place = function(itemstack, placer, pointed_thing)
  376. return farming.place_seed(itemstack, placer,
  377. pointed_thing, mname .. ":" .. pname .. "_1")
  378. end,
  379. })
  380. -- Register harvest
  381. minetest.register_craftitem(":" .. mname .. ":" .. pname, {
  382. description = pname:gsub("^%l", string.upper),
  383. inventory_image = mname .. "_" .. pname .. ".png",
  384. groups = def.groups or {flammable = 2},
  385. })
  386. -- Register growing steps
  387. for i = 1, def.steps do
  388. local base_rarity = 1
  389. if def.steps ~= 1 then
  390. base_rarity = 8 - (i - 1) * 7 / (def.steps - 1)
  391. end
  392. local drop = {
  393. items = {
  394. {items = {mname .. ":" .. pname}, rarity = base_rarity},
  395. {items = {mname .. ":" .. pname}, rarity = base_rarity * 2},
  396. {items = {mname .. ":seed_" .. pname}, rarity = base_rarity},
  397. {items = {mname .. ":seed_" .. pname}, rarity = base_rarity * 2},
  398. }
  399. }
  400. local g = {
  401. snappy = 3, flammable = 2, plant = 1, growing = 1,
  402. attached_node = 1, not_in_creative_inventory = 1,
  403. }
  404. -- Last step doesn't need growing=1 so Abm never has to check these
  405. if i == def.steps then
  406. g.growing = 0
  407. end
  408. local node_name = mname .. ":" .. pname .. "_" .. i
  409. local next_plant = nil
  410. if i < def.steps then
  411. next_plant = mname .. ":" .. pname .. "_" .. (i + 1)
  412. end
  413. minetest.register_node(node_name, {
  414. drawtype = "plantlike",
  415. waving = 1,
  416. tiles = {mname .. "_" .. pname .. "_" .. i .. ".png"},
  417. paramtype = "light",
  418. paramtype2 = def.paramtype2,
  419. place_param2 = def.place_param2,
  420. walkable = false,
  421. buildable_to = true,
  422. sunlight_propagates = true,
  423. drop = drop,
  424. selection_box = farming.select,
  425. groups = g,
  426. sounds = default.node_sound_leaves_defaults(),
  427. minlight = def.minlight,
  428. maxlight = def.maxlight,
  429. next_plant = next_plant
  430. })
  431. end
  432. -- add to farming.registered_plants
  433. farming.registered_plants[mname .. ":" .. pname] = {
  434. crop = mname .. ":" .. pname,
  435. seed = mname .. ":seed_" .. pname,
  436. steps = def.steps,
  437. minlight = def.minlight,
  438. maxlight = def.maxlight
  439. }
  440. --print(dump(farming.registered_plants[mname .. ":" .. pname]))
  441. -- Return info
  442. return {seed = mname .. ":seed_" .. pname, harvest = mname .. ":" .. pname}
  443. end
  444. -- default settings
  445. farming.carrot = 0.001
  446. farming.potato = 0.001
  447. farming.tomato = 0.001
  448. farming.cucumber = 0.001
  449. farming.corn = 0.001
  450. farming.coffee = 0.001
  451. farming.melon = 0.001
  452. farming.pumpkin = 0.001
  453. farming.cocoa = true
  454. farming.raspberry = 0.001
  455. farming.blueberry = 0.001
  456. farming.rhubarb = 0.001
  457. farming.beans = 0.001
  458. farming.grapes = 0.001
  459. farming.barley = true
  460. farming.chili = 0.003
  461. farming.hemp = 0.003
  462. farming.garlic = 0.001
  463. farming.onion = 0.001
  464. farming.pepper = 0.002
  465. farming.pineapple = 0.001
  466. farming.peas = 0.001
  467. farming.beetroot = 0.001
  468. farming.mint = 0.005
  469. farming.cabbage = 0.001
  470. farming.blackberry = 0.002
  471. farming.soy = 0.001
  472. farming.vanilla = 0.001
  473. farming.lettuce = 0.001
  474. farming.artichoke = 0.001
  475. farming.parsley = 0.002
  476. farming.grains = true
  477. farming.rarety = 0.002
  478. -- Load new global settings if found inside mod folder
  479. local input = io.open(farming.path.."/farming.conf", "r")
  480. if input then
  481. dofile(farming.path .. "/farming.conf")
  482. input:close()
  483. end
  484. -- load new world-specific settings if found inside world folder
  485. local worldpath = minetest.get_worldpath()
  486. input = io.open(worldpath.."/farming.conf", "r")
  487. if input then
  488. dofile(worldpath .. "/farming.conf")
  489. input:close()
  490. end
  491. -- important items
  492. dofile(farming.path.."/soil.lua")
  493. dofile(farming.path.."/hoes.lua")
  494. dofile(farming.path.."/grass.lua")
  495. dofile(farming.path.."/utensils.lua")
  496. -- default crops
  497. dofile(farming.path.."/crops/wheat.lua")
  498. dofile(farming.path.."/crops/cotton.lua")
  499. -- helper function
  500. local function ddoo(file, check)
  501. if check then
  502. dofile(farming.path .. "/crops/" .. file)
  503. end
  504. end
  505. -- add additional crops and food (if enabled)
  506. ddoo("carrot.lua", farming.carrot)
  507. ddoo("potato.lua", farming.potato)
  508. ddoo("tomato.lua", farming.tomato)
  509. ddoo("cucumber.lua", farming.cucumber)
  510. ddoo("corn.lua", farming.corn)
  511. ddoo("coffee.lua", farming.coffee)
  512. ddoo("melon.lua", farming.melon)
  513. ddoo("pumpkin.lua", farming.pumpkin)
  514. ddoo("cocoa.lua", farming.cocoa)
  515. ddoo("raspberry.lua", farming.raspberry)
  516. ddoo("blueberry.lua", farming.blueberry)
  517. ddoo("rhubarb.lua", farming.rhubarb)
  518. ddoo("beans.lua", farming.beans)
  519. ddoo("grapes.lua", farming.grapes)
  520. ddoo("barley.lua", farming.barley)
  521. ddoo("hemp.lua", farming.hemp)
  522. ddoo("garlic.lua", farming.garlic)
  523. ddoo("onion.lua", farming.onion)
  524. ddoo("pepper.lua", farming.pepper)
  525. ddoo("pineapple.lua", farming.pineapple)
  526. ddoo("peas.lua", farming.peas)
  527. ddoo("beetroot.lua", farming.beetroot)
  528. ddoo("chili.lua", farming.chili)
  529. ddoo("ryeoatrice.lua", farming.grains)
  530. ddoo("mint.lua", farming.mint)
  531. ddoo("cabbage.lua", farming.cabbage)
  532. ddoo("blackberry.lua", farming.blackberry)
  533. ddoo("soy.lua", farming.soy)
  534. ddoo("vanilla.lua", farming.vanilla)
  535. ddoo("lettuce.lua", farming.lettuce)
  536. ddoo("artichoke.lua", farming.artichoke)
  537. ddoo("parsley.lua", farming.parsley)
  538. dofile(farming.path .. "/food.lua")
  539. dofile(farming.path .. "/mapgen.lua")
  540. dofile(farming.path .. "/compatibility.lua") -- Farming Plus compatibility
  541. dofile(farming.path .. "/lucky_block.lua")