Version 3.0

This commit is contained in:
echoes91 2024-02-03 10:56:25 +01:00
parent 5ac99ff835
commit 534e7ecc8f
8 changed files with 115 additions and 77 deletions

View File

@ -1,3 +1,7 @@
Version 3.0:
- First attempt to detect collisionboxes
- Implemented settings through settingtypes.txt, deprecating "spears.conf"
Version 2.4: Version 2.4:
- Functions rewrite, fixed many vector/variable declarations and unknown node handling - Functions rewrite, fixed many vector/variable declarations and unknown node handling
- Added pigiron support - Added pigiron support

28
README
View File

@ -1,28 +0,0 @@
=== SPEARS for MINETEST by Echoes91 ===
This mod adds spears to Minetest.
How to download:
If you have this file, you already know
Forum topic:
https://forum.minetest.net/viewtopic.php?f=11&t=13367
How to install:
http://wiki.minetest.com/wiki/Installing_Mods
How to use the mod:
Craft a spear with | | | (material) |
| | stick | |
| stick | | |,
works also mirrored.
Spears can be thrown using right mouse button, they either hurt someone or stick where they land on; damage depends on speed and material, flight trajectory is ballistic with (unrealistic) drag.
Spears can be used to fight, but are slower and weaker than swords.
Spears can be used to dig, but are slower and weaker and pickaxes.
If you miss the target it's your fault, the spear is fine.
You can grab a spear on the fly (or maybe wait until it falls?).
You can stick a spear into a block, but if you remove the block then the spear falls (obviously).
This mod contains the word "spear" more than 100 times, if you want the exact number you can count them.
If you feel clever, throw a spear right above you and look at it to see what happens.
You can even throw a spear from above down to your foes, it's even easier to hit.
Someone once reported to have hit its own foot but it takes practice to do that.

View File

@ -1,16 +1,30 @@
DISABLE_STONE_SPEAR = false -- Seems like defaults in settingtypes.txt are not taken by default
DISABLE_STEEL_SPEAR = false if minetest.settings:get("spears_throw_speed") == nil then
DISABLE_COPPER_SPEAR = false minetest.settings:set("spears_throw_speed", 13)
DISABLE_BRONZE_SPEAR = false end
DISABLE_DIAMOND_SPEAR = false
DISABLE_OBSIDIAN_SPEAR = false if minetest.settings:get("spears_drag_coeff") == nil then
DISABLE_GOLD_SPEAR = false minetest.settings:set("spears_drag_coeff", 0.1)
DISABLE_IRON_SPEAR = false end
SPEARS_THROW_SPEED = 13
if minetest.settings:get("spears_node_cracky_limit") == nil then
minetest.settings:set("spears_node_cracky_limit", 3)
end
-- DISABLE_STONE_SPEAR = false
-- DISABLE_STEEL_SPEAR = false
-- DISABLE_COPPER_SPEAR = false
-- DISABLE_BRONZE_SPEAR = false
-- DISABLE_DIAMOND_SPEAR = false
-- DISABLE_OBSIDIAN_SPEAR = false
-- DISABLE_GOLD_SPEAR = false
-- DISABLE_IRON_SPEAR = false
-- SPEARS_THROW_SPEED = 13
SPEARS_V_ZERO = {x = 0, y = 0, z = 0} SPEARS_V_ZERO = {x = 0, y = 0, z = 0}
SPEARS_DRAG_COEFF = 0.1 -- SPEARS_DRAG_COEFF = 0.1
SPEARS_NODE_UNKNOWN = nil SPEARS_NODE_UNKNOWN = nil
SPEARS_NODE_THROUGH = 0 SPEARS_NODE_THROUGH = 0
SPEARS_NODE_STICKY = 1 SPEARS_NODE_STICKY = 1
SPEARS_NODE_CRACKY = 2 SPEARS_NODE_CRACKY = 2
SPEARS_NODE_CRACKY_LIMIT = 3 -- SPEARS_NODE_CRACKY_LIMIT = 3

View File

