api.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. -- Localize for performance.
  2. local math_random = math.random
  3. function farming.notify_soil(pos)
  4. local minp = vector.add(pos, -4)
  5. local maxp = vector.add(pos, 4)
  6. local soils = minetest.find_nodes_in_area(minp, maxp, "group:field")
  7. if soils and #soils > 0 then
  8. for i=1, #soils do
  9. local timer = minetest.get_node_timer(soils[i])
  10. if timer and not timer:is_started() then
  11. timer:start(math_random(1, 60))
  12. end
  13. end
  14. end
  15. end
  16. function farming.notify_soil_single(pos)
  17. local timer = minetest.get_node_timer(pos)
  18. if timer and not timer:is_started() then
  19. timer:start(math_random(1, 60))
  20. end
  21. end
  22. -- Wear out hoes, place soil
  23. -- TODO Ignore group:flower
  24. farming.hoe_on_use = function(itemstack, user, pointed_thing, uses)
  25. local pt = pointed_thing
  26. -- check if pointing at a node
  27. if not pt then
  28. return
  29. end
  30. if pt.type ~= "node" then
  31. return
  32. end
  33. local under = minetest.get_node(pt.under)
  34. -- Let hoes be used to get resources back from planted mese crystals.
  35. -- Note that harvesting a crystal completely yeilds more fragments,
  36. -- but there is a risk that the you won't be able to restore the plant when you're done.
  37. if string.find(under.name, "^mese_crystals:mese_crystal_ore%d") then
  38. user:get_inventory():add_item("main", "default:mese_crystal_fragment 3")
  39. ambiance.sound_play("default_break_glass", pt.under, 0.3, 10)
  40. minetest.remove_node(pt.under)
  41. -- 1/2 chance to get bluerack back; this is because 1 bluerack makes 2 seeds.
  42. -- This way, we don't make it possible to magically duplicate resources.
  43. if math_random(1, 2) == 1 then
  44. local p = {x=pt.under.x, y=pt.under.y-1, z=pt.under.z}
  45. if minetest.get_node(p).name == "default:obsidian" then
  46. minetest.add_node(p, {name="rackstone:bluerack"})
  47. ambiance.sound_play("default_dig_cracky", pt.under, 1.0, 10)
  48. end
  49. end
  50. return
  51. end
  52. local p = {x=pt.under.x, y=pt.under.y+1, z=pt.under.z}
  53. local above = minetest.get_node(p)
  54. -- return if any of the nodes is not registered
  55. if not minetest.reg_ns_nodes[under.name] then
  56. return
  57. end
  58. if not minetest.reg_ns_nodes[above.name] then
  59. return
  60. end
  61. -- check if the node above the pointed thing is air
  62. if above.name ~= "air" then
  63. return
  64. end
  65. -- check if pointing at soil
  66. if minetest.get_item_group(under.name, "soil") ~= 1 then
  67. return
  68. end
  69. -- check if (wet) soil defined
  70. local ndef = minetest.reg_ns_nodes[under.name]
  71. if ndef.soil == nil or ndef.soil.wet == nil or ndef.soil.dry == nil then
  72. return
  73. end
  74. if minetest.is_protected(pt.under, user:get_player_name()) then
  75. minetest.record_protection_violation(pt.under, user:get_player_name())
  76. return
  77. end
  78. if minetest.is_protected(pt.above, user:get_player_name()) then
  79. minetest.record_protection_violation(pt.above, user:get_player_name())
  80. return
  81. end
  82. -- turn the node into soil and play sound
  83. minetest.add_node(pt.under, {name = ndef.soil.dry})
  84. minetest.sound_play("default_dig_crumbly", {
  85. pos = pt.under,
  86. gain = 0.5,
  87. }, true)
  88. farming.notify_soil_single(pt.under)
  89. if not minetest.setting_getbool("creative_mode") then
  90. -- wear tool
  91. local wdef = itemstack:get_definition()
  92. itemstack:add_wear(65535/(uses-1))
  93. -- tool break sound
  94. if itemstack:get_count() == 0 and wdef.sound and wdef.sound.breaks then
  95. minetest.sound_play(wdef.sound.breaks, {pos = pt.above, gain = 0.5}, true)
  96. end
  97. end
  98. return itemstack
  99. end
  100. -- Register new hoes
  101. farming.register_hoe = function(name, def)
  102. -- Check for : prefix (register new hoes in your mod's namespace)
  103. if name:sub(1,1) ~= ":" then
  104. name = ":" .. name
  105. end
  106. -- Check def table
  107. if def.description == nil then
  108. def.description = "Hoe"
  109. end
  110. if def.inventory_image == nil then
  111. def.inventory_image = "unknown_item.png"
  112. end
  113. if def.recipe == nil then
  114. def.recipe = {
  115. {"air","air",""},
  116. {"","group:stick",""},
  117. {"","group:stick",""}
  118. }
  119. end
  120. if def.max_uses == nil then
  121. def.max_uses = 30
  122. end
  123. -- Register the tool
  124. minetest.register_tool(name, {
  125. description = def.description,
  126. inventory_image = def.inventory_image,
  127. on_use = function(itemstack, user, pointed_thing)
  128. return farming.hoe_on_use(itemstack, user, pointed_thing, def.max_uses)
  129. end,
  130. groups = def.groups,
  131. sound = {breaks = "default_tool_breaks"},
  132. })
  133. -- Register its recipe
  134. if def.material == nil then
  135. minetest.register_craft({
  136. output = name:sub(2),
  137. recipe = def.recipe
  138. })
  139. else
  140. local handle = "group:stick"
  141. if def.handle then
  142. handle = def.handle
  143. end
  144. minetest.register_craft({
  145. output = name:sub(2),
  146. recipe = {
  147. {def.material, def.material, ""},
  148. {"", handle, ""},
  149. {"", handle, ""}
  150. }
  151. })
  152. -- Reverse Recipe
  153. minetest.register_craft({
  154. output = name:sub(2),
  155. recipe = {
  156. {"", def.material, def.material},
  157. {"", handle, ""},
  158. {"", handle, ""}
  159. }
  160. })
  161. end
  162. end
  163. local function tick_multiplier(pos, def)
  164. local minp = vector.subtract(pos, 2)
  165. local maxp = vector.add(pos, 2)
  166. local mult = 1
  167. local cold = minetest.find_nodes_in_area(minp, maxp, "group:cold")
  168. mult = mult + (#cold / 2)
  169. -- Plant can disable minerals, if they should not grow any faster when
  170. -- minerals are present.
  171. if not def.farming_minerals_unused then
  172. minp = vector.subtract(pos, 3)
  173. maxp = vector.add(pos, 3)
  174. local minerals = minetest.find_nodes_in_area(minp, maxp, "glowstone:minerals")
  175. mult = mult - (#minerals / 4)
  176. end
  177. -- Multiplier cannot be less than 0.3.
  178. if mult < 0.2 then mult = 0.2 end
  179. return mult
  180. end
  181. -- how often node timers for plants will tick, +/- some random value
  182. local function tick(pos, def)
  183. local mult = tick_multiplier(pos, def)
  184. local min = (def.farming_growing_time_min or 200) * mult
  185. local max = (def.farming_growing_time_max or 350) * mult
  186. minetest.get_node_timer(pos):start(math_random(min, max))
  187. --minetest.get_node_timer(pos):start(1.0) -- Debug
  188. end
  189. -- how often a growth failure tick is retried (e.g. too dark)
  190. local function tick_again(pos, def)
  191. local min = 50
  192. local max = 100
  193. minetest.get_node_timer(pos):start(math_random(min, max))
  194. --minetest.get_node_timer(pos):start(1.0) -- Debug
  195. end
  196. -- Seed placement
  197. farming.place_seed = function(itemstack, placer, pointed_thing, plantname)
  198. local pt = pointed_thing
  199. -- check if pointing at a node
  200. if not pt then
  201. return itemstack
  202. end
  203. if pt.type ~= "node" then
  204. return itemstack
  205. end
  206. local under = minetest.get_node(pt.under)
  207. -- Pass through interactions to nodes that define them (like chests).
  208. do
  209. local ndef = minetest.reg_ns_nodes[under.name]
  210. if ndef and ndef.on_rightclick and not placer:get_player_control().sneak then
  211. return ndef.on_rightclick(pt.under, under, placer, itemstack, pt)
  212. end
  213. end
  214. local above = minetest.get_node(pt.above)
  215. -- Permit player to place seed on protected soil (by commenting this code).
  216. -- This allows players to build public farms.
  217. --if minetest.is_protected(pt.under, placer:get_player_name()) then
  218. -- minetest.record_protection_violation(pt.under, placer:get_player_name())
  219. -- return
  220. --end
  221. if minetest.is_protected(pt.above, placer:get_player_name()) then
  222. minetest.record_protection_violation(pt.above, placer:get_player_name())
  223. return
  224. end
  225. -- return if any of the nodes is not registered
  226. if not minetest.reg_ns_nodes[under.name] then
  227. return itemstack
  228. end
  229. if not minetest.reg_ns_nodes[above.name] then
  230. return itemstack
  231. end
  232. -- check if pointing at the top of the node
  233. if pt.above.y ~= pt.under.y+1 then
  234. return itemstack
  235. end
  236. -- check if you can replace the node above the pointed node
  237. local ndef = minetest.reg_ns_nodes[above.name]
  238. if not ndef or not ndef.buildable_to then
  239. return itemstack
  240. end
  241. local pdef = minetest.reg_ns_nodes[plantname]
  242. if not pdef then
  243. return itemstack
  244. end
  245. local have_surface = false
  246. if pdef.soil_nodes then
  247. for k, v in ipairs(pdef.soil_nodes) do
  248. if v == under.name then
  249. have_surface = true
  250. break
  251. end
  252. end
  253. end
  254. -- check if pointing at soil
  255. if minetest.get_item_group(under.name, "soil") < 2 and not have_surface then
  256. return itemstack
  257. end
  258. -- add the node and remove 1 item from the itemstack
  259. -- note: use of `add_node` automatically invokes droplift + dirtspread notifications.
  260. minetest.add_node(pt.above, {name = plantname, param2 = 1})
  261. tick(pt.above, pdef)
  262. itemstack:take_item()
  263. return itemstack
  264. end
  265. -- This should only ever be called from the `on_timer' callback of a node.
  266. farming.grow_plant = function(pos, elapsed)
  267. local node = minetest.get_node(pos)
  268. local name = node.name
  269. local def = minetest.reg_ns_nodes[name]
  270. local soil_node = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
  271. if not soil_node then
  272. tick_again(pos, def)
  273. --minetest.chat_send_all('fail 1')
  274. return
  275. end
  276. if not def.next_plant then
  277. -- disable timer for fully grown plant
  278. --minetest.chat_send_all('fail 2')
  279. return
  280. end
  281. -- Allow to randomly choose the next plant from a variety.
  282. local next_plant = def.next_plant
  283. if type(next_plant) == "table" then
  284. next_plant = next_plant[math.random(1, #next_plant)]
  285. end
  286. local have_soil = false
  287. if def.soil_nodes then
  288. for k, v in ipairs(def.soil_nodes) do
  289. if v == soil_node.name then
  290. have_soil = true
  291. break
  292. end
  293. end
  294. end
  295. -- grow seed
  296. if not have_soil then
  297. if minetest.get_item_group(node.name, "seed") ~= 0 and def.fertility then
  298. -- omitted is a check for light, we assume seeds can germinate in the dark.
  299. for _, v in pairs(def.fertility) do
  300. if minetest.get_item_group(soil_node.name, v) ~= 0 then
  301. local placenode = {name = next_plant}
  302. if def.place_param2 then
  303. placenode.param2 = def.place_param2
  304. end
  305. minetest.swap_node(pos, placenode)
  306. if minetest.reg_ns_nodes[next_plant].next_plant then
  307. tick(pos, def)
  308. --minetest.chat_send_all('fail 4')
  309. return
  310. end
  311. end
  312. end
  313. --minetest.chat_send_all('fail 8')
  314. return
  315. end
  316. end
  317. -- check if on wet soil
  318. if not have_soil then
  319. if minetest.get_item_group(soil_node.name, "soil") < 3 then
  320. tick_again(pos, def)
  321. --minetest.chat_send_all('fail 5')
  322. return
  323. end
  324. end
  325. -- check light
  326. local light = minetest.get_node_light(pos)
  327. if not light or light < def.minlight or light > def.maxlight then
  328. tick_again(pos, def)
  329. --minetest.chat_send_all('fail 6')
  330. return
  331. end
  332. local npdef = minetest.reg_ns_nodes[next_plant]
  333. -- grow
  334. local placenode = {name = next_plant}
  335. if npdef.place_param2 then
  336. placenode.param2 = npdef.place_param2
  337. elseif npdef.paramtype2 == "degrotate" then
  338. placenode.param2 = math_random(0, 239)
  339. end
  340. minetest.swap_node(pos, placenode)
  341. -- new timer needed?
  342. if npdef.next_plant then
  343. tick(pos, npdef)
  344. elseif npdef.farming_restart_timer then
  345. -- Allow the last plant in a growing
  346. -- sequence to request a timer restart.
  347. tick(pos, npdef)
  348. end
  349. --minetest.chat_send_all('fail 7')
  350. return
  351. end
  352. -- Register plants
  353. farming.register_plant = function(name, def)
  354. local mname = name:split(":")[1]
  355. local pname = name:split(":")[2]
  356. -- Check def table
  357. if not def.description then
  358. def.description = "Seed"
  359. end
  360. if not def.inventory_image then
  361. def.inventory_image = "unknown_item.png"
  362. end
  363. if not def.steps then
  364. return nil
  365. end
  366. if not def.minlight then
  367. def.minlight = 1
  368. end
  369. if not def.maxlight then
  370. def.maxlight = 14
  371. end
  372. if not def.fertility then
  373. def.fertility = {}
  374. end
  375. -- Register seed
  376. local g = {level = 1, seed = 1, seed_oil = 1, snappy = 3, attached_node = 1, flammable = 2, notify_destruct = 1}
  377. for k, v in pairs(def.fertility) do
  378. g[v] = 1
  379. end
  380. minetest.register_node(":" .. mname .. ":seed_" .. pname, {
  381. description = def.description,
  382. tiles = {def.inventory_image},
  383. inventory_image = def.inventory_image,
  384. wield_image = def.inventory_image,
  385. drawtype = "signlike",
  386. groups = g,
  387. paramtype = "light",
  388. paramtype2 = "wallmounted",
  389. place_param2 = def.place_param2 or nil, -- this isn't actually used for placement
  390. walkable = false,
  391. sunlight_propagates = true,
  392. selection_box = {
  393. type = "fixed",
  394. fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
  395. },
  396. fertility = def.fertility,
  397. sounds = default.node_sound_dirt_defaults({
  398. dug = {name = "default_grass_footstep", gain = 0.2},
  399. place = {name = "default_place_node", gain = 0.25},
  400. }),
  401. on_place = function(itemstack, placer, pointed_thing)
  402. local under = pointed_thing.under
  403. local node = minetest.get_node(under)
  404. local udef = minetest.reg_ns_nodes[node.name]
  405. if udef and udef.on_rightclick and
  406. not (placer and placer:get_player_control().sneak) then
  407. return udef.on_rightclick(under, node, placer, itemstack,
  408. pointed_thing) or itemstack
  409. end
  410. return farming.place_seed(itemstack, placer, pointed_thing, mname .. ":seed_" .. pname)
  411. end,
  412. next_plant = mname .. ":" .. pname .. "_1",
  413. on_timer = farming.grow_plant,
  414. minlight = def.minlight,
  415. maxlight = def.maxlight,
  416. })
  417. -- Register harvest
  418. minetest.register_craftitem(":" .. mname .. ":" .. pname, {
  419. description = pname:gsub("^%l", string.upper),
  420. inventory_image = mname .. "_" .. pname .. ".png",
  421. groups = {flammable = 2},
  422. -- Pass through flowerpot data if available.
  423. flowerpot_insert = def.flowerpot_insert,
  424. })
  425. -- Register growing steps
  426. for i = 1, def.steps do
  427. local drop = {
  428. items = {
  429. {items = {mname .. ":" .. pname}, rarity = 9 - i},
  430. {items = {mname .. ":" .. pname}, rarity= 18 - i * 2},
  431. {items = {mname .. ":seed_" .. pname}, rarity = 9 - i},
  432. {items = {mname .. ":seed_" .. pname}, rarity = 18 - i * 2},
  433. }
  434. }
  435. local nodegroups = utility.dig_groups("crop", {flammable = 2, plant = 1, not_in_creative_inventory = 1, attached_node = 1, notify_destruct = 1})
  436. nodegroups[pname] = i
  437. local next_plant = nil
  438. if i < def.steps then
  439. next_plant = mname .. ":" .. pname .. "_" .. (i + 1)
  440. end
  441. minetest.register_node(mname .. ":" .. pname .. "_" .. i, {
  442. drawtype = "plantlike",
  443. waving = 1,
  444. tiles = {mname .. "_" .. pname .. "_" .. i .. ".png"},
  445. paramtype = "light",
  446. paramtype2 = def.paramtype2 or nil,
  447. place_param2 = def.place_param2 or nil,
  448. walkable = false,
  449. buildable_to = true,
  450. drop = drop,
  451. selection_box = {
  452. type = "fixed",
  453. fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
  454. },
  455. groups = nodegroups,
  456. sounds = default.node_sound_leaves_defaults(),
  457. next_plant = next_plant,
  458. on_timer = farming.grow_plant,
  459. minlight = def.minlight,
  460. maxlight = def.maxlight,
  461. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  462. -- Pass through flowerpot data if available.
  463. flowerpot_drop = def.flowerpot_drop,
  464. })
  465. end
  466. -- Return
  467. local r = {
  468. seed = mname .. ":seed_" .. pname,
  469. harvest = mname .. ":" .. pname
  470. }
  471. return r
  472. end