Advertisement
einsteinK

CCraft - Vortex (API)

Aug 30th, 2014
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. layer = vortex and vortex.layer or 100
  2.  
  3. function locate()
  4.   local x,y,z = gps.locate()
  5.   if not x then
  6.     error("Can't get location",0)
  7.   end
  8.   if X and (x ~= X or y ~= Y or z ~= Z) then
  9.     error("GPS desync",0)
  10.   end return x,y,z
  11. end
  12.  
  13. local function zeroAssert(suc,err)
  14.     if not suc then
  15.         error(err,2)
  16.     end
  17. end
  18.  
  19. local function gotoLayer(layer)
  20.   local x,y,z = locate()
  21.   if y < layer then
  22.     for i=1,layer-y do
  23.       zeroAssert(turtle.up())
  24.     end
  25.   else
  26.     for i=1,y-layer do
  27.       zeroAssert(turtle.down())
  28.     end
  29.   end
  30. end
  31.  
  32. if vortex then
  33.     vortex.rotate(0)
  34. else
  35.   local ok
  36.   for i=1,4 do
  37.     if turtle.forward() then
  38.       turtle.turnLeft()
  39.       turtle.turnLeft()
  40.       ok = true break
  41.     else
  42.       turtle.turnLeft()
  43.     end
  44.   end
  45.   if not ok then
  46.     error("Can't determine rotation",0)
  47.   end
  48.   local pos = {locate()}
  49.   turtle.forward()
  50.   for k,v in pairs{locate()} do
  51.     pos[k] = v - pos[k]
  52.   end
  53.   if pos[1] == 0 then
  54.     if pos[3] == -1 then
  55.       -- Facing north
  56.     else
  57.       -- Facing south
  58.       turtle.turnLeft()
  59.       turtle.turnLeft()
  60.     end
  61.   elseif pos[1] == 1 then
  62.     -- Facing east
  63.     turtle.turnLeft()
  64.   else
  65.     -- Facing west
  66.     turtle.turnRight()
  67.   end
  68. end
  69.  
  70. ro = vortex and vortex.ro or 0
  71. function rotate(n)
  72.   ro,n = ro%4,n%4
  73.   if ro < 0 then
  74.     ro = ro + 4
  75.   end
  76.   if math.abs(ro-n-4) <
  77.     math.abs(ro-n) then
  78.     ro = ro - 4
  79.   end
  80.   while ro < n do
  81.     turtle.turnLeft()
  82.   end
  83.   while ro > n do
  84.     turtle.turnRight()
  85.   end ro = (ro + 4)%4
  86. end
  87.  
  88. local old = vortex and vortex.old or {
  89.     turnLeft = turtle.turnLeft;
  90.     turnRight = turtle.turnRight;
  91.     forward = turtle.forward;
  92.     back = turtle.back;
  93.     down = turtle.down;
  94.     up = turtle.up;
  95. }
  96.  
  97. boundsFunc = vortex and vortex.boundsFunc or function(x,y,z)
  98.     if x > 1111 then return false end
  99.     if x < 1073 then return false end
  100.     if z > 1703 then return false end
  101.     if z < 1649 then return false end
  102.     if z < 1665 and x > 1086 then
  103.         return false
  104.     end return y < 101 and y > 76
  105. end
  106.  
  107. X,Y,Z = gps.locate()
  108.  
  109. function turtle.turnLeft()
  110.     old.turnLeft() ro = (ro + 1)%4
  111. end
  112. function turtle.turnRight()
  113.     old.turnRight() ro = (ro + 3)%4
  114. end
  115. function turtle.forward()
  116.     local x,z = X,Z
  117.     if ro == 0 then
  118.         z = z - 1
  119.     elseif ro == 1 then
  120.         x = x - 1
  121.     elseif ro == 2 then
  122.         z = z + 1
  123.     elseif ro == 3 then
  124.         x = x + 1
  125.     end
  126.     if not boundsFunc(x,Y,z) then
  127.         return false,"Outside bounds"
  128.     elseif not old.forward() then
  129.         return false,"Can't move forward"
  130.     end X,Z = x,z return true
  131. end
  132. function turtle.back()
  133.     local x,z = X,Z
  134.     if ro == 0 then
  135.         z = z + 1
  136.     elseif ro == 1 then
  137.         x = x + 1
  138.     elseif ro == 2 then
  139.         z = z - 1
  140.     elseif ro == 3 then
  141.         x = x - 1
  142.     end
  143.     if not boundsFunc(x,Y,z) then
  144.         return false,"Outside bounds"
  145.     elseif not old.back() then
  146.         return false,"Can't move back"
  147.     end X,Z = x,z return true
  148. end
  149. function turtle.up()
  150.     if not boundsFunc(X,Y+1,Z) then
  151.         return false,"Outside bounds"
  152.     elseif not old.up() then
  153.         return false,"Can't move up"
  154.     end Y = Y + 1 return true
  155. end
  156. function turtle.down()
  157.     if not boundsFunc(X,Y-1,Z) then
  158.         return false,"Outside bounds"
  159.     elseif not old.down() then
  160.         return false,"Can't move down"
  161.     end Y = Y - 1 return true
  162. end
  163.  
  164. local function succeedOrUp(f)
  165.     local s,e  = f()
  166.     while not s do
  167.         if e == "Outside bounds" then
  168.             error("Has to move outside bounds",0)
  169.         end zeroAssert(turtle.up())
  170.         s,e = f()
  171.     end
  172. end
  173.  
  174. function goto(X,Y,Z,l)
  175.     --gotoLayer(l or Y)
  176.     local x,y,z = vortex.locate()
  177.     x,y,z = x-X,y-Y,z-Z
  178.     rotate(z < 0 and 2 or 0)
  179.     for i=1,math.abs(z) do
  180.       succeedOrUp(turtle.forward)
  181.     end
  182.     rotate(x > 0 and 1 or -1)
  183.     for i=1,math.abs(x) do
  184.       succeedOrUp(turtle.forward)
  185.     end gotoLayer(Y)
  186. end
  187.  
  188. function goto_old(X,Y,Z,l)
  189.     gotoLayer(l == true and Y or l or layer)
  190.     local x,y,z = vortex.locate()
  191.     x,y,z = x-X,y-Y,z-Z
  192.     rotate(z < 0 and 2 or 0)
  193.     for i=1,math.abs(z) do
  194.       zeroAssert(turtle.forward())
  195.     end
  196.     rotate(x > 0 and 1 or -1)
  197.     for i=1,math.abs(x) do
  198.       zeroAssert(turtle.forward())
  199.     end
  200.     if y > 0 then
  201.       for i=1,y do
  202.         zeroAssert(turtle.down())
  203.       end
  204.     else
  205.       for i=1,-y do
  206.         zeroAssert(turtle.up())
  207.       end
  208.     end
  209. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement