Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get a22LsH3d Positioning
- SDM = require("SDM")
- CVF = require("CVF")
- POS = {}
- local initData = {}
- POS.position = nil
- POS.direction = nil
- local TURTLE = SDM.TURTLE
- local sectionDir = ""
- local beltDir = ""
- local varList = {
- "dirSection", "dirTopBelt", "dirBottomBelt", "initChestCoord", "yModulo",
- "chestRowMod", "chestColMod", "elevPos", "sectionMod", "leftSecMod",
- "rightSecMod", "lowBeltY", "upBeltY", "diskOutCoord", "diskFinishCoord",
- "entryModulo", "exitModulo", "pickaxeChestCoord", "chestOutCoord",
- "bLeftStart", "colBlockNum", "blockHeight", "beltPos", "chestRefuelCoord"
- }
- POS.sectionDir = ""
- POS.beltDir = ""
- function POS.init()
- initData = TURTLE.loadInitData()
- if not initData then
- print("Can't find Init Data")
- return false, 1
- end
- if not POS.get_GPS_Position() then
- print("Can't locate Turtle")
- return false, 2
- end
- if CVF.isXDir(initData.dirSection) then
- sectionDir = "x"
- beltDir = "z"
- POS.sectionDir = "x"
- POS.beltDir = "z"
- else
- sectionDir = "z"
- beltDir = "x"
- POS.sectionDir = "z"
- POS.beltDir = "x"
- end
- if not POS.get_Direction() then
- return false, 3
- end
- return true
- end
- function POS.get_GPS_Position()
- local x, y, z = gps.locate()
- if not x or not y or not z then
- print("GPS not available.")
- return false
- end
- POS.position = {x = math.floor(x), y = math.floor(y), z = math.floor(z)}
- if not POS.position.x or not POS.position.y or not POS.position.z then
- print("GPS not available.")
- return false
- end
- return true
- end
- function POS.get_Direction()
- local disk, dPath = SDM.getDisk("init")
- if disk then
- POS.direction = initData.dirSection
- return true
- end
- disk, dPath = SDM.getDisk("outgoing")
- if disk then
- for i=1, 4 do
- local success, data = turtle.inspect()
- if success and data.name == "minecraft:chest" then
- POS.direction = initData.dirTopBelt
- return true
- end
- turtle.turnRight()
- end
- end
- return false
- end
- function POS.isInBounds(pos)
- if not pos then pos = POS.position end
- local d1 = (pos[sectionDir] - initData.initChestCoord[sectionDir]) * initData.dirSection[sectionDir]
- return d1 >= -2
- end
- function POS.isInSystem(pos)
- if not pos then pos = POS.position end
- if POS.isOnBottomBelt(pos) then return true end
- if POS.isOnTopBelt(pos) then return true end
- if POS.isOnBottomSection(pos) then return true end
- if POS.isOnTopsection(pos) then return true end
- if POS.isOnLeftChestRow(pos) then return true end
- if POS.isOnRightChestRow(pos) then return true end
- if POS.isOnElevatorUp(pos) then return true end
- if POS.isOnElevatorDown(pos) then return true end
- if not POS.isInBounds(pos) then return true end
- return false
- end
- function POS.isOnBottomBelt(pos)
- if not pos then pos = POS.position end
- if not (pos.y == initData.lowBeltY) then return false end
- return initData.beltPos == pos[sectionDir]
- end
- function POS.isOnTopBelt(pos)
- if not pos then pos = POS.position end
- if not (pos.y == initData.upBeltY) then return false end
- return initData.beltPos == pos[sectionDir]
- end
- function POS.isOnBottomSection(pos)
- if not pos then pos = POS.position end
- local d1 = (pos[sectionDir] - initData.initChestCoord[sectionDir]) * initData.dirSection[sectionDir]
- local modYPos = pos.y % initData.blockHeight
- local modYSection = initData.lowBeltY % initData.blockHeight
- if d1 < -1 then return false end
- if not (pos[beltDir] % 8 == initData.sectionMod) then return false end
- return modYPos == modYSection
- end
- function POS.isOnTopsection(pos)
- if not pos then pos = POS.position end
- local d1 = (pos[sectionDir] - initData.initChestCoord[sectionDir]) * initData.dirSection[sectionDir]
- local modYPos = pos.y % initData.blockHeight
- local modYSection = (initData.lowBeltY - (initData.blockHeight - 5)) % initData.blockHeight
- if d1 < -1 then return false end
- if not (pos[beltDir] % 8 == initData.sectionMod) then return false end
- return modYPos == modYSection
- end
- function POS.isOnLeftChestRow(pos)
- if not pos then pos = POS.position end
- local d1 = (pos[sectionDir] - initData.initChestCoord[sectionDir]) * initData.dirSection[sectionDir]
- if d1 < 0 then return false end
- return pos[beltDir] % 8 == initData.leftSecMod % 8
- end
- function POS.isOnRightChestRow(pos)
- if not pos then pos = POS.position end
- local d1 = (pos[sectionDir] - initData.initChestCoord[sectionDir]) * initData.dirSection[sectionDir]
- if d1 < 0 then return false end
- return pos[beltDir] % 8 == initData.rightSecMod % 8
- end
- function POS.isOnHighestChestRow(pos)
- if not (POS.isOnLeftChestRow(pos) or POS.isOnRightChestRow(pos)) then return false end
- if not pos then pos = POS.position end
- local modYPos = pos.y % initData.blockHeight
- local modYSection = (initData.lowBeltY - (initData.blockHeight - 5)) % initData.blockHeight
- return modYPos == modYSection
- end
- function POS.isOnElevatorUp(pos)
- if not pos then pos = POS.position end
- local d1 = (pos[sectionDir] - initData.initChestCoord[sectionDir]) * initData.dirSection[sectionDir]
- if d1 ~= -1 then return false end
- return pos[beltDir] % 8 == initData.rightSecMod % 8
- end
- function POS.isOnElevatorDown(pos)
- if not pos then pos = POS.position end
- local d1 = (pos[sectionDir] - initData.initChestCoord[sectionDir]) * initData.dirSection[sectionDir]
- if d1 ~= -1 then return false end
- return pos[beltDir] % 8 == initData.leftSecMod % 8
- end
- function POS.isOnBeltExitUp(pos)
- if not pos then pos = POS.position end
- local d1 = (pos[sectionDir] - initData.initChestCoord[sectionDir]) * initData.dirSection[sectionDir]
- if d1 ~= -2 then return false end
- return pos[beltDir] % 8 == initData.rightSecMod % 8
- end
- function POS.isOnBeltExitDown(pos)
- if not pos then pos = POS.position end
- local d1 = (pos[sectionDir] - initData.initChestCoord[sectionDir]) * initData.dirSection[sectionDir]
- if d1 ~= -2 then return false end
- return pos[beltDir] % 8 == initData.leftSecMod % 8
- end
- function POS.getSection(pos)
- if not pos then pos = POS.position end
- local rAxisCoord = pos[beltDir]
- local fAxisCoord = pos[sectionDir]
- local d1 = (fAxisCoord - initData.initChestCoord[sectionDir]) * initData.dirSection[sectionDir]
- if d1 < -1 then return "out" end
- local testPos = {
- [sectionDir] = fAxisCoord,
- ["y"] = initData.lowBeltY,
- [beltDir] = rAxisCoord
- }
- if POS.isOnBottomSection(testPos) then return testPos[beltDir] end
- testPos[beltDir] = rAxisCoord + 1
- if POS.isOnBottomSection(testPos) then return testPos[beltDir] end
- testPos[beltDir] = rAxisCoord - 1
- if POS.isOnBottomSection(testPos) then return testPos[beltDir] end
- testPos[beltDir] = rAxisCoord + 2
- if POS.isOnBottomSection(testPos) then return testPos[beltDir] end
- testPos[beltDir] = rAxisCoord - 2
- if POS.isOnBottomSection(testPos) then return testPos[beltDir] end
- testPos[beltDir] = rAxisCoord + 3
- if POS.isOnBottomSection(testPos) then return testPos[beltDir] end
- testPos[beltDir] = rAxisCoord - 3
- if POS.isOnBottomSection(testPos) then return testPos[beltDir] end
- testPos[beltDir] = rAxisCoord + 4
- if POS.isOnBottomSection(testPos) then return "center" end
- return nil
- end
- function POS.getSectionHeight(pos)
- if not pos then pos = POS.position end
- local yModulo = initData.lowBeltY % initData.blockHeight
- for dy = 0, initData.blockHeight - 1 do
- if (pos.y - dy) % initData.blockHeight == yModulo then return (pos.y - dy) end
- end
- return nil
- end
- return POS
Advertisement