crossbow.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. --[[
  2. Minetest Mod - Simple Shooter [shooter] 0.5.3
  3. =======================================
  4. License Source Code: 2013 Stuart Jones - LGPL v2.1
  5. License Textures: Stuart Jones - WTFPL
  6. Licence Models: Stuart Jones - CC-BY-SA 3.0
  7. License Sounds: freesound.org
  8. --]]
  9. minetest.register_alias('crossbow', 'castle_weapons:crossbow')
  10. minetest.register_alias('bolt', 'castle_weapons:crossbow_bolt')
  11. minetest.register_alias('castle:crossbow', 'castle_weapons:crossbow')
  12. minetest.register_alias('castle:bolt', 'castle_weapons:crossbow_bolt')
  13. minetest.register_alias('castle:crossbow_bolt', 'castle_weapons:crossbow_bolt')
  14. minetest.register_alias('castle:crossbow_loaded', 'castle_weapons:crossbow_loaded')
  15. -- internationalization boilerplate
  16. local MP = minetest.get_modpath(minetest.get_current_modname())
  17. local S, NS = dofile(MP..'/intllib.lua')
  18. local crossbow={}
  19. CROSSBOW_USES = 300
  20. CROSSBOW_BOLT_TOOL_CAPS = {damage_groups={fleshy=4}}
  21. CROSSBOW_BOLT_LIFETIME = 60-- 1 minute
  22. CROSSBOW_ENABLE_PARTICLE_FX = false
  23. CROSSBOW_ENABLE_PROTECTION = true
  24. CROSSBOW_EXPLOSION_TEXTURE = 'castle_crossbow_hit.png'
  25. CROSSBOW_ALLOW_NODES = true
  26. CROSSBOW_ALLOW_ENTITIES = true
  27. CROSSBOW_ALLOW_PLAYERS = true
  28. CROSSBOW_PLAYER_OFFSET = {x=0, y=1, z=0}
  29. CROSSBOW_ENTITY_OFFSET = {x=0, y=0, z=0}
  30. CROSSBOW_ENTITIES = {
  31. 'zombies:1arm',
  32. 'zombies:crawler',
  33. 'zombies:normal',
  34. 'scorpion:big',
  35. 'scorpion:boss',
  36. 'scorpion:little',
  37. 'scorpion:pet',
  38. 'farm_mobs:goat_he',
  39. 'farm_mobs:goat_she',
  40. 'farm_mobs:dog',
  41. 'fantasy_mobs:fairy',
  42. 'fantasy_mobs:gnome',
  43. 'fantasy_mobs:goblin',
  44. 'fantasy_mobs:mummy',
  45. 'fantasy_mobs:larva',
  46. 'fantasy_mobs:larva_pet',
  47. 'fantasy_mobs:cavefreak_slash',
  48. 'fantasy_mobs:cavefreak_fire',
  49. 'fantasy_mobs:lava_titan',
  50. 'desert_life:ostrich',
  51. 'desert_life:armadillo',
  52. 'epic:ocean_guardian',
  53. 'arctic_life:penguin',
  54. 'arctic_life:walrus',
  55. 'desert_life:ostrich',
  56. 'desert_life:armadillo',
  57. 'mobs_animal:bunny',
  58. 'mobs_animal:chicken',
  59. 'mobs_animal:cow',
  60. 'mobs_animal:kitten',
  61. 'mobs_animal:panda',
  62. 'mobs_animal:sheep_white',
  63. 'mobs_animal:pumba',
  64. 'mob_horse:horse',
  65. 'mobs_monster:dirt_monster',
  66. 'mobs_monster:dungeon_master',
  67. 'mobs_monster:lava_flan',
  68. 'mobs_monster:mese_monster',
  69. 'mobs_monster:oerkki',
  70. 'mobs_monster:sand_monster',
  71. 'mobs_monster:spider',
  72. 'mobs_monster:stone_monster',
  73. 'mobs_monster:tree_monster',
  74. 'mobs_crocs:crocodile',
  75. 'mobs_crocs:crocodile_swim',
  76. 'mobs_crocs:crocodile_float',
  77. 'mobs_turtles:turtle',
  78. 'mobs_turtles:seaturtle',
  79. 'viron:viron_mob',
  80. 'viron:viron_queen',
  81. 'viron:viron_larve',
  82. }
  83. if minetest.is_singleplayer() == true then
  84. CROSSBOW_ALLOW_ENTITIES = true
  85. CROSSBOW_ALLOW_PLAYERS = true
  86. end
  87. local allowed_entities = {}
  88. for _,v in ipairs(CROSSBOW_ENTITIES) do
  89. allowed_entities[v] = 1
  90. end
  91. local function get_dot_product(v1, v2)
  92. return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z
  93. end
  94. local function get_particle_pos(p, v, d)
  95. return vector.add(p, vector.multiply(v, {x=d, y=d, z=d}))
  96. end
  97. function crossbow:spawn_particles(pos, texture)
  98. if CROSSBOW_ENABLE_PARTICLE_FX == true then
  99. if type(texture) ~= 'string' then
  100. texture = CROSSBOW_EXPLOSION_TEXTURE
  101. end
  102. local spread = {x=0.1, y=0.1, z=0.1}
  103. minetest.add_particlespawner({
  104. amount = 15,
  105. time = 0.3,
  106. minpos = vector.subtract(pos, spread),
  107. maxpos = vector.add(pos, spread),
  108. minvel = {x=-1, y=1, z=-1},
  109. maxvel = {x=1, y=2, z=1},
  110. minacc = {x=-2, y=-2, z=-2},
  111. maxacc = {x=2, y=-2, z=2},
  112. minexptime = 0.1,
  113. maxexptime = 0.75,
  114. minsize = 1,
  115. maxsize = 2,
  116. collisiondetection = false,
  117. texture = texture,
  118. })
  119. end
  120. end
  121. function crossbow:punch_node(pos, def)
  122. local node = minetest.get_node(pos)
  123. if not node then
  124. return
  125. end
  126. local item = minetest.registered_items[node.name]
  127. if not item then
  128. return
  129. end
  130. if CROSSBOW_ENABLE_PROTECTION then
  131. if minetest.is_protected(pos, def.name) then
  132. return
  133. end
  134. end
  135. if item.groups then
  136. for k, v in pairs(def.groups) do
  137. local level = item.groups[k] or 0
  138. if level >= v then
  139. minetest.remove_node(pos)
  140. if item.tiles then
  141. if item.tiles[1] then
  142. crossbow:spawn_particles(pos, item.tiles[1])
  143. end
  144. end
  145. break
  146. end
  147. end
  148. end
  149. end
  150. function crossbow:is_valid_object(object)
  151. if object then
  152. if object:is_player() == true then
  153. return CROSSBOW_ALLOW_PLAYERS
  154. end
  155. if CROSSBOW_ALLOW_ENTITIES == true then
  156. local luaentity = object:get_luaentity()
  157. if luaentity then
  158. if luaentity.name then
  159. if allowed_entities[luaentity.name] then
  160. return true
  161. end
  162. end
  163. end
  164. end
  165. end
  166. end
  167. function crossbow:get_intersect_pos(ray, plane, collisionbox)
  168. local v = vector.subtract(ray.pos, plane.pos)
  169. local r1 = get_dot_product(v, plane.normal)
  170. local r2 = get_dot_product(ray.dir, plane.normal)
  171. if r2 ~= 0 then
  172. local t = -(r1 / r2)
  173. local td = vector.multiply(ray.dir, {x=t, y=t, z=t})
  174. local pt = vector.add(ray.pos, td)
  175. local pd = vector.subtract(pt, plane.pos)
  176. if math.abs(pd.x) < collisionbox[4] and
  177. math.abs(pd.y) < collisionbox[5] and
  178. math.abs(pd.z) < collisionbox[6] then
  179. return pt
  180. end
  181. end
  182. end
  183. function crossbow:process_round(round)
  184. local target = {object=nil, distance=10000}
  185. local p1 = round.pos
  186. local v1 = round.ray
  187. for _,ref in ipairs(castle.objects) do
  188. local p2 = vector.add(ref.pos, ref.offset)
  189. if p1 and p2 and ref.name ~= round.name then
  190. local d = vector.distance(p1, p2)
  191. if d < round.def.step and d < target.distance then
  192. local ray = {pos=p1, dir=v1}
  193. local plane = {pos=p2, normal={x=-1, y=0, z=-1}}
  194. local pos = crossbow:get_intersect_pos(ray, plane, ref.collisionbox)
  195. if pos then
  196. target.object = ref.object
  197. target.pos = pos
  198. target.distance = d
  199. end
  200. end
  201. end
  202. end
  203. if target.object and target.pos then
  204. local success, pos = minetest.line_of_sight(p1, target.pos, 1)
  205. if success then
  206. local user = minetest.get_player_by_name(round.name)
  207. if user then
  208. target.object:punch(user, nil, round.def.tool_caps, v1)
  209. crossbow:spawn_particles(target.pos, CROSSBOW_EXPLOSION_TEXTURE)
  210. end
  211. return 1
  212. elseif pos and CROSSBOW_ALLOW_NODES == true then
  213. crossbow:punch_node(pos, round.def)
  214. return 1
  215. end
  216. elseif CROSSBOW_ALLOW_NODES == true then
  217. local d = round.def.step
  218. local p2 = vector.add(p1, vector.multiply(v1, {x=d, y=d, z=d}))
  219. local success, pos = minetest.line_of_sight(p1, p2, 1)
  220. if pos then
  221. crossbow:punch_node(pos, round.def)
  222. return 1
  223. end
  224. end
  225. end
  226. local function get_animation_frame(dir)
  227. local angle = math.atan(dir.y)
  228. local frame = 90 - math.floor(angle * 360 / math.pi)
  229. if frame < 1 then
  230. frame = 1
  231. elseif frame > 180 then
  232. frame = 180
  233. end
  234. return frame
  235. end
  236. local function get_target_pos(p1, p2, dir, offset)
  237. local d = vector.distance(p1, p2) - offset
  238. local td = vector.multiply(dir, {x=d, y=d, z=d})
  239. return vector.add(p1, td)
  240. end
  241. local function punch_object(puncher, object)
  242. if puncher and crossbow:is_valid_object(object) then
  243. if puncher ~= object then
  244. local dir = puncher:get_look_dir()
  245. local p1 = puncher:getpos()
  246. local p2 = object:getpos()
  247. local tpos = get_target_pos(p1, p2, dir, 0)
  248. crossbow:spawn_particles(tpos, CROSSBOW_EXPLOSION_TEXTURE)
  249. object:punch(puncher, nil, CROSSBOW_BOLT_TOOL_CAPS, dir)
  250. end
  251. end
  252. end
  253. local function stop_crossbow_bolt(object, pos, stuck)
  254. local acceleration = {x=0, y=-10, z=0}
  255. if stuck == true then
  256. pos = pos or object:getpos()
  257. acceleration = {x=0, y=0, z=0}
  258. object:moveto(pos)
  259. end
  260. object:set_properties({
  261. physical = true,
  262. collisionbox = {-1/8,-1/8,-1/8, 1/8,1/8,1/8},
  263. })
  264. object:setvelocity({x=0, y=0, z=0})
  265. object:setacceleration(acceleration)
  266. end
  267. minetest.register_craftitem('castle_weapons:crossbow_bolt', {
  268. description = S('Bolt'),
  269. inventory_image = 'castle_crossbow_bolt_inv.png',
  270. })
  271. minetest.register_entity('castle_weapons:crossbow_bolt_entity', {
  272. physical = false,
  273. visual = 'mesh',
  274. mesh = 'castle_crossbow_bolt.b3d',
  275. visual_size = {x=1.0, y=1.0},
  276. textures = {
  277. 'castle_crossbow_bolt_uv.png'
  278. },
  279. timer = 0,
  280. lifetime = CROSSBOW_BOLT_LIFETIME,
  281. player = nil,
  282. state = 'init',
  283. node_pos = nil,
  284. collisionbox = {0,0,0, 0,0,0},
  285. on_activate = function(self, staticdata)
  286. self.object:set_armor_groups({immortal=1})
  287. if staticdata == 'expired' then
  288. self.object:remove()
  289. end
  290. end,
  291. on_punch = function(self, puncher)
  292. if puncher then
  293. if puncher:is_player() then
  294. local stack = 'castle_weapons:crossbow_bolt'
  295. local inv = puncher:get_inventory()
  296. if inv:room_for_item('main', stack) then
  297. inv:add_item('main', stack)
  298. self.object:remove()
  299. end
  300. end
  301. end
  302. end,
  303. on_step = function(self, dtime)
  304. if self.state == 'init' then
  305. return
  306. end
  307. self.timer = self.timer + dtime
  308. self.lifetime = self.lifetime - dtime
  309. if self.lifetime < 0 then
  310. self.object:remove()
  311. return
  312. elseif self.state == 'dropped' then
  313. return
  314. elseif self.state == 'stuck' then
  315. if self.timer > 1 then
  316. if self.node_pos then
  317. local node = minetest.get_node(self.node_pos)
  318. if node.name then
  319. local item = minetest.registered_items[node.name]
  320. if item then
  321. if not item.walkable then
  322. self.state = 'dropped'
  323. stop_crossbow_bolt(self.object)
  324. return
  325. end
  326. end
  327. end
  328. end
  329. self.timer = 0
  330. end
  331. return
  332. end
  333. if self.timer > 0.2 then
  334. local pos = self.object:getpos()
  335. local dir = vector.normalize(self.object:getvelocity())
  336. local frame = get_animation_frame(dir)
  337. self.object:set_animation({x=frame, y=frame}, 0)
  338. local objects = minetest.get_objects_inside_radius(pos, 5)
  339. for _,obj in ipairs(objects) do
  340. if crossbow:is_valid_object(obj) then
  341. local collisionbox = {-0.25,-1.0,-0.25, 0.25,0.8,0.25}
  342. local offset = CROSSBOW_PLAYER_OFFSET
  343. if not obj:is_player() then
  344. offset = CROSSBOW_ENTITY_OFFSET
  345. local ent = obj:get_luaentity()
  346. if ent then
  347. local def = minetest.registered_entities[ent.name]
  348. collisionbox = def.collisionbox or collisionbox
  349. end
  350. end
  351. local opos = vector.add(obj:getpos(), offset)
  352. local ray = {pos=pos, dir=dir}
  353. local plane = {pos=opos, normal={x=-1, y=0, z=-1}}
  354. local ipos = crossbow:get_intersect_pos(ray, plane, collisionbox)
  355. if ipos then
  356. punch_object(self.player, obj)
  357. end
  358. end
  359. end
  360. local p = vector.add(pos, vector.multiply(dir, {x=5, y=5, z=5}))
  361. local _, npos = minetest.line_of_sight(pos, p, 1)
  362. if npos then
  363. local node = minetest.get_node(npos)
  364. local tpos = get_target_pos(pos, npos, dir, 0.66)
  365. self.node_pos = npos
  366. self.state = 'stuck'
  367. stop_crossbow_bolt(self.object, tpos, true)
  368. minetest.sound_play('castle_crossbow_bolt', {gain = 0.08, max_hear_distance = 2})
  369. end
  370. self.timer = 0
  371. end
  372. end,
  373. get_staticdata = function(self)
  374. return 'expired'
  375. end,
  376. })
  377. minetest.register_tool('castle_weapons:crossbow_loaded', {
  378. description = S('Crossbow'),
  379. inventory_image = 'castle_crossbow_loaded.png',
  380. groups = {not_in_creative_inventory=1},
  381. on_use = function(itemstack, user, pointed_thing)
  382. minetest.sound_play('castle_crossbow_click', {object=user})
  383. if not minetest.settings:get_bool('creative_mode') then
  384. itemstack:add_wear(65535/CROSSBOW_USES)
  385. end
  386. itemstack = 'castle_weapons:crossbow 1 '..itemstack:get_wear()
  387. local pos = user:getpos()
  388. local dir = user:get_look_dir()
  389. local yaw = user:get_look_yaw()
  390. if pos and dir and yaw then
  391. pos.y = pos.y + 1.5
  392. local obj = minetest.add_entity(pos, 'castle_weapons:crossbow_bolt_entity')
  393. local ent = nil
  394. if obj then
  395. ent = obj:get_luaentity()
  396. end
  397. if ent then
  398. obj:set_properties({
  399. textures = {'castle_crossbow_bolt_uv.png'}
  400. })
  401. minetest.sound_play('castle_crossbow_shoot', {object=obj})
  402. local frame = get_animation_frame(dir)
  403. obj:setyaw(yaw + math.pi)
  404. obj:set_animation({x=frame, y=frame}, 0)
  405. obj:setvelocity({x=dir.x * 20, y=dir.y * 20, z=dir.z * 20})
  406. if pointed_thing.type ~= 'nothing' then
  407. local ppos = minetest.get_pointed_thing_position(pointed_thing, false)
  408. local _, npos = minetest.line_of_sight(pos, ppos, 1)
  409. if npos then
  410. ppos = npos
  411. pointed_thing.type = 'node'
  412. end
  413. if pointed_thing.type == 'object' then
  414. punch_object(user, pointed_thing.ref)
  415. elseif pointed_thing.type == 'node' then
  416. local node = minetest.get_node(ppos)
  417. local tpos = get_target_pos(pos, ppos, dir, 0.66)
  418. minetest.after(0.2, function(object, pos, npos)
  419. ent.node_pos = npos
  420. ent.state = 'stuck'
  421. stop_crossbow_bolt(object, pos, true)
  422. minetest.sound_play('castle_crossbow_bolt', {gain = 0.08, max_hear_distance = 2})
  423. end, obj, tpos, ppos)
  424. return itemstack
  425. end
  426. end
  427. obj:setacceleration({x=dir.x * -3, y=-5, z=dir.z * -3})
  428. ent.player = ent.player or user
  429. ent.state = 'flight'
  430. end
  431. end
  432. return itemstack
  433. end,
  434. })
  435. minetest.register_tool('castle_weapons:crossbow', {
  436. description = S('Crossbow'),
  437. inventory_image = 'castle_crossbow_inv.png',
  438. on_use = function(itemstack, user, pointed_thing)
  439. local inv = user:get_inventory()
  440. if inv:contains_item('main', 'castle_weapons:crossbow_bolt') then
  441. minetest.sound_play('castle_crossbow_reload', {object=user})
  442. if not minetest.settings:get_bool('creative_mode') then
  443. inv:remove_item('main', 'castle_weapons:crossbow_bolt 1')
  444. end
  445. return 'castle_weapons:crossbow_loaded 1 '..itemstack:get_wear()
  446. end
  447. minetest.sound_play('castle_crossbow_click', {object=user})
  448. end,
  449. })
  450. -----------
  451. --Crafting
  452. -----------
  453. minetest.register_craft({
  454. output = 'castle_weapons:crossbow',
  455. recipe = {
  456. {'default:steel_ingot', 'default:stick', 'default:steel_ingot'},
  457. {'farming:string', 'farming:string', 'farming:string'},
  458. {'', 'default:stick', ''},
  459. }
  460. })
  461. minetest.register_craft({
  462. output = 'castle_weapons:crossbow_bolt',
  463. recipe = {
  464. {'epic:arrow_tip', 'default:stick'},
  465. }
  466. })