Advertisement
Guest User

startup

a guest
Oct 25th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.93 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. if crashcount == nil then crashcount = 0 end
  10.  
  11. if fieldx == nil then fieldx = 9 end
  12. if fieldy == nil then fieldy = 9 end
  13.  
  14. if wait == nil then wait = 4200 end
  15. if waitafter == nil then waitafter = true end
  16.  
  17.  
  18.  
  19. function refuel()
  20.   if turtle.getFuelLevel() < 324 then
  21.     turtle.select(15)
  22.     turtle.refuel()
  23.     turtle.select(1)
  24.   end
  25. end
  26.  
  27. if direction == nil then direction = 0 end
  28.  
  29. function turn(newdirection)
  30.     if newdirection==direction then
  31.         return true
  32.     else
  33.         while not (direction==newdirection) do
  34.           if newdirection > direction then
  35.               turtle.turnLeft((newdirection - direction))
  36.               direction = direction + 1
  37.           else turtle.turnRight((newdirection - direction))
  38.               direction = direction + 1
  39.           end
  40.         end
  41.     end
  42. end
  43.  
  44. function coord()
  45. print("Position: "..posx.." / "..posy)
  46. end
  47.  
  48. function iposx()
  49. posx = posx + 1
  50. end
  51.  
  52. function iposy()
  53. posy = posy + 1
  54. end
  55.  
  56. function dposx()
  57. posx = posx - 1
  58. end
  59.  
  60. function dposy()
  61. posy = posy - 1
  62. end
  63.  
  64. function mine()
  65.     print("doing stuff here")
  66.     turtle.select(1)
  67.     turtle.placeDown()
  68.     turtle.digDown()
  69.    
  70.     for i=14,9,-1 do
  71.       turtle.select(i)
  72.       turtle.placeDown()
  73.     end
  74.     turtle.select(1)
  75. end
  76.  
  77. function move(dx, dy)
  78. --coord()
  79.   retr = nil
  80.   if dy >= posy then
  81.     retr = (moveDirY(posy, dy) and moveDirX(posx, dx))
  82.   else
  83.     retr = (moveDirX(posx, dx) and moveDirY(posy, dy))
  84.   end
  85.  
  86.   --print("move() :: "..tostring(retr).." ::")
  87. return retr  
  88.   --coord()
  89. end
  90.  
  91. function moveDirX(ax, dx)
  92.     if ax==dx then return (not detect()) end
  93.     if ax == 0 then return step(dx,0) else
  94.         delta = dx - ax
  95.         --print("dX: "..tostring(delta))
  96.         return step(delta, 0)
  97.     end
  98. end
  99.  
  100. function moveDirY(ay, dy)
  101.     if (ay==dy) then return (not detect())
  102.     else
  103.         if ay == 0 then return step(dy, 1)
  104.           else
  105.             delta = dy - ay
  106.             --print("dY: "..tostring(delta))
  107.             return step(delta, 1)
  108.         end
  109.     end
  110. end
  111.  
  112. function betrag(zahl1)
  113.     if zahl1 < 0 then return zahl1 * -1
  114.     else return zahl1
  115.     end
  116. end
  117.  
  118. function step(count, axis)
  119.     refuel()
  120.     retr = nil
  121.     if axis == 0 then
  122.         if count < 0 then
  123.             retr = moveLeft(betrag(count))
  124.         else
  125.             retr = moveRight(betrag(count))
  126.         end
  127.     else
  128.         if count > 0 then
  129.             retr = moveForward(betrag(count))
  130.         else
  131.             retr = moveBack(betrag(count))
  132.         end
  133.     end
  134.  
  135. --print("step() :: "..tostring(retr).." ::")
  136. --sleep(3)
  137. return retr    
  138. end
  139.  
  140. function problem()
  141.     if turtle.getFuelLevel() < (fieldx*fieldy) and turtle.getItemCount(16) == 0 then
  142.         print("not enough fuel. Homing!")
  143.         return true
  144.     else
  145.       return false
  146.     end
  147. end
  148.  
  149. function detect()
  150.     if turtle.detect() then
  151.         crashcount = crashcount + 1
  152.         return true
  153.     else
  154.         return false
  155.     end
  156. end
  157.  
  158. function moveRight(ci)
  159.     if problem() then return false end
  160.  
  161.     refuel()
  162.     turtle.turnRight(1)
  163.         for i=1, ci do
  164.             print("Moving right. New: "..(posx+1).." / "..posy)
  165.             if not detect() and turtle.forward() then
  166.                 iposx()
  167.             else
  168.               return false
  169.             end
  170.         end
  171.     turtle.turnLeft(1)
  172.     return true
  173. end
  174.  
  175. function moveLeft(ci)
  176.     if problem() then return false end
  177.  
  178.     refuel()
  179.     turtle.turnLeft(1)
  180.         for i=1, ci do
  181.             print("Moving left. New: "..(posx-1).." / "..posy)
  182.             if not detect() and turtle.forward() then
  183.               dposx()
  184.             else
  185.               return false
  186.          
  187.             end
  188.         end
  189.     turtle.turnRight(1)
  190.     return true
  191. end
  192.  
  193. function moveForward(ci)
  194.     if problem() then return false end
  195.  
  196.     refuel()
  197.     for i=1, ci do
  198.         print("Moving forward. New: "..posx.." / "..(posy+1))
  199.         if not detect() and turtle.forward() then
  200.             iposy()
  201.         else return false
  202.         end
  203.     end
  204.     return true
  205. end
  206.  
  207. function moveBack(ci)
  208.     if problem() then return false end
  209.  
  210.     refuel()
  211.     turn180()
  212.     for i=1, ci do
  213.         print("Moving back. New: "..posx.." / "..(posy-1))
  214.         if not detect() and turtle.forward() then
  215.             dposy()
  216.         else return false
  217.         end
  218.     end
  219.    
  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.     turtle.select(15)
  244.     while not turtle.compareDown() do
  245.       if not turtle.forward(1) then
  246.       turtle.turnLeft(1)
  247.      
  248.       end
  249.      
  250.     end
  251.     turtle.turnLeft(1)
  252.     turtle.forward(1)
  253.     turtle.turnLeft(1)  
  254.     posx=1
  255.     posy=0
  256.     direction = 0
  257.     crashcount= -1
  258.     print("Sending -true- to caller")
  259.     return true
  260.    
  261. end
  262.  
  263. function unlocked()
  264.   if turtle.getItemCount(15) > 0 then
  265.     return true
  266.   else
  267.     return false
  268.   end
  269. end
  270.  
  271. function empty()
  272.   turtle.turnLeft(1)
  273.   turtle.forward()
  274.   turtle.select(15)
  275.   if turtle.compareDown() then
  276.     for i=14,9,-1 do
  277.       turtle.dropDown()
  278.     end
  279.   else return false
  280.   end
  281.   turtle.back()
  282.   turtle.select(1)
  283.   turtle.turnRight(1)
  284. end
  285.  
  286. function restock()
  287.   turtle.turnRight(1)
  288.   turtle.forward()
  289.   turtle.down(1)
  290.   turtle.select(15)
  291.   if turtle.compareDown() then
  292.     turtle.select(16)
  293.     turtle.suckDown()
  294.     turtle.select(1)
  295.     turtle.up()
  296.     turtle.back()
  297.     turtle.turnLeft(1)
  298.     return true
  299.   else return false
  300.   end
  301.  
  302. end
  303.  
  304. function loop1()
  305. print("Initializing Harvester GPS alpha 1.101")
  306. sleep(3)
  307. while unlocked() do
  308. for f=1, 8 do
  309.  
  310.   for i=1, fieldx do
  311.     for j=1, fieldy do
  312.       --if crashcount > 4 then homing() end
  313.       if not ((i==5) and (j==5)) then
  314.         --move(i,j)
  315.         if not move(i,j) then return false end
  316.         mine()
  317.       else
  318.         if not move(i,j) then return false end
  319.       end
  320.     end
  321.     refuel()
  322.   end
  323.   move(1,0)
  324.   empty()
  325.   moveup(4)
  326. end
  327. return true
  328. end
  329.  
  330. end
  331.  
  332.  
  333. while true do
  334.  
  335. if not waitafter then
  336.     print("Sleep before farming...")
  337. else
  338.     print("\\")
  339.     print("Getting shit done.")
  340. end
  341.  
  342.  
  343. if not waitafter then
  344.     sleep(wait)
  345. end
  346.  
  347. while(loop1()) do end
  348.  
  349. while not homing() do
  350.  if not unlocked() then
  351.   return false
  352.  end
  353.  
  354.  end
  355. print("Homing done.")
  356. print("Restarting Harvester...")
  357.  
  358. empty()
  359. restock()
  360.  
  361. if waitafter and (crashcount >= 0) then
  362.  print("Sleeping after...") sleep(wait)
  363.  else print("sleep is for the poor." )
  364.  crashcount = 0
  365.  end
  366.  
  367. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement