Advertisement
joeld98

A* in computercraft - Rev 3

Aug 20th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 40.03 KB | None | 0 0
  1. local function log(msg) -- adapted from: https://github.com/rxi/log.lua
  2.   if not os.loadAPI("rwt") then
  3.     if not shell.run("pastebin", "get", "TNLjCCKq", "rwt") then
  4.       print("Could not load Time API")
  5.     else
  6.       os.loadAPI("rwt")
  7.     end
  8.   end
  9.   --logTime = rwt.getTime("utc",-4,dateTime24_mmddyyyy,10)
  10.   logTime = "TIMEOFF"
  11.   local fp = io.open("seek.log", "a")
  12.   --local str = string.format("[%s]%s\n",logTime, msg)
  13.   --local str = string.format("[%s]%s\n",logTime, msg)
  14.   local str = string.format("%s\n",msg)
  15.       fp:write(str)
  16.       fp:close()
  17. end
  18.  
  19. local function saveTable(table,fileName)
  20.   local hWrite = fs.open(fileName, "w")
  21.   hWrite.write(textutils.serialize(table))
  22.   hWrite.close()
  23. end
  24.  
  25. local function getFacingDirection()
  26.     log("Get Facing Direction Called")
  27.     local function findClearBlock()
  28.     local turnsToClear = 0
  29.     while turtle.inspect() and turnsToClear <=4 do
  30.       turtle.turnRight()
  31.       turnsToClear = turnsToClear + 1
  32.     end
  33.     if turnsToClear >= 4 then
  34.       return nil
  35.     else
  36.     end
  37.     if turnsToClear ~=4 and turnsToClear ~=0 then
  38.       for faceHome = 1, turnsToClear do
  39.         turtle.turnLeft()
  40.       end
  41.     end
  42.     return turnsToClear
  43.     end
  44.     local startLoc = vector.new(gps.locate())
  45.     local dirLoc
  46.     local facingDir = 0
  47.     local facingVector
  48.     if turtle.forward() then
  49.     dirLoc = vector.new(gps.locate())
  50.     turtle.back()
  51.     facingVector = dirLoc - startLoc
  52.     facingDir = ((facingVector.x + math.abs(facingVector.x) * 2) + (facingVector.z + math.abs(facingVector.z) * 3))
  53.     else
  54.     local tCount = findClearBlock()
  55.     if tCount == nil then
  56.       return nil
  57.     elseif tCount == 2 then
  58.       turtle.back()
  59.       dirLoc = vector.new(gps.locate())
  60.       turtle.forward()
  61.       facingVector = startLoc - dirLoc
  62.       facingDir = (((facingVector.x + math.abs(facingVector.x) * 2) + (facingVector.z + math.abs(facingVector.z) * 3)))
  63.     elseif tCount == 1 then
  64.       turtle.turnRight()
  65.       turtle.forward()
  66.       turtle.turnLeft()
  67.       dirLoc = vector.new(gps.locate())
  68.       turtle.turnLeft()
  69.       turtle.forward()
  70.       turtle.turnRight()
  71.       facingVector = dirLoc - startLoc
  72.       facingDir = (((facingVector.x + math.abs(facingVector.x) * 2) + (facingVector.z + math.abs(facingVector.z) * 3))) - 1
  73.     elseif
  74.       tCount == 3 then
  75.       turtle.turnLeft()
  76.       turtle.forward()
  77.       turtle.turnRight()
  78.       dirLoc = vector.new(gps.locate())
  79.       turtle.turnRight()
  80.       turtle.forward()
  81.       turtle.turnLeft()
  82.       facingVector = dirLoc - startLoc
  83.       facingDir = ((facingVector.x + math.abs(facingVector.x) * 2) + (facingVector.z + math.abs(facingVector.z) * 3)) + 1
  84.       if facingDir > 4 then
  85.       facingDir = 1
  86.       end
  87.     end
  88.     end
  89.     log(string.format("Facing:%s", tostring(facingDir)))
  90.     return facingDir
  91. end
  92.  
  93. local function rightTurn()
  94.     if not facingDirection then
  95.         facingDirection = getFacingDirection()
  96.     end
  97.     turtle.turnRight()
  98.     facingDirection = facingDirection + 1
  99.     if facingDirection > 4 then
  100.         facingDirection = 1
  101.     end
  102. end
  103.  
  104. local function leftTurn()
  105.     if not facingDirection then
  106.         facingDirection = getFacingDirection()
  107.     end
  108.     turtle.turnLeft()
  109.     facingDirection = facingDirection - 1
  110.     if facingDirection < 1 then
  111.         facingDirection = 4
  112.     end
  113. end
  114.  
  115.  
  116. local function getNodeData(currentVector, targetVector , startVector, parentVector)
  117.   log("GetNodeData Called")
  118.   local cNode, cNodeKey = nil
  119.   local fNode, fNodeKey, fInspect, fResult = nil
  120.   local uNode, uNodeKey, uInspect,uResult = nil
  121.   local dNode, dNodeKey, dInspect, dResult = nil
  122.   local rNode, rNodeKey, rInspect, rResult = nil
  123.   local bNode, bNodeKey, bInspect, bResult = nil
  124.   local lNode, lNodeKey, lInspect, lResult = nil  
  125.   local fuNode, fuNodeKey = nil
  126.   local ruNode, ruNodeKey = nil
  127.   local luNode, luNodeKey = nil
  128.   local buNode, buNodeKey = nil
  129.   local fdNode, fdNodeKey = nil
  130.   local rdNode, rdNodeKey = nil
  131.   local bdode, bdNodeKey = nil
  132.   local bdrNode, bdNodeKey = nil
  133.   local frNode, frNodeKey = nil
  134.   local rrNode, rrNodeKey = nil
  135.   local flNode, flNodeKey = nil
  136.   local lrNode, lrNodeKey = nil
  137.   local fruNode, fruNodeKey = nil
  138.   local rruNode, rruNodeKey = nil
  139.   local fluNode, fluNodeKey = nil
  140.   local lruNode, lruNodeKey = nil
  141.   local frdNode, frdNodeKey = nil
  142.   local rrdNode, rrdNodeKey = nil
  143.   local fldNode, fldNodeKey = nil
  144.   local lrdNode, lrdNodeKey = nil
  145.   cNode = {x = 0, y = 0, z = 0}
  146.  
  147.   fNode = {x = 0, y = 0, z = 0}
  148.   uNode = {x = 0, y = 0, z = 0}
  149.   dNode = {x = 0, y = 0, z = 0}  
  150.   rNode = {x = 0, y = 0, z = 0}
  151.   bNode = {x = 0, y = 0, z = 0}
  152.   lNode = {x = 0, y = 0, z = 0}
  153.   frNode = {x = 0, y = 0, z = 0}
  154.   fruNode = {x = 0, y = 0, z = 0}
  155.   frdNode = {x = 0, y = 0, z = 0}
  156.   fuNode = {x = 0, y = 0, z = 0}
  157.   fdNode = {x = 0, y = 0, z = 0}  
  158.   flNode = {x = 0, y = 0, z = 0}
  159.   fluNode = {x = 0, y = 0, z = 0}
  160.   fldNode = {x = 0, y = 0, z = 0}
  161.   ruNode = {x = 0, y = 0, z = 0}
  162.   rdNode = {x = 0, y = 0, z = 0}
  163.   rrNode = {x = 0, y = 0, z = 0}
  164.   rruNode = {x = 0, y = 0, z = 0}  
  165.   rrdNode = {x = 0, y = 0, z = 0}  
  166.   buNode = {x = 0, y = 0, z = 0}
  167.   bdNode = {x = 0, y = 0, z = 0}       
  168.   lrNode = {x = 0, y = 0, z = 0}
  169.   lruNode = {x = 0, y = 0, z = 0}
  170.   lrdNode = {x = 0, y = 0, z = 0}
  171.   luNode = {x = 0, y = 0, z = 0}
  172.   ldNode = {x = 0, y = 0, z = 0}
  173.  
  174.   if not facingDirection then
  175.     log("Facing Direction not found in gotoNode(). Calling getFacingDirection()")
  176.     facingDirection = getFacingDirection()  
  177.   end
  178.   uNode.x = currentVector.x
  179.   uNode.z = currentVector.z
  180.   uNode.y = currentVector.y + 1  
  181.   dNode.x = currentVector.x
  182.   dNode.z = currentVector.z
  183.   dNode.y = currentVector.y - 1
  184.    cNodeKey = string.format("%d,%d,%d", currentVector.x, currentVector.y, currentVector.z)
  185.   if facingDirection == 1 then  
  186.     if currentVector.x <= 0 then  
  187.       fNode.x = currentVector.x - 1
  188.       bNode.x = currentVector.x + 1
  189.       lNode.x = currentVector.x
  190.       rNode.x = currentVector.x
  191.     else
  192.       fNode.x = currentVector.x + 1
  193.       bNode.x = currentVector.x - 1
  194.       lNode.x = currentVector.x
  195.       rNode.x = currentVector.x
  196.     end
  197.     if currentVector.z <= 0 then  
  198.       lNode.z = currentVector.z + 1
  199.       rNode.z = currentVector.z - 1  
  200.       fNode.z = currentVector.z
  201.       bNode.z = currentVector.z
  202.     else
  203.       lNode.z = currentVector.z - 1  
  204.       rNode.z = currentVector.z + 1
  205.       fNode.z = currentVector.z
  206.       bNode.z = currentVector.z
  207.     end
  208.     fNode.y = currentVector.y
  209.     bNode.y = currentVector.y
  210.     lNode.y = currentVector.y
  211.     rNode.y = currentVector.y
  212.     fNodeKey = string.format("%d,%d,%d", fNode.x, fNode.y, fNode.z)
  213.     frNodeKey = string.format("%d,%d,%d", fNode.x, fNode.y, rNode.z)
  214.     rNodeKey = string.format("%d,%d,%d", rNode.x, rNode.y, rNode.z)
  215.     rrNodeKey = string.format("%d,%d,%d", bNode.x, bNode.y, rNode.z)
  216.     bNodeKey = string.format("%d,%d,%d", bNode.x, bNode.y, bNode.z)
  217.     lrNodeKey = string.format("%d,%d,%d", bNode.x, lNode.y, lNode.z)
  218.     lNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, lNode.z)
  219.     flNodeKey = string.format("%d,%d,%d", fNode.x, lNode.y, lNode.z)
  220.     uNodeKey = string.format("%d,%d,%d", uNode.x, uNode.y, uNode.z)
  221.     dNodeKey = string.format("%d,%d,%d", dNode.x, dNode.y, dNode.z)
  222.     fuNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, fNode.z)
  223.     fruNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, rNode.z)
  224.     ruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, rNode.z)
  225.     rruNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, rNode.z)
  226.     buNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, bNode.z)
  227.     lruNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, lNode.z)
  228.     luNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, lNode.z)
  229.     fluNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, fNode.z)
  230.     fdNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, fNode.z)
  231.     frdNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, rNode.z)
  232.     rdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, rNode.z)
  233.     rrdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, rNode.z)
  234.     bdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, bNode.z)
  235.     lrdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, lNode.z)
  236.     ldNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, lNode.z)
  237.     fldNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, lNode.z)
  238.   elseif facingDirection == 2 then  
  239.     if currentVector.x <= 0 then
  240.       rNode.x = currentVector.x + 1
  241.       lNode.x = currentVector.x - 1
  242.       fNode.x = currentVector.x
  243.       bNode.x = currentVector.x
  244.     else
  245.       rNode.x = currentVector.x - 1
  246.       lNode.x = currentVector.x + 1
  247.       fNode.x = currentVector.x
  248.       bNode.x = currentVector.x
  249.     end
  250.     if currentVector.z <=0 then
  251.       fNode.z = currentVector.z - 1
  252.       bNode.z = currentVector.z + 1
  253.       lNode.z = currentVector.z
  254.       rNode.z = currentVector.z
  255.     else
  256.       fNode.z = currentVector.z + 1
  257.       bNode.z = currentVector.z - 1
  258.       lNode.z = currentVector.z
  259.       rNode.z = currentVector.z
  260.     end
  261.     fNode.y = currentVector.y
  262.     bNode.y = currentVector.y
  263.     lNode.y = currentVector.y
  264.     rNode.y = currentVector.y
  265.     fNodeKey = string.format("%d,%d,%d", fNode.x, fNode.y, fNode.z)
  266.     frNodeKey = string.format("%d,%d,%d", rNode.x, fNode.y, fNode.z)
  267.     rNodeKey = string.format("%d,%d,%d", rNode.x, rNode.y, rNode.z)
  268.     rrNodeKey = string.format("%d,%d,%d", rNode.x, bNode.y, bNode.z)
  269.     bNodeKey = string.format("%d,%d,%d", bNode.x, bNode.y, bNode.z)
  270.     lrNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, bNode.z)
  271.     lNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, lNode.z)
  272.     flNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, fNode.z)
  273.     uNodeKey = string.format("%d,%d,%d", uNode.x, uNode.y, uNode.z)
  274.     dNodeKey = string.format("%d,%d,%d", dNode.x, dNode.y, dNode.z)
  275.     fuNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, fNode.z)
  276.     fruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, fNode.z)
  277.     ruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, rNode.z)
  278.     rruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, bNode.z)
  279.     buNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, bNode.z)
  280.     lruNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, bNode.z)
  281.     luNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, lNode.z)
  282.     fluNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, fNode.z)
  283.     fdNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, fNode.z)
  284.     frdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, fNode.z)
  285.     rdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, rNode.z)
  286.     rrdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, bNode.z)
  287.     bdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, bNode.z)
  288.     lrdNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, bNode.z)
  289.     ldNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, lNode.z)
  290.     fldNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, fNode.z)
  291.   elseif facingDirection == 3 then
  292.     if currentVector.x <= 0 then
  293.       fNode.x = currentVector.x + 1
  294.       bNode.x = currentVector.x - 1
  295.       lNode.x = currentVector.x
  296.       rNode.x = currentVector.x
  297.     else
  298.       fNode.x = currentVector.x - 1
  299.       bNode.x = currentVector.x + 1
  300.       lNode.x = currentVector.x
  301.       rNode.x = currentVector.x
  302.     end
  303.     if currentVector.z <=0 then
  304.       lNode.z = currentVector.z - 1
  305.       rNode.z = currentVector.z + 1
  306.       fNode.z = currentVector.z
  307.       bNode.z = currentVector.z
  308.     else
  309.       lNode.z = currentVector.z + 1
  310.       rNode.z = currentVector.z - 1
  311.       fNode.z = currentVector.z
  312.       bNode.z = currentVector.z
  313.     end
  314.     fNode.y = currentVector.y
  315.     bNode.y = currentVector.y
  316.     lNode.y = currentVector.y
  317.     rNode.y = currentVector.y
  318.     fNodeKey = string.format("%d,%d,%d", fNode.x, fNode.y, fNode.z)
  319.     frNodeKey = string.format("%d,%d,%d", fNode.x, fNode.y, rNode.z)
  320.     rNodeKey = string.format("%d,%d,%d", rNode.x, rNode.y, rNode.z)
  321.     rrNodeKey = string.format("%d,%d,%d", bNode.x, bNode.y, rNode.z)
  322.     bNodeKey = string.format("%d,%d,%d", bNode.x, bNode.y, bNode.z)
  323.     lrNodeKey = string.format("%d,%d,%d", bNode.x, lNode.y, lNode.z)
  324.     lNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, lNode.z)
  325.     flNodeKey = string.format("%d,%d,%d", fNode.x, lNode.y, lNode.z)
  326.     uNodeKey = string.format("%d,%d,%d", uNode.x, uNode.y, uNode.z)
  327.     dNodeKey = string.format("%d,%d,%d", dNode.x, dNode.y, dNode.z)
  328.     fuNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, fNode.z)
  329.     fruNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, rNode.z)
  330.     ruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, rNode.z)
  331.     rruNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, rNode.z)
  332.     buNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, bNode.z)
  333.     lruNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, lNode.z)
  334.     luNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, lNode.z)
  335.     fluNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, fNode.z)
  336.     fdNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, fNode.z)
  337.     frdNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, rNode.z)
  338.     rdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, rNode.z)
  339.     rrdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, rNode.z)
  340.     bdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, bNode.z)
  341.     lrdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, lNode.z)
  342.     ldNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, lNode.z)
  343.     fldNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, lNode.z)
  344.  
  345.   elseif facingDirection == 4 then
  346.     if currentVector.x <= 0 then
  347.       lNode.x = currentVector.x + 1
  348.       rNode.x = currentVector.x - 1
  349.       fNode.x = currentVector.x
  350.       bNode.x = currentVector.x
  351.       else
  352.       rNode.x = currentVector.x + 1
  353.       lNode.x = currentVector.x - 1
  354.       fNode.x = currentVector.x
  355.       bNode.x = currentVector.x
  356.     end
  357.     if currentVector.z >=0 then
  358.       fNode.z = currentVector.z - 1
  359.       bNode.z = currentVector.z + 1
  360.       lNode.z = currentVector.z
  361.       rNode.z = currentVector.z
  362.       else
  363.       fNode.z = currentVector.z + 1
  364.       bNode.z = currentVector.z - 1
  365.       lNode.z = currentVector.z
  366.       rNode.z = currentVector.z
  367.     end
  368.     fNode.y = currentVector.y
  369.     bNode.y = currentVector.y
  370.     lNode.y = currentVector.y
  371.     rNode.y = currentVector.y
  372.     fNodeKey = string.format("%d,%d,%d", fNode.x, fNode.y, fNode.z)
  373.     frNodeKey = string.format("%d,%d,%d", rNode.x, fNode.y, fNode.z)
  374.     rNodeKey = string.format("%d,%d,%d", rNode.x, rNode.y, rNode.z)
  375.     rrNodeKey = string.format("%d,%d,%d", rNode.x, bNode.y, bNode.z)
  376.     bNodeKey = string.format("%d,%d,%d", bNode.x, bNode.y, bNode.z)
  377.     lrNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, bNode.z)
  378.     lNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, lNode.z)
  379.     flNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, fNode.z)
  380.     uNodeKey = string.format("%d,%d,%d", uNode.x, uNode.y, uNode.z)
  381.     dNodeKey = string.format("%d,%d,%d", dNode.x, dNode.y, dNode.z)
  382.     fuNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, fNode.z)
  383.     fruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, fNode.z)
  384.     ruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, rNode.z)
  385.     rruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, bNode.z)
  386.     buNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, bNode.z)
  387.     lruNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, bNode.z)
  388.     luNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, lNode.z)
  389.     fluNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, fNode.z)
  390.     fdNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, fNode.z)
  391.     frdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, fNode.z)
  392.     rdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, rNode.z)
  393.     rrdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, bNode.z)
  394.     bdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, bNode.z)
  395.     lrdNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, bNode.z)
  396.     ldNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, lNode.z)
  397.     fldNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, fNode.z)
  398.   end
  399.   log(string.format("fNode:%s",fNodeKey))
  400.   log(string.format("frNode:%s",frNodeKey))
  401.   log(string.format("rNode:%s",rNodeKey))
  402.   log(string.format("rrNode:%s",rrNodeKey))
  403.   log(string.format("bNode:%s",bNodeKey))
  404.   log(string.format("lrNode:%s",lrNodeKey))
  405.   log(string.format("lNode:%s",lNodeKey))
  406.   log(string.format("flNode:%s",flNodeKey))
  407.   log(string.format("uNode:%s",uNodeKey))
  408.   log(string.format("dNode:%s",dNodeKey))
  409.   log(string.format("fuNode:%s",fuNodeKey))
  410.   log(string.format("fruNode:%s",fruNodeKey))
  411.   log(string.format("ruNode:%s",ruNodeKey))
  412.   log(string.format("rruNode:%s",rruNodeKey))
  413.   log(string.format("buNode:%s",buNodeKey))
  414.   log(string.format("lruNode:%s",lruNodeKey))
  415.   log(string.format("luNode:%s",luNodeKey))
  416.   log(string.format("fluNode:%s",fluNodeKey))
  417.   log(string.format("fdNode:%s",fdNodeKey))
  418.   log(string.format("frdNode:%s",frdNodeKey))
  419.   log(string.format("rdNode:%s",rdNodeKey))
  420.   log(string.format("rrdNode:%s",rrdNodeKey))
  421.   log(string.format("bdNode:%s",bdNodeKey))
  422.   log(string.format("lrdNode:%s",lrdNodeKey))
  423.   log(string.format("ldNode:%s",ldNodeKey))
  424.   log(string.format("fldNode:%s",fldNodeKey))
  425.  
  426.   if not parentVector then
  427.     pNodeKey = string.format("%d,%d,%d", currentVector.x, currentVector.y, currentVector.z)
  428.   else
  429.     pNodeKey = string.format("%d,%d,%d", parentVector.x, parentVector.y, parentVector.z)
  430.   end
  431.   if not nodeList[pNodeKey] then -- this should only happen on the first pass when seekDestination is called. Our last pass currentVector is passed as our parent thereafter.
  432.     log(string.format("Adding pNode:%s to nodeList",pNodeKey))
  433.     nodeList[pNodeKey] = {}
  434.     nodeList[pNodeKey].x = currentVector.x
  435.     nodeList[pNodeKey].y = currentVector.y
  436.     nodeList[pNodeKey].z = currentVector.z
  437.     nodeList[pNodeKey].g = 10
  438.     nodeList[pNodeKey].startx = startVector.x
  439.     nodeList[pNodeKey].starty = startVector.y
  440.     nodeList[pNodeKey].startz = startVector.z
  441.     nodeList[pNodeKey].endx = targetVector.x
  442.     nodeList[pNodeKey].endy = targetVector.y
  443.     nodeList[pNodeKey].endz = targetVector.z
  444.     nodeList[pNodeKey].h = math.abs(nodeList[pNodeKey].x - nodeList[pNodeKey].endx) +  math.abs(nodeList[pNodeKey].y-nodeList[pNodeKey].endy) + math.abs(nodeList[pNodeKey].z-nodeList[pNodeKey].endz)
  445.     nodeList[pNodeKey].f = nodeList[pNodeKey].g + nodeList[pNodeKey].h
  446.     nodeList[pNodeKey].state = "closed"
  447.   elseif nodeList[pNodeKey].state == "open" then
  448.     log("OPEN pNode exists in nodeList. Marking as closed")  
  449.     nodeList[pNodeKey].state = "closed"
  450.   end
  451.   if not nodeList[cNodeKey] then -- this should only happen on the first pass when seekDestination is called. Parent and Current are equal on this pass. One of our last pass nodes should become our current node thereafter.
  452.     log(string.format("Adding cNode:%s to nodeList",cNodeKey))
  453.     nodeList[cNodeKey] = {}
  454.     nodeList[cNodeKey].x = currentVector.x
  455.     nodeList[cNodeKey].y = currentVector.y
  456.     nodeList[cNodeKey].z = currentVector.z
  457.     nodeList[cNodeKey].g = 10
  458.     nodeList[cNodeKey].startx = startVector.x
  459.     nodeList[cNodeKey].starty = startVector.y
  460.     nodeList[cNodeKey].startz = startVector.z
  461.     nodeList[cNodeKey].endx = targetVector.x
  462.     nodeList[cNodeKey].endy = targetVector.y
  463.     nodeList[cNodeKey].endz = targetVector.z
  464.     nodeList[cNodeKey].h = math.abs(nodeList[pNodeKey].x - nodeList[cNodeKey].endx) +  math.abs(nodeList[pNodeKey].y-nodeList[cNodeKey].endy) + math.abs(nodeList[pNodeKey].z-nodeList[cNodeKey].endz)
  465.     nodeList[cNodeKey].f = nodeList[cNodeKey].g + nodeList[cNodeKey].h
  466.     nodeList[cNodeKey].state = "closed"
  467.   elseif nodeList[cNodeKey].state == "open" then
  468.     log("cNode exists in nodeList. Marking as closed")  
  469.     nodeList[cNodeKey].state = "closed"
  470.   end
  471.   if not nodeList[fNodeKey] then
  472.     log(string.format("Adding fNode:%s to nodeList",fNodeKey))
  473.     nodeList[fNodeKey] = {}
  474.     nodeList[fNodeKey].x = fNode.x
  475.     nodeList[fNodeKey].y = fNode.y
  476.     nodeList[fNodeKey].z = fNode.z
  477.     nodeList[fNodeKey].startx = startVector.x
  478.     nodeList[fNodeKey].starty = startVector.y
  479.     nodeList[fNodeKey].startz = startVector.z
  480.     nodeList[fNodeKey].endx = targetVector.x
  481.     nodeList[fNodeKey].endy = targetVector.y
  482.     nodeList[fNodeKey].endz = targetVector.z
  483.     nodeList[fNodeKey].g = math.abs((nodeList[cNodeKey].x-nodeList[fNodeKey].x) + (nodeList[cNodeKey].y-nodeList[fNodeKey].y) + (nodeList[cNodeKey].z-nodeList[fNodeKey].z)) + nodeList[pNodeKey].g
  484.     nodeList[fNodeKey].h = math.abs(nodeList[fNodeKey].x - nodeList[fNodeKey].endx) +  math.abs(nodeList[fNodeKey].y-nodeList[fNodeKey].endy) + math.abs(nodeList[fNodeKey].z-nodeList[fNodeKey].endz)
  485.     nodeList[fNodeKey].f = nodeList[fNodeKey].g + nodeList[fNodeKey].h
  486.     nodeList[fNodeKey].state = "open"
  487.   elseif nodeList[fNodeKey].state == "open" then
  488.     log("fNode exists. Updating Parent node.")
  489.     --put logic here to recalc g if needed.
  490.   end
  491.   if not nodeList[uNodeKey] then
  492.     log(string.format("Adding uNode:%s to nodeList",uNodeKey))
  493.     nodeList[uNodeKey] = {}
  494.     nodeList[uNodeKey].x = uNode.x
  495.     nodeList[uNodeKey].y = uNode.y
  496.     nodeList[uNodeKey].z = uNode.z
  497.     nodeList[uNodeKey].startx = startVector.x
  498.     nodeList[uNodeKey].starty = startVector.y
  499.     nodeList[uNodeKey].startz = startVector.z
  500.     nodeList[uNodeKey].endx = targetVector.x
  501.     nodeList[uNodeKey].endy = targetVector.y
  502.     nodeList[uNodeKey].endz = targetVector.z
  503.     nodeList[uNodeKey].g = math.abs((nodeList[pNodeKey].x-nodeList[uNodeKey].x) + (nodeList[pNodeKey].y-nodeList[uNodeKey].y) + (nodeList[pNodeKey].z-nodeList[uNodeKey].z)) + nodeList[pNodeKey].g
  504.     nodeList[uNodeKey].h = math.abs(nodeList[uNodeKey].x - nodeList[uNodeKey].endx) +  math.abs(nodeList[uNodeKey].y-nodeList[uNodeKey].endy) + math.abs(nodeList[uNodeKey].z-nodeList[uNodeKey].endz)
  505.     nodeList[uNodeKey].f = nodeList[uNodeKey].g + nodeList[uNodeKey].h
  506.     nodeList[uNodeKey].state = "open"
  507.   elseif nodeList[uNodeKey].state == "open" then
  508.     log("uNode exists. Updating Parent node.")
  509.     --put logic here to recalc g if needed.
  510.   end
  511.   if not nodeList[dNodeKey] then
  512.     log(string.format("Adding dNode:%s to nodeList",dNodeKey))
  513.     nodeList[dNodeKey] = {}
  514.     nodeList[dNodeKey].x = dNode.x
  515.     nodeList[dNodeKey].y = dNode.y
  516.     nodeList[dNodeKey].z = dNode.z
  517.     nodeList[dNodeKey].startx = startVector.x
  518.     nodeList[dNodeKey].starty = startVector.y
  519.     nodeList[dNodeKey].startz = startVector.z
  520.     nodeList[dNodeKey].endx = targetVector.x
  521.     nodeList[dNodeKey].endy = targetVector.y
  522.     nodeList[dNodeKey].endz = targetVector.z
  523.     nodeList[dNodeKey].g = math.abs((nodeList[pNodeKey].x-nodeList[dNodeKey].x) + (nodeList[pNodeKey].y-nodeList[dNodeKey].y) + (nodeList[pNodeKey].z-nodeList[dNodeKey].z)) + nodeList[pNodeKey].g
  524.     nodeList[dNodeKey].h = math.abs(nodeList[dNodeKey].x - nodeList[dNodeKey].endx) +  math.abs(nodeList[dNodeKey].y-nodeList[dNodeKey].endy) + math.abs(nodeList[dNodeKey].z-nodeList[dNodeKey].endz)
  525.     nodeList[dNodeKey].f = nodeList[dNodeKey].g + nodeList[dNodeKey].h
  526.     nodeList[dNodeKey].state = "open"
  527.   elseif nodeList[dNodeKey].state == "open" then
  528.     log("dNode exists. Updating Parent node.")
  529.     --put logic here to recalc g if needed.
  530.   end
  531.   if not nodeList[rNodeKey] then
  532.     log(string.format("Adding rNode:%s to nodeList",rNodeKey))
  533.     nodeList[rNodeKey] = {}
  534.     nodeList[rNodeKey].x = rNode.x
  535.     nodeList[rNodeKey].y = rNode.y
  536.     nodeList[rNodeKey].z = rNode.z
  537.     nodeList[rNodeKey].startx = startVector.x
  538.     nodeList[rNodeKey].starty = startVector.y
  539.     nodeList[rNodeKey].startz = startVector.z
  540.     nodeList[rNodeKey].endx = targetVector.x
  541.     nodeList[rNodeKey].endy = targetVector.y
  542.     nodeList[rNodeKey].endz = targetVector.z
  543.     nodeList[rNodeKey].g = math.abs((nodeList[pNodeKey].x-nodeList[rNodeKey].x) + (nodeList[pNodeKey].y-nodeList[rNodeKey].y) + (nodeList[pNodeKey].z-nodeList[rNodeKey].z)) + nodeList[pNodeKey].g
  544.     nodeList[rNodeKey].h = math.abs(nodeList[rNodeKey].x - nodeList[rNodeKey].endx) +  math.abs(nodeList[rNodeKey].y-nodeList[rNodeKey].endy) + math.abs(nodeList[rNodeKey].z-nodeList[rNodeKey].endz)
  545.     nodeList[rNodeKey].f = nodeList[rNodeKey].g + nodeList[rNodeKey].h
  546.     nodeList[rNodeKey].state = "open"
  547.   elseif nodeList[rNodeKey].state == "open" then
  548.     log("rNode exists. Updating Parent node.")
  549.     --put logic here to recalc g if needed.
  550.   end
  551.   if not nodeList[bNodeKey] then
  552.     log(string.format("Adding bNode:%s to nodeList",bNodeKey))
  553.     nodeList[bNodeKey] = {}
  554.     nodeList[bNodeKey].x = bNode.x
  555.     nodeList[bNodeKey].y = bNode.y
  556.     nodeList[bNodeKey].z = bNode.z
  557.     nodeList[bNodeKey].startx = startVector.x
  558.     nodeList[bNodeKey].starty = startVector.y
  559.     nodeList[bNodeKey].startz = startVector.z
  560.     nodeList[bNodeKey].endx = targetVector.x
  561.     nodeList[bNodeKey].endy = targetVector.y
  562.     nodeList[bNodeKey].endz = targetVector.z
  563.     nodeList[bNodeKey].g = math.abs((nodeList[pNodeKey].x-nodeList[bNodeKey].x) + (nodeList[pNodeKey].y-nodeList[bNodeKey].y) + (nodeList[pNodeKey].z-nodeList[bNodeKey].z)) + nodeList[pNodeKey].g
  564.     nodeList[bNodeKey].h = math.abs(nodeList[bNodeKey].x - nodeList[bNodeKey].endx) +  math.abs(nodeList[bNodeKey].y-nodeList[bNodeKey].endy) + math.abs(nodeList[bNodeKey].z-nodeList[bNodeKey].endz)
  565.     nodeList[bNodeKey].f = nodeList[bNodeKey].g + nodeList[bNodeKey].h
  566.     nodeList[bNodeKey].state = "open"
  567.   elseif nodeList[bNodeKey].state == "open" then
  568.     log("bNode exists. Updating Parent node.")
  569.     --put logic here to recalc g if needed.
  570.   end
  571.   if not nodeList[lNodeKey] then
  572.     log(string.format("Adding lNode:%s to nodeList",lNodeKey))
  573.     nodeList[lNodeKey] = {}
  574.     nodeList[lNodeKey].x = lNode.x
  575.     nodeList[lNodeKey].y = lNode.y
  576.     nodeList[lNodeKey].z = lNode.z
  577.     nodeList[lNodeKey].startx = startVector.x
  578.     nodeList[lNodeKey].starty = startVector.y
  579.     nodeList[lNodeKey].startz = startVector.z
  580.     nodeList[lNodeKey].endx = targetVector.x
  581.     nodeList[lNodeKey].endy = targetVector.y
  582.     nodeList[lNodeKey].endz = targetVector.z
  583.     nodeList[lNodeKey].g = math.abs((nodeList[pNodeKey].x-nodeList[lNodeKey].x) + (nodeList[pNodeKey].y-nodeList[lNodeKey].y) + (nodeList[pNodeKey].z-nodeList[lNodeKey].z)) + nodeList[pNodeKey].g
  584.     nodeList[lNodeKey].h = math.abs(nodeList[lNodeKey].x - nodeList[lNodeKey].endx) +  math.abs(nodeList[lNodeKey].y-nodeList[lNodeKey].endy) + math.abs(nodeList[lNodeKey].z-nodeList[lNodeKey].endz)
  585.     nodeList[lNodeKey].f = nodeList[lNodeKey].g + nodeList[lNodeKey].h
  586.     nodeList[lNodeKey].state = "open"
  587.   elseif nodeList[lNodeKey].state == "open" then
  588.     log("lNode exists. Updating Parent node.")
  589.     --put logic here to recalc g if needed.
  590.   end
  591.   if nodeList[fuNodeKey] then
  592.     log("fuNode exists. Updating g.")
  593.     nodeList[fuNodeKey].g = nodeList[fuNodeKey].g + nodeList[cNodeKey].g
  594.     nodeList[fuNodeKey].f = nodeList[fuNodeKey].g + nodeList[fuNodeKey].h
  595.   end
  596.   if nodeList[fdNodeKey] then
  597.     log("fdNode exists. Updating g.")
  598.     nodeList[fdNodeKey].g = nodeList[fdNodeKey].g + nodeList[cNodeKey].g
  599.     nodeList[fdNodeKey].f = nodeList[fdNodeKey].g + nodeList[fdNodeKey].h
  600.   end
  601.   if nodeList[frNodeKey] then
  602.     log("frNode exists. Updating g.")
  603.     nodeList[frNodeKey].g = nodeList[frNodeKey].g + nodeList[cNodeKey].g
  604.     nodeList[frNodeKey].f = nodeList[frNodeKey].g + nodeList[frNodeKey].h
  605.   end
  606.   if nodeList[fruNodeKey] then
  607.     log("fruNode exists. Updating g.")
  608.     nodeList[fruNodeKey].g = nodeList[fruNodeKey].g + nodeList[cNodeKey].g
  609.     nodeList[fruNodeKey].f = nodeList[fruNodeKey].g + nodeList[fruNodeKey].h
  610.   end
  611.   if nodeList[frdNodeKey] then
  612.     log("frdNode exists. Updating g.")
  613.     nodeList[frdNodeKey].g = nodeList[frdNodeKey].g + nodeList[cNodeKey].g
  614.     nodeList[frdNodeKey].f = nodeList[frdNodeKey].g + nodeList[frdNodeKey].h
  615.   end  
  616.   if nodeList[ruNodeKey] then
  617.     log("ruNode exists. Updating g.")
  618.     nodeList[ruNodeKey].g = nodeList[ruNodeKey].g + nodeList[cNodeKey].g
  619.     nodeList[ruNodeKey].f = nodeList[ruNodeKey].g + nodeList[ruNodeKey].h
  620.   end
  621.   if nodeList[rdNodeKey] then
  622.     log("rdNode exists. Updating g.")
  623.     nodeList[rdNodeKey].g = nodeList[rdNodeKey].g + nodeList[cNodeKey].g
  624.     nodeList[rdNodeKey].f = nodeList[rdNodeKey].g + nodeList[rdNodeKey].h
  625.   end  
  626.   if nodeList[rrNodeKey] then
  627.     log("rrNode exists. Updating g.")
  628.     nodeList[rrNodeKey].g = nodeList[rrNodeKey].g + nodeList[cNodeKey].g
  629.     nodeList[rrNodeKey].f = nodeList[rrNodeKey].g + nodeList[rrNodeKey].h
  630.   end
  631.   if nodeList[rruNodeKey] then
  632.     log("rruNode exists. Updating g.")
  633.     nodeList[rruNodeKey].g = nodeList[rruNodeKey].g + nodeList[cNodeKey].g
  634.     nodeList[rruNodeKey].f = nodeList[rruNodeKey].g + nodeList[rruNodeKey].h
  635.   end
  636.   if nodeList[rrdNodeKey] then
  637.     log("rrdNode exists. Updating g.")
  638.     nodeList[rrdNodeKey].g = nodeList[rrdNodeKey].g + nodeList[cNodeKey].g
  639.     nodeList[rrdNodeKey].f = nodeList[rrdNodeKey].g + nodeList[rrdNodeKey].h
  640.   end  
  641.   if nodeList[buNodeKey] then
  642.     log("buNode exists. Updating g.")  
  643.     nodeList[buNodeKey].g = nodeList[buNodeKey].g + nodeList[cNodeKey].g
  644.     nodeList[buNodeKey].f = nodeList[buNodeKey].g + nodeList[buNodeKey].h  
  645.   end
  646.   if nodeList[bdNodeKey] then
  647.     log("bdNode exists. Updating g.")  
  648.     nodeList[bdNodeKey].g = nodeList[bdNodeKey].g + nodeList[cNodeKey].g
  649.     nodeList[bdNodeKey].f = nodeList[bdNodeKey].g + nodeList[bdNodeKey].h  
  650.   end
  651.   if nodeList[lrNodeKey] then
  652.     log("lrNode exists. Updating g.")  
  653.     nodeList[lrNodeKey].g = nodeList[lrNodeKey].g + nodeList[cNodeKey].g
  654.     nodeList[lrNodeKey].f = nodeList[lrNodeKey].g + nodeList[lrNodeKey].h  
  655.   end
  656.   if nodeList[lruNodeKey] then
  657.     log("lruNode exists. Updating g.") 
  658.     nodeList[lruNodeKey].g = nodeList[lruNodeKey].g + nodeList[cNodeKey].g
  659.     nodeList[lruNodeKey].f = nodeList[lruNodeKey].g + nodeList[lruNodeKey].h  
  660.   end  
  661.   if nodeList[lrdNodeKey] then
  662.     log("lrdNode exists. Updating g.") 
  663.     nodeList[lrdNodeKey].g = nodeList[lrdNodeKey].g + nodeList[cNodeKey].g
  664.     nodeList[lrdNodeKey].f = nodeList[lrdNodeKey].g + nodeList[lrdNodeKey].h  
  665.   end
  666.   if nodeList[luNodeKey] then
  667.     log("luNode exists. Updating g.")  
  668.     nodeList[luNodeKey].g = nodeList[luNodeKey].g + nodeList[cNodeKey].g
  669.     nodeList[luNodeKey].f = nodeList[luNodeKey].g + nodeList[luNodeKey].h  
  670.   end
  671.   if nodeList[ldNodeKey] then
  672.     log("ldNode exists. Updating g.")  
  673.     nodeList[ldNodeKey].g = nodeList[ldNodeKey].g + nodeList[cNodeKey].g
  674.     nodeList[ldNodeKey].f = nodeList[ldNodeKey].g + nodeList[ldNodeKey].h  
  675.   end  
  676.   if nodeList[flNodeKey] then
  677.     log("flNode exists. Updating g.")
  678.     nodeList[flNodeKey].g = nodeList[flNodeKey].g + nodeList[cNodeKey].g
  679.     nodeList[flNodeKey].f = nodeList[flNodeKey].g + nodeList[flNodeKey].h
  680.   end
  681.   if nodeList[fluNodeKey] then
  682.     log("fluNode exists. Updating g.")
  683.     nodeList[fluNodeKey].g = nodeList[fluNodeKey].g + nodeList[cNodeKey].g
  684.     nodeList[fluNodeKey].f = nodeList[fluNodeKey].g + nodeList[fluNodeKey].h
  685.   end
  686.   if nodeList[fldNodeKey] then
  687.     log("fldNode exists. Updating g.")
  688.     nodeList[fldNodeKey].g = nodeList[fldNodeKey].g + nodeList[cNodeKey].g
  689.     nodeList[fldNodeKey].f = nodeList[fldNodeKey].g + nodeList[fldNodeKey].h
  690.   end  
  691.    
  692.   log("begingin Node Inspections.")
  693.   log(string.format("Starting Direction:%s", facingDirection))
  694.   local fResult,fInspect = turtle.inspect() -- inspect Left, up and down at no cost.
  695.   local uResult,uInspect = turtle.inspectUp() -- using inspect instead of detect to capture block type
  696.   local dResult,dInspect = turtle.inspectDown() -- might need it later
  697.   --turtle.turnRight()
  698.   rightTurn()
  699.   log(string.format("Facing:%s to inspect rNode", facingDirection))
  700.   local rResult,rInspect = turtle.inspect()  -- time/move cost but no fuel cost so get them all
  701.   --turtle.turnRight()
  702.   rightTurn()
  703.   log(string.format("Facing:%s to inspect bNode", facingDirection))
  704.   local bResult,bInspect = turtle.inspect()
  705.   --turtle.turnRight()
  706.   rightTurn()
  707.   log(string.format("Facing:%s to inspect lNode", facingDirection))
  708.   local lResult,lInspect = turtle.inspect()
  709.   --turtle.turnRight()
  710.   rightTurn()
  711.   log(string.format("Returned to starting direction of: %s ", facingDirection))
  712.   if fResult == true then -- if any block is found close the node and keep the block type
  713.     nodeList[fNodeKey].state = "closed"
  714.     nodeList[fNodeKey].block_type = fInspect.name
  715.   end
  716.   if uResult == true then
  717.     nodeList[uNodeKey].state = "closed"
  718.     nodeList[uNodeKey].block_type = uInspect.name
  719.   end
  720.   if dResult == true then
  721.     nodeList[dNodeKey].state = "closed"
  722.     nodeList[dNodeKey].block_type = dInspect.name
  723.   end
  724.   if rResult == true then
  725.     nodeList[rNodeKey].state = "closed"
  726.     nodeList[rNodeKey].block_type = rInspect.name
  727.   end
  728.   if bResult == true then
  729.     nodeList[bNodeKey].state = "closed"
  730.     nodeList[bNodeKey].block_type = bInspect.name
  731.   end
  732.   if lResult == true then
  733.     nodeList[lNodeKey].state = "closed"
  734.     nodeList[lNodeKey].block_type = lInspect.name  
  735.   end
  736.   log(string.format("fResult= %s ", tostring(fResult)))
  737.   log(string.format("uResult= %s ", tostring(uResult)))
  738.   log(string.format("dResult= %s ", tostring(dResult)))
  739.   log(string.format("rResult= %s ", tostring(rResult)))
  740.   log(string.format("bResult= %s ", tostring(bResult)))
  741.   log(string.format("lResult= %s ", tostring(lResult)))
  742.   for k, v in pairs(nodeList) do -- just cheking that table loaded as expected
  743.     --log(string.format("Node:%s End X:%d End Y:%d End Z:%d", tostring(k), nodeList[k].endx, nodeList[k].endy,nodeList[k].endz))
  744.     log(string.format("%s ; %s ; %d ; %s ; %d", tostring(k), tostring(nodeList[k].state),nodeList[k].f,nodeList[k].g, nodeList[k].h))
  745.   end
  746.   --return nodeList
  747.   return nodeList[parentNodeKey], true
  748. end
  749.  
  750. local function getBestNode(currentVector, targetVector , startVector)
  751.   local bestMove = nil
  752.   for vNode, nTable in pairs(nodeList) do
  753.     if nTable.state == "open" then  
  754.       if bestMove == nil then  
  755.         log(string.format("Setting bestMove to:%s", tostring(vNode)))
  756.         bestMove = vNode
  757.       end
  758.       if nTable.f < nodeList[bestMove].f then  
  759.         bestMove = vNode
  760.       elseif nTable.f == nodeList[bestMove].f then  
  761.         if nTable.h < nodeList[bestMove].h then  
  762.           bestMove = vNode
  763.         elseif nTable.h == nodeList[bestMove].h then
  764.           if nodeList[bestMove].y > currentVector.y then
  765.             if nTable.y == currentVector.y then
  766.               bestMove = vNode
  767.             end
  768.           end
  769.         end
  770.       else
  771.       end
  772.     else
  773.     end
  774.   end
  775.   if nodeList[bestMove].state == "open" then -- make sure the best node is open before finishing
  776.     local bmVector = vector.new(nodeList[bestMove].x,nodeList[bestMove].y,nodeList[bestMove].z)
  777.     return bmVector -- returning node as vector. passing as indect to nodeList did not work
  778.   else
  779.     return false -- if bestMove is not open let caller know.
  780.   end
  781. end
  782.  
  783. local function gotoNode(bnOffset, currentVector)  
  784.   if not facingDirection then
  785.     log("facingDirectin not found in gotoNode(). Calling getFacingDirection()")
  786.     facingDirection = getFacingDirection()  -- using to pick move forward or back
  787.   end
  788.   log(string.format("Facing Direction is :%s", tostring(facingDirection)))
  789.   local moves
  790.   if facingDirection == 1 then
  791.     if bnOffset.x < 0 then
  792.       for moves = 1,math.abs(bnOffset.x) do --offsets should always be 1 but will move more if backtracking
  793.         turtle.forward()
  794.       end
  795.     else
  796.       for moves = 1,math.abs(bnOffset.x) do
  797.         turtle.back()
  798.       end
  799.     end
  800.     if bnOffset.z > 0 then
  801.       leftTurn()
  802.       for moves = 1,math.abs(bnOffset.z) do
  803.           turtle.forward()
  804.         end
  805.       rightTurn()
  806.     else
  807.       rightTurn()
  808.         for moves = 1,math.abs(bnOffset.z) do
  809.           turtle.forward()
  810.         end
  811.       leftTurn()
  812.     end
  813.     if bnOffset.y < 0 then
  814.       for moves = 1,math.abs(bnOffset.y) do
  815.         turtle.down()
  816.       end
  817.     elseif bnOffset.y > 0 then
  818.       for moves = 1,math.abs(bnOffset.z) do
  819.         turtle.up()
  820.       end
  821.     end
  822.   elseif facingDirection == 2 then
  823.     if bnOffset.z > 0 then
  824.       for moves = 1,math.abs(bnOffset.z) do
  825.         turtle.back()
  826.       end
  827.     elseif bnOffset.z < 0 then
  828.       for moves = 1,math.abs(bnOffset.z) do
  829.         turtle.forward()
  830.       end
  831.     end
  832.     if bnOffset.x < 0 then
  833.       --turtle.turnLeft()
  834.       leftTurn()
  835.       for moves = 1,math.abs(bnOffset.x) do
  836.         turtle.forward()
  837.       end
  838.       --turtle.turnRight()
  839.       rightTurn()
  840.     elseif bnOffset.x > 0 then
  841.       --turtle.turnRight()
  842.       rightTurn()
  843.       for moves = 1,math.abs(bnOffset.x) do
  844.         turtle.forward()
  845.       end
  846.       --turtle.turnLeft()
  847.       leftTurn()
  848.     end
  849.     if bnOffset.y < 0 then
  850.       for moves = 1,math.abs(bnOffset.y) do
  851.         turtle.down()
  852.       end
  853.     elseif bnOffset.y > 0 then
  854.       for moves = 1,math.abs(bnOffset.z) do
  855.         turtle.up()
  856.       end
  857.     end
  858.   elseif facingDirection == 3 then
  859.     if bnOffset.x > 0 then
  860.       for moves = 1,math.abs(bnOffset.x) do
  861.         turtle.forward()
  862.       end
  863.     else
  864.       for moves = 1,math.abs(bnOffset.x) do
  865.         turtle.back()
  866.       end
  867.     end
  868.     if bnOffset.z < 0 then
  869.       --turtle.turnLeft()
  870.       leftTurn()
  871.       for moves = 1,math.abs(bnOffset.z) do
  872.         turtle.forward()
  873.       end
  874.       --turtle.turnRight()
  875.       rightTurn()
  876.     else
  877.       --turtle.turnRight()
  878.       rightTurn()
  879.       for moves = 1,math.abs(bnOffset.z) do
  880.         turtle.forward()
  881.       end
  882.       --turtle.turnLeft()
  883.       leftTurn()
  884.     end
  885.     if bnOffset.y < 0 then
  886.       for moves = 1,math.abs(bnOffset.y) do
  887.         turtle.down()
  888.       end
  889.     elseif bnOffset.y > 0 then
  890.       for moves = 1,math.abs(bnOffset.z) do
  891.         turtle.up()
  892.       end
  893.     end
  894.   elseif facingDirection == 4 then
  895.     if bnOffset.z < 0 then
  896.       for moves = 1,math.abs(bnOffset.z) do
  897.         turtle.back()        
  898.       end
  899.     elseif bnOffset.z > 0 then
  900.       for moves = 1,math.abs(bnOffset.z) do
  901.         turtle.forward()        
  902.       end
  903.     end
  904.     if bnOffset.x > 0 then
  905.       --turtle.turnLeft()
  906.       leftTurn()
  907.       for moves = 1,math.abs(bnOffset.x) do
  908.         turtle.forward()
  909.       end
  910.       --turtle.turnRight()
  911.       rightTurn()
  912.     elseif bnOffset.x < 0 then
  913.       --turtle.turnRight()
  914.       rightTurn()
  915.       for moves = 1,math.abs(bnOffset.x) do
  916.         turtle.forward()
  917.       end
  918.       --turtle.turnLeft()
  919.       leftTurn()
  920.     end
  921.     if bnOffset.y < 0 then
  922.       for moves = 1,math.abs(bnOffset.y) do
  923.         turtle.down()
  924.       end
  925.     elseif bnOffset.y > 0 then
  926.       for moves = 1,math.abs(bnOffset.z) do
  927.         turtle.up()
  928.       end
  929.     end
  930.   end
  931.  
  932. end
  933.  
  934. local function seekDestination(currentVector, targetVector)
  935.   local hDisplacement
  936.   local moveCount = 0
  937.   local startVector = currentVector
  938.   local parentVector = currentVector
  939.   log(string.format("Current X:%s, Current Y:%s, Current Z:%s, Target X:%s,Target Y:%s,Target Z:%s", tostring(currentVector.x), tostring(currentVector.y), tostring(currentVector.z), tostring(targetVector.x), tostring(targetVector.y), tostring(targetVector.z)))
  940.   while currentVector.x ~= targetVector.x and currentVector.z ~= targetVector.z and moveCount < 21 do
  941.     moveCount = moveCount + 1
  942.     log(string.format("Starting move #%d Facing Direction:%s", moveCount, facingDirection))
  943.     log(string.format("Current X:%s, Current Y:%s, Current Z:%s, Target X:%s,Target Y:%s,Target Z:%s", tostring(currentVector.x), tostring(currentVector.y), tostring(currentVector.z), tostring(targetVector.x), tostring(targetVector.y), tostring(targetVector.z)))
  944.     local lastParentNode, nodeDataStatus = getNodeData(currentVector, targetVector, startVector, parentVector)
  945.     log(string.format("getNodeData returned %s", tostring(nodeDataStatus)))
  946.     local bestMoveVector = getBestNode(currentVector, targetVector , startVector)
  947.     log(string.format("getBestNode returned %s", tostring(bestMoveVector)))
  948.     if bestMoveVector then
  949.       log("Calculating BestMove Offset")
  950.       local bestMoveOffset = bestMoveVector - currentVector
  951.       log(string.format("Offset of %s beween bestmove:%s and current%s", tostring(bestMoveOffset),tostring(bestMoveVector), tostring(currentVector)))
  952.       local bmResult = gotoNode(bestMoveOffset, currentVector)
  953.       log(string.format("gotoNode returned %s", tostring(bmResult)))
  954.       parentVector = currentVector
  955.       log(string.format("Parent Vector set to %s", tostring(parentVector)))
  956.     else
  957.       log("No Best Node Returned")
  958.     end
  959.     currentVector = vector.new(gps.locate(5))
  960.     log(string.format("new current Vector set to %s", tostring(currentVector)))
  961.   end
  962. end
  963. --************Starting Seek*************
  964. facingDirection  = getFacingDirection()
  965. nodeList = {}
  966. cVector = vector.new(gps.locate(5))
  967. sVector = cVector
  968. targetVector = vector.new(-12, 75,-562)
  969. m = seekDestination(cVector,targetVector,sVector)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement