CCJJSax

3x3tunnel V2

May 31st, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.62 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. torchDist = 5 -- 5 by default
  4. totalDig = 0
  5. chestPlaced = 0
  6. tunnelH = nil
  7. tunnelW = nil
  8.  
  9. turtle.select(1)
  10.  
  11. local function clear(color, lineStart, lineEnd)
  12.     if color == nil and lineStart == nil then
  13.         term.clear()
  14.         term.setCursorPos(1, 1)
  15.     elseif color == true then
  16.         term.setTextColor(colors.white)
  17.         term.setBackgroundColor(colors.black)
  18.         term.clear()
  19.         term.setCursorPos(1, 1)
  20.     end
  21.     if lineStart ~= nil then
  22.         if lineEnd == nil then
  23.             term.setCursorPos(1, lineStart)
  24.             term.clearLine()
  25.         else
  26.             for i = lineStart, lineEnd do
  27.                 term.setCursorPos(1, i)
  28.                 term.clearLine()
  29.             end
  30.         end
  31.     end
  32. end
  33.  
  34. local function screenWrite(text, pos, line, textColor, backgroundColor, fullLineBackgroundColor, newLine, slowPrint)
  35. -- for colors, use Computercrafts Color API here "http://computercraft.info/wiki/Colors_(API)"
  36.     local screenX, screenY = term.getSize()
  37.     if pos == "center" then
  38.         startPos = screenX/2 - string.len(text)/2
  39.     elseif pos == "left" then
  40.         startPos = 1
  41.     elseif pos == "right" then
  42.         startPos = screenX - string.len(text)
  43.     elseif type(pos) == "number" then
  44.         if pos < screenX or pos > 1 then
  45.             startPos = pos
  46.         else
  47.             error("Cannot start a line out of the screen")
  48.         end
  49.     end
  50.     if string.len(text) > screenX then
  51.         error("Sorry, this program does not support a screenWrite() that long at this time.")
  52.     else
  53.         if term.isColor() == true then
  54.             term.setTextColor(textColor)
  55.             term.setBackgroundColor(backgroundColor)
  56.             if fullLineBackgroundColor == true then
  57.                 term.setCursorPos(1, line)
  58.                 clear(false, line)
  59.                 --term.clearLine()
  60.             end
  61.         end
  62.         term.setCursorPos(startPos, line)
  63.         if slowPrint == false or slowPrint == nil then
  64.             term.write(text)
  65.         else
  66.             if slowPrint == 0 then
  67.                 error("Writing something at a pace of 0 letters per minute won't accomplish anything")
  68.             end
  69.             textutils.slowWrite(text, slowPrint)
  70.         end
  71.     end
  72.     if newLine == true then
  73.         term.setCursorPos(1, line+1)
  74.     end
  75. end
  76.  
  77. function renderBar(current, max, label, line)   -- Special thanks to Lyqyd and Bomb Bloke from the Computercraft forums for this function.
  78.     local cursorX, cursorY = term.getCursorPos()
  79.     local screenX, screenY = term.getSize()
  80.     local center = math.floor(screenX / 2)
  81.     local transition = current * screenX / max
  82.     local textStart = center - math.floor(#label / 2)
  83.     local textStop = textStart + #label
  84.     local before, after = textStart - 1, screenX - textStop
  85.     local beforeStart, afterStart = math.floor(before / 2) - math.floor(string.len(tostring(current)) / 2), math.floor(after / 2) - math.floor(string.len(tostring(after)) / 2) + textStop
  86.     local barString = string.rep(" ", beforeStart)..tostring(current)
  87.     barString = barString..string.rep(" ", textStart - #barString)..label
  88.     barString = barString..string.rep(" ", afterStart - #barString)..tostring(max)
  89.     barString = barString..string.rep(" ", screenX - #barString)
  90.     local first, second = string.sub(barString, math.min(1, transition - 1), transition - 1), string.sub(barString, transition)
  91.     term.setCursorPos(1, line)--cursorY)
  92.     term.setTextColor(colors.black)
  93.     term.setBackgroundColor(colors.green)
  94.     term.write(first)
  95.     term.setBackgroundColor(colors.red)
  96.     term.write(second)
  97. end
  98.  
  99. local function isBetween(small, large, numb)
  100.     if numb > small then
  101.         if numb < large then
  102.             return true
  103.         else
  104.             return false
  105.         end
  106.     else
  107.         return false
  108.     end
  109. end
  110.  
  111. local function invGrid() -- WIP
  112.     for i = 1, 4 do
  113.         for m = 1, 4 do
  114.             if i+m % 2 == 0 then
  115.                 paintutils.drawPixel(m+29, i+5, colors.gray)
  116.             else
  117.                 paintutils.drawPixel(m+29, i+5, colors.lightGray)
  118.             end
  119.         end
  120.     end
  121. --[[
  122.     local linePosX = 30
  123.     local linePosY = 6
  124.     for i = 1, 16 do
  125.         if i % 4 == 0 then
  126.             linePosY = linePosY + 1
  127.             linePosX = 30
  128.         end
  129.         if i % 2 == 0 then
  130.             paintutils.drawPixel(linePosX, linePosY, colors.lightGray)
  131.             linePosX = linePosX + 1
  132.         else
  133.             paintutils.drawPixel(linePosX, linePosY, colors.gray)
  134.             linePosX = linePosX + 1
  135.         end
  136.     end]]
  137.  
  138. end
  139.  
  140. local function tunnelDisplay(current, lackFuel) -- add fuel bar
  141.     renderBar(current, tArgs[1], "Progress", 1)
  142.     screenWrite("Blocks dug: "..totalDig, "center", 3, colors.black, colors.lightGray, true, false, false)
  143.     -- Fuel text color
  144.     screenWrite("Fuel Remaining: "..turtle.getFuelLevel(), "center", 4, colors.black, colors.lightGray, true, false, false)
  145.  
  146. --  if lackFuel then
  147. --      renderBar(
  148. --  end
  149. end
  150.  
  151. local function tunnelDisplayExtra(info, msg)
  152.     local screenX, screenY = term.getSize()
  153. --screenWrite(text, pos, line, textColor, backgroundColor, fullLineBackgroundColor, newLine, slowPrint)
  154.     term.setBackgroundColor(colors.lightGray)
  155.     clear(false, 5, 11)
  156.     if info == 1 then
  157.         screenWrite("This program will place", "left", 6, colors.black, colors.lightGray, false, false, false)
  158.         screenWrite("torches every 5 blocks.", "left", 7, colors.black, colors.lightGray, false, false, false)
  159.         --screenWrite("Slot 16 is the bottom right slot.", "left", 8, colors.black, colors.lightGray, false, false, false)
  160.         screenWrite("Slot 16 is shown on", "left", 9, colors.black, colors.lightGray, false, false, false)
  161.         screenWrite("the graph to the right", "left", 10, colors.black, colors.lightGray, false, false, false)
  162.         screenWrite("WIP", 34, 8, colors.black, colors.lightGray, false, false, false)
  163.         -- invGrid()
  164.     elseif info == 2 then
  165.         screenWrite("This program will place a bridge down.", "left", 6, colors.black, colors.lightGray, false, false, false)
  166.         screenWrite("It needs solid blocks to place.", "left", 7, colors.black, colors.lightGray, false, false, false)
  167.     elseif info == 3 then
  168.         screenWrite("You will need "..tostring(tArgs[1]*5 - turtle.getFuelLevel()).." more", "left", 6, colors.black, colors.lightGray, false, false, false)
  169.         screenWrite("fuel to complete this tunnel.", "left", 7, colors.black, colors.lightGray, false, false, false)
  170.         screenWrite("Left click drag to refuel enough.", "left", 9, colors.black, colors.lightGray, false, false, false)
  171.         screenWrite("Right click drag to refuel completely.", "left", 10, colors.black, colors.lightGray, false, false, false)
  172.     elseif info == "exit" then
  173.         screenWrite("Are you sure you want to exit?", "center", 6, colors.red, colors.lightGray, false, false, false)
  174.         screenWrite(" Yes ", 12, 9, colors.white, colors.gray, false, false, false)
  175.         screenWrite(" No ", screenX - 15, 9, colors.white, colors.gray, false, false, false)
  176.         while true do
  177.             local evt, arg1, arg2, arg3 = os.pullEvent()
  178.             if evt == "mouse_click" then
  179.                 if arg3 == 9 then
  180.                     if isBetween(12, 12+5, arg2) == true then
  181.                         clear(true)
  182.                         print("Thank you for using JJSax's tunnel program.")
  183.                         error()
  184.                     elseif isBetween(screenX - 15, screenX - 15 + 4, arg2) == true then
  185.                         clear(true)
  186.                         break
  187.                     end
  188.                 end
  189.             end
  190.         end
  191.     elseif info == "shortCenter" then
  192.         screenWrite(msg, "center", 7, colors.black, colors.lightGray, false, false, false)
  193.     end
  194. end
  195.  
  196. local function smartRefuel(fuelRequired) -- WIP
  197.     for i = 1, 14 do -- check for chest first
  198.         if tArgs[1]*5 > turtle.getFuelLevel() then
  199.             if turtle.getItemCount(i) > 0 then
  200.                 turtle.select(i)
  201.                 if turtle.refuel(0) == true then
  202.                     turtle.refuel(1)
  203.                     matchSlots = {}
  204.                     for i = 2, 14 do -- check for chest first
  205.                         if turtle.compareTo(i) then
  206.                             matchSlots[#matchSlots+1] = i
  207.                         end
  208.                     end
  209.                     local newFuelLevel = turtle.getFuelLevel()
  210.                     local fuelPerItem = newFuelLevel - startFuel
  211.                     local numberToFill = tArgs[1] * 5 / fuelPerItem
  212.                     turtle.refuel(numberToFill-1)
  213.                     if tArgs[1]*5 < turtle.getFuelLevel() then
  214.                         break
  215.                     end
  216.                 end
  217.             end
  218.             if turtle.getFuelLevel() > startFuel then
  219.                 tunnelDisplayExtra("shortCenter", turtle.getFuelLevel() - startFuel.." fuel consumed.")
  220.             elseif i < 16 then
  221.                 tunnelDisplayExtra("shortCenter", "No fuel yet.  Still looking")
  222.             else
  223.                 tunnelDisplayExtra("shortCenter", "No fuel in inventory.")
  224.             end
  225.         end
  226.     end
  227. end
  228.  
  229. local function turn(dir)
  230.     if dir == "left" or dir == "right" or dir == "around" then
  231.         if dir == "around" then
  232.             dir = "left 2"
  233.         end
  234.         shell.run("turn "..dir)
  235.     else
  236.         clear()
  237.         screenWrite("Not a turning dir", "center", 6, colors.black, colors.red, false, true, false)
  238.     end
  239. end
  240.  
  241. function checkDig(dir, cur)-- _ to dig forward
  242.     if dir ~= "up" then
  243.         if dir == "left" or dir == "right" then
  244.             turn(dir)
  245.         end
  246.         while turtle.detect() do
  247.             turtle.dig()
  248.             totalDig = totalDig + 1
  249.             tunnelDisplay(cur)
  250.         end
  251.         while turtle.suck() do end
  252.         if dir == "left" then
  253.             turn("right")
  254.         elseif dir == "right" then
  255.             turn("left")
  256.         end
  257.     else
  258.         if dir == "up" then
  259.             while turtle.detectUp() do
  260.                 turtle.digUp()
  261.                 totalDig = totalDig + 1
  262.                 tunnelDisplay(cur)
  263.                 sleep(.4)
  264.             end
  265.             while turtle.suckUp() do end
  266.         elseif dir == "down" then
  267.             while turtle.detectDown() do
  268.                 turtle.digDown()
  269.                 totalDig = totalDig + 1
  270.                 tunnelDisplay(cur)
  271.             end
  272.             while turtle.suckDown() do end
  273.         else
  274.             error("Can't dig "..dir)
  275.         end
  276.     end
  277. end
  278.  
  279. --[[ ADD CUSTIMIZATION SCREEN HERE
  280.  
  281. ]]
  282.  
  283. blocksNeeded = 10
  284. torchesNeeded = tArgs[1] / 5
  285.  
  286. local i = 0
  287. local timer = os.startTimer(1)
  288. clear(true)
  289. if (turtle.getFuelLevel() < tArgs[1] * 5) or (math.ceil(torchesNeeded - turtle.getItemCount(16)-1) >= 0) then
  290.     if math.ceil(torchesNeeded - turtle.getItemCount(16)) > 0 then
  291.         screenWrite("Need "..math.ceil(torchesNeeded-turtle.getItemCount(16)).." more torches in slot 16.", "center", 1, colors.orange, colors.black, false, false, false)
  292.     else
  293.         screenWrite("Torch supply fulfilled", "center", 1, colors.lime, colors.black, true, false, false)
  294.     end
  295.     if blocksNeeded-turtle.getItemCount(1) > 0 then
  296.         screenWrite("I recommend "..blocksNeeded-turtle.getItemCount(1).." more blocks.", "center", 2, colors.cyan, colors.black, true, false, false)
  297.     else
  298.         screenWrite("Block request fulfilled", "center", 2, colors.lime, colors.black, true, false, false)
  299.     end
  300.     if tArgs[1]*5 > turtle.getFuelLevel() then
  301.         screenWrite(" You need "..tArgs[1]*5 - turtle.getFuelLevel().." more fuel! ", "center", 3, colors.black, colors.red, false, false, false)
  302.     end
  303.  
  304.     -- up to stack 64 of items.
  305.  
  306.     screenWrite(" Exit ", "right", 13, colors.white, colors.red, false, false, false)
  307.     screenWrite("Press any letter to skip", "left", 13, colors.lime, colors.black, false, false, 50)
  308. end
  309. while (turtle.getFuelLevel() < tArgs[1] * 5) or (math.ceil(torchesNeeded - turtle.getItemCount(16)-1) >= 0) do
  310.     if math.ceil(torchesNeeded - turtle.getItemCount(16)) > 0 then
  311.         clear(true, 1)
  312.         screenWrite("Need "..math.ceil(torchesNeeded-turtle.getItemCount(16)).." more torches in slot 16.", "center", 1, colors.orange, colors.black, true, false, false)
  313.     else
  314.         screenWrite("Torch supply fulfilled", "center", 1, colors.lime, colors.black, true, false, false)
  315.     end
  316.     if blocksNeeded-turtle.getItemCount(1) > 0 then
  317.         screenWrite("I recommend "..blocksNeeded-turtle.getItemCount(1).." more blocks.", "center", 2, colors.cyan, colors.black, true, false, false)
  318.     else
  319.         screenWrite("Block request fulfilled", "center", 2, colors.lime, colors.black, true, false, false)
  320.     end
  321.     if tArgs[1]*5 > turtle.getFuelLevel() then
  322.         screenWrite(" You need "..tArgs[1]*5 - turtle.getFuelLevel().." more fuel! ", "center", 3, colors.black, colors.red, false, false, false)
  323.     else
  324.         screenWrite("Fuel request fulfilled", "center", 3, colors.lime, colors.black, true, false, false)
  325.     end
  326.     screenWrite("Press any letter to skip", "left", 13, colors.lime, colors.black, false, false, false)
  327.     screenWrite(" Exit ", "right", 13, colors.white, colors.red, false, false, false)
  328.     local evt, arg1, arg2, arg3 = os.pullEvent()
  329.     if evt == "timer" then
  330.         if arg1 == timer then
  331.             timer = os.startTimer(1)
  332.         end
  333.     elseif evt == "mouse_drag" then  -- Turn into function
  334.         startFuel = turtle.getFuelLevel()
  335.         turtle.select(1)
  336.         if arg1 == 1 then
  337.             if arg3 == 3 then
  338.             term.setTextColor(colors.white)
  339.             term.setBackgroundColor(colors.black)
  340.                 -- TEMP.  Real thing is in smartRefuel()
  341.                 while turtle.getFuelLevel() < tArgs[1] * 5 do
  342.                     for i = 2, 14 do
  343.                         if turtle.getItemCount(i) > 0 then
  344.                             turtle.select(i)
  345.                             if turtle.refuel(0) then
  346.                                 while turtle.getItemCount(i) > 0 do
  347.                                     turtle.refuel(1)
  348.                                 end
  349.                             end
  350.                         end
  351.                     end
  352.                 end
  353.            
  354.                 --smartRefuel()
  355.             end
  356.             --shell.run("noFuel") -- REMOVE
  357.         elseif arg1 == 2 then
  358.             if arg3 == 3 then
  359.                 for i = 1, 16 do
  360.                     if turtle.getItemCount(i) > 0 then
  361.                         turtle.select(i)
  362.                         if turtle.refuel(0) == true then
  363.                             turtle.refuel(turtle.getItemCount(i))
  364.                         end
  365.                     end
  366.                 end
  367.             end
  368.             --shell.run("noFuel") -- REMOVE
  369.         end
  370.     elseif evt == "mouse_click" then
  371.         if arg3 <= 3 then
  372.             clear(true, 4, 12)
  373.             if arg1 == 1 then
  374.                 tunnelDisplayExtra(arg3)
  375.             elseif arg1 == 2 then
  376.                
  377.             end
  378.         elseif arg3 == 13 then
  379.             if arg2 >= 33 then
  380.                 tunnelDisplayExtra("exit")
  381.             end
  382.         end
  383.     elseif evt == "char" then
  384.         break
  385.     end
  386. end
  387.  
  388. clear(true)
  389. tunnelDisplay(1)
  390. checkDig("up", 1)
  391.  
  392. shell.run("noFuel") -- REMOVE
  393. for i = 1, tArgs[1] do
  394.     tunnelDisplay(i)
  395.     sleep(.5)
  396. end
  397. --[[ -- Temp Cut
  398. for i = 1, tArgs[1] do
  399.     tunnelDisplay(i)
  400.     checkDig(_, i)
  401.     checkDig("left", i)
  402.     checkDig("right", i)
  403.     checkDig("up", i)
  404.     turtle.up()
  405.     checkDig("left", i)
  406.     checkDig("right", i)
  407.     turtle.down()
  408.     turtle.down()
  409.     checkDig("left", i)
  410.     checkDig("right", i)
  411.     if turtle.detectDown() == false then
  412.         for i = 1, 15 do -- change when adding chest
  413.             turtle.select(i)
  414.             if turtle.placeDown() then
  415.                 temp = true
  416.                 break
  417.             end
  418.             if temp ~= true then
  419.                 error("DEBUG: Add something to do if out of placeable blocks")
  420.             end
  421.         end
  422.     end
  423.     turtle.up()
  424. end
  425. ]]
  426.  
  427. clear(true)
  428. print("Thank you for using JJSax's tunnel program.")
Advertisement
Add Comment
Please, Sign In to add comment