hoes.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. local S = farming.translate
  2. local tr = minetest.get_modpath("toolranks")
  3. -- Hoe registration function
  4. farming.register_hoe = function(name, def)
  5. -- Check for : prefix (register new hoes in your mod's namespace)
  6. if name:sub(1,1) ~= ":" then
  7. name = ":" .. name
  8. end
  9. -- Check def table
  10. if def.description == nil then
  11. def.description = S("Hoe")
  12. end
  13. if def.inventory_image == nil then
  14. def.inventory_image = "unknown_item.png"
  15. end
  16. if def.max_uses == nil then
  17. def.max_uses = 30
  18. end
  19. -- add hoe group
  20. def.groups = def.groups or {}
  21. def.groups.hoe = 1
  22. -- Register the tool
  23. minetest.register_tool(name, {
  24. description = def.description,
  25. inventory_image = def.inventory_image,
  26. on_use = function(itemstack, user, pointed_thing)
  27. return farming.hoe_on_use(itemstack, user, pointed_thing, def.max_uses)
  28. end,
  29. groups = def.groups,
  30. sound = {breaks = "default_tool_breaks"}
  31. })
  32. -- Register its recipe
  33. if def.recipe then
  34. minetest.register_craft({
  35. output = name:sub(2),
  36. recipe = def.recipe
  37. })
  38. elseif def.material then
  39. minetest.register_craft({
  40. output = name:sub(2),
  41. recipe = {
  42. {def.material, def.material, ""},
  43. {"", "group:stick", ""},
  44. {"", "group:stick", ""}
  45. }
  46. })
  47. end
  48. end
  49. -- Turns dirt with group soil=1 into soil
  50. function farming.hoe_on_use(itemstack, user, pointed_thing, uses)
  51. local pt = pointed_thing
  52. -- am I going to hoe the top of a dirt node?
  53. if not pt or pt.type ~= "node"
  54. or pt.above.y ~= pt.under.y + 1 then
  55. return
  56. end
  57. local under = minetest.get_node(pt.under)
  58. local upos = pointed_thing.under
  59. if minetest.is_protected(upos, user:get_player_name()) then
  60. minetest.record_protection_violation(upos, user:get_player_name())
  61. return
  62. end
  63. local p = {x = pt.under.x, y = pt.under.y + 1, z = pt.under.z}
  64. local above = minetest.get_node(p)
  65. -- return if any of the nodes is not registered
  66. if not minetest.registered_nodes[under.name]
  67. or not minetest.registered_nodes[above.name] then
  68. return
  69. end
  70. -- check if the node above the pointed thing is air
  71. if above.name ~= "air" then
  72. return
  73. end
  74. -- check if pointing at dirt
  75. if minetest.get_item_group(under.name, "soil") ~= 1 then
  76. return
  77. end
  78. -- check if (wet) soil defined
  79. local ndef = minetest.registered_nodes[under.name]
  80. if ndef.soil == nil or ndef.soil.wet == nil or ndef.soil.dry == nil then
  81. return
  82. end
  83. if minetest.is_protected(pt.under, user:get_player_name()) then
  84. minetest.record_protection_violation(pt.under, user:get_player_name())
  85. return
  86. end
  87. -- turn the node into soil, wear out item and play sound
  88. minetest.set_node(pt.under, {name = ndef.soil.dry})
  89. minetest.sound_play("default_dig_crumbly", {pos = pt.under, gain = 0.5}, true)
  90. local wdef = itemstack:get_definition()
  91. local wear = 65535 / (uses - 1)
  92. if farming.is_creative(user:get_player_name()) then
  93. if tr then
  94. wear = 1
  95. else
  96. wear = 0
  97. end
  98. end
  99. if tr then
  100. itemstack = toolranks.new_afteruse(itemstack, user, under, {wear = wear})
  101. else
  102. itemstack:add_wear(wear)
  103. end
  104. if itemstack:get_count() == 0 and wdef.sound and wdef.sound.breaks then
  105. minetest.sound_play(wdef.sound.breaks, {pos = pt.above, gain = 0.5}, true)
  106. end
  107. return itemstack
  108. end
  109. -- Define Hoes
  110. farming.register_hoe(":farming:hoe_wood", {
  111. description = S("Wooden Hoe"),
  112. inventory_image = "farming_tool_woodhoe.png",
  113. max_uses = 30,
  114. material = "group:wood"
  115. })
  116. minetest.register_craft({
  117. type = "fuel",
  118. recipe = "farming:hoe_wood",
  119. burntime = 5
  120. })
  121. farming.register_hoe(":farming:hoe_stone", {
  122. description = S("Stone Hoe"),
  123. inventory_image = "farming_tool_stonehoe.png",
  124. max_uses = 90,
  125. material = "group:stone"
  126. })
  127. farming.register_hoe(":farming:hoe_steel", {
  128. description = S("Steel Hoe"),
  129. inventory_image = "farming_tool_steelhoe.png",
  130. max_uses = 200,
  131. material = "default:steel_ingot"
  132. })
  133. farming.register_hoe(":farming:hoe_bronze", {
  134. description = S("Bronze Hoe"),
  135. inventory_image = "farming_tool_bronzehoe.png",
  136. max_uses = 500,
  137. groups = {not_in_creative_inventory = 1}
  138. })
  139. farming.register_hoe(":farming:hoe_mese", {
  140. description = S("Mese Hoe"),
  141. inventory_image = "farming_tool_mesehoe.png",
  142. max_uses = 350,
  143. groups = {not_in_creative_inventory = 1},
  144. })
  145. farming.register_hoe(":farming:hoe_diamond", {
  146. description = S("Diamond Hoe"),
  147. inventory_image = "farming_tool_diamondhoe.png",
  148. max_uses = 500,
  149. groups = {not_in_creative_inventory = 1}
  150. })
  151. -- Toolranks support
  152. if tr then
  153. minetest.override_item("farming:hoe_wood", {
  154. original_description = S("Wood Hoe"),
  155. description = toolranks.create_description(S("Wood Hoe"))})
  156. minetest.override_item("farming:hoe_stone", {
  157. original_description = S("Stone Hoe"),
  158. description = toolranks.create_description(S("Stone Hoe"))})
  159. minetest.override_item("farming:hoe_steel", {
  160. original_description = S("Steel Hoe"),
  161. description = toolranks.create_description(S("Steel Hoe"))})
  162. minetest.override_item("farming:hoe_bronze", {
  163. original_description = S("Bronze Hoe"),
  164. description = toolranks.create_description(S("Bronze Hoe"))})
  165. minetest.override_item("farming:hoe_mese", {
  166. original_description = S("Mese Hoe"),
  167. description = toolranks.create_description(S("Mese Hoe"))})
  168. minetest.override_item("farming:hoe_diamond", {
  169. original_description = S("Diamond Hoe"),
  170. description = toolranks.create_description(S("Diamond Hoe"))})
  171. end
  172. -- hoe bomb function
  173. local function hoe_area(pos, player)
  174. -- check for protection
  175. if minetest.is_protected(pos, player:get_player_name()) then
  176. minetest.record_protection_violation(pos, player:get_player_name())
  177. return
  178. end
  179. local r = 5 -- radius
  180. -- remove flora (grass, flowers etc.)
  181. local res = minetest.find_nodes_in_area(
  182. {x = pos.x - r, y = pos.y - 1, z = pos.z - r},
  183. {x = pos.x + r, y = pos.y + 2, z = pos.z + r},
  184. {"group:flora"})
  185. for n = 1, #res do
  186. minetest.swap_node(res[n], {name = "air"})
  187. end
  188. -- replace dirt with tilled soil
  189. res = nil
  190. res = minetest.find_nodes_in_area_under_air(
  191. {x = pos.x - r, y = pos.y - 1, z = pos.z - r},
  192. {x = pos.x + r, y = pos.y + 2, z = pos.z + r},
  193. {"group:soil"})
  194. for n = 1, #res do
  195. minetest.swap_node(res[n], {name = "farming:soil"})
  196. end
  197. end
  198. -- throwable hoe bomb
  199. minetest.register_entity("farming:hoebomb_entity", {
  200. initial_properties = {
  201. physical = true,
  202. visual = "sprite",
  203. visual_size = {x = 1.0, y = 1.0},
  204. textures = {"farming_hoe_bomb.png"},
  205. collisionbox = {-0.1,-0.1,-0.1,0.1,0.1,0.1}
  206. },
  207. lastpos = {},
  208. player = "",
  209. on_step = function(self, dtime)
  210. if not self.player then
  211. self.object:remove()
  212. return
  213. end
  214. local pos = self.object:get_pos()
  215. if self.lastpos.x ~= nil then
  216. local vel = self.object:get_velocity()
  217. -- only when potion hits something physical
  218. if vel.x == 0
  219. or vel.y == 0
  220. or vel.z == 0 then
  221. if self.player ~= "" then
  222. -- round up coords to fix glitching through doors
  223. self.lastpos = vector.round(self.lastpos)
  224. hoe_area(self.lastpos, self.player)
  225. end
  226. self.object:remove()
  227. return
  228. end
  229. end
  230. self.lastpos = pos
  231. end
  232. })
  233. -- actual throwing function
  234. local function throw_potion(itemstack, player)
  235. local playerpos = player:get_pos()
  236. local obj = minetest.add_entity({
  237. x = playerpos.x,
  238. y = playerpos.y + 1.5,
  239. z = playerpos.z
  240. }, "farming:hoebomb_entity")
  241. if not obj then return end
  242. local dir = player:get_look_dir()
  243. local velocity = 20
  244. obj:set_velocity({x = dir.x * velocity, y = dir.y * velocity, z = dir.z * velocity})
  245. obj:set_acceleration({x = dir.x * -3, y = -9.5, z = dir.z * -3})
  246. obj:get_luaentity().player = player
  247. end
  248. -- hoe bomb item
  249. minetest.register_craftitem("farming:hoe_bomb", {
  250. description = S("Hoe Bomb (use or throw on grassy areas to hoe land)"),
  251. inventory_image = "farming_hoe_bomb.png",
  252. groups = {flammable = 2, not_in_creative_inventory = 1},
  253. on_use = function(itemstack, user, pointed_thing)
  254. if pointed_thing.type == "node" then
  255. hoe_area(pointed_thing.above, user)
  256. else
  257. throw_potion(itemstack, user)
  258. if not farming.is_creative(user:get_player_name()) then
  259. itemstack:take_item()
  260. return itemstack
  261. end
  262. end
  263. end,
  264. })
  265. -- Mithril Scythe (special item)
  266. farming.scythe_not_drops = {"farming:trellis", "farming:beanpole"}
  267. farming.add_to_scythe_not_drops = function(item)
  268. table.insert(farming.scythe_not_drops, item)
  269. end
  270. minetest.register_tool("farming:scythe_mithril", {
  271. description = S("Mithril Scythe (Use to harvest and replant crops)"),
  272. inventory_image = "farming_scythe_mithril.png",
  273. sound = {breaks = "default_tool_breaks"},
  274. on_use = function(itemstack, placer, pointed_thing)
  275. if pointed_thing.type ~= "node" then
  276. return
  277. end
  278. local pos = pointed_thing.under
  279. local name = placer:get_player_name()
  280. if minetest.is_protected(pos, name) then
  281. return
  282. end
  283. local node = minetest.get_node_or_nil(pos)
  284. if not node then
  285. return
  286. end
  287. local def = minetest.registered_nodes[node.name]
  288. if not def or not def.drop or not def.groups or not def.groups.plant then
  289. return
  290. end
  291. local drops = minetest.get_node_drops(node.name, "")
  292. if not drops or #drops == 0 or (#drops == 1 and drops[1] == "") then
  293. return
  294. end
  295. -- get crop name
  296. local mname = node.name:split(":")[1]
  297. local pname = node.name:split(":")[2]
  298. local sname = tonumber(pname:split("_")[2])
  299. pname = pname:split("_")[1]
  300. if not sname then
  301. return
  302. end
  303. -- add dropped items
  304. for _, dropped_item in pairs(drops) do
  305. -- dont drop items on this list
  306. for _, not_item in pairs(farming.scythe_not_drops) do
  307. if dropped_item == not_item then
  308. dropped_item = nil
  309. end
  310. end
  311. if dropped_item then
  312. local obj = minetest.add_item(pos, dropped_item)
  313. if obj then
  314. obj:set_velocity({
  315. x = math.random(-10, 10) / 9,
  316. y = 3,
  317. z = math.random(-10, 10) / 9
  318. })
  319. end
  320. end
  321. end
  322. -- Run script hook
  323. for _, callback in pairs(core.registered_on_dignodes) do
  324. callback(pos, node, placer)
  325. end
  326. -- play sound
  327. minetest.sound_play("default_grass_footstep", {pos = pos, gain = 1.0}, true)
  328. local replace = mname .. ":" .. pname .. "_1"
  329. if minetest.registered_nodes[replace] then
  330. local p2 = minetest.registered_nodes[replace].place_param2 or 1
  331. minetest.set_node(pos, {name = replace, param2 = p2})
  332. else
  333. minetest.set_node(pos, {name = "air"})
  334. end
  335. if not farming.is_creative(name) then
  336. itemstack:add_wear(65535 / 350) -- 350 uses
  337. return itemstack
  338. end
  339. end,
  340. })
  341. if minetest.get_modpath("moreores") then
  342. minetest.register_craft({
  343. output = "farming:scythe_mithril",
  344. recipe = {
  345. {"", "moreores:mithril_ingot", "moreores:mithril_ingot"},
  346. {"moreores:mithril_ingot", "", "group:stick"},
  347. {"", "", "group:stick"}
  348. }
  349. })
  350. end