SkyCrafter0

turtlemovement.lua

Nov 29th, 2020
392
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. turtlemovement.lua, my version of lama
  3.  
  4. tData table:
  5. tData[1] is turtle position
  6. {1,2,3,"east"}
  7. ]]
  8.  
  9. local settingsPath = ".tm/settings"
  10.  
  11. if not fs.exists(settingsPath) then
  12.   fs.open(settingsPath,"w")
  13. end
  14.  
  15. local tm = {}
  16.  
  17. local function save(tTbl,sFilepath)
  18.   local file = fs.open(sFilepath,"w")
  19.   file.write(textutils.serialise(tTbl))
  20.   file.close()
  21. end
  22.  
  23. local function load(sFilepath)
  24.   local file = fs.open(sFilepath,"r")
  25.   local data = file.readAll()
  26.   file.close()
  27.   return textutils.unserialise(data)
  28. end
  29.  
  30. local tData = load(settingsPath)
  31.  
  32. local tLoc = tData[1]
  33. local x,y,z,facing = tLoc[1],tLoc[2],tLoc[3],tLoc[4]
  34.  
  35. local function savePos()
  36.   tLoc[1],tLoc[2],tLoc[3],tLoc[4] = x,y,z,facing
  37.   tData[1] = tLoc
  38.   save(tData,settingsPath)
  39. end
  40.  
  41. function tm.forward(nDistance)
  42.   nDistance = nDistance or 1
  43.   for i=1,nDistance do
  44.       if turtle.forward() then
  45.       if facing == "north" then z = z - 1
  46.       elseif facing == "west" then x = x - 1
  47.       elseif facing == "south" then z = z + 1
  48.       elseif facing == "east" then x = x + 1 end
  49.     end
  50.   end
  51.   savePos()
  52. end
  53.  
  54. function tm.back(nDistance)
  55.   nDistance = nDistance or 1
  56.   for i=1,nDistance do
  57.       if turtle.back() then
  58.         if facing == "north" then z = z + 1
  59.         elseif facing == "west" then x = x + 1
  60.         elseif facing == "south" then z = z - 1
  61.         elseif facing == "east" then x = x - 1 end
  62.       end
  63.   end
  64.   savePos()
  65. end
  66.  
  67. function tm.up(nDistance)
  68.   nDistance = nDistance or 1
  69.   for i=1,nDistance do
  70.     if turtle.up() then y = y + 1 end
  71.   end
  72.   savePos()
  73. end
  74.  
  75. function tm.down(nDistance)
  76.   nDistance = nDistance or 1
  77.   for i=1,nDistance do
  78.     if turtle.down() then y = y - 1 end
  79.   end
  80.   savePos()
  81. end
  82.  
  83. function tm.turnLeft()
  84.   turtle.turnLeft()
  85.   if facing == "north" then facing = "west"
  86.   elseif facing == "west" then facing = "south"
  87.   elseif facing == "south" then facing = "east"
  88.   elseif facing == "east" then facing = "north" end
  89.   savePos()
  90. end
  91.  
  92. function tm.turnRight()
  93.   turtle.turnRight()
  94.   if facing == "north" then facing = "east"
  95.   elseif facing == "west" then facing = "north"
  96.   elseif facing == "south" then facing = "west"
  97.   elseif facing == "east" then facing = "south" end
  98.   savePos()
  99. end
  100.  
  101. function tm.setPosition(x,y,z,facing)
  102.   tLoc[1],tLoc[2],tLoc[3],tLoc[4] = x,y,z,facing
  103. end
  104.  
  105. function tm.face(sDirection)
  106.   if sDirection == "north" then
  107.     if facing == "west" then tm.turnRight()
  108.     elseif facing == "south" then tm.turnRight() tm.turnRight()
  109.     elseif facing == "east" then tm.turnLeft() end
  110.   elseif sDirection == "west" then
  111.     if facing == "south" then tm.turnRight()
  112.     elseif facing == "east" then tm.turnRight() tm.turnRight()
  113.     elseif facing == "north" then tm.turnLeft() end
  114.   elseif sDirection == "south" then
  115.     if facing == "east" then tm.turnRight()
  116.     elseif facing == "north" then tm.turnRight() tm.turnRight()
  117.     elseif facing == "west" then tm.turnLeft() end
  118.   elseif sDirection == "east" then
  119.     if facing == "north" then tm.turnRight()
  120.     elseif facing == "west" then tm.turnRight() tm.turnRight()
  121.     elseif facing == "south" then tm.turnLeft() end
  122.   end
  123.   tm.save()
  124. end
  125.  
  126. function tm.getPosition()
  127.   return x,y,z,facing
  128. end
  129.  
  130. function tm.save()
  131.   savePos()
  132. end
  133.  
  134. return tm
RAW Paste Data