cragrim

ItemFeeder - RP2 Filter "workaround"

Jul 29th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.52 KB | None | 0 0
  1. --UPDATE2014-06-09!
  2. --Keep It Simple Stupid! (KISS)
  3. --Get this instead: http://pastebin.com/zzAurEPk :)
  4. -----------------------------------
  5. --ITEM QUANTITY FEEDER BY CRAGRIM--
  6. -----------------------------------
  7. --INSTRUCTIONS:
  8. --http://www.promethea-ftb.com/index.php?threads/cragrims-computercraft-thread.639/#post-2880
  9. ----------------
  10. --DECLARATIONS--
  11. ----------------
  12.  
  13. --version
  14. local strVersion = "0.2"
  15.  
  16. --get command line arguments in a table
  17. local tblArgs = { ... }
  18.  
  19. --declare default item amounts
  20. local tblItemCount = {}
  21.  
  22. --get the name of the program if named other than "itemfeeder"
  23. local ITEMFEEDER = shell.getRunningProgram()
  24.  
  25. --declare special characters to avoid misshaps
  26. strLP = [[(]] --left parentheses
  27. strRP = [[)]] --right parentheses
  28. strQ = [["]] --doublequote
  29.  
  30. --error has occurred
  31. bolErr = false
  32.  
  33. --Debug mode prints information about what the program is doing
  34. DebugMode = false
  35.  
  36. -------------
  37. --FUNCTIONS--
  38. -------------
  39.  
  40.     -------------------------
  41.     --STRING/ARRAY HANDLING--
  42.     -------------------------
  43.  
  44. --Put quotes around this
  45. function strQuote(this)
  46.     return strQ..this..strQ
  47. end
  48.  
  49.  
  50. --Put parentheses around that
  51. function strParen(that)
  52.     return strLP..that..strRP
  53. end
  54.  
  55. --Split a string
  56. function splitStr(str)
  57.     local strIndex = {}
  58.     i = 1
  59.     for w in string.gmatch(str, "[%w+]+") do
  60.         strIndex[i] = w
  61.         i = i + 1
  62.     end
  63.     return strIndex
  64. end
  65.  
  66. --Get table size
  67. function tableSize(myTable)
  68.     local intArgsCount = 0
  69.     if next(myTable) == nil then
  70.         --check if table is empty (do nothing)
  71.     else
  72.         --for each element add 1
  73.         for _ in pairs(myTable) do
  74.             intArgsCount = intArgsCount + 1
  75.         end
  76.     end
  77.     return intArgsCount
  78. end
  79.  
  80.     ----------------------
  81.     --COSMETIC FUNCTIONS--
  82.     ----------------------
  83.  
  84. function ClearScreen()
  85.     term.clear()
  86.     term.setCursorPos(1,1)
  87. end
  88.  
  89. function Pause()
  90.     write("Press any key to continue...")
  91.     read()
  92. end
  93.  
  94. --Print out the saved item data stored in the table
  95. function PrintSetting()
  96.     write("[X]")
  97.     count = 2
  98.     for i = 2,16 do
  99.         if count == 4 then
  100.             print("["..tblItemCount[i].."]")
  101.             count = 0
  102.         else
  103.             write("["..tblItemCount[i].."]")
  104.         end
  105.         count = count + 1
  106.     end
  107. end
  108.  
  109. function PrintProgramInfo()
  110.     ClearScreen()
  111.     print("ITEM QUANTITY FEEDER BY CRAGRIM")
  112.     print("Program Version: "..strVersion)
  113.     print("Computer Label: "..ComputerLabel())
  114. end
  115.  
  116. --Only print if in debug mode
  117. function debugPrint(msg)
  118.     if DebugMode == true then
  119.         print(msg)
  120.     end
  121. end
  122.     ---------------------------------
  123.     --STARTUP INFORMATIVE FUNCTIONS--
  124.     ---------------------------------
  125.  
  126. --Print information about current work
  127. function StartInfo()
  128.     PrintProgramInfo()
  129.     print("")
  130.     print("FEEDING THIS ITEM QUANTITY:")
  131.    
  132.     LoadItemSettings()
  133.     PrintSetting()
  134.    
  135.     if bolErr == false then
  136.         print("Running! Hold CTRL+T to abort...")
  137.     end
  138. end
  139.    
  140. --Print information if no arguments were given
  141. function PrintDescription()
  142.     PrintProgramInfo()
  143.     print("")
  144.     print("AVAILABLE COMMANDS:")
  145.     print("save - Save item quantity in inventory")
  146.     print("show - Load and display current config")
  147.     print("work - Starts feeding top->down")
  148.     print("(overflow always goes in front)")
  149.     print("")
  150.     print("SYNTAX:")
  151.     print(ITEMFEEDER.." [cmd] [slot1] [slot2] [etc]")
  152. end
  153.  
  154. function PrintShowSetting()
  155.     ClearScreen()
  156.     PrintProgramInfo()
  157.     print("")
  158.     print("Saved item quantity in item slots:")
  159.  
  160.     LoadItemSettings()
  161.     PrintSetting()
  162. end
  163.  
  164.     ---------------------
  165.     --SETTING FUNCTIONS--
  166.     ---------------------
  167.  
  168. function SaveItemSettings()
  169.     --Declare startup string to automatically launch work program with item amounts after a computer reboot
  170.     --local strStartup = "shell.run"..strParen(strQuote(ITEMFEEDER)..","..strQuote("work")..","..strQuote(tblArgs[2])..","..strQuote(tblArgs[3])..","..strQuote(tblArgs[4])..","..strQuote(tblArgs[5]))
  171.     local strStartup = ""
  172.    
  173.     --Go through each slot in inventory, if slot is empty make nil value 0 instead
  174.     for i = 2,16 do
  175.         itemsInSlot = turtle.getItemCount(i)
  176.         if itemsInSlot > 0 then
  177.             tblItemCount[i] = itemsInSlot
  178.         else
  179.             tblItemCount[i] = 0
  180.         end
  181.     end
  182.    
  183.     --puzzle together the startup string to save the item count in each slot
  184.     for i = 2,16 do
  185.         strStartup = strStartup..","..strQuote(tblItemCount[i])
  186.     end
  187.     strStartup = strQuote(ITEMFEEDER)..","..strQuote("work")..strStartup
  188.     strStartup = "shell.run"..strParen(strStartup)
  189.    
  190.     ClearScreen()
  191.     debugPrint(strStartup)
  192.  
  193.     PrintProgramInfo()
  194.     print("")
  195.     print("Current item quantity in item slots:")
  196.     PrintSetting()
  197.    
  198.     --Write to startup program
  199.     local fileWrite = fs.open ("startup", "w")
  200.     fileWrite.write(strStartup)
  201.     fileWrite.close()
  202.     write("Saving settings to startup...")
  203.    
  204.     --Read the startup program to make sure it was actually saved, this is sometimes skipped on slow/lagging servers   
  205.     local fileRead=fs.open("startup", "r")
  206.     if fileRead.readAll() == strStartup then
  207.         print(" success!")
  208.     else
  209.         print(" error!")
  210.         bolErr = true
  211.     end
  212.     fileRead.close()
  213. end
  214.  
  215. function LoadItemSettings()
  216.     --the work slot is always zero
  217.     tblItemCount[1] = 0
  218.    
  219.     --translate argument strings to new table with only integers to avoid errors
  220.     for i = 2,16 do
  221.         if tblArgs[i] == nil then
  222.             --convert nil values to 0 to avoid trouble
  223.             tblItemCount[i] = 0
  224.         else
  225.             --convert string table to integer table
  226.             tblItemCount[i] = tonumber(tblArgs[i])
  227.         end
  228.     end
  229.    
  230.     --HERE
  231.    
  232.     --Read from startup program
  233.     local fileRead=fs.open("startup", "r")
  234.     strStartup = fileRead.readAll()
  235.     fileRead.close()
  236.    
  237.     --temporary table
  238.     local startupTable = {}
  239.     --split startup string
  240.     startupTable = splitStr(strStartup)
  241.    
  242.     --go through each slot in the startup and translate to integer
  243.     for i = 2,16 do
  244.         tblItemCount[i] = tonumber(startupTable[i+3])
  245.     end
  246.    
  247. end
  248.  
  249.     ----------------------------
  250.     --ERROR CHECKING FUNCTIONS--
  251.     ----------------------------
  252.  
  253. --Makes sure that the entered value is an integer
  254. function TypeCheck(number)
  255.     number = tonumber(number)
  256.     if type(number) ~= "number" then
  257.         return false
  258.     else
  259.         return true
  260.     end
  261. end
  262.    
  263. --Wait for this to happen before updating in order to avoid lag
  264. --Add pullevents to reduce lag even more (don't think its proper in this program though)
  265. function WaitForMe()
  266.     --os.pullEvent("redstone") --wait for a "redstone" event
  267.     os.sleep(2.0) --sleep for 2 seconds
  268. end
  269.  
  270.  
  271. --Make sure you have set a computer label.. if not then automatically assign a random label
  272. function ComputerLabel()
  273.     if os.getComputerLabel() == nil then
  274.         os.setComputerLabel("SomeItemFeeder"..math.random(0,99999))
  275.         return os.getComputerLabel()
  276.     else
  277.         return os.getComputerLabel()
  278.     end
  279. end
  280.  
  281. ----------------------------------------
  282. --INPUT/OUTPUT/OVERFLOW SIDE ASSIGMENT--
  283. ----------------------------------------
  284.  
  285. --Not really implemented, maybe I will, maybe I wont
  286.  
  287. function sendToOverflow()
  288.     turtle.select(1)
  289.     while turtle.drop() == false do
  290.         os.sleep(1.0)
  291.         if turtle.getItemCount(1) == 0 then
  292.             break
  293.         end
  294.         debugPrint("Warning! Overflow chest is full!")
  295.     end
  296. end
  297.  
  298. -----------
  299. --PROGRAM--
  300. -----------
  301.  
  302. if tblArgs[1] == "save" or tblArgs[1] == "SAVE" then
  303.     --Save work
  304.     SaveItemSettings()
  305.  
  306. elseif tblArgs[1] == "show" or tblArgs[1] == "SHOW" then
  307.     --Show saved work settings
  308.     PrintShowSetting()
  309.    
  310. elseif tblArgs[1] == "work" or tblArgs[1] == "WORK" or tblArgs[1] == "run" or tblArgs[1] == "RUN" then
  311.    
  312.     StartInfo()
  313.    
  314.     --Main loop
  315.     while bolErr == false do
  316.         --check if there is a block above
  317.         if turtle.detectUp() == true then
  318.             if turtle.detect() == true then
  319.                 itemSlotToSend = 0
  320.                 turtle.select(1)
  321.                
  322.                 --suck from chest above
  323.                 while turtle.suckUp() == false do
  324.                     --no chest or no items in that chest, wait
  325.                     os.sleep(2.0)
  326.                 end
  327.  
  328.                 --ItemID detection
  329.                 --Attempt to move items to each slot to determine itemID by comparing with current inventory
  330.                 for i = 2,16 do
  331.                     debugPrint("Checking itemID in slot "..i)
  332.                     --check if the slot have any items otherwise skip it
  333.                     if turtle.getItemCount(i) > 0 then
  334.                         --check if the pulled items are above 55 (64-9)
  335.                         if turtle.getItemCount(1) > 55 then
  336.                             --put items back to prevent overflowing the turtle inventory in case the input chest is full
  337.                             while turtle.dropUp(turtle.getItemCount(1)-55) == false do
  338.                                 --prevent putting back items in an overfull chest
  339.                                 WaitForMe()
  340.                                 debugPrint("Input chest is full or removed...")
  341.                             end
  342.                         end
  343.                        
  344.                         if turtle.transferTo(i) == true then
  345.                             -- item was found
  346.                             debugPrint("Item is same as in slot "..i.." moving on...")
  347.                            
  348.                             --Count item from the slot it was found and compare with config, if right amount then send down
  349.                             turtle.select(i)
  350.                             while turtle.getItemCount(i) > tblItemCount[i] do
  351.                                 turtle.dropDown(tblItemCount[i])
  352.                                 WaitForMe()
  353.                             end
  354.                            
  355.                             --bolErr = true
  356.                             break
  357.                         else
  358.                             if i == 16 then
  359.                                 --item was not found, send it to overflow
  360.                                 sendToOverflow()
  361.                                 break
  362.                             end
  363.                             os.sleep(0.1)
  364.                         end
  365.                        
  366.                     else
  367.                         --item amount than 0 drop
  368.                         sendToOverflow()
  369.                         debugPrint("No items in this slot "..i.." skipping.")
  370.                     end
  371.                 end
  372.             else
  373.                 print("No attached overflow (front)... abort.")
  374.                 break
  375.             end
  376.         else
  377.             print("No attached input (top)... abort.")
  378.             break
  379.         end
  380.        
  381.         os.sleep(0.5)
  382.     end
  383.    
  384. else
  385.     --Print instructions
  386.     PrintDescription()
  387. end
  388.  
  389. --Some kind of error occured
  390. if bolErr == true then
  391.     print("An error occurred... program failed.")
  392.     print("Please check your server settings or try again.")
  393. end
Add Comment
Please, Sign In to add comment