Make arrow stuck when they hit solid node (WIP)
[MineClone/MineClone2.git] / mods / ITEMS / mcl_bows / arrow.lua
blob27de706823874e4e08da000eb050bf81b3871d89
1 -- Time in seconds after which a stuck arrow is deleted
2 local ARROW_TIMEOUT = 60
4 local mod_mcl_hunger = minetest.get_modpath("mcl_hunger")
5 local mod_awards = minetest.get_modpath("awards") and minetest.get_modpath("mcl_achievements")
7 minetest.register_craftitem("mcl_bows:arrow", {
8 description = "Arrow",
9 _doc_items_longdesc = [[Arrows are ammunition for bows and dispensers.
10 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.]],
11 _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.",
12 inventory_image = "mcl_bows_arrow_inv.png",
13 groups = { ammo=1, ammo_bow=1 },
14 _on_dispense = function(itemstack, dispenserpos, droppos, dropnode, dropdir)
15 -- Shoot arrow
16 local shootpos = vector.add(dispenserpos, vector.multiply(dropdir, 0.51))
17 local yaw = math.atan2(dropdir.z, dropdir.x) - math.pi/2
18 mcl_bows.shoot_arrow(itemstack:get_name(), shootpos, dropdir, yaw, nil, 19, 3)
19 end,
22 minetest.register_node("mcl_bows:arrow_box", {
23 drawtype = "nodebox",
24 is_ground_content = false,
25 node_box = {
26 type = "fixed",
27 fixed = {
28 -- Shaft
29 {-6.5/17, -1.5/17, -1.5/17, 6.5/17, 1.5/17, 1.5/17},
30 --Spitze
31 {-4.5/17, 2.5/17, 2.5/17, -3.5/17, -2.5/17, -2.5/17},
32 {-8.5/17, 0.5/17, 0.5/17, -6.5/17, -0.5/17, -0.5/17},
33 --Federn
34 {6.5/17, 1.5/17, 1.5/17, 7.5/17, 2.5/17, 2.5/17},
35 {7.5/17, -2.5/17, 2.5/17, 6.5/17, -1.5/17, 1.5/17},
36 {7.5/17, 2.5/17, -2.5/17, 6.5/17, 1.5/17, -1.5/17},
37 {6.5/17, -1.5/17, -1.5/17, 7.5/17, -2.5/17, -2.5/17},
39 {7.5/17, 2.5/17, 2.5/17, 8.5/17, 3.5/17, 3.5/17},
40 {8.5/17, -3.5/17, 3.5/17, 7.5/17, -2.5/17, 2.5/17},
41 {8.5/17, 3.5/17, -3.5/17, 7.5/17, 2.5/17, -2.5/17},
42 {7.5/17, -2.5/17, -2.5/17, 8.5/17, -3.5/17, -3.5/17},
45 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"},
46 groups = {not_in_creative_inventory=1},
49 -- FIXME: Restore arrow state properly on re-loading
50 local THROWING_ARROW_ENTITY={
51 physical = false,
52 visual = "wielditem",
53 visual_size = {x=0.4, y=0.4},
54 textures = {"mcl_bows:arrow_box"},
55 collisionbox = {0,0,0,0,0,0},
57 _lastpos={},
58 _startpos=nil,
59 _damage=1, -- Damage on impact
60 _stuck=false, -- Whether arrow is stuck
61 _stucktimer=nil,-- Amount of time (in seconds) the arrow has been stuck so far
62 _shooter=nil, -- ObjectRef of player or mob who shot it
65 THROWING_ARROW_ENTITY.on_step = function(self, dtime)
66 local pos = self.object:getpos()
67 local node = minetest.get_node(pos)
69 if self._stuck then
70 self._stucktimer = self._stucktimer + dtime
71 if self._stucktimer > ARROW_TIMEOUT then
72 self.object:remove()
73 return
74 end
75 local objects = minetest.get_objects_inside_radius(pos, 2)
76 for _,obj in ipairs(objects) do
77 if obj:is_player() then
78 if not minetest.settings:get_bool("creative_mode") then
79 -- Pickup arrow if player is nearby
80 if obj:get_inventory():room_for_item("main", "mcl_bows:arrow") then
81 obj:get_inventory():add_item("main", "mcl_bows:arrow")
82 minetest.sound_play("item_drop_pickup", {
83 pos = pos,
84 max_hear_distance = 16,
85 gain = 1.0,
87 self.object:remove()
88 return
89 end
90 else
91 self.object:remove()
92 return
93 end
94 end
95 end
97 -- Check for object collision. Done every tick (hopefully this is not too stressing)
98 else
99 local objs = minetest.get_objects_inside_radius(pos, 2)
100 local closest_object
101 local closest_distance
102 local ok = false
104 -- Iterate through all objects and remember the closest attackable object
105 for k, obj in pairs(objs) do
106 -- Arrows can only damage players and mobs
107 if obj ~= self._shooter and obj:is_player() then
108 ok = true
109 elseif obj:get_luaentity() ~= nil then
110 if obj ~= self._shooter and obj:get_luaentity()._cmi_is_mob then
111 ok = true
115 if ok then
116 local dist = vector.distance(pos, obj:getpos())
117 if not closest_object or not closest_distance then
118 closest_object = obj
119 closest_distance = dist
120 elseif dist < closest_distance then
121 closest_object = obj
122 closest_distance = dist
127 -- If an attackable object was found, we will damage the closest one only
128 if closest_object ~= nil then
129 local obj = closest_object
130 local is_player = obj:is_player()
131 local lua = obj:get_luaentity()
132 if obj ~= self._shooter and (is_player or (lua and lua._cmi_is_mob)) then
133 obj:punch(self.object, 1.0, {
134 full_punch_interval=1.0,
135 damage_groups={fleshy=self._damage},
136 }, nil)
138 if is_player then
139 if self._shooter and self._shooter:is_player() then
140 -- “Ding” sound for hitting another player
141 minetest.sound_play({name="mcl_bows_hit_player", gain=0.1}, {to_player=self._shooter})
143 if mod_mcl_hunger then
144 mcl_hunger.exhaust(obj:get_player_name(), mcl_hunger.EXHAUST_DAMAGE)
148 if lua then
149 local entity_name = lua.name
150 -- Achievement for hitting skeleton, wither skeleton or stray (TODO) with an arrow at least 50 meters away
151 -- NOTE: Range has been reduced because mobs unload much earlier than that ... >_>
152 -- TODO: This achievement should be given for the kill, not just a hit
153 if self._shooter and self._shooter:is_player() and vector.distance(pos, self._startpos) >= 20 then
154 if mod_awards and (entity_name == "mobs_mc:skeleton" or entity_name == "mobs_mc:stray" or entity_name == "mobs_mc:witherskeleton") then
155 awards.unlock(self._shooter:get_player_name(), "mcl:snipeSkeleton")
159 self.object:remove()
164 -- Check for node collision
165 if self._lastpos.x~=nil and not self._stuck then
166 local def = minetest.registered_nodes[node.name]
167 if (def and def.walkable) or not def then
168 -- Arrow is stuck and no longer moves
169 self._stuck = true
170 self._stucktimer = 0
171 self.object:set_velocity({x=0, y=0, z=0})
172 self.object:set_acceleration({x=0, y=0, z=0})
173 elseif (def and def.liquidtype ~= "none") then
174 -- Slow down arrow in liquids
175 local v = def.liquid_viscosity
176 if not v then
177 v = 0
179 local vpenalty = math.max(0.1, 0.98 - 0.1 * v)
180 local vel = self.object:get_velocity()
181 if math.abs(vel.x) > 0.001 then
182 vel.x = vel.x * vpenalty
184 if math.abs(vel.z) > 0.001 then
185 vel.z = vel.z * vpenalty
187 self.object:set_velocity(vel)
191 -- Update internal variable
192 self._lastpos={x=pos.x, y=pos.y, z=pos.z}
195 minetest.register_entity("mcl_bows:arrow_entity", THROWING_ARROW_ENTITY)
197 if minetest.get_modpath("mcl_core") and minetest.get_modpath("mcl_mobitems") then
198 minetest.register_craft({
199 output = 'mcl_bows:arrow 4',
200 recipe = {
201 {'mcl_core:flint'},
202 {'mcl_core:stick'},
203 {'mcl_mobitems:feather'}