Add missing pickup action file

master
Ciaran Gultnieks 2014-11-16 21:56:38 +00:00
parent b82f54fba3
commit dd48fe72e6
2 changed files with 85 additions and 2 deletions

View File

@ -39,13 +39,31 @@ people.actions.dig = function(state)
-- Do the dig - a bit hacky for now, need to look at
-- * selecting correct tool
-- * proper drops (e.g. custom handlers in other mods)
-- * handling failures (e.g. node protection)
local n = minetest.get_node(state.action.pos)
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
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
minetest.dig_node(state.action.pos)
dbg.v1(state.ent.name.." dug "..n.name.." at "..minetest.pos_to_string(state.action.pos))
return true

65
actions/pickup.lua Normal file
View File

@ -0,0 +1,65 @@
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
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
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
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
end