Advertisement
Guest User

startup

a guest
Nov 23rd, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.56 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(16)
  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=12,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.       if turtle.up() then iposz(1)
  232.       else storystep = -1 return false end
  233.     end
  234.     return true
  235. end
  236.  
  237. function movedown(dct)
  238.     for i=1, dct do
  239.       if turtle.down(1) then dposz(1)
  240.       else storystep = 1 return false end
  241.     end
  242. return true
  243. end
  244.  
  245. function homing()
  246.     print("Assuming crash. Homing...")
  247.     turtle.select(15)
  248.     while not turtle.compareDown() do
  249.       if not turtle.forward(1) then
  250.       turtle.turnLeft(1)
  251.      
  252.       end
  253.      
  254.     end
  255.     turtle.turnLeft(1)
  256.     turtle.forward(1)
  257.     turtle.turnLeft(1)  
  258.     posx=1
  259.     posy=0
  260.     direction = 0
  261.     crashcount= -1
  262.     print("Sending -true- to caller")
  263.     return true
  264.    
  265. end
  266.  
  267. function unlocked()
  268.   if turtle.getItemCount(15) > 0 then
  269.     return true
  270.   else
  271.     return false
  272.   end
  273.  
  274. end
  275.  
  276. function empty()
  277.   turtle.turnLeft(1)
  278.   turtle.forward(1)
  279.   turtle.turnRight(1)
  280.  
  281.   turtle.select(15)
  282.   checked = turtle.compareDown()
  283.   if checked then
  284.     turtle.select(16)
  285.  
  286.    
  287.     for fi=12,9,-1 do
  288.     turtle.select(fi)
  289.       turtle.dropDown()
  290.     end
  291.    
  292.    
  293.     turtle.turnRight(1)
  294.     turtle.forward(1)
  295.     turtle.turnLeft(1)
  296.    
  297.     turtle.select(1)
  298.     return checked
  299.    
  300.   else
  301.  
  302.   turtle.turnRight(1)
  303.   turtle.forward(1)
  304.   turtle.select(1)
  305.   turtle.turnLeft(1)
  306.   return checked
  307.   end
  308. end
  309.  
  310. function restock()
  311.   turtle.turnRight(1)
  312.   turtle.forward(1)
  313.   turtle.turnLeft(1)
  314.  
  315.   if not turtle.down(1) then return false end
  316.   turtle.select(15)
  317.   if turtle.compareDown() then
  318.     turtle.select(16)
  319.     --Loop 1k range for each lava bucket
  320.     tkm = 4
  321.     turtle.turnLeft()
  322.     turtle.turnLeft()
  323.     turtle.forward()
  324.     g=0
  325.     while g <= (tkm-1) do
  326.      
  327.       --turtle.select(1)
  328.       --if not turtle.suckDown() then return false end
  329.       turtle.select(16)
  330.       turtle.placeDown()
  331.       if turtle.refuel() then g = g + 1 end
  332.     end
  333.     turtle.placeDown()
  334.     turtle.turnLeft()
  335.     turtle.turnLeft()
  336.     turtle.forward()
  337.     --turtle.select(16)
  338.     --turtle.refuel()
  339.     for i=12,9,-1 do
  340.       turtle.select(i)
  341.       turtle.refuel()
  342.     end
  343.    
  344.     turtle.up(1)
  345.    
  346.     turtle.turnLeft(1)
  347.     turtle.forward(1)
  348.     turtle.turnRight(1)
  349.     --[[ print("Emptying? ->>"..tostring(empty()))
  350.     ]]--
  351.     turtle.select(1)
  352.     return true
  353.   else
  354.     turtle.up(1)
  355.     turtle.turnLeft(1)
  356.     turtle.forward(1)
  357.     turtle.turnRight(1)
  358.     empty()
  359.     turtle.select(1)
  360.     return false
  361.   end
  362.  
  363. end
  364.  
  365. --Here goes the Z-Story Management /w CD
  366. story = 1
  367. stories = 8
  368. sheight = 4
  369. storystep = 1
  370. posz = story * 1
  371. zNegBoundary = 0
  372. zPosBoundary = 31
  373.  
  374. function iposz(i)
  375.   if (posz+i) <= zPosBoundary then
  376.     posz = posz+i
  377.   else
  378.     posz = zPosBoundary
  379.     storystep = -1
  380.   end
  381. end
  382.  
  383. function dposz(i)
  384.   if (posz-i) >= zNegBoundary then
  385.     posz = posz-i
  386.   else
  387.     posz = zNegBoundary
  388.     storystep = 1
  389.   end
  390. end
  391.  
  392. function zChangePos(cnt,stStep)
  393.   if stStep >= 0 then
  394.     succ = moveup(cnt)
  395.     print("Moveup: "..tostring(succ).." . "..tostring(cnt))
  396.     return succ
  397.     else
  398.     succ = movedown(stories*sheight)
  399.     print("Movedown: "..tostring(succ).." . "..tostring(cnt))
  400.     return succ
  401.     end
  402.  
  403. end
  404.  
  405. function loop1()
  406. print("Your personal farm beast.")
  407. print("Initializing GPS alpha 1.12")
  408. sleep(3)
  409. print("Empty? ->>"..tostring(empty()))
  410.  
  411. while unlocked() do
  412.  
  413. for d=1,stories do
  414.    turtle.turnLeft()
  415.    turtle.turnLeft()
  416.    turtle.suck()
  417.    turtle.suck()
  418.    turtle.turnLeft()
  419.    turtle.turnLeft()
  420.    for i=1, fieldx do
  421.     for j=1, fieldy do
  422.       --if crashcount > 4 then homing() end
  423.       if ((i==5) and (j==5)) then
  424.        
  425.         if not move(i,j) then return false end
  426.        
  427.       else
  428.         if not move(i,j) then return false end
  429.         mine()
  430.       end
  431.     end
  432.     refuel()
  433.   end
  434.  
  435.   --posx=1
  436.   --posy=0
  437.   move(1,0)
  438.   if not empty() then print("chest not found") return false end
  439.   restock()
  440.   if not empty() then print("chest not found") return false end
  441.   storystep = 1
  442.   zAssert = zChangePos(sheight,storystep)
  443.   print("zAssert: "..tostring(zAssert))
  444.   if not zAssert then
  445.     movedown((sheight*stories)+1)
  446.   else
  447.    
  448.     iposz(sheight)
  449.   end
  450. end
  451. end
  452.  
  453. end
  454.  
  455.  
  456. while true do
  457.  
  458. if not waitafter then
  459.     print("Sleep before farming...")
  460. else
  461.     print("\\")
  462.     --print("Getting shit done.")
  463. end
  464.  
  465.  
  466. if not waitafter then
  467.     sleep(wait)
  468. end
  469.  
  470. while(loop1()) do end
  471. print("loop1 just got killed")
  472. while not homing() do
  473.  if not unlocked() then
  474.   return false
  475.  end
  476.  
  477.  end
  478. print("Homing done.")
  479. print("Restarting Harvester...")
  480.  
  481. if restock() then storystep = 1 empty() zChangePos(4,1)
  482. else storystep = -1 crashcount = -1 move(1,0) empty() zChangePos(4,1) end
  483.  
  484. if waitafter and (crashcount >= 0) then
  485.  print("Sleeping after...") sleep(wait)
  486.  else print("sleep is for the poor." )
  487.  crashcount = 0
  488.  storystep = 1
  489.  end
  490.  
  491. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement