2020-04-19 18:50:54 +02:00
|
|
|
function spears_throw (itemstack, player, pointed_thing)
|
2015-10-03 20:45:32 +02:00
|
|
|
local spear = itemstack:get_name() .. '_entity'
|
2020-04-19 18:50:54 +02:00
|
|
|
local player_pos = player:get_pos()
|
|
|
|
local head_pos = vector.new(player_pos.x, player_pos.y + player:get_properties().eye_height, player_pos.z)
|
2020-04-19 11:55:40 +02:00
|
|
|
local direction = player:get_look_dir()
|
2020-04-19 18:50:54 +02:00
|
|
|
local throw_pos = vector.add(head_pos, vector.multiply(direction,0.5))
|
2020-04-19 11:55:40 +02:00
|
|
|
local pitch = player:get_look_vertical()
|
|
|
|
local yaw = player:get_look_horizontal()
|
2020-04-19 18:50:54 +02:00
|
|
|
local rotation = vector.new(0, yaw + math.pi/2, pitch + math.pi/6)
|
2020-04-20 23:50:20 +02:00
|
|
|
-- Plant into node
|
2020-04-19 18:50:54 +02:00
|
|
|
if pointed_thing.type == "node" then
|
|
|
|
local node = minetest.get_node(pointed_thing.under)
|
|
|
|
if minetest.registered_nodes[node.name].walkable and vector.distance(pointed_thing.above, throw_pos) < 1 then
|
2020-04-26 12:58:52 +02:00
|
|
|
local spear_object = minetest.add_entity(vector.divide(vector.add(vector.multiply(pointed_thing.above, 2), pointed_thing.under), 3), spear)
|
2020-04-19 18:50:54 +02:00
|
|
|
spear_object:set_rotation(rotation)
|
|
|
|
spear_object:get_luaentity()._wear = itemstack:get_wear()
|
|
|
|
spear_object:get_luaentity()._stickpos = pointed_thing.under
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- Avoid hitting yourself and throw
|
2020-04-20 23:50:20 +02:00
|
|
|
local throw_speed = 12
|
2020-04-26 12:58:52 +02:00
|
|
|
while vector.distance(player_pos, throw_pos) < 1.2 do
|
2020-05-08 19:32:01 +02:00
|
|
|
throw_pos = vector.add(throw_pos, vector.multiply(direction, 0.1))
|
2020-04-19 18:50:54 +02:00
|
|
|
end
|
2020-04-26 12:58:52 +02:00
|
|
|
local player_vel = player:get_player_velocity()
|
2020-04-19 18:50:54 +02:00
|
|
|
local spear_object = minetest.add_entity(throw_pos, spear)
|
2020-04-26 12:58:52 +02:00
|
|
|
spear_object:set_velocity(vector.add(player_vel, vector.multiply(direction, throw_speed)))
|
2020-04-19 18:50:54 +02:00
|
|
|
spear_object:set_rotation(rotation)
|
|
|
|
minetest.sound_play("spears_throw", {pos = player_pos}, true)
|
2020-04-19 11:55:40 +02:00
|
|
|
spear_object:get_luaentity()._wear = itemstack:get_wear()
|
2020-04-19 18:50:54 +02:00
|
|
|
spear_object:get_luaentity()._stickpos = nil
|
2015-10-03 20:45:32 +02:00
|
|
|
return true
|
|
|
|
end
|
2015-10-16 20:09:26 +02:00
|
|
|
|
2020-04-18 15:31:17 +02:00
|
|
|
function spears_set_entity(spear_type, base_damage, toughness)
|
2015-10-16 20:09:26 +02:00
|
|
|
local SPEAR_ENTITY={
|
2020-04-19 11:55:40 +02:00
|
|
|
initial_properties = {
|
|
|
|
physical = false,
|
|
|
|
visual = "item",
|
2020-04-20 23:50:20 +02:00
|
|
|
visual_size = {x = 0.3, y = 0.3, z = 0.3},
|
2020-04-19 11:55:40 +02:00
|
|
|
wield_item = "spears:spear_" .. spear_type,
|
2020-04-20 23:50:20 +02:00
|
|
|
collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
|
2020-04-19 11:55:40 +02:00
|
|
|
},
|
|
|
|
|
2020-04-19 21:53:10 +02:00
|
|
|
on_activate = function (self, staticdata, dtime_s)
|
|
|
|
self.object:set_armor_groups({immortal = 1})
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_punch = function (self, puncher)
|
2020-04-20 23:50:20 +02:00
|
|
|
if puncher:is_player() then -- Grab the spear
|
2020-04-19 11:55:40 +02:00
|
|
|
local stack = {name='spears:spear_' .. spear_type, wear = self._wear}
|
|
|
|
local inv = puncher:get_inventory()
|
|
|
|
if inv:room_for_item("main", stack) then
|
|
|
|
inv:add_item("main", stack)
|
|
|
|
self.object:remove()
|
2015-10-16 20:09:26 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
2020-04-19 11:55:40 +02:00
|
|
|
|
|
|
|
on_step = function(self, dtime)
|
|
|
|
if not self._wear then
|
2015-10-16 20:09:26 +02:00
|
|
|
self.object:remove()
|
2020-04-19 11:55:40 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local velocity = self.object:get_velocity()
|
|
|
|
local speed = vector.length(velocity)
|
2020-04-20 23:50:20 +02:00
|
|
|
-- Spear is stuck ?
|
2020-05-08 19:32:01 +02:00
|
|
|
if self._stickpos then
|
2020-04-19 18:50:54 +02:00
|
|
|
local node = minetest.get_node(self._stickpos)
|
2020-05-08 19:32:01 +02:00
|
|
|
local node_cracky = minetest.registered_nodes[node.name].groups.cracky
|
|
|
|
if node_cracky and node_cracky < 3 then
|
2020-04-26 12:58:52 +02:00
|
|
|
minetest.sound_play("default_metal_footstep", {pos = pos}, true)
|
|
|
|
self.object:remove()
|
|
|
|
minetest.add_item(self.object:get_pos(), {name='spears:spear_' .. spear_type, wear = self._wear})
|
2020-05-08 19:32:01 +02:00
|
|
|
elseif not self._node_walkable then
|
2020-04-26 12:58:52 +02:00
|
|
|
minetest.sound_play("default_place_node", {pos = throw_pos}, true)
|
|
|
|
end
|
2020-05-08 19:32:01 +02:00
|
|
|
self._node_walkable = minetest.registered_nodes[node.name].walkable
|
|
|
|
if not node or not self._node_walkable then -- Fall when node is removed
|
2020-04-19 18:50:54 +02:00
|
|
|
self.object:remove()
|
|
|
|
minetest.add_item(self.object:get_pos(), {name='spears:spear_' .. spear_type, wear = self._wear})
|
|
|
|
return
|
|
|
|
end
|
2020-04-20 23:50:20 +02:00
|
|
|
else -- Spear is flying
|
2020-04-19 18:50:54 +02:00
|
|
|
local direction = vector.normalize(velocity)
|
2020-04-26 12:58:52 +02:00
|
|
|
local yaw = minetest.dir_to_yaw(direction)
|
2020-04-19 18:50:54 +02:00
|
|
|
local pitch = math.acos(velocity.y/speed) - math.pi/3
|
2020-04-26 12:58:52 +02:00
|
|
|
local pos = self.object:get_pos()
|
2020-05-08 19:32:01 +02:00
|
|
|
local spearhead_pos = vector.add(pos, vector.multiply(direction, 0.5))
|
|
|
|
local node = minetest.get_node(spearhead_pos)
|
2020-04-19 18:50:54 +02:00
|
|
|
self.object:set_rotation({x = 0, y = yaw + math.pi/2, z = pitch})
|
|
|
|
-- Hit someone?
|
2020-05-08 19:32:01 +02:00
|
|
|
local objects_in_radius = minetest.get_objects_inside_radius(spearhead_pos, 0.6)
|
2020-04-19 18:50:54 +02:00
|
|
|
for _,object in ipairs(objects_in_radius) do
|
|
|
|
if object:get_luaentity() ~= self and object:get_armor_groups().fleshy then
|
|
|
|
local damage = (speed + base_damage)^1.15 - 20
|
|
|
|
object:punch(self.object, 1.0, {full_punch_interval = 1.0, damage_groups = {fleshy=damage},}, direction)
|
2020-04-19 11:55:40 +02:00
|
|
|
self.object:remove()
|
2020-04-19 18:50:54 +02:00
|
|
|
minetest.sound_play("spears_hit", {pos = pos}, true)
|
|
|
|
minetest.add_item(pos, {name='spears:spear_' .. spear_type, wear = self._wear + 65535/toughness})
|
2020-04-19 11:55:40 +02:00
|
|
|
return
|
|
|
|
end
|
2020-04-19 18:50:54 +02:00
|
|
|
end
|
2020-04-20 23:50:20 +02:00
|
|
|
-- Hit a node?
|
2020-05-08 19:32:01 +02:00
|
|
|
if node and minetest.registered_nodes[node.name].walkable
|
|
|
|
and not minetest.registered_nodes[node.name].buildable_to
|
|
|
|
and not (minetest.registered_nodes[node.name].groups.slab and spearhead_pos.y % 1 < 0.5)
|
|
|
|
then -- Stick
|
|
|
|
self.object:set_acceleration({x = 0, y = 0, z = 0})
|
|
|
|
self.object:set_velocity({x = 0, y = 0, z = 0})
|
|
|
|
self._wear = self._wear + 65535/toughness
|
|
|
|
if self._wear >= 65535 then
|
|
|
|
minetest.sound_play("default_tool_breaks", {pos = pos}, true)
|
|
|
|
self.object:remove()
|
|
|
|
return
|
2015-10-16 20:09:26 +02:00
|
|
|
end
|
2020-05-08 19:32:01 +02:00
|
|
|
self._stickpos = spearhead_pos
|
|
|
|
else -- Get drag
|
|
|
|
local drag = math.max(minetest.registered_nodes[node.name].liquid_viscosity, 0.1)
|
|
|
|
local acceleration = vector.multiply(velocity, -drag)
|
|
|
|
acceleration.y = acceleration.y - 10 * ((7 - drag) / 7)
|
|
|
|
self.object:set_acceleration(acceleration)
|
|
|
|
end
|
2015-10-16 20:09:26 +02:00
|
|
|
end
|
2020-04-19 11:55:40 +02:00
|
|
|
end,
|
|
|
|
}
|
2015-10-16 20:09:26 +02:00
|
|
|
return SPEAR_ENTITY
|
|
|
|
end
|