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},
65 -- FIXME: Arrow velocity is a bit strange. If the arrow flies VERY long, the acceleration can cause the velocity to become negative
66 -- and the arrow flies backwards.
70 visual_size
= {x
=0.4, y
=0.4},
71 textures
= {"mcl_bows:arrow_box"},
72 collisionbox
= {-0.19, -0.125, -0.19, 0.19, 0.125, 0.19},
73 collide_with_objects
= false,
77 _damage
=1, -- Damage on impact
78 _stuck
=false, -- Whether arrow is stuck
79 _stucktimer
=nil,-- Amount of time (in seconds) the arrow has been stuck so far
80 _stuckrechecktimer
=nil,-- An additional timer for periodically re-checking the stuck status of an arrow
81 _stuckin
=nil, --Position of node in which arow is stuck.
82 _shooter
=nil, -- ObjectRef of player or mob who shot it
85 -- Destroy arrow entity self at pos and drops it as an item
86 local spawn_item
= function(self
, pos
)
87 if not minetest
.settings
:get_bool("creative_mode") then
88 local item
= minetest
.add_item(pos
, "mcl_bows:arrow")
89 item
:set_velocity({x
=0, y
=0, z
=0})
90 item
:set_yaw(self
.object
:get_yaw())
95 ARROW_ENTITY
.on_step
= function(self
, dtime
)
96 local pos
= self
.object
:getpos()
97 local dpos
= table.copy(pos
) -- digital pos
98 dpos
= vector
.round(dpos
)
99 local node
= minetest
.get_node(dpos
)
102 self
._stucktimer
= self
._stucktimer
+ dtime
103 self
._stuckrechecktimer
= self
._stuckrechecktimer
+ dtime
104 if self
._stucktimer
> ARROW_TIMEOUT
then
108 -- Drop arrow as item when it is no longer stuck
109 -- FIXME: Arrows are a bit slot to react and continue to float in mid air for a few seconds.
110 if self
._stuckrechecktimer
> STUCK_RECHECK_TIME
then
112 if self
._stuckin
then
113 stuckin_def
= minetest
.registered_nodes
[minetest
.get_node(self
._stuckin
).name
]
115 -- TODO: In MC, arrow just falls down without turning into an item
116 if stuckin_def
and stuckin_def
.walkable
== false then
117 spawn_item(self
, pos
)
120 self
._stuckrechecktimer
= 0
122 local objects
= minetest
.get_objects_inside_radius(pos
, 2)
123 for _
,obj
in ipairs(objects
) do
124 if obj
:is_player() then
125 if not minetest
.settings
:get_bool("creative_mode") then
126 -- Pickup arrow if player is nearby
127 if obj
:get_inventory():room_for_item("main", "mcl_bows:arrow") then
128 obj
:get_inventory():add_item("main", "mcl_bows:arrow")
129 minetest
.sound_play("item_drop_pickup", {
131 max_hear_distance
= 16,
144 -- Check for object collision. Done every tick (hopefully this is not too stressing)
146 local objs
= minetest
.get_objects_inside_radius(pos
, 2)
148 local closest_distance
151 -- Iterate through all objects and remember the closest attackable object
152 for k
, obj
in pairs(objs
) do
153 -- Arrows can only damage players and mobs
154 if obj
~= self
._shooter
and obj
:is_player() then
156 elseif obj
:get_luaentity() ~= nil then
157 if obj
~= self
._shooter
and obj
:get_luaentity()._cmi_is_mob
then
163 local dist
= vector
.distance(pos
, obj
:getpos())
164 if not closest_object
or not closest_distance
then
166 closest_distance
= dist
167 elseif dist
< closest_distance
then
169 closest_distance
= dist
174 -- If an attackable object was found, we will damage the closest one only
175 if closest_object
~= nil then
176 local obj
= closest_object
177 local is_player
= obj
:is_player()
178 local lua
= obj
:get_luaentity()
179 if obj
~= self
._shooter
and (is_player
or (lua
and lua
._cmi_is_mob
)) then
180 obj
:punch(self
.object
, 1.0, {
181 full_punch_interval
=1.0,
182 damage_groups
={fleshy
=self
._damage
},
186 if self
._shooter
and self
._shooter
:is_player() then
187 -- “Ding” sound for hitting another player
188 minetest
.sound_play({name
="mcl_bows_hit_player", gain
=0.1}, {to_player
=self
._shooter
})
190 if mod_mcl_hunger
then
191 mcl_hunger
.exhaust(obj
:get_player_name(), mcl_hunger
.EXHAUST_DAMAGE
)
196 local entity_name
= lua
.name
197 -- Achievement for hitting skeleton, wither skeleton or stray (TODO) with an arrow at least 50 meters away
198 -- NOTE: Range has been reduced because mobs unload much earlier than that ... >_>
199 -- TODO: This achievement should be given for the kill, not just a hit
200 if self
._shooter
and self
._shooter
:is_player() and vector
.distance(pos
, self
._startpos
) >= 20 then
201 if mod_awards
and (entity_name
== "mobs_mc:skeleton" or entity_name
== "mobs_mc:stray" or entity_name
== "mobs_mc:witherskeleton") then
202 awards
.unlock(self
._shooter
:get_player_name(), "mcl:snipeSkeleton")
212 -- Check for node collision
213 if self
._lastpos
.x
~=nil and not self
._stuck
then
214 local def
= minetest
.registered_nodes
[node
.name
]
215 local vel
= self
.object
:get_velocity()
216 -- Arrow has stopped in one axis, so it probably hit something
217 if (math
.abs(vel
.x
) < 0.0001) or (math
.abs(vel
.z
) < 0.0001) or (math
.abs(vel
.y
) < 0.00001) then
218 -- Check for the node to which the arrow is pointing
220 if math
.abs(vel
.y
) < 0.00001 then
221 if self
._lastpos
.y
< pos
.y
then
222 dir
= {x
=0, y
=1, z
=0}
224 dir
= {x
=0, y
=-1, z
=0}
227 dir
= minetest
.facedir_to_dir(minetest
.dir_to_facedir(minetest
.yaw_to_dir(self
.object
:get_yaw()-YAW_OFFSET
)))
229 self
._stuckin
= vector
.add(pos
, dir
)
230 local snode
= minetest
.get_node(self
._stuckin
)
231 local sdef
= minetest
.registered_nodes
[snode
.name
]
233 -- If node is non-walkable, unknown or ignore, don't make arrow stuck.
234 -- This causes a deflection in the engine.
235 if not sdef
or sdef
.walkable
== false or snode
.name
== "ignore" then
237 -- Lose 1/3 of velocity on deflection
238 self
.object
:set_velocity(vector
.multiply(vel
, 0.6667))
242 -- Node was walkable, make arrow stuck
245 self
._stuckrechecktimer
= 0
247 self
.object
:set_velocity({x
=0, y
=0, z
=0})
248 self
.object
:set_acceleration({x
=0, y
=0, z
=0})
250 -- Push the button! Push, push, push the button!
251 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
252 mesecon
.push_button(dpos
, node
)
254 elseif (def
and def
.liquidtype
~= "none") then
255 -- Slow down arrow in liquids
256 local v
= def
.liquid_viscosity
260 local vpenalty
= math
.max(0.1, 0.98 - 0.1 * v
)
261 if math
.abs(vel
.x
) > 0.001 then
262 vel
.x
= vel
.x
* vpenalty
264 if math
.abs(vel
.z
) > 0.001 then
265 vel
.z
= vel
.z
* vpenalty
267 self
.object
:set_velocity(vel
)
272 if not self
._stuck
then
273 local vel
= self
.object
:get_velocity()
274 self
.object
:set_yaw(minetest
.dir_to_yaw(vel
)+YAW_OFFSET
)
277 -- Update internal variable
278 self
._lastpos
={x
=pos
.x
, y
=pos
.y
, z
=pos
.z
}
281 -- Force recheck of stuck arrows when punched.
282 -- Otherwise, punching has no effect.
283 ARROW_ENTITY
.on_punch
= function(self
)
285 self
._stuckrechecktimer
= STUCK_RECHECK_TIME
289 ARROW_ENTITY
.get_staticdata
= function(self
)
291 lastpos
= self
._lastpos
,
292 startpos
= self
._startpos
,
293 damage
= self
._damage
,
295 stucktimer
= self
._stucktimer
,
296 stuckin
= self
._stuckin
,
298 if self
._shooter
and self
._shooter
:is_player() then
299 out
.shootername
= self
._shooter
:get_player_name()
301 return minetest
.serialize(out
)
304 ARROW_ENTITY
.on_activate
= function(self
, staticdata
, dtime_s
)
305 local data
= minetest
.deserialize(staticdata
)
307 self
._lastpos
= data
.lastpos
308 self
._startpos
= data
.startpos
309 self
._damage
= data
.damage
310 self
._stuck
= data
.stuck
312 -- Perform a stuck recheck on the next step
313 self
._stuckrechecktimer
= STUCK_RECHECK_TIME
315 self
._stucktimer
= data
.stucktimer
316 self
._stuckin
= data
.stuckin
317 if data
.shootername
then
318 local shooter
= minetest
.get_player_by_name(data
.shootername
)
319 if shooter
and shooter
:is_player() then
320 self
._shooter
= shooter
326 minetest
.register_entity("mcl_bows:arrow_entity", ARROW_ENTITY
)
328 if minetest
.get_modpath("mcl_core") and minetest
.get_modpath("mcl_mobitems") then
329 minetest
.register_craft({
330 output
= 'mcl_bows:arrow 4',
334 {'mcl_mobitems:feather'}
339 if minetest
.get_modpath("doc_identifier") ~= nil then
340 doc
.sub
.identifier
.register_object("mcl_bows:arrow_entity", "craftitems", "mcl_bows:arrow")