Levistator

tunnelQuarry

Jul 16th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.26 KB | None | 0 0
  1. local tArgs = {...} --command line arguments table
  2. local resetPos = {} --table of arguments to set the persist values
  3. local prevPos = {} --table to hold the loaded saved previous values
  4. local height = tArgs[1] or prevHeight --sets height to equal, in order of precedence, tArgs[1] or prevHeight
  5. local width = tArgs[2] or prevWidth --sets width to equal, in order of precedence, tArgs[2] or prevWidth
  6. local depth = tArgs[3] or prevDepth --sets depth to equal, in order of precedence, tArgs[3] or prevDepth
  7. local prevHeight = prevPos[2] or 0 --sets the prevHeight equal to prevPos[2], but if it cant then it sets it equal to 0
  8. local prevWidth = prevPos[1] or 0 --same
  9. local prevDepth = prevPos[3] or 0 --same
  10. local persist = prevPos[4] or false --same except false.
  11. local currHeight = 0 --sets a changeable variable to 0 for saving
  12. local currWidth = 0 --sets a changeable variable to 0 for saving
  13. local currDepth = 0 --sets a changeable variable to 0 for saving
  14. local resetHeight = resetPos[2] or 0 --adjusts
  15. local resetWidth = resetPos[1] or 0
  16. local resetDepth = resetPos[3] or 0
  17. local m=0 --makes a multipurpose variable
  18. fs.makeDir("prevPos")
  19. fs.makeDir("resetPos")
  20.  
  21. function loadPrev()
  22.     local w = fs.open("prevPos/prevWidth","r")
  23.     local h = fs.open("prevPos/prevHeight","r")
  24.     local d = fs.open("prevPos/prevDepth","r")
  25.         table.insert(prevPos,w)
  26.         table.insert(prevPos,h)
  27.         table.insert(prevPos,d)
  28.     w.close()
  29.     h.close()
  30.     d.close()
  31. end
  32.  
  33. function checkPersist()
  34.     local p = fs.open("resetPos/persist","r")
  35.     if p == nil then
  36.         local pw = fs.open("resetPos/persist","w")
  37.         if pw ~= nil then
  38.             pw.write(persist)
  39.             pw.close()
  40.         end
  41.     elseif p ~= nil then
  42.         table.insert(prevPos,p)
  43.         p.close()
  44.     end
  45. end
  46.    
  47. function forward() --makes a persistant forward function in case of gravel/sand/mob/player
  48.     while turtle.forward() == false do --keeps looping if it can't move forward, stops when it does succesfully.
  49.         if turtle.detect() then --detects for a block as obstruction
  50.             turtle.dig()  -- breaks block if detected
  51.         else
  52.             turtle.attack() --else attacks to move mob/player
  53.         end
  54.     end
  55.     refuel() --checks if turtle needs fuel, fuels if needed
  56. end
  57.  
  58. function moveRight() --moves turtle right one
  59.     turtle.turnRight()
  60.     forward()
  61.     turtle.turnLeft()
  62. end
  63.  
  64. function moveLeft() -- moves turtle left one
  65.     turtle.turnLeft()
  66.     forward()
  67.     turtle.turnRight()
  68. end
  69.  
  70. function up() --makes a persistant up function in case of gravel/sand/mob/player
  71.     while turtle.up() == false do --loops to move up until succesful
  72.         if turtle.detectUp() == true then --if block in the way
  73.             turtle.digUp() --breaks it
  74.         else --if mob/player is in the way
  75.             turtle.attackUp() --attacks to move it
  76.         end
  77.     end
  78. end
  79.  
  80. function down() -- persistant down function
  81.     while turtle.down() == false do
  82.         if turtle.detectDown() == true then
  83.             turtle.digDown()
  84.         else
  85.             turtle.attack()
  86.         end
  87.     end
  88. end
  89.  
  90. function digRow() --makes function to dig a row. saves turtle position in row. allows session persistance
  91. local w = fs.open("prevPos/prevWidth","w") --opens width save file for writing
  92.     for i=1,math.abs(resetWidth-width)-1 do --loop to dig row. reacts for session persistance
  93.         turtle.dig()
  94.         if m==0 then --if it last moved left will move right
  95.             moveRight()
  96.             currWidth = currWidth+1 --currWidth increases towards right
  97.         elseif m==1 then --if last moved right will move left
  98.             moveLeft()
  99.             currWidth = currWidth-1 --currWidth decreases towards left
  100.         end
  101.         checkInv()
  102.         w.write(currWidth) --writes currWidth to width save file
  103.     w.close() --closes width save file
  104.     end
  105.     turtle.dig() --breaks the last block in the row without possibly moving too far
  106.     resetWidth=0
  107. end
  108.  
  109. function nextRow() --function to move turtle up to the next row. saves turtle height. allows session persistence
  110. local h = fs.open("prevPos/prevHeight","w") --opens height save file for writing
  111.     for i=1,math.abs(resetHeight-height)-1 do --loop to dig 1 depth  of tunnel
  112.         digRow() --initiates the digRow function
  113.         if m==0 then
  114.             m=1
  115.         elseif m==1 then
  116.             m=0
  117.         end
  118.         up() --when digRow is done moves turtle up to next row
  119.         currHeight = currHeight+1 --currHeight increases going up
  120.         h.write(currHeight) --writes currHeight to height save file
  121.         h.close() --closes height save file
  122.     end
  123.     digRow()
  124.     if m==0 then
  125.         m=1
  126.     elseif m==1 then
  127.         m=0
  128.     end
  129.     resetHeight=0
  130. end
  131.  
  132. function tunnel() --function to move the turtle deeper into tunnel. saves depth. allows session persistence
  133. local d = fs.open("prevPos/prevDepth","w") --opens depth save file for writing
  134.     persist = true
  135.     for i=1,math.abs(resetDepth-depth)-1 do --loop to dig tunnel
  136.         nextRow() --initiates the function nextRow
  137.         resetTurtlePos()
  138.         forward()
  139.         currDepth = currDepth+1 --currDepth increases further into tunnel
  140.         d.write(currDepth) --writes currDepth to depth save file
  141.         d.close() --closes depth save file.
  142.     end
  143.     nextRow() --digs final depth of tunnel
  144.     resetTurtlePos()
  145.     turtle.turnRight()
  146.     turtle.turnRight()
  147.     for i=1,math.abs(resetDepth-depth) do
  148.         forward()
  149.     end
  150.     resetDepth=0
  151. end
  152.  
  153. function resetTurtlePos() --function to reset the turtle so that the tunnel is straight. saves width and height. allows session persistence
  154. local h = fs.open("prevPos/prevHeight","w") --opens height save file for writing
  155. local w = fs.open("prevPos/prevWidth","w") --opens width save file for writing
  156.     if m==1 then --if it just moved right
  157.         for i=1,math.abs(resetHeight-height)-1 do --moves down, session persistence
  158.             down()
  159.             currHeight = currHeight-1 --currHeight decreases as move down
  160.             h.write(currHeight) --writes currHeight to height save file
  161.             h.close() --closes height save file
  162.         end
  163.         turtle.turnLeft()
  164.         for i=1,math.abs(resetWidth-width)-1 do --moves left, session persistence
  165.             forward()
  166.             currWidth = currWidth-1 --currWidth decreases as move left
  167.             w.write(currWidth) --writes currWidth to width save file
  168.             w.close() --closes width save file
  169.         end
  170.         turtle.turnRight()
  171.     elseif m==0 then --if it just moved left
  172.         for i=1,math.abs(resetHeight-height) do --moves down, session persistence
  173.             turtle.down()
  174.             currHeight = currHeight-1 --currHeight decreases as move down
  175.             h.write(currHeight) --write currHeight to height save file
  176.             h.close() --closes height save file
  177.         end
  178.     end
  179.     m=0
  180. end
  181.  
  182.  
  183. function cleanUp() --cleans inventory
  184.     turtle.select(16) --selects enderchest
  185.     turtle.turnRight()
  186.     turtle.turnRight() --turns around
  187.     turtle.place() --places enderchest
  188.     for i=1,14 do --selects each slot 1 through 14 and drops items into enderchest
  189.         turtle.select(i)
  190.         turtle.drop()
  191.     end
  192. end
  193.  
  194. function checkInv() -- checks if cleanUp needs to happen
  195.     if turtle.getItemCount(14) ~= 0 then --checks last possibly available inventory slot
  196.         cleanUp() --runs cleanUp if inventory is full
  197.     end
  198. end
  199.  
  200. function refuel() --checks if turtle needs fuel
  201.     if turtle.getFuelLevel() <400 then --if turtle has less than 400 fuel
  202.         turtle.select(15) --turtle selects coal
  203.         turtle.refuel(4) --refuels with 4
  204.         turtle.select(1) --selects slot 1 again
  205.     end
  206. end
  207.  
  208. function sessionPersistence() --allows for session persistence in this program
  209.     if resetTurtle == true then
  210.         local w = fs.open("prevPos/prevWidth","w")
  211.         local h = fs.open("prevPos/prevHeight","w")
  212.         local d = fs.open("prevPos/prevDepth","w")
  213.         local p = fs.open("prevPos/persist","w")
  214.             w.write(nil)
  215.             h.write(nil)
  216.             d.write(nil)
  217.             p.write(nil)
  218.         w.close()
  219.         h.close()
  220.         d.close()
  221.         p.close()
  222.     elseif resetTurtle == false then
  223.         loadPrev()
  224.        
  225.     end
  226. local w = fs.open("prevPos/prevWidth","r") --opens width save file for reading
  227. local h = fs.open("prevPos/prevHeight","r") --opens height save file for reading
  228. local d = fs.open("prevPos/prevDepth","r") --opens depth save file for reading
  229.     table.insert(resetPos,w) --sets data from prevPos/prevWidth to the table resetPos in slot [1]
  230.     table.insert(resetPos,h) --sets data from prevPos/prevHeight to the table resetPos in slot [2]
  231.     table.insert(resetPos,d) --sets data from prevPos/prevDepth to the table resetPos in slot [3]
  232.     if w~=nil and h~=nil and d~=nil then
  233.         w.close()
  234.         h.close()
  235.         d.close()
  236.     end
  237. end
  238.  
  239. function run()
  240.     checkPersist()
  241.     if persist == true then
  242.         print("Would you like to reset this program's persistence? true/false: ")
  243.         local resetTurtle = read()
  244.     end
  245.     sessionPersistence()
  246.     tunnel()
  247. end
  248.  
  249. run()
Advertisement
Add Comment
Please, Sign In to add comment