Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local TYPE_FUNCTION = 1
- local TYPE_VALUE = 2
- local TYPE_VAR = 3
- local TYPE_USER_FUNCTION = 4
- local TYPE_DEFAULT_FUNCTION = 5
- local TYPE_ARR = 6
- local MODE_TEST = 1
- local MODE_MC = 2
- local test = false
- os.loadAPI("DFE")
- local turtleAdapter = DFE.turtleAdapter
- local storageData = { direction = 0 }
- DFE.setStorageData(storageData)
- function printTable(tbl)
- printTableIndex(tbl, "", {})
- end
- function tableContainsValue(table, value)
- for i, v in pairs(table) do
- if v == value then
- return true
- end
- end
- return false
- end
- function printTableIndex(tbl, before, alreadyUsed)
- for i, v in pairs(tbl) do
- if type(v) == "table" then
- if tableContainsValue(alreadyUsed, v) then
- print(before .. i .. ":", "alreadyUsed");
- else
- print(before .. i .. ": [");
- table.insert(alreadyUsed, v)
- printTableIndex(v, before .. "\t", alreadyUsed)
- print(before .. "]")
- end
- else
- print(before .. i .. ":", v);
- end
- end
- end
- local gpsAdapter = {
- locate = function()
- local x, y, z = gps.locate()
- if x == nil then
- if not test then
- shell.run("gps", "locate")
- end
- error("unable to execute gps.locate()")
- end
- return { x, y, z }
- end
- }
- local AREA_EMPTY = -1
- local AREA_SET = -2
- function swapInArray(arr1, arr2, index)
- local v = arr1[index]
- arr1[index] = arr2[index]
- arr2[index] = v;
- end
- local fcts = {
- navi_setSpace = function(args)
- local space = {}
- local pos = args[1]
- local pos2 = args[2]
- if pos[1] > pos2[1] then
- swapInArray(pos, pos2, 1);
- end
- if pos[2] > pos2[2] then
- swapInArray(pos, pos2, 2);
- end
- if pos[3] > pos2[3] then
- swapInArray(pos, pos2, 3);
- end
- storageData.spaceOffset = args[1]
- storageData.spaceEnd = args[2]
- for x = 1, pos2[1] - pos[1] + 1 do
- space[x] = {}
- for y = 1, pos2[2] - pos[2] + 1 do
- space[x][y] = {}
- for z = 1, pos2[3] - pos[3] + 1 do
- space[x][y][z] = AREA_EMPTY
- end
- end
- end
- storageData.space = space
- end,
- navi_setNullPos = function(args)
- storageData.nullpos = gpsAdapter.locate()
- local o
- --for i = 0, 3 do
- o = checkOrientation(storageData.nullpos)
- if o < 0 then
- o = o + 4
- end
- o = o % 4
- --end
- if o == nil then
- error("fail")
- end
- storageData.nullOr = o
- storageData.direction = 0
- print("finished init: ", storageData.nullOr)
- end,
- navi_getPos = function(args)
- local pos = gpsAdapter.locate()
- return pos --{ pos[1] - nullPos[1] + 1, pos[2] - nullPos[2] + 1, pos[3] - nullPos[3] + 1 }
- end
- }
- function checkOrientation(currentPos)
- for i = 1, 4 do
- local o
- if turtleAdapter.forward() then
- local newPos = gpsAdapter.locate()
- if newPos[1] > currentPos[1] then
- o = 0
- elseif newPos[3] > currentPos[3] then
- o = 1
- elseif newPos[1] < currentPos[1] then
- o = 2
- elseif newPos[3] < currentPos[3] then
- o = 3
- end
- turtleAdapter.turnLeft()
- turtleAdapter.turnLeft()
- turtleAdapter.forward()
- turtleAdapter.turnLeft()
- turtleAdapter.turnLeft()
- end
- if o ~= nil then
- for j = 1, i - 1 do
- turtleAdapter.turnLeft()
- end
- o = o - i + 1
- if o < 0 then
- o = o + 4
- end
- return o
- end
- turtleAdapter.turnRight()
- end
- error("unable to init orientation, turtle is obstructed")
- end
- function initToContinue()
- if not test then
- local o = checkOrientation(gpsAdapter.locate())
- local o1 = (storageData.direction + storageData.nullOr) % 4
- if o ~= o1 then
- if o > o1 then
- o1 = o1 + 4
- end
- for i = 1, o1 - o do
- turtle.turnRight()
- end
- local o = checkOrientation(gpsAdapter.locate())
- local o1 = (storageData.direction + storageData.nullOr) % 4
- if o ~= o1 then
- error("fail: " .. o .. ", " .. o1)
- end
- end
- end
- end
- function printSpace(args)
- for z = 1, #storageData.space[1][1] do
- local txt = ""
- for x = 1, #storageData.space do
- txt = txt .. storageData.space[x][1][z] .. ", "
- end
- print(txt)
- end
- end
- function moveTo(pos, direction)
- local done = false
- local pos = pos
- while not done do
- local path = getPathTo(pos)
- if path == nil then
- error("unable to get to pos (" .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ")")
- end
- done = followPath(path)
- end
- setOrientation(direction)
- end
- local defaultFunctions = {
- {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "activateSave",
- fct = function(args)
- storageData.save = args[1];
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 2 }, name = "navi_setSpace",
- fct = fcts.navi_setSpace
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 2 }, name = "moveTo",
- fct = function(args)
- moveTo(args[1], args[2])
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0 }, name = "navi_setNullPos",
- fct = fcts.navi_setNullPos
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0 }, name = "navi_getPos",
- fct = fcts.navi_getPos
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "print",
- fct = function(args)
- if type(args[1]) == "table" then
- printTable(args[1])
- else
- print(args[1])
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 }, name = "mvFwd",
- fct = function(args)
- local par = tonumber(args[1])
- if par == nil then
- par = 1
- end
- for i = 1, par do
- turtleAdapter.forward()
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 }, name = "mvBack",
- fct = function(args)
- local par = tonumber(args[1])
- if par == nil then
- par = 1
- end
- turtleAdapter.turnLeft()
- turtleAdapter.turnLeft()
- for i = 1, par do
- turtleAdapter.forward()
- end
- turtleAdapter.turnLeft()
- turtleAdapter.turnLeft()
- end
- }, { type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 }, name = "mvUp",
- fct = function(args)
- local par = tonumber(args[1])
- if par == nil then
- par = 1
- end
- for i = 1, par do
- turtleAdapter.up()
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 }, name = "mvDown",
- fct = function(args)
- local par = tonumber(args[1])
- if par == nil then
- par = 1
- end
- for i = 1, par do
- turtleAdapter.down()
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 }, name = "left",
- fct = function(args)
- local par = tonumber(args[1])
- if par == nil then
- par = 1
- end
- for i = 1, par do
- turtleAdapter.turnLeft()
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 }, name = "right",
- fct = function(args)
- local par = tonumber(args[1])
- if par == nil then
- par = 1
- end
- for i = 1, par do
- turtleAdapter.turnRight()
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0 }, name = "printSpace",
- fct = printSpace
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "getItemName",
- fct = function(args)
- return turtleAdapter.getItemName(args[1])
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "getItemCount",
- fct = function(args)
- return turtleAdapter.getItemCount(args[1])
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 2, 3 }, name = "digAt",
- fct = function(args)
- local pos = args[2]
- local dir = faceTowardsBlock(pos, true)
- turtleAdapter.select(args[1])
- if not test then
- while true do
- local name = turtleAdapter.getItemName(args[1])
- if name == nil or args[3] == nil or name == args[3] then
- break
- end
- turtleAdapter.dropUp()
- end
- end
- if dir < 4 then
- dig(args[1])
- elseif dir == 4 then
- digUp(args[1])
- elseif dir == 5 then
- digDown(args[1])
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "dig",
- fct = function(args)
- if not test then
- while true do
- local name = turtleAdapter.getItemName(args[1])
- if name == nil or args[2] == nil or name == args[2] then
- break
- end
- turtleAdapter.dropUp()
- end
- end
- turtleAdapter.dig()
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "digUp",
- fct = function(args)
- turtleAdapter.select(args[1])
- if not test then
- while true do
- local name = turtleAdapter.getItemName(args[1])
- if name == nil or args[2] == nil or name == args[2] then
- break
- end
- turtleAdapter.dropUp()
- end
- end
- turtleAdapter.digUp()
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "digDown",
- fct = function(args)
- turtleAdapter.select(args[1])
- if not test then
- while true do
- local name = turtleAdapter.getItemName(args[1])
- if name == nil or args[2] == nil or name == args[2] then
- break
- end
- turtleAdapter.dropUp()
- end
- end
- turtleAdapter.digDown()
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "place",
- fct = function(args)
- turtleAdapter.select(args[1])
- turtleAdapter.place()
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "placeUp",
- fct = function(args)
- turtleAdapter.select(args[1])
- turtleAdapter.placeUp()
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "placeDown",
- fct = function(args)
- turtleAdapter.select(args[1])
- turtleAdapter.placeDown()
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 3 }, name = "placeAt",
- fct = function(args)
- if args[3] ~= nil then
- local pos = args[2]
- local direction = args[3]
- if direction < 4 then
- if direction == 0 then
- moveTo({ pos[1] - 1, pos[2], pos[3] }, direction)
- elseif direction == 1 then
- moveTo({ pos[1], pos[2], pos[3] - 1 }, direction)
- elseif direction == 2 then
- moveTo({ pos[1] + 1, pos[2], pos[3] }, direction)
- elseif direction == 3 then
- moveTo({ pos[1], pos[2], pos[3] + 1 }, direction)
- else
- error("fail")
- end
- place(args[1])
- elseif direction == 4 then
- moveTo({ pos[1], pos[2] - 1, pos[3] }, 0)
- placeUp(args[1])
- elseif direction == 5 then
- moveTo({ pos[1], pos[2] + 1, pos[3] }, 0)
- placeDown(args[1])
- end
- elseif args[2] ~= nil then
- local pos = args[2]
- local dir = faceTowardsBlock(pos, true)
- if dir < 4 then
- place(args[1])
- elseif dir == 4 then
- placeUp(args[1])
- elseif dir == 5 then
- placeDown(args[1])
- end
- else
- place(args[1])
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 2 }, name = "peripheral",
- fct = function(args)
- peripheral.call(args[1], args[2])
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "setTestOutput",
- fct = function(args)
- if type(args[1]) ~= "boolean" then
- error("arg of function 'setstorageData.testOutput' needs to be of type 'boolean'")
- end
- storageData.testOutput = args[1]
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "drop",
- fct = function(args)
- turtleAdapter.select(args[1])
- if args[2] ~= nil then
- turtleAdapter.drop(args[2])
- else
- turtleAdapter.drop(1)
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "dropUp",
- fct = function(args)
- turtleAdapter.select(args[1])
- if args[2] ~= nil then
- turtleAdapter.dropUp(args[2])
- else
- turtleAdapter.dropUp(1)
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "dropDown",
- fct = function(args)
- turtleAdapter.select(args[1])
- if args[2] ~= nil then
- turtleAdapter.dropDown(args[2])
- else
- turtleAdapter.dropDown(1)
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "suck",
- fct = function(args)
- turtleAdapter.select(args[1])
- if args[2] ~= nil then
- turtleAdapter.suck(args[2])
- else
- turtleAdapter.suck(1)
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "suckUp",
- fct = function(args)
- turtleAdapter.select(args[1])
- if args[2] ~= nil then
- turtleAdapter.suckUp(args[2])
- else
- turtleAdapter.suckUp(1)
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 }, name = "suckDown",
- fct = function(args)
- turtleAdapter.select(args[1])
- if args[2] ~= nil then
- turtleAdapter.suckDown(args[2])
- else
- turtleAdapter.suckDown(1)
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 2 }, name = "redstone",
- fct = function(args)
- redstone.setOutput(args[1], args[2])
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0 }, name = "refuel",
- fct = function(args)
- for i = 1, 16 do
- turtleAdapter.select(i)
- turtleAdapter.refuel()
- end
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0 }, name = "getFuelLevel",
- fct = function(args)
- return turtleAdapter.getFuelLevel()
- end
- }, {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 }, name = "sleep",
- fct = function(args)
- if args[1] > 2 then
- if storageData.toSleep ~= nil then
- args[1] = storageData.toSleep - (storageData.currTime - storageData.sleepStart)
- end
- storageData.sleepStart = os.clock()
- storageData.currTime = os.clock()
- storageData.toSleep = args[1]
- local printLast = 0
- local count = 0
- while storageData.currTime - storageData.sleepStart < args[1] do
- sleep(0.1)
- storageData.currTime = os.clock()
- if storageData.currTime - storageData.sleepStart > printLast + 1 then
- printLast = printLast + 1
- if count ~= 0 then
- local x, y = term.getCursorPos()
- term.setCursorPos(1, y - 1)
- else
- count = 1
- end
- print("slept: ", printLast, "/", storageData.toSleep, "s")
- save()
- end
- end
- storageData.toSleep = nil
- else
- sleep(args[1])
- end
- end
- }
- }
- function faceTowardsBlock(pos, upOrDownAllowed)
- local done = false
- local minInd
- while not done do
- local path0 = { getPathTo({ pos[1] - 1, pos[2], pos[3] }), 0 }
- local path1 = { getPathTo({ pos[1], pos[2], pos[3] - 1 }), 1 }
- local path2 = { getPathTo({ pos[1] + 1, pos[2], pos[3] }), 2 }
- local path3 = { getPathTo({ pos[1], pos[2], pos[3] + 1 }), 3 }
- local path4 = {}
- local path5 = {}
- if upOrDownAllowed then
- path4[1] = getPathTo({ pos[1], pos[2] + 1, pos[3] })
- path4[2] = 5
- path5[1] = getPathTo({ pos[1], pos[2] - 1, pos[3] })
- path5[2] = 4
- end
- local min
- local minPath
- for i, storageData in ipairs({ path0, path1, path2, path3, path4, path5 }) do
- if storageData[1] ~= nil and (min == nil or #storageData[1] < min) then
- min = #storageData[1]
- minPath = storageData[1]
- minInd = storageData[2]
- end
- end
- if min == nil then
- error("unable to get to pos (" .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ")")
- end
- done = followPath(minPath)
- setOrientation(minInd % 4)
- end
- return minInd
- end
- function digUp(id)
- local pos = translateToSpacePos(fcts.navi_getPos())
- storageData.space[pos[1]][pos[2] + 1][pos[3]] = AREA_EMPTY
- turtleAdapter.select(id)
- turtleAdapter.digUp()
- end
- function digDown(id)
- local pos = translateToSpacePos(fcts.navi_getPos())
- storageData.space[pos[1]][pos[2] - 1][pos[3]] = AREA_EMPTY
- turtleAdapter.select(id)
- turtleAdapter.digDown()
- end
- function dig(id)
- local pos = translateToSpacePos(fcts.navi_getPos())
- if storageData.direction == 0 then
- storageData.space[pos[1] + 1][pos[2]][pos[3]] = AREA_EMPTY
- elseif storageData.direction == 1 then
- storageData.space[pos[1]][pos[2]][pos[3] + 1] = AREA_EMPTY
- elseif storageData.direction == 2 then
- storageData.space[pos[1] - 1][pos[2]][pos[3]] = AREA_EMPTY
- elseif storageData.direction == 3 then
- storageData.space[pos[1]][pos[2]][pos[3] - 1] = AREA_EMPTY
- end
- turtleAdapter.select(id)
- turtleAdapter.dig()
- end
- function placeUp(id)
- local pos = translateToSpacePos(fcts.navi_getPos())
- storageData.space[pos[1]][pos[2] + 1][pos[3]] = AREA_SET
- turtleAdapter.select(id)
- while not turtleAdapter.placeUp() do
- if turtleAdapter.detectUp() then
- turtleAdapter.digUp()
- turtleAdapter.attackUp()
- end
- end
- end
- function placeDown(id)
- local pos = translateToSpacePos(fcts.navi_getPos())
- storageData.space[pos[1]][pos[2] - 1][pos[3]] = AREA_SET
- turtleAdapter.select(id)
- while not turtleAdapter.placeDown() do
- if turtleAdapter.detectDown() then
- turtleAdapter.digDown()
- turtleAdapter.attackDown()
- end
- end
- end
- function place(id)
- local pos = translateToSpacePos(fcts.navi_getPos())
- if storageData.direction == 0 then
- storageData.space[pos[1] + 1][pos[2]][pos[3]] = AREA_SET
- elseif storageData.direction == 1 then
- storageData.space[pos[1]][pos[2]][pos[3] + 1] = AREA_SET
- elseif storageData.direction == 2 then
- storageData.space[pos[1] - 1][pos[2]][pos[3]] = AREA_SET
- elseif storageData.direction == 3 then
- storageData.space[pos[1]][pos[2]][pos[3] - 1] = AREA_SET
- end
- turtleAdapter.select(id)
- while not turtleAdapter.place() do
- if turtleAdapter.detect() then
- turtleAdapter.dig()
- turtleAdapter.attack()
- end
- end
- end
- function followPath(path)
- local currPos = translateToSpacePos(fcts.navi_getPos())
- for i, pos in ipairs(path) do
- if pos[1] > currPos[1] then
- setOrientation(0)
- while not turtleAdapter.forward() do
- if turtleAdapter.detect() then
- storageData.space[pos[1]][pos[2]][pos[3]] = AREA_SET
- return false
- else
- turtleAdapter.attack()
- end
- end
- elseif pos[1] < currPos[1] then
- setOrientation(2)
- while not turtleAdapter.forward() do
- if turtleAdapter.detect() then
- storageData.space[pos[1]][pos[2]][pos[3]] = AREA_SET
- return false
- else
- turtleAdapter.attack()
- end
- end
- elseif pos[2] > currPos[2] then
- while not turtleAdapter.up() do
- if turtleAdapter.detectUp() then
- storageData.space[pos[1]][pos[2]][pos[3]] = AREA_SET
- return false
- else
- turtleAdapter.attackUp()
- end
- end
- elseif pos[2] < currPos[2] then
- while not turtleAdapter.down() do
- if turtleAdapter.detectDown() then
- storageData.space[pos[1]][pos[2]][pos[3]] = AREA_SET
- return false
- else
- turtleAdapter.attackDown()
- end
- end
- elseif pos[3] > currPos[3] then
- setOrientation(1)
- while not turtleAdapter.forward() do
- if turtleAdapter.detect() then
- storageData.space[pos[1]][pos[2]][pos[3]] = AREA_SET
- return false
- else
- turtleAdapter.attack()
- end
- end
- elseif pos[3] < currPos[3] then
- setOrientation(3)
- while not turtleAdapter.forward() do
- if turtleAdapter.detect() then
- storageData.space[pos[1]][pos[2]][pos[3]] = AREA_SET
- return false
- else
- turtleAdapter.attack()
- end
- end
- end
- currPos = pos
- end
- return true
- --[[printTable(translateToSpacePos(fcts.navi_getPos()))
- printTable(currPos)]]
- end
- function setOrientation(orientation)
- if orientation ~= storageData.direction then
- if orientation > storageData.direction then
- if orientation - storageData.direction == 3 then
- turtleAdapter.turnLeft()
- else
- for i = storageData.direction + 1, orientation do
- turtleAdapter.turnRight()
- end
- end
- else
- if orientation - storageData.direction == -3 then
- turtleAdapter.turnRight()
- else
- for i = orientation + 1, storageData.direction do
- turtleAdapter.turnLeft()
- end
- end
- end
- end
- end
- function translateToSpacePos(pos, spaceCoordinates)
- local nullPos = storageData.nullpos
- if spaceCoordinates == nil then
- pos = { pos[1] - nullPos[1], pos[2] - nullPos[2], pos[3] - nullPos[3] }
- local x = pos[1]
- local z = pos[3]
- if storageData.nullOr == 1 then
- pos[1] = z
- pos[3] = -x
- elseif storageData.nullOr == 2 then
- pos[1] = -x
- pos[3] = -z
- elseif storageData.nullOr == 3 then
- pos[1] = -z
- pos[3] = x
- end
- pos = { pos[1] + 1, pos[2] + 1, pos[3] + 1 }
- end
- pos = {
- pos[1] - storageData.spaceOffset[1] + 1,
- pos[2] - storageData.spaceOffset[2] + 1,
- pos[3] - storageData.spaceOffset[3] + 1
- }
- return pos
- end
- function getPathTo(posSource)
- local currPosSource = fcts.navi_getPos()
- local pos = translateToSpacePos(posSource, true)
- local currPos = translateToSpacePos(currPosSource)
- local space = {}
- for x = 1, #storageData.space do
- space[x] = {}
- for y = 1, #storageData.space[1] do
- space[x][y] = {}
- for z = 1, #storageData.space[1][1] do
- space[x][y][z] = storageData.space[x][y][z]
- end
- end
- end
- if pos[1] > #space or pos[2] > #space[1] or pos[3] > #space[1][1]
- or pos[1] < 1 or pos[2] < 1 or pos[3] < 1 then
- return nil
- end
- if space[pos[1]][pos[2]][pos[3]] == AREA_SET then
- return nil
- end
- space[pos[1]][pos[2]][pos[3]] = 0
- local done = false
- while not done do
- done = true
- for x = 1, #space do
- for y = 1, #space[1] do
- for z = 1, #space[1][1] do
- local vSelf = space[x][y][z]
- if vSelf ~= AREA_SET then
- --x
- if x > 1 and space[x - 1][y][z] >= 0 and (vSelf == AREA_EMPTY or space[x - 1][y][z] + 1 < vSelf) then
- space[x][y][z] = space[x - 1][y][z] + 1
- vSelf = space[x][y][z]
- done = false
- end
- if x < #space and space[x + 1][y][z] >= 0 and (vSelf == AREA_EMPTY or space[x + 1][y][z] + 1 < vSelf) then
- space[x][y][z] = space[x + 1][y][z] + 1
- vSelf = space[x][y][z]
- done = false
- end
- --z
- if z > 1 and space[x][y][z - 1] >= 0 and (vSelf == AREA_EMPTY or space[x][y][z - 1] + 1 < vSelf) then
- space[x][y][z] = space[x][y][z - 1] + 1
- vSelf = space[x][y][z]
- done = false
- end
- if z < #space[1][1] and space[x][y][z + 1] >= 0 and (vSelf == AREA_EMPTY or space[x][y][z + 1] + 1 < vSelf) then
- space[x][y][z] = space[x][y][z + 1] + 1
- vSelf = space[x][y][z]
- done = false
- end
- --y
- if y > 1 and space[x][y - 1][z] >= 0 and (vSelf == AREA_EMPTY or space[x][y - 1][z] + 1 < vSelf) then
- space[x][y][z] = space[x][y - 1][z] + 1
- vSelf = space[x][y][z]
- done = false
- end
- if y < #space[1] and space[x][y + 1][z] >= 0 and (vSelf == AREA_EMPTY or space[x][y + 1][z] + 1 < vSelf) then
- space[x][y][z] = space[x][y + 1][z] + 1
- vSelf = space[x][y][z]
- done = false
- end
- end
- end
- end
- end
- end
- local path = {}
- local x = currPos[1]
- local y = currPos[2]
- local z = currPos[3]
- local num = space[x][y][z]
- if num == nil then
- error(x .. ", " .. y .. ", " .. z)
- end
- if num < 0 then
- return nil
- end
- while num > 0 do
- if x > 1 and space[x - 1][y][z] == num - 1 then
- x = x - 1
- num = num - 1
- elseif x < #space and space[x + 1][y][z] == num - 1 then
- x = x + 1
- num = num - 1
- elseif z > 1 and space[x][y][z - 1] == num - 1 then
- z = z - 1
- num = num - 1
- elseif z < #space[1][1] and space[x][y][z + 1] == num - 1 then
- z = z + 1
- num = num - 1
- elseif y > 1 and space[x][y - 1][z] == num - 1 then
- y = y - 1
- num = num - 1
- elseif y < #space[1] and space[x][y + 1][z] == num - 1 then
- y = y + 1
- num = num - 1
- else
- error("fail " .. x .. "," .. y .. "," .. z .. ": " .. num)
- end
- table.insert(path, { x, y, z })
- end
- return path
- end
- function getDefaultFunctions()
- return defaultFunctions
- end
- function setTest(value, tbl)
- for i, v in pairs(tbl) do
- _G[i] = v
- end
- test = value
- if test then
- _G.sleep = function(n)
- -- seconds
- local t0 = os.clock()
- while os.clock() - t0 <= n do
- end
- end
- _G.turtle = {
- forward = function()
- if storageData.testOutput then
- print("fct: turtle.forward()")
- end
- return true
- end,
- up = function()
- if storageData.testOutput then
- print("fct: turtle.up()")
- end
- return true
- end,
- down = function()
- if storageData.testOutput then
- print("fct: turtle.down()")
- end
- return true
- end,
- turnLeft = function()
- if storageData.testOutput then
- print("fct: turtle.turnLeft()")
- end
- return true
- end,
- turnRight = function()
- if storageData.testOutput then
- print("fct: turtle.turnRight()")
- end
- return true
- end,
- select = function(id)
- if storageData.testOutput then
- print("fct: turtle.select(" .. id .. ")")
- end
- return true
- end,
- place = function()
- if storageData.testOutput then
- print("fct: turtle.place()")
- end
- return true
- end,
- placeUp = function()
- if storageData.testOutput then
- print("fct: turtle.placeUp()")
- end
- return true
- end,
- placeDown = function()
- if storageData.testOutput then
- print("fct: turtle.placeDown()")
- end
- return true
- end,
- dig = function()
- if storageData.testOutput then
- print("fct: turtle.dig()")
- end
- return true
- end,
- digUp = function()
- if storageData.testOutput then
- print("fct: turtle.digUp()")
- end
- return true
- end,
- digDown = function()
- if storageData.testOutput then
- print("fct: turtle.digDown()")
- end
- return true
- end,
- attack = function()
- if storageData.testOutput then
- print("fct: turtle.attack()")
- end
- return true
- end,
- attackUp = function()
- if storageData.testOutput then
- print("fct: turtle.attackUp()")
- end
- return true
- end,
- attackDown = function()
- if storageData.testOutput then
- print("fct: turtle.attackDown()")
- end
- return true
- end,
- detect = function()
- if storageData.space ~= nil then
- local pos = storageData.pos
- local o = storageData.direction
- if o == 0 then
- if #storageData.space > pos[1] then
- return #storageData.space[pos[1] + 1][pos[2]][pos[3]] == AREA_SET
- end
- return false
- elseif o == 1 then
- if #storageData.space[1][1] > pos[3] then
- return #storageData.space[pos[1]][pos[2]][pos[3] + 1] == AREA_SET
- end
- return false
- elseif o == 2 then
- if #storageData.space < pos[1] then
- return #storageData.space[pos[1] - 1][pos[2]][pos[3]] == AREA_SET
- end
- return false
- elseif o == 3 then
- if #storageData.space[1][1] < pos[3] then
- return #storageData.space[pos[1]][pos[2]][pos[3] - 1] == AREA_SET
- end
- return false
- end
- error("fail:" .. o)
- else
- return false;
- end
- end,
- detectUp = function()
- if storageData.space ~= nil then
- local pos = storageData.pos
- if #storageData.space[1] > pos[2] then
- return #storageData.space[pos[1]][pos[2] + 1][pos[3]] == AREA_SET
- end
- return false
- else
- return false;
- end
- end,
- detectDown = function()
- if storageData.space ~= nil then
- local pos = storageData.pos
- if #storageData.space[1] < pos[2] then
- return #storageData.space[pos[1]][pos[2] - 1][pos[3]] == AREA_SET
- end
- return false
- else
- return false;
- end
- end,
- getItemCount = function(id)
- return 0
- end
- }
- _G.gps = {
- locate = function()
- return storageData.pos[1], storageData.pos[2], storageData.pos[3]
- end
- }
- _G.peripheral = {
- call = function(arg1, arg2)
- if storageData.testOutput then
- print("fct: peripheral.call(" .. arg1 .. ", " .. arg2 .. ")")
- end
- end
- }
- end
- end
- function setHu(arg)
- end
- function setStorageData(data, saveFunction)
- _G.save = saveFunction
- storageData = data
- DFE.setStorageData(storageData)
- if test and storageData.pos == nil then
- storageData.pos = { 123, 60, 546 }
- end
- if storageData.direction == nil then
- storageData.direction = 0
- end
- if storageData.nullOr == nil then
- storageData.nullOr = 0
- end
- if storageData.testOutput == nil then
- storageData.testOutput = true
- end
- end
- return { setTest, setStorageData, defaultFunctions, initToContinue }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement