people/actions/dig.lua

105 lines
3.6 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.dig = function(state)
if not state.action.pos or type(state.action.pos) ~= "table" then
dbg.v1(state.ent.name.." has invalid dig action")
return true, false
end
local distance = vector.distance(state.pos, state.action.pos)
if distance > 7 then
dbg.v1(state.ent.name.." too far away to dig "..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 dig position")
state.action = {"face", yaw=yaw, prevaction=state.action}
return false
end
state.anim = "mine"
if not state.action.digtime then
state.action.digtime = 1
return false
end
state.action.digtime = state.action.digtime - state.dtime
if state.action.digtime > 0 then
return false
end
-- Do the dig - a bit hacky for now, need to look at
-- * selecting correct tool
-- * proper drops (e.g. custom handlers in other mods)
local n = minetest.get_node(state.action.pos)
local dign = n.name
if false then
-- old style, do it directly....
if not minetest.dig_node(state.action.pos) then
dbg.v1(state.ent.name.." failed to dig "..n.name.." at "..minetest.pos_to_string(state.action.pos))
-- Cancel gathering of of the item type if it hits this problem
-- otherwise an infinite repetition will happen.
if #state.gather.items > 0 then
local r = 0
for i, nn in ipairs(state.gather.items) do
if nn == n.name then
r = i
break
end
end
if r ~= 0 then
table.remove(state.gather.items, r)
end
end
return true, false
end
for _, item in pairs(minetest.get_node_drops(n.name, "")) do
state.ent.inventory:add_item("main", ItemStack(item))
dbg.v1(state.ent.name.."got "..item.." from dig")
end
else
-- new style, do it via node_dig
local oldnn = n.name
minetest.node_dig(state.action.pos, n, people.get_fake_player(state.ent))
-- Unfortunately, no indication of success/failure is returned, so...
n = minetest.get_node(state.action.pos)
if n.name == oldnn then
dbg.v1(state.ent.name.." failed to dig "..n.name.." at "..minetest.pos_to_string(state.action.pos).. " - it was still there afterwards")
-- Cancel gathering of of the item type if it hits this problem
-- otherwise an infinite repetition will happen.
if #state.gather.items > 0 then
local r = 0
for i, nn in ipairs(state.gather.items) do
if nn == n.name then
r = i
break
end
end
if r ~= 0 then
table.remove(state.gather.items, r)
end
end
return true, false
end
end
dbg.v1(state.ent.name.." dug "..dign.." at "..minetest.pos_to_string(state.action.pos))
return true, true
end