Advertisement
Wyvern67

Printer tool

Oct 25th, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.72 KB | None | 0 0
  1. debugMode = false
  2. function split(str,splitter)
  3.     if not splitter then return end
  4.     if not str then return end
  5.     words = {}
  6.     i=0
  7.     for part in string.gmatch(str, "[^%"..splitter.."]+") do -- get each part
  8.         i=i+1
  9.         words[i] = part
  10.     end
  11.     return words
  12. end
  13. string.split = split
  14.  
  15. if isTurtle() then
  16.     printer = peripheral.wrap("top")
  17.     if printer == nil then
  18.         print("You are currently using a turtle. Turtles must be placed under the printer for this program to work properly.")
  19.     end
  20.     if turtle.craft == nil then
  21.         print("You are currently using a non-crafty turtle. Either use a regular computer or use a crafty turtle.")
  22.     end
  23. else
  24.     printer = peripheral.find("printer")
  25. end
  26.  
  27. function dropFinishedBook()
  28.    
  29. end
  30.  
  31. function hasInk()
  32.     if printer.getInkLevel() > 0 then
  33.         return true
  34.     else
  35.         return false
  36.     end
  37. end
  38.  
  39. function hasPaper()
  40.     if printer.getPaperLevel() > 0 then
  41.         return true
  42.     else
  43.         return false
  44.     end
  45. end
  46.  
  47. function checkPrintingRequirements()
  48.     if hasPaper() == false or hasInk() == false then   
  49.         print("Printing is impossible: Either ink or paper is missing")
  50.         while hasPaper() == false or hasInk() == false do
  51.             sleep(1)
  52.         end
  53.     end
  54. end
  55.  
  56. function isTurtle()
  57.     if type(turtle) == "table" then
  58.         return true
  59.     else
  60.         return false
  61.     end
  62. end
  63.  
  64. function isStringUseOptimal()
  65.         -- if the inventory is sorted like so:
  66.         -- P P P
  67.         -- P P P
  68.         -- P B S
  69.         -- P=Page, B=Book, S=String
  70.         -- Then the function returns true.
  71.         -- This setup uses 1 string for 7 pages instead of one for one
  72.     for i=1,16 do
  73.         if turtle.getItemCount(i) == 0 then
  74.             if i <= 9 and i%4 ~= 0 then
  75.                 turtle.select(i)
  76.                 return false
  77.             end
  78.         end
  79.     end
  80.     return true
  81. end
  82.  
  83. function craftBook()
  84.     if isStringUseOptimal() then
  85.         turtle.select(10)
  86.         turtle.craft()
  87.         turtle.select(1)
  88.         turtle.suckUp()
  89.         return true
  90.     else
  91.         turtle.suckUp()
  92.         return false
  93.     end
  94. end
  95.  
  96. function getPage(fileName)
  97.     local h = fs.open(fileName, "r")
  98.     file = h.readAll()
  99.     h.close()
  100.     return file
  101. end
  102.  
  103. function fitWords(line)
  104.     local words = string.split(line, " ")
  105.     local line = {}
  106.     local sum = 0
  107.     local j=1
  108.     line[j] = ""
  109.     for i,word in pairs(words) do
  110.         sum = sum + string.len(word) + 1
  111.         if sum < 25 then
  112.             line[j] = line[j] .. word .. " "
  113.         else
  114.             sum = string.len(word) + 1
  115.             j=j+1
  116.             line[j] = word .. " "
  117.         end
  118.         -- print("line["..j.."] = "..line[j] .. " (" .. sum .. ")")
  119.     end
  120.     return line
  121. end
  122.  
  123. function printBook(text)
  124.     if debugMode then
  125.         -- Debug mode. It doesn't print shit
  126.         printer = fs.open("test", "w")
  127.         printer.setCursorPos = function() end
  128.         printer.newPage = function() return true end
  129.         printer.endPage = function() return true end
  130.         printer.getPaperLevel = function() return 1 end
  131.         printer.getInkLevel = function() return 1 end
  132.         printer.write = printer.writeLine
  133.     end
  134.    
  135.     lines = fitWords(text)
  136.    
  137.     if #lines%21 == 0 then
  138.         numberOfPages = #lines/21
  139.     else
  140.         numberOfPages = math.floor(#lines/21) + 1
  141.     end
  142.     -- print(numberOfPages)
  143.    
  144.     for page=0, numberOfPages-1 do
  145.         checkPrintingRequirements()
  146.         printer.newPage()
  147.         for line=1, 21 do
  148.             if line+21*page <= #lines then
  149.                 printer.setCursorPos(1, line)
  150.                 printer.write(lines[line+21*page])
  151.                 print(lines[line+21*page])
  152.                 --print(line+21*page)
  153.             end
  154.         end
  155.         if printer.endPage() == false then
  156.             print("Error: Printer is full. Please clear the printer tray.")
  157.             while printer.endPage() == false do
  158.                 sleep(1)
  159.             end
  160.         end
  161.        
  162.         if isTurtle() then
  163.             craftBook()
  164.         end
  165.     end
  166.     if debugMode then
  167.         printer.close()
  168.     end
  169.    
  170.     if isTurtle() then
  171.         turtle.select(10)
  172.         turtle.craft()
  173.         dropFinishedBook()
  174.     end
  175. end
  176.  
  177. function printFile(fileName)
  178.     h = fs.open(fileName, "r")
  179.     local text = h.readAll()
  180.     h.close()
  181.     printBook(text)
  182. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement