Advertisement
BasketMC

ComputerCraft: Storage Cell API

Dec 3rd, 2015
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.84 KB | None | 0 0
  1. -- apis/StorageCell
  2.  
  3. os.loadAPI("apis/BasketNet")
  4. os.loadAPI("apis/ChestManagement")
  5. os.loadAPI("apis/Database")
  6.  
  7. local inventory = nil
  8.  
  9. local iDepth = 0
  10. local iRotation = 0
  11.  
  12. function init()
  13.     if not rednet.isOpen("left") then
  14.         rednet.open("left")
  15.     end
  16.  
  17.     while true do
  18.         if turtle.getItemCount(16) > 0 then
  19.             break
  20.         end
  21.         sleep(5)
  22.     end
  23.  
  24.     while true do
  25.         BasketNet.sendToMasterControl("Name " ..  os.getComputerLabel(), "BasketNet")
  26.         local iSenderID, sMessage, sProtocol = BasketNet.receiveFromMasterControl(5, "BasketNetAck")
  27.             if iSenderID ~= nil and sMessage == "NameReceived" then
  28.             break
  29.         end
  30.     end
  31.  
  32.     while true do
  33.         BasketNet.sendToMasterControl("Register StorageCell", "BasketNet")
  34.         local iSenderID, sMessage, sProtocol = BasketNet.receiveFromMasterControl(5, "BasketNetAck")
  35.             if iSenderID ~= nil and sMessage == "Ack" then
  36.             break
  37.         end
  38.     end
  39.  
  40.     returnHome()
  41.  
  42.     if turtle.getFuelLevel() < 1000 then
  43.         print("I'm low on fuel")
  44.         return
  45.     end
  46.  
  47.     inventory = Database.loadTable("inventory")
  48.     if inventory == nil then
  49.         takeInventory()
  50.     end
  51.  
  52.     --storeBuffer()
  53. end
  54.  
  55. function returnHome()
  56.  
  57.     iDepth = 0
  58.     iRotation = 0
  59.  
  60.     while turtle.up() do end
  61.     for i = 1, 4, 1 do
  62.         local status, info = turtle.inspect()
  63.         if status then
  64.             if info.name == "IronChest:BlockIronChest" and info.metadata == 2 then
  65.                 return true
  66.             end
  67.         end
  68.         turtle.turnRight()
  69.     end
  70.     return false
  71. end
  72.  
  73. function takeInventory()
  74.  
  75.     print("Taking Inventory...")
  76.  
  77.     inventory = {}
  78.  
  79.     Database.saveTable("inventory", nil)
  80.  
  81.     returnHome()
  82.     local i = 1
  83.  
  84.     -- While there is an empty space below the turtle
  85.     while not turtle.detectDown() do
  86.         turtle.turnRight()
  87.         inventory[i] = ChestManagement.whatIsInChest()
  88.         i = i + 1
  89.         if math.fmod(i, 4) == 0 then
  90.             turtle.down()
  91.         end
  92.     end
  93.  
  94.     Database.saveTable("inventory", inventory)
  95.  
  96.     returnHome()
  97.  
  98.     displayInventory()
  99.  
  100.     return true
  101. end
  102.  
  103. function processRequest()
  104.         local iSenderID, sMessage, sProtocol = BasketNet.receiveFromMasterControl(60)
  105.         if iSenderID == nil then
  106.                 return false
  107.         end
  108.  
  109.     print("Processing Request from " .. iSenderID .. "\n\t\t\t" .. sMessage)
  110.  
  111.     local tParsedMessage = {}
  112.  
  113.     for sPart in string.gmatch(sMessage, "%S+") do
  114.         table.insert(tParsedMessage, sPart)
  115.     end
  116.  
  117.     if sMessage == "Ready?" then
  118.         BasketNet.sendToMasterControl("Ready", "BasketNetReadyCheck")
  119.     elseif sMessage == "GetInventory" then
  120.         sendInventory()
  121.     elseif sMessage == "Store" then
  122.         storeBuffer()
  123.     elseif tParsedMessage[1] == "Retrieve" then
  124.        
  125.             local iSenderID, sMessage, sProtocol = BasketNet.receiveFromMasterControl(3, "BasketNetRetrieve")
  126.             if iSenderID == nil then
  127.                     return false
  128.             end
  129.  
  130.         if type(sMessage) == "table" then
  131.             BasketNet.sendToMasterControl("Retrieving", "BasketNetRetrieve")
  132.             print("\tRetrieving " .. tParsedMessage[3] .. "x " .. sMessage.name)
  133.             retrieveItems(sMessage, tParsedMessage[3])
  134.         end
  135.        
  136.     elseif sMessage == "Reboot" then
  137.         os.reboot()
  138.     end
  139. end
  140.  
  141. function sendInventory()
  142.     print("\t\t\tSending Inventory to Master Control")
  143.     BasketNet.sendToMasterControl(inventory, "BasketNetGetInventory")
  144. end
  145.  
  146. function requestRefueling()
  147.  
  148.     while true do
  149.         BasketNet.sendToMasterControl("Ready", "RefuelRequest")
  150.         local iSenderId, sMessage, sProtocol = BasketNet.receiveFromMasterControl(5)
  151.             if iSenderID ~= nil and sMessage == "RequestReceived" then
  152.             break
  153.         end
  154.     end
  155.  
  156.     while true do
  157.         local iSenderId, sMessage, sProtocol = BasketNet.receiveFromMasterControl(10)
  158.             if iSenderID ~= nil and sMessage == "Refuel" then
  159.             break
  160.         end
  161.     end
  162.  
  163.     returnHome()
  164.  
  165.     for i = 1, 15, 1 do
  166.         turtle.select(i)
  167.         turtle.suck()
  168.         if turtle.getFuelLevel() < turtle.getFuelLimit() then
  169.             turtle.refuel()
  170.         else
  171.             break
  172.         end
  173.     end
  174.  
  175.     for i = 1, 15, 1 do
  176.         turtle.select(i)
  177.         turtle.drop()
  178.     end
  179.  
  180.     while true do
  181.         BasketNet.sendToMasterControl("Ready", "RefuelingComplete")
  182.         local iSenderId, sMessage, sProtocol = BasketNet.receiveFromMasterControl(5)
  183.             if iSenderID ~= nil and sMessage == "RequestReceived" then
  184.             break
  185.         end
  186.     end
  187.  
  188.     while true do
  189.         local iSenderId, sMessage, sProtocol = BasketNet.receiveFromMasterControl(10)
  190.             if iSenderID ~= nil and sMessage == "Clear" then
  191.             break
  192.         end
  193.     end
  194.  
  195.     returnHome()
  196.  
  197. end
  198.  
  199. function retrieveItemByChestID(iChestID, iAmount)
  200.  
  201.     local tTargetItem
  202.  
  203.     for tItems in pairs(inventory[iChestID]) do
  204.         for tTargetItem in tItems do
  205.             break
  206.         end
  207.     end
  208.  
  209.     return retrieveItems(tTargetItem, iAmount)
  210.  
  211. end
  212.  
  213. function retrieveItems(tTargetItem, iAmount)
  214.  
  215.     if tTargetItem ~= nil then
  216.         for iChestID, tItems in pairs(inventory) do
  217.             for k, tItemTest in pairs(tItems) do
  218.                 if ChestManagement.itemsAreTheSame(tTargetItem, tItemTest) then
  219.                     goToChest(iChestID)
  220.                     for i = 1, 15, 1 do
  221.                         turtle.select(i)
  222.                         if turtle.suck() then
  223.                             iAmount = iAmount - turtle.getItemCount(i)
  224.                            
  225.                             if iAmount < 0 then
  226.                                 turtle.dropUp(0 - iAmount)
  227.                                 iAmount = 0
  228.                             end
  229.  
  230.                             inventory[iChestID][k].count = inventory[iChestID][k].count - turtle.getItemCount(i)
  231.                             if inventory[iChestID][k].count <= 0 then
  232.                                 table.remove(inventory[iChestID], k)
  233.                             end
  234.  
  235.                             if iAmount == 0 then
  236.                                 break
  237.                             end
  238.                         end
  239.                     end
  240.  
  241.                     Database.saveTable("inventory", inventory)
  242.                    
  243.                     if iAmount == 0 or turtle.getItemCount(15) > 0 then
  244.                         returnHome()
  245.                         for i = 1, 15, 1 do
  246.                             if turtle.getItemCount(i) > 0 then
  247.                                 turtle.select(i)
  248.                                 if not turtle.drop() then
  249.                                     -- Buffer is full!
  250.                                 end
  251.                             end
  252.                         end
  253.                     end
  254.                 end
  255.                 break
  256.             end
  257.  
  258.             if iAmount == 0 then
  259.                 return true
  260.             end
  261.         end
  262.     end
  263. end
  264.  
  265. function storeBuffer()
  266.  
  267.     if turtle.getFuelLevel() < 1000 then
  268.         print("I'm low on fuel")
  269.         returnHome()
  270.         return false
  271.     end
  272.  
  273.     print("Storing Buffer...")
  274.  
  275.     while true do
  276.  
  277.         returnHome()
  278.  
  279.         for i = 1, 15, 1 do
  280.             turtle.select(i)
  281.             if not turtle.suck() then
  282.                 i = i - 1
  283.                 if i >= 1 and turtle.getItemDetail(i) ~= nil then
  284.                     print("\tGrabbed " .. i .. " stack(s) of " .. turtle.getItemDetail(i).name)
  285.                 end
  286.                 break
  287.             end
  288.  
  289.             if i > 1 and not ChestManagement.itemsAreTheSame(turtle.getItemDetail(1), turtle.getItemDetail(i)) then
  290.                 turtle.drop()
  291.                 i = i - 1
  292.                 if i >= 1 and turtle.getItemDetail(i) ~= nil then
  293.                     print("\tGrabbed " .. i .. " stack(s) of " .. turtle.getItemDetail(i).name)
  294.                 end
  295.                 break
  296.             end
  297.         end
  298.  
  299.         -- If there is nothing in the buffer
  300.         if turtle.getItemCount(1) == 0 then
  301.             break
  302.         end
  303.  
  304.         local chest = nil
  305.  
  306.         local bFinished = false
  307.  
  308.         for iPass = 1, 2, 1 do
  309.  
  310.             for chest, items in pairs(inventory) do
  311.  
  312.                 if bFinished then
  313.                     break
  314.                 end
  315.  
  316.                 local bPutInThisChest = false
  317.                 local bChestIsFull = false
  318.    
  319.                 local iFirstSlotWithItems = 1
  320.                 for iFirstSlotWithItems = 1, 15, 1 do
  321.                     if turtle.getItemCount(i) > 0 then
  322.                         break
  323.                     end
  324.                 end
  325.  
  326.                 -- The first pass look for chests w/ that item already
  327.                 if iPass == 1 then
  328.                     for k, tItem1 in pairs(items) do                   
  329.                         if ChestManagement.itemsAreTheSame(tItem1, turtle.getItemDetail(iFirstSlotWithItems)) then
  330.                             bPutInThisChest = true
  331.                             break
  332.                         end
  333.                     end
  334.                 else
  335.                     if #items == 0 then
  336.                         bPutInThisChest = true
  337.                     end
  338.                 end
  339.  
  340.                 if bPutInThisChest then
  341.  
  342.                     print("\tHeading to chest " .. chest)
  343.  
  344.                     -- Move the turtle up or down to the destination chest
  345.                     while iDepth ~= math.floor(chest / 4) do
  346.                         if iDepth > math.floor(chest / 4) then
  347.                             turtle.up()
  348.                             iDepth = iDepth - 1
  349.                         else
  350.                             turtle.down()
  351.                             iDepth = iDepth + 1
  352.                         end
  353.                     end
  354.  
  355.                     -- Rotate the turtle to the destination chest
  356.                     while iRotation ~= math.fmod(chest, 4) do
  357.                         --if iRotation - math.fmod(chest, 4) == 3 then
  358.                         --  turtle.turnLeft()
  359.                         --  iRotation = iRotation - 1
  360.                         --else
  361.                             turtle.turnRight()
  362.                             iRotation = iRotation + 1
  363.                         --end
  364.                         iRotation = math.fmod(iRotation, 4)
  365.                     end
  366.  
  367.                     bFinished = true
  368.  
  369.                     for i = 1, 15, 1 do
  370.                         if turtle.getItemCount(i) > 0 then
  371.                             local info = turtle.getItemDetail(i)
  372.                             if info ~= nil then
  373.                                 turtle.select(i)
  374.                                 if not turtle.drop() then
  375.                                     bFinished = false
  376.                                     break
  377.                                 end
  378.  
  379.                                 local iAmountRemaining = turtle.getItemCount(i)
  380.  
  381.                                 local added = false
  382.  
  383.                                 for k, tItem in pairs(inventory[chest]) do
  384.                                     if ChestManagement.itemsAreTheSame(tItem, info) then
  385.                                         inventory[chest][k].count = inventory[chest][k].count + info.count - iAmountRemaining
  386.                                         Database.saveTable("inventory", inventory)
  387.                                         added  = true
  388.                                         break
  389.                                     end
  390.                                 end
  391.  
  392.                                 if not added then
  393.                                     table.insert(inventory[chest], info)
  394.                                     Database.saveTable("inventory", inventory)
  395.                                 end
  396.                             end
  397.                         end
  398.                     end
  399.                 end
  400.  
  401.             end
  402.  
  403.         end
  404.  
  405.         if not bFinished then
  406.             print("Unable to find chest to put these items in")
  407.             return false
  408.         end
  409.  
  410.     end
  411.  
  412. end
  413.  
  414. function goToChest(iChestID)
  415.  
  416.     -- Move the turtle up or down to the destination chest
  417.     while iDepth ~= math.floor(iChestID / 4) do
  418.         if iDepth > math.floor(iChestID / 4) then
  419.             turtle.up()
  420.             iDepth = iDepth - 1
  421.         else
  422.             turtle.down()
  423.             iDepth = iDepth + 1
  424.         end
  425.     end
  426.  
  427.     -- Rotate the turtle to the destination chest
  428.     while iRotation ~= math.fmod(iChestID, 4) do
  429.         --if iRotation - math.fmod(iChestID, 4) == 3 then
  430.         --  turtle.turnLeft()
  431.         --  iRotation = iRotation - 1
  432.         --else
  433.         turtle.turnRight()
  434.         iRotation = iRotation + 1
  435.         --end
  436.         iRotation = math.fmod(iRotation, 4)
  437.     end
  438.  
  439. end
  440.  
  441. function displayInventory()
  442.     for i = 1, #inventory do
  443.         print(" ");
  444.         print("Chest ", i, ":");
  445.         for k, v in pairs(inventory[i]) do
  446.             print(v, "x ", k)
  447.         end
  448.     end
  449. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement