1 -- Time in seconds after which a stuck arrow is deleted
2 local ARROW_TIMEOUT
= 60
3 -- Time after which stuck arrow is rechecked for being stuck
4 local STUCK_RECHECK_TIME
= 5
8 local YAW_OFFSET
= -math
.pi
/2
10 local mod_mcl_hunger
= minetest
.get_modpath("mcl_hunger")
11 local mod_awards
= minetest
.get_modpath("awards") and minetest
.get_modpath("mcl_achievements")
12 local mod_button
= minetest
.get_modpath("mesecons_button")
14 minetest
.register_craftitem("mcl_bows:arrow", {
15 description
= "Arrow",
16 _doc_items_longdesc
= [[Arrows are ammunition for bows and dispensers.
17 An arrow fired from a bow has a regular damage of 1-9. At full charge, there's a 20% chance of a critical hit dealing 10 damage instead. An arrow fired from a dispenser always deals 3 damage.
18 Arrows might get stuck on solid blocks and can be retrieved again. They are also capable of pushing wooden buttons.]],
19 _doc_items_usagehelp
= "To use arrows as ammunition for a bow, just put them anywhere in your inventory, they will be used up automatically. To use arrows as ammunition for a dispenser, place them in the dispenser's inventory. To retrieve an arrow that sticks in a block, simply walk close to it.",
20 inventory_image
= "mcl_bows_arrow_inv.png",
21 groups
= { ammo
=1, ammo_bow
=1 },
22 _on_dispense
= function(itemstack
, dispenserpos
, droppos
, dropnode
, dropdir
)
24 local shootpos
= vector
.add(dispenserpos
, vector
.multiply(dropdir
, 0.51))
25 local yaw
= math
.atan2(dropdir
.z
, dropdir
.x
) + YAW_OFFSET
26 mcl_bows
.shoot_arrow(itemstack
:get_name(), shootpos
, dropdir
, yaw
, nil, 19, 3)
30 -- This is a fake node, used as model for the arrow entity.
31 -- It's not supposed to be usable as item or real node.
32 -- TODO: Use a proper mesh for the arrow entity
33 minetest
.register_node("mcl_bows:arrow_box", {
35 is_ground_content
= false,
40 {-6.5/17, -1.5/17, -1.5/17, -4.5/17, 1.5/17, 1.5/17},
41 {-4.5/17, -0.5/17, -0.5/17, 5.5/17, 0.5/17, 0.5/17},
42 {5.5/17, -1.5/17, -1.5/17, 6.5/17, 1.5/17, 1.5/17},
44 {-4.5/17, 2.5/17, 2.5/17, -3.5/17, -2.5/17, -2.5/17},
45 {-8.5/17, 0.5/17, 0.5/17, -6.5/17, -0.5/17, -0.5/17},
47 {6.5/17, 1.5/17, 1.5/17, 7.5/17, 2.5/17, 2.5/17},
48 {7.5/17, -2.5/17, 2.5/17, 6.5/17, -1.5/17, 1.5/17},
49 {7.5/17, 2.5/17, -2.5/17, 6.5/17, 1.5/17, -1.5/17},
50 {6.5/17, -1.5/17, -1.5/17, 7.5/17, -2.5/17, -2.5/17},
52 {7.5/17, 2.5/17, 2.5/17, 8.5/17, 3.5/17, 3.5/17},
53 {8.5/17, -3.5/17, 3.5/17, 7.5/17, -2.5/17, 2.5/17},
54 {8.5/17, 3.5/17, -3.5/17, 7.5/17, 2.5/17, -2.5/17},
55 {7.5/17, -2.5/17, -2.5/17, 8.5/17, -3.5/17, -3.5/17},
58 tiles
= {"mcl_bows_arrow.png^[transformFX", "mcl_bows_arrow.png^[transformFX", "mcl_bows_arrow_back.png", "mcl_bows_arrow_front.png", "mcl_bows_arrow.png", "mcl_bows_arrow.png^[transformFX"},
60 paramtype2
= "facedir",
61 sunlight_propagates
= true,
62 groups
= {not_in_creative_inventory
=1, dig_immediate
=3},
68 visual_size
= {x
=0.4, y
=0.4},
69 textures
= {"mcl_bows:arrow_box"},
70 collisionbox
= {-0.19, -0.125, -0.19, 0.19, 0.125, 0.19},
71 collide_with_objects
= false,
75 _damage
=1, -- Damage on impact
76 _stuck
=false, -- Whether arrow is stuck
77 _stucktimer
=nil,-- Amount of time (in seconds) the arrow has been stuck so far
78 _stuckrechecktimer
=nil,-- An additional timer for periodically re-checking the stuck status of an arrow
79 _stuckin
=nil, --Position of node in which arow is stuck.
80 _shooter
=nil, -- ObjectRef of player or mob who shot it
82 _viscosity
=0, -- Viscosity of node the arrow is currently in
83 _deflection_cooloff
=0, -- Cooloff timer after an arrow deflection, to prevent many deflections in quick succession
86 -- Destroy arrow entity self at pos and drops it as an item
87 local spawn_item
= function(self
, pos
)
88 if not minetest
.settings
:get_bool("creative_mode") then
89 local item
= minetest
.add_item(pos
, "mcl_bows:arrow")
90 item
:set_velocity({x
=0, y
=0, z
=0})
91 item
:set_yaw(self
.object
:get_yaw())
96 ARROW_ENTITY
.on_step
= function(self
, dtime
)
97 local pos
= self
.object
:getpos()
98 local dpos
= table.copy(pos
) -- digital pos
99 dpos
= vector
.round(dpos
)
100 local node
= minetest
.get_node(dpos
)
103 self
._stucktimer
= self
._stucktimer
+ dtime
104 self
._stuckrechecktimer
= self
._stuckrechecktimer
+ dtime
105 if self
._stucktimer
> ARROW_TIMEOUT
then
109 -- Drop arrow as item when it is no longer stuck
110 -- FIXME: Arrows are a bit slot to react and continue to float in mid air for a few seconds.
111 if self
._stuckrechecktimer
> STUCK_RECHECK_TIME
then
113 if self
._stuckin
then
114 stuckin_def
= minetest
.registered_nodes
[minetest
.get_node(self
._stuckin
).name
]
116 -- TODO: In MC, arrow just falls down without turning into an item
117 if stuckin_def
and stuckin_def
.walkable
== false then
118 spawn_item(self
, pos
)
121 self
._stuckrechecktimer
= 0
123 -- Pickup arrow if player is nearby (not in Creative Mode)
124 local objects
= minetest
.get_objects_inside_radius(pos
, 1)
125 for _
,obj
in ipairs(objects
) do
126 if obj
:is_player() then
127 if not minetest
.settings
:get_bool("creative_mode") then
128 if obj
:get_inventory():room_for_item("main", "mcl_bows:arrow") then
129 obj
:get_inventory():add_item("main", "mcl_bows:arrow")
130 minetest
.sound_play("item_drop_pickup", {
132 max_hear_distance
= 16,
142 -- Check for object "collision". Done every tick (hopefully this is not too stressing)
144 -- We just check for any hurtable objects nearby.
145 -- The radius of 3 is fairly liberal, but anything lower than than will cause
146 -- arrow to hilariously go through mobs often.
147 -- TODO: Implement an ACTUAL collision detection (engine support needed).
148 local objs
= minetest
.get_objects_inside_radius(pos
, 3)
150 local closest_distance
152 if self
._deflection_cooloff
> 0 then
153 self
._deflection_cooloff
= self
._deflection_cooloff
- dtime
156 -- Iterate through all objects and remember the closest attackable object
157 for k
, obj
in pairs(objs
) do
159 -- Arrows can only damage players and mobs
160 if obj
~= self
._shooter
and obj
:is_player() then
162 elseif obj
:get_luaentity() ~= nil then
163 if obj
~= self
._shooter
and obj
:get_luaentity()._cmi_is_mob
then
169 local dist
= vector
.distance(pos
, obj
:getpos())
170 if not closest_object
or not closest_distance
then
172 closest_distance
= dist
173 elseif dist
< closest_distance
then
175 closest_distance
= dist
180 -- If an attackable object was found, we will damage the closest one only
181 if closest_object
~= nil then
182 local obj
= closest_object
183 local is_player
= obj
:is_player()
184 local lua
= obj
:get_luaentity()
185 if obj
~= self
._shooter
and (is_player
or (lua
and lua
._cmi_is_mob
)) then
186 obj
:punch(self
.object
, 1.0, {
187 full_punch_interval
=1.0,
188 damage_groups
={fleshy
=self
._damage
},
192 if self
._shooter
and self
._shooter
:is_player() then
193 -- “Ding” sound for hitting another player
194 minetest
.sound_play({name
="mcl_bows_hit_player", gain
=0.1}, {to_player
=self
._shooter
})
196 if mod_mcl_hunger
then
197 mcl_hunger
.exhaust(obj
:get_player_name(), mcl_hunger
.EXHAUST_DAMAGE
)
202 local entity_name
= lua
.name
203 -- Achievement for hitting skeleton, wither skeleton or stray (TODO) with an arrow at least 50 meters away
204 -- NOTE: Range has been reduced because mobs unload much earlier than that ... >_>
205 -- TODO: This achievement should be given for the kill, not just a hit
206 if self
._shooter
and self
._shooter
:is_player() and vector
.distance(pos
, self
._startpos
) >= 20 then
207 if mod_awards
and (entity_name
== "mobs_mc:skeleton" or entity_name
== "mobs_mc:stray" or entity_name
== "mobs_mc:witherskeleton") then
208 awards
.unlock(self
._shooter
:get_player_name(), "mcl:snipeSkeleton")
218 -- Check for node collision
219 if self
._lastpos
.x
~=nil and not self
._stuck
then
220 local def
= minetest
.registered_nodes
[node
.name
]
221 local vel
= self
.object
:get_velocity()
222 -- Arrow has stopped in one axis, so it probably hit something.
223 -- This detection is a bit clunky, but sadly, MT does not offer a direct collision detection for us. :-(
224 if (math
.abs(vel
.x
) < 0.0001) or (math
.abs(vel
.z
) < 0.0001) or (math
.abs(vel
.y
) < 0.00001) then
225 -- Check for the node to which the arrow is pointing
227 if math
.abs(vel
.y
) < 0.00001 then
228 if self
._lastpos
.y
< pos
.y
then
229 dir
= {x
=0, y
=1, z
=0}
231 dir
= {x
=0, y
=-1, z
=0}
234 dir
= minetest
.facedir_to_dir(minetest
.dir_to_facedir(minetest
.yaw_to_dir(self
.object
:get_yaw()-YAW_OFFSET
)))
236 self
._stuckin
= vector
.add(dpos
, dir
)
237 local snode
= minetest
.get_node(self
._stuckin
)
238 local sdef
= minetest
.registered_nodes
[snode
.name
]
240 -- If node is non-walkable, unknown or ignore, don't make arrow stuck.
241 -- This causes a deflection in the engine.
242 if not sdef
or sdef
.walkable
== false or snode
.name
== "ignore" then
244 if self
._deflection_cooloff
<= 0 then
245 -- Lose 1/3 of velocity on deflection
246 local newvel
= vector
.multiply(vel
, 0.6667)
248 self
.object
:set_velocity(newvel
)
249 -- Reset deflection cooloff timer to prevent many deflections happening in quick succession
250 self
._deflection_cooloff
= 1.0
254 -- Node was walkable, make arrow stuck
257 self
._stuckrechecktimer
= 0
259 self
.object
:set_velocity({x
=0, y
=0, z
=0})
260 self
.object
:set_acceleration({x
=0, y
=0, z
=0})
262 -- Push the button! Push, push, push the button!
263 if mod_button
and minetest
.get_item_group(node
.name
, "button") > 0 and minetest
.get_item_group(node
.name
, "button_push_by_arrow") == 1 then
264 local bdir
= minetest
.wallmounted_to_dir(node
.param2
)
265 -- Check the button orientation
266 if vector
.equals(vector
.add(dpos
, bdir
), self
._stuckin
) then
267 mesecon
.push_button(dpos
, node
)
271 elseif (def
and def
.liquidtype
~= "none") then
272 -- Slow down arrow in liquids
273 local v
= def
.liquid_viscosity
277 local old_v
= self
._viscosity
279 local vpenalty
= math
.max(0.1, 0.98 - 0.1 * v
)
280 if math
.abs(vel
.x
) > 0.001 then
281 vel
.x
= vel
.x
* vpenalty
283 if math
.abs(vel
.z
) > 0.001 then
284 vel
.z
= vel
.z
* vpenalty
286 self
.object
:set_velocity(vel
)
291 if not self
._stuck
then
292 local vel
= self
.object
:get_velocity()
293 self
.object
:set_yaw(minetest
.dir_to_yaw(vel
)+YAW_OFFSET
)
296 -- Update internal variable
297 self
._lastpos
={x
=pos
.x
, y
=pos
.y
, z
=pos
.z
}
300 -- Force recheck of stuck arrows when punched.
301 -- Otherwise, punching has no effect.
302 ARROW_ENTITY
.on_punch
= function(self
)
304 self
._stuckrechecktimer
= STUCK_RECHECK_TIME
308 ARROW_ENTITY
.get_staticdata
= function(self
)
310 lastpos
= self
._lastpos
,
311 startpos
= self
._startpos
,
312 damage
= self
._damage
,
314 stuckin
= self
._stuckin
,
317 -- If _stucktimer is missing for some reason, assume the maximum
318 if not self
._stucktimer
then
319 self
._stucktimer
= ARROW_TIMEOUT
321 out
.stuckstarttime
= minetest
.get_gametime() - self
._stucktimer
323 if self
._shooter
and self
._shooter
:is_player() then
324 out
.shootername
= self
._shooter
:get_player_name()
326 return minetest
.serialize(out
)
329 ARROW_ENTITY
.on_activate
= function(self
, staticdata
, dtime_s
)
330 local data
= minetest
.deserialize(staticdata
)
332 self
._stuck
= data
.stuck
334 if data
.stuckstarttime
then
335 -- First, check if the stuck arrow is aleady past its life timer.
336 -- If yes, delete it.
337 self
._stucktimer
= minetest
.get_gametime() - data
.stuckstarttime
338 if self
._stucktimer
> ARROW_TIMEOUT
then
344 -- Perform a stuck recheck on the next step.
345 self
._stuckrechecktimer
= STUCK_RECHECK_TIME
347 self
._stuckin
= data
.stuckin
350 -- Get the remaining arrow state
351 self
._lastpos
= data
.lastpos
352 self
._startpos
= data
.startpos
353 self
._damage
= data
.damage
354 if data
.shootername
then
355 local shooter
= minetest
.get_player_by_name(data
.shootername
)
356 if shooter
and shooter
:is_player() then
357 self
._shooter
= shooter
361 self
.object
:set_armor_groups({ immortal
= 1 })
364 minetest
.register_entity("mcl_bows:arrow_entity", ARROW_ENTITY
)
366 if minetest
.get_modpath("mcl_core") and minetest
.get_modpath("mcl_mobitems") then
367 minetest
.register_craft({
368 output
= 'mcl_bows:arrow 4',
372 {'mcl_mobitems:feather'}
377 if minetest
.get_modpath("doc_identifier") ~= nil then
378 doc
.sub
.identifier
.register_object("mcl_bows:arrow_entity", "craftitems", "mcl_bows:arrow")