Advertisement
Umaler

LocalMover

Jan 16th, 2025 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | Gaming | 0 0
  1. turtle = require("turtle")
  2.  
  3. --[[
  4.     A little wrapper for ComputerCraft turtle.
  5.     Intended for counting local coordinates.
  6. ]]--
  7.  
  8. local position = {
  9.     x = 0,
  10.     y = 0,
  11.     z = 0
  12. }
  13.  
  14. local direction = "forward"
  15.  
  16. local resultingTable = {
  17.  
  18.     moveUp = function()
  19.         code, msg = turtle.up()
  20.         if code then
  21.             position.y = position.y + 1
  22.         end
  23.         return code, msg
  24.     end,
  25.  
  26.     moveDown = function()
  27.         code, msg = turtle.down()
  28.         if code then
  29.             position.y = position.y - 1
  30.         end
  31.         return code, msg
  32.     end,
  33.  
  34.     moveForward = function()
  35.         code, msg = turtle.forward()
  36.         if code then
  37.             position.y = position.x + 1
  38.         end
  39.         return code, msg
  40.     end,
  41.  
  42.     moveBack = function()
  43.         code, msg = turtle.back()
  44.         if code then
  45.             position.y = position.x - 1
  46.         end
  47.         return code, msg
  48.     end,
  49.  
  50.     moveRight = function()
  51.         code, msg = turtle.turnRight()
  52.         if not code then
  53.             return code, msg
  54.         end
  55.         direction = "right"
  56.  
  57.         code, msg = turtle.forward()
  58.         if code then
  59.             position.z = position.z + 1
  60.         else
  61.             return code, msg
  62.         end
  63.  
  64.         code, msg = turtle.turnLeft()
  65.         if code then
  66.             direction = "forward"
  67.         end
  68.         return code, msg
  69.     end,
  70.  
  71.     moveLeft = function()
  72.         code, msg = turtle.turnLeft()
  73.         if not code then
  74.             return code, msg
  75.         end
  76.         direction = "left"
  77.  
  78.         code, msg = turtle.forward()
  79.         if code then
  80.             position.z = position.z - 1
  81.         else
  82.             return code, msg
  83.         end
  84.  
  85.         code, msg = turtle.turnLeft()
  86.         if code then
  87.             direction = "left"
  88.         end
  89.         return code, msg
  90.     end,
  91.  
  92.     fixDirection = function()
  93.         if direction == "forward" then
  94.             return true
  95.         elseif direction == "left" then
  96.             code, msg = turtle.turnRight()
  97.             if code then
  98.                 direction = "forward"
  99.             end
  100.             return code, msg
  101.         elseif direction == "right" then
  102.             code, msg = turtle.turnLeft()
  103.             if code then
  104.                 direction = "forward"
  105.             end
  106.             return code, msg
  107.         else
  108.             return false, "Incorrect direction"
  109.         end
  110.     end,
  111.  
  112.     getPosition = function()
  113.         return position
  114.     end,
  115.  
  116.     getDirection = function()
  117.         return direction
  118.     end
  119.  
  120. }
  121.  
  122. return resultingTable
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement