Advertisement
guitarplayer616

api2

May 21st, 2017
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 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 home()
  99.   returning = true
  100.   update("returning",true)
  101.   while y ~= 0 do
  102.     if y < 0 then
  103.       turtle.digUp()
  104.       up()
  105.     elseif y > 0 then
  106.       turtle.digDown()
  107.       down()
  108.     end
  109.   end
  110.   if z > 0 then
  111.     while f ~= 0 do
  112.       left()
  113.     end
  114.   elseif z < 0 then
  115.     while f ~= 2 do
  116.       left()
  117.     end
  118.   end
  119.   while z ~= 0 do
  120.     forward()
  121.   end
  122.   if x > 0 then
  123.     while f ~= 3 do
  124.       left()
  125.     end
  126.   elseif x < 0 then
  127.     while f ~= 1 do
  128.       left()
  129.     end
  130.   end
  131.   while x ~= 0 do
  132.     forward()
  133.   end
  134. end
  135.  
  136.  
  137. function checkFuel(flag)
  138.   if not flag then
  139.     local fuel = turtle.getFuelLevel()
  140.     local distance = math.abs(x) + math.abs(y) + math.abs(z)
  141.     if fuel-1 <= distance then
  142.       home()
  143.       error("Alert: Fuel level critical. Returning to home point...")
  144.     end
  145.   end
  146. end
  147.  
  148. function update(var,value)
  149.     if not tostring(var) then
  150.         error("var needs to be a string")
  151.     end
  152.     value = tostring(value)
  153.     local str = fs.open("info","r").readAll()
  154.     if str:find(var) then
  155.         str = str:gsub(var.."%=.-%;",var.."="..value..";")
  156.     else
  157.         str = str .."\n"..var.."="..value..";"
  158.     end
  159.     local h = fs.open("info","w")
  160.     h.write(str)
  161.     h.close()
  162. end
  163.  
  164. function log(data)
  165.   local h = fs.open("log", "a")
  166.   if type(data) == "string" then
  167.     h.writeLine(data)
  168.   elseif type(data) == "table" then
  169.     for i = 1, #data do
  170.       h.writeLine(data[i])
  171.     end
  172.   end
  173.   h.close()
  174. end
  175.  
  176. if not fs.exists("info") then
  177.     local h = fs.open("info","w")
  178.     x,y,z,f = 0,0,0,0
  179.     h.writeLine("x=0;")
  180.     h.writeLine("y=0;")
  181.     h.writeLine("z=0;")
  182.     h.writeLine("f=0;")
  183.     h.writeLine("returning=false;")
  184.     h.close()
  185. else
  186.     shell.run("info")
  187. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement