SkyCrafter0

turtlemovement.lua

Nov 25th, 2020 (edited)
578
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. turtlemovement.lua, my version of lama.
  3. this library tracks the turtle position and facing just like lama, but because lama is broken, I can not use it.
  4.  
  5. tData table:
  6. tData[1] is turtle position
  7. {1,2,3,"east"}
  8. ]]
  9.  
  10. local settingsPath = ".tm/settings"
  11.  
  12. if not fs.exists(settingsPath) then
  13.   fs.open(settingsPath,"w")
  14. end
  15.  
  16. local tm = {}
  17.  
  18. local function save(tTbl,sFilepath)
  19.   local file = fs.open(sFilepath,"w")
  20.   file.write(textutils.serialise(tTbl))
  21.   file.close()
  22. end
  23.  
  24. local function load(sFilepath)
  25.   local file = fs.open(sFilepath,"r")
  26.   local data = file.readAll()
  27.   file.close()
  28.   return textutils.unserialise(data)
  29. end
  30.  
  31. local tData = load(settingsPath)
  32.  
  33. local tLoc = tData[1]
  34. local x,y,z,facing = tLoc[1],tLoc[2],tLoc[3],tLoc[4]
  35.  
  36. local function savePos()
  37.   tLoc[1],tLoc[2],tLoc[3],tLoc[4] = x,y,z,facing
  38.   tData[1] = tLoc
  39. end
  40.  
  41. function tm.forward(nDistance)
  42.   nDistance = nDistance or 1
  43.   for i=1,nDistance do
  44.       turtle.forward()
  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.  
  52. function tm.back(nDistance)
  53.   nDistance = nDistance or 1
  54.   for i=1,nDistance do
  55.       turtle.forward()
  56.       if facing == "north" then z = z + 1
  57.       elseif facing == "west" then x = x + 1
  58.       elseif facing == "south" then z = z - 1
  59.       elseif facing == "east" then x = x - 1 end
  60.   end
  61. end
  62.  
  63. function tm.up(nDistance)
  64.   nDistance = nDistance or 1
  65.   for i=1,nDistance do
  66.       turtle.up()
  67.       y = y + 1
  68.   end
  69. end
  70.  
  71. function tm.down(nDistance)
  72.   nDistance = nDistance or 1
  73.   for i=1,nDistance do
  74.       turtle.up()
  75.       y = y - 1
  76.   end
  77. end
  78.  
  79. function tm.turnLeft()
  80.   turtle.turnLeft()
  81.   if facing == "north" then facing = "west"
  82.   elseif facing == "west" then facing = "south"
  83.   elseif facing == "south" then facing = "east"
  84.   elseif facing == "east" then facing = "north" end
  85. end
  86.  
  87. function tm.turnLeft()
  88.   turtle.turnLeft()
  89.   if facing == "north" then facing = "east"
  90.   elseif facing == "west" then facing = "north"
  91.   elseif facing == "south" then facing = "west"
  92.   elseif facing == "east" then facing = "south" end
  93. end
  94.  
  95. function tm.setPosition(x,y,z,facing)
  96.   tLoc[1],tLoc[2],tLoc[3],tLoc[4] = x,y,z,facing
  97. end
  98.  
  99. function tm.getPositon()
  100.   return x,y,z,facing
  101. end
  102.  
  103. function tm.save()
  104.   savePos()
  105. end
  106.  
  107. return tm
RAW Paste Data