axe.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. local creative = minetest.setting_getbool("creative_mode")
  2. local lottthrowing_register_axe = function(axe, desc, damage, craft1, craft2)
  3. local axe_entity = "lottthrowing:" .. axe .. "_entity"
  4. local lottthrowing_throw_axe = function(itemstack, player)
  5. if not minetest.setting_getbool("creative_mode") then
  6. player:get_inventory():remove_item("main", player:get_wield_index())
  7. end
  8. local playerpos = player:getpos()
  9. local obj = minetest.add_entity({x=playerpos.x,y=playerpos.y+1.5,z=playerpos.z}, axe_entity)
  10. obj:get_luaentity().player = player or nil
  11. local dir = player:get_look_dir()
  12. obj:setvelocity({x=dir.x*27, y=dir.y*30, z=dir.z*27})
  13. obj:setacceleration({x=dir.x*-1, y=-5, z=dir.z*-1})
  14. obj:setyaw(player:get_look_yaw()+math.pi)
  15. minetest.sound_play("lottthrowing_sound", {pos=playerpos})
  16. if obj:get_luaentity().player == "" then
  17. obj:get_luaentity().player = player
  18. end
  19. obj:get_luaentity().node = player:get_inventory():get_stack("main", 1):get_name()
  20. return true
  21. end
  22. minetest.register_craftitem("lottthrowing:axe_" .. axe, {
  23. description = desc .. " Throwing Axe",
  24. on_use = function(itemstack, user, pointed_thing)
  25. lottthrowing_throw_axe(item, user, pointed_thing)
  26. if not creative then
  27. itemstack:take_item()
  28. end
  29. return itemstack
  30. end,
  31. inventory_image = "lottthrowing_axe_" .. axe .. "_inv.png",
  32. })
  33. minetest.register_node("lottthrowing:" .. axe .. "_axe_box", {
  34. drawtype = "nodebox",
  35. node_box = {
  36. type = "fixed",
  37. fixed = {
  38. {0.375, -0.5, -0.0625, 0.5, -0.375, 0.0625}, -- NodeBox1
  39. {0.25, -0.375, -0.0625, 0.375, -0.25, 0.0625}, -- NodeBox2
  40. {0.125, -0.25, -0.0625, 0.25, -0.125, 0.0625}, -- NodeBox3
  41. {0, -0.125, -0.0625, 0.125, 0, 0.0625}, -- NodeBox4
  42. {-0.125, 0, -0.0625, 0, 0.125, 0.0625}, -- NodeBox5
  43. {-0.25, 0, -0.0625, -0.125, 0.25, 0.0625}, -- NodeBox6
  44. {-0.375, 0, -0.0625, -0.25, 0.375, 0.0625}, -- NodeBox7
  45. {-0.5, 0, -0.0625, -0.375, 0.5, 0.0625}, -- NodeBox8
  46. }
  47. },
  48. tiles = {
  49. "lottthrowing_axe_top.png",
  50. "lottthrowing_axe_top.png",
  51. "lottthrowing_axe_back.png",
  52. "lottthrowing_axe_" .. axe .. "_front.png",
  53. "lottthrowing_axe_" .. axe .. ".png",
  54. "lottthrowing_axe_" .. axe .. "2.png"
  55. },
  56. groups = {not_in_creative_inventory=1},
  57. })
  58. local aep = axe_entity .. "_placeholder"
  59. local aep = {
  60. physical = false,
  61. timer = 0,
  62. visual = "wielditem",
  63. visual_size = {x=0.5, y=0.5},
  64. textures = {"lottthrowing:" .. axe .. "_axe_box"},
  65. lastpos = {},
  66. collisionbox = {0,0,0,0,0,0},
  67. player = nil,
  68. }
  69. aep.on_step = function(self, dtime)
  70. self.timer=self.timer+dtime
  71. local pos = self.object:getpos()
  72. local node = minetest.get_node(pos)
  73. if self.timer>0.2 then
  74. local objs = minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 2)
  75. for k, obj in pairs(objs) do
  76. if obj:get_luaentity() ~= nil then
  77. if obj:get_luaentity().name ~= axe_entity and obj:get_luaentity().name ~= "__builtin:item" then
  78. obj:punch(self.player, 1.0, {
  79. full_punch_interval=1.0,
  80. damage_groups={fleshy=damage},
  81. }, nil)
  82. self.object:remove()
  83. if math.random(1, 4) ~= 3 and not creative then
  84. minetest.add_item(self.lastpos, 'lottthrowing:axe_' .. axe)
  85. end
  86. end
  87. else
  88. obj:punch(self.player, 1.0, {
  89. full_punch_interval=1.0,
  90. damage_groups={fleshy=damage},
  91. }, nil)
  92. self.object:remove()
  93. if math.random(1, 4) ~= 3 and not creative then
  94. minetest.add_item(self.lastpos, 'lottthrowing:axe_' .. axe)
  95. end
  96. end
  97. end
  98. end
  99. if self.lastpos.x~=nil then
  100. if node.name ~= "air" then
  101. self.object:remove()
  102. if not creative then
  103. minetest.add_item(self.lastpos, 'lottthrowing:axe_' .. axe)
  104. end
  105. end
  106. end
  107. self.lastpos={x=pos.x, y=pos.y, z=pos.z}
  108. end
  109. minetest.register_entity(axe_entity, aep)
  110. if not craft2 then
  111. minetest.register_craft({
  112. output = "lottthrowing:axe_" .. axe .. " 4",
  113. recipe = {
  114. {craft1, "group:stick"},
  115. {craft1, "group:stick"},
  116. {"", "group:stick"},
  117. }
  118. })
  119. else
  120. minetest.register_craft({
  121. output = "lottthrowing:axe_" .. axe .. " 4",
  122. recipe = {
  123. {craft2, "group:stick"},
  124. {craft1, "group:stick"},
  125. {"", "group:stick"},
  126. }
  127. })
  128. end
  129. end
  130. lottthrowing_register_axe("dwarf", "Dwarvern", 12, "lottores:mithril_ingot", "default:steel_ingot")
  131. lottthrowing_register_axe("elf", "Elven", 10, "lottores:galvorn_ingot", "default:steel_ingot")
  132. lottthrowing_register_axe("steel", "Steel", 5, "default:steel_ingot")
  133. lottthrowing_register_axe("galvorn", "Galvorn", 7, "lottores:galvorn_ingot")