largeNumberGoeshere

tunnelandback5.lua

Apr 19th, 2021 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.00 KB | None | 0 0
  1. args = {...}
  2. paces = 0
  3.  
  4. fuelStausLineNumber = 11
  5. paceStatusLineNumber = 12
  6. infoLineNumber = 13
  7.  
  8. fuelLinePrefix = "Fuel: "
  9.  
  10. waterBucketSlot = 2
  11.  
  12.      
  13. -- Predefined Functions
  14.    
  15.  function printToLine(str, line)
  16.      a,b = term.getCursorPos()
  17.      term.setCursorPos(1,line)
  18.      term.clearLine(1,line)
  19.      write(str)    
  20.      term.setCursorPos(a,b)
  21.  end
  22.  
  23.  
  24.  function replaceLava()
  25.         local a,b = turtle.inspectDown() or 0,0
  26.         if b~= "No block to inspect" and type(b) == "table" then
  27.                     if b.name then
  28.                             if b.name == "minecraft:lava" then
  29.                                     --place water
  30.                                     turtle.select(waterBucketSlot)
  31.                                     turtle.place()
  32.                                     sleep(1)
  33.                                     turtle.place()
  34.                            
  35.                             end
  36.                     end
  37.            
  38.             end
  39.  end
  40.  
  41.     function digSection()
  42.             if turtle.detectDown() then
  43.                     turtle.digDown()
  44.             end
  45.            
  46.             if turtle.detectUp() then
  47.                     turtle.digUp()
  48.             end
  49.            
  50.             if turtle.detect() then
  51.                     turtle.dig()
  52.             end
  53.            
  54.            
  55.             replaceLava()
  56.            
  57.    
  58.     end
  59.  
  60.     function turnBackwards()
  61.             turtle.turnLeft()
  62.             turtle.turnLeft()
  63.     end
  64.      
  65.     function attemptForward()
  66.             success = turtle.forward()
  67.             if not success then
  68.                     while turtle.forward() == false do
  69.                          digSection()
  70.                     end
  71.             end
  72.     end
  73.      
  74.      function isFuelAvailable()
  75.             turtle.select(fuelSlot)
  76.             while turtle.refuel(0) == false do
  77.                 printToLine("Please insert a valid fuel",fuelStausLineNumber)
  78.             end
  79.      end
  80.      
  81.      
  82.     function refuelIfNeccesary()
  83.          turtle.select(fuelSlot)
  84.     isFuelAvailable()
  85.              while turtle.getFuelLevel() < fuelBuffer do
  86.         turtle.select(fuelSlot)
  87.                              turtle.refuel(1)
  88.                              printToLine("Refueling",fuelStausLineNumber)
  89.                  end
  90.         printToLine(fuelLinePrefix.. tostring(turtle.getFuelLevel()) , fuelStausLineNumber)
  91. --               print("Fuel level: "..tostring(turtle.getFuelLevel()) )  
  92. end
  93.    
  94.      
  95. function estimateFuel()
  96.             turtle.select(fuelSlot)
  97.             --fuelPerItem = 80
  98.             fuelItems = turtle.getItemCount(fuelSlot)-1
  99.             --print(fuelPerItem)
  100.             totalFuel = fuelPerItem*fuelItems + turtle.getFuelLevel()
  101.             print(totalFuel)
  102.             return totalFuel
  103.     end
  104.    
  105.      
  106.     function estimateFuelNeeded()
  107.             fuelNeeded = length*2+fuelBuffer
  108.             return fuelNeeded    
  109.  end
  110.    
  111.      
  112. function isEnoughFuel()
  113.             fuelLevel = estimateFuel()
  114.             neededFuel = estimateFuelNeeded()
  115.            
  116.             print(fuelLevel.."fuel detected")
  117.             print("fuel required: ".. neededFuel)
  118.            
  119.             if fuelLevel < neededFuel then
  120.                    
  121.  
  122.      print("Not enough fuel for full journey")
  123.                     print(neededFuel-fuelBuffer.." more fuel units required")
  124.      print("That is approx this qty of your fuel")
  125.      print((neededFuel-fuelBuffer)/fuelPerItem)
  126.      error("Insufficent fuel provided")
  127.             end    
  128. end
  129.  
  130.          
  131.     function goBack()
  132.      turnBackwards()
  133.      for i=1, paces do
  134.                             attemptForward()
  135.                             refuelIfNeccesary()
  136.             end
  137.      turnBackwards()
  138.     end
  139.    
  140.      
  141.     function returnIfFull()            
  142.             slotsFull = 0
  143.             for i=1, 16, 1 do
  144.                    
  145.                     turtle.select(i)
  146.                     if turtle.getItemCount() > 0 then
  147.                             slotsFull = slotsFull + 1
  148.                     else
  149.                                    
  150.                     end
  151.                                
  152.             end
  153.             if slotsFull >= maxSlot then
  154.                     print("max slots full. returning")
  155.      goBack()
  156.                     error("No space left")
  157.             end
  158.            
  159.             turtle.select(fuelSlot)
  160.     end
  161.    
  162.     function msgAndWait(msg)
  163.         write(tostring(msg)..": ")
  164.         read("")
  165.         write("")
  166.     end
  167.    
  168.    
  169.     function yesOrNoToBool(response)
  170.         local responseStr   = tostring(response)
  171.         local responseLower = string.lower(responseStr)
  172.        
  173.         if responseLower == "yes" or responseLower == "y" then
  174.             return true
  175.            
  176.         elseif responseLower == "no" or responseLower == "n" then
  177.                 return false
  178.        
  179.         else
  180.             local errorDescription = "invalid response: '"..responseLower.."'"
  181.             return nil
  182.         end
  183.     end
  184.    
  185. function setToDefaultIfNil(var,defaultValue)
  186.         if var == nil then
  187.                 return defaultValue
  188.         else
  189.                 return var
  190.         end
  191. end
  192.    
  193.  
  194. function getFuelValue()
  195.         --fuelSlot = ,,,
  196.  
  197.         local getFuelVal = require("getFuelVal")
  198.         local fuelVal = getFuelVal(fuelSlot)
  199.         return fuelVal
  200.  
  201. end
  202.  
  203.     --- set variables and request from user if needed. If all esle fails, set to defaults
  204.     function setup()
  205.         --values
  206.         maxSlot = 12
  207.         slotCheckTMax= 10
  208.         fuelSlot = 1
  209.  
  210.  
  211.         fuelBuffer = 100
  212.         dist = args[1]
  213.         confirm  = yesOrNoToBool(args[2]) --or yesOrNoToBool("yes")
  214.         goBackVal= yesOrNoToBool(args[3]) --or yesOrNoToBool("yes")
  215.         fuelValue = args[4] --or getFuelValue()
  216.        
  217.         dist = setToDefaultIfNil(dist,10)
  218.         confirm = setToDefaultIfNil(confirm,yesOrNoToBool("yes"))
  219.         goBackVal = setToDefaultIfNil(goBackVal,yesOrNoToBool("yes"))
  220.         fuelValue = setToDefaultIfNil(fuelValue,getFuelValue())
  221.        
  222.        
  223.         --estimate fuel value
  224.  
  225.        
  226.         --
  227.        
  228.        
  229.         if dist == nil then
  230.   print("Did you know,")
  231.         print("You may also specify any values as paramaters? ")
  232.         print("you can type - to not specify an expected paramater :")
  233.         print("  1: length forward")
  234.         print("  2: wait for confirmation screen before going? (yes/no)")
  235.         print("  3: go back after? (yes/no)")
  236.         print("  4: fuel value as a number")
  237.        
  238.         -- print("2: fuel slot no")
  239.         -- print("3: fuel buffer" )
  240.         -- print("4: fuel value (coal = 80)")
  241.         -- print("5: max slots full")
  242.         -- print("6: slot full check interval")
  243.         -- print("7: show confirm menu? yes/no")
  244.         end
  245.          
  246.         if dist == nil then
  247.                 write("Please enter length forward: ")
  248.         end
  249.         length = tonumber(dist) or tonumber(read())  or 10
  250.          
  251.         -- if args[2] == nil then
  252.                 -- write("please enter fuel slot: ")
  253.         -- end
  254.         -- fuelSlot = tonumber(args[2]) or tonumber(read()) or 1
  255.          
  256.         if fuelValue == nil then
  257.             print()
  258.             print(
  259.             [[Please enter fuel value
  260.             coal = 80  
  261.             biofuel can = 520
  262.             scrap = 15  
  263.             coalfuel can = 1520
  264.             wooden scaffolding = 15
  265.             peat = 80  
  266.             sugarcane = 0 (Do not use it!)
  267.             ...
  268.             ]])
  269.              
  270.             write("press enter to see more")
  271.             read("")
  272.             print([[
  273.             wooden tools = 10  
  274.             lava = 1000
  275.             blaze rod = 120
  276.             wood blocks = 15
  277.             sticks = 5
  278.             coal or charcoal = 80
  279.             mushroom block = 15
  280.             Coal coke = 320
  281.             ]])
  282.             write("select value: ")
  283.         end
  284.        
  285.         fuelPerItem = tonumber(fuelValue) or tonumber(read()) --or 80
  286.          
  287.         print(confirm)
  288.         if confirm == true then
  289.                 print("----------------")
  290.                 write("length: "..tostring(length) .."\n")
  291.                 write("FuelSlot: " .. tostring(fuelSlot) .."\n")
  292.                 write("Fuel buffer: "..tostring(fuelBuffer) .."\n")
  293.                 write("Item fuel value: "..tostring(fuelPerItem) .. "\n")
  294.                 print("")
  295.                  
  296.                 write("press enter to begin: ")
  297.                 read("X")
  298.         elseif confirm == false then
  299.                
  300.         else
  301.                 print("Error: invalid confirm value.")
  302.                 read("X")
  303.                 --read("")
  304.         end
  305. end
  306.  
  307. function isTurtle()
  308.         if not turtle then
  309.                 error("this program requires a turtle")
  310.         end
  311. end
  312.  
  313.  
  314. function main()
  315.     for i=1, length do
  316.     if paces%slotCheckTMax == 0 then
  317.         returnIfFull()
  318.     end    
  319.        
  320.         -- dig the blocks
  321.         digSection()
  322.        
  323.         -- go forward and repeat until done
  324.     canGoForward = turtle.forward()
  325.     while not canGoForward do
  326.          digSection()
  327.          canGoForward = turtle.forward()
  328.     end
  329.        
  330.         -- we have gone one pace
  331.     paces = paces + 1
  332.     refuelIfNeccesary()
  333.  
  334.         -- If we've gone all the way then 'return to sender'
  335.    if paces ==  length then
  336.                 if goBackVal then
  337.                         printToLine("going back",infoLineNumber)
  338.                         goBack()
  339.                         sleep(1)
  340.                 else
  341.                         printToLine("not going back - ",infoLineNumber)
  342.                         sleep(1)
  343.                        
  344.                 end
  345.    end
  346.    
  347.         --update display to show status
  348.    printToLine("Paces: "..tostring(paces+1).."/"..tostring(length),paceStatusLineNumber)
  349.    printToLine(fuelLinePrefix..tostring(turtle.getFuelLevel()),fuelStausLineNumber)
  350.  
  351.     end
  352. end
  353.  
  354. ------------------------------------------------------------------
  355. --         \/ The magic begins here \/
  356. ------------------------------------------------------------------
  357.  
  358. isTurtle()
  359.  
  360. term.clear()
  361. setup()
  362. isEnoughFuel()
  363. term.clear()
  364. isFuelAvailable()
  365. refuelIfNeccesary()
  366.  
  367. turtle.up()
  368. main()
  369.    
  370. print()
  371. term.clear()
  372. term.setCursorPos(1,1)
  373. turtle.down()
Add Comment
Please, Sign In to add comment