Implement stuck arrow timeout properly
[MineClone/MineClone2.git] / mods / ITEMS / mcl_bows / arrow.lua
blob442ceb219ae74879f812a5eae202ff73d1ececd8
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
6 local GRAVITY = 9.81
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)
23 -- Shoot arrow
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)
27 end,
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", {
34 drawtype = "nodebox",
35 is_ground_content = false,
36 node_box = {
37 type = "fixed",
38 fixed = {
39 -- Shaft
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},
43 -- Tip
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},
46 -- Fletching
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"},
59 paramtype = "light",
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.
67 local ARROW_ENTITY={
68 physical = true,
69 visual = "wielditem",
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,
75 _lastpos={},
76 _startpos=nil,
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())
91 end
92 self.object:remove()
93 end
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)
101 if self._stuck then
102 self._stucktimer = self._stucktimer + dtime
103 self._stuckrechecktimer = self._stuckrechecktimer + dtime
104 if self._stucktimer > ARROW_TIMEOUT then
105 self.object:remove()
106 return
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
111 local stuckin_def
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)
118 return
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", {
130 pos = pos,
131 max_hear_distance = 16,
132 gain = 1.0,
134 self.object:remove()
135 return
137 else
138 self.object:remove()
139 return
144 -- Check for object collision. Done every tick (hopefully this is not too stressing)
145 else
146 local objs = minetest.get_objects_inside_radius(pos, 2)
147 local closest_object
148 local closest_distance
149 local ok = false
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
155 ok = true
156 elseif obj:get_luaentity() ~= nil then
157 if obj ~= self._shooter and obj:get_luaentity()._cmi_is_mob then
158 ok = true
162 if ok then
163 local dist = vector.distance(pos, obj:getpos())
164 if not closest_object or not closest_distance then
165 closest_object = obj
166 closest_distance = dist
167 elseif dist < closest_distance then
168 closest_object = obj
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},
183 }, nil)
185 if is_player then
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)
195 if lua then
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")
206 self.object:remove()
207 return
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
219 local dir
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}
223 else
224 dir = {x=0, y=-1, z=0}
226 else
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
236 self._stuckin = nil
237 -- Lose 1/3 of velocity on deflection
238 self.object:set_velocity(vector.multiply(vel, 0.6667))
239 return
242 -- Node was walkable, make arrow stuck
243 self._stuck = true
244 self._stucktimer = 0
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
257 if not v then
258 v = 0
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)
271 -- Update yaw
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)
284 if self._stuck then
285 self._stuckrechecktimer = STUCK_RECHECK_TIME
289 ARROW_ENTITY.get_staticdata = function(self)
290 local out = {
291 lastpos = self._lastpos,
292 startpos = self._startpos,
293 damage = self._damage,
294 stuck = self._stuck,
295 stuckin = self._stuckin,
297 if self._stuck then
298 -- If _stucktimer is missing for some reason, assume the maximum
299 if not self._stucktimer then
300 self._stucktimer = ARROW_TIMEOUT
302 out.stuckstarttime = minetest.get_gametime() - self._stucktimer
304 if self._shooter and self._shooter:is_player() then
305 out.shootername = self._shooter:get_player_name()
307 return minetest.serialize(out)
310 ARROW_ENTITY.on_activate = function(self, staticdata, dtime_s)
311 local data = minetest.deserialize(staticdata)
312 if data then
313 self._stuck = data.stuck
314 if data.stuck then
315 if data.stuckstarttime then
316 -- First, check if the stuck arrow is aleady past its life timer.
317 -- If yes, delete it.
318 self._stucktimer = minetest.get_gametime() - data.stuckstarttime
319 if self._stucktimer > ARROW_TIMEOUT then
320 self.object:remove()
321 return
325 -- Perform a stuck recheck on the next step.
326 self._stuckrechecktimer = STUCK_RECHECK_TIME
328 self._stuckin = data.stuckin
331 -- Get the remaining arrow state
332 self._lastpos = data.lastpos
333 self._startpos = data.startpos
334 self._damage = data.damage
335 if data.shootername then
336 local shooter = minetest.get_player_by_name(data.shootername)
337 if shooter and shooter:is_player() then
338 self._shooter = shooter
344 minetest.register_entity("mcl_bows:arrow_entity", ARROW_ENTITY)
346 if minetest.get_modpath("mcl_core") and minetest.get_modpath("mcl_mobitems") then
347 minetest.register_craft({
348 output = 'mcl_bows:arrow 4',
349 recipe = {
350 {'mcl_core:flint'},
351 {'mcl_core:stick'},
352 {'mcl_mobitems:feather'}
357 if minetest.get_modpath("doc_identifier") ~= nil then
358 doc.sub.identifier.register_object("mcl_bows:arrow_entity", "craftitems", "mcl_bows:arrow")