people/actions/follow.lua

38 lines
1.2 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.follow = function(state)
if not state.action.name or type(state.action.name) ~= "string" then
dbg.v1(state.ent.name.." has invalid follow 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 follow them")
return true, false
end
local targetpos = player:getpos()
local dist = vector.length(vector.subtract(targetpos, state.pos))
if dist < 5 then
-- We're near enough, so just face the target and wait a bit
state.yaw = vector.get_yaw(state.pos, targetpos)
state.wait = 4
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.." following "..state.action.name.." via "..minetest.pos_to_string(targetpos))
state.action = {"go", pos=targetpos, prevaction=state.action}
return false
end