standard_arrows.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. -- Localize for performance.
  2. local math_random = math.random
  3. function throwing_register_arrow_standard (kind, desc, eq, toughness, craft, craftcount)
  4. minetest.register_craftitem("throwing:arrow_" .. kind, {
  5. description = desc .. " Arrow",
  6. inventory_image = "throwing_arrow_" .. kind .. ".png",
  7. })
  8. minetest.register_node("throwing:arrow_" .. kind .. "_box", {
  9. drawtype = "nodebox",
  10. node_box = {
  11. type = "fixed",
  12. fixed = {
  13. -- Shaft
  14. {-6.5/17, -1.5/17, -1.5/17, 6.5/17, 1.5/17, 1.5/17},
  15. --Spitze
  16. {-4.5/17, 2.5/17, 2.5/17, -3.5/17, -2.5/17, -2.5/17},
  17. {-8.5/17, 0.5/17, 0.5/17, -6.5/17, -0.5/17, -0.5/17},
  18. --Federn
  19. {6.5/17, 1.5/17, 1.5/17, 7.5/17, 2.5/17, 2.5/17},
  20. {7.5/17, -2.5/17, 2.5/17, 6.5/17, -1.5/17, 1.5/17},
  21. {7.5/17, 2.5/17, -2.5/17, 6.5/17, 1.5/17, -1.5/17},
  22. {6.5/17, -1.5/17, -1.5/17, 7.5/17, -2.5/17, -2.5/17},
  23. {7.5/17, 2.5/17, 2.5/17, 8.5/17, 3.5/17, 3.5/17},
  24. {8.5/17, -3.5/17, 3.5/17, 7.5/17, -2.5/17, 2.5/17},
  25. {8.5/17, 3.5/17, -3.5/17, 7.5/17, 2.5/17, -2.5/17},
  26. {7.5/17, -2.5/17, -2.5/17, 8.5/17, -3.5/17, -3.5/17},
  27. }
  28. },
  29. tiles = {"throwing_arrow_" .. kind .. ".png", "throwing_arrow_" .. kind .. ".png", "throwing_arrow_" .. kind .. "_back.png", "throwing_arrow_" .. kind .. "_front.png", "throwing_arrow_" .. kind .. "_2.png", "throwing_arrow_" .. kind .. ".png"},
  30. groups = {not_in_creative_inventory=1},
  31. })
  32. local THROWING_ARROW_ENTITY = {
  33. _name = "throwing:arrow_" .. kind,
  34. physical = false,
  35. timer=0,
  36. visual = "wielditem",
  37. visual_size = {x=0.1, y=0.1},
  38. textures = {"throwing:arrow_" .. kind .. "_box"},
  39. lastpos={},
  40. collisionbox = {0,0,0,0,0,0},
  41. static_save = false,
  42. }
  43. function THROWING_ARROW_ENTITY.hit_player(self, obj, intersection_point)
  44. local vel = self.object:get_velocity()
  45. local speed = vector.length(vel) / 2
  46. local damage = ((speed + eq)^1.2)/5
  47. throwing_arrow_punch_entity(obj, self, damage*500)
  48. end
  49. function THROWING_ARROW_ENTITY.hit_object(self, obj, intersection_point)
  50. local vel = self.object:get_velocity()
  51. local speed = vector.length(vel) / 2
  52. local damage = ((speed + eq)^1.2)/5
  53. local luaent = obj:get_luaentity()
  54. if luaent and (luaent.mob or luaent._cmi_is_mob) then
  55. if luaent.add_item_drop then
  56. --minetest.chat_send_all('adding!')
  57. luaent:add_item_drop("throwing:arrow_" .. kind)
  58. end
  59. end
  60. throwing_arrow_punch_entity(obj, self, damage*500)
  61. ambiance.sound_play("throwing_arrow_hit", obj:get_pos(), 1.0, 32)
  62. end
  63. function THROWING_ARROW_ENTITY.hit_node(self, under, above, intersection_point)
  64. if math_random() < toughness then
  65. local ent = minetest.add_item(above, 'throwing:arrow_' .. kind)
  66. if ent then
  67. local luaent = ent:get_luaentity()
  68. if not luaent then
  69. ent:remove()
  70. return
  71. end
  72. local liquid = false
  73. local node = minetest.get_node(under)
  74. local ndef = minetest.registered_nodes[node.name]
  75. if ndef.liquidtype ~= "none" then
  76. liquid = true
  77. end
  78. if intersection_point then
  79. ent:set_pos(intersection_point)
  80. ent:set_velocity({x=0, y=0, z=0})
  81. if not liquid then
  82. -- I wish the API used quaternions. :(
  83. local op = self.lastpos
  84. local v = vector.normalize(vector.subtract(intersection_point, op))
  85. local O = vector.length({x=v.x, y=0, z=v.z})
  86. local A = v.y
  87. local pitch = 0
  88. if math.abs(O) > 0 then
  89. pitch = math.atan(A/O)
  90. end
  91. ent:set_properties({
  92. automatic_rotate = 0,
  93. physical = true,
  94. collide_with_objects = true,
  95. collisionbox = {-0.2, -0.2, -0.2, 0.2, 0.2, 0.2},
  96. })
  97. ent:set_rotation({x=0, y=self.object:get_yaw(), z=pitch})
  98. ent:set_acceleration({x=0, y=0, z=0})
  99. luaent.stuck_arrow = true
  100. end
  101. end
  102. end
  103. else
  104. minetest.add_item(above, 'default:stick')
  105. end
  106. ambiance.sound_play("throwing_arrow_hit", under, 1.0, 32)
  107. end
  108. THROWING_ARROW_ENTITY.on_step = function(self, dtime)
  109. throwing.do_fly(self, dtime)
  110. end
  111. minetest.register_entity("throwing:arrow_" .. kind .. "_entity", THROWING_ARROW_ENTITY)
  112. if craftcount == 1 then
  113. minetest.register_craft({
  114. output = 'throwing:arrow_' .. kind .. ' ' .. craftcount,
  115. recipe = {
  116. {craft, 'default:stick', 'default:stick'},
  117. }
  118. })
  119. elseif craftcount == 3 then
  120. minetest.register_craft({
  121. output = 'throwing:arrow_' .. kind .. ' ' .. craftcount,
  122. recipe = {
  123. {'', 'default:stick', 'default:stick'},
  124. {craft, 'default:stick', 'default:stick'},
  125. {'', 'default:stick', 'default:stick'},
  126. }
  127. })
  128. end
  129. end
  130. throwing_register_arrow_standard ('stone', 'Stone', 5, 0.88, 'default:cobble', 3)
  131. throwing_register_arrow_standard ('steel', 'Steel', 15, 0.94, 'default:steel_ingot', 3)
  132. throwing_register_arrow_standard ('diamond', 'Diamond', 25, 0.97, 'dusts:diamond_shard', 1)
  133. throwing_register_arrow_standard ('obsidian', 'Obsidian', 20, 0.88, 'default:obsidian_shard', 1)
  134. throwing_register_arrow_standard ('mese', 'Mese', 17, 0.90, 'default:mese_crystal_fragment', 1)