kLeeNex24

inventory API for ComputerCraft

Sep 16th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.69 KB | None | 0 0
  1. --STUB needs to be implemented properly
  2. function getStackSize(type)
  3.   return 64
  4. end
  5.  
  6.  
  7. local function dropIn(inv,type,quantity)
  8.   somePlaced=false
  9.   for i=1,inv.numSlots,1 do
  10.     if inv.slots[i].type==type then
  11.       --how many can fit?
  12.       if quantity+inv.slots[i].quantity <=64 then
  13.         inv.slots[i].quantity=inv.slots[i].quantity+quantity
  14.         somePlaced=true
  15.         quantity=0
  16.         break
  17.       else
  18.         quantity=quantity-(64-inv.slots[i].quantity)
  19.         inv.slots[i].quantity=64
  20.         somePlaced=true
  21.       end
  22.     elseif inv.slots[i].type==0 then
  23.       inv.slots[i].type=type
  24.       inv.slots[i].quantity=quantity
  25.         quantity=0
  26.       somePlaced=true
  27.       break
  28.     end
  29.   end
  30.  
  31.   if inv.autoSave then
  32.     save(inv)
  33.   end
  34.   return somePlaced, quantity
  35. end
  36.  
  37.  
  38. local function suckOut(inv)
  39.   qtyTaken=0
  40.   type=0
  41.   for i=1,inv.numSlots,1 do
  42.     if inv.slots[i].quantity~=0 then
  43.       qtyTaken=inv.slots[i].quantity
  44.       type=inv.slots[i].type
  45.       inv.slots[i].type=0
  46.       inv.slots[i].quantity=0
  47.       if inv.autoSave then
  48.         save(inv)
  49.       end
  50.       return type, qtyTaken
  51.     end
  52.   end
  53.   return 0, 0
  54. end
  55.  
  56.  
  57. local function getQuantity(inv,type)
  58.   count=0
  59.   for n,slot in pairs(inv.slots) do
  60.     if slot.type==type then
  61.       count=count+slot.quantity
  62.     end
  63.   end
  64.   return count
  65. end
  66.  
  67. local function findSlot(inv,type,after)
  68.   next=after and after+1 or 1
  69.   for i=next,inv.numSlots,1 do
  70.     if inv.slots[next].type==type then
  71.       return next
  72.     end
  73.     next=next+1
  74.   end
  75.   return 0
  76. end
  77.  
  78. function getNextUnknownType(inv)
  79.   local i=1
  80.   local type="unknown"
  81.   while true do
  82.     if inv:findSlot(type)==0 then
  83.       return type
  84.     end
  85.     i=i+1
  86.     type="unknown"..i
  87.   end  
  88. end
  89.  
  90.  
  91. function save(inv,filename)
  92.   if filename==nil or filename=="" then
  93.     filename=inv.fileName
  94.   end
  95.   if filename==nil or filename=="" then
  96.     filename=".inventory"
  97.     count=1
  98.     while fs.exists(filename) do
  99.       count=count+1
  100.       filename=".inventory"..count
  101.     end
  102.   end
  103.  
  104.   inv.fileName=filename
  105.  
  106.   local file=fs.open(filename,"w")
  107.   file.write("inv\n")
  108.   file.write(inv.numSlots.."\n")
  109.   for i=1,inv.numSlots,1 do
  110.     file.write(inv.slots[i].quantity.." "..inv.slots[i].type.."\n")
  111.   end
  112.   file.close()
  113. end
  114.  
  115.  
  116. function load(filename)
  117.   local inv=nil
  118.   if not fs.exists(filename) then
  119.     return nil
  120.   end
  121.   local file=fs.open(filename,"r")
  122.   local header=file.readLine()
  123.   if header=="inv" then
  124.     local size=file.readLine()+0
  125.     inv=inventory.create(inv,false)
  126.     inv.fileName=filename
  127.     for i=1,size,1 do
  128.       local line=file.readLine()
  129.       local spacePos=string.find(line," ")
  130.       inv.slots[i].quantity=string.sub(line,1,spacePos-1)+0
  131.       inv.slots[i].type=string.sub(line,spacePos+1)
  132.     end  
  133.   end
  134.  
  135.   return inv
  136.    
  137. end
  138.  
  139.  
  140. local function display(inv)
  141.    str=""
  142.    for i=1,inv.numSlots,1 do
  143.      str=str..i.." - "..inv.slots[i].type.." x"..inv.slots[i].quantity.."\t"
  144.    end
  145.    print(str)
  146. end
  147.  
  148. local function setType(inv, slot, type)
  149.   prevType=inv.slots[slot].type
  150.   inv.slots[slot].type=type
  151.  
  152.   --if prevtype was specified at all, get others identified as same
  153.   if prevType~=0 then
  154.     for i=1,inv.numSlots,1 do
  155.       if inv.slots[i].type==prevType then
  156.         inv.slots[i].type=type
  157.       end
  158.     end
  159.   end
  160.  
  161.   if inv.autoSave then
  162.     save(inv)
  163.   end
  164. end
  165.  
  166. inventoryTemplate= {
  167.   dropIn=dropIn,
  168.   suckOut=suckOut,
  169.   getQuantity=getQuantity,
  170.   display=display,
  171.   findSlot=findSlot,
  172.   setType=setType,
  173.   getNextUnknownType=getNextUnknownType,
  174.  
  175.   autoSave=false,
  176.   fileName=""
  177. }
  178.  
  179.  
  180. local function initializeTurtleInventory(inv)
  181.   --everything will be unknown..
  182.   unknownCount=0
  183.  
  184.   for i=1,inv.numSlots,1 do
  185.     q=turtle.getItemCount(i)
  186.     if q>0 then
  187.       --compare to previous
  188.       turtle.select(i)
  189.       inv.slots[i].quantity=q
  190.       for j=1,i-1,1 do
  191.         if turtle.compareTo(j) then
  192.           inv.slots[i].type=inv.slots[j].type
  193.           break
  194.         end
  195.       end
  196.       if inv.slots[i].type==0 then
  197.         unknownCount=unknownCount+1
  198.         inv.slots[i].type="unknown"..unknownCount
  199.       end
  200.     end
  201.   end
  202. end
  203.  
  204.  
  205. function create(numSlots, isMe)
  206.   newInv={}
  207.   if numSlots==nil then
  208.     numSlots=16 --assume minimum, for safety
  209.   end
  210.  
  211.   newInv.numSlots=numSlots
  212.   newInv.slots={}
  213.   newInv.isMe=isMe and true or false
  214.    
  215.   for i=1,numSlots,1 do
  216.     newInv.slots[i]={}
  217.     newInv.slots[i].quantity=0
  218.     newInv.slots[i].type=0
  219.   end
  220.  
  221.   setmetatable(newInv,{__index=inventoryTemplate})
  222.   --if it's an isMe, initialize automatically
  223.   if isMe then
  224.     initializeTurtleInventory(newInv)
  225.   end
  226.  
  227.   return newInv
  228.    
  229. end
Advertisement
Add Comment
Please, Sign In to add comment