api.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. })
  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})
  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. minetest.register_craft({
  141. output = name:sub(2),
  142. recipe = {
  143. {def.material, def.material, ""},
  144. {"", "group:stick", ""},
  145. {"", "group:stick", ""}
  146. }
  147. })
  148. -- Reverse Recipe
  149. minetest.register_craft({
  150. output = name:sub(2),
  151. recipe = {
  152. {"", def.material, def.material},
  153. {"", "group:stick", ""},
  154. {"", "group:stick", ""}
  155. }
  156. })
  157. end
  158. end
  159. local function tick_multiplier(pos)
  160. local minp = vector.subtract(pos, 2)
  161. local maxp = vector.add(pos, 2)
  162. local mult = 1
  163. local cold = minetest.find_nodes_in_area(minp, maxp, "group:cold")
  164. mult = mult + (#cold / 2)
  165. minp = vector.subtract(pos, 3)
  166. maxp = vector.add(pos, 3)
  167. local minerals = minetest.find_nodes_in_area(minp, maxp, "glowstone:minerals")
  168. mult = mult - (#minerals / 4)
  169. -- Multiplier cannot be less than 0.3.
  170. if mult < 0.2 then mult = 0.2 end
  171. return mult
  172. end
  173. -- how often node timers for plants will tick, +/- some random value
  174. local function tick(pos)
  175. local mult = tick_multiplier(pos)
  176. local min = 200 * mult
  177. local max = 350 * mult
  178. minetest.get_node_timer(pos):start(math_random(min, max))
  179. --minetest.get_node_timer(pos):start(1.0) -- Debug
  180. end
  181. -- how often a growth failure tick is retried (e.g. too dark)
  182. local function tick_again(pos)
  183. local min = 40
  184. local max = 80
  185. minetest.get_node_timer(pos):start(math_random(min, max))
  186. --minetest.get_node_timer(pos):start(1.0) -- Debug
  187. end
  188. -- Seed placement
  189. farming.place_seed = function(itemstack, placer, pointed_thing, plantname)
  190. local pt = pointed_thing
  191. -- check if pointing at a node
  192. if not pt then
  193. return itemstack
  194. end
  195. if pt.type ~= "node" then
  196. return itemstack
  197. end
  198. local under = minetest.get_node(pt.under)
  199. -- Pass through interactions to nodes that define them (like chests).
  200. do
  201. local pdef = minetest.reg_ns_nodes[under.name]
  202. if pdef and pdef.on_rightclick and not placer:get_player_control().sneak then
  203. return pdef.on_rightclick(pt.under, under, placer, itemstack, pt)
  204. end
  205. end
  206. local above = minetest.get_node(pt.above)
  207. -- Permit player to place seed on protected soil (by commenting this code).
  208. -- This allows players to build public farms.
  209. --if minetest.is_protected(pt.under, placer:get_player_name()) then
  210. -- minetest.record_protection_violation(pt.under, placer:get_player_name())
  211. -- return
  212. --end
  213. if minetest.is_protected(pt.above, placer:get_player_name()) then
  214. minetest.record_protection_violation(pt.above, placer:get_player_name())
  215. return
  216. end
  217. -- return if any of the nodes is not registered
  218. if not minetest.reg_ns_nodes[under.name] then
  219. return itemstack
  220. end
  221. if not minetest.reg_ns_nodes[above.name] then
  222. return itemstack
  223. end
  224. -- check if pointing at the top of the node
  225. if pt.above.y ~= pt.under.y+1 then
  226. return itemstack
  227. end
  228. -- check if you can replace the node above the pointed node
  229. local ndef = minetest.reg_ns_nodes[above.name]
  230. if not ndef or not ndef.buildable_to then
  231. return itemstack
  232. end
  233. -- check if pointing at soil
  234. if minetest.get_item_group(under.name, "soil") < 2 then
  235. return itemstack
  236. end
  237. -- add the node and remove 1 item from the itemstack
  238. -- note: use of `add_node` automatically invokes droplift + dirtspread notifications.
  239. minetest.add_node(pt.above, {name = plantname, param2 = 1})
  240. tick(pt.above)
  241. itemstack:take_item()
  242. return itemstack
  243. end
  244. -- This should only ever be called from the `on_timer' callback of a node.
  245. farming.grow_plant = function(pos, elapsed)
  246. local node = minetest.get_node(pos)
  247. local name = node.name
  248. local def = minetest.reg_ns_nodes[name]
  249. if not def.next_plant then
  250. -- disable timer for fully grown plant
  251. return
  252. end
  253. -- grow seed
  254. if minetest.get_item_group(node.name, "seed") and def.fertility then
  255. local soil_node = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
  256. if not soil_node then
  257. tick_again(pos)
  258. return
  259. end
  260. -- omitted is a check for light, we assume seeds can germinate in the dark.
  261. for _, v in pairs(def.fertility) do
  262. if minetest.get_item_group(soil_node.name, v) ~= 0 then
  263. local placenode = {name = def.next_plant}
  264. if def.place_param2 then
  265. placenode.param2 = def.place_param2
  266. end
  267. minetest.swap_node(pos, placenode)
  268. if minetest.reg_ns_nodes[def.next_plant].next_plant then
  269. tick(pos)
  270. return
  271. end
  272. end
  273. end
  274. return
  275. end
  276. -- check if on wet soil
  277. local below = minetest.get_node({x = pos.x, y = pos.y - 1, z = pos.z})
  278. if minetest.get_item_group(below.name, "soil") < 3 then
  279. tick_again(pos)
  280. return
  281. end
  282. -- check light
  283. local light = minetest.get_node_light(pos)
  284. if not light or light < def.minlight or light > def.maxlight then
  285. tick_again(pos)
  286. return
  287. end
  288. -- grow
  289. local placenode = {name = def.next_plant}
  290. if def.place_param2 then
  291. placenode.param2 = def.place_param2
  292. end
  293. minetest.swap_node(pos, placenode)
  294. -- new timer needed?
  295. if minetest.reg_ns_nodes[def.next_plant].next_plant then
  296. tick(pos)
  297. end
  298. return
  299. end
  300. -- Register plants
  301. farming.register_plant = function(name, def)
  302. local mname = name:split(":")[1]
  303. local pname = name:split(":")[2]
  304. -- Check def table
  305. if not def.description then
  306. def.description = "Seed"
  307. end
  308. if not def.inventory_image then
  309. def.inventory_image = "unknown_item.png"
  310. end
  311. if not def.steps then
  312. return nil
  313. end
  314. if not def.minlight then
  315. def.minlight = 1
  316. end
  317. if not def.maxlight then
  318. def.maxlight = 14
  319. end
  320. if not def.fertility then
  321. def.fertility = {}
  322. end
  323. -- Register seed
  324. local g = {level = 1, seed = 1, snappy = 3, attached_node = 1, flammable = 2, notify_destruct = 1}
  325. for k, v in pairs(def.fertility) do
  326. g[v] = 1
  327. end
  328. minetest.register_node(":" .. mname .. ":seed_" .. pname, {
  329. description = def.description,
  330. tiles = {def.inventory_image},
  331. inventory_image = def.inventory_image,
  332. wield_image = def.inventory_image,
  333. drawtype = "signlike",
  334. groups = g,
  335. paramtype = "light",
  336. paramtype2 = "wallmounted",
  337. place_param2 = def.place_param2 or nil, -- this isn't actually used for placement
  338. walkable = false,
  339. sunlight_propagates = true,
  340. selection_box = {
  341. type = "fixed",
  342. fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
  343. },
  344. fertility = def.fertility,
  345. sounds = default.node_sound_dirt_defaults({
  346. dug = {name = "default_grass_footstep", gain = 0.2},
  347. place = {name = "default_place_node", gain = 0.25},
  348. }),
  349. on_place = function(itemstack, placer, pointed_thing)
  350. local under = pointed_thing.under
  351. local node = minetest.get_node(under)
  352. local udef = minetest.reg_ns_nodes[node.name]
  353. if udef and udef.on_rightclick and
  354. not (placer and placer:get_player_control().sneak) then
  355. return udef.on_rightclick(under, node, placer, itemstack,
  356. pointed_thing) or itemstack
  357. end
  358. return farming.place_seed(itemstack, placer, pointed_thing, mname .. ":seed_" .. pname)
  359. end,
  360. next_plant = mname .. ":" .. pname .. "_1",
  361. on_timer = farming.grow_plant,
  362. minlight = def.minlight,
  363. maxlight = def.maxlight,
  364. })
  365. -- Register harvest
  366. minetest.register_craftitem(":" .. mname .. ":" .. pname, {
  367. description = pname:gsub("^%l", string.upper),
  368. inventory_image = mname .. "_" .. pname .. ".png",
  369. groups = {flammable = 2},
  370. -- Pass through flowerpot data if available.
  371. flowerpot_insert = def.flowerpot_insert,
  372. })
  373. -- Register growing steps
  374. for i = 1, def.steps do
  375. local drop = {
  376. items = {
  377. {items = {mname .. ":" .. pname}, rarity = 9 - i},
  378. {items = {mname .. ":" .. pname}, rarity= 18 - i * 2},
  379. {items = {mname .. ":seed_" .. pname}, rarity = 9 - i},
  380. {items = {mname .. ":seed_" .. pname}, rarity = 18 - i * 2},
  381. }
  382. }
  383. local nodegroups = utility.dig_groups("crop", {flammable = 2, plant = 1, not_in_creative_inventory = 1, attached_node = 1, notify_destruct = 1})
  384. nodegroups[pname] = i
  385. local next_plant = nil
  386. if i < def.steps then
  387. next_plant = mname .. ":" .. pname .. "_" .. (i + 1)
  388. end
  389. minetest.register_node(mname .. ":" .. pname .. "_" .. i, {
  390. drawtype = "plantlike",
  391. waving = 1,
  392. tiles = {mname .. "_" .. pname .. "_" .. i .. ".png"},
  393. paramtype = "light",
  394. paramtype2 = def.paramtype2 or nil,
  395. place_param2 = def.place_param2 or nil,
  396. walkable = false,
  397. buildable_to = true,
  398. drop = drop,
  399. selection_box = {
  400. type = "fixed",
  401. fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
  402. },
  403. groups = nodegroups,
  404. sounds = default.node_sound_leaves_defaults(),
  405. next_plant = next_plant,
  406. on_timer = farming.grow_plant,
  407. minlight = def.minlight,
  408. maxlight = def.maxlight,
  409. movement_speed_multiplier = default.SLOW_SPEED_PLANTS,
  410. -- Pass through flowerpot data if available.
  411. flowerpot_drop = def.flowerpot_drop,
  412. })
  413. end
  414. -- Return
  415. local r = {
  416. seed = mname .. ":seed_" .. pname,
  417. harvest = mname .. ":" .. pname
  418. }
  419. return r
  420. end