people/actions/claimplot.lua

40 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.claimplot = function(state)
if not state.action.name or type(state.action.name) ~= "string" then
dbg.v1(state.ent.name.." has invalid claimplot action")
return true, false
end
local destpos = footpath.get_nodepos(name)
if destpos then
dbg.v1(state.ent.name.." can't claim "..state.action.name.." - it already exists")
return true, false
end
if not state.action.at then
local plot = footpath.nearest_vacant_plot(state.pos)
if not plot then
dbg.v1(state.ent.name.." can't claim "..state.action.name.." - there are no free plots")
return true, false
end
state.action.at = plot.name
state.action = {"go", name=plot.name, successaction=state.action}
return false
end
local result = footpath.claim_plot(state.action.at, state.action.name)
if result ~= true then
dbg.v1(state.ent.name.." failed to claim plot "..state.action.name.." at "..state.action.at.." - "..result)
return true, false
end
dbg.v1(state.ent.name.." claimed plot "..state.action.name.." at "..state.action.at)
return true, true
end