Advertisement
Tatantyler

Navigation API

Dec 24th, 2012
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.10 KB | None | 0 0
  1. local facingTable = {
  2.     [0] = "south",
  3.     [1]= "west",
  4.     [2] = "north",
  5.     [3] = "east",
  6.     ["south"] = 0,
  7.     ["west"] = 1,
  8.     ["north"] = 2,
  9.     ["east"] = 3,
  10. }
  11.  
  12. local fuelTable = {
  13.     ["Biofuel Can"] = 624,
  14.     ["Scrap"] = 18,
  15.     ["Coalfuel Can"] = 1824,
  16.     ["Wooden Scaffolding"] = 18,
  17.     ["Peat"] = 96,
  18.     ["Wooden Tools"] = 12,
  19.     ["Lava"] = 1200,
  20.     ["Blaze Rod"] = 144,
  21.     ["Wood Blocks"] = 18,
  22.     ["Sticks"] = 6,
  23.     ["Coal/Charcoal"] = 96,
  24.     ["Mushroom Blocks"] = 18,
  25.     ["Coal Coke"] = 192,
  26. }
  27.  
  28. local vars = {
  29.     currentFacing = 0
  30. }
  31.  
  32. function saveVars()
  33.     local handle = fs.open("Guidance.vars", "w")
  34.     handle.write(textutils.serialize(vars))
  35.     handle.close()
  36. end
  37.  
  38. function loadVars()
  39.     if fs.exists("guidance.vars") then
  40.         local handle = fs.open("Guidance.vars", "r")
  41.         vars = textutils.unserialize(handle.readAll())
  42.         handle.close()
  43.     end
  44. end
  45.  
  46. function getPos()
  47.     local currentX, currentY, currentZ = 0
  48.     if vars.useGPS then
  49.         rednet.open("right")
  50.         currentX, currentY, currentZ = gps.locate(5)
  51.     end
  52.     if vars.deadReckon ~= nil and (currentX == nil or currentX == 0) then
  53.         currentX = vars.deadReckon.x
  54.         currentY = vars.deadReckon.y
  55.         currentZ = vars.deadReckon.z
  56.     end
  57.     if currentX ~= nil then
  58.         vars.deadReckon = {}
  59.         vars.deadReckon.x = currentX
  60.         vars.deadReckon.y = currentY
  61.         vars.deadReckon.z = currentZ
  62.     end
  63.     return currentX, currentY, currentZ
  64. end
  65.  
  66. function turnToFacing(f)
  67.     while true do
  68.         if facingTable[f] == facingTable[vars.currentFacing] then
  69.             break
  70.         end
  71.         turtle.turnRight()
  72.         vars.currentFacing = vars.currentFacing + 1
  73.         if vars.currentFacing > 3 then
  74.             vars.currentFacing = 0
  75.         end
  76.     end
  77.     saveVars()
  78. end
  79.  
  80. function setFacing(f)
  81.     vars.currentFacing = f
  82.     saveVars()
  83. end
  84.  
  85. function useGPS(b)
  86.     vars.useGPS = b
  87.     saveVars()
  88. end
  89.  
  90. function getVar(key, value)
  91.     return vars[key]
  92. end
  93.  
  94. function editVar(key, value)
  95.     vars[key] = value
  96. end
  97.  
  98. function calculateFuelUsage(distance, fuelType)
  99.     return math.ceil(distance / fuelTable[fuelType]), math.ceil(distance % fuelTable[fuelType])
  100. end
  101.  
  102. function goTo(x,y,z)
  103.     rednet.open("right")
  104.     if vars.useGPS then
  105.         local currentX, currentY, currentZ = gps.locate(5)
  106.     end
  107.     if vars.deadReckon ~= nil and currentX == nil then
  108.         currentX = vars.deadReckon.x
  109.         currentY = vars.deadReckon.y
  110.         currentZ = vars.deadReckon.z
  111.     end
  112.     if currentX ~= nil then
  113.         vars.deadReckon = {}
  114.         vars.deadReckon.x = currentX
  115.         vars.deadReckon.y = currentY
  116.         vars.deadReckon.z = currentZ
  117.         saveVars()
  118.         local offsetX = currentX - x
  119.         local offsetY = currentY - y
  120.         local offsetZ = currentZ - z
  121.         print("Location is: "..currentX.." "..currentY.." "..currentZ)
  122.         local distance = math.abs(offsetX)+math.abs(offsetY)+math.abs(offsetZ)
  123.         print("Calculated distance to travel: "..distance.." blocks.")
  124.         local coalUsed = calculateFuelUsage(distance, "Coal/Charcoal")
  125.         if turtle.getFuelLevel() ~= "unlimited" then
  126.             if turtle.getFuelLevel() < distance then
  127.                 print("Fuel will be required.")
  128.                 print("Please put coal in the lower left slot.")
  129.                 if coalUsed ~= 1 then
  130.                     print("You will need "..coalUsed.." units of coal.")
  131.                 else
  132.                     print("You will need 1 unit of coal.")
  133.                 end
  134.                 while true do
  135.                     if turtle.getItemCount(16) > 0 then
  136.                         turtle.select(16)
  137.                         turtle.refuel()
  138.                         if turtle.getFuelLevel() > distance then
  139.                             print("Fueling complete.")
  140.                             break
  141.                         end
  142.                     end
  143.                     os.sleep(0)
  144.                 end
  145.             end
  146.         end
  147.         if offsetZ < 0 then
  148.             turnToFacing(facingTable["south"])
  149.         elseif offsetZ > 0 then
  150.             turnToFacing(facingTable["north"])
  151.         end
  152.         for i=1, math.abs(offsetZ) do
  153.             while true do
  154.                 if turtle.detect() then
  155.                     if not ((peripheral.getType("front") == "turtle") or (peripheral.getType("front") == "computer")) then
  156.                         turtle.dig()
  157.                     end
  158.                 else
  159.                     break
  160.                 end
  161.                 os.sleep(0)
  162.             end
  163.             turtle.forward()
  164.             if offsetZ < 0 then
  165.                 vars.deadReckon.z = vars.deadReckon.z+1
  166.             else
  167.                 vars.deadReckon.z = vars.deadReckon.z-1
  168.             end
  169.         end
  170.         if offsetX < 0 then
  171.             turnToFacing(facingTable["east"])
  172.         elseif offsetX > 0 then
  173.             turnToFacing(facingTable["west"])
  174.         end
  175.         for i=1, math.abs(offsetX) do
  176.             while true do
  177.                 if turtle.detect() then
  178.                     if not ((peripheral.getType("front") == "turtle") or (peripheral.getType("front") == "computer")) then
  179.                         turtle.dig()
  180.                     end
  181.                 else
  182.                     break
  183.                 end
  184.                 os.sleep(0)
  185.             end
  186.             turtle.forward()
  187.             if offsetX < 0 then
  188.                 vars.deadReckon.x = vars.deadReckon.x+1
  189.             else
  190.                 vars.deadReckon.x = vars.deadReckon.x-1
  191.             end
  192.         end
  193.         for i=1, math.abs(offsetY) do
  194.             if offsetY < 0 then
  195.                 while true do
  196.                     if turtle.detectUp() then
  197.                         if not ((peripheral.getType("top") == "turtle") or (peripheral.getType("top") == "computer")) then
  198.                             turtle.digUp()
  199.                         end
  200.                     else
  201.                         break
  202.                     end
  203.                     os.sleep(0)
  204.                 end
  205.                 turtle.up()
  206.                 vars.deadReckon.y = vars.deadReckon.y+1
  207.             elseif offsetY > 0 then
  208.                 while true do
  209.                     if turtle.detectDown() then
  210.                         if not ((peripheral.getType("bottom") == "turtle") or (peripheral.getType("bottom") == "computer")) then
  211.                             turtle.digDown()
  212.                         end
  213.                     else
  214.                         break
  215.                     end
  216.                     os.sleep(0)
  217.                 end
  218.                 turtle.down()
  219.                 vars.deadReckon.y = vars.deadReckon.y-1
  220.             end
  221.         end
  222.     end
  223.     saveVars()
  224. end
  225.  
  226. function printDeadReckon()
  227.     if vars.deadReckon ~= nil then
  228.         print("X: "..vars.deadReckon["x"])
  229.         print("Y: "..vars.deadReckon["y"])
  230.         print("Z: "..vars.deadReckon["z"])
  231.     end
  232. end
  233.  
  234. function primeDeadReckon(x,y,z)
  235.     if x == nil then
  236.         rednet.open("right")
  237.         local currentX, currentY, currentZ = gps.locate(5)
  238.         vars.deadReckon = {}
  239.         vars.deadReckon.x = currentX
  240.         vars.deadReckon.y = currentY
  241.         vars.deadReckon.z = currentZ
  242.     else
  243.         vars.deadReckon = {}
  244.         vars.deadReckon.x = x
  245.         vars.deadReckon.y = y
  246.         vars.deadReckon.z = z
  247.     end
  248.     saveVars()
  249.     printDeadReckon()
  250. end
  251.  
  252. function setup(x,y,z,f,gps)
  253.     if (x ~= nil) and (y ~= nil) and (z ~= nil) and (f ~= nil) then
  254.         primeDeadReckon(x,y,z)
  255.         setFacing(f)
  256.         useGPS(gps)
  257.     else
  258.         print("Setup Utility:")
  259.         print("Please enter the coordinates for this turtle's location:")
  260.         write("X: ")
  261.         local XLoc = ""
  262.         while true do
  263.             XLoc = tonumber(read())
  264.             if XLoc then
  265.                 break
  266.             end
  267.         end
  268.        
  269.         write("Y: ")
  270.         local YLoc = ""
  271.         while true do
  272.             YLoc = tonumber(read())
  273.             if YLoc then
  274.                 break
  275.             end
  276.         end
  277.        
  278.         write("Z: ")
  279.         local ZLoc = ""
  280.         while true do
  281.             ZLoc = tonumber(read())
  282.             if ZLoc then
  283.                 break
  284.             end
  285.         end
  286.        
  287.         write("Facing: ")
  288.         local Facing = ""
  289.         while true do
  290.             Facing = tonumber(read())
  291.             if Facing then
  292.                 break
  293.             end
  294.         end
  295.         primeDeadReckon(XLoc, YLoc, ZLoc)
  296.         setFacing(Facing)
  297.         while true do
  298.             print("Should this turtle use GPS?")
  299.             write(">")
  300.             term.setCursorBlink(true)
  301.             answer = string.lower(string.sub(read(),1,1))
  302.             if answer == "n" then
  303.                 gps = false
  304.                 break
  305.             elseif answer == "y" then
  306.                 gps = true
  307.                 break
  308.             else
  309.                 local x,y = term.getCursorPos()
  310.                 print("Please provide a valid answer.")
  311.                 term.setCursorPos(1, y-1)
  312.             end
  313.         end
  314.         useGPS(gps)
  315.         print("Setup complete.")
  316.         saveVars()
  317.     end
  318. end
  319.  
  320. loadVars()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement