init.lua 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. if minetest.settings:get("bows_unmodified") then --ed
  2. catchballs={type=0}
  3. minetest.register_craft({
  4. output = "catchballs:catchball1",
  5. recipe = {
  6. {"default:gold_ingot","default:steel_ingot","default:gold_ingot"},
  7. {"default:steel_ingot","default:mese_crystal","default:steel_ingot"},
  8. {"default:gold_ingot","default:steel_ingot","default:gold_ingot"},
  9. }
  10. })
  11. minetest.register_craft({
  12. output = "catchballs:catchball2",
  13. recipe = {
  14. {"default:gold_ingot","default:steel_ingot","default:gold_ingot"},
  15. {"default:steel_ingot","default:mese","default:steel_ingot"},
  16. {"default:gold_ingot","default:steel_ingot","default:gold_ingot"},
  17. }
  18. })
  19. minetest.register_entity("catchballs:catchball",{
  20. hp_max = 10,
  21. physical =true,
  22. weight = 1,
  23. collisionbox = {-0.2,-0.2,-0.2,0.2,0.2,0.2},
  24. visual = "wielditem",
  25. visual_size = {x=0.3,y=0.3},
  26. textures ={"catchballs:catchball1"},
  27. colors = {},
  28. spritediv = {x=1, y=1},
  29. initial_sprite_basepos = {x=0, y=0},
  30. is_visible = true,
  31. makes_footstep_sound = true,
  32. automatic_rotate =3.14,
  33. kill=function(self)
  34. self.object:set_hp(0)
  35. self.object:punch(self.object, 100,{full_punch_interval=1.0,damage_groups={fleshy=4}})
  36. return self
  37. end,
  38. ret=function(self)
  39. local pos=self.object:get_pos()
  40. if not self.retur and self.meta then
  41. self.retur=1
  42. local e=minetest.add_entity({x=pos.x,y=pos.y+1,z=pos.z}, self.meta.name)
  43. local en=e:get_luaentity()
  44. if en.on_activate then
  45. en.on_activate(en,self.meta.data)
  46. end
  47. self.object:set_properties({physical=false})
  48. self.object:set_acceleration({x =0, y =0, z =0})
  49. minetest.add_particlespawner({
  50. amount = 40,
  51. time =0.5,
  52. minpos = pos,
  53. maxpos =pos,
  54. minvel = {x=-2, y=0, z=-2},
  55. maxvel = {x=2, y=2, z=2},
  56. minacc = {x=0, y=0, z=0},
  57. maxacc = {x=0, y=0, z=0},
  58. minexptime = 2,
  59. maxexptime = 1,
  60. minsize = 0.5,
  61. maxsize = 1,
  62. glow=10,
  63. texture = "catchballs_catchball.png^[colorize:#ffcf42ff",
  64. })
  65. local nod=minetest.registered_nodes[minetest.get_node(pos).name]
  66. if nod and nod.buildable_to==true then minetest.set_node(pos,{name="catchballs:light"}) end
  67. end
  68. if self.retur then
  69. local pos2=self.user:get_pos()
  70. pos2.y=pos2.y+1
  71. local v={x=(pos.x-pos2.x)*-4,y=(pos.y-pos2.y)*-4,z=(pos.z-pos2.z)*-4}
  72. self.object:set_velocity(v)
  73. for _, ob in ipairs(minetest.get_objects_inside_radius(pos, 1.5)) do
  74. if ob:is_player() and ob:get_player_name()==self.user_name then
  75. ob:get_inventory():add_item("main", self.item)
  76. self.kill(self)
  77. return self
  78. end
  79. end
  80. end
  81. if self.timer<=0 then
  82. minetest.add_item(pos,self.item)
  83. self.kill(self)
  84. return self
  85. end
  86. end,
  87. on_activate=function(self, staticdata)
  88. self.type=catchballs.type
  89. self.item=catchballs.item
  90. self.object:set_properties({textures={self.item}})
  91. if self.type==1 then
  92. elseif self.type==2 then
  93. self.user=catchballs.user
  94. self.meta=catchballs.meta
  95. self.user_name=self.user:get_player_name()
  96. self.rego=1
  97. elseif self.type==3 then
  98. self.user=catchballs.user
  99. self.user_name=self.user:get_player_name()
  100. else
  101. minetest.add_item(self.object:get_pos(),self.item)
  102. self.kill(self)
  103. return self
  104. end
  105. catchballs.type=0
  106. catchballs.user=nil
  107. catchballs.meta=nil
  108. catchballs.item=nil
  109. self.object:set_acceleration({x =0, y =-10, z =0})
  110. return self
  111. end,
  112. on_step=function(self, dtime)
  113. self.time=self.time-dtime
  114. if self.time>0 then return self end
  115. if self.catch and self.rego then
  116. self.timer=self.timer-dtime
  117. self.ret(self)
  118. return self
  119. end
  120. if self.object:get_velocity().y==0 and not self.catch then
  121. self.catch=1
  122. if self.type==2 then self.rego=1 end
  123. if self.rego then return end
  124. local pos=self.object:get_pos()
  125. local ob1
  126. local d1=10
  127. local d2=10
  128. local en1
  129. for _, ob in ipairs(minetest.get_objects_inside_radius(pos, 5)) do
  130. local en=ob:get_luaentity()
  131. local pos2=ob:get_pos()
  132. d2=vector.distance(pos,pos2)
  133. if en and d2<d1 and en.type and en.type~="" and not en.catchball and en.itemstring==nil then
  134. ob1=ob
  135. en1=en
  136. d1=d2
  137. end
  138. end
  139. if not ob1 then
  140. if self.type==3 then
  141. self.object:set_properties({physical=false})
  142. self.retur=1
  143. self.rego=1
  144. self.object:set_acceleration({x =0, y =0, z =0})
  145. return
  146. end
  147. minetest.add_item(pos,self.item)
  148. self.kill(self)
  149. return self
  150. end
  151. local item=ItemStack(self.item):to_table()
  152. item.wear=1
  153. local data=""
  154. if en1.get_staticdata then
  155. data=en1.get_staticdata(en1)
  156. end
  157. if type(data)~="string" then data="" end
  158. item.meta.data=data
  159. item.meta.name=en1.name
  160. item.meta.description=en1.name
  161. en1.on_step=nil
  162. ob1:remove()
  163. if self.type==3 then
  164. self.object:set_properties({physical=false})
  165. self.retur=1
  166. self.rego=1
  167. self.item=item
  168. self.object:set_acceleration({x =0, y =0, z =0})
  169. return self
  170. end
  171. minetest.add_item(pos,item)
  172. self.kill(self)
  173. return self
  174. end
  175. end,
  176. time=0.2,
  177. timer=15,
  178. type="",
  179. catchball=1,
  180. })
  181. minetest.register_tool("catchballs:catchball1", {
  182. description = "Catch ball",
  183. range=1,
  184. inventory_image = "catchballs_catchball.png",
  185. on_use = function(itemstack, user, pointed_thing)
  186. if user.fakeplayer then return end
  187. local dir
  188. local pos
  189. if user:get_luaentity() then
  190. user=user:get_luaentity():get_luaentity()
  191. pos=user.object:get_pos()
  192. dir=catchballs.get_dir(pos,catchballs.pointat(user))
  193. else
  194. dir=user:get_look_dir()
  195. pos=user:get_pos()
  196. end
  197. local item=itemstack:to_table()
  198. catchballs.user=user
  199. if item.meta and item.meta.data then
  200. catchballs.type=2
  201. catchballs.meta=item.meta
  202. else
  203. catchballs.type=1
  204. end
  205. catchballs.item="catchballs:catchball1"
  206. pos.y=pos.y+1
  207. local v={x=dir.x*15,y=dir.y*15,z=dir.z*15}
  208. local e=minetest.add_entity(pos, "catchballs:catchball")
  209. e:set_velocity(v)
  210. itemstack:take_item()
  211. if user.inv and user.inv["catchballs:catchball1"] and type(user.inv["catchballs:catchball1"])=="number" then
  212. user.inv["catchballs:catchball1"]=user.inv["catchballs:catchball1"]-1
  213. if user.inv["catchballs:catchball1"]<=0 then user.inv["catchballs:catchball1"]=nil end
  214. end
  215. return itemstack
  216. end
  217. })
  218. minetest.register_tool("catchballs:catchball2", {
  219. description = "Master catch ball",
  220. inventory_image = "catchballs_catchball2.png",
  221. on_use = function(itemstack, user, pointed_thing)
  222. if user.fakeplayer then return end
  223. local dir
  224. local pos
  225. local item=itemstack:to_table()
  226. catchballs.user=user
  227. catchballs.item="catchballs:catchball2"
  228. if item.meta and item.meta.data then
  229. catchballs.type=2
  230. catchballs.meta=item.meta
  231. else
  232. catchballs.type=3
  233. end
  234. if user:get_luaentity() then
  235. catchballs.type=1
  236. user=user:get_luaentity():get_luaentity()
  237. pos=user.object:get_pos()
  238. dir=catchballs.get_dir(pos,catchballs.pointat(user))
  239. else
  240. dir=user:get_look_dir()
  241. pos=user:get_pos()
  242. end
  243. pos.y=pos.y+1
  244. local v={x=dir.x*15,y=dir.y*15,z=dir.z*15}
  245. local e=minetest.add_entity(pos, "catchballs:catchball")
  246. e:set_velocity(v)
  247. itemstack:take_item()
  248. if user.inv and user.inv["catchballs:catchball2"] and type(user.inv["catchballs:catchball2"])=="number" then
  249. user.inv["catchballs:catchball2"]=user.inv["catchballs:catchball2"]-1
  250. if user.inv["catchballs:catchball2"]<=0 then user.inv["catchballs:catchball2"]=nil end
  251. end
  252. return itemstack
  253. end
  254. })
  255. minetest.register_node("catchballs:light", {
  256. description = "Light",
  257. drop="",
  258. light_source = 10,
  259. paramtype = "light",
  260. alpha = 50,
  261. walkable=false,
  262. drawtype = "airlike",
  263. sunlight_propagates = false,
  264. pointable = false,
  265. buildable_to = true,
  266. groups = {not_in_creative_inventory=1},
  267. on_construct=function(pos)
  268. minetest.get_node_timer(pos):start(1)
  269. end,
  270. on_timer = function (pos, elapsed)
  271. minetest.set_node(pos,{name="air"})
  272. return false
  273. end,
  274. })
  275. catchballs.pointat=function(self)
  276. local pos=self.object:get_pos()
  277. local yaw=self.object:get_yaw()
  278. if yaw ~= yaw or type(yaw)~="number" then
  279. yaw=0
  280. end
  281. local x =math.sin(yaw) * -1
  282. local z =math.cos(yaw) * 1
  283. return {x=pos.x+x,y=pos.y,z=pos.z+z}
  284. end
  285. catchballs.get_dir=function(pos1,pos2)
  286. local d=math.floor(vector.distance(pos1,pos2)+0.5)
  287. return {x=(pos1.x-pos2.x)/-d,y=(pos1.y-pos2.y)/-d,z=(pos1.z-pos2.z)/-d}
  288. end
  289. end--ed