Advertisement
Guest User

adex

a guest
Jan 21st, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.19 KB | None | 0 0
  1. local y = fs.open("startup","w")
  2. y.write("shell.run(\"adex\")")
  3. y.close()
  4. function ask(napis,oneKey)
  5. term.clear()
  6. term.setCursorPos(1,1)
  7. term.write(napis)
  8. term.setCursorPos(1,2)
  9. local answer = ""
  10. local x,answerPos = term.getCursorPos()
  11. local nEvent,nKey = os.pullEvent()
  12. while nKey == keys.enter do
  13. nEvent,nKey = os.pullEvent()
  14. end
  15. if oneKey then
  16. return nKey,nEvent
  17. else
  18. answer = read()
  19. return answer
  20. end
  21. end
  22.  
  23.  
  24. if fs.exists("TEMP") then
  25. shell.run("TEMP")
  26. os.loadAPI("TEMP")
  27. else
  28. depth = 0
  29. xPos,zPos = 0,0
  30. xDir,zDir = 1,0
  31. a = {
  32. size = tonumber(ask("Podaj wielkosc wykopu:")),
  33. proX = 0,
  34. proY = 0,
  35. proZ = 0,
  36. ores = 0,
  37. bedrock = false,
  38. moveX = tonumber(ask("Podaj przesuniecie x:")),
  39. moveY = tonumber(ask("Podaj przesuniecie y:")),
  40. moveZ = tonumber(ask("Podaj przesuniecie z:")),
  41. }
  42. a.proX = a.moveX
  43. a.proZ = a.moveZ
  44. end
  45. local goTo -- Filled in further down
  46. local refuel -- Filled in further down
  47.  
  48. function save()
  49. local temp = fs.open("TEMP","w")
  50. temp.write("\nxPos="..tostring(xPos))
  51. temp.write("\nzPos="..tostring(zPos))
  52. temp.write("\ndepth="..tostring(depth))
  53. temp.write("\nxDir,zDir="..tostring(xDir)..","..tostring(zDir).."\n")
  54. temp.write("a="..textutils.serialize(a))
  55. temp.close()
  56. end
  57.  
  58. local function unload( _bKeepOneFuelStack )
  59.     print( "Unloading items..." )
  60.     for n=1,16 do
  61.         local nCount = turtle.getItemCount(n)
  62.         if nCount > 0 then
  63.             turtle.select(n)           
  64.             local bDrop = true
  65.             if _bKeepOneFuelStack and turtle.refuel(0) then
  66.                 bDrop = false
  67.                 _bKeepOneFuelStack = false
  68.             end        
  69.             if bDrop then
  70.                 turtle.drop()
  71.             end
  72.         end
  73.     end
  74.     collected = 0
  75.     turtle.select(1)
  76.  refuel()
  77. end
  78.  
  79. local function returnSupplies()
  80.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  81.     print( "Returning to surface..." )
  82.     goTo( 0,0,0,-1,0 )
  83.    
  84.     local fuelNeeded = 12*(x+y+z) + 1
  85.     if not refuel( fuelNeeded ) then
  86.         unload( true )
  87.         print( "Waiting for fuel" )
  88.         while not refuel( fuelNeeded ) do
  89.             os.pullEvent( "turtle_inventory" )
  90.         end
  91.     else
  92.         unload( true ) 
  93.     end
  94.    
  95.     print( "Resuming mining..." )
  96. end
  97.  
  98. local function collect()   
  99.     local bFull = true
  100.     local nTotalItems = 0
  101.     for n=1,16 do
  102.         local nCount = turtle.getItemCount(n)
  103.         if nCount == 0 then
  104.             bFull = false
  105.         end
  106.         nTotalItems = nTotalItems + nCount
  107.     end
  108.    
  109.     if nTotalItems > collected then
  110.         collected = nTotalItems
  111.         if math.fmod(collected + unloaded, 50) == 0 then
  112.             print( "Mined "..(collected + unloaded).." items." )
  113.         end
  114.     end
  115.    
  116.     if bFull then
  117.         print( "No empty slots left." )
  118.         return false
  119.     end
  120.     return true
  121. end
  122.  
  123. function refuel( ammount )
  124.     local fuelLevel = turtle.getFuelLevel()
  125.     if fuelLevel == "unlimited" then
  126.         return true
  127.  end
  128. local needed = 19000   
  129. if turtle.getFuelLevel() < needed then
  130.         local fueled = false
  131.   turtle.select(1)
  132.         while turtle.refuel(1) do
  133.             if turtle.placeUp() then
  134.                     if turtle.getFuelLevel() >= needed then
  135.                         return true
  136.                     end
  137.             else
  138.    return false
  139.             end
  140.         end
  141.         return false
  142.     end
  143.     return true
  144. end
  145.  
  146. local function checkBed()
  147. local bUp,nUp = turtle.inspectUp()
  148. local bDown,nDown = turtle.inspectDown()
  149. local bFor,nFor = turtle.inspect()
  150. if bFor then
  151.   if string.find(string.lower(nFor.name),"bedrock") then
  152.     a.bedrock = true
  153.    return true
  154.   else
  155.     a.proY = a.proY-1
  156.   end
  157. end
  158. return false
  159. end
  160.  
  161. local function tryUp()
  162. while not turtle.up() do
  163.   if not turtle.digUp() then
  164.     if turtle.detect() then
  165.      if checkBed() then
  166.      return false
  167.     end
  168.     else
  169.     sleep(0.5)
  170.     end
  171.   end
  172. end
  173. depth = depth + 1
  174. save()    
  175. return true
  176. end
  177.  
  178. local function tryForwards()   
  179.     while not turtle.forward() do
  180.         if not turtle.dig() then
  181.    if turtle.detect() then
  182.     if checkBed() then
  183.     bedrock = true
  184.     a.proY = a.proY + 1
  185.     end
  186.     if not tryUp() then
  187.     return false
  188.           end
  189.     end
  190.     if turtle.attack() or turtle.attackUp() then
  191.              turtle.suck()
  192.              sleep( 0.5 )
  193.           end
  194.   end
  195.     end
  196.     xPos = xPos + xDir
  197.     zPos = zPos + zDir
  198. save()
  199.     return true
  200. end
  201.  
  202. local function tryDown()   
  203.     while not turtle.down() do
  204.         if not turtle.digDown() then
  205.    if turtle.detectDown() then
  206.     if not a.bedrock then
  207.     print("Found bedbrock!Finishing...")
  208.     a.bedrock=true
  209.     end
  210.     a.proY=a.proY+1
  211.     save()
  212.     return false    
  213.     end
  214.    end
  215.     end
  216.  
  217.     depth = depth - 1
  218. save()
  219.     return true
  220. end
  221.  
  222. local function turnLeft()
  223.     turtle.turnLeft()
  224.     xDir, zDir = -zDir, xDir
  225. save()
  226. end
  227.  
  228. local function turnRight()
  229.     turtle.turnRight()
  230.     xDir, zDir = zDir, -xDir
  231. save()
  232. end
  233.  
  234. function goTo( x, y, z, xd, zd)
  235. while x~=xPos or y~=depth or z~=zPos or bedrock do
  236.     if zPos > z then
  237.         while zDir ~= -1 do
  238.             turnLeft()
  239.         end
  240.         while zPos > z do
  241.          tryForwards()
  242.         end
  243.     elseif zPos < z then
  244.         while zDir ~= 1 do
  245.             turnLeft()
  246.         end
  247.         while zPos < z do
  248.             tryForwards()
  249.         end
  250.     end
  251.    
  252.     if xPos > x then
  253.         while xDir ~= -1 do
  254.             turnLeft()
  255.         end
  256.         while xPos > x do
  257.             tryForwards()
  258.         end
  259.     elseif xPos < x then
  260.         while xDir ~= 1 do
  261.             turnLeft()
  262.         end
  263.         while xPos < x do
  264.             tryForwards()
  265.         end
  266.     end
  267.     while depth ~=y do
  268.     if depth > y then
  269.         if not tryDown() then
  270.   tryForwards()
  271.   end
  272.     else
  273.   if not tryUp() then
  274.   tryForwards()
  275.   end
  276.  end
  277.     end
  278. end
  279.     while zDir ~= zd or xDir ~= xd do
  280.         if xDir==zd and zDir==-xd then
  281.   turnLeft()
  282.   else
  283.   turnRight()
  284.      end
  285. end
  286. end
  287.  
  288. function checkFull()
  289. if turtle.getItemCount(16)~=0 then
  290. returnSupplies()
  291. goTo(0,a.proY+a.moveY,0,1,0)
  292. end
  293. term.clear()
  294. term.setCursorPos(1,1)
  295. print("exp XYZ: ",a.proX," ",a.proY," ",a.proZ)
  296. print("moje XYZ: ",xPos," ",depth," ",zPos)
  297. end
  298.  
  299. if not turtle.inspect then
  300. turtle.inspectUp = function()
  301. turtle.digUp()
  302. return false
  303. end
  304. turtle.inspectDown = function()
  305. turtle.digDown()
  306. return false
  307. end
  308. end
  309.  
  310. function checkOre()
  311. local function inspect()
  312. local bUp,Up = turtle.inspectUp()
  313. local bDown,Down = turtle.inspectDown()
  314. if bUp then
  315.   if string.find(string.lower(Up.name),"ore") then
  316.   turtle.digUp()
  317.   a.ores=a.ores+1
  318.   end
  319. end
  320. if bDown then
  321.   if string.find(string.lower(Down.name),"ore") then
  322.   turtle.digDown()
  323.   a.ores=a.ores+1
  324.   end
  325. end
  326. end
  327.  
  328. local function mustdig()
  329. while true do
  330.   if not turtle.dig() then
  331.   sleep(1)
  332.   end
  333. end
  334. end
  335. parallel.waitForAny(inspect,mustdig)
  336. end  
  337.  
  338. print("excavating...")
  339. if xPos == 0 and zPos == 0 and depth ==0 then
  340. refuel(1)
  341. end
  342. checkFull()
  343. if a.proY+a.moveY~=depth then
  344. goTo(xPos,a.proY+a.moveY,zPos,xDir,zDir)
  345. end
  346. while true do
  347. while a.proZ-a.moveZ < a.size do
  348.   while a.proX-a.moveX < a.size do
  349.      if math.fmod(a.proY,6)==0 then
  350.       if math.fmod(a.proZ,2)==0 then
  351.         goTo(a.proX,a.proY+a.moveY,a.proZ,1,0)
  352.       else
  353.         goTo(a.size-a.proX-1,a.proY+a.moveY,a.proZ,-1,0)
  354.       end
  355.      else
  356.       if math.fmod(a.proZ,2)==0 then
  357.         goTo(a.size-a.proX-1,a.proY+a.moveY,a.size-a.proZ-1,-1,0)
  358.       else
  359.         goTo(a.proX,a.proY+a.moveY,a.size-a.proZ-1,1,0)
  360.       end
  361.      end    
  362.     checkOre()
  363.     checkFull()
  364.     a.proX = a.proX + 1
  365.     save()
  366.   end
  367.   a.proX = a.moveX
  368.   a.proZ = a.proZ+1
  369.   save()
  370. end  
  371. a.proZ = a.moveZ
  372. a.proY = a.proY-3
  373. if a.bedrock then
  374. break
  375. end
  376. save()
  377. end
  378. goTo(0,0,0,-1,0)
  379. unload(false)
  380. print("Found "..a.ores.." ores total")
  381. fs.delete("startup")
  382. fs.delete("TEMP")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement