Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --UPDATE2014-06-09!
- --Keep It Simple Stupid! (KISS)
- --Get this instead: http://pastebin.com/zzAurEPk :)
- -----------------------------------
- --ITEM QUANTITY FEEDER BY CRAGRIM--
- -----------------------------------
- --INSTRUCTIONS:
- --http://www.promethea-ftb.com/index.php?threads/cragrims-computercraft-thread.639/#post-2880
- ----------------
- --DECLARATIONS--
- ----------------
- --version
- local strVersion = "0.2"
- --get command line arguments in a table
- local tblArgs = { ... }
- --declare default item amounts
- local tblItemCount = {}
- --get the name of the program if named other than "itemfeeder"
- local ITEMFEEDER = shell.getRunningProgram()
- --declare special characters to avoid misshaps
- strLP = [[(]] --left parentheses
- strRP = [[)]] --right parentheses
- strQ = [["]] --doublequote
- --error has occurred
- bolErr = false
- --Debug mode prints information about what the program is doing
- DebugMode = false
- -------------
- --FUNCTIONS--
- -------------
- -------------------------
- --STRING/ARRAY HANDLING--
- -------------------------
- --Put quotes around this
- function strQuote(this)
- return strQ..this..strQ
- end
- --Put parentheses around that
- function strParen(that)
- return strLP..that..strRP
- end
- --Split a string
- function splitStr(str)
- local strIndex = {}
- i = 1
- for w in string.gmatch(str, "[%w+]+") do
- strIndex[i] = w
- i = i + 1
- end
- return strIndex
- end
- --Get table size
- function tableSize(myTable)
- local intArgsCount = 0
- if next(myTable) == nil then
- --check if table is empty (do nothing)
- else
- --for each element add 1
- for _ in pairs(myTable) do
- intArgsCount = intArgsCount + 1
- end
- end
- return intArgsCount
- end
- ----------------------
- --COSMETIC FUNCTIONS--
- ----------------------
- function ClearScreen()
- term.clear()
- term.setCursorPos(1,1)
- end
- function Pause()
- write("Press any key to continue...")
- read()
- end
- --Print out the saved item data stored in the table
- function PrintSetting()
- write("[X]")
- count = 2
- for i = 2,16 do
- if count == 4 then
- print("["..tblItemCount[i].."]")
- count = 0
- else
- write("["..tblItemCount[i].."]")
- end
- count = count + 1
- end
- end
- function PrintProgramInfo()
- ClearScreen()
- print("ITEM QUANTITY FEEDER BY CRAGRIM")
- print("Program Version: "..strVersion)
- print("Computer Label: "..ComputerLabel())
- end
- --Only print if in debug mode
- function debugPrint(msg)
- if DebugMode == true then
- print(msg)
- end
- end
- ---------------------------------
- --STARTUP INFORMATIVE FUNCTIONS--
- ---------------------------------
- --Print information about current work
- function StartInfo()
- PrintProgramInfo()
- print("")
- print("FEEDING THIS ITEM QUANTITY:")
- LoadItemSettings()
- PrintSetting()
- if bolErr == false then
- print("Running! Hold CTRL+T to abort...")
- end
- end
- --Print information if no arguments were given
- function PrintDescription()
- PrintProgramInfo()
- print("")
- print("AVAILABLE COMMANDS:")
- print("save - Save item quantity in inventory")
- print("show - Load and display current config")
- print("work - Starts feeding top->down")
- print("(overflow always goes in front)")
- print("")
- print("SYNTAX:")
- print(ITEMFEEDER.." [cmd] [slot1] [slot2] [etc]")
- end
- function PrintShowSetting()
- ClearScreen()
- PrintProgramInfo()
- print("")
- print("Saved item quantity in item slots:")
- LoadItemSettings()
- PrintSetting()
- end
- ---------------------
- --SETTING FUNCTIONS--
- ---------------------
- function SaveItemSettings()
- --Declare startup string to automatically launch work program with item amounts after a computer reboot
- --local strStartup = "shell.run"..strParen(strQuote(ITEMFEEDER)..","..strQuote("work")..","..strQuote(tblArgs[2])..","..strQuote(tblArgs[3])..","..strQuote(tblArgs[4])..","..strQuote(tblArgs[5]))
- local strStartup = ""
- --Go through each slot in inventory, if slot is empty make nil value 0 instead
- for i = 2,16 do
- itemsInSlot = turtle.getItemCount(i)
- if itemsInSlot > 0 then
- tblItemCount[i] = itemsInSlot
- else
- tblItemCount[i] = 0
- end
- end
- --puzzle together the startup string to save the item count in each slot
- for i = 2,16 do
- strStartup = strStartup..","..strQuote(tblItemCount[i])
- end
- strStartup = strQuote(ITEMFEEDER)..","..strQuote("work")..strStartup
- strStartup = "shell.run"..strParen(strStartup)
- ClearScreen()
- debugPrint(strStartup)
- PrintProgramInfo()
- print("")
- print("Current item quantity in item slots:")
- PrintSetting()
- --Write to startup program
- local fileWrite = fs.open ("startup", "w")
- fileWrite.write(strStartup)
- fileWrite.close()
- write("Saving settings to startup...")
- --Read the startup program to make sure it was actually saved, this is sometimes skipped on slow/lagging servers
- local fileRead=fs.open("startup", "r")
- if fileRead.readAll() == strStartup then
- print(" success!")
- else
- print(" error!")
- bolErr = true
- end
- fileRead.close()
- end
- function LoadItemSettings()
- --the work slot is always zero
- tblItemCount[1] = 0
- --translate argument strings to new table with only integers to avoid errors
- for i = 2,16 do
- if tblArgs[i] == nil then
- --convert nil values to 0 to avoid trouble
- tblItemCount[i] = 0
- else
- --convert string table to integer table
- tblItemCount[i] = tonumber(tblArgs[i])
- end
- end
- --HERE
- --Read from startup program
- local fileRead=fs.open("startup", "r")
- strStartup = fileRead.readAll()
- fileRead.close()
- --temporary table
- local startupTable = {}
- --split startup string
- startupTable = splitStr(strStartup)
- --go through each slot in the startup and translate to integer
- for i = 2,16 do
- tblItemCount[i] = tonumber(startupTable[i+3])
- end
- end
- ----------------------------
- --ERROR CHECKING FUNCTIONS--
- ----------------------------
- --Makes sure that the entered value is an integer
- function TypeCheck(number)
- number = tonumber(number)
- if type(number) ~= "number" then
- return false
- else
- return true
- end
- end
- --Wait for this to happen before updating in order to avoid lag
- --Add pullevents to reduce lag even more (don't think its proper in this program though)
- function WaitForMe()
- --os.pullEvent("redstone") --wait for a "redstone" event
- os.sleep(2.0) --sleep for 2 seconds
- end
- --Make sure you have set a computer label.. if not then automatically assign a random label
- function ComputerLabel()
- if os.getComputerLabel() == nil then
- os.setComputerLabel("SomeItemFeeder"..math.random(0,99999))
- return os.getComputerLabel()
- else
- return os.getComputerLabel()
- end
- end
- ----------------------------------------
- --INPUT/OUTPUT/OVERFLOW SIDE ASSIGMENT--
- ----------------------------------------
- --Not really implemented, maybe I will, maybe I wont
- function sendToOverflow()
- turtle.select(1)
- while turtle.drop() == false do
- os.sleep(1.0)
- if turtle.getItemCount(1) == 0 then
- break
- end
- debugPrint("Warning! Overflow chest is full!")
- end
- end
- -----------
- --PROGRAM--
- -----------
- if tblArgs[1] == "save" or tblArgs[1] == "SAVE" then
- --Save work
- SaveItemSettings()
- elseif tblArgs[1] == "show" or tblArgs[1] == "SHOW" then
- --Show saved work settings
- PrintShowSetting()
- elseif tblArgs[1] == "work" or tblArgs[1] == "WORK" or tblArgs[1] == "run" or tblArgs[1] == "RUN" then
- StartInfo()
- --Main loop
- while bolErr == false do
- --check if there is a block above
- if turtle.detectUp() == true then
- if turtle.detect() == true then
- itemSlotToSend = 0
- turtle.select(1)
- --suck from chest above
- while turtle.suckUp() == false do
- --no chest or no items in that chest, wait
- os.sleep(2.0)
- end
- --ItemID detection
- --Attempt to move items to each slot to determine itemID by comparing with current inventory
- for i = 2,16 do
- debugPrint("Checking itemID in slot "..i)
- --check if the slot have any items otherwise skip it
- if turtle.getItemCount(i) > 0 then
- --check if the pulled items are above 55 (64-9)
- if turtle.getItemCount(1) > 55 then
- --put items back to prevent overflowing the turtle inventory in case the input chest is full
- while turtle.dropUp(turtle.getItemCount(1)-55) == false do
- --prevent putting back items in an overfull chest
- WaitForMe()
- debugPrint("Input chest is full or removed...")
- end
- end
- if turtle.transferTo(i) == true then
- -- item was found
- debugPrint("Item is same as in slot "..i.." moving on...")
- --Count item from the slot it was found and compare with config, if right amount then send down
- turtle.select(i)
- while turtle.getItemCount(i) > tblItemCount[i] do
- turtle.dropDown(tblItemCount[i])
- WaitForMe()
- end
- --bolErr = true
- break
- else
- if i == 16 then
- --item was not found, send it to overflow
- sendToOverflow()
- break
- end
- os.sleep(0.1)
- end
- else
- --item amount than 0 drop
- sendToOverflow()
- debugPrint("No items in this slot "..i.." skipping.")
- end
- end
- else
- print("No attached overflow (front)... abort.")
- break
- end
- else
- print("No attached input (top)... abort.")
- break
- end
- os.sleep(0.5)
- end
- else
- --Print instructions
- PrintDescription()
- end
- --Some kind of error occured
- if bolErr == true then
- print("An error occurred... program failed.")
- print("Please check your server settings or try again.")
- end
Add Comment
Please, Sign In to add comment