hoes.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. local S = farming.intllib
  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. -- check if pointing at a node
  53. if not pt or pt.type ~= "node" then
  54. return
  55. end
  56. local under = minetest.get_node(pt.under)
  57. local upos = pointed_thing.under
  58. if minetest.is_protected(upos, user:get_player_name()) then
  59. minetest.record_protection_violation(upos, user:get_player_name())
  60. return
  61. end
  62. local p = {x = pt.under.x, y = pt.under.y + 1, z = pt.under.z}
  63. local above = minetest.get_node(p)
  64. -- return if any of the nodes is not registered
  65. if not minetest.registered_nodes[under.name]
  66. or not minetest.registered_nodes[above.name] then
  67. return
  68. end
  69. -- check if the node above the pointed thing is air
  70. if above.name ~= "air" then
  71. return
  72. end
  73. -- check if pointing at dirt
  74. if minetest.get_item_group(under.name, "soil") ~= 1 then
  75. return
  76. end
  77. -- turn the node into soil, wear out item and play sound
  78. minetest.set_node(pt.under, {name = "farming:soil"})
  79. minetest.sound_play("default_dig_crumbly", {pos = pt.under, gain = 0.5})
  80. local wear = 65535 / (uses -1)
  81. if farming.is_creative(user:get_player_name()) then
  82. if tr then
  83. wear = 1
  84. else
  85. wear = 0
  86. end
  87. end
  88. if tr then
  89. itemstack = toolranks.new_afteruse(itemstack, user, under, {wear = wear})
  90. else
  91. itemstack:add_wear(wear)
  92. end
  93. return itemstack
  94. end
  95. -- Define Hoes
  96. farming.register_hoe(":farming:hoe_wood", {
  97. description = S("Wooden Hoe"),
  98. inventory_image = "farming_tool_woodhoe.png",
  99. max_uses = 30,
  100. material = "group:wood"
  101. })
  102. minetest.register_craft({
  103. type = "fuel",
  104. recipe = "farming:hoe_wood",
  105. burntime = 5,
  106. })
  107. farming.register_hoe(":farming:hoe_stone", {
  108. description = S("Stone Hoe"),
  109. inventory_image = "farming_tool_stonehoe.png",
  110. max_uses = 90,
  111. material = "group:stone"
  112. })
  113. farming.register_hoe(":farming:hoe_steel", {
  114. description = S("Steel Hoe"),
  115. inventory_image = "farming_tool_steelhoe.png",
  116. max_uses = 200,
  117. material = "default:steel_ingot"
  118. })
  119. farming.register_hoe(":farming:hoe_bronze", {
  120. description = S("Bronze Hoe"),
  121. inventory_image = "farming_tool_bronzehoe.png",
  122. max_uses = 500,
  123. groups = {not_in_creative_inventory = 1},
  124. })
  125. farming.register_hoe(":farming:hoe_mese", {
  126. description = S("Mese Hoe"),
  127. inventory_image = "farming_tool_mesehoe.png",
  128. max_uses = 350,
  129. groups = {not_in_creative_inventory = 1},
  130. })
  131. farming.register_hoe(":farming:hoe_diamond", {
  132. description = S("Diamond Hoe"),
  133. inventory_image = "farming_tool_diamondhoe.png",
  134. max_uses = 500,
  135. groups = {not_in_creative_inventory = 1},
  136. })
  137. -- Toolranks support
  138. if tr then
  139. minetest.override_item("farming:hoe_wood", {
  140. original_description = "Wood Hoe",
  141. description = toolranks.create_description("Wood Hoe")})
  142. minetest.override_item("farming:hoe_stone", {
  143. original_description = "Stone Hoe",
  144. description = toolranks.create_description("Stone Hoe")})
  145. minetest.override_item("farming:hoe_steel", {
  146. original_description = "Steel Hoe",
  147. description = toolranks.create_description("Steel Hoe")})
  148. minetest.override_item("farming:hoe_bronze", {
  149. original_description = "Bronze Hoe",
  150. description = toolranks.create_description("Bronze Hoe")})
  151. minetest.override_item("farming:hoe_mese", {
  152. original_description = "Mese Hoe",
  153. description = toolranks.create_description("Mese Hoe")})
  154. minetest.override_item("farming:hoe_diamond", {
  155. original_description = "Diamond Hoe",
  156. description = toolranks.create_description("Diamond Hoe")})
  157. end
  158. -- hoe bomb function
  159. local function hoe_area(pos, player)
  160. -- check for protection
  161. if minetest.is_protected(pos, player:get_player_name()) then
  162. minetest.record_protection_violation(pos, player:get_player_name())
  163. return
  164. end
  165. local r = 5 -- radius
  166. -- remove flora (grass, flowers etc.)
  167. local res = minetest.find_nodes_in_area(
  168. {x = pos.x - r, y = pos.y - 1, z = pos.z - r},
  169. {x = pos.x + r, y = pos.y + 2, z = pos.z + r},
  170. {"group:flora"})
  171. for n = 1, #res do
  172. minetest.swap_node(res[n], {name = "air"})
  173. end
  174. -- replace dirt with tilled soil
  175. res = nil
  176. res = minetest.find_nodes_in_area_under_air(
  177. {x = pos.x - r, y = pos.y - 1, z = pos.z - r},
  178. {x = pos.x + r, y = pos.y + 2, z = pos.z + r},
  179. {"group:soil"})
  180. for n = 1, #res do
  181. minetest.swap_node(res[n], {name = "farming:soil"})
  182. end
  183. end
  184. -- throwable hoe bomb
  185. minetest.register_entity("farming:hoebomb_entity", {
  186. physical = true,
  187. visual = "sprite",
  188. visual_size = {x = 1.0, y = 1.0},
  189. textures = {"farming_hoe_bomb.png"},
  190. collisionbox = {0,0,0,0,0,0},
  191. lastpos = {},
  192. player = "",
  193. on_step = function(self, dtime)
  194. if not self.player then
  195. self.object:remove()
  196. return
  197. end
  198. local pos = self.object:get_pos()
  199. if self.lastpos.x ~= nil then
  200. local vel = self.object:getvelocity()
  201. -- only when potion hits something physical
  202. if vel.x == 0
  203. or vel.y == 0
  204. or vel.z == 0 then
  205. if self.player ~= "" then
  206. -- round up coords to fix glitching through doors
  207. self.lastpos = vector.round(self.lastpos)
  208. hoe_area(self.lastpos, self.player)
  209. end
  210. self.object:remove()
  211. return
  212. end
  213. end
  214. self.lastpos = pos
  215. end
  216. })
  217. -- actual throwing function
  218. local function throw_potion(itemstack, player)
  219. local playerpos = player:get_pos()
  220. local obj = minetest.add_entity({
  221. x = playerpos.x,
  222. y = playerpos.y + 1.5,
  223. z = playerpos.z
  224. }, "farming:hoebomb_entity")
  225. local dir = player:get_look_dir()
  226. local velocity = 20
  227. obj:setvelocity({
  228. x = dir.x * velocity,
  229. y = dir.y * velocity,
  230. z = dir.z * velocity
  231. })
  232. obj:setacceleration({
  233. x = dir.x * -3,
  234. y = -9.5,
  235. z = dir.z * -3
  236. })
  237. obj:get_luaentity().player = player
  238. end
  239. -- hoe bomb item
  240. minetest.register_craftitem("farming:hoe_bomb", {
  241. description = S("Hoe Bomb (use or throw on grassy areas to hoe land"),
  242. inventory_image = "farming_hoe_bomb.png",
  243. groups = {flammable = 2, not_in_creative_inventory = 1},
  244. on_use = function(itemstack, user, pointed_thing)
  245. if pointed_thing.type == "node" then
  246. hoe_area(pointed_thing.above, user)
  247. else
  248. throw_potion(itemstack, user)
  249. if not farming.is_creative(user:get_player_name()) then
  250. itemstack:take_item()
  251. return itemstack
  252. end
  253. end
  254. end,
  255. })