Just some sanity checks for bow charging
[MineClone/MineClone2.git] / mods / ITEMS / mcl_throwing / arrow.lua
blob0795b30b0e573f4ca99fc9f22453743b413daf16
1 local mod_mcl_hunger = minetest.get_modpath("mcl_hunger")
2 local mod_awards = minetest.get_modpath("awards") and minetest.get_modpath("mcl_achievements")
4 minetest.register_craftitem("mcl_throwing:arrow", {
5 description = "Arrow",
6 _doc_items_longdesc = "Arrows are ammunition for bows and dispensers.",
7 _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. Arrows fired from a dispenser always deal 3 damage.",
8 inventory_image = "mcl_throwing_arrow_inv.png",
9 groups = { ammo=1, ammo_bow=1 },
10 _on_dispense = function(itemstack, dispenserpos, droppos, dropnode, dropdir)
11 -- Shoot arrow
12 local shootpos = vector.add(dispenserpos, vector.multiply(dropdir, 0.51))
13 local yaw = math.atan2(dropdir.z, dropdir.x) - math.pi/2
14 mcl_throwing.shoot_arrow(itemstack:get_name(), shootpos, dropdir, yaw, nil, 19, 3)
15 end,
18 minetest.register_node("mcl_throwing:arrow_box", {
19 drawtype = "nodebox",
20 is_ground_content = false,
21 node_box = {
22 type = "fixed",
23 fixed = {
24 -- Shaft
25 {-6.5/17, -1.5/17, -1.5/17, 6.5/17, 1.5/17, 1.5/17},
26 --Spitze
27 {-4.5/17, 2.5/17, 2.5/17, -3.5/17, -2.5/17, -2.5/17},
28 {-8.5/17, 0.5/17, 0.5/17, -6.5/17, -0.5/17, -0.5/17},
29 --Federn
30 {6.5/17, 1.5/17, 1.5/17, 7.5/17, 2.5/17, 2.5/17},
31 {7.5/17, -2.5/17, 2.5/17, 6.5/17, -1.5/17, 1.5/17},
32 {7.5/17, 2.5/17, -2.5/17, 6.5/17, 1.5/17, -1.5/17},
33 {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, 8.5/17, 3.5/17, 3.5/17},
36 {8.5/17, -3.5/17, 3.5/17, 7.5/17, -2.5/17, 2.5/17},
37 {8.5/17, 3.5/17, -3.5/17, 7.5/17, 2.5/17, -2.5/17},
38 {7.5/17, -2.5/17, -2.5/17, 8.5/17, -3.5/17, -3.5/17},
41 tiles = {"mcl_throwing_arrow.png^[transformFX", "mcl_throwing_arrow.png^[transformFX", "mcl_throwing_arrow_back.png", "mcl_throwing_arrow_front.png", "mcl_throwing_arrow.png", "mcl_throwing_arrow.png^[transformFX"},
42 groups = {not_in_creative_inventory=1},
45 local THROWING_ARROW_ENTITY={
46 physical = false,
47 visual = "wielditem",
48 visual_size = {x=0.4, y=0.4},
49 textures = {"mcl_throwing:arrow_box"},
50 collisionbox = {0,0,0,0,0,0},
52 _lastpos={},
53 _startpos=nil,
54 _damage=1, -- Damage on impact
55 _shooter=nil, -- ObjectRef of player or mob who shot it
58 THROWING_ARROW_ENTITY.on_step = function(self, dtime)
59 local pos = self.object:getpos()
60 local node = minetest.get_node(pos)
62 -- Check for object collision. Done every tick (hopefully this is not too stressing)
64 local objs = minetest.get_objects_inside_radius(pos, 2)
65 local closest_object
66 local closest_distance
67 local ok = false
69 -- Iterate through all objects and remember the closest attackable object
70 for k, obj in pairs(objs) do
71 -- Arrows can only damage players and mobs
72 if obj ~= self._shooter and obj:is_player() then
73 ok = true
74 elseif obj:get_luaentity() ~= nil then
75 if obj ~= self._shooter and obj:get_luaentity()._cmi_is_mob then
76 ok = true
77 end
78 end
80 if ok then
81 local dist = vector.distance(pos, obj:getpos())
82 if not closest_object or not closest_distance then
83 closest_object = obj
84 closest_distance = dist
85 elseif dist < closest_distance then
86 closest_object = obj
87 closest_distance = dist
88 end
89 end
90 end
92 -- If an attackable object was found, we will damage the closest one only
93 if closest_object ~= nil then
94 local obj = closest_object
95 local is_player = obj:is_player()
96 local lua = obj:get_luaentity()
97 if obj ~= self._shooter and (is_player or (lua and lua._cmi_is_mob)) then
98 obj:punch(self.object, 1.0, {
99 full_punch_interval=1.0,
100 damage_groups={fleshy=self._damage},
101 }, nil)
102 if mod_mcl_hunger and is_player then
103 mcl_hunger.exhaust(obj:get_player_name(), mcl_hunger.EXHAUST_DAMAGE)
106 if lua then
107 local entity_name = lua.name
108 -- Achievement for hitting skeleton, wither skeleton or stray (TODO) with an arrow at least 50 meters away
109 -- NOTE: Range has been reduced because mobs unload much earlier than that ... >_>
110 -- TODO: This achievement should be given for the kill, not just a hit
111 if self._shooter and self._shooter:is_player() and vector.distance(pos, self._startpos) >= 20 then
112 if mod_awards and (entity_name == "mobs_mc:skeleton" or entity_name == "mobs_mc:stray" or entity_name == "mobs_mc:witherskeleton") then
113 awards.unlock(self._shooter:get_player_name(), "mcl:snipeSkeleton")
117 self.object:remove()
122 -- Check for node collision
123 if self._lastpos.x~=nil then
124 local def = minetest.registered_nodes[node.name]
125 if (def and def.walkable) or not def then
126 if not minetest.settings:get_bool("creative_mode") then
127 minetest.add_item(self._lastpos, 'mcl_throwing:arrow')
129 self.object:remove()
133 -- Update internal variable
134 self._lastpos={x=pos.x, y=pos.y, z=pos.z}
137 minetest.register_entity("mcl_throwing:arrow_entity", THROWING_ARROW_ENTITY)
139 if minetest.get_modpath("mcl_core") and minetest.get_modpath("mcl_mobitems") then
140 minetest.register_craft({
141 output = 'mcl_throwing:arrow 4',
142 recipe = {
143 {'mcl_core:flint'},
144 {'mcl_core:stick'},
145 {'mcl_mobitems:feather'}