Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function getFacingDirection()
- local function findClearBlock()
- local turnsToClear = 0
- while turtle.inspect() and turnsToClear <=4 do
- turtle.turnRight()
- turnsToClear = turnsToClear + 1
- end
- if turnsToClear >= 4 then
- return nil
- else
- end
- if turnsToClear ~=4 and turnsToClear ~=0 then
- for faceHome = 1, turnsToClear do
- turtle.turnLeft()
- end
- end
- return turnsToClear
- end
- local startLoc = vector.new(gps.locate())
- local dirLoc
- local facingDir = 0
- local facingVector
- if turtle.forward() then
- dirLoc = vector.new(gps.locate())
- turtle.back()
- facingVector = dirLoc - startLoc
- facingDir = ((facingVector.x + math.abs(facingVector.x) * 2) + (facingVector.z + math.abs(facingVector.z) * 3))
- else
- local tCount = findClearBlock()
- if tCount == nil then
- return nil
- elseif tCount == 2 then
- turtle.back()
- dirLoc = vector.new(gps.locate())
- turtle.forward()
- facingVector = startLoc - dirLoc
- facingDir = (((facingVector.x + math.abs(facingVector.x) * 2) + (facingVector.z + math.abs(facingVector.z) * 3)))
- elseif tCount == 1 then
- turtle.turnRight()
- turtle.forward()
- turtle.turnLeft()
- dirLoc = vector.new(gps.locate())
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- facingVector = dirLoc - startLoc
- facingDir = (((facingVector.x + math.abs(facingVector.x) * 2) + (facingVector.z + math.abs(facingVector.z) * 3))) - 1
- elseif
- tCount == 3 then
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- dirLoc = vector.new(gps.locate())
- turtle.turnRight()
- turtle.forward()
- turtle.turnLeft()
- facingVector = dirLoc - startLoc
- facingDir = ((facingVector.x + math.abs(facingVector.x) * 2) + (facingVector.z + math.abs(facingVector.z) * 3)) + 1
- if facingDir > 4 then
- facingDir = 1
- end
- end
- end
- return facingDir
- end
- local function rightTurn()
- if not facingDirection then
- facingDirection = getFacingDirection()
- end
- turtle.turnRight()
- facingDirection = facingDirection + 1
- if facingDirection > 4 then
- facingDirection = 1
- end
- end
- local function leftTurn()
- if not facingDirection then
- facingDirection = getFacingDirection()
- end
- turtle.turnLeft()
- facingDirection = facingDirection - 1
- if facingDirection < 1 then
- facingDirection = 4
- end
- end
- local function getNodeData(currentVector, targetVector , startVector, parentVector)
- local cNode, cNodeKey = nil
- local fNode, fNodeKey, fInspect, fResult = nil
- local uNode, uNodeKey, uInspect,uResult = nil
- local dNode, dNodeKey, dInspect, dResult = nil
- local rNode, rNodeKey, rInspect, rResult = nil
- local bNode, bNodeKey, bInspect, bResult = nil
- local lNode, lNodeKey, lInspect, lResult = nil
- local fuNode, fuNodeKey = nil
- local ruNode, ruNodeKey = nil
- local luNode, luNodeKey = nil
- local buNode, buNodeKey = nil
- local fdNode, fdNodeKey = nil
- local rdNode, rdNodeKey = nil
- local bdode, bdNodeKey = nil
- local bdrNode, bdNodeKey = nil
- local frNode, frNodeKey = nil
- local rrNode, rrNodeKey = nil
- local flNode, flNodeKey = nil
- local lrNode, lrNodeKey = nil
- local fruNode, fruNodeKey = nil
- local rruNode, rruNodeKey = nil
- local fluNode, fluNodeKey = nil
- local lruNode, lruNodeKey = nil
- local frdNode, frdNodeKey = nil
- local rrdNode, rrdNodeKey = nil
- local fldNode, fldNodeKey = nil
- local lrdNode, lrdNodeKey = nil
- cNode = {x = 0, y = 0, z = 0}
- fNode = {x = 0, y = 0, z = 0}
- uNode = {x = 0, y = 0, z = 0}
- dNode = {x = 0, y = 0, z = 0}
- rNode = {x = 0, y = 0, z = 0}
- bNode = {x = 0, y = 0, z = 0}
- lNode = {x = 0, y = 0, z = 0}
- frNode = {x = 0, y = 0, z = 0}
- fruNode = {x = 0, y = 0, z = 0}
- frdNode = {x = 0, y = 0, z = 0}
- fuNode = {x = 0, y = 0, z = 0}
- fdNode = {x = 0, y = 0, z = 0}
- flNode = {x = 0, y = 0, z = 0}
- fluNode = {x = 0, y = 0, z = 0}
- fldNode = {x = 0, y = 0, z = 0}
- ruNode = {x = 0, y = 0, z = 0}
- rdNode = {x = 0, y = 0, z = 0}
- rrNode = {x = 0, y = 0, z = 0}
- rruNode = {x = 0, y = 0, z = 0}
- rrdNode = {x = 0, y = 0, z = 0}
- buNode = {x = 0, y = 0, z = 0}
- bdNode = {x = 0, y = 0, z = 0}
- lrNode = {x = 0, y = 0, z = 0}
- lruNode = {x = 0, y = 0, z = 0}
- lrdNode = {x = 0, y = 0, z = 0}
- luNode = {x = 0, y = 0, z = 0}
- ldNode = {x = 0, y = 0, z = 0}
- if not facingDirection then
- facingDirection = getFacingDirection()
- end
- uNode.x = currentVector.x
- uNode.z = currentVector.z
- uNode.y = currentVector.y + 1
- dNode.x = currentVector.x
- dNode.z = currentVector.z
- dNode.y = currentVector.y - 1
- cNodeKey = string.format("%d,%d,%d", currentVector.x, currentVector.y, currentVector.z)
- if facingDirection == 1 then
- if currentVector.x <= 0 then
- fNode.x = currentVector.x - 1
- bNode.x = currentVector.x + 1
- lNode.x = currentVector.x
- rNode.x = currentVector.x
- else
- fNode.x = currentVector.x + 1
- bNode.x = currentVector.x - 1
- lNode.x = currentVector.x
- rNode.x = currentVector.x
- end
- if currentVector.z <= 0 then
- lNode.z = currentVector.z + 1
- rNode.z = currentVector.z - 1
- fNode.z = currentVector.z
- bNode.z = currentVector.z
- else
- lNode.z = currentVector.z - 1
- rNode.z = currentVector.z + 1
- fNode.z = currentVector.z
- bNode.z = currentVector.z
- end
- fNode.y = currentVector.y
- bNode.y = currentVector.y
- lNode.y = currentVector.y
- rNode.y = currentVector.y
- fNodeKey = string.format("%d,%d,%d", fNode.x, fNode.y, fNode.z)
- frNodeKey = string.format("%d,%d,%d", fNode.x, fNode.y, rNode.z)
- rNodeKey = string.format("%d,%d,%d", rNode.x, rNode.y, rNode.z)
- rrNodeKey = string.format("%d,%d,%d", bNode.x, bNode.y, rNode.z)
- bNodeKey = string.format("%d,%d,%d", bNode.x, bNode.y, bNode.z)
- lrNodeKey = string.format("%d,%d,%d", bNode.x, lNode.y, lNode.z)
- lNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, lNode.z)
- flNodeKey = string.format("%d,%d,%d", fNode.x, lNode.y, lNode.z)
- uNodeKey = string.format("%d,%d,%d", uNode.x, uNode.y, uNode.z)
- dNodeKey = string.format("%d,%d,%d", dNode.x, dNode.y, dNode.z)
- fuNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, fNode.z)
- fruNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, rNode.z)
- ruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, rNode.z)
- rruNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, rNode.z)
- buNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, bNode.z)
- lruNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, lNode.z)
- luNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, lNode.z)
- fluNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, fNode.z)
- fdNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, fNode.z)
- frdNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, rNode.z)
- rdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, rNode.z)
- rrdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, rNode.z)
- bdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, bNode.z)
- lrdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, lNode.z)
- ldNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, lNode.z)
- fldNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, lNode.z)
- elseif facingDirection == 2 then
- if currentVector.x <= 0 then
- rNode.x = currentVector.x + 1
- lNode.x = currentVector.x - 1
- fNode.x = currentVector.x
- bNode.x = currentVector.x
- else
- rNode.x = currentVector.x - 1
- lNode.x = currentVector.x + 1
- fNode.x = currentVector.x
- bNode.x = currentVector.x
- end
- if currentVector.z <=0 then
- fNode.z = currentVector.z - 1
- bNode.z = currentVector.z + 1
- lNode.z = currentVector.z
- rNode.z = currentVector.z
- else
- fNode.z = currentVector.z + 1
- bNode.z = currentVector.z - 1
- lNode.z = currentVector.z
- rNode.z = currentVector.z
- end
- fNode.y = currentVector.y
- bNode.y = currentVector.y
- lNode.y = currentVector.y
- rNode.y = currentVector.y
- fNodeKey = string.format("%d,%d,%d", fNode.x, fNode.y, fNode.z)
- frNodeKey = string.format("%d,%d,%d", rNode.x, fNode.y, fNode.z)
- rNodeKey = string.format("%d,%d,%d", rNode.x, rNode.y, rNode.z)
- rrNodeKey = string.format("%d,%d,%d", rNode.x, bNode.y, bNode.z)
- bNodeKey = string.format("%d,%d,%d", bNode.x, bNode.y, bNode.z)
- lrNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, bNode.z)
- lNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, lNode.z)
- flNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, fNode.z)
- uNodeKey = string.format("%d,%d,%d", uNode.x, uNode.y, uNode.z)
- dNodeKey = string.format("%d,%d,%d", dNode.x, dNode.y, dNode.z)
- fuNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, fNode.z)
- fruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, fNode.z)
- ruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, rNode.z)
- rruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, bNode.z)
- buNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, bNode.z)
- lruNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, bNode.z)
- luNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, lNode.z)
- fluNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, fNode.z)
- fdNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, fNode.z)
- frdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, fNode.z)
- rdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, rNode.z)
- rrdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, bNode.z)
- bdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, bNode.z)
- lrdNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, bNode.z)
- ldNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, lNode.z)
- fldNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, fNode.z)
- elseif facingDirection == 3 then
- if currentVector.x <= 0 then
- fNode.x = currentVector.x + 1
- bNode.x = currentVector.x - 1
- lNode.x = currentVector.x
- rNode.x = currentVector.x
- else
- fNode.x = currentVector.x - 1
- bNode.x = currentVector.x + 1
- lNode.x = currentVector.x
- rNode.x = currentVector.x
- end
- if currentVector.z <=0 then
- lNode.z = currentVector.z - 1
- rNode.z = currentVector.z + 1
- fNode.z = currentVector.z
- bNode.z = currentVector.z
- else
- lNode.z = currentVector.z + 1
- rNode.z = currentVector.z - 1
- fNode.z = currentVector.z
- bNode.z = currentVector.z
- end
- fNode.y = currentVector.y
- bNode.y = currentVector.y
- lNode.y = currentVector.y
- rNode.y = currentVector.y
- fNodeKey = string.format("%d,%d,%d", fNode.x, fNode.y, fNode.z)
- frNodeKey = string.format("%d,%d,%d", fNode.x, fNode.y, rNode.z)
- rNodeKey = string.format("%d,%d,%d", rNode.x, rNode.y, rNode.z)
- rrNodeKey = string.format("%d,%d,%d", bNode.x, bNode.y, rNode.z)
- bNodeKey = string.format("%d,%d,%d", bNode.x, bNode.y, bNode.z)
- lrNodeKey = string.format("%d,%d,%d", bNode.x, lNode.y, lNode.z)
- lNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, lNode.z)
- flNodeKey = string.format("%d,%d,%d", fNode.x, lNode.y, lNode.z)
- uNodeKey = string.format("%d,%d,%d", uNode.x, uNode.y, uNode.z)
- dNodeKey = string.format("%d,%d,%d", dNode.x, dNode.y, dNode.z)
- fuNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, fNode.z)
- fruNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, rNode.z)
- ruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, rNode.z)
- rruNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, rNode.z)
- buNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, bNode.z)
- lruNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, lNode.z)
- luNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, lNode.z)
- fluNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, fNode.z)
- fdNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, fNode.z)
- frdNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, rNode.z)
- rdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, rNode.z)
- rrdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, rNode.z)
- bdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, bNode.z)
- lrdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, lNode.z)
- ldNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, lNode.z)
- fldNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, lNode.z)
- elseif facingDirection == 4 then
- if currentVector.x <= 0 then
- lNode.x = currentVector.x + 1
- rNode.x = currentVector.x - 1
- fNode.x = currentVector.x
- bNode.x = currentVector.x
- else
- rNode.x = currentVector.x + 1
- lNode.x = currentVector.x - 1
- fNode.x = currentVector.x
- bNode.x = currentVector.x
- end
- if currentVector.z >=0 then
- fNode.z = currentVector.z - 1
- bNode.z = currentVector.z + 1
- lNode.z = currentVector.z
- rNode.z = currentVector.z
- else
- fNode.z = currentVector.z + 1
- bNode.z = currentVector.z - 1
- lNode.z = currentVector.z
- rNode.z = currentVector.z
- end
- fNode.y = currentVector.y
- bNode.y = currentVector.y
- lNode.y = currentVector.y
- rNode.y = currentVector.y
- fNodeKey = string.format("%d,%d,%d", fNode.x, fNode.y, fNode.z)
- frNodeKey = string.format("%d,%d,%d", rNode.x, fNode.y, fNode.z)
- rNodeKey = string.format("%d,%d,%d", rNode.x, rNode.y, rNode.z)
- rrNodeKey = string.format("%d,%d,%d", rNode.x, bNode.y, bNode.z)
- bNodeKey = string.format("%d,%d,%d", bNode.x, bNode.y, bNode.z)
- lrNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, bNode.z)
- lNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, lNode.z)
- flNodeKey = string.format("%d,%d,%d", lNode.x, lNode.y, fNode.z)
- uNodeKey = string.format("%d,%d,%d", uNode.x, uNode.y, uNode.z)
- dNodeKey = string.format("%d,%d,%d", dNode.x, dNode.y, dNode.z)
- fuNodeKey = string.format("%d,%d,%d", fNode.x, uNode.y, fNode.z)
- fruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, fNode.z)
- ruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, rNode.z)
- rruNodeKey = string.format("%d,%d,%d", rNode.x, uNode.y, bNode.z)
- buNodeKey = string.format("%d,%d,%d", bNode.x, uNode.y, bNode.z)
- lruNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, bNode.z)
- luNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, lNode.z)
- fluNodeKey = string.format("%d,%d,%d", lNode.x, uNode.y, fNode.z)
- fdNodeKey = string.format("%d,%d,%d", fNode.x, dNode.y, fNode.z)
- frdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, fNode.z)
- rdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, rNode.z)
- rrdNodeKey = string.format("%d,%d,%d", rNode.x, dNode.y, bNode.z)
- bdNodeKey = string.format("%d,%d,%d", bNode.x, dNode.y, bNode.z)
- lrdNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, bNode.z)
- ldNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, lNode.z)
- fldNodeKey = string.format("%d,%d,%d", lNode.x, dNode.y, fNode.z)
- end
- if not parentVector then
- pNodeKey = string.format("%d,%d,%d", currentVector.x, currentVector.y, currentVector.z)
- else
- pNodeKey = string.format("%d,%d,%d", parentVector.x, parentVector.y, parentVector.z)
- end
- 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.
- nodeList[pNodeKey] = {}
- nodeList[pNodeKey].x = currentVector.x
- nodeList[pNodeKey].y = currentVector.y
- nodeList[pNodeKey].z = currentVector.z
- nodeList[pNodeKey].g = 10
- nodeList[pNodeKey].startx = startVector.x
- nodeList[pNodeKey].starty = startVector.y
- nodeList[pNodeKey].startz = startVector.z
- nodeList[pNodeKey].endx = targetVector.x
- nodeList[pNodeKey].endy = targetVector.y
- nodeList[pNodeKey].endz = targetVector.z
- 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)
- nodeList[pNodeKey].f = nodeList[pNodeKey].g + nodeList[pNodeKey].h
- nodeList[pNodeKey].state = "closed"
- elseif nodeList[pNodeKey].state == "open" then
- nodeList[pNodeKey].state = "closed"
- end
- 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.
- nodeList[cNodeKey] = {}
- nodeList[cNodeKey].x = currentVector.x
- nodeList[cNodeKey].y = currentVector.y
- nodeList[cNodeKey].z = currentVector.z
- nodeList[cNodeKey].g = 10
- nodeList[cNodeKey].startx = startVector.x
- nodeList[cNodeKey].starty = startVector.y
- nodeList[cNodeKey].startz = startVector.z
- nodeList[cNodeKey].endx = targetVector.x
- nodeList[cNodeKey].endy = targetVector.y
- nodeList[cNodeKey].endz = targetVector.z
- 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)
- nodeList[cNodeKey].f = nodeList[cNodeKey].g + nodeList[cNodeKey].h
- nodeList[cNodeKey].state = "closed"
- elseif nodeList[cNodeKey].state == "open" then
- nodeList[cNodeKey].state = "closed"
- end
- if not nodeList[fNodeKey] then
- nodeList[fNodeKey] = {}
- nodeList[fNodeKey].x = fNode.x
- nodeList[fNodeKey].y = fNode.y
- nodeList[fNodeKey].z = fNode.z
- nodeList[fNodeKey].startx = startVector.x
- nodeList[fNodeKey].starty = startVector.y
- nodeList[fNodeKey].startz = startVector.z
- nodeList[fNodeKey].endx = targetVector.x
- nodeList[fNodeKey].endy = targetVector.y
- nodeList[fNodeKey].endz = targetVector.z
- 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
- 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)
- nodeList[fNodeKey].f = nodeList[fNodeKey].g + nodeList[fNodeKey].h
- nodeList[fNodeKey].state = "open"
- elseif nodeList[fNodeKey].state == "open" then
- --put logic here to recalc g if needed.
- end
- if not nodeList[uNodeKey] then
- nodeList[uNodeKey] = {}
- nodeList[uNodeKey].x = uNode.x
- nodeList[uNodeKey].y = uNode.y
- nodeList[uNodeKey].z = uNode.z
- nodeList[uNodeKey].startx = startVector.x
- nodeList[uNodeKey].starty = startVector.y
- nodeList[uNodeKey].startz = startVector.z
- nodeList[uNodeKey].endx = targetVector.x
- nodeList[uNodeKey].endy = targetVector.y
- nodeList[uNodeKey].endz = targetVector.z
- 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
- 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)
- nodeList[uNodeKey].f = nodeList[uNodeKey].g + nodeList[uNodeKey].h
- nodeList[uNodeKey].state = "open"
- elseif nodeList[uNodeKey].state == "open" then
- --put logic here to recalc g if needed.
- end
- if not nodeList[dNodeKey] then
- nodeList[dNodeKey] = {}
- nodeList[dNodeKey].x = dNode.x
- nodeList[dNodeKey].y = dNode.y
- nodeList[dNodeKey].z = dNode.z
- nodeList[dNodeKey].startx = startVector.x
- nodeList[dNodeKey].starty = startVector.y
- nodeList[dNodeKey].startz = startVector.z
- nodeList[dNodeKey].endx = targetVector.x
- nodeList[dNodeKey].endy = targetVector.y
- nodeList[dNodeKey].endz = targetVector.z
- 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
- 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)
- nodeList[dNodeKey].f = nodeList[dNodeKey].g + nodeList[dNodeKey].h
- nodeList[dNodeKey].state = "open"
- elseif nodeList[dNodeKey].state == "open" then
- --put logic here to recalc g if needed.
- end
- if not nodeList[rNodeKey] then
- nodeList[rNodeKey] = {}
- nodeList[rNodeKey].x = rNode.x
- nodeList[rNodeKey].y = rNode.y
- nodeList[rNodeKey].z = rNode.z
- nodeList[rNodeKey].startx = startVector.x
- nodeList[rNodeKey].starty = startVector.y
- nodeList[rNodeKey].startz = startVector.z
- nodeList[rNodeKey].endx = targetVector.x
- nodeList[rNodeKey].endy = targetVector.y
- nodeList[rNodeKey].endz = targetVector.z
- 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
- 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)
- nodeList[rNodeKey].f = nodeList[rNodeKey].g + nodeList[rNodeKey].h
- nodeList[rNodeKey].state = "open"
- elseif nodeList[rNodeKey].state == "open" then
- --put logic here to recalc g if needed.
- end
- if not nodeList[bNodeKey] then
- nodeList[bNodeKey] = {}
- nodeList[bNodeKey].x = bNode.x
- nodeList[bNodeKey].y = bNode.y
- nodeList[bNodeKey].z = bNode.z
- nodeList[bNodeKey].startx = startVector.x
- nodeList[bNodeKey].starty = startVector.y
- nodeList[bNodeKey].startz = startVector.z
- nodeList[bNodeKey].endx = targetVector.x
- nodeList[bNodeKey].endy = targetVector.y
- nodeList[bNodeKey].endz = targetVector.z
- 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
- 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)
- nodeList[bNodeKey].f = nodeList[bNodeKey].g + nodeList[bNodeKey].h
- nodeList[bNodeKey].state = "open"
- elseif nodeList[bNodeKey].state == "open" then
- --put logic here to recalc g if needed.
- end
- if not nodeList[lNodeKey] then
- nodeList[lNodeKey] = {}
- nodeList[lNodeKey].x = lNode.x
- nodeList[lNodeKey].y = lNode.y
- nodeList[lNodeKey].z = lNode.z
- nodeList[lNodeKey].startx = startVector.x
- nodeList[lNodeKey].starty = startVector.y
- nodeList[lNodeKey].startz = startVector.z
- nodeList[lNodeKey].endx = targetVector.x
- nodeList[lNodeKey].endy = targetVector.y
- nodeList[lNodeKey].endz = targetVector.z
- 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
- 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)
- nodeList[lNodeKey].f = nodeList[lNodeKey].g + nodeList[lNodeKey].h
- nodeList[lNodeKey].state = "open"
- elseif nodeList[lNodeKey].state == "open" then
- --put logic here to recalc g if needed.
- end
- if nodeList[fuNodeKey] then
- nodeList[fuNodeKey].g = nodeList[fuNodeKey].g + nodeList[cNodeKey].g
- nodeList[fuNodeKey].f = nodeList[fuNodeKey].g + nodeList[fuNodeKey].h
- end
- if nodeList[fdNodeKey] then
- nodeList[fdNodeKey].g = nodeList[fdNodeKey].g + nodeList[cNodeKey].g
- nodeList[fdNodeKey].f = nodeList[fdNodeKey].g + nodeList[fdNodeKey].h
- end
- if nodeList[frNodeKey] then
- nodeList[frNodeKey].g = nodeList[frNodeKey].g + nodeList[cNodeKey].g
- nodeList[frNodeKey].f = nodeList[frNodeKey].g + nodeList[frNodeKey].h
- end
- if nodeList[fruNodeKey] then
- nodeList[fruNodeKey].g = nodeList[fruNodeKey].g + nodeList[cNodeKey].g
- nodeList[fruNodeKey].f = nodeList[fruNodeKey].g + nodeList[fruNodeKey].h
- end
- if nodeList[frdNodeKey] then
- nodeList[frdNodeKey].g = nodeList[frdNodeKey].g + nodeList[cNodeKey].g
- nodeList[frdNodeKey].f = nodeList[frdNodeKey].g + nodeList[frdNodeKey].h
- end
- if nodeList[ruNodeKey] then
- nodeList[ruNodeKey].g = nodeList[ruNodeKey].g + nodeList[cNodeKey].g
- nodeList[ruNodeKey].f = nodeList[ruNodeKey].g + nodeList[ruNodeKey].h
- end
- if nodeList[rdNodeKey] then
- nodeList[rdNodeKey].g = nodeList[rdNodeKey].g + nodeList[cNodeKey].g
- nodeList[rdNodeKey].f = nodeList[rdNodeKey].g + nodeList[rdNodeKey].h
- end
- if nodeList[rrNodeKey] then
- nodeList[rrNodeKey].g = nodeList[rrNodeKey].g + nodeList[cNodeKey].g
- nodeList[rrNodeKey].f = nodeList[rrNodeKey].g + nodeList[rrNodeKey].h
- end
- if nodeList[rruNodeKey] then
- nodeList[rruNodeKey].g = nodeList[rruNodeKey].g + nodeList[cNodeKey].g
- nodeList[rruNodeKey].f = nodeList[rruNodeKey].g + nodeList[rruNodeKey].h
- end
- if nodeList[rrdNodeKey] then
- nodeList[rrdNodeKey].g = nodeList[rrdNodeKey].g + nodeList[cNodeKey].g
- nodeList[rrdNodeKey].f = nodeList[rrdNodeKey].g + nodeList[rrdNodeKey].h
- end
- if nodeList[buNodeKey] then
- nodeList[buNodeKey].g = nodeList[buNodeKey].g + nodeList[cNodeKey].g
- nodeList[buNodeKey].f = nodeList[buNodeKey].g + nodeList[buNodeKey].h
- end
- if nodeList[bdNodeKey] then
- nodeList[bdNodeKey].g = nodeList[bdNodeKey].g + nodeList[cNodeKey].g
- nodeList[bdNodeKey].f = nodeList[bdNodeKey].g + nodeList[bdNodeKey].h
- end
- if nodeList[lrNodeKey] then
- nodeList[lrNodeKey].g = nodeList[lrNodeKey].g + nodeList[cNodeKey].g
- nodeList[lrNodeKey].f = nodeList[lrNodeKey].g + nodeList[lrNodeKey].h
- end
- if nodeList[lruNodeKey] then
- nodeList[lruNodeKey].g = nodeList[lruNodeKey].g + nodeList[cNodeKey].g
- nodeList[lruNodeKey].f = nodeList[lruNodeKey].g + nodeList[lruNodeKey].h
- end
- if nodeList[lrdNodeKey] then
- nodeList[lrdNodeKey].g = nodeList[lrdNodeKey].g + nodeList[cNodeKey].g
- nodeList[lrdNodeKey].f = nodeList[lrdNodeKey].g + nodeList[lrdNodeKey].h
- end
- if nodeList[luNodeKey] then
- nodeList[luNodeKey].g = nodeList[luNodeKey].g + nodeList[cNodeKey].g
- nodeList[luNodeKey].f = nodeList[luNodeKey].g + nodeList[luNodeKey].h
- end
- if nodeList[ldNodeKey] then
- nodeList[ldNodeKey].g = nodeList[ldNodeKey].g + nodeList[cNodeKey].g
- nodeList[ldNodeKey].f = nodeList[ldNodeKey].g + nodeList[ldNodeKey].h
- end
- if nodeList[flNodeKey] then
- nodeList[flNodeKey].g = nodeList[flNodeKey].g + nodeList[cNodeKey].g
- nodeList[flNodeKey].f = nodeList[flNodeKey].g + nodeList[flNodeKey].h
- end
- if nodeList[fluNodeKey] then
- nodeList[fluNodeKey].g = nodeList[fluNodeKey].g + nodeList[cNodeKey].g
- nodeList[fluNodeKey].f = nodeList[fluNodeKey].g + nodeList[fluNodeKey].h
- end
- if nodeList[fldNodeKey] then
- nodeList[fldNodeKey].g = nodeList[fldNodeKey].g + nodeList[cNodeKey].g
- nodeList[fldNodeKey].f = nodeList[fldNodeKey].g + nodeList[fldNodeKey].h
- end
- local fResult,fInspect = turtle.inspect()
- local uResult,uInspect = turtle.inspectUp()
- local dResult,dInspect = turtle.inspectDown()
- rightTurn()
- local rResult,rInspect = turtle.inspect()
- rightTurn()
- local bResult,bInspect = turtle.inspect()
- rightTurn()
- local lResult,lInspect = turtle.inspect()
- rightTurn()
- if fResult == true then
- nodeList[fNodeKey].state = "closed"
- nodeList[fNodeKey].block_type = fInspect.name
- end
- if uResult == true then
- nodeList[uNodeKey].state = "closed"
- nodeList[uNodeKey].block_type = uInspect.name
- end
- if dResult == true then
- nodeList[dNodeKey].state = "closed"
- nodeList[dNodeKey].block_type = dInspect.name
- end
- if rResult == true then
- nodeList[rNodeKey].state = "closed"
- nodeList[rNodeKey].block_type = rInspect.name
- end
- if bResult == true then
- nodeList[bNodeKey].state = "closed"
- nodeList[bNodeKey].block_type = bInspect.name
- end
- if lResult == true then
- nodeList[lNodeKey].state = "closed"
- nodeList[lNodeKey].block_type = lInspect.name
- end
- return nodeList[parentNodeKey], true
- end
- local function getBestNode(currentVector, targetVector , startVector)
- local bestMove = nil
- for vNode, nTable in pairs(nodeList) do
- if nTable.state == "open" then
- if bestMove == nil then
- bestMove = vNode
- end
- if nTable.f < nodeList[bestMove].f then
- bestMove = vNode
- elseif nTable.f == nodeList[bestMove].f then
- if nTable.h < nodeList[bestMove].h then
- bestMove = vNode
- elseif nTable.h == nodeList[bestMove].h then
- if nodeList[bestMove].y > currentVector.y then
- if nTable.y == currentVector.y then
- bestMove = vNode
- end
- end
- end
- else
- end
- else
- end
- end
- if nodeList[bestMove].state == "open" then
- local bmVector = vector.new(nodeList[bestMove].x,nodeList[bestMove].y,nodeList[bestMove].z)
- return bmVector
- else
- return false
- end
- end
- local function gotoNode(bnOffset, currentVector)
- if not facingDirection then
- facingDirection = getFacingDirection() -- using to pick move forward or back
- end
- local moves
- if facingDirection == 1 then
- if bnOffset.x < 0 then
- for moves = 1,math.abs(bnOffset.x) do
- turtle.forward()
- end
- else
- for moves = 1,math.abs(bnOffset.x) do
- turtle.back()
- end
- end
- if bnOffset.z > 0 then
- leftTurn()
- for moves = 1,math.abs(bnOffset.z) do
- turtle.forward()
- end
- rightTurn()
- else
- rightTurn()
- for moves = 1,math.abs(bnOffset.z) do
- turtle.forward()
- end
- leftTurn()
- end
- if bnOffset.y < 0 then
- for moves = 1,math.abs(bnOffset.y) do
- turtle.down()
- end
- elseif bnOffset.y > 0 then
- for moves = 0 ,math.abs(bnOffset.z) do
- turtle.up()
- end
- end
- elseif facingDirection == 2 then
- if bnOffset.z > 0 then
- for moves = 1,math.abs(bnOffset.z) do
- turtle.back()
- end
- elseif bnOffset.z < 0 then
- for moves = 1,math.abs(bnOffset.z) do
- turtle.forward()
- end
- end
- if bnOffset.x < 0 then
- leftTurn()
- for moves = 1,math.abs(bnOffset.x) do
- turtle.forward()
- end
- rightTurn()
- elseif bnOffset.x > 0 then
- rightTurn()
- for moves = 1,math.abs(bnOffset.x) do
- turtle.forward()
- end
- leftTurn()
- end
- if bnOffset.y < 0 then
- for moves = 1,math.abs(bnOffset.y) do
- turtle.down()
- end
- elseif bnOffset.y > 0 then
- for moves = 0,math.abs(bnOffset.z) do
- turtle.up()
- end
- end
- elseif facingDirection == 3 then
- if bnOffset.x > 0 then
- for moves = 1,math.abs(bnOffset.x) do
- turtle.forward()
- end
- else
- for moves = 1,math.abs(bnOffset.x) do
- turtle.back()
- end
- end
- if bnOffset.z < 0 then
- leftTurn()
- for moves = 1,math.abs(bnOffset.z) do
- turtle.forward()
- end
- rightTurn()
- else
- rightTurn()
- for moves = 1,math.abs(bnOffset.z) do
- turtle.forward()
- end
- leftTurn()
- end
- if bnOffset.y < 0 then
- for moves = 1,math.abs(bnOffset.y) do
- turtle.down()
- end
- elseif bnOffset.y > 0 then
- for moves = 0 ,math.abs(bnOffset.z) do
- turtle.up()
- end
- end
- elseif facingDirection == 4 then
- if bnOffset.z < 0 then
- for moves = 1,math.abs(bnOffset.z) do
- turtle.back()
- end
- elseif bnOffset.z > 0 then
- for moves = 1,math.abs(bnOffset.z) do
- turtle.forward()
- end
- end
- if bnOffset.x > 0 then
- leftTurn()
- for moves = 1,math.abs(bnOffset.x) do
- turtle.forward()
- end
- rightTurn()
- elseif bnOffset.x < 0 then
- rightTurn()
- for moves = 1,math.abs(bnOffset.x) do
- turtle.forward()
- end
- leftTurn()
- end
- if bnOffset.y < 0 then
- for moves = 1,math.abs(bnOffset.y) do
- turtle.down()
- end
- elseif bnOffset.y > 0 then
- for moves = 0,math.abs(bnOffset.z) do
- turtle.up()
- end
- end
- end
- end
- local function seekDestination(currentVector, targetVector)
- local hDisplacement
- local moveCount = 0
- local startVector = currentVector
- local parentVector = currentVector
- repeat
- moveCount = moveCount + 1
- local lastParentNode, nodeDataStatus = getNodeData(currentVector, targetVector, startVector, parentVector)
- local bestMoveVector = getBestNode(currentVector, targetVector , startVector)
- if bestMoveVector then
- local bestMoveOffset = bestMoveVector - currentVector
- local bmResult = gotoNode(bestMoveOffset, currentVector)
- parentVector = currentVector
- end
- currentVector = vector.new(gps.locate(5))
- until currentVector.x == targetVector.x and currentVector.y == targetVector.y and currentVector.z == targetVector.z
- end
- --************Starting Seek*************
- facingDirection = getFacingDirection()
- nodeList = {}
- cVector = vector.new(gps.locate(5))
- sVector = cVector
- targetVector = vector.new(-12, 75,-562)
- m = seekDestination(cVector,targetVector,sVector)
RAW Paste Data