Advertisement
Guest User

sugar

a guest
Sep 9th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.80 KB | None | 0 0
  1. local richtung = 0
  2.  
  3. function forceForward()
  4.   while not turtle.forward() do
  5.     turtle.dig()
  6.   end
  7. end
  8.  
  9. function saveState(mode)
  10.   local f = io.open("sugar.sav", "w")
  11.   f:write(tostring(mode).."\n")
  12.   f:write(tostring(richtung))
  13.   f:close()
  14. end
  15.  
  16. function init()
  17.   local f = fs.open("sugar.sav", "r")
  18.  
  19.   local mode = f:readLine()
  20.   local tmpRichtung = f:readLine()
  21.  
  22.   write(mode)
  23.   write(tmpRichtung)
  24.  
  25.   f:close()
  26.    
  27.   if (mode) and (tonumber(mode) == 1) then
  28.     returnToStart()    
  29.   else
  30.     if (tmpRichtung) then
  31.       richtung = tonumber(tmpRichtung)
  32.     end
  33.    
  34.     turtle.select(16)
  35.     if not turtle.compareDown() then
  36.       turtle.back()
  37.     end
  38.   end
  39.  
  40.   saveState(0)
  41. end
  42.  
  43. function waitForRefuel()
  44.   while turtle.getFuelLevel() < 1000 do
  45.     sleep(0.5)
  46.   end
  47. end
  48.  
  49. function turn()
  50.   if richtung % 4 < 2 then
  51.     turtle.turnRight()
  52.   elseif richtung % 4 <= 3 then
  53.     turtle.turnLeft()
  54.   end
  55.  
  56.   if richtung < 3 then
  57.     richtung = richtung + 1
  58.   else
  59.     richtung = 0
  60.   end
  61.  
  62.   saveState(0)
  63. end
  64.  
  65. function returnToStart()
  66.   turtle.select(15)
  67.   while not turtle.compareDown() do
  68.     forceForward()
  69.   end
  70.  
  71.   for i=1, 14, 1 do
  72.     turtle.select(i)
  73.     turtle.drop()
  74.   end
  75.  
  76.   turtle.turnRight()
  77.   waitForRefuel()
  78.   richtung = 0
  79. end
  80.  
  81. function go()
  82.   turtle.select(15)
  83.   if (turtle.compare()) then
  84.     turtle.turnRight()
  85.     saveState(1)
  86.     returnToStart()
  87.     saveState(0)
  88.   end
  89.  
  90.   turtle.select(16)
  91.   if (turtle.compare()) or not (turtle.detect()) then
  92.     turtle.dig()
  93.    
  94.     if (turtle.forward()) then
  95.       if not turtle.compareDown() then
  96.         turtle.back()
  97.         turn()
  98.         go()
  99.       end
  100.     else
  101.       turn()
  102.       go()
  103.     end
  104.   else
  105.     turn()
  106.     go()
  107.   end
  108. end
  109.  
  110. init()
  111. while true do
  112.   go()
  113. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement