people/actions/drop.lua

48 lines
1.3 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.drop = function(state)
if not state.action.item or type(state.action.item) ~= "string" then
dbg.v1(state.ent.name.." has invalid drop action")
return true, false
end
local item = state.action.item
if not minetest.registered_items[item] then
-- Not a registered item name, maybe it's the description instead?
local litem = string.lower(item)
for k, v in pairs(minetest.registered_items) do
if string.lower(v.description) == litem then
item = k
break
end
end
end
if state.action.count then
item = item.." "..state.action.count
end
local stack = state.ent.inventory:remove_item("main", item)
if not stack then
dbg.v1(state.ent.name.." couldn't drop "..item)
return true, false
end
local v = vector.from_speed_yaw(1, state.yaw)
local p = {x=state.pos.x+v.x, y=state.pos.y+1.5, z=state.pos.z+v.z}
local obj = minetest.add_item(p, stack)
if obj then
v.x = v.x * 2
v.y = v.y * 2 + 1
v.z = v.z * 2
obj:setvelocity(v)
end
dbg.v1(state.ent.name.." dropped "..item)
return true, true
end