people/actions/obtain.lua

70 lines
2.2 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.obtain = function(state)
if type(state.action.num) ~= "number" then
dbg.v1(state.ent.name.." has invalid num for obtain action")
return true, false
end
if type(state.action.item) ~= "string" then
dbg.v1(state.ent.name.." has invalid item for obtain action")
return true, false
end
-- TODO check it's a real item too!
-- See how many we've got, and therefore how many we need...
local inv = state.ent.inventory
local count = 0
for i, stack in ipairs(inv:get_list("main")) do
local nn = stack:get_name()
if nn == state.action.item then
count = count + stack:get_count()
end
end
local needed = state.action.num - count
if needed <= 0 then
dbg.v2(state.ent.name.." has "..count.." "..state.action.item)
return true, true
end
if not state.action.crafted then
state.action.crafted = true
state.action = {"craft", item=state.action.item, num=needed,
prevaction=state.action}
return false
end
if not state.action.bought then
state.action.bought = true
-- TODO - Here is where we need knowledge or memory of what goods can
-- be purchased where. Before we have that, we're just going to hard
-- code a few things to test the concept...
local where = nil
if state.action.item == "default:wood" then
where = "Building Supplies 2"
elseif state.action.item == "default:stone" or
state.action.item == "default:stonebrick" or
state.action.item == "default:cobble" then
where = "Building Supplies 1"
end
if where then
state.action = {"go", name=where, prevaction=
{"buy", item=state.action.item, num=needed, prevaction=
state.action}}
return false
else
dbg.v3(state.ent.name.." doesn't know where to buy "..state.action.item)
end
end
dbg.v1(state.ent.name.." can't obtain "..state.action.item)
return true, false
end