Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.49 KB | None | 0 0
  1. if posx == nil then posx = 1 end
  2. if posy == nil then posy = 0 end
  3.  
  4. if north == nil then north = 1 end
  5. if east == nil then east = 2 end
  6. if south == nil then south = 3 end
  7. if west == nil then west = 4 end
  8.  
  9.  
  10. if fieldx == nil then fieldx = 9 end
  11. if fieldy == nil then fieldy = 9 end
  12.  
  13. if wait == nil then wait = 4200 end
  14. if waitafter == nil then waitafter = true end
  15.  
  16.  
  17.  
  18. function refuel()
  19.   if turtle.getFuelLevel() < 324 then
  20.     turtle.select(15)
  21.     turtle.refuel()
  22.     turtle.select(1)
  23.   end
  24. end
  25.  
  26. if direction == nil then direction = 0 end
  27.  
  28. function turn(newdirection)
  29.     if newdirection==direction then
  30.         return true
  31.     else
  32.         while not (direction==newdirection)
  33.         if newdirection > direction then
  34.             turtle.turnLeft((newdirection - direction))
  35.             direction = direction + 1
  36.         else turtle.turnRight((newdirection - direction))
  37.             direction = direction + 1
  38.         end
  39.     end
  40. end
  41.  
  42. function coord()
  43. print("Position: "..posx.." / "..posy)
  44. end
  45.  
  46. function iposx()
  47. posx = posx + 1
  48. end
  49.  
  50. function iposy()
  51. posy = posy + 1
  52. end
  53.  
  54. function dposx()
  55. posx = posx - 1
  56. end
  57.  
  58. function dposy()
  59. posy = posy - 1
  60. end
  61.  
  62. function mine()
  63.     print("doing stuff here")
  64.     turtle.select(1)
  65.     turtle.placeDown()
  66.     turtle.digDown()
  67.    
  68.     for i=14,5,-1 do
  69.       turtle.select(i)
  70.       turtle.placeDown()
  71.     end
  72.     turtle.select(1)
  73. end
  74.  
  75. function move(dx, dy)
  76. --coord()
  77.   if dy >= posy then
  78.     moveDirY(posy, dy)
  79.     moveDirX(posx, dx)
  80.   else
  81.     moveDirX(posx, dx)
  82.     moveDirY(posy, dy)
  83.   end
  84.   coord()
  85. end
  86.  
  87. function moveDirX(ax, dx)
  88.     if ax==dx then return true end
  89.     if ax == 0 then step(dx, 0) else
  90.         delta = dx - ax
  91.         return step(delta, 0)
  92.     end
  93. end
  94.  
  95. function moveDirY(ay, dy)
  96.     if not (ay==dy) then
  97.         if ay == 0 then step(dy, 1) else
  98.             delta = dy - ay
  99.             return step(delta, 1)
  100.         end
  101.     else
  102.         return true
  103.     end
  104. end
  105.  
  106. function betrag(zahl1)
  107.     if zahl1 < 0 then return zahl1 * -1
  108.     else return zahl1
  109.     end
  110. end
  111.  
  112. function step(count, axis)
  113.     refuel()
  114.     if axis == 0 then
  115.         if count < 0 then
  116.             return moveLeft(betrag(count))
  117.         else
  118.             return moveRight(betrag(count))
  119.         end
  120.     else
  121.         if count > 0 then
  122.             return moveForward(betrag(count))
  123.         else
  124.             return moveBack(betrag(count))
  125.         end
  126.     end
  127.    
  128. end
  129.  
  130. function problem()
  131.     if turtle.getFuelLevel() < (fieldx*fieldy) and turtle.getItemCount(16) == 0 then
  132.         print("not enough fuel")
  133.         return true
  134.     end
  135.     if turtle.detect() then
  136.         print("Uups!")
  137.         return false
  138.     end
  139.     return false
  140. end
  141.  
  142. function detect()
  143.     if turtle.detect() then
  144.         return true
  145.     else
  146.         return false
  147. end
  148.  
  149. function moveRight(ci)
  150.     if problem() then return false end
  151.  
  152.     refuel()
  153.     turtle.turnRight(1)
  154.         for i=1, ci do
  155.             print("Moving right. New: "..(posx+1).." / "..posy)
  156.             if not turtle.detect() then
  157.                 turtle.forward()
  158.                 iposx()
  159.             else print("Resetting X-Axis to "..(fieldx+1))
  160.                  posx=fieldx+1
  161.             turn180()
  162.             return false
  163.             end
  164.         end
  165.     turtle.turnLeft(1)
  166. end
  167.  
  168. function moveLeft(ci)
  169.     if problem() then return false end
  170.  
  171.     refuel()
  172.     turtle.turnLeft(1)
  173.         for i=1, ci do
  174.             print("Moving left. New: "..(posx-1).." / "..posy)
  175.             if not turtle.detect() then
  176.                 turtle.forward()
  177.                 dposx()
  178.             else print("Resetting X-Axis to 0")
  179.                  posx=0
  180.             turn180()
  181.             return false
  182.             end
  183.         end
  184.     turtle.turnRight(1)
  185. end
  186.  
  187. function moveForward(ci)
  188.     if problem() then return false end
  189.  
  190.     refuel()
  191.     for i=1, ci do
  192.         print("Moving forward. New: "..posx.." / "..(posy+1))
  193.         if not turtle.detect() then
  194.             turtle.forward()
  195.             iposy()
  196.         else print("Resetting Y-Axis to "..(fieldy+1))
  197.              posy=(fieldy+1)
  198.             turn180()
  199.             return false
  200.         end
  201.     end
  202. end
  203.  
  204. function moveBack(ci)
  205.     if problem() then return false end
  206.  
  207.     refuel()
  208.     turn180()
  209.     for i=1, ci do
  210.         print("Moving back. New: "..posx.." / "..(posy-1))
  211.         if not turtle.detect() then
  212.             turtle.forward()
  213.             dposy()
  214.         else print("Resetting Y-Axis to 0 and stopping.")
  215.              posy=0
  216.             turn180()
  217.             return false
  218.         end
  219.     end
  220.     turn180()
  221.     return true
  222. end
  223.  
  224. function turn180()
  225.     turtle.turnRight()
  226.     turtle.turnRight()
  227. end
  228.  
  229. function moveup(uct)
  230.     for i=1, uct do
  231.       turtle.up(1)
  232.     end
  233. end
  234.  
  235. function movedown(dct)
  236.     for i=1, dct do
  237.       turtle.down(1)
  238.     end
  239. end
  240.  
  241. function homing()
  242.     print("Assuming crash. Homing...")
  243.     moveLeft((fieldx+1))
  244.    
  245.     if detect() then posx=0
  246.     else moveLeft(1) end
  247.     moveBack((fieldy+1))
  248.    
  249.     if detect() then posx=0
  250.     else moveLeft(1) end
  251. end
  252.  
  253. function loop1()
  254. print("Initializing Harvester GPS alpha 0.905")
  255. while turtle.getItemCount(15) >= 1 do
  256. for f=1, 8 do
  257.  
  258.   for i=1, 9 do
  259.     for j=1, 9 do
  260.       if crashcount > 4 then homing() end
  261.       if not ((i==5) and (j==5)) then
  262.         move(i,j)
  263.         mine()
  264.        
  265.       else
  266.         move(i,j)
  267.       end
  268.     end
  269.     refuel()
  270.   end
  271.   move(1,0)
  272.   movedown(4)
  273. end
  274. return true
  275. end
  276.  
  277. while true do
  278.  
  279. if not waitafter then
  280.     print("Sleep before farming...")
  281. else
  282.     print("\\")
  283.     print("Getting shit done.")
  284. end
  285.  
  286.  
  287. if not waitafter then
  288.     sleep(wait)
  289. end
  290.  
  291. while(loop1()) end
  292.  
  293. homing()
  294.  
  295. end
  296.  
  297. if waitafter then sleep(wait) print("Sleeping after...") end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement