Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- debugMode = false
- function split(str,splitter)
- if not splitter then return end
- if not str then return end
- words = {}
- i=0
- for part in string.gmatch(str, "[^%"..splitter.."]+") do -- get each part
- i=i+1
- words[i] = part
- end
- return words
- end
- string.split = split
- if isTurtle() then
- printer = peripheral.wrap("top")
- if printer == nil then
- print("You are currently using a turtle. Turtles must be placed under the printer for this program to work properly.")
- end
- if turtle.craft == nil then
- print("You are currently using a non-crafty turtle. Either use a regular computer or use a crafty turtle.")
- end
- else
- printer = peripheral.find("printer")
- end
- function dropFinishedBook()
- end
- function hasInk()
- if printer.getInkLevel() > 0 then
- return true
- else
- return false
- end
- end
- function hasPaper()
- if printer.getPaperLevel() > 0 then
- return true
- else
- return false
- end
- end
- function checkPrintingRequirements()
- if hasPaper() == false or hasInk() == false then
- print("Printing is impossible: Either ink or paper is missing")
- while hasPaper() == false or hasInk() == false do
- sleep(1)
- end
- end
- end
- function isTurtle()
- if type(turtle) == "table" then
- return true
- else
- return false
- end
- end
- function isStringUseOptimal()
- -- if the inventory is sorted like so:
- -- P P P
- -- P P P
- -- P B S
- -- P=Page, B=Book, S=String
- -- Then the function returns true.
- -- This setup uses 1 string for 7 pages instead of one for one
- for i=1,16 do
- if turtle.getItemCount(i) == 0 then
- if i <= 9 and i%4 ~= 0 then
- turtle.select(i)
- return false
- end
- end
- end
- return true
- end
- function craftBook()
- if isStringUseOptimal() then
- turtle.select(10)
- turtle.craft()
- turtle.select(1)
- turtle.suckUp()
- return true
- else
- turtle.suckUp()
- return false
- end
- end
- function getPage(fileName)
- local h = fs.open(fileName, "r")
- file = h.readAll()
- h.close()
- return file
- end
- function fitWords(line)
- local words = string.split(line, " ")
- local line = {}
- local sum = 0
- local j=1
- line[j] = ""
- for i,word in pairs(words) do
- sum = sum + string.len(word) + 1
- if sum < 25 then
- line[j] = line[j] .. word .. " "
- else
- sum = string.len(word) + 1
- j=j+1
- line[j] = word .. " "
- end
- -- print("line["..j.."] = "..line[j] .. " (" .. sum .. ")")
- end
- return line
- end
- function printBook(text)
- if debugMode then
- -- Debug mode. It doesn't print shit
- printer = fs.open("test", "w")
- printer.setCursorPos = function() end
- printer.newPage = function() return true end
- printer.endPage = function() return true end
- printer.getPaperLevel = function() return 1 end
- printer.getInkLevel = function() return 1 end
- printer.write = printer.writeLine
- end
- lines = fitWords(text)
- if #lines%21 == 0 then
- numberOfPages = #lines/21
- else
- numberOfPages = math.floor(#lines/21) + 1
- end
- -- print(numberOfPages)
- for page=0, numberOfPages-1 do
- checkPrintingRequirements()
- printer.newPage()
- for line=1, 21 do
- if line+21*page <= #lines then
- printer.setCursorPos(1, line)
- printer.write(lines[line+21*page])
- print(lines[line+21*page])
- --print(line+21*page)
- end
- end
- if printer.endPage() == false then
- print("Error: Printer is full. Please clear the printer tray.")
- while printer.endPage() == false do
- sleep(1)
- end
- end
- if isTurtle() then
- craftBook()
- end
- end
- if debugMode then
- printer.close()
- end
- if isTurtle() then
- turtle.select(10)
- turtle.craft()
- dropFinishedBook()
- end
- end
- function printFile(fileName)
- h = fs.open(fileName, "r")
- local text = h.readAll()
- h.close()
- printBook(text)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement