Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Cargo Taker
- local chestA = peripheral.wrap("right")
- local chestB = peripheral.wrap("top")
- local printer = peripheral.wrap("left")
- local modem = peripheral.wrap("bottom")
- local trigger = colors.green -- Redstone Frequency which triggers the program to start
- local retainer = colors.pink -- While on Train is prevented from leaving. Active while stock is being counted
- local dump = colors.red -- When stock take is finished, pulse this frequency to dump inventory into sorting system
- local chestAEmpty = colors.cyan -- Fires when chest A is empty
- local chestBEmpty = colors.lightBlue -- Fires when Chest B is empty
- function rs2And(output, outputSide, input1, input2, notOutput)
- local command = redstone.setOutput
- local antiOutput = not output
- if type(output) == "number" then
- command = redstone.setBundledOutput
- antiOutput = notOutput or 0
- end
- if input1 and input2 then
- command(outputSide, output)
- return output
- else
- command(outputSide, antiOutput)
- return antiOutput
- end
- end
- function getID()
- local characters = {"q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m","Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M",}
- local digits = {1,2,3,4,5,6,7,8,9,0,}
- local id = ""
- for i = 1,12 do
- local randIni = math.random(1,100)
- if randIni <= 65 then
- local rand = math.random(1, #characters)
- id = id..characters[rand]
- else
- local rand = math.random(1, #digits)
- id = id..digits[rand]
- end
- end
- return id
- end
- function getChestContents(chest)
- chest.condense()
- local size = chest.getSizeInventory()
- local contents = {}
- for i = 0, size-1 do
- local data = chest.getStackInSlot(i)
- if data ~= nil then
- local t = {
- ["slot"] = i,
- ["name"] = data["name"],
- ["amount"] = data["qty"],
- }
- table.insert(contents, 0, t)
- else
- break
- end
- sleep(0.01)
- end
- return contents
- end
- function combineTable(s, t)
- for i,v in pairs(t) do
- local k = v["name"]
- local n = v["amount"]
- s[k] = s[k] + n
- end
- return s
- end
- function initialiseStockTake(s, a, b)
- for i,v in pairs(a) do
- local k = v["name"]
- s[k] = 0
- end
- for i,v in pairs(b) do
- local k = v["name"]
- s[k] = 0
- end
- return s
- end
- function newLine(n)
- if n == nil or n < 1 then
- n = 1
- end
- local x,y = printer.getCursorPos()
- printer.setCursorPos(1, y+n)
- end
- function requestTrainID()
- local id = "unknown"
- modem.open(998)
- print("Sending ID Request")
- modem.transmit(999,998, "ID Request")
- local looping = true
- local timer = os.startTimer(20)
- while looping do
- local event = {os.pullEvent()}
- if event[1] == "modem_message" then
- print("ID Request Succesful")
- id = event[5]
- looping = false
- elseif event[2] == timer then
- print("Connection Timed Out")
- looping = false
- end
- end
- modem.close(998)
- print("ID Set to - "..id)
- return id
- end
- function longPrint(str)
- print("LongPrint - "..str)
- local x,y = printer.getPageSize()
- for i in string.gmatch(str, ".") do
- local cX, cY = printer.getCursorPos()
- if cX > x then
- newLine()
- end
- printer.write(i)
- end
- end
- function titlePage(id)
- local trainID = requestTrainID()
- printer.newPage()
- printer.setPageTitle(id)
- printer.setCursorPos(1,1)
- printer.write("Cloudy Mountain Depot")
- newLine(2)
- printer.write("Inventory ID:")
- newLine()
- printer.write(id)
- newLine(2)
- local tiempo = textutils.formatTime(os.time(), true)
- local day = os.day()
- printer.write("Day "..day)
- newLine()
- printer.write("Time "..tiempo.."hrs")
- newLine(2)
- printer.write("Train ID:")
- newLine()
- printer.write(trainID)
- printer.endPage()
- end
- function listInventory(data, id)
- print(id)
- local length = #data
- local x = 25
- local y = 21
- for i,v in pairs(data) do
- print(i.." - "..v[1]..": "..v[2])
- end
- print("Table Length = "..length)
- print("Page Length = "..y)
- print("Total Pages = "..math.ceil(length/y))
- printer.newPage()
- printer.setPageTitle(id)
- printer.setCursorPos(1,1)
- for i = 1, length do
- local str = data[i][1]..": "..data[i][2]
- local strLength = string.len(str)
- if strLength > x then
- local aX, aY = printer.getCursorPos()
- local linesRequired = math.ceil(strLength/x)
- print("Lines: "..linesRequired)
- --printer.write("String too long")
- print("Page Remaining: "..y-aY)
- if y - aY < linesRequired then
- printer.endPage()
- printer.newPage()
- printer.setPageTitle(id)
- printer.setCursorPos(1,1)
- end
- longPrint(str)
- else
- printer.write(str)
- end
- local cX,cY = printer.getCursorPos()
- if cY >= y then
- print("New Page")
- printer.endPage()
- printer.newPage()
- printer.setPageTitle(id)
- printer.setCursorPos(1,1)
- else
- newLine()
- end
- end
- printer.endPage()
- end
- function printProg(data)
- local invID = getID()
- invID = "CMD: "..invID
- local array = {}
- for i,v in pairs(data) do
- local t = {i,v}
- table.insert(array, 0, t)
- end
- if #array < 1 then
- print("Cargo Empty\nCancelling Print")
- modem.transmit(997,999, "refresh")
- return
- end
- print("Publishing Stock Receipt")
- titlePage(invID)
- listInventory(array, invID)
- end
- function mainProg()
- local stockTake = {}
- print("Ready to inspect Cargo")
- local aContents = getChestContents(chestA)
- local bContents = getChestContents(chestB)
- stockTake = initialiseStockTake(stockTake, aContents, bContents)
- stockTake = combineTable(stockTake, aContents)
- stockTake = combineTable(stockTake, bContents)
- printProg(stockTake)
- sleep(3)
- end
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print("Cargo Taker")
- os.pullEvent("redstone")
- if redstone.testBundledInput("back", trigger) then
- print("Cargo arrived: Stalling Train and waiting for cargo to enter examination bay")
- redstone.setBundledOutput("back", retainer)
- sleep(20)
- mainProg()
- print("Stock Taken: Dumping Inventory")
- local red = redstone.getBundledOutput("back")
- redstone.setBundledOutput("back", red+dump)
- local c = 0
- repeat
- sleep(1)
- c = c+1
- until redstone.testBundledInput("back", colors.blue) == true or c == 60
- print("Releasing Train")
- redstone.setBundledOutput("back", 0)
- print("Goodbye Train")
- sleep(1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement