people/actions/harvest.lua

64 lines
1.9 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.harvest = function(state)
if not state.action.name or type(state.action.name) ~= "string" then
dbg.v1(state.ent.name.." has invalid harvest action")
return true, false
end
local hitit = nil
local objects = minetest.get_objects_inside_radius(state.pos, 6)
for k, obj in ipairs(objects) do
if not obj:is_player() then
local ent = obj:get_luaentity()
if ent and ent.name == state.action.name then
hitit = ent
break
end
end
end
if not hitit then
dbg.v1(state.ent.name.." can't find "..state.action.name)
return true, false
end
local yaw = vector.get_yaw(state.pos, hitit.object:getpos())
-- 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.harvesttime then
state.action.harvesttime = 1
return false
end
state.action.harvesttime = state.action.harvesttime - state.dtime
if state.action.harvesttime > 0 then
return false
end
local spinfo = animals.species[hitit.species]
if not spinfo.is_harvestable or not spinfo.is_harvestable(hitit) then
dbg.v1(state.ent.name.." can't harvest from "..state.action.name)
return true, false
end
-- Actually do it, if it's still there
spinfo.on_harvest(hitit, people.get_fake_player(state.ent))
dbg.v1(state.ent.name.." harvested from "..state.action.name)
return true, false
end