Thunder7102

moveAPI - devBranch

Jun 24th, 2014
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.12 KB | None | 0 0
  1. --[[
  2.     MoveAPI is meant to be a replacement for the general
  3.     movement commands you use for turtles.
  4.    
  5.     Feature List
  6.     ------------
  7.     Keeps track of coordinates
  8.     Auto-handles sand/gravel
  9.     Variable forward()/up()/down()
  10.     Has moveTo() certain coordinates
  11.     Can travel() with a chunkloader
  12.         to ignore chunk load boundaries
  13. ]]
  14.  
  15. version = 1.3
  16.  
  17. xPos = 0
  18. yPos = 0
  19. zPos = 0
  20. dir = "north"
  21. chunkSlot = 2
  22. dirMoving = "forward"
  23. local isDebug = true
  24. local shouldPathFind = true
  25.  
  26. --[[
  27.     Here is your digging functions overwriting
  28. ]]
  29.  
  30. dig = turtle.dig
  31. digUp = turtle.digUp
  32. digDown = turtle.digDown
  33.  
  34. local function digDetect(detectFun, digFun)
  35.     while detectFun() do
  36.         if not digFun() and detectFun() then
  37.             return false
  38.         end
  39.         digFun()
  40.         sleep(.3)
  41.     end
  42.     return true
  43. end
  44.  
  45. turtle.dig = function() return digDetect(turtle.detect, dig) end
  46. turtle.digUp = function() return digDetect(turtle.detectUp, digUp) end
  47. turtle.digDown = function() return digDetect(turtle.detectDown, digDown) end
  48.  
  49. --[[
  50.     This is non-forceful movement in x distance
  51.     Returns false if something in the way.
  52. ]]--
  53.  
  54. forward = turtle.forward
  55. up = turtle.up
  56. down = turtle.down
  57.  
  58. turtle.forward = function(...)
  59.    --Returns true if successful, false if not
  60.    local arg = {...}
  61.     local distance = 0
  62.    
  63.     if arg[1] == nil then
  64.         distance = 1
  65.     else
  66.         if type(arg[1]) ~= "number" then error("Invalid type passed into down()!") end
  67.         distance = arg[1]
  68.     end
  69.    local x=0
  70.    while x<distance do
  71.        
  72.         dirMoving = "forward"
  73.         saveData()
  74.         if not forward() then
  75.             return false
  76.         end
  77.  
  78.         if dir == "north" then
  79.             zPos = zPos-1
  80.         elseif dir == "east" then
  81.             xPos = xPos+1
  82.         elseif dir == "west" then
  83.             xPos = xPos-1
  84.         elseif dir == "south" then
  85.             zPos=zPos+1
  86.         end
  87.         x=x+1
  88.    end
  89.    saveData()
  90.    return true
  91. end
  92.  
  93. local function movement(moveFunct, changeY, Moving, ...)
  94.     local arg = {...}
  95.     local distance = 0
  96.    
  97.     if arg[1] == nil then
  98.         distance = 1
  99.     else
  100.         if type(arg[1]) ~= "number" then error("Invalid type passed into down()!") end
  101.         distance = arg[1]
  102.     end
  103.    local x=0
  104.    while x<distance do
  105.         dirMoving = Moving
  106.         saveData()
  107.         if not moveFunct() then
  108.             return false
  109.         end
  110.         yPos=yPos+changeY
  111.         x=x+1
  112.    end
  113.    saveData()
  114.    return true
  115. end
  116.  
  117. turtle.up = function(...) return movement(up, 1, "up", ...) end
  118. turtle.down = function(...) return movement(down, -1, "down", ...) end
  119.    
  120. --[[
  121.     Here is a more forceful movements. If something is in the way,
  122.     the turtle WILL mine it out. Sand and gravel are also handled
  123.     properly.
  124. ]]
  125.  
  126. function fMovement(detectFunc, digFunc, moveFunc, attackFunc,...)
  127.     local args = {...}
  128.     local distance = 1
  129.     if type(args[1]) == "number" then
  130.        distance = args[1]
  131.     end
  132.  
  133.     for i=1, distance do
  134.         debug("Moving: "..i.."/"..distance)
  135.         while not moveFunc() do
  136.             if not digFunc() and detectFunc() then return false
  137.             else attackFunc() end
  138.         end
  139.     end
  140.    
  141.     return true
  142. end
  143.  
  144. turtle.fForward = function(...)  return fMovement(turtle.detect, turtle.dig, turtle.forward, turtle.attack,...) end
  145. turtle.fUp = function(...)  return fMovement(turtle.detectUp, turtle.digUp, turtle.up, turtle.attackUp,...) end
  146. turtle.fDown = function(...)  return fMovement(turtle.detectDown, turtle.digDown, turtle.down, turtle.attackDown,...) end
  147.  
  148. --[[
  149.     Here is all your turning code.
  150. ]]
  151. turnLeft = turtle.turnLeft
  152. turnRight = turtle.turnRight
  153.  
  154. turtle.turnLeft = function()
  155.     --Turns turtle left once and changes dir
  156.     turnLeft()
  157.     if dir == "north" then dir = "west"
  158.     elseif dir == "south" then dir = "east"
  159.     elseif dir == "west" then dir = "south"
  160.     elseif dir == "east" then dir = "north"
  161.     end
  162.     saveData() 
  163. end
  164.  
  165. turtle.turnRight = function()
  166.     --Turns turtle right once and changes dir
  167.     turnRight()
  168.     if dir == "north" then dir = "east"
  169.     elseif dir == "south" then dir = "west"
  170.     elseif dir == "west" then dir = "north"
  171.     elseif dir == "east" then dir = "south"
  172.     end
  173.     saveData() 
  174. end
  175.  
  176. turtle.turnAround = function()
  177.     turtle.turnRight()
  178.     turtle.turnRight()
  179.     saveData()
  180. end
  181.  
  182. turtle.turnToDir = function(directionName)
  183.     if directionName == "north" or directionName == "east" or directionName == "west" or directionName == "south" then
  184.         while dir ~= directionName do
  185.             turtle.turnLeft()
  186.             saveData()
  187.         end
  188.     else
  189.         error("setDir() had improper parameter passed into it. north/south/east/west required. You entered: "..directionName)
  190.         return "Improper Parameter used."
  191.     end
  192. end
  193.  
  194. --[[
  195.     These are here to provide information to the user.
  196.     Lots of figuring out coordinates of or in front
  197.     of turtle. I also placed setCoords() because...seemed appropriate.
  198. ]]
  199.  
  200. turtle.setCoords = function(...)
  201.     --Just a quick way to forceSet coordinates :)
  202.     --If you want non-relative coordinates, dir 0 = East, North: 3, South: 1, West: 2
  203.     local stuff = {...}
  204.     if #stuff <4 or #stuff > 4 then
  205.         error("Improper amount of variables passed into setCoords(). Requires 4, you entered "..#stuff)
  206.     elseif type(stuff[1]) ~= "number" or  type(stuff[2]) ~= "number" or type(stuff[3]) ~= "number" then
  207.         error("Numbers required to be entered into first three parameters. You entered: "..stuff[1].." "..stuff[2].." "..stuff[3])
  208.     elseif type(stuff[4]) ~= "string" then
  209.         error("Last parameter must be a string. You entered: "..stuff[4])
  210.     elseif stuff[4] ~= "north" and stuff[4] ~= "south" and stuff[4] ~= "east" and stuff[4] ~= "west" then
  211.         error("Last parameter requires a direction. north/south/east/west. You entered: "..stuff[4])
  212.     end
  213.  
  214.     xPos = stuff[1]
  215.     yPos = stuff[2]
  216.     zPos = stuff[3]
  217.     dir = stuff[4]
  218.     saveData()
  219. end
  220.  
  221. turtle.getCoords = function()
  222.     --Use this if you want the current coordinates of the turtle.
  223.     return xPos, yPos, zPos, dir
  224. end
  225.  
  226. turtle.getCoordsTbl = function()
  227.     --Returns a table of the current coordinates (good for quickly setting locations)
  228.     local table = {turtle.getX(), turtle.getY(), turtle.getZ(), turtle.getDir()}
  229.     return table
  230. end
  231.  
  232. turtle.getX = function()
  233.     return xPos
  234. end
  235.  
  236. turtle.getY = function()
  237.     return yPos
  238. end
  239.  
  240. turtle.getZ = function()
  241.     return zPos
  242. end
  243.  
  244. turtle.getDir = function()
  245.     return dir
  246. end
  247.  
  248. turtle.getFront = function()
  249.     --Returns coordinates right in front of turtle
  250.     if dir == "north" then
  251.         return xPos, yPos, zPos-1
  252.     elseif dir == "south" then
  253.         return xPos, yPos, zPos+1
  254.     elseif dir == "east" then
  255.         return xPos+1, yPos, zPos
  256.     elseif dir == "west" then
  257.         return xPos-1, yPos, zPos
  258.     else
  259.         print("Error, dir is improper value.")
  260.     end
  261. end
  262.  
  263. turtle.getFrontTbl = function()
  264.     --Returns coordinate table right in front of turtle
  265.     local table = {}
  266.  
  267.     if dir == "north" then
  268.         table = {xPos, yPos, zPos-1}
  269.     elseif dir == "south" then
  270.         table = {xPos, yPos, zPos+1}
  271.     elseif dir == "east" then
  272.         table = {xPos+1, yPos, zPos}
  273.     elseif dir == "west" then
  274.         table = {xPos-1, yPos, zPos}
  275.     else
  276.         print("Error, dir is improper value.")
  277.     end
  278.  
  279.     return table
  280. end
  281.  
  282. --[[
  283.     Here are some of the more advanced functions such as
  284.     move(), moveTo(), fMoveTo(), and travel().
  285.  
  286.     These are the intelligent functions for really going from
  287.     place to place. Pathfinding of the functions is very much a work in
  288.     progress. Do not rely on it too heavily. If you need to seriously
  289.     find around obstacles, I suggest writing your own. 
  290. ]]
  291.  
  292. turtle.move = function(direction, distance)
  293.     --This loop is setup so that it can be used regurally to save lines of code and to
  294.     --further modularize. Moves a certain distance in a specific direction.
  295.  
  296.     --Sets to proper direction before looping
  297.     if direction == "north" or direction == "south" or direction == "east" or direction == "west" then
  298.         turtle.turnToDir(direction)
  299.     end
  300.  
  301.     --This is the main loop.
  302.     local i = 0
  303.    
  304.     while i < distance do
  305.         if direction == "up" then
  306.             turtle.fUp()
  307.         elseif direction == "down" then
  308.             turtle.fDown()
  309.         else
  310.             turtle.fForward()
  311.         end
  312.  
  313.         i=i+1
  314.     end
  315. end
  316.  
  317. local function smartMove(moveFunc, shouldBreak,aiType,...)
  318.     --Base for intelligent navigation commands.
  319.     local args = {...}
  320.     local coords = {}
  321.     local axisOdr = {}
  322.     local isTrav = false
  323.     debug(aiType.." executing...")
  324.    
  325.     --This handles the travel special case
  326.    
  327.     coords, axisOdr, isTrav = _parseInput(args)
  328.  
  329.     if isTrav then
  330.         debug("Writing persist...")
  331.         writeTravPerst(coords,axisOdr)
  332.     end
  333.  
  334.     --Here, the turtle will actually follow the specific axis
  335.    
  336.     debug("Beginning movement with x: "..turtle.getX()..", y: "..turtle.getY()..", z: "..turtle.getZ())
  337.     debug("\nGoing to x: "..coords[1]..", y: "..coords[2]..", z: "..coords[3])
  338.     debug("Axis order is: "..axisOdr[1]..", "..axisOdr[2]..", "..axisOdr[3])
  339.     local shouldCont = true
  340.     for i=1, #axisOdr do
  341.         if axisOdr[i] == "x" and shouldCont then
  342.             if not moveFunc(coords[1], "x", shouldBreak) then shouldCont = false end
  343.         end
  344.  
  345.         if axisOdr[i] == "y" and shouldCont then
  346.             if not moveFunc(coords[2], "y", shouldBreak) then shouldCont = false end
  347.         end
  348.  
  349.         if axisOdr[i] == "z" and shouldCont then
  350.             if not moveFunc(coords[3], "z", shouldBreak) then shouldCont = false end
  351.         end
  352.     end
  353.    
  354.     --Some expiramental code which tries to find a way around
  355.     if not shouldCont then
  356.         if not collisionHandler(aiType, axisOdr, coords) then return false end
  357.     end
  358.     turtle.turnToDir(coords[4])
  359.     return true
  360. end
  361.  
  362. turtle.moveTo = function(...) if smartMove(goAxis, false, "moveTo",...) then return true else return false end end
  363. turtle.fMoveTo = function(...) if smartMove(goAxis, true, "fMoveTo", ...) then return true else return false end end
  364. turtle.travel = function(...)
  365.     if smartMove(loadLoop, false, "travel", "trav", ...) then return true else return false end
  366.     delTravPerst()
  367. end
  368.  
  369. --[[
  370.     HELPER COMMANDS
  371. ]]
  372.  
  373. function loadLoop(toCoord, axis, ...)
  374.     --This loop is setup so that it can be used regurally to save lines of code and to
  375.     --further modularize.
  376.  
  377.     --Random idiot-proofing.
  378.     if axis ~= "x" and axis ~= "y" and axis ~= "z" then
  379.         delTravPerst()
  380.         error("Improper parameter entered for axis. Need x, y, or z. Entered: "..axis)
  381.     end
  382.  
  383.     if type(toCoord) ~= "number" then
  384.         delTravPerst()
  385.         error("Improper parameter entered for coordinate. Please enter number. Entered: "..toCoord)
  386.     end
  387.  
  388.     if turtle.getItemCount(chunkSlot) == 0 then
  389.         delTravPerst()
  390.         error("Please provide a chunkloader in slot: "..chunkSlot.." or setChunkSlot() to where one is.")
  391.     end
  392.     --Alright, let's find the direction.
  393.     local direction
  394.     local distance
  395.     --Sets direction based on axis and currnet coordinates
  396.     if axis == "x" then
  397.         if toCoord>turtle.getX() then
  398.             direction = "east"
  399.             distance = toCoord - turtle.getX()
  400.         else
  401.             direction = "west"
  402.             distance =  turtle.getX() - toCoord
  403.         end
  404.     elseif axis == "y" then
  405.         if toCoord>turtle.getY() then
  406.             direction = "up"
  407.             distance = toCoord - turtle.getY()
  408.         else
  409.             direction = "down"
  410.             distance =  turtle.getY() - toCoord
  411.         end
  412.     else
  413.         if toCoord>turtle.getZ() then
  414.             direction = "south"
  415.             distance = toCoord - turtle.getZ()
  416.         else
  417.             direction = "north"
  418.             distance =  turtle.getZ() - toCoord
  419.         end
  420.     end
  421.  
  422.     --For them silly negatives. ex. -439 - -16 = -423
  423.     if distance < 0 then
  424.         distance = distance*-1
  425.     end
  426.  
  427.     --Sets to proper direction before looping
  428.     if direction == "north" or direction == "south" or direction == "east" or direction == "west" then
  429.         turtle.select(chunkSlot)
  430.         turtle.digUp()
  431.         turtle.placeUp()
  432.         turtle.digUp()
  433.         turtle.turnToDir(direction)
  434.     end
  435.  
  436.     --This is the main loop.
  437.     local i = 0
  438.     local increment = 3
  439.     local counter = 2
  440.  
  441.     while i < distance do
  442.         if direction == "up" then
  443.             if not turtle.fUp() then return false end
  444.         elseif direction == "down" then
  445.             if not turtle.fDown() then return false end
  446.         else
  447.             if not turtle.fForward() then return false end
  448.         end
  449.  
  450.         --Counter is here so the turtle can put up the chunkloader ever increment amount of blocks
  451.         counter = counter+1
  452.         if counter == increment then
  453.             --This ensures that too moves are not made before
  454.             --chunk unloads and ensures to refresh chunkload loop
  455.             turtle.select(chunkSlot)
  456.             if not turtle.detectUp() then
  457.                 turtle.placeUp()
  458.                 turtle.digUp()
  459.             else
  460.                 turtle.digUp()
  461.                 turtle.placeUp()
  462.                 turtle.digUp()
  463.             end
  464.  
  465.             counter = 0
  466.         end
  467.  
  468.         i=i+1
  469.     end
  470.     turtle.select(1)
  471.     return true
  472. end
  473.  
  474. function goAxis(toCoord, axis, shouldBreak)
  475.     --Checks the axis (x, y, or z) and moves along it. Will break blocks/eat things if shouldBreak true
  476.    
  477.     --If x is the axis, then go east or west
  478.     if axis == "x" then
  479.         if toCoord>turtle.getX() then
  480.             local distance = toCoord - turtle.getX()
  481.             for i = 1, distance do
  482.                 --East should be here, turn to right direction and go!
  483.                 turtle.turnToDir("east")
  484.                 if shouldBreak then
  485.                     if not turtle.fForward() then return false, "east" end
  486.                 else
  487.                     if not turtle.forward() then return false, "east" end
  488.                 end
  489.             end
  490.         elseif toCoord<turtle.getX() then
  491.             local distance = turtle.getX() - toCoord
  492.             for i = 1, distance do
  493.                 turtle.turnToDir("west")
  494.                 if shouldBreak then
  495.                     if not turtle.fForward() then return false, "west" end
  496.                 else
  497.                     if not turtle.forward() then return false, "west" end
  498.                 end
  499.             end
  500.         end
  501.     end
  502.     --If y is axis, go up or down
  503.     if axis == "y" then
  504.         if toCoord>turtle.getY() then
  505.             local distance = toCoord - turtle.getY()
  506.             for i = 1, distance do
  507.                 --Gotta go up, here
  508.                 if shouldBreak then
  509.                     if not turtle.fUp() then return false, "up" end
  510.                 else
  511.                     if not turtle.up() then return false, "up" end
  512.                 end
  513.             end
  514.         elseif toCoord<turtle.getY() then
  515.             local distance = turtle.getY() - toCoord
  516.             for i = 1, distance do
  517.                 if shouldBreak then
  518.                     if not turtle.fDown() then return false, "down" end
  519.                 else
  520.                     if not turtle.down() then return false, "down" end
  521.                 end
  522.             end
  523.         end
  524.     end
  525.    
  526.     --If z is the axis, handle it this way
  527.     if axis == "z" then
  528.         if toCoord>turtle.getZ() then
  529.             local distance = toCoord - turtle.getZ()
  530.             for i = 1, distance do
  531.                 --East should be here, turn to right direction and go!
  532.                 turtle.turnToDir("south")
  533.                 if shouldBreak then
  534.                     if not turtle.fForward() then return false, "south" end
  535.                 else
  536.                     if not turtle.forward() then return false, "south" end
  537.                 end
  538.             end
  539.         elseif toCoord<turtle.getZ() then
  540.             local distance = turtle.getZ() - toCoord
  541.             for i = 1, distance do
  542.                 turtle.turnToDir("north")
  543.                 if shouldBreak then
  544.                     if not turtle.fForward() then return false, "north" end
  545.                 else
  546.                     if not turtle.forward() then return false, "north" end
  547.                 end
  548.             end
  549.         end
  550.     end
  551.     return true
  552. end
  553.  
  554. --Various helper functions from handling input, to pathfinding
  555. turtle.setChunkSlot = function(slot)
  556.   if slot>0 and slot<17 then
  557.     chunkSlot = slot
  558.     return true
  559.   else
  560.     return false
  561.   end
  562. end
  563.  
  564. turtle.setPathFinding = function(setting)
  565.     --Sets if pathfinding will happen when obstacles are hit in moveTo or travel
  566.     if type(setting) ~= "boolean" then return false end
  567.  
  568.     shouldPathFind = setting
  569. end
  570.  
  571. function collisionHandler(moveType, axisOdr, coords)
  572.     --This will allow at least a basic attempt at collisionhandling. It will not solve mazes.
  573.     if not shouldPathFind then return false end
  574.     if fs.exists("moveCol") then return false end
  575.  
  576.     local f = fs.open("moveCol", "w")
  577.     f.write("m")
  578.     f.close()
  579.     local totCollisions = 0
  580.     local step = 1
  581.     local colX, colY, colZ, colDir = turtle.getCoords()
  582.     local keepTrying = true
  583.     --This is not meant to be a maze-solver, just help a little.
  584.     while totCollisions < 10 and keepTrying do
  585.         colX, colY, colZ, colDir = turtle.getCoords()
  586.         totCollisions = totCollisions+1
  587.         --This is if the same spot is a problem
  588.         debug(turtle.getCoords())
  589.         debug("Coll Coords: "..colX.." "..colY.." "..colZ)
  590.         if turtle.getX() == colX and turtle.getY() == colY and turtle.getZ() == colZ then
  591.             --Still stuck in same spot..better try something else.
  592.             step = step+1
  593.         else
  594.             --New spot, alright, let's start from scratch.
  595.             step = 1
  596.             colX, colY, colZ = turtle.getCoords()
  597.         end
  598.  
  599.         debug("\nCollision! Total: "..totCollisions.."\nStep: "..step)
  600.         --Let's try a couple things.
  601.         if step == 1 then
  602.             turtle.turnAround()
  603.             turtle.forward(1)
  604.             axisOdr[1], axisOdr[2] = axisOdr[2], axisOdr[1]
  605.         elseif step == 2 then
  606.             turtle.turnLeft()
  607.             turtle.forward(2)
  608.             turtle.up()
  609.         elseif step == 3 then
  610.             turtle.turnRight()
  611.             turtle.forward(2)
  612.             turtle.down()
  613.             axisOdr[2], axisOdr[3] = axisOdr[3], axisOdr[2]
  614.         end
  615.  
  616.         --This just reexecutes a try.
  617.  
  618.         if moveType == "travel" then
  619.             if turtle.travel(coords, axisOdr[1], axisOdr[2], axisOdr[3]) then
  620.                 fs.delete("moveCol")
  621.                 return true
  622.             end
  623.         elseif moveType == "moveTo" then
  624.             if turtle.moveTo(coords, axisOdr[1], axisOdr[2], axisOdr[3]) then
  625.                 fs.delete("moveCol")
  626.                 return true
  627.             end
  628.         elseif moveType == "fMoveTo" then
  629.             if turtle.fMoveTo(coords, axisOdr[1], axisOdr[2], axisOdr[3]) then
  630.                 fs.delete("moveCol")
  631.                 return true
  632.             end
  633.         end
  634.     end
  635.  
  636.     fs.delete("moveCol")
  637.     return false
  638. end
  639.  
  640. function _parseInput(inputTable)
  641.     --Parses through user input and returns an axis-table and coordinates.
  642.     local args = inputTable
  643.     local coords = {}
  644.     local axisOdr = {}
  645.     local isTrav = false
  646.  
  647.     --Let's loop through and get the coordinates if they exist.
  648.     for i=1, #args do
  649.         --Let's load up all their stuff.
  650.         debug("Parsing Input #"..i..": ")
  651.         debug(args[i])
  652.         if type(args[i]) == "number" then
  653.             coords[#coords+1] = args[i]
  654.         elseif args[i] == "x" or args[i] == "y" or args[i] == "z" then
  655.             axisOdr[#axisOdr+1] = args[i]
  656.         elseif args[i] == "north" or args[i] == "south" or args[i] == "east" or args[i] == "west" then
  657.             coords[4] = args[i]
  658.         elseif type(args[i]) == "table" then
  659.             --Making sure they don't pass in some random table.
  660.             if #args[i] ~= 3 and #args[i] ~= 4 then
  661.                 error("Error passing in table as parameter. Not enough parameters present in table. Needs to hold 3 or 4 entries. Table length: "..#args[i])
  662.             end
  663.  
  664.             --This lets the user pass in a table where it will get evaluated independantly.
  665.             for k=1, #args[i] do
  666.                 if type(args[i][k]) == "number" then
  667.                     coords[#coords+1] = args[i][k]
  668.                 elseif args[i][k] == "north" or args[i][k] == "south" or args[i][k] == "east" or args[i][k] == "west" then
  669.                     coords[4] = args[i][k]
  670.                 end
  671.             end
  672.         elseif args[i] == "trav" then
  673.             isTrav = true
  674.         else
  675.             error("Improper parameter passed into function. Passed in: "..args[i])
  676.         end
  677.     end
  678.    
  679.     --Ok, stuff has been placed, now, let's idiot-check
  680.    
  681.     --This makes sure they provided 3 coordinates.
  682.     local nums = 0
  683.     for i=1, #coords do
  684.         if type(coords[i]) == "number" then
  685.             nums = nums+1
  686.         end
  687.     end
  688.  
  689.     if nums ~= 3 then error("Improper number of coordinate parameters. Three required. You entered: "..nums) end
  690.     if coords[4] == nil then coords[4] = "north" end
  691.     --This will be the default path whe pathfinding.
  692.     if #axisOdr ~= 3 then
  693.         debug("Axis order either not entered or entered incorrectly, adjusting to default x -> z -> y...")
  694.         axisOdr = {"x", "z", "y"}
  695.     end
  696.  
  697.     --Now, let's just shoot everything back.
  698.     return coords,axisOdr,isTrav
  699. end
  700.  
  701. --[[
  702.     Saving
  703.     This is for redundancy of the API. You shouldn't need to
  704.     call this...ever.
  705. ]]
  706. function saveData()
  707.     local saveData = {xPos, yPos, zPos, dir, turtle.getFuelLevel(), chunkSlot, dirMoving}
  708.     local f = fs.open("api/moveLoc", "w")
  709.     f.write(textutils.serialize(saveData))
  710.     f.close()
  711. end
  712.  
  713. function loadData()
  714.     debug("Checking for previous config...")
  715.     if fs.exists("api/moveLoc") then
  716.         f = fs.open("api/moveLoc", "r")
  717.         local data = textutils.unserialize(f.readAll())
  718.         f.close()
  719.  
  720.         debug("Config found. Loading data...")
  721.         --Checks to make sure turtle didn't move or do anything wonky after reboot
  722.         if data[5] == turtle.getFuelLevel() then
  723.             --Loading in the data since nothig bad happened
  724.             local _d = ""
  725.             xPos, yPos, zPos, dir, _d, chunkSlot, dirMoving = unpack(data)
  726.         elseif data[5] == (turtle.getFuelLevel()+1) then
  727.             --Here, we've gotta fix a few things since the turtle did in fact, move
  728.             debug("Looks like the turtle moved and didn't save his position, altering...")
  729.             if data[7] == "forward" then
  730.                 --Forward means he went somewhere on the x/z axis...gotta account for it
  731.                 if data[4] == "north" then
  732.                     data[3] = data[3]-1
  733.                 elseif data[4] == "east" then
  734.                     data[1] = data[1]+1
  735.                 elseif data[4] == "west" then
  736.                     data[1] = data[1]-1
  737.                 elseif data[4] == "south" then
  738.                     data[3]=data[3]+1
  739.                 end
  740.             elseif data[7] == "up" then
  741.                 data[2] = data[2]+1
  742.             elseif data[7] == "down" then
  743.                 data[2] = data[2]-1
  744.             end
  745.  
  746.             --Now we can load in the fixed data.
  747.             debug("Loading data in...")
  748.             xPos, yPos, zPos, dir, _, chunkSlot, dirMoving = unpack(data)
  749.             debug("Data loaded!")
  750.             debug("X: "..xPos)
  751.             debug("Y: "..yPos)
  752.             debug("Z: "..zPos)
  753.             debug("Direction: "..dir)
  754.         elseif data[5] > (turtle.getFuelLevel()-1) or data[5] < turtle.getFuelLevel() then
  755.             debug("Looks like the fuel level changed by more than one. Wiping location knowledge.")
  756.             saveData()
  757.         else
  758.             debug("Something really weird is going on here. The recorded fuel is: "..data[5].." but current fuel is: "..turtle.getFuelLevel())
  759.             debug("Get Signify/Noiro to look at this because this wasn't supposed to happen.")
  760.             saveData()
  761.         end
  762.     end
  763. end
  764.  
  765. function writeTravPerst(coords, axisOdr)
  766.     --Travel persistence through restarts.
  767.     if fs.exists("moveStartup-No-Delete") or fs.exists("no~startup") then
  768.         debug("\n\nTrav is already going, ignoring...")
  769.         return false
  770.     elseif fs.exists("startup") then
  771.         debug("\nStartup exists..moving..")
  772.         fs.move("startup", "moveStartup-No-Delete")
  773.     else
  774.         debug("\n\nNo startup existed..writing my own temp one...")
  775.         local f = fs.open("no~startup", "w")
  776.         f.write("o")
  777.         f.close()
  778.     end
  779.  
  780.     local f = fs.open("startup", "w")
  781.     if fs.exists("api/move") then
  782.         f.writeLine("os.loadAPI(\"api/move\")")
  783.     elseif fs.exists("move") then
  784.         f.writeLine("os.loadAPI(\"move\")")
  785.     else
  786.         f.close()
  787.         return false
  788.     end
  789.     --Explosive write to file
  790.     f.writeLine("if turtle.detectUp() and turtle.getItemCount(turtle.getSelectedSlot()) == 0 then")
  791.     f.writeLine("turtle.digUp()")
  792.     f.writeLine("end")
  793.     f.writeLine("turtle.travel("..coords[1]..", "..coords[2]..", "..coords[3]..", "..coords[4]..", "..axisOdr[1]..", "..axisOdr[2]..","..axisOdr[3]..")")
  794.     f.writeLine("move.delTravPerst()")
  795.  
  796.     f.writeLine("if fs.exists(\"no~startup\") then fs.delete(\"no~startup\")")
  797.     f.writeLine("elseif fs.exists(\"moveStartup-No-Delete\") then")
  798.     f.writeLine("fs.delete(\"startup\")")
  799.     f.writeLine("fs.move(\"moveStartup-No-Delete\", \"startup\")")
  800.     f.writeLine("else")
  801.     f.writeLine("fs.delete(\"startup\")")
  802.     f.writeLine("end")
  803.     f.writeLine("os.reboot()")
  804.     f.close()
  805.     return true
  806. end
  807.  
  808. function delTravPerst()
  809.     --Travel persistence through restarts.
  810.     if fs.exists("startup") and fs.exists("no~startup") or fs.exists("moveStartup-No-Delete") then
  811.         fs.delete("no~startup")
  812.         fs.delete("startup")
  813.         if fs.exists("moveStartup-No-Delete") then
  814.             fs.move("moveStartup-No-Delete", "startup")
  815.         end
  816.     end
  817. end
  818. --[[
  819.     Random internal testing stuff
  820. ]]
  821. function debug(message)
  822.     if isDebug then
  823.         print(message)
  824.         sleep(.5)
  825.     end
  826. end
  827.  
  828. --Execution Code
  829. delTravPerst()
  830. if fs.exists("moveCol") then
  831.     fs.delete("moveCol")
  832. end
  833. loadData()
Advertisement
Add Comment
Please, Sign In to add comment