Advertisement
Thunder7102

moveAPI

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