Advertisement
DanchiZZ

TurtlePark Moving API

Oct 9th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.18 KB | None | 0 0
  1. -- Moving with coordinates API
  2. North = 0
  3. East = 1
  4. South = 2
  5. West = 3
  6.  
  7. do
  8.   local x, y, z, direction
  9.   local startX, startY, startZ, startDirection
  10.   local xDiff = { 1, 0, -1, 0 }
  11.   local zDiff = { 0, 1, 0, -1 }
  12.   -- Old for default setup
  13.   --local xDiff = { 0, 1, 0, -1 }
  14.   --local zDiff = { -1, 0, 1, 0 }
  15.  
  16.  
  17.   -- Initialize coordinates
  18.   function setup(newX, newY, newZ, newDirection)
  19.     if (newX == nil and newY == nil and newZ == nil and newDirection == nil) then
  20.       local file = fs.open("location", "r")
  21.       x = tonumber(file.readLine())
  22.       y = tonumber(file.readLine())
  23.       z = tonumber(file.readLine())
  24.       direction = tonumber(file.readLine())
  25.       file.close()
  26.       local file = fs.open("startlocation", "r")
  27.       startX = tonumber(file.readLine())
  28.       startY = tonumber(file.readLine())
  29.       startZ = tonumber(file.readLine())
  30.       startDirection = tonumber(file.readLine())
  31.       file.close()
  32.     elseif (newX == nil or newY == nil or newZ == nil or newDirection == nil) then
  33.       logger.error("Need correct coordinates and direction")
  34.     else
  35.       x = 0
  36.       y = 0
  37.       z = 0
  38.       direction = 0
  39.       startX = newX
  40.       startY = newY
  41.       startZ = newZ
  42.       startDirection = newDirection
  43.     end
  44.   end
  45.  
  46.  
  47.   function writeToFile()
  48.     local file = fs.open("location", "w")
  49.     file.writeLine(x)
  50.     file.writeLine(y)
  51.     file.writeLine(z)
  52.     file.writeLine(direction)
  53.     file.flush()
  54.     file.close()
  55.   end
  56.  
  57.  
  58.   function calculatePoint(toX, toY, toZ)
  59.     newX = startX + xDiff[startDirection] * toX
  60.     newY = startY + toY
  61.     newZ = startZ + zDiff[startDirection] * toZ
  62.     newDirection = (direction - startDirection + 4) % 4
  63.     return newX, newY, newZ, newDirection
  64.   end
  65.  
  66.  
  67.   function getLocation()
  68.     return x, y, z, direction;
  69.   end
  70.  
  71.  
  72.   function getRealLocation()
  73.     return calculatePoint(x, y, z, direction);
  74.   end
  75.  
  76.  
  77.   function up()
  78.     if (turtle.up()) then
  79.       y = y + 1
  80.       writeToFile()
  81.       logger.debug("Moved to "..x.." "..y.." "..z)
  82.       return true
  83.     else
  84.       return false
  85.     end
  86.   end
  87.  
  88.   function down()
  89.     if (turtle.down()) then
  90.       y = y - 1
  91.       writeToFile()
  92.       logger.debug("Moved to "..x.." "..y.." "..z)
  93.       return true
  94.     else
  95.       return false
  96.     end
  97.   end
  98.  
  99.   function turnRight()
  100.     if (turtle.turnRight()) then
  101.       direction = direction + 1
  102.       if direction == 4 then
  103.         direction = North
  104.       end
  105.       writeToFile()
  106.       logger.debug("Moved to "..x.." "..y.." "..z)
  107.       return true
  108.     else
  109.       return false
  110.     end
  111.   end
  112.  
  113.   function turnLeft()
  114.     if (turtle.turnLeft()) then
  115.       direction = direction - 1
  116.       if (direction == -1) then
  117.         direction = West
  118.       end
  119.       writeToFile()
  120.       logger.debug("Moved to "..x.." "..y.." "..z)
  121.       return true
  122.     else
  123.       return false
  124.     end
  125.   end
  126.  
  127.   function turn(side)
  128.     while side ~= direction do
  129.       if side - direction == 1 or side - direction == -3 then
  130.         turnRight()
  131.       else
  132.         turnLeft()
  133.       end
  134.     end
  135.   end
  136.  
  137.   function forward()
  138.     if (turtle.forward()) then
  139.       print(xDiff[direction])
  140.       x = x + xDiff[direction + 1]
  141.       z = z + zDiff[direction + 1]
  142.       writeToFile()
  143.       logger.debug("Moved to "..x.." "..y.." "..z)
  144.       return true
  145.     else
  146.       return false
  147.     end
  148.   end
  149.  
  150.   function back()
  151.     if (turtle.back()) then
  152.       x = x - xDiff[direction + 1]
  153.       z = z - zDiff[direction + 1]
  154.       writeToFile()
  155.       logger.debug("Moved to "..x.." "..y.." "..z)
  156.       return true
  157.     else
  158.       return false
  159.     end
  160.   end
  161.  
  162.   function safeMove(toX, toY, toZ)
  163.     while toY > y do
  164.       up()
  165.     end
  166.     while toY < y do
  167.       down()
  168.     end
  169.     if toX > x then
  170.       turn(North)
  171.       while toX > x do
  172.         forward()
  173.       end
  174.     end
  175.     if toX < x then
  176.       turn(South)
  177.       while toX < x do
  178.         forward()
  179.       end
  180.     end
  181.     if toZ > z then
  182.       turn(East)
  183.       while toZ > z do
  184.         forward()
  185.       end
  186.     end
  187.     if toZ < z then
  188.       turn(West)
  189.       while toZ < z do
  190.         forward()
  191.       end
  192.     end
  193.   end
  194. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement