standard_arrows.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. throwing_arrow_punch_entity(obj, self, damage*500)
  54. ambiance.sound_play("throwing_arrow_hit", obj:get_pos(), 1.0, 32)
  55. end
  56. function THROWING_ARROW_ENTITY.hit_node(self, under, above, intersection_point)
  57. if math_random() < toughness then
  58. local ent = minetest.add_item(above, 'throwing:arrow_' .. kind)
  59. if ent then
  60. local luaent = ent:get_luaentity()
  61. if not luaent then
  62. ent:remove()
  63. return
  64. end
  65. local liquid = false
  66. local node = minetest.get_node(under)
  67. local ndef = minetest.registered_nodes[node.name]
  68. if ndef.liquidtype ~= "none" then
  69. liquid = true
  70. end
  71. if intersection_point then
  72. ent:set_pos(intersection_point)
  73. ent:set_velocity({x=0, y=0, z=0})
  74. if not liquid then
  75. -- I wish the API used quaternions. :(
  76. local op = self.lastpos
  77. local v = vector.normalize(vector.subtract(intersection_point, op))
  78. local O = vector.length({x=v.x, y=0, z=v.z})
  79. local A = v.y
  80. local pitch = 0
  81. if math.abs(O) > 0 then
  82. pitch = math.atan(A/O)
  83. end
  84. ent:set_properties({
  85. automatic_rotate = 0,
  86. physical = true,
  87. collide_with_objects = true,
  88. collisionbox = {-0.2, -0.2, -0.2, 0.2, 0.2, 0.2},
  89. })
  90. ent:set_rotation({x=0, y=self.object:get_yaw(), z=pitch})
  91. ent:set_acceleration({x=0, y=0, z=0})
  92. luaent.stuck_arrow = true
  93. end
  94. end
  95. end
  96. else
  97. minetest.add_item(above, 'default:stick')
  98. end
  99. ambiance.sound_play("throwing_arrow_hit", under, 1.0, 32)
  100. end
  101. THROWING_ARROW_ENTITY.on_step = function(self, dtime)
  102. throwing.do_fly(self, dtime)
  103. end
  104. minetest.register_entity("throwing:arrow_" .. kind .. "_entity", THROWING_ARROW_ENTITY)
  105. if craftcount == 1 then
  106. minetest.register_craft({
  107. output = 'throwing:arrow_' .. kind .. ' ' .. craftcount,
  108. recipe = {
  109. {craft, 'default:stick', 'default:stick'},
  110. }
  111. })
  112. elseif craftcount == 3 then
  113. minetest.register_craft({
  114. output = 'throwing:arrow_' .. kind .. ' ' .. craftcount,
  115. recipe = {
  116. {'', 'default:stick', 'default:stick'},
  117. {craft, 'default:stick', 'default:stick'},
  118. {'', 'default:stick', 'default:stick'},
  119. }
  120. })
  121. end
  122. end
  123. throwing_register_arrow_standard ('stone', 'Stone', 5, 0.88, 'default:cobble', 3)
  124. throwing_register_arrow_standard ('steel', 'Steel', 15, 0.94, 'default:steel_ingot', 3)
  125. throwing_register_arrow_standard ('diamond', 'Diamond', 25, 0.97, 'dusts:diamond_shard', 1)
  126. throwing_register_arrow_standard ('obsidian', 'Obsidian', 20, 0.88, 'default:obsidian_shard', 1)
  127. throwing_register_arrow_standard ('mese', 'Mese', 17, 0.90, 'default:mese_crystal_fragment', 1)