6 | 6 |
throwing.target_both = 3
|
7 | 7 |
|
8 | 8 |
throwing.modname = minetest.get_current_modname()
|
|
9 |
local S = minetest.get_translator("throwing")
|
9 | 10 |
local use_toolranks = minetest.get_modpath("toolranks") and minetest.settings:get_bool("throwing.toolranks", true)
|
10 | 11 |
|
11 | 12 |
--------- Arrows functions ---------
|
|
23 | 24 |
return minetest.registered_items[arrow].throwing_entity(pos, player)
|
24 | 25 |
end
|
25 | 26 |
else
|
26 | |
obj = minetest.add_entity(pos, "__builtin:item", arrow)
|
|
27 |
return minetest.add_entity(pos, "__builtin:item", arrow)
|
27 | 28 |
end
|
28 | 29 |
end
|
29 | 30 |
|
|
108 | 109 |
end
|
109 | 110 |
|
110 | 111 |
if not minetest.settings:get_bool("creative_mode") then
|
111 | |
if new_stack then
|
112 | |
inventory:set_stack("main", arrrow_index, new_stack)
|
113 | |
else
|
114 | |
local stack = inventory:get_stack("main", arrow_index)
|
115 | |
stack:take_item()
|
116 | |
inventory:set_stack("main", arrow_index, stack)
|
117 | |
end
|
|
112 |
inventory:set_stack("main", arrow_index, new_stack)
|
118 | 113 |
end
|
119 | 114 |
|
120 | 115 |
return true
|
|
134 | 129 |
minetest.log(level or "action", "[throwing] Arrow "..(self.item or self.name).." throwed by player "..self.player.." "..tostring(self.timer).."s ago "..message)
|
135 | 130 |
end
|
136 | 131 |
|
137 | |
local hit = function(pos, node, obj)
|
|
132 |
local hit = function(pos1, node1, obj)
|
138 | 133 |
if obj then
|
139 | 134 |
if obj:is_player() then
|
140 | 135 |
if obj:get_player_name() == self.player then -- Avoid hitting the hitter
|
|
153 | 148 |
player:get_inventory():add_item("main", self.item)
|
154 | 149 |
end
|
155 | 150 |
if self.on_hit_fails then
|
156 | |
self:on_hit_fails(pos, player, self.data)
|
|
151 |
self:on_hit_fails(pos1, player, self.data)
|
157 | 152 |
end
|
158 | 153 |
end
|
159 | 154 |
|
|
163 | 158 |
return
|
164 | 159 |
end
|
165 | 160 |
|
166 | |
if node and minetest.is_protected(pos, self.player) and not self.allow_protected then -- Forbid hitting nodes in protected areas
|
167 | |
minetest.record_protection_violation(pos, self.player)
|
|
161 |
if node1 and minetest.is_protected(pos1, self.player) and not self.allow_protected then -- Forbid hitting nodes in protected areas
|
|
162 |
minetest.record_protection_violation(pos1, self.player)
|
168 | 163 |
logging("hitted a node into a protected area")
|
169 | 164 |
return
|
170 | 165 |
end
|
171 | 166 |
|
172 | 167 |
if self.on_hit then
|
173 | |
local ret, reason = self:on_hit(pos, self.last_pos, node, obj, player, self.data)
|
|
168 |
local ret, reason = self:on_hit(pos1, self.last_pos, node1, obj, player, self.data)
|
174 | 169 |
if ret == false then
|
175 | 170 |
if reason then
|
176 | 171 |
logging(": on_hit function failed for reason: "..reason)
|
|
184 | 179 |
end
|
185 | 180 |
|
186 | 181 |
if self.on_hit_sound then
|
187 | |
minetest.sound_play(self.on_hit_sound, {pos = pos, gain = 0.8})
|
188 | |
end
|
189 | |
if node then
|
190 | |
logging("collided with node "..node.name.." at ("..pos.x..","..pos.y..","..pos.z..")")
|
|
182 |
minetest.sound_play(self.on_hit_sound, {pos = pos1, gain = 0.8})
|
|
183 |
end
|
|
184 |
|
|
185 |
local identifier
|
|
186 |
if node1 then
|
|
187 |
identifier = "node " .. node1.name
|
191 | 188 |
elseif obj then
|
192 | 189 |
if obj:get_luaentity() then
|
193 | |
logging("collided with luaentity "..obj:get_luaentity().name.." at ("..pos.x..","..pos.y..","..pos.z..")")
|
|
190 |
identifier = "luaentity " .. obj:get_luaentity().name
|
194 | 191 |
elseif obj:is_player() then
|
195 | |
logging("collided with player "..obj:get_player_name().." at ("..pos.x..","..pos.y..","..pos.z..")")
|
|
192 |
identifier = "player " .. obj:get_player_name()
|
196 | 193 |
else
|
197 | |
logging("collided with object at ("..pos.x..","..pos.y..","..pos.z..")")
|
198 | |
end
|
|
194 |
identifier = "unknown object"
|
|
195 |
end
|
|
196 |
end
|
|
197 |
if identifier then
|
|
198 |
logging("collided with " .. identifier .. " at " .. minetest.pos_to_string(pos1) .. ")")
|
199 | 199 |
end
|
200 | 200 |
|
201 | 201 |
-- Toolranks support: update bow uses
|
202 | 202 |
if self.toolranks then
|
203 | 203 |
local inventory = player:get_inventory()
|
204 | 204 |
-- Check that the player did not move the bow
|
205 | |
if inventory:get_stack("main", self.toolranks.index):get_name() == self.toolranks.itemstack:get_name() then
|
206 | |
local new_itemstack = toolranks.new_afteruse(self.toolranks.itemstack, player, nil, {wear = self.toolranks.wear})
|
|
205 |
local current_stack = inventory:get_stack("main", self.toolranks.index)
|
|
206 |
if current_stack:get_name() == self.toolranks.name then
|
|
207 |
local new_itemstack = toolranks.new_afteruse(current_stack, player, nil, {wear = self.toolranks.wear})
|
207 | 208 |
inventory:set_stack("main", self.toolranks.index, new_itemstack)
|
208 | 209 |
end
|
209 | 210 |
end
|
|
294 | 295 |
if minetest.settings:get_bool("throwing.allow_arrow_placing") and pointed_thing.above then
|
295 | 296 |
local playername = placer:get_player_name()
|
296 | 297 |
if not minetest.is_protected(pointed_thing.above, playername) then
|
297 | |
minetest.log("action", "Player "..playername.." placed arrow "..name.." at ("..pointed_thing.above.x..","..pointed_thing.above.y..","..pointed_thing.above.z..")")
|
|
298 |
minetest.log("action", "Player "..playername.." placed arrow "..name.." at "..minetest.pos_to_string(pointed_thing.above))
|
298 | 299 |
minetest.set_node(pointed_thing.above, {name = name})
|
299 | 300 |
itemstack:take_item()
|
300 | 301 |
return itemstack
|
301 | 302 |
else
|
302 | |
minetest.log("warning", "Player "..playername.." tried to place arrow "..name.." into a protected area at ("..pointed_thing.above.x..","..pointed_thing.above.y..","..pointed_thing.above.z..")")
|
|
303 |
minetest.log("warning", "Player "..playername.." tried to place arrow "..name.." into a protected area at "..minetest.pos_to_string(pointed_thing.above))
|
303 | 304 |
minetest.record_protection_violation(pointed_thing.above, playername)
|
304 | 305 |
return itemstack
|
305 | 306 |
end
|
|
352 | 353 |
|
353 | 354 |
|
354 | 355 |
---------- Bows -----------
|
|
356 |
if use_toolranks and minetest.get_modpath("toolranks_extras") and toolranks_extras.register_tool_type then
|
|
357 |
toolranks_extras.register_tool_type("bow", S("bow"), S("Arrows thrown"))
|
|
358 |
end
|
|
359 |
|
355 | 360 |
function throwing.register_bow(name, def)
|
356 | 361 |
local enable_toolranks = use_toolranks and not def.no_toolranks
|
357 | 362 |
|
|
400 | 405 |
minetest.after(def.delay or 0, function()
|
401 | 406 |
-- Re-check that the arrow can be thrown. Overwrite the new_stack
|
402 | 407 |
local old_new_stack = new_stack
|
403 | |
res, new_stack = def.allow_shot(user, user:get_inventory():get_stack("main", arrow_index), arrow_index, true)
|
|
408 |
|
|
409 |
local arrow_stack = user:get_inventory():get_stack("main", arrow_index)
|
|
410 |
|
|
411 |
res, new_stack = def.allow_shot(user, arrow_stack, arrow_index, true)
|
|
412 |
if not res then
|
|
413 |
return
|
|
414 |
end
|
|
415 |
|
404 | 416 |
if not new_stack then
|
405 | 417 |
new_stack = old_new_stack
|
406 | 418 |
end
|
407 | |
if not res then
|
408 | |
return
|
|
419 |
if not new_stack then
|
|
420 |
arrow_stack:take_item()
|
|
421 |
new_stack = arrow_stack
|
409 | 422 |
end
|
410 | 423 |
|
411 | 424 |
-- Shoot arrow
|
|
413 | 426 |
local toolranks_data
|
414 | 427 |
if enable_toolranks then
|
415 | 428 |
toolranks_data = {
|
416 | |
itemstack = itemstack,
|
|
429 |
name = itemstack:get_name(),
|
417 | 430 |
index = bow_index,
|
418 | 431 |
wear = uses
|
419 | 432 |
}
|
|
427 | 440 |
|
428 | 441 |
if def.throw_itself then
|
429 | 442 |
-- This is a bug. If we return ItemStack(nil), the player punches the entity,
|
430 | |
-- and if the entity if a __builtin:item, it gets back to his inventory.
|
|
443 |
-- and if the entity is a __builtin:item, it gets back to his inventory.
|
431 | 444 |
minetest.after(0.1, function()
|
432 | 445 |
user:get_inventory():remove_item("main", itemstack)
|
433 | 446 |
end)
|
|
441 | 454 |
|
442 | 455 |
if enable_toolranks then
|
443 | 456 |
def.original_description = def.original_description or def.description
|
444 | |
def.description = toolranks.create_description(def.description, 0, 1)
|
|
457 |
def.description = toolranks.create_description(def.description)
|
445 | 458 |
end
|
446 | 459 |
|
447 | 460 |
minetest.register_tool(name, def)
|