Advertisement
CaptainSpaceCat

Safe Startup

May 21st, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. function forward(num)
  2.   num = num or 1
  3.   for i = 1, num do
  4.     if turtle.forward() then
  5.       if f == 0 then
  6.         z = z - 1
  7.         update("z",z)
  8.       elseif f == 1 then
  9.         x = x + 1
  10.         update("x",x)
  11.       elseif f == 2 then
  12.         z = z + 1
  13.         update("z",z)
  14.       elseif f == 3 then
  15.         x = x - 1
  16.         update("x",x)
  17.       end
  18.       checkFuel(returning)
  19.     else
  20.       return false
  21.     end
  22.   end
  23.   return true
  24. end
  25.  
  26. function back(num)
  27.   num = num or 1
  28.   for i = 1, num do
  29.     if turtle.back() then
  30.       if f == 0 then
  31.         z = z + 1
  32.         update("z",z)
  33.       elseif f == 1 then
  34.         x = x - 1
  35.         update("x",x)
  36.       elseif f == 2 then
  37.         z = z - 1
  38.         update("z",z)
  39.       elseif f == 3 then
  40.         x = x + 1
  41.         update("x",x)
  42.       end
  43.       checkFuel(returning)
  44.     else
  45.       return false
  46.     end
  47.   end
  48.   return true
  49. end
  50.  
  51. function up(num)
  52.   num = num or 1
  53.   for i = 1, num do
  54.     if turtle.up() then
  55.       y = y + 1
  56.       update("y",y)
  57.       checkFuel(returning)
  58.     else
  59.       return false
  60.     end
  61.   end
  62.   return true
  63. end
  64.  
  65. function down(num)
  66.   num = num or 1
  67.   for i = 1, num do
  68.     if turtle.down() then
  69.       y = y - 1
  70.       update("y",y)
  71.       checkFuel(returning)
  72.       return true
  73.     else
  74.       return false
  75.     end
  76.   end
  77.   return true
  78. end
  79.  
  80. function left(num)
  81.   num = num or 1
  82.   for i = 1, num do
  83.     turtle.turnLeft()
  84.     f = (f+3)%4
  85.     update("f",f)
  86.   end
  87. end
  88.  
  89. function right(num)
  90.   num = num or 1
  91.   for i = 1, num do
  92.     turtle.turnRight()
  93.     f = (f+1)%4
  94.     update("f",f)
  95.   end
  96. end
  97.  
  98. function update(var,value)
  99.     if not tostring(var) then
  100.         error("var needs to be a string")
  101.     end
  102.     value = tostring(value)
  103.     local str = fs.open("info","r").readAll()
  104.     if str:find(var) then
  105.         str = str:gsub(var.."%=.-%;",var.."="..value..";")
  106.     else
  107.         str = str .."\n"..var.."="..value..";"
  108.     end
  109.     local h = fs.open("info","w")
  110.     h.write(str)
  111.     h.close()
  112. end
  113.  
  114. --     0 (-z)
  115. -- 3 (-x)  1 (+x)
  116. --     2 (+z)
  117.  
  118. function home()
  119.   returning = true
  120.   while y ~= 0 do
  121.     if y < 0 then
  122.       turtle.digUp()
  123.       up()
  124.     elseif y > 0 then
  125.       turtle.digDown()
  126.       down()
  127.     end
  128.   end
  129.   if z > 0 then
  130.     while f ~= 0 do
  131.       left()
  132.     end
  133.   elseif z < 0 then
  134.     while f ~= 2 do
  135.       left()
  136.     end
  137.   end
  138.   while z ~= 0 do
  139.     forward()
  140.   end
  141.   if x > 0 then
  142.     while f ~= 3 do
  143.       left()
  144.     end
  145.   elseif x < 0 then
  146.     while f ~= 1 do
  147.       left()
  148.     end
  149.   end
  150.   while x ~= 0 do
  151.     forward()
  152.   end
  153. end
  154.  
  155. shell.run("info")
  156. home()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement