Advertisement
bob558

tunel - графика разобраться

Nov 9th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.13 KB | None | 0 0
  1. --Begin clear function
  2. local function clear()
  3.   term.clear()
  4.   term.setCursorPos(1,1)
  5. end
  6. --End clear function
  7.  
  8. --Begin drawMenu function
  9. local function drawMenu()
  10.     clear()
  11.     for i = 1,#options-1 do
  12.         if sel == i then write("*") end
  13.         print(options[i].." "..tostring(values[i]))
  14.     end
  15.     if sel == 8 then
  16.         print("\n\n\n["..options[8].."]")
  17.     else
  18.         print("\n\n\n"..options[8])
  19.     end
  20. end
  21. --End drawMenu function
  22.  
  23. --Begin setValue function
  24. local function setValue(value)
  25.     if value == "Disabled" then
  26.         value = false
  27.     else
  28.         value = true
  29.     end
  30.     return value
  31. end
  32. --End setValue function
  33.  
  34. --Begin toggle function
  35. local function toggle(able)
  36.     if able == "Disabled" then
  37.         able = "Enabled"
  38.     else
  39.         able = "Disabled"
  40.     end
  41.     return able
  42. end
  43. --End toggle function
  44.  
  45. --Begin saveProgress function
  46. local function saveProgress()
  47.     fs.delete("btProgress")
  48.     d = fs.open("btProgress", "w")
  49.     d.writeLine("This contains the current position of the turtle in btunnel.")
  50.     d.writeLine(loc)
  51.     d.writeLine(tostring(curLength))
  52.     d.writeLine(tostring(length))
  53.     d.writeLine(facing)
  54.     d.close()
  55. end
  56. --End saveProgress function
  57.  
  58. --Begin loadProgress function
  59. local function loadProgress()
  60.     d = fs.open("btProgress", "r")
  61.     comment1 = d.readLine()
  62.     loc = d.readLine()
  63.     curLength = tonumber(d.readLine())
  64.     length = tonumber(d.readLine())
  65.     facing = d.readLine()
  66. end
  67. --End loadProgress function
  68.  
  69. --Begin saveSettings function
  70. local function saveSettings()
  71.     if fs.exists("btSettings") then
  72.         fs.delete("btSettings")
  73.     end
  74.     h = fs.open("btSettings","w")
  75.     h.writeLine("--Better Tunnel Settings")
  76.     h.writeLine("--Deleting will reset settings")
  77.     h.close()
  78.     h = fs.open("btSettings","a")
  79.     h.writeLine(orders)
  80.     h.writeLine(useChests)
  81.     h.writeLine(useEnder)
  82.     h.writeLine(useTorches)
  83.     h.writeLine(bridgeGaps)
  84.     h.writeLine(tonumber(compID))
  85.     h.writeLine(tonumber(fuelSlot))
  86.     h.writeLine(tunnelType)
  87.     h.close()
  88. end
  89. --End saveSettings function
  90.  
  91. --Begin checkChests function
  92. local function checkChests()
  93.     if useChests then
  94.         max = 15
  95.         if chests == 0 then
  96.             if useEnder then
  97.                 print("\nPlease input an enderchest into slot "..chestSlot.."...")
  98.             else
  99.                 print("\nPlease input chests into slot "..chestSlot.."...")
  100.             end
  101.             while chests == 0 do
  102.                 chests = turtle.getItemCount(chestSlot)
  103.                 sleep(1)
  104.             end
  105.         end
  106.     end
  107. end
  108. --End the checkChests function
  109.  
  110. --Begin the checkTorches function
  111. local function checkTorches()
  112.     if useTorches then
  113.         if useChests then
  114.             torchSlot = 15
  115.             torches = turtle.getItemCount(torchSlot)  
  116.             max = 14
  117.         else
  118.             torchSlot = 16
  119.             torches = turtle.getItemCount(torchSlot)
  120.             max = 15
  121.         end
  122.         if torches == 0 then
  123.             print("\nPlease input torches into slot "..torchSlot.."...")
  124.             while torches == 0 do
  125.                 torches = turtle.getItemCount(torchSlot)
  126.                 sleep(1)
  127.             end
  128.         end
  129.     end
  130. end
  131. --End the checkTorches function
  132.  
  133. --Begin loadSettings function
  134. local function loadSettings()
  135.     h = fs.open("btSettings", "r")
  136.     comment1 = h.readLine()
  137.     comment2 = h.readLine()
  138.     orders = h.readLine()
  139.     useChests = h.readLine()
  140.     useEnder = h.readLine()
  141.     useTorches = h.readLine()
  142.     bridgeGaps = h.readLine()
  143.     compID = tonumber(h.readLine())
  144.     fuelSlot = tonumber(h.readLine())
  145.     tunnelType = h.readLine()
  146.     h.close()
  147.     max = 16
  148.     checkChests()
  149.     checkTorches()
  150. end
  151. --End loadSettings function
  152.  
  153. --Begin findLoc function
  154. local function findLoc()
  155.     if facing == "right" then
  156.         if loc == "top" then
  157.             dig()
  158.             down()
  159.             loc = "mid"
  160.             dig()
  161.             down()
  162.             loc = "bot"
  163.             dig()
  164.             turtle.turnLeft()
  165.             facing = "forw"
  166.             forward()
  167.             length = length+1
  168.         elseif loc == "mid" then
  169.             dig()
  170.             down()
  171.             loc = "bot"
  172.             dig()
  173.             turtle.turnLeft()
  174.             facing = "forw"
  175.             forward()
  176.             length = length+1
  177.         elseif loc == "bot" then
  178.             dig()
  179.             turtle.turnLeft()
  180.             facing = "forw"
  181.             forward()
  182.             length = length+1
  183.         end
  184.     end
  185. end
  186. --End findLoc function
  187.  
  188. --Begin modemExists function
  189. local function modemExists()
  190.     if peripheral.getType("right") == "modem" then
  191.         return true
  192.     end
  193.     return false
  194. end
  195. --End modemExists function
  196.  
  197. --Begin invalid function
  198. local function invalid()
  199.     print("\nInvalid")
  200.     sleep(1)
  201. end
  202. --End invalid function
  203.  
  204. --Begin isValid function
  205. local function isValid(check, isOrders)
  206.     vType = type(check)
  207.     check = string.lower(check)
  208.     if not isOrders or isOrders == nil then
  209.         if check == "y" or check == "n" and vType == "string" then
  210.             return true
  211.         end
  212.     else
  213.         if check == "u" or check == "r" and vType == "string" then
  214.             return true
  215.         end
  216.     end
  217.     return false
  218. end
  219. --End isValid function
  220.  
  221. --Begin the useFuel function
  222. local function useFuel()
  223.     clear()
  224.     if useFuelSlot then
  225.         continue = false
  226.         repeat
  227.             clear()
  228.             print("What slot would you like the fuel to be in?: ")
  229.             fuelSlot = tonumber(read())
  230.             if fuelSlot == torchSlot or fuelSlot == chestSlot or fuelSlot > 16 then
  231.                 continue = false
  232.                 invalid()
  233.             else
  234.                 if fuelSlot == max then
  235.                     max = max-1
  236.                 end
  237.                 print("\nPlease input fuel into slot ",fuelSlot,"...")
  238.                 fuelAmount = turtle.getItemCount(fuelSlot)
  239.                 while fuelAmount == 0 do
  240.                     fuelAmount = turtle.getItemCount(fuelSlot)
  241.                     sleep(1)
  242.                 end
  243.                 continue = true
  244.             end
  245.         until continue
  246.     end
  247.     continue = true
  248. end
  249. --End the useFuel Function
  250.  
  251. --Begin dropoff function
  252. local function dropOff()
  253.     turtle.digUp()
  254.     turtle.select(chestSlot)
  255.     turtle.placeUp()
  256.     for i = 1,max do
  257.         if turtle.getItemCount(i) > 0 and i ~= fuelSlot then
  258.             turtle.select(i)
  259.             turtle.dropUp()
  260.         end
  261.     end
  262.     if useEnder == true then
  263.         sleep(2)
  264.         turtle.select(chestSlot)
  265.         turtle.digUp()
  266.     end
  267.     turtle.select(1)
  268. end
  269. --End dropoff function
  270.  
  271. --Begin dig function
  272. local function dig()
  273.     while turtle.detect() do
  274.         turtle.dig()
  275.     end
  276. end
  277. --End dig function
  278.  
  279. --Begin up function
  280. function up(num)
  281.     curUp = 1
  282.     if num == nil then
  283.         num = 1
  284.     end
  285.     while curUp <= num do
  286.         if not turtle.up() then
  287.             turtle.digUp()
  288.             turtle.attackUp()
  289.         else
  290.             curUp = curUp+1
  291.         end
  292.     end
  293. end
  294. --End up function
  295.  
  296. --Begin forward function
  297. function forward(num)
  298.     curForw = 1
  299.     if num == nil then
  300.         num = 1
  301.     end
  302.     while curForw <= num do
  303.         if not turtle.forward() then
  304.             turtle.dig()
  305.             turtle.attack()
  306.         else
  307.             curForw = curForw+1
  308.         end
  309.     end
  310. end
  311. --End forward function
  312.  
  313. --Begin down function
  314. function down(num)
  315.     curDown = 1
  316.     if num == nil then
  317.         num = 1
  318.     end
  319.     while curDown <= num do
  320.         if not turtle.down() then
  321.             turtle.digDown()
  322.             turtle.attackDown()
  323.         else
  324.             curDown = curDown+1
  325.         end
  326.     end
  327. end
  328. --End down function
  329.  
  330. --Begin the tryRefuel function
  331. local function tryRefuel()
  332.     for i = 1,max do
  333.         if turtle.getItemCount(i) > 0 then
  334.             turtle.select(i)
  335.             if turtle.refuel(1) then
  336.                 return true
  337.             end
  338.         end
  339.     end
  340.     turtle.select(1)
  341.     return false
  342. end
  343. --End the tryRefuel function
  344.  
  345. --Begin refuel function
  346. local function refuel()
  347.     curFuel = turtle.getFuelLevel()
  348.     if curFuel == "unlimited" or curFuel >= 10 then
  349.         return
  350.     else
  351.         if not tryRefuel() then
  352.             print("Please supply more fuel to coninue...")
  353.             while not tryRefuel() do
  354.                 sleep(1)
  355.             end
  356.         end
  357.     end
  358. end
  359. --End refuel function
  360.  
  361. --Begin the stackFuel function
  362. local function stackFuel()
  363.     for i = 1,16 do
  364.         if turtle.getItemCount(i) > 0 and i ~= fuelSlot and i < max then
  365.             turtle.select(i)
  366.             if turtle.compareTo(fuelSlot) then
  367.                 turtle.transferTo(fuelSlot, turtle.getItemCount(i))
  368.             end
  369.         end
  370.     end
  371.     turtle.select(1)
  372. end
  373. --End the stackFuel function
  374.  
  375. --Begin tunnel function
  376. local function tunnel(tunLength)
  377.     for i = 1,tunLength do
  378.         if useTorches then
  379.             turtle.select(torchSlot)
  380.             if torch == 9 or curLength == 1 or curLength == tunLength-1 then
  381.                 turtle.digDown()
  382.                 turtle.placeDown()
  383.                 turtle.select(1)
  384.                 torch = 1
  385.             else
  386.                 torch = torch+1
  387.             end
  388.             turtle.select(1)
  389.         end
  390.         if bridgeGaps and not turtle.detectDown() then
  391.             turtle.placeDown()
  392.         end
  393.         print("Current tunnel length: ",curLength-1)
  394.         refuel()
  395.         turtle.turnLeft()
  396.         dig()
  397.         up()
  398.         dig()
  399.         if tunnelType == "3x3" then
  400.             up()
  401.             dig()
  402.         end
  403.         turtle.turnRight()
  404.         turtle.turnRight()
  405.         dig()
  406.         down()
  407.         dig()
  408.         if tunnelType == "3x3" then
  409.             down()
  410.             dig()
  411.         end
  412.         turtle.turnLeft()
  413.         if useFuelSlot == true then
  414.             stackFuel()
  415.         end
  416.         refuel()
  417.         if useChests then
  418.             if turtle.getItemCount(max) > 0 then
  419.                 dropOff()
  420.             end
  421.         end
  422.         turtle.select(1)
  423.         turtle.dig()
  424.         forward()
  425.         curLength = curLength+1
  426.         saveProgress()
  427.         saveSettings()
  428.     end
  429. end
  430. --End tunnel function
  431.  
  432.  
  433. --Begin Main code
  434. --[[Check if the turtle already began a tunnel and needs to finish it now
  435. if fs.exists("btProgress") then
  436.     print("I need to continue with my tunnel!")
  437.     loadProgress()
  438.     loadSettings()
  439.     findLoc()
  440. end
  441. --]]
  442.  
  443. --Declare Variables
  444. max = 16
  445. continue = false
  446. torchSlot = 15
  447. chestSlot = 16
  448. curLength = 1
  449. useSettings = false
  450. compID = 1
  451. facing = "right"
  452. torches = turtle.getItemCount(torchSlot)
  453. chests = turtle.getItemCount(chestSlot)
  454.  
  455. values={
  456.     "User",
  457.     "Disabled",
  458.     "Disabled",
  459.     "Disabled",
  460.     "Disabled",
  461.     "3x3",
  462.     "Disabled"
  463. }
  464. orders = values[1]
  465. useChests = values[2]
  466. useEnder = values[3]
  467. useTorches = values[4]
  468. bridgeGaps = values[5]
  469. tunnelType = values[6]
  470. useFuelSlot = values[7]
  471.  
  472. options={
  473.     "Receive orders via:",
  474.     "Use chests:",
  475.     "Use Enderchests:",
  476.     "Use Torches:",
  477.     "Bridge Gaps:",
  478.     "Tunnel Type:",
  479.     "Specific Slot for Fuel:",
  480.     "Continue"
  481. }
  482.  
  483. clear()
  484. print("Better Tunnel v2.0")
  485. sleep(2.5)
  486.  
  487. --If a settings file exists, ask the user if they want to load it
  488. if fs.exists("btSettings") then
  489.     continue = false
  490.     repeat
  491.         clear()
  492.         print("A settings file was found.")
  493.         print("Would you like to load it?(Y/N): ")
  494.         load = read()
  495.         load = string.lower(load)
  496.         if load == "y" then
  497.             useSettings = true
  498.             continue = true
  499.         elseif load == "n" then
  500.             useSettings = false
  501.             continue = true
  502.         else
  503.             invalid()
  504.         end
  505.     until continue
  506. else
  507.     useSettings = false
  508. end
  509.  
  510. --If they chose to load it, it will load the setting from the file
  511. if useSettings then
  512.     loadSettings()
  513. else--If the file didn't exist or the user chose not to load it, the program will ask the user for settings
  514.     sel = 1--The first option is selected when the screen is first drawn
  515.  
  516.     clear()--clear the screen before starting
  517.     while true do
  518.         clear()
  519.         drawMenu()--draw the menu
  520.         event, key=os.pullEvent("key")--wait for a key to be pressed
  521.         if key == keys.up and sel ~= 1 then--long and excessive logic(I feel it could be improved) to decide what to select next
  522.             sel = sel-1
  523.         elseif key == keys.down and sel ~= 8 then
  524.             sel = sel+1
  525.         elseif key == keys.enter then
  526.             if sel == 1 then
  527.                 if values[1] == "User" then
  528.                     values[1] = "Rednet"
  529.                 else
  530.                     values[1] = "User"
  531.                 end
  532.             elseif sel == 2 then
  533.                 values[2] = toggle(values[2])
  534.                 if values[2] == "Disabled" and values[3] ~= "Disabled" then
  535.                     values[3] = toggle(values[3])
  536.                 end
  537.             elseif sel == 3 then
  538.                 values[3] = toggle(values[3])
  539.                 if values[3] == "Enabled" and values [2] ~= "Enabled" then
  540.                     values[2] = toggle(values[2])
  541.                 end
  542.             elseif sel == 4 then
  543.                 values[4] = toggle(values[4])
  544.             elseif sel == 5 then
  545.                 values[5] = toggle(values[5])
  546.             elseif sel == 6 then
  547.                 if values[6] == "3x3" then
  548.                     values[6] = "2x3"
  549.                 else
  550.                     values[6] = "3x3"
  551.                 end
  552.             elseif sel == 7 then
  553.                 values[7] = toggle(values[7])
  554.             elseif sel == 8 then
  555.                 orders = values[1]
  556.                 useChests = setValue(values[2])
  557.                 useEnder = setValue(values[3])
  558.                 useTorches = setValue(values[4])
  559.                 bridgeGaps = setValue(values[5])
  560.                 useFuelSlot = setValue(values[7])
  561.                 break
  562.             end
  563.         end
  564.     end
  565.     useFuel()
  566.     checkChests()
  567.     checkTorches()
  568. end
  569.  
  570. if (not modemExists() and (orders == "Rednet")) then
  571.     clear()
  572.     print("No modem was detected on this turtle!")
  573.     print("User input has been automatically selected.")
  574.     orders = "User"
  575.     sleep(3)
  576. end
  577.  
  578. --Run the tunnel function based on the orders chosen
  579. if orders == "Rednet" then
  580.     rednet.open("right")
  581.     clear()
  582.     if not useSettings then
  583.         continue = false
  584.         repeat
  585.             print("You chose to receive orders via Rednet.")
  586.             write("What computer would you like me to accept messages from?: ")
  587.             compID = tonumber(read())
  588.             if compID > 0 then
  589.                 continue = true
  590.             else
  591.                 invalid()
  592.             end
  593.         until continue
  594.     end
  595.     clear()
  596.     print("I will listen for orders from the computer with id: "..compID)
  597.     id, msg = rednet.receive()
  598.     if id == tonumber(compID) then
  599.         length = msg
  600.         checkChests()
  601.         checkTorches()
  602.         print("Beginning tunnel with length: "..length)
  603.         tunnel(length)
  604.     end
  605. elseif orders == "User" then
  606.     repeat
  607.         clear()
  608.         print("You chose to receive orders via User.")
  609.         write("How long would you like the tunnel to be?(In blocks): ")
  610.         length = tonumber(read())
  611.         vType = type(length)
  612.         if vType == "number" then
  613.             print("Beginning tunnel with length: "..length)
  614.             continue = true
  615.             checkChests()
  616.             checkTorches()
  617.             tunnel(length)
  618.         else
  619.             invalid()
  620.             continue = false
  621.         end
  622.     until continue
  623. end
  624.  
  625. clear()
  626. print("I'm done!")
  627. print("I completed a tunnel with length: "..length)
  628.  
  629. if not useSettings then
  630.     print("Would you like to save these settings for use in the next tunnel?(Y/N): ")
  631.     save = read()
  632.     save = string.lower(save)
  633.     if save == "y" then
  634.         saveSettings()
  635.         print("Settings have been saved")
  636.         sleep(2)
  637.     end
  638. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement