Blackhome

Basic Movement

Feb 4th, 2026 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | Gaming | 0 0
  1.     -- pastebin get wsLBFGgx BasicMovement
  2.  
  3.     CVF = require("CVF")
  4.     POS = require("POS")
  5.  
  6.     BM = {}
  7.     local initData = nil
  8.  
  9.     function BM.forward()
  10.         if POS.direction then
  11.             if turtle.forward() then
  12.                 POS.position = CVF.add(POS.position, POS.direction)
  13.                 return true
  14.             else
  15.                 while not POS.get_GPS_Position() do sleep(1) end
  16.             end
  17.         else
  18.             local oldPos = {x= POS.position.x, y=POS.position.y, z=POS.position.z}
  19.             if turtle.forward() then
  20.                 while not POS.get_GPS_Position() do sleep(1) end
  21.                 POS.direction = {x=POS.position.x-oldPos.x, z=POS.position.z-oldPos.z}
  22.                 return true
  23.             else
  24.                 while not POS.get_GPS_Position() do sleep(1) end
  25.             end
  26.         end
  27.         return false
  28.     end
  29.     function BM.up()
  30.         if turtle.up() then
  31.             POS.position.y = POS.position.y + 1
  32.             return true
  33.         else
  34.             while not POS.get_GPS_Position() do sleep(1) end
  35.         end
  36.         return false
  37.     end
  38.     function BM.down()
  39.         if turtle.down() then
  40.             POS.position.y = POS.position.y - 1
  41.             return true
  42.         else
  43.             while not POS.get_GPS_Position() do sleep(1) end
  44.         end
  45.         return false
  46.     end
  47.     function BM.back()
  48.         if turtle.back() then
  49.             POS.position = CVF.sub(POS.position, POS.direction)
  50.             return true
  51.         else
  52.             while not POS.get_GPS_Position() do sleep(1) end
  53.         end
  54.         return false
  55.     end
  56.  
  57.     function BM.turnLeft()
  58.         if turtle.turnLeft() then
  59.             if POS.direction then
  60.                 POS.direction = CVF.getLeftDir(POS.direction)
  61.             end
  62.             return true
  63.         end
  64.         return false
  65.     end
  66.     function BM.turnRight()
  67.         if turtle.turnRight() then
  68.             if POS.direction then
  69.                 POS.direction = CVF.getRightDir(POS.direction)
  70.             end
  71.             return true
  72.         end
  73.         return false
  74.     end
  75.     function BM.turnAround()
  76.         return BM.turnRight() and BM.turnRight()
  77.     end
  78.  
  79.     function BM.turnInDirection(endDir)
  80.         if not endDir then return false end
  81.         if not initData then
  82.             initData = SDM.TURTLE.loadInitData()
  83.         end
  84.         local dirList = {
  85.                 ["LEFT"]        = initData.dirBottomBelt,
  86.                 ["RIGHT"]       = initData.dirTopBelt,
  87.                 ["FORWARD"]     = initData.dirSection,
  88.                 ["BACKWARD"]    = CVF.mult(-1, initData.dirSection)
  89.             }
  90.         if type(endDir) == "string" then
  91.             endDir = dirList[endDir]
  92.         end
  93.         local startDir = POS.direction
  94.  
  95.         local strTurnDir = CVF.getTurnDirection(startDir, endDir)
  96.         if not strTurnDir then return true end
  97.  
  98.         local turnList = {
  99.             ["left"]        = BM.turnLeft,
  100.             ["right"]       = BM.turnRight,
  101.             ["around"]    = BM.turnAround
  102.         }
  103.         return turnList[strTurnDir]()
  104.     end
  105.  
  106.     return BM
Advertisement
Add Comment
Please, Sign In to add comment