minetest-mod-nssm/nssm_spears.lua

364 lines
8.1 KiB
Lua
Raw Normal View History

2018-08-08 10:56:14 +01:00
2022-09-28 19:01:53 +01:00
-- functions
local function spears_shot(itemstack, player)
2018-08-09 11:36:34 +01:00
2018-08-08 10:56:14 +01:00
local spear = itemstack:get_name() .. '_entity'
2018-08-08 12:20:07 +01:00
local playerpos = player:get_pos()
2022-09-27 19:26:58 +01:00
local obj = minetest.add_entity({
x = playerpos.x,
y = playerpos.y + 1.5,
z = playerpos.z
}, spear)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
local dir = player:get_look_dir()
2022-09-28 19:01:53 +01:00
if spear == "nssm:spear_of_peace_entity" then
sp = 32
gravity = 9.8
else
sp = 16
gravity = 9.8
end
2018-08-08 10:56:14 +01:00
local dr = .3
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
obj:set_velocity({x = dir.x * sp, y = dir.y * sp, z = dir.z * sp})
obj:set_acceleration({x = -dir.x * dr, y = -gravity, z = -dir.z * dr})
obj:set_yaw(player:get_look_yaw() + math.pi)
2018-08-08 10:56:14 +01:00
obj:get_luaentity().wear = itemstack:get_wear()
2022-09-28 19:01:53 +01:00
minetest.sound_play("spears_sound", {pos = playerpos}, true)
2018-08-08 10:56:14 +01:00
return true
end
2018-08-09 11:36:34 +01:00
local function spears_set_entity(kind, eq, toughness)
2022-09-28 19:01:53 +01:00
local SPEAR_ENTITY = {
2018-08-08 10:56:14 +01:00
physical = false,
2022-09-27 19:26:58 +01:00
timer = 0,
2018-08-08 10:56:14 +01:00
visual = "wielditem",
2022-09-27 19:26:58 +01:00
visual_size = {x = 0.15, y = 0.1},
2018-08-08 10:56:14 +01:00
textures = {"nssm:spear_" .. kind},
lastpos={},
collisionbox = {0,0,0,0,0,0},
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
on_punch = function(self, puncher)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
if puncher then
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
if puncher:is_player() then
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
local stack = {
name = "nssm:spear_" .. kind,
wear = self.wear + 65535 / toughness
}
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
local inv = puncher:get_inventory()
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
if inv:room_for_item("main", stack) then
inv:add_item("main", stack)
self.object:remove()
end
end
end
2022-09-27 19:26:58 +01:00
end
2018-08-08 10:56:14 +01:00
}
2022-09-27 19:26:58 +01:00
2018-08-08 10:56:14 +01:00
SPEAR_ENTITY.on_step = function(self, dtime)
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
self.timer=self.timer + dtime
2022-09-28 19:01:53 +01:00
2018-08-08 12:20:07 +01:00
local pos = self.object:get_pos()
2018-08-08 10:56:14 +01:00
local node = minetest.get_node(pos)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
if not self.wear then
self.object:remove()
return
end
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
if self.lastpos.x~=nil then
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
if node.name ~= "air"
and not (string.find(node.name, "grass")
and not string.find(node.name, "dirt"))
and not string.find(node.name, "flowers:")
and not string.find(node.name, "farming:") then
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
self.object:remove()
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
if self.wear + 65535 / toughness < 65535 then
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
minetest.add_item(self.lastpos, {
name = "nssm:spear_" .. kind,
wear = self.wear + 65535 / toughness
})
2018-08-08 10:56:14 +01:00
end
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
elseif self.timer > 0.2 then
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
local objs = minetest.get_objects_inside_radius({
x = pos.x, y = pos.y, z = pos.z}, 1)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
for k, obj in pairs(objs) do
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
if obj:get_luaentity() ~= nil then
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
if obj:get_luaentity().name ~= "nssm:spear_" .. kind .. "_entity"
and obj:get_luaentity().name ~= "__builtin:item" then
2022-09-28 19:01:53 +01:00
2018-08-08 12:20:07 +01:00
local speed = vector.length(self.object:get_velocity())
2022-09-27 19:26:58 +01:00
local damage = (speed + eq) ^ 1.12 - 20
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
obj:punch(self.object, 1.0, {
2022-09-27 19:26:58 +01:00
full_punch_interval = 1.0,
damage_groups={fleshy = damage}
2018-08-08 10:56:14 +01:00
}, nil)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
self.object:remove()
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
if self.wear + 65535 / toughness < 65535 then
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
minetest.add_item(self.lastpos, {
name = "nssm:spear_" .. kind,
wear = self.wear + 65535 / toughness
})
2018-08-08 10:56:14 +01:00
end
end
2023-04-26 07:16:33 +01:00
break -- only harm first entity hit
2018-08-08 10:56:14 +01:00
end
end
end
end
2022-09-27 19:26:58 +01:00
self.lastpos={x = pos.x, y = pos.y, z = pos.z}
2018-08-08 10:56:14 +01:00
end
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
return SPEAR_ENTITY
end
2022-09-28 19:01:53 +01:00
-- Tools
2018-08-08 10:56:14 +01:00
2018-08-09 11:36:34 +01:00
local function spears_register_spear(kind, desc, eq, toughness, material)
2018-08-08 10:56:14 +01:00
minetest.register_tool("nssm:spear_" .. kind, {
description = desc .. " Spear",
2018-08-09 11:36:34 +01:00
wield_image = "spear_" .. kind .. ".png",
2018-08-08 10:56:14 +01:00
inventory_image = "spear_" .. kind .. ".png^[transform4",
2022-09-27 19:26:58 +01:00
wield_scale= {x = 2, y = 1, z = 1},
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
on_drop = function(itemstack, user, pointed_thing)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
spears_shot(itemstack, user)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
if not minetest.setting_getbool("creative_mode") then
itemstack:take_item()
end
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
return itemstack
end,
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
on_place = function(itemstack, user, pointed_thing)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
minetest.add_item(pointed_thing.above, itemstack)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
if not minetest.setting_getbool("creative_mode") then
itemstack:take_item()
end
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
return itemstack
end,
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
tool_capabilities = {
full_punch_interval = 1.3,
2022-09-27 19:26:58 +01:00
max_drop_level = 1,
groupcaps = {
snappy = {times = {[3]=0.2, [2]=0.2, [1]=0.2},
uses = toughness, maxlevel = 1},
2018-08-08 10:56:14 +01:00
},
2022-09-27 19:26:58 +01:00
damage_groups = {fleshy = eq}
2018-08-08 10:56:14 +01:00
}
})
2018-08-09 11:36:34 +01:00
2022-09-28 19:01:53 +01:00
local SPEAR_ENTITY = spears_set_entity(kind, eq, toughness)
2018-08-09 11:36:34 +01:00
2018-08-08 10:56:14 +01:00
minetest.register_entity("nssm:spear_" .. kind .. "_entity", SPEAR_ENTITY)
2018-08-09 11:36:34 +01:00
2018-08-08 10:56:14 +01:00
minetest.register_craft({
2022-09-27 19:26:58 +01:00
output = "nssm:spear_" .. kind,
2018-08-08 10:56:14 +01:00
recipe = {
2022-09-27 19:26:58 +01:00
{"group:wood", "group:wood", material}
2018-08-08 10:56:14 +01:00
}
})
2018-08-09 11:36:34 +01:00
2018-08-08 10:56:14 +01:00
minetest.register_craft({
2022-09-27 19:26:58 +01:00
output = "nssm:spear_" .. kind,
2018-08-08 10:56:14 +01:00
recipe = {
2022-09-27 19:26:58 +01:00
{material, "group:wood", "group:wood"}
2018-08-08 10:56:14 +01:00
}
})
end
2022-09-27 19:26:58 +01:00
spears_register_spear("ant", "Ant", 6, 25, "nssm:ant_mandible")
spears_register_spear("mantis", "Mantis", 6, 10, "nssm:mantis_claw")
spears_register_spear("manticore", "Manticore", 8, 8, "nssm:manticore_spine")
spears_register_spear("ice_tooth", "Ice Tooth", 16, 200, "nssm:ice_tooth")
spears_register_spear("little_ice_tooth", "Little Ice Tooth", 7, 10, "nssm:little_ice_tooth")
spears_register_spear("duck_beak", "Duck Beak", 5, 6, "nssm:duck_beak")
spears_register_spear("felucco_horn", "Felucco Horn", 7, 9, "nssm:felucco_horn")
2018-08-08 10:56:14 +01:00
2022-09-28 19:01:53 +01:00
-- Spear of peace
2018-08-09 11:36:34 +01:00
minetest.register_tool("nssm:spear_of_peace", {
description = "Spear of Peace",
wield_image = "spear_of_peace.png",
inventory_image = "spear_of_peace.png^[transform4",
2022-09-27 19:26:58 +01:00
wield_scale= {x = 4, y = 2, z = 2},
2022-09-28 19:01:53 +01:00
2018-08-09 11:36:34 +01:00
on_drop = function(itemstack, user, pointed_thing)
2022-09-28 19:01:53 +01:00
2018-08-09 11:36:34 +01:00
spears_shot(itemstack, user)
2022-09-28 19:01:53 +01:00
2018-08-09 11:36:34 +01:00
if not minetest.setting_getbool("creative_mode") then
itemstack:take_item()
end
2022-09-28 19:01:53 +01:00
2018-08-09 11:36:34 +01:00
return itemstack
end,
2022-09-28 19:01:53 +01:00
2018-08-09 11:36:34 +01:00
on_place = function(itemstack, user, pointed_thing)
2022-09-28 19:01:53 +01:00
2018-08-09 11:36:34 +01:00
minetest.add_item(pointed_thing.above, itemstack)
2022-09-28 19:01:53 +01:00
2018-08-09 11:36:34 +01:00
if not minetest.setting_getbool("creative_mode") then
itemstack:take_item()
end
2022-09-28 19:01:53 +01:00
2018-08-09 11:36:34 +01:00
return itemstack
end,
2022-09-28 19:01:53 +01:00
2018-08-09 11:36:34 +01:00
tool_capabilities = {
full_punch_interval = 0.7,
2022-09-27 19:26:58 +01:00
max_drop_level = 1,
groupcaps = {
snappy = {times = {[3]=0.2, [2]=0.2, [1]=0.2}, uses = 500, maxlevel = 1}
},
damage_groups = {fleshy = 18}
2018-08-09 11:36:34 +01:00
}
})
local function spears_set_sentity(kind, eq, toughness)
2022-09-28 19:01:53 +01:00
local SUPERSPEAR_ENTITY = {
2018-08-08 10:56:14 +01:00
physical = false,
2022-09-27 19:26:58 +01:00
timer = 0,
2018-08-08 10:56:14 +01:00
visual = "wielditem",
2022-09-27 19:26:58 +01:00
visual_size = {x = 0.15, y = 0.1},
2018-08-08 10:56:14 +01:00
textures = {"nssm:spear_" .. kind},
lastpos={},
collisionbox = {0,0,0,0,0,0},
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
on_punch = function(self, puncher)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
if puncher then
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
if puncher:is_player() then
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
local stack = {
name = "nssm:spear_" .. kind,
wear = self.wear + 65535 / toughness
}
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
local inv = puncher:get_inventory()
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
if inv:room_for_item("main", stack) then
inv:add_item("main", stack)
self.object:remove()
end
end
end
2022-09-27 19:26:58 +01:00
end
2018-08-08 10:56:14 +01:00
}
2018-08-09 11:36:34 +01:00
2018-08-08 10:56:14 +01:00
SUPERSPEAR_ENTITY.on_step = function(self, dtime)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
self.timer=self.timer+dtime
2022-09-28 19:01:53 +01:00
2018-08-08 12:20:07 +01:00
local pos = self.object:get_pos()
2018-08-08 10:56:14 +01:00
local node = minetest.get_node(pos)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
if not self.wear then
self.object:remove()
return
end
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
if self.lastpos.x ~= nil then
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
if node.name ~= "air"
and not (string.find(node.name, "grass")
and not string.find(node.name, "dirt"))
and not string.find(node.name, "flowers:")
and not string.find(node.name, "farming:") then
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
self.object:remove()
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
if self.wear + 65535 / toughness < 65535 then
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
minetest.add_item(self.lastpos, {
name = "nssm:spear_" .. kind,
wear = self.wear + 65535 / toughness
})
2018-08-08 10:56:14 +01:00
end
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
elseif self.timer > 0.2 then
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
local objs = minetest.get_objects_inside_radius({
x = pos.x, y = pos.y, z = pos.z}, 1)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
for k, obj in pairs(objs) do
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
if obj:get_luaentity() ~= nil then
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
if obj:get_luaentity().name ~= "nssm:spear_" .. kind .. "_entity"
and obj:get_luaentity().name ~= "__builtin:item" then
2022-09-28 19:01:53 +01:00
2018-08-08 12:20:07 +01:00
local speed = vector.length(self.object:get_velocity())
2022-09-27 19:26:58 +01:00
local damage = (speed + eq) ^ 1.12 - 20
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
obj:punch(self.object, 1.0, {
2022-09-27 19:26:58 +01:00
full_punch_interval = 1.0,
damage_groups={fleshy = damage}
2018-08-08 10:56:14 +01:00
}, nil)
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
self.object:remove()
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
if self.wear + 65535 / toughness < 65535 then
2022-09-28 19:01:53 +01:00
2022-09-27 19:26:58 +01:00
minetest.add_item(self.lastpos, {
name = "nssm:spear_" .. kind,
wear = self.wear + 65535 / toughness
})
2018-08-08 10:56:14 +01:00
end
end
end
end
end
end
2022-09-27 19:26:58 +01:00
self.lastpos = {x = pos.x, y = pos.y, z = pos.z}
2018-08-08 10:56:14 +01:00
end
2022-09-28 19:01:53 +01:00
2018-08-08 10:56:14 +01:00
return SUPERSPEAR_ENTITY
end
2018-08-09 11:36:34 +01:00
2022-09-28 19:01:53 +01:00
SUPERSPEAR_ENTITY = spears_set_sentity("of_peace", 30, 300)
2018-08-09 11:36:34 +01:00
minetest.register_entity("nssm:spear_of_peace_entity", SUPERSPEAR_ENTITY)
minetest.register_craft({
2022-09-27 19:26:58 +01:00
output = "nssm:spear_of_peace",
2018-08-09 11:36:34 +01:00
recipe = {
2022-09-27 19:26:58 +01:00
{"nssm:wrathful_moranga", "group:wood", "group:wood"}
2018-08-09 11:36:34 +01:00
}
})