init.lua 16 KB

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