people/actions/pickup.lua

66 lines
2.4 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.pickup = function(state)
if not state.action.pos or type(state.action.pos) ~= "table" or not state.action.item then
dbg.v1(state.ent.name.." has invalid pickup action")
return true, false
end
local distance = vector.distance(state.pos, state.action.pos)
if distance > 8 then
dbg.v1(state.ent.name.." too far away to pickup "..minetest.pos_to_string(state.action.pos)..
" from "..minetest.pos_to_string(state.pos).." - "..distance)
return true, false
end
local yaw = vector.get_yaw(state.pos, state.action.pos)
-- Need to be fuzzy about comparing this, because of the
-- lua double/minetest float issue
if math.abs(yaw - state.yaw) > 0.2 then
dbg.v3(state.ent.name.." turning to face pickup position")
state.action = {"face", yaw=yaw, prevaction=state.action}
return false
end
state.anim = "mine"
if not state.action.pickuptime then
state.action.pickuptime = 1
return false
end
state.action.pickuptime = state.action.pickuptime - state.dtime
if state.action.pickuptime > 0 then
return false
end
-- Actually pick it up (if it's still there!)
local objects = minetest.get_objects_inside_radius(
state.action.pos, 1)
for _, obj in ipairs(objects) do
if obj:get_entity_name() == "__builtin:item" then
local ent = obj:get_luaentity()
if ent.itemstring then
if string.match("^" .. state.action.item .. " .*", ent.itemstring) then
obj:remove()
state.ent.inventory:add_item("main", ItemStack(ent.itemstring))
-- The object doesn't seem to get removed straight away (even
-- after returning from lua, as the docs state) so get rid of
-- its itemstring so we don't see it again!
ent.itemstring = ""
dbg.v1(state.ent.name.." picked up "..state.action.item.." at "..minetest.pos_to_string(state.action.pos))
return true, true
end
end
end
end
dbg.v1(state.ent.name.." could not pick up "..state.action.item.." at "..minetest.pos_to_string(state.action.pos))
return true, false
end