Advertisement
Guest User

cargoTaker

a guest
Nov 15th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.20 KB | None | 0 0
  1. --Cargo Taker
  2.  
  3. local chestA = peripheral.wrap("right")
  4. local chestB = peripheral.wrap("top")
  5. local printer = peripheral.wrap("left")
  6. local modem = peripheral.wrap("bottom")
  7.  
  8. local trigger = colors.green -- Redstone Frequency which triggers the program to start
  9. local retainer = colors.pink -- While on Train is prevented from leaving. Active while stock is being counted
  10. local dump = colors.red -- When stock take is finished, pulse this frequency to dump inventory into sorting system
  11. local chestAEmpty = colors.cyan -- Fires when chest A is empty
  12. local chestBEmpty = colors.lightBlue -- Fires when Chest B is empty
  13.  
  14. function rs2And(output, outputSide, input1, input2, notOutput)
  15.     local command = redstone.setOutput
  16.     local antiOutput = not output
  17.     if type(output) == "number" then
  18.         command = redstone.setBundledOutput
  19.         antiOutput = notOutput or 0
  20.     end
  21.     if input1 and input2 then
  22.         command(outputSide, output)
  23.         return output
  24.     else
  25.         command(outputSide, antiOutput)
  26.         return antiOutput
  27.     end
  28. end
  29.  
  30. function getID()
  31.     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",}
  32.     local digits = {1,2,3,4,5,6,7,8,9,0,}
  33.     local id = ""
  34.     for i = 1,12 do
  35.         local randIni = math.random(1,100)
  36.         if randIni <= 65 then
  37.             local rand = math.random(1, #characters)
  38.             id = id..characters[rand]
  39.         else
  40.             local rand = math.random(1, #digits)
  41.             id = id..digits[rand]
  42.         end
  43.     end
  44.     return id
  45. end
  46.  
  47. function getChestContents(chest)
  48.     chest.condense()
  49.     local size = chest.getSizeInventory()
  50.     local contents = {}
  51.     for i = 0, size-1 do
  52.         local data = chest.getStackInSlot(i)
  53.         if data ~= nil then
  54.             local t = {
  55.                 ["slot"] = i,
  56.                 ["name"] = data["name"],
  57.                 ["amount"] = data["qty"],
  58.                 }
  59.             table.insert(contents, 0, t)
  60.         else
  61.             break
  62.         end
  63.         sleep(0.01)
  64.     end
  65.     return contents
  66. end
  67.  
  68. function combineTable(s, t)
  69.     for i,v in pairs(t) do
  70.         local k = v["name"]
  71.         local n = v["amount"]
  72.         s[k] = s[k] + n
  73.     end
  74.     return s
  75. end
  76.  
  77. function initialiseStockTake(s, a, b)
  78.     for i,v in pairs(a) do
  79.         local k = v["name"]
  80.         s[k] = 0
  81.     end
  82.     for i,v in pairs(b) do
  83.         local k = v["name"]
  84.         s[k] = 0
  85.     end
  86.     return s
  87. end
  88.  
  89. function newLine(n)
  90.     if n == nil or n < 1 then
  91.         n = 1
  92.     end
  93.     local x,y = printer.getCursorPos()
  94.     printer.setCursorPos(1, y+n)
  95. end
  96.  
  97. function requestTrainID()
  98.     local id = "unknown"
  99.     modem.open(998)
  100.     print("Sending ID Request")
  101.     modem.transmit(999,998, "ID Request")
  102.     local looping = true
  103.     local timer = os.startTimer(20)
  104.     while looping do
  105.         local event = {os.pullEvent()}
  106.         if event[1] == "modem_message" then
  107.             print("ID Request Succesful")
  108.             id = event[5]
  109.             looping = false
  110.         elseif event[2] == timer then
  111.             print("Connection Timed Out")          
  112.             looping = false
  113.         end
  114.     end
  115.     modem.close(998)
  116.     print("ID Set to - "..id)
  117.     return id
  118. end
  119.  
  120. function longPrint(str)
  121.     print("LongPrint - "..str)
  122.     local x,y = printer.getPageSize()
  123.     for i in string.gmatch(str, ".") do
  124.         local cX, cY = printer.getCursorPos()
  125.         if cX > x then
  126.             newLine()
  127.         end
  128.         printer.write(i)
  129.     end
  130. end
  131.  
  132. function titlePage(id)
  133.     local trainID = requestTrainID()
  134.     printer.newPage()
  135.     printer.setPageTitle(id)
  136.     printer.setCursorPos(1,1)
  137.     printer.write("Cloudy Mountain Depot")
  138.     newLine(2)
  139.     printer.write("Inventory ID:")
  140.     newLine()
  141.     printer.write(id)
  142.     newLine(2)
  143.     local tiempo = textutils.formatTime(os.time(), true)
  144.     local day = os.day()
  145.     printer.write("Day "..day)
  146.     newLine()
  147.     printer.write("Time "..tiempo.."hrs")
  148.     newLine(2)
  149.     printer.write("Train ID:")
  150.     newLine()
  151.     printer.write(trainID)
  152.     printer.endPage()
  153. end
  154.  
  155. function listInventory(data, id)
  156.     print(id)
  157.     local length = #data
  158.     local x = 25
  159.     local y = 21
  160.     for i,v in pairs(data) do
  161.         print(i.." - "..v[1]..": "..v[2])
  162.     end
  163.     print("Table Length  = "..length)
  164.     print("Page Length = "..y)
  165.     print("Total Pages = "..math.ceil(length/y))
  166.     printer.newPage()
  167.     printer.setPageTitle(id)
  168.     printer.setCursorPos(1,1)
  169.     for i = 1, length do       
  170.         local str = data[i][1]..": "..data[i][2]
  171.         local strLength = string.len(str)
  172.         if strLength > x then
  173.             local aX, aY = printer.getCursorPos()
  174.             local linesRequired = math.ceil(strLength/x)
  175.             print("Lines: "..linesRequired)
  176.             --printer.write("String too long")
  177.             print("Page Remaining: "..y-aY)
  178.             if y - aY < linesRequired then
  179.                 printer.endPage()
  180.                 printer.newPage()
  181.                 printer.setPageTitle(id)
  182.                 printer.setCursorPos(1,1)
  183.             end
  184.             longPrint(str)
  185.         else
  186.             printer.write(str)
  187.         end    
  188.         local cX,cY = printer.getCursorPos()
  189.         if cY >= y then
  190.             print("New Page")
  191.             printer.endPage()
  192.             printer.newPage()
  193.             printer.setPageTitle(id)
  194.             printer.setCursorPos(1,1)
  195.         else
  196.             newLine()
  197.         end
  198.     end
  199.     printer.endPage()      
  200. end
  201.  
  202. function printProg(data)
  203.     local invID = getID()
  204.     invID = "CMD: "..invID
  205.     local array = {}
  206.     for i,v in pairs(data) do
  207.         local t = {i,v}
  208.         table.insert(array, 0, t)
  209.     end
  210.     if #array < 1 then
  211.         print("Cargo Empty\nCancelling Print")
  212.         return
  213.     end
  214.     print("Publishing Stock Receipt")
  215.     titlePage(invID)
  216.     listInventory(array, invID)
  217. end
  218.  
  219. function mainProg()
  220.     local stockTake = {}
  221.     print("Ready to inspect Cargo")
  222.     local aContents = getChestContents(chestA)
  223.     local bContents = getChestContents(chestB)
  224.     stockTake = initialiseStockTake(stockTake, aContents, bContents)
  225.     stockTake = combineTable(stockTake, aContents)
  226.     stockTake = combineTable(stockTake, bContents)
  227.     printProg(stockTake)
  228.     sleep(3)
  229. end
  230.  
  231. while true do
  232.     term.clear()
  233.     term.setCursorPos(1,1)
  234.     print("Cargo Taker")
  235.     os.pullEvent("redstone")
  236.     if redstone.testBundledInput("back", trigger) then
  237.         print("Cargo arrived: Stalling Train and waiting for cargo to enter examination bay")
  238.         redstone.setBundledOutput("back", retainer)
  239.         sleep(20)
  240.         mainProg()
  241.         print("Stock Taken: Dumping Inventory")
  242.         local red = redstone.getBundledOutput("back")
  243.         redstone.setBundledOutput("back", red+dump)
  244.         local c = 0
  245.         repeat
  246.             sleep(1)
  247.             c = c+1
  248.         until redstone.testBundledInput("back", colors.blue) == true or c == 60
  249.         print("Releasing Train")
  250.         redstone.setBundledOutput("back", 0)
  251.         print("Goodbye Train")
  252.         sleep(1)
  253.        
  254.     end
  255. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement