init.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. -- apply soil effect
  180. local node_under = minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z})
  181. local soil = minetest.get_item_group(node_under.name, "soil")
  182. stage_length = stage_length*3/soil
  183. timer:set(stage_length, -0.5 * math.random() * STAGE_LENGTH_AVG)
  184. end
  185. elseif timer:is_started() then
  186. timer:stop()
  187. end
  188. end
  189. -- detects a crop at given position, starting or stopping growth timer when needed
  190. function farming.handle_growth(pos, node)
  191. if not pos then
  192. return
  193. end
  194. local stages_left = register_plant_node(node or pos)
  195. if stages_left then
  196. set_growing(pos, stages_left)
  197. end
  198. end
  199. minetest.after(0, function()
  200. for _, node_def in pairs(minetest.registered_nodes) do
  201. register_plant_node(node_def)
  202. end
  203. end)
  204. -- Just in case a growing type or added node is missed (also catches existing
  205. -- nodes added to map before timers were incorporated).
  206. minetest.register_abm({
  207. nodenames = {"group:growing"},
  208. interval = 300,
  209. chance = 1,
  210. catch_up = false,
  211. action = function(pos, node)
  212. farming.handle_growth(pos, node)
  213. end
  214. })
  215. -- Plant timer function that grows plants under the right conditions.
  216. function farming.plant_growth_timer(pos, elapsed, node_name)
  217. local stages = plant_stages[node_name]
  218. if not stages then
  219. return false
  220. end
  221. local max_growth = #stages.stages_left
  222. if max_growth <= 0 then
  223. return false
  224. end
  225. -- custom growth check
  226. local chk = minetest.registered_nodes[node_name].growth_check
  227. if chk then
  228. if chk(pos, node_name) then
  229. return true
  230. end
  231. -- otherwise check for wet soil beneath crop
  232. else
  233. local under = minetest.get_node({x = pos.x, y = pos.y - 1, z = pos.z})
  234. if minetest.get_item_group(under.name, "wet") == 0 then
  235. return true
  236. end
  237. if minetest.get_item_group(under.name, "soil") == 0 then
  238. return true
  239. end
  240. end
  241. local growth
  242. local light_pos = {x = pos.x, y = pos.y, z = pos.z}
  243. local lambda = elapsed / STAGE_LENGTH_AVG
  244. if lambda < 0.1 then
  245. return true
  246. end
  247. local MIN_LIGHT = minetest.registered_nodes[node_name].minlight or farming.min_light
  248. local MAX_LIGHT = minetest.registered_nodes[node_name].maxlight or farming.max_light
  249. if max_growth == 1 or lambda < 2.0 then
  250. local light = (minetest.get_node_light(light_pos) or 0)
  251. --print ("light level:", light)
  252. if light < MIN_LIGHT or light > MAX_LIGHT then
  253. return true
  254. end
  255. growth = 1
  256. else
  257. local night_light = (minetest.get_node_light(light_pos, 0) or 0)
  258. local day_light = (minetest.get_node_light(light_pos, 0.5) or 0)
  259. local night_growth = night_light >= MIN_LIGHT and night_light <= MAX_LIGHT
  260. local day_growth = day_light >= MIN_LIGHT and day_light <= MAX_LIGHT
  261. if not night_growth then
  262. if not day_growth then
  263. return true
  264. end
  265. lambda = day_or_night_time(elapsed, true) / STAGE_LENGTH_AVG
  266. elseif not day_growth then
  267. lambda = day_or_night_time(elapsed, false) / STAGE_LENGTH_AVG
  268. end
  269. growth = statistics.poisson(lambda, max_growth)
  270. if growth < 1 then
  271. return true
  272. end
  273. end
  274. if minetest.registered_nodes[stages.stages_left[growth]] then
  275. local p2 = minetest.registered_nodes[stages.stages_left[growth] ].place_param2 or 1
  276. minetest.swap_node(pos, {name = stages.stages_left[growth], param2 = p2})
  277. else
  278. return true
  279. end
  280. return growth ~= max_growth
  281. end
  282. -- refill placed plant by crabman (26/08/2015) updated by TenPlus1
  283. function farming.refill_plant(player, plantname, index)
  284. local inv = player:get_inventory()
  285. local old_stack = inv:get_stack("main", index)
  286. if old_stack:get_name() ~= "" then
  287. return
  288. end
  289. for i, stack in ipairs(inv:get_list("main")) do
  290. if stack:get_name() == plantname and i ~= index then
  291. inv:set_stack("main", index, stack)
  292. stack:clear()
  293. inv:set_stack("main", i, stack)
  294. return
  295. end
  296. end
  297. end
  298. -- Place Seeds on Soil
  299. function farming.place_seed(itemstack, placer, pointed_thing, plantname)
  300. local pt = pointed_thing
  301. -- check if pointing at a node
  302. if not pt or pt.type ~= "node" then
  303. return
  304. end
  305. local under = minetest.get_node(pt.under)
  306. -- am I right-clicking on something that has a custom on_place set?
  307. -- thanks to Krock for helping with this issue :)
  308. local def = minetest.registered_nodes[under.name]
  309. if placer and itemstack and def and def.on_rightclick then
  310. return def.on_rightclick(pt.under, under, placer, itemstack)
  311. end
  312. local above = minetest.get_node(pt.above)
  313. -- check if pointing at the top of the node
  314. if pt.above.y ~= pt.under.y + 1 then
  315. return
  316. end
  317. -- return if any of the nodes is not registered
  318. if not minetest.registered_nodes[under.name]
  319. or not minetest.registered_nodes[above.name] then
  320. return
  321. end
  322. -- can I replace above node, and am I pointing at soil
  323. if not minetest.registered_nodes[above.name].buildable_to
  324. or minetest.get_item_group(under.name, "soil") < 2
  325. -- avoid multiple seed placement bug
  326. or minetest.get_item_group(above.name, "plant") ~= 0 then
  327. return
  328. end
  329. -- is player planting seed?
  330. local name = placer and placer:get_player_name() or ""
  331. -- if not protected then add node and remove 1 item from the itemstack
  332. if not minetest.is_protected(pt.above, name) then
  333. local p2 = minetest.registered_nodes[plantname].place_param2 or 1
  334. minetest.set_node(pt.above, {name = plantname, param2 = p2})
  335. --minetest.get_node_timer(pt.above):start(1)
  336. --farming.handle_growth(pt.above)--, node)
  337. minetest.sound_play("default_place_node", {pos = pt.above, gain = 1.0})
  338. if placer and itemstack
  339. and not farming.is_creative(placer:get_player_name()) then
  340. local name = itemstack:get_name()
  341. itemstack:take_item()
  342. -- check for refill
  343. if itemstack:get_count() == 0 then
  344. minetest.after(0.10,
  345. farming.refill_plant,
  346. placer,
  347. name,
  348. placer:get_wield_index()
  349. )
  350. end
  351. end
  352. return itemstack
  353. end
  354. end
  355. -- Function to register plants (default farming compatibility)
  356. farming.register_plant = function(name, def)
  357. if not def.steps then
  358. return nil
  359. end
  360. local mname = name:split(":")[1]
  361. local pname = name:split(":")[2]
  362. -- Check def
  363. def.description = def.description or S("Seed")
  364. def.inventory_image = def.inventory_image or "unknown_item.png"
  365. def.minlight = def.minlight or 12
  366. def.maxlight = def.maxlight or 15
  367. -- Register seed
  368. minetest.register_node(":" .. mname .. ":seed_" .. pname, {
  369. description = def.description,
  370. tiles = {def.inventory_image},
  371. inventory_image = def.inventory_image,
  372. wield_image = def.inventory_image,
  373. drawtype = "signlike",
  374. groups = {seed = 1, snappy = 3, attached_node = 1, flammable = 2},
  375. paramtype = "light",
  376. paramtype2 = "wallmounted",
  377. walkable = false,
  378. sunlight_propagates = true,
  379. selection_box = farming.select,
  380. place_param2 = def.place_param2 or nil,
  381. next_plant = mname .. ":" .. pname .. "_1",
  382. on_place = function(itemstack, placer, pointed_thing)
  383. return farming.place_seed(itemstack, placer,
  384. pointed_thing, mname .. ":" .. pname .. "_1")
  385. end,
  386. })
  387. -- Register harvest
  388. minetest.register_craftitem(":" .. mname .. ":" .. pname, {
  389. description = pname:gsub("^%l", string.upper),
  390. inventory_image = mname .. "_" .. pname .. ".png",
  391. groups = def.groups or {flammable = 2},
  392. })
  393. -- Register growing steps
  394. for i = 1, def.steps do
  395. local base_rarity = 1
  396. if def.steps ~= 1 then
  397. base_rarity = 8 - (i - 1) * 7 / (def.steps - 1)
  398. end
  399. local drop = {
  400. items = {
  401. {items = {mname .. ":" .. pname}, rarity = base_rarity},
  402. {items = {mname .. ":" .. pname}, rarity = base_rarity * 2},
  403. {items = {mname .. ":seed_" .. pname}, rarity = base_rarity},
  404. {items = {mname .. ":seed_" .. pname}, rarity = base_rarity * 2},
  405. }
  406. }
  407. local g = {
  408. snappy = 3, flammable = 2, plant = 1, growing = 1,
  409. attached_node = 1, not_in_creative_inventory = 1,
  410. }
  411. -- Last step doesn't need growing=1 so Abm never has to check these
  412. if i == def.steps then
  413. g.growing = 0
  414. end
  415. local node_name = mname .. ":" .. pname .. "_" .. i
  416. local next_plant = nil
  417. if i < def.steps then
  418. next_plant = mname .. ":" .. pname .. "_" .. (i + 1)
  419. end
  420. minetest.register_node(node_name, {
  421. drawtype = "plantlike",
  422. waving = 1,
  423. tiles = {mname .. "_" .. pname .. "_" .. i .. ".png"},
  424. paramtype = "light",
  425. paramtype2 = def.paramtype2,
  426. place_param2 = def.place_param2,
  427. walkable = false,
  428. buildable_to = true,
  429. sunlight_propagates = true,
  430. drop = drop,
  431. selection_box = farming.select,
  432. groups = g,
  433. sounds = default.node_sound_leaves_defaults(),
  434. minlight = def.minlight,
  435. maxlight = def.maxlight,
  436. next_plant = next_plant
  437. })
  438. end
  439. -- add to farming.registered_plants
  440. farming.registered_plants[mname .. ":" .. pname] = {
  441. crop = mname .. ":" .. pname,
  442. seed = mname .. ":seed_" .. pname,
  443. steps = def.steps,
  444. minlight = def.minlight,
  445. maxlight = def.maxlight
  446. }
  447. --print(dump(farming.registered_plants[mname .. ":" .. pname]))
  448. -- Return info
  449. return {seed = mname .. ":seed_" .. pname, harvest = mname .. ":" .. pname}
  450. end
  451. -- default settings
  452. farming.carrot = 0.001
  453. farming.potato = 0.001
  454. farming.tomato = 0.001
  455. farming.cucumber = 0.001
  456. farming.corn = 0.001
  457. farming.coffee = 0.001
  458. farming.melon = 0.001
  459. farming.pumpkin = 0.001
  460. farming.cocoa = true
  461. farming.raspberry = 0.001
  462. farming.blueberry = 0.001
  463. farming.rhubarb = 0.001
  464. farming.beans = 0.001
  465. farming.grapes = 0.001
  466. farming.barley = true
  467. farming.chili = 0.003
  468. farming.hemp = 0.003
  469. farming.garlic = 0.001
  470. farming.onion = 0.001
  471. farming.pepper = 0.002
  472. farming.pineapple = 0.001
  473. farming.peas = 0.001
  474. farming.beetroot = 0.001
  475. farming.mint = 0.005
  476. farming.cabbage = 0.001
  477. farming.blackberry = 0.002
  478. farming.soy = 0.001
  479. farming.vanilla = 0.001
  480. farming.lettuce = 0.001
  481. farming.artichoke = 0.001
  482. farming.parsley = 0.002
  483. farming.grains = true
  484. farming.rarety = 0.002
  485. -- Load new global settings if found inside mod folder
  486. local input = io.open(farming.path.."/farming.conf", "r")
  487. if input then
  488. dofile(farming.path .. "/farming.conf")
  489. input:close()
  490. end
  491. -- load new world-specific settings if found inside world folder
  492. local worldpath = minetest.get_worldpath()
  493. input = io.open(worldpath.."/farming.conf", "r")
  494. if input then
  495. dofile(worldpath .. "/farming.conf")
  496. input:close()
  497. end
  498. -- important items
  499. dofile(farming.path.."/soil.lua")
  500. dofile(farming.path.."/hoes.lua")
  501. dofile(farming.path.."/grass.lua")
  502. dofile(farming.path.."/utensils.lua")
  503. -- default crops
  504. dofile(farming.path.."/crops/wheat.lua")
  505. dofile(farming.path.."/crops/cotton.lua")
  506. -- helper function
  507. local function ddoo(file, check)
  508. if check then
  509. dofile(farming.path .. "/crops/" .. file)
  510. end
  511. end
  512. -- add additional crops and food (if enabled)
  513. ddoo("carrot.lua", farming.carrot)
  514. ddoo("potato.lua", farming.potato)
  515. ddoo("tomato.lua", farming.tomato)
  516. ddoo("cucumber.lua", farming.cucumber)
  517. ddoo("corn.lua", farming.corn)
  518. ddoo("coffee.lua", farming.coffee)
  519. ddoo("melon.lua", farming.melon)
  520. ddoo("pumpkin.lua", farming.pumpkin)
  521. ddoo("cocoa.lua", farming.cocoa)
  522. ddoo("raspberry.lua", farming.raspberry)
  523. ddoo("blueberry.lua", farming.blueberry)
  524. ddoo("rhubarb.lua", farming.rhubarb)
  525. ddoo("beans.lua", farming.beans)
  526. ddoo("grapes.lua", farming.grapes)
  527. ddoo("barley.lua", farming.barley)
  528. ddoo("hemp.lua", farming.hemp)
  529. ddoo("garlic.lua", farming.garlic)
  530. ddoo("onion.lua", farming.onion)
  531. ddoo("pepper.lua", farming.pepper)
  532. ddoo("pineapple.lua", farming.pineapple)
  533. ddoo("peas.lua", farming.peas)
  534. ddoo("beetroot.lua", farming.beetroot)
  535. ddoo("chili.lua", farming.chili)
  536. ddoo("ryeoatrice.lua", farming.grains)
  537. ddoo("mint.lua", farming.mint)
  538. ddoo("cabbage.lua", farming.cabbage)
  539. ddoo("blackberry.lua", farming.blackberry)
  540. ddoo("soy.lua", farming.soy)
  541. ddoo("vanilla.lua", farming.vanilla)
  542. ddoo("lettuce.lua", farming.lettuce)
  543. ddoo("artichoke.lua", farming.artichoke)
  544. ddoo("parsley.lua", farming.parsley)
  545. dofile(farming.path .. "/food.lua")
  546. dofile(farming.path .. "/mapgen.lua")
  547. dofile(farming.path .. "/compatibility.lua") -- Farming Plus compatibility
  548. dofile(farming.path .. "/lucky_block.lua")