@ -27,7 +27,7 @@ function spears_throw (itemstack, player, pointed_thing)
return false return false
end end
else -- Avoid hitting yourself and throw else -- Avoid hitting yourself and throw
local throw_speed = SPEARS_THROW_SPEED local throw_speed = tonumber(minetest.settings:get("spears_throw_speed"))
while vector.distance(player_pos, throw_pos) < 1.2 do while vector.distance(player_pos, throw_pos) < 1.2 do
throw_pos = vector.add(throw_pos, vector.multiply(direction, 0.1)) throw_pos = vector.add(throw_pos, vector.multiply(direction, 0.1))
end end
@ -90,10 +90,10 @@ function spears_set_entity(spear_type, base_damage, toughness)
local pitch = math.acos(velocity.y/speed) - math.pi/3 local pitch = math.acos(velocity.y/speed) - math.pi/3
local spearhead_pos = vector.add(pos, vector.multiply(direction, 0.5)) local spearhead_pos = vector.add(pos, vector.multiply(direction, 0.5))
self.object:set_rotation({x = 0, y = yaw + math.pi/2, z = pitch}) self.object:set_rotation({x = 0, y = yaw + math.pi/2, z = pitch})
-- Hit someone? -- Hit a target?
local objects_in_radius = minetest.get_objects_inside_radius(spearhead_pos, 0.6) local objects_in_radius = minetest.get_objects_inside_radius(spearhead_pos, 1)
for _,object in ipairs(objects_in_radius) do for _,object in ipairs(objects_in_radius) do
if object:get_luaentity() ~= self and object:get_armor_groups().fleshy then if spears_check_target(self, object, spearhead_pos) and object:get_armor_groups().fleshy then
local damage = (speed + base_damage)^1.15 - 20 local damage = (speed + base_damage)^1.15 - 20
object:punch(self.object, 1.0, {full_punch_interval = 1.0, damage_groups = {fleshy=damage},}, direction) object:punch(self.object, 1.0, {full_punch_interval = 1.0, damage_groups = {fleshy=damage},}, direction)
self.object:remove() self.object:remove()
@ -130,7 +130,8 @@ function spears_set_entity(spear_type, base_damage, toughness)
end end
else -- Get drag else -- Get drag
local viscosity = minetest.registered_nodes[node.name].liquid_viscosity local viscosity = minetest.registered_nodes[node.name].liquid_viscosity
local drag = math.max(viscosity, SPEARS_DRAG_COEFF) local drag_coeff = tonumber(minetest.settings:get("spears_drag_coeff"))
local drag = math.max(viscosity, drag_coeff)
local acceleration = vector.multiply(velocity, -drag) local acceleration = vector.multiply(velocity, -drag)
acceleration.y = acceleration.y - 10 * ((7 - drag) / 7) acceleration.y = acceleration.y - 10 * ((7 - drag) / 7)
self.object:set_acceleration(acceleration) self.object:set_acceleration(acceleration)
@ -143,11 +144,12 @@ end
function spears_check_node(node_name) function spears_check_node(node_name)
local node = minetest.registered_nodes[node_name] local node = minetest.registered_nodes[node_name]
local cracky_limit = tonumber(minetest.settings:get("spears_node_cracky_limit"))
if node == nil then if node == nil then
return SPEARS_NODE_UNKNOWN return SPEARS_NODE_UNKNOWN
elseif node.groups.cracky ~= nil and node.groups.cracky < SPEARS_NODE_CRACKY_LIMIT then elseif node.groups.cracky ~= nil and node.groups.cracky < cracky_limit then
return SPEARS_NODE_CRACKY return SPEARS_NODE_CRACKY
elseif node.walkable and not node.buildable then elseif node.walkable and not node.buildable_to then
return SPEARS_NODE_STICKY return SPEARS_NODE_STICKY
else else
return SPEARS_NODE_THROUGH return SPEARS_NODE_THROUGH
@ -163,3 +165,59 @@ function spears_wear(initial_wear, toughness)
return wear return wear
end end
end end
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

View File

@ -1,18 +1,14 @@
dofile(minetest.get_modpath("spears").."/defaults.lua") dofile(minetest.get_modpath("spears").."/defaults.lua")
local input = io.open(minetest.get_modpath("spears").."/spears.conf", "r") -- local input = io.open(minetest.get_modpath("spears").."/spears.conf", "r")
if input then -- if input then
dofile(minetest.get_modpath("spears").."/spears.conf") -- dofile(minetest.get_modpath("spears").."/spears.conf")
input:close() -- input:close()
input = nil -- input = nil
end -- end
dofile(minetest.get_modpath("spears").."/functions.lua") dofile(minetest.get_modpath("spears").."/functions.lua")
dofile(minetest.get_modpath("spears").."/tools.lua") dofile(minetest.get_modpath("spears").."/tools.lua")
local log_mods = minetest.settings:get_bool("log_mods") minetest.log("action", "[MOD] Spears loaded with throwing speed " .. minetest.settings:get("spears_throw_speed") .. " and drag coeff. " .. minetest.settings:get("spears_drag_coeff"))
if minetest.settings:get_bool("log_mods") then
minetest.log("action", "[MOD] Spears loaded")
end

