Advertisement
guitarplayer616

SuperTurtle

Dec 5th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.71 KB | None | 0 0
  1. local sArgs = ...
  2. local start = sArgs or "right"
  3. if start == "right" then
  4.   start2 = "left"
  5. else
  6.   start2 = "right"
  7. end
  8. faces = {}
  9. faces[0] = "north"
  10. faces[1] = "east"
  11. faces[2] = "south"
  12. faces[3] = "west"
  13.  
  14.  
  15.  
  16. function init()
  17.   if fs.exists("coords") then
  18.     os.loadAPI("coords")
  19.     x = coords.cX
  20.     y = coords.cY
  21.     z = coords.cZ
  22.     face = coords.cFace
  23.     distance = coords.cDistance
  24.     print(x,y,z,face,distance)
  25.   else
  26.     x = 309
  27.     y = 2
  28.     z = 152
  29.     distance = 0
  30.     face = "north"
  31.     face = tostring(face):lower()
  32.     rewriteCoords()
  33.   end
  34.  
  35.   shell.run("clear")
  36.   print("requires 2 chunkLoaders in slot 14")
  37.   print("requires 1 enderChest in slot 15")
  38.   print("requires 1 bucket in slot 16")
  39.   print("requires minimum 100 fuel")
  40.   print(nil)
  41.   print(x..", "..y..", "..z)
  42.   print(face)
  43.   while turtle.getFuelLevel() < 100 do
  44.     local times = 0
  45.     turtle.refuel(64)
  46.     sleep(.5)
  47.     if times >= 25 then
  48.       panic("Reinit: no fuel")  
  49.     end
  50.     times = times + 1
  51.   end
  52.   while true do
  53.     local times = 0
  54.     for i=1,16 do
  55.     turtle.select(i)
  56.     local sel = turtle.getItemDetail()
  57.     if sel and sel.name == "minecraft:bucket" then
  58.         turtle.transferTo(16)
  59.     elseif sel and sel.name == "EnderStorage:enderChest" then
  60.         turtle.transferTo(15)
  61.     elseif sel and sel.name == "ChickenChunks:chickenChunkLoader" then
  62.         turtle.transferTo(14)
  63.     end
  64.     end
  65.     local chunk = turtle.getItemDetail(14)
  66.     local chest = turtle.getItemDetail(15)
  67.     local bucket = turtle.getItemDetail(16)
  68.     if chunk and chest and bucket then
  69.       if chunk.name == "ChickenChunks:chickenChunkLoader" and chest.name == "EnderStorage:enderChest" and bucket.name == "minecraft:bucket" then
  70.     break
  71.       end
  72.     end
  73.     if times >= 25 then
  74.     panic("Scattered Inventory")
  75.         print("no stuff")
  76.     end
  77.     sleep(1)
  78.   end
  79. end
  80.  
  81. function upload(path)
  82.     local file = fs.open( path, "r" )
  83.     local text = file.readAll()
  84.     file.close()
  85.     local response = http.post(
  86.         "http://pastebin.com/api/api_post.php",
  87.         "api_option=paste&"..
  88.         "api_dev_key=ddfc115bf6e1cd0262ed53ee041c124b&"..
  89.     "api_user_key=14fbf14a69d7c0652d52ce3dea8f7727&"..
  90.         "api_paste_format=lua&"..
  91.         "api_paste_name="..textutils.urlEncode(os.getComputerLabel()).."&"..
  92.         "api_paste_code="..textutils.urlEncode(text)
  93.     )
  94.     if response then
  95.     print(response.readAll())
  96.     end
  97. end
  98.  
  99. function panic(s)
  100.   local h = fs.open("log","a")
  101.   h.writeLine("SuperMiningTurtle(TM)")
  102.   h.writeLine("ERROR!")
  103.   h.writeLine("Coordinates: ("..x..", "..y..", "..z..")")
  104.   h.writeLine("Distance: "..distance)
  105.   h.writeLine("Direction: "..face)
  106.   h.writeLine("Fuel: "..turtle.getFuelLevel())
  107.   h.writeLine("InvCount: "..checkIfFull())
  108.   h.writeLine("BucketSlot(16): "..turtle.getItemDetail(16).name)
  109.   h.writeLine("ChestSlot(15): "..turtle.getItemDetail(15).name)
  110.   h.writeLine("ChunkloaderSlot(14): "..turtle.getItemDetail(14).name)
  111.   if s then
  112.     h.writeLine([[Error Description: "]]..s..[["]])
  113.   end
  114.   h.close()
  115.   upload("log")
  116.   textutils.slowPrint("ERROR")
  117.   error()
  118. end    
  119.    
  120.    
  121. function update()
  122.   if face == "north" then
  123.     z = z - 1
  124.   elseif face == "south" then
  125.     z = z + 1
  126.   elseif face == "west" then
  127.     x = x - 1
  128.   elseif face == "east" then
  129.     x = x + 1
  130.   end
  131.   rewriteCoords()
  132. end
  133.  
  134. function updateFace(dir)
  135.   if dir == "right" then
  136.     if face == "north" then
  137.         face = "east"
  138.     elseif face == "east" then
  139.         face = "south"
  140.     elseif face == "south" then
  141.         face = "west"
  142.     elseif face == "west" then
  143.         face = "north"
  144.     end
  145.   elseif dir == "left" then
  146.     if face == "north" then
  147.         face = "west"
  148.     elseif face == "west" then
  149.         face = "south"
  150.     elseif face == "south" then
  151.         face = "east"
  152.     elseif face == "east" then
  153.         face = "north"
  154.     end
  155.   end
  156.  
  157.   --for i = 0,3 do
  158.     --if face == faces[i] then
  159.       --if dir == "right" then
  160.     --face = faces[(i + 1)%4]
  161.       --elseif dir == "left" then
  162.         --face = faces[(i + 3)%4]
  163.       --end
  164.     --end
  165.   --end
  166.   rewriteCoords()
  167. end
  168.  
  169. function rewriteCoords()
  170.   local h = fs.open("coords","w")
  171.   h.writeLine("cX = "..x)
  172.   h.writeLine("cY = "..y)
  173.   h.writeLine("cZ = "..z)
  174.   h.writeLine("cFace = \""..face.."\"")
  175.   h.writeLine("cDistance = "..distance)
  176.   h.close()
  177.   os.loadAPI("coords")
  178. end
  179.  
  180. function mine(dir)
  181.   if dir == "left" or "right" then
  182.     shell.run("go "..dir)
  183.     updateFace(dir)
  184.     cycle()
  185.     shell.run("go "..dir)
  186.     updateFace(dir)
  187.     for i=1,4 do
  188.       cycle()
  189.     end
  190.   else
  191.     return
  192.   end
  193.   distance = distance + 1
  194. end  
  195.    
  196. function cycle()
  197.   local count = 0
  198.   turtle.dig()
  199.   if not turtle.forward() then
  200.       sleep(1)
  201.       if not turtle.dig() then
  202.         panic("stuck")
  203.         print("stuck")
  204.       else
  205.     while not turtle.forward() do turtle.dig() end
  206.     update()
  207.       end
  208.   else
  209.     update()
  210.   end
  211.   turtle.digUp()
  212.   turtle.digDown()
  213.   if not turtle.detectDown() then
  214.     turtle.select(16)
  215.     if turtle.placeDown() then
  216.       if not turtle.refuel(64) then
  217.         turtle.placeDown()
  218.       end
  219.     end
  220.   end
  221. end
  222.  
  223. function checkInv()
  224.   if turtle.getFuelLevel() < 1000 then
  225.     shell.run("refuel")
  226.   end
  227.   if checkIfFull() == 16 then
  228.     emptyInv()
  229.   end
  230.   if distance%16 == 0 or distance == 0 then
  231.     loadChunks()
  232.   end
  233. end  
  234.  
  235. function checkIfFull()
  236.   local inv = 0
  237.   for i=1,16 do
  238.     turtle.select(i)
  239.     if turtle.getItemCount() > 0 then
  240.       inv = inv + 1
  241.     end
  242.   end
  243.   return inv
  244. end
  245.  
  246. function emptyInv()
  247.   turtle.select(15)
  248.   turtle.placeDown()
  249.   for i = 1,13 do
  250.     turtle.select(i)
  251.     turtle.dropDown(64)
  252.   end
  253.   turtle.select(15)
  254.   turtle.digDown()
  255. end
  256.  
  257. function loadChunks()
  258.   turtle.select(14)
  259.   turtle.placeDown()
  260.   if distance ~= 0 then
  261.     local left, right = travelBack()
  262.     turtle.select(14)
  263.     turtle.digDown()
  264.     shell.run("go right right")
  265.     updateFace("right")
  266.     updateFace("right")
  267.     for i=1,16 do
  268.       turtle.forward()
  269.       update()
  270.     end
  271.     if not left then
  272.       turtle.turnLeft()
  273.       updateFace("left")
  274.     else
  275.       turtle.turnRight()
  276.       updateFace("right")
  277.     end
  278.   end
  279. end
  280.  
  281. function travelBack()
  282.   turtle.turnRight()
  283.   updateFace("right")
  284.   local right = turtle.detect()
  285.   turtle.turnLeft()
  286.   updateFace("left")
  287.   turtle.turnLeft()
  288.   updateFace("left")
  289.   local left = turtle.detect()
  290.   turtle.turnRight()
  291.   updateFace("right")
  292.   if not left then
  293.     turtle.turnLeft()
  294.     updateFace("left")
  295.   else
  296.     turtle.turnRight()
  297.     updateFace("right")
  298.   end
  299.   for i=1,16 do
  300.     turtle.forward()
  301.     update()
  302.   end
  303.   return left, right
  304. end
  305.  
  306. init()
  307. checkInv()
  308. while true do
  309.     mine(start)
  310.     mine(start2)
  311.     checkInv()
  312. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement