Advertisement
theinsekt

craftingExperiment

Sep 3rd, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. --theinsekt/craftstuff
  2. --experiment, untested
  3.  
  4. --need to be able to specify positions in a crafting table
  5. recipes={
  6. awkward={"craft",3,{"water bottle",1},{"nether wart",1}}
  7. }
  8. materials={
  9. awkward=1,
  10. }
  11.  
  12. function craftHelp(recipes,materials,want,amount,crafting)
  13.  --get how much of material already have
  14.  local haveAmount=materials[want]
  15.  if haveAmount==nil then haveAmount=0 end
  16.  
  17.  
  18.  if haveAmount<want then
  19.   if haveAmount>0 then
  20.    --add have this amount of material
  21.    crafting[#crafting+1]={want,haveAmount,"have"}
  22.    amount=amount-haveAmount
  23.    haveAmount=0
  24.    materials[want]=haveAmount
  25.   end
  26.   --need to craft the rest
  27.   --check if have materials in crafting recipe
  28.   if recipes[want]==nil then
  29.     --no crafting recipe, so need this material
  30.     crafting[#crafting+1]={want,amount,"need"}
  31.   else
  32.    --have crafting recipe, so add need to craft
  33.    crafting[#crafting+1]={want,amount,"craft"}
  34.    --check all the components in the crafting recipe
  35.    for k,v in pairs(recipes[want]) do
  36.     local how,amountOut,want2,amount2=unpack(v)
  37.     --todo take amountOut into account
  38.     --put as many craftings as as neccesary but
  39.     --put extra materials in the materials table
  40.     craftHelp(recipes,materials,want2,amount2*amount,crafting)
  41.    end
  42.   end
  43.  else--haveAmount>=want
  44.   crafting[#crafting+1]={want,amount,"have"}
  45.   haveAmount=haveAmount-amount
  46.   materials[want]=haveAmount
  47.   amount=0
  48.  end
  49.  --if there are no need entries,
  50.  --it's then possible to go from bottom up in the
  51.  --crafting, and perform the crafting actions, if
  52. end
  53.  
  54. function craft(recipes,materials,want,amount)
  55.  local res={}
  56.  craftHelp(recipes,materials,want,amount,res)
  57.  return res
  58. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement