1
0
mirror of https://gitlab.com/echoes91/spears.git synced 2024-11-14 13:50:18 +01:00
spears/functions.lua

225 lines
8.2 KiB
Lua
Raw Normal View History

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)
2022-10-12 23:20:51 +02:00
local wear = itemstack:get_wear()
local pointed_a = pointed_thing.above
local pointed_b = pointed_thing.under
if pointed_thing.type == "node" and vector.distance(pointed_a, throw_pos) < 1 then -- Stick into node
local node = minetest.get_node(pointed_b)
local check_node = spears_check_node(node.name)
2024-02-07 22:46:58 +01:00
if check_node == nil then
2022-10-12 23:20:51 +02:00
return false
2024-02-07 22:52:24 +01:00
elseif check_node == 'cracky' then
2022-10-12 23:20:51 +02:00
minetest.sound_play("default_metal_footstep", {pos = pointed_a}, true)
return false
2024-02-07 22:52:24 +01:00
elseif check_node == 'sticky' then
2022-10-12 23:20:51 +02:00
local spear_object = minetest.add_entity(vector.divide(vector.add(vector.multiply(pointed_a, 2), pointed_b), 3), spear)
2020-04-19 18:50:54 +02:00
spear_object:set_rotation(rotation)
2022-10-12 23:20:51 +02:00
spear_object:get_luaentity()._wear = wear
spear_object:get_luaentity()._stickpos = pointed_b
minetest.sound_play("default_place_node", {pos = pointed_a}, true)
return false
2020-04-19 18:50:54 +02:00
end
2024-02-07 22:46:58 +01:00
else -- Throw
local throw_speed = tonumber(minetest.settings:get("spears_throw_speed") or 13)
--while vector.distance(player_pos, throw_pos) < 1.2 do
-- throw_pos = vector.add(throw_pos, vector.multiply(direction, 0.1))
--end
2022-10-12 23:20:51 +02:00
local player_vel = player:get_velocity()
local spear_object = minetest.add_entity(throw_pos, spear)
spear_object:set_velocity(vector.add(player_vel, vector.multiply(direction, throw_speed)))
spear_object:set_rotation(rotation)
minetest.sound_play("spears_throw", {pos = player_pos}, true)
spear_object:get_luaentity()._wear = wear
spear_object:get_luaentity()._stickpos = nil
2024-02-07 22:46:58 +01:00
spear_object:get_luaentity()._owner = player:get_luaentity()
2022-10-12 23:20:51 +02:00
return true
2020-04-19 18:50:54 +02:00
end
2015-10-03 20:45:32 +02:00
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)
2022-10-12 23:20:51 +02:00
local wear = self._wear
if wear == nil then
2015-10-16 20:09:26 +02:00
self.object:remove()
2022-10-12 23:20:51 +02:00
return false
2020-04-19 11:55:40 +02:00
end
2022-09-28 23:44:35 +02:00
local pos = self.object:get_pos()
2020-04-19 11:55:40 +02:00
local velocity = self.object:get_velocity()
local speed = vector.length(velocity)
2022-10-12 23:20:51 +02:00
if self._stickpos ~= nil then -- Spear is stuck
2020-04-19 18:50:54 +02:00
local node = minetest.get_node(self._stickpos)
2022-10-12 23:20:51 +02:00
local check_node = spears_check_node(node.name)
2024-02-07 22:52:24 +01:00
if check_node ~= 'sticky' then -- Fall when node is removed
2020-04-19 18:50:54 +02:00
self.object:remove()
2022-10-12 23:20:51 +02:00
minetest.add_item(pos, {name='spears:spear_' .. spear_type, wear = wear})
return false
2020-04-19 18:50:54 +02:00
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-05-08 19:32:01 +02:00
local spearhead_pos = vector.add(pos, vector.multiply(direction, 0.5))
2020-04-19 18:50:54 +02:00
self.object:set_rotation({x = 0, y = yaw + math.pi/2, z = pitch})
2024-02-03 10:56:25 +01:00
-- Hit a target?
local objects_in_radius = minetest.get_objects_inside_radius(spearhead_pos, 1)
2020-04-19 18:50:54 +02:00
for _,object in ipairs(objects_in_radius) do
2024-02-03 10:56:25 +01:00
if spears_check_target(self, object, spearhead_pos) and object:get_armor_groups().fleshy then
2020-04-19 18:50:54 +02:00
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)
2022-10-12 23:20:51 +02:00
wear = spears_wear(wear, toughness)
minetest.add_item(pos, {name='spears:spear_' .. spear_type, wear = wear})
return true
2020-04-19 11:55:40 +02:00
end
2020-04-19 18:50:54 +02:00
end
2022-10-12 23:20:51 +02:00
-- Hit a node?
local node = minetest.get_node(spearhead_pos)
local check_node = spears_check_node(node.name)
2024-02-07 22:52:24 +01:00
if check_node == nil then
2022-10-12 23:20:51 +02:00
self.object:remove()
minetest.add_item(pos, {name='spears:spear_' .. spear_type, wear = wear})
2024-02-07 22:46:58 +01:00
elseif check_node ~= 'through' then
2022-10-12 23:20:51 +02:00
wear = spears_wear(wear, toughness)
if wear >= 65535 then
2020-05-08 19:32:01 +02:00
minetest.sound_play("default_tool_breaks", {pos = pos}, true)
self.object:remove()
2022-10-12 23:20:51 +02:00
minetest.add_item(pos, {name='defaut:stick'})
return false
2024-02-07 22:46:58 +01:00
elseif check_node == 'cracky' then
2022-10-12 23:20:51 +02:00
minetest.sound_play("default_metal_footstep", {pos = pos}, true)
self.object:remove()
minetest.add_item(pos, {name='spears:spear_' .. spear_type, wear = wear})
return false
2024-02-07 22:46:58 +01:00
elseif check_node == 'sticky' then
2024-02-07 22:49:31 +01:00
self.object:set_acceleration({x = 0, y = 0, z = 0})
self.object:set_velocity({x = 0, y = 0, z = 0})
2022-10-12 23:20:51 +02:00
minetest.sound_play("default_place_node", {pos = pos}, true)
self._stickpos = spearhead_pos
self._wear = wear
2015-10-16 20:09:26 +02:00
end
2022-10-12 23:20:51 +02:00
else -- Get drag
local viscosity = minetest.registered_nodes[node.name].liquid_viscosity
2024-02-03 10:56:25 +01:00
local drag_coeff = tonumber(minetest.settings:get("spears_drag_coeff"))
local drag = math.max(viscosity, drag_coeff)
2020-05-08 19:32:01 +02:00
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
2022-10-12 23:20:51 +02:00
function spears_check_node(node_name)
local node = minetest.registered_nodes[node_name]
2024-02-07 22:46:58 +01:00
local cracky_limit = tonumber(minetest.settings:get("spears_node_cracky_limit") or 3)
2022-10-12 23:20:51 +02:00
if node == nil then
2024-02-07 22:46:58 +01:00
return nil
2024-02-03 10:56:25 +01:00
elseif node.groups.cracky ~= nil and node.groups.cracky < cracky_limit then
2024-02-07 22:46:58 +01:00
return 'cracky'
2024-02-03 10:56:25 +01:00
elseif node.walkable and not node.buildable_to then
2024-02-07 22:46:58 +01:00
return 'sticky'
2022-10-12 23:20:51 +02:00
else
2024-02-07 22:46:58 +01:00
return 'through'
2022-10-12 23:20:51 +02:00
end
end
function spears_wear(initial_wear, toughness)
if not minetest.settings:get_bool("creative_mode") then
local wear = initial_wear + 65535/toughness
return wear
else
local wear = initial_wear
return wear
end
end
2024-02-03 10:56:25 +01:00
function spears_check_target(self, object, spearhead_pos)
if object:get_luaentity() == self then
return false
elseif isPointInsideEntity(object, spearhead_pos) then
-- print(self.name .. " hit " .. object:get_luaentity().name)
return true
else
-- print(self.name .. " missed " .. object:get_luaentity().name)
return false
end
end
-- Function to check if a point is inside an entity
function isPointInsideEntity(object, point)
local pos = object:get_pos()
local collisionbox = object:get_properties().collisionbox
local yaw = object:get_yaw()
-- Adjust position for the center of the collision box
local center = {
x = pos.x + (collisionbox[1] + collisionbox[4]) / 2,
y = pos.y + (collisionbox[2] + collisionbox[5]) / 2,
z = pos.z + (collisionbox[3] + collisionbox[6]) / 2
}
-- Rotate the point around the entity's center
local rotatedPoint = rotatePoint(point, center, yaw)
-- Check if the rotated point is inside the collision box
return rotatedPoint.x >= pos.x + collisionbox[1] and
rotatedPoint.x <= pos.x + collisionbox[4] and
rotatedPoint.y >= pos.y + collisionbox[2] and
rotatedPoint.y <= pos.y + collisionbox[5] and
rotatedPoint.z >= pos.z + collisionbox[3] and
rotatedPoint.z <= pos.z + collisionbox[6]
end
-- Function to rotate a point around a center and yaw angle
function rotatePoint(point, center, yaw)
local cosYaw = math.cos(yaw)
local sinYaw = math.sin(yaw)
local translatedX = point.x - center.x
local translatedZ = point.z - center.z
local rotatedX = translatedX * cosYaw - translatedZ * sinYaw
local rotatedZ = translatedX * sinYaw + translatedZ * cosYaw
return {
x = rotatedX + center.x,
y = point.y,
z = rotatedZ + center.z
}
end