View File

@ -1,6 +1,8 @@
name = spears name = spears
depends = default depends = default
optional_depends = pigiron
description = Add spears to Minetest, versatile weapons that can be thrown description = Add spears to Minetest, versatile weapons that can be thrown
release = 3.0
author = Echoes91 author = Echoes91
title = Spears title = Spears
min_minetest_version = 5.4 min_minetest_version = 5.4

View File

@ -1,8 +0,0 @@
DISABLE_STONE_SPEAR = true
DISABLE_STEEL_SPEAR = true
DISABLE_COPPER_SPEAR = true
DISABLE_BRONZE_SPEAR = true
DISABLE_DIAMOND_SPEAR = true
DISABLE_OBSIDIAN_SPEAR = true
DISABLE_GOLD_SPEAR = true
SPEARS_THROW_SPEED = 13

View File

@ -54,44 +54,44 @@ function spears_register_spear(spear_type, desc, base_damage, toughness, materia
}) })
end end
if not DISABLE_STONE_SPEAR then if minetest.settings:get_bool('spears_enable_stone_spear') then
spears_register_spear('stone', 'Stone', 4, 20, 'group:stone') spears_register_spear('stone', 'Stone', 4, 20, 'group:stone')
minetest.log("info", "[MOD] Stone spear enabled")
end end
if minetest.get_modpath("pigiron") then if minetest.get_modpath("pigiron") then
if not DISABLE_IRON_SPEAR then if minetest.settings:get_bool('spears_enable_iron_spear') then
spears_register_spear('iron', 'Iron', 5.5, 30, 'pigiron:iron_ingot') spears_register_spear('iron', 'Iron', 5.5, 30, 'pigiron:iron_ingot')
end end
if not DISABLE_STEEL_SPEAR then if minetest.settings:get_bool('spears_enable_steel_spear') then
spears_register_spear('steel', 'Steel', 6, 35, 'default:steel_ingot') spears_register_spear('steel', 'Steel', 6, 35, 'default:steel_ingot')
end end
if not DISABLE_COPPER_SPEAR then if minetest.settings:get_bool('spears_enable_copper_spear') then
spears_register_spear('copper', 'Copper', 4.8, 30, 'default:copper_ingot') spears_register_spear('copper', 'Copper', 4.8, 30, 'default:copper_ingot')
end end
if not DISABLE_BRONZE_SPEAR then if minetest.settings:get_bool('spears_enable_bronze_spear') then
spears_register_spear('bronze', 'Bronze', 5.5, 35, 'default:bronze_ingot') spears_register_spear('bronze', 'Bronze', 5.5, 35, 'default:bronze_ingot')
end end
else else
if not DISABLE_STEEL_SPEAR then if minetest.settings:get_bool('spears_enable_steel_spear') then
spears_register_spear('steel', 'Steel', 6, 30, 'default:steel_ingot') spears_register_spear('steel', 'Steel', 6, 30, 'default:steel_ingot')
end end
if not DISABLE_COPPER_SPEAR then if minetest.settings:get_bool('spears_enable_copper_spear') then
spears_register_spear('copper', 'Copper', 5, 30, 'default:copper_ingot') spears_register_spear('copper', 'Copper', 5, 30, 'default:copper_ingot')
end end
if not DISABLE_BRONZE_SPEAR then if minetest.settings:get_bool('spears_enable_bronze_spear') then
spears_register_spear('bronze', 'Bronze', 6, 35, 'default:bronze_ingot') spears_register_spear('bronze', 'Bronze', 6, 35, 'default:bronze_ingot')
end end
end end
if minetest.settings:get_bool('spears_enable_obsidian_spear') then
if not DISABLE_OBSIDIAN_SPEAR then
spears_register_spear('obsidian', 'Obsidian', 8, 30, 'default:obsidian') spears_register_spear('obsidian', 'Obsidian', 8, 30, 'default:obsidian')
end end
if not DISABLE_DIAMOND_SPEAR then if minetest.settings:get_bool('spears_enable_diamond_spear') then
spears_register_spear('diamond', 'Diamond', 8, 40, 'default:diamond') spears_register_spear('diamond', 'Diamond', 8, 40, 'default:diamond')
end end
if not DISABLE_GOLD_SPEAR then if minetest.settings:get_bool('spears_enable_gold_spear') then
spears_register_spear('gold', 'Golden', 5, 40, 'default:gold_ingot') spears_register_spear('gold', 'Golden', 5, 40, 'default:gold_ingot')
end end