Advertisement
CaptainSpaceCat

Safely Wrapped Function Runner

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