nssm_spears.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. --functions
  2. local function spears_shot (itemstack, player)
  3. local spear = itemstack:get_name() .. '_entity'
  4. local playerpos = player:get_pos()
  5. local obj = minetest.add_entity({x=playerpos.x,y=playerpos.y+1.5,z=playerpos.z}, spear)
  6. local dir = player:get_look_dir()
  7. if spear == "nssm:spear_of_peace_entity" then
  8. sp = 32
  9. gravity = 9.8
  10. else
  11. sp = 16
  12. gravity = 9.8
  13. end
  14. local dr = .3
  15. obj:set_velocity({x=dir.x*sp, y=dir.y*sp, z=dir.z*sp})
  16. obj:set_acceleration({x=-dir.x*dr, y=-gravity, z=-dir.z*dr})
  17. obj:set_yaw(player:get_look_yaw()+math.pi)
  18. minetest.sound_play("spears_sound", {pos=playerpos})
  19. obj:get_luaentity().wear = itemstack:get_wear()
  20. return true
  21. end
  22. local function spears_set_entity(kind, eq, toughness)
  23. local SPEAR_ENTITY={
  24. physical = false,
  25. timer=0,
  26. visual = "wielditem",
  27. visual_size = {x=0.15, y=0.1},
  28. textures = {"nssm:spear_" .. kind},
  29. lastpos={},
  30. collisionbox = {0,0,0,0,0,0},
  31. on_punch = function(self, puncher)
  32. if puncher then
  33. if puncher:is_player() then
  34. local stack = {name='nssm:spear_' .. kind, wear=self.wear+65535/toughness}
  35. local inv = puncher:get_inventory()
  36. if inv:room_for_item("main", stack) then
  37. inv:add_item("main", stack)
  38. self.object:remove()
  39. end
  40. end
  41. end
  42. end,
  43. }
  44. SPEAR_ENTITY.on_step = function(self, dtime)
  45. self.timer=self.timer+dtime
  46. local pos = self.object:get_pos()
  47. local node = minetest.get_node(pos)
  48. if not self.wear then
  49. self.object:remove()
  50. return
  51. end
  52. if self.lastpos.x~=nil then
  53. if node.name ~= "air" and not (string.find(node.name, 'grass') and not string.find(node.name, 'dirt')) and not string.find(node.name, 'flowers:') and not string.find(node.name, 'farming:') then
  54. self.object:remove()
  55. if self.wear+65535/toughness < 65535 then
  56. minetest.add_item(self.lastpos, {name='nssm:spear_' .. kind, wear=self.wear+65535/toughness})
  57. end
  58. elseif self.timer>0.2 then
  59. local objs = minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 1)
  60. for k, obj in pairs(objs) do
  61. if obj:get_luaentity() ~= nil then
  62. if obj:get_luaentity().name ~= "nssm:spear_" .. kind .. "_entity" and obj:get_luaentity().name ~= "__builtin:item" then
  63. local speed = vector.length(self.object:get_velocity())
  64. local damage = (speed + eq)^1.12-20
  65. obj:punch(self.object, 1.0, {
  66. full_punch_interval=1.0,
  67. damage_groups={fleshy=damage},
  68. }, nil)
  69. self.object:remove()
  70. if self.wear+65535/toughness < 65535 then
  71. minetest.add_item(self.lastpos, {name='nssm:spear_' .. kind, wear=self.wear+65535/toughness})
  72. end
  73. end
  74. end
  75. end
  76. end
  77. end
  78. self.lastpos={x=pos.x, y=pos.y, z=pos.z}
  79. end
  80. return SPEAR_ENTITY
  81. end
  82. --Tools
  83. local function spears_register_spear(kind, desc, eq, toughness, material)
  84. minetest.register_tool("nssm:spear_" .. kind, {
  85. description = desc .. " Spear",
  86. wield_image = "spear_" .. kind .. ".png",
  87. inventory_image = "spear_" .. kind .. ".png^[transform4",
  88. wield_scale= {x=2,y=1,z=1},
  89. on_drop = function(itemstack, user, pointed_thing)
  90. spears_shot(itemstack, user)
  91. if not minetest.setting_getbool("creative_mode") then
  92. itemstack:take_item()
  93. end
  94. return itemstack
  95. end,
  96. on_place = function(itemstack, user, pointed_thing)
  97. minetest.add_item(pointed_thing.above, itemstack)
  98. if not minetest.setting_getbool("creative_mode") then
  99. itemstack:take_item()
  100. end
  101. return itemstack
  102. end,
  103. tool_capabilities = {
  104. full_punch_interval = 1.3,
  105. max_drop_level=1,
  106. groupcaps={
  107. snappy = {times={[3]=0.2, [2]=0.2, [1]=0.2}, uses=toughness, maxlevel=1},
  108. },
  109. damage_groups = {fleshy=eq},
  110. }
  111. })
  112. local SPEAR_ENTITY=spears_set_entity(kind, eq, toughness)
  113. minetest.register_entity("nssm:spear_" .. kind .. "_entity", SPEAR_ENTITY)
  114. minetest.register_craft({
  115. output = 'nssm:spear_' .. kind,
  116. recipe = {
  117. {'group:wood', 'group:wood', material},
  118. }
  119. })
  120. minetest.register_craft({
  121. output = 'nssm:spear_' .. kind,
  122. recipe = {
  123. {material, 'group:wood', 'group:wood'},
  124. }
  125. })
  126. end
  127. spears_register_spear('ant', 'Ant', 6, 25, 'nssm:ant_mandible')
  128. spears_register_spear('mantis', 'Mantis', 6, 10, 'nssm:mantis_claw')
  129. spears_register_spear('manticore', 'Manticore', 8, 8, 'nssm:manticore_spine')
  130. spears_register_spear('ice_tooth', 'Ice Tooth', 16, 200, 'nssm:ice_tooth')
  131. spears_register_spear('little_ice_tooth', 'Little Ice Tooth', 7, 10, 'nssm:little_ice_tooth')
  132. spears_register_spear('duck_beak', 'Duck Beak', 5, 6, 'nssm:duck_beak')
  133. spears_register_spear('felucco_horn', 'Felucco Horn', 7, 9, 'nssm:felucco_horn')
  134. --Spear of peace
  135. minetest.register_tool("nssm:spear_of_peace", {
  136. description = "Spear of Peace",
  137. wield_image = "spear_of_peace.png",
  138. inventory_image = "spear_of_peace.png^[transform4",
  139. wield_scale= {x=4,y=2,z=2},
  140. on_drop = function(itemstack, user, pointed_thing)
  141. spears_shot(itemstack, user)
  142. if not minetest.setting_getbool("creative_mode") then
  143. itemstack:take_item()
  144. end
  145. return itemstack
  146. end,
  147. on_place = function(itemstack, user, pointed_thing)
  148. minetest.add_item(pointed_thing.above, itemstack)
  149. if not minetest.setting_getbool("creative_mode") then
  150. itemstack:take_item()
  151. end
  152. return itemstack
  153. end,
  154. tool_capabilities = {
  155. full_punch_interval = 0.7,
  156. max_drop_level=1,
  157. groupcaps={
  158. snappy = {times={[3]=0.2, [2]=0.2, [1]=0.2}, uses=500, maxlevel=1},
  159. },
  160. damage_groups = {fleshy=18},
  161. }
  162. })
  163. local function spears_set_sentity(kind, eq, toughness)
  164. local SUPERSPEAR_ENTITY={
  165. physical = false,
  166. timer=0,
  167. visual = "wielditem",
  168. visual_size = {x=0.15, y=0.1},
  169. textures = {"nssm:spear_" .. kind},
  170. lastpos={},
  171. collisionbox = {0,0,0,0,0,0},
  172. on_punch = function(self, puncher)
  173. if puncher then
  174. if puncher:is_player() then
  175. local stack = {name='nssm:spear_' .. kind, wear=self.wear+65535/toughness}
  176. local inv = puncher:get_inventory()
  177. if inv:room_for_item("main", stack) then
  178. inv:add_item("main", stack)
  179. self.object:remove()
  180. end
  181. end
  182. end
  183. end,
  184. }
  185. SUPERSPEAR_ENTITY.on_step = function(self, dtime)
  186. self.timer=self.timer+dtime
  187. local pos = self.object:get_pos()
  188. local node = minetest.get_node(pos)
  189. if not self.wear then
  190. self.object:remove()
  191. return
  192. end
  193. if self.lastpos.x~=nil then
  194. if node.name ~= "air" and not (string.find(node.name, 'grass') and not string.find(node.name, 'dirt')) and not string.find(node.name, 'flowers:') and not string.find(node.name, 'farming:') then
  195. self.object:remove()
  196. if self.wear+65535/toughness < 65535 then
  197. minetest.add_item(self.lastpos, {name='nssm:spear_' .. kind, wear=self.wear+65535/toughness})
  198. end
  199. elseif self.timer>0.2 then
  200. local objs = minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 1)
  201. for k, obj in pairs(objs) do
  202. if obj:get_luaentity() ~= nil then
  203. if obj:get_luaentity().name ~= "nssm:spear_" .. kind .. "_entity" and obj:get_luaentity().name ~= "__builtin:item" then
  204. local speed = vector.length(self.object:get_velocity())
  205. --minetest.chat_send_all(speed)
  206. local damage = (speed + eq)^1.12-20
  207. obj:punch(self.object, 1.0, {
  208. full_punch_interval=1.0,
  209. damage_groups={fleshy=damage},
  210. }, nil)
  211. self.object:remove()
  212. if self.wear+65535/toughness < 65535 then
  213. minetest.add_item(self.lastpos, {name='nssm:spear_' .. kind, wear=self.wear+65535/toughness})
  214. end
  215. end
  216. end
  217. end
  218. end
  219. end
  220. self.lastpos={x=pos.x, y=pos.y, z=pos.z}
  221. end
  222. return SUPERSPEAR_ENTITY
  223. end
  224. SUPERSPEAR_ENTITY=spears_set_sentity("of_peace", 30, 300)
  225. minetest.register_entity("nssm:spear_of_peace_entity", SUPERSPEAR_ENTITY)
  226. minetest.register_craft({
  227. output = 'nssm:spear_of_peace',
  228. recipe = {
  229. {'nssm:wrathful_moranga', 'group:wood', 'group:wood'},
  230. }
  231. })