init.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. -- Bows Mod by UjEdwin
  2. bows = {
  3. pvp = minetest.settings:get_bool('enable_pvp'),
  4. registered_arrows = {},
  5. registered_bows = {},
  6. }
  7. local creative_mode_cache = minetest.settings:get_bool('creative_mode')
  8. function bows.is_creative(name)
  9. return creative_mode_cache or minetest.check_player_privs(name, {creative = true})
  10. end
  11. bows.register_arrow = function(name, def)
  12. if name == nil
  13. or name == '' then
  14. return false
  15. end
  16. def.damage = def.damage or 0
  17. def.name = 'bows:' .. name
  18. def.level = def.level or 1
  19. def.disp_entity = def.displayed_entity or def.name
  20. def.on_hit_object = def.on_hit_object
  21. def.on_hit_node = def.on_hit_node
  22. def.speed = def.speed or 0
  23. def.on_hit_sound = def.on_hit_sound or 'default_dig_dig_immediate'
  24. bows.registered_arrows[def.name] = def
  25. if def.type == 'bolt' then
  26. minetest.register_craftitem(':bows:' .. name, {
  27. description = def.description or name,
  28. inventory_image = def.texture or 'bows_arrow_wooden.png',
  29. groups = {bolt = 1},
  30. drop_chance = def.drop_chance
  31. })
  32. else
  33. minetest.register_craftitem(':bows:' .. name, {
  34. description = def.description or name,
  35. inventory_image = def.texture or 'bows_arrow_wooden.png',
  36. groups = {arrow = 1},
  37. drop_chance = def.drop_chance
  38. })
  39. end
  40. if def.craft then
  41. minetest.register_craft({
  42. output = def.name ..' ' .. (def.craft_count or 1),
  43. recipe = def.craft
  44. })
  45. end
  46. end
  47. bows.register_bow = function(name, def)
  48. if name == nil
  49. or name == '' then
  50. return false
  51. end
  52. def.replace = 'bows:' .. name .. '_loaded'
  53. def.name = 'bows:' .. name
  54. def.uses = def.uses - 1 or 49
  55. def.ammo = def.ammo or 'arrow'
  56. bows.registered_bows[def.replace] = def
  57. bows.registered_bows[def.name] = def
  58. minetest.register_tool(':' .. def.name, {
  59. description = def.description or name,
  60. inventory_image = def.texture or 'bows_bow.png',
  61. on_use = bows.load,
  62. groups = {bow = 1},
  63. })
  64. minetest.register_tool(':' .. def.replace, {
  65. description = def.description or name,
  66. inventory_image = def.texture_loaded or 'bows_bow_loaded.png',
  67. on_use = bows.shoot,
  68. groups = {bow = 1, not_in_creative_inventory = 1},
  69. })
  70. if def.craft then
  71. minetest.register_craft({output = def.name,recipe = def.craft})
  72. end
  73. end
  74. bows.load = function(itemstack, user, pointed_thing)
  75. local name = itemstack:get_name()
  76. local ammo = bows.registered_bows[name].ammo
  77. local inv = user:get_inventory()
  78. local index = user:get_wield_index() - 1
  79. local arrow = inv:get_stack('main', index)
  80. if minetest.get_item_group(arrow:get_name(), ammo) == 0 then
  81. return itemstack
  82. end
  83. local item = itemstack:to_table()
  84. local meta = minetest.deserialize(item.metadata)
  85. meta = {arrow = arrow:get_name()}
  86. item.metadata = minetest.serialize(meta)
  87. item.name = item.name .. '_loaded'
  88. itemstack:replace(item)
  89. if not bows.is_creative(user:get_player_name()) then
  90. inv:set_stack('main', index,
  91. ItemStack(arrow:get_name() .. ' ' .. (arrow:get_count() - 1)))
  92. end
  93. return itemstack
  94. end
  95. bows.shoot = function(itemstack, user, pointed_thing)
  96. local item = itemstack:to_table()
  97. local meta = minetest.deserialize(item.metadata)
  98. if (not (meta and meta.arrow))
  99. or (not bows.registered_arrows[meta.arrow]) then
  100. return itemstack
  101. end
  102. local name = itemstack:get_name()
  103. local replace = bows.registered_bows[name].name
  104. local ar = bows.registered_bows[name].uses
  105. local wear = bows.registered_bows[name].uses
  106. local arrow_speed = bows.registered_arrows[meta.arrow].speed
  107. local level = bows.registered_bows[name].level + arrow_speed
  108. bows.tmp = {}
  109. bows.tmp.arrow = meta.arrow
  110. bows.tmp.user = user
  111. bows.tmp.name = meta.arrow
  112. item.arrow = ''
  113. item.metadata = minetest.serialize(meta)
  114. item.name = replace
  115. itemstack:replace(item)
  116. local pos = user:get_pos()
  117. local dir = user:get_look_dir()
  118. local e = minetest.add_entity({
  119. x = pos.x,
  120. y = pos.y + 1.5,
  121. z = pos.z
  122. }, 'bows:arrow')
  123. e:set_velocity({x = dir.x * level, y = dir.y * level, z = dir.z * level})
  124. e:set_acceleration({x = dir.x * -3, y = -4, z = dir.z * -3})
  125. e:set_yaw(user:get_look_horizontal() - math.pi/2)
  126. if not bows.is_creative(user:get_player_name()) then
  127. itemstack:add_wear(65535 / wear)
  128. end
  129. minetest.sound_play('bows_shoot', {pos = pos})
  130. return itemstack
  131. end
  132. local path = minetest.get_modpath('bows')
  133. dofile(path .. '/arrow.lua')
  134. dofile(path .. '/bows.lua')
  135. dofile(path .. '/crossbows.lua')
  136. minetest.register_alias('castle_weapons:crossbow', 'bows:crossbow')
  137. minetest.register_alias('castle_weapons:crossbow_bolt', 'bows:bolt_3')