people/actions/attackplayer.lua

64 lines
1.8 KiB
Lua

local dbg
if moddebug then dbg=moddebug.dbg("people") else dbg={v1=function() end,v2=function() end,v3=function() end} end
people.actions.attackplayer = function(state)
if not state.action.name or type(state.action.name) ~= "string" then
dbg.v1(state.ent.name.." has invalid attack target")
return true, false
end
local player = minetest.get_player_by_name(state.action.name)
if not player then
dbg.v1(state.ent.name.." can't find "..state.action.name.." to attack them")
return true, false
end
local targetpos = player:getpos()
local dir = vector.subtract(targetpos, state.pos)
local dist = vector.length(dir)
if dist > 30 then
dbg.v1(state.ent.name.." target out of range")
return true, false
end
if dist < 2 then
-- We're near enough...
if not minetest.line_of_sight(state.pos, targetpos, 0.5) then
dbg.v2(state.ent.name.." can't attack, no line of sight")
return true, false
end
local caps = {
full_punch_interval=2,
groupcaps={
fleshy={times={1, 1, 1}},
snappy={times={1, 1, 1}},
},
damage_groups = {fleshy=5},
}
player:punch(state.ent.object, 5, caps, dir)
state.yaw = vector.get_yaw(state.pos, targetpos)
state.wait = 2
if state.ent.on_done_attack then
state.ent.on_done_attack(state.ent)
end
return false
end
local dir = math.random(math.pi*2)
targetpos = vector.add(targetpos, vector.from_speed_yaw(2, dir))
targetpos = vector.round(targetpos)
-- TODO find surface Y
dbg.v3(state.ent.name.." attacking "..state.action.name.." via "..minetest.pos_to_string(targetpos))
state.action = {"go", pos=targetpos, prevaction=state.action}
return false
end