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
- if os.loadAPI == nil then
- DFE = require("turtleStd")
- test = true
- DFE.setTest(true)
- else
- DFE = require("DFE")
- test = false
- DFE.setTest(false)
- end
- turtleAdapter = DFE.turtleAdapter
- local storage
- local save
- 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
- function trim(s)
- return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
- end
- local defaultFunctions = {
- shellRun = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 10 },
- fct = function(...)
- if shell == nil then
- return
- end
- return shell.run(table.unpack(arg))
- end
- },
- shellExecute = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 10 },
- fct = function(...)
- if shell == nil then
- return
- end
- return shell.execute(table.unpack(arg))
- end
- },
- findText = {
- type = TYPE_DEFAULT_FUNCTION,
- pars = { 2 },
- nonCritical = true,
- fct = function(txt, pattern)
- return txt:find(pattern)
- end
- },
- splitText = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 2 },
- nonCritical = true,
- fct = function(txt, pattern)
- return toTurtiArray(splitTextPattern(txt, pattern, true))
- end
- },
- stringLower = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 },
- nonCritical = true,
- fct = function(s)
- return string.lower(s)
- end
- },
- stringUpper = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 },
- nonCritical = true,
- fct = function(s)
- return string.upper(s)
- end
- },
- tonumber = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 },
- nonCritical = true,
- fct = function(value)
- return tonumber(trim(value))
- end
- },
- input = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 },
- fct = function(label)
- if label then
- io.write("input<" .. label .. ">: ")
- else
- io.write("input: ")
- end
- return io.read()
- end
- },
- activateSave = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 },
- fct = function(value)
- storage.save = value;
- end
- },
- inspectName = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0 },
- fct = function()
- local success, data = turtle.inspect()
- if not success then
- return nil
- end
- return data.name
- end
- },
- inspectNameUp = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0 },
- fct = function()
- local success, data = turtle.inspectUp()
- if not success then
- return nil
- end
- return data.name
- end
- },
- inspectNameDown = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0 },
- fct = function()
- local success, data = turtle.inspectDown()
- if not success then
- return nil
- end
- return data.name
- end
- },
- craft = {
- type = TYPE_DEFAULT_FUNCTION,
- pars = { 0, 1 },
- fct = function(...)
- return turtle.craft(...)
- end
- },
- printf = {
- type = TYPE_DEFAULT_FUNCTION,
- pars = { 1 },
- nonCritical = true,
- fct = function(src)
- local tagOpen = src:find("<")
- while tagOpen ~= nil do
- write(src:sub(1, tagOpen - 1))
- src = src:sub(tagOpen + 1)
- local tagClose = src:find(">")
- local command = src:sub(1, tagClose - 1)
- src = src:sub(tagClose + 1)
- tagOpen = src:find("<")
- if not tagOpen then
- write(src)
- break
- end
- local body = src:sub(1, tagOpen - 1)
- src = src:sub(tagOpen + 1)
- tagClose = src:find(">")
- if not tagClose then
- write(body .. "<" .. src)
- break
- end
- local cSep = command:find(":")
- if cSep then
- local name = command:sub(1, cSep - 1)
- local argument = command:sub(cSep + 1)
- if name == "c" then
- if not test then
- local oldColor = term.getTextColor()
- term.setTextColor(colors[argument])
- write(body)
- term.setTextColor(oldColor)
- else
- write(body)
- end
- end
- end
- src = src:sub(tagClose + 1)
- tagOpen = src:find("<")
- end
- write(src .. "\n")
- end
- },
- print = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 1000 },
- nonCritical = true,
- fct = function(...)
- local args = { ... }
- if #args == 0 then
- write("null\n")
- return
- end
- local first = true
- for i, v in ipairs(args) do
- if first then
- first = false
- else
- write(" ")
- end
- if v == nil then
- write("null")
- elseif type(v) == "table" then
- printTable(v, true)
- else
- write(tostring(v))
- end
- end
- write("\n")
- end
- },
- mvFwd = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 },
- fct = function(steps)
- local par = tonumber(steps)
- if par == nil then
- par = 1
- end
- for i = 1, par do
- turtleAdapter.forward()
- end
- end
- },
- mvBack = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 },
- fct = function(steps)
- local par = tonumber(steps)
- if par == nil then
- par = 1
- end
- turtleAdapter.turnLeft()
- turtleAdapter.turnLeft()
- for i = 1, par do
- turtleAdapter.forward()
- end
- turtleAdapter.turnLeft()
- turtleAdapter.turnLeft()
- return moved
- end
- },
- mvUp = { type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 },
- fct = function(steps)
- local par = tonumber(steps)
- if par == nil then
- par = 1
- end
- for i = 1, par do
- turtleAdapter.up()
- end
- return moved
- end
- },
- mvDown = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 },
- fct = function(steps)
- local par = tonumber(steps)
- if par == nil then
- par = 1
- end
- for i = 1, par do
- turtleAdapter.down()
- end
- return moved
- end
- },
- left = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 },
- fct = function(steps)
- local par = tonumber(steps)
- if par == nil then
- par = 1
- end
- for i = 1, par do
- turtleAdapter.turnLeft()
- end
- end
- },
- right = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 },
- fct = function(steps)
- local par = tonumber(steps)
- if par == nil then
- par = 1
- end
- for i = 1, par do
- turtleAdapter.turnRight()
- end
- end
- },
- equipLeft = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 },
- fct = function(slot)
- if slot then
- turtle.select(slot + 1)
- end
- return turtle.equipLeft()
- end
- },
- equipRight = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 1 },
- fct = function(slot)
- if slot then
- turtle.select(slot + 1)
- end
- return turtle.equipRight()
- end
- },
- getItemName = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 },
- fct = function(slot)
- return turtleAdapter.getItemName(slot + 1)
- end
- },
- getItemSpace = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 },
- fct = function(slot)
- return turtleAdapter.getItemSpace(slot + 1)
- end
- },
- getItemCount = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 },
- fct = function(slot)
- return turtleAdapter.getItemCount(slot + 1)
- end
- },
- dig = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 2 },
- fct = function(slot, expected)
- if not slot then
- return turtleAdapter.dig()
- end
- if not test then
- while true do
- local name = turtleAdapter.getItemName(slot + 1)
- if name == nil or expected == nil or name == expected then
- break
- end
- turtleAdapter.dropUp()
- end
- end
- turtleAdapter.select(slot + 1)
- return turtleAdapter.dig()
- end
- },
- digUp = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 2 },
- fct = function(slot, expected)
- if not slot then
- return turtleAdapter.digUp()
- end
- if not test then
- while true do
- local name = turtleAdapter.getItemName(slot + 1)
- if name == nil or expected == nil or name == expected then
- break
- end
- turtleAdapter.dropUp()
- end
- end
- turtleAdapter.select(slot + 1)
- return turtleAdapter.digUp()
- end
- },
- digDown = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 2 },
- fct = function(slot, expected)
- if not slot then
- return turtleAdapter.digDown()
- end
- if not test then
- while true do
- local name = turtleAdapter.getItemName(slot + 1)
- if name == nil or expected == nil or name == expected then
- break
- end
- turtleAdapter.dropUp()
- end
- end
- turtleAdapter.select(slot + 1)
- return turtleAdapter.digDown()
- end
- },
- place = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 },
- fct = function(slot)
- turtleAdapter.select(slot + 1)
- return turtleAdapter.place()
- end
- },
- placeUp = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 },
- fct = function(slot)
- turtleAdapter.select(slot + 1)
- return turtleAdapter.placeUp()
- end
- },
- placeDown = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 },
- fct = function(slot)
- turtleAdapter.select(slot + 1)
- return turtleAdapter.placeDown()
- end
- },
- peripheral = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 2, 456412 },
- fct = function(side, method, ...)
- return peripheral.call(side, method, ...)
- end
- },
- setTestOutput = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 },
- fct = function(value)
- if type(value) ~= "boolean" then
- error("arg of function 'setstorageData.testOutput' needs to be of type 'boolean'")
- end
- storage.testOutput = value
- end
- },
- drop = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 },
- fct = function(slot, num)
- turtleAdapter.select(slot + 1)
- if num ~= nil then
- turtleAdapter.drop(num)
- else
- turtleAdapter.drop(1)
- end
- end
- },
- dropUp = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 },
- fct = function(slot, num)
- turtleAdapter.select(slot + 1)
- if num ~= nil then
- turtleAdapter.dropUp(num)
- else
- turtleAdapter.dropUp(1)
- end
- end
- },
- dropDown = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1, 2 },
- fct = function(slot, num)
- turtleAdapter.select(slot + 1)
- if num ~= nil then
- turtleAdapter.dropDown(num)
- else
- turtleAdapter.dropDown(1)
- end
- end
- },
- select = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 },
- fct = function(slot)
- turtle.select(slot + 1)
- end
- },
- suck = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 2 },
- fct = function(slot, num)
- if not num then
- num = 1
- end
- if slot then
- turtleAdapter.select(slot + 1)
- end
- turtleAdapter.suck(num)
- end
- },
- suckUp = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 2 },
- fct = function(slot, num)
- if not num then
- num = 1
- end
- if slot then
- turtleAdapter.select(slot + 1)
- end
- turtleAdapter.suckUp(num)
- end
- },
- suckDown = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0, 2 },
- fct = function(slot, num)
- if not num then
- num = 1
- end
- if slot then
- turtleAdapter.select(slot + 1)
- end
- turtleAdapter.suckDown(num)
- end
- },
- redstone = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 2 },
- fct = function(side, value)
- redstone.setOutput(side, value)
- end
- },
- getRedstoneInput = {
- type = TYPE_DEFAULT_FUNCTION, pars = {},
- fct = function(side)
- return redstone.getInput(side)
- end
- },
- refuel = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0 },
- fct = function()
- for i = 1, 16 do
- turtleAdapter.select(i)
- turtleAdapter.refuel()
- end
- end
- },
- getFuelLevel = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0 },
- fct = function()
- return turtleAdapter.getFuelLevel()
- end
- },
- clearTerminal = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 0 },
- nonCritical = true,
- fct = function()
- if term == nil then
- return
- end
- term.clear()
- term.setCursorPos(1, 1)
- end
- },
- sleep = {
- type = TYPE_DEFAULT_FUNCTION, pars = { 1 },
- fct = function(seconds)
- local storage = ThreadManager.currentThread.storage
- if seconds > 2 then
- if storage.toSleep ~= nil then
- seconds = storage.toSleep - (storage.currTime - storage.sleepStart)
- end
- storage.sleepStart = os.clock()
- storage.currTime = os.clock()
- storage.toSleep = seconds
- local printLast = 0
- local count = 0
- while storage.currTime - storage.sleepStart < seconds do
- sleep(0.1)
- yield()
- storage.currTime = os.clock()
- if storage.currTime - storage.sleepStart > printLast + 1 then
- printLast = printLast + 1
- --[[if count ~= 0 then
- if not test then
- local x, y = term.getCursorPos()
- term.setCursorPos(1, y - 1)
- end
- else
- count = 1
- end]]--
- --print("slept: ", printLast, "/", storage.toSleep, "s")
- save()
- end
- end
- storage.toSleep = nil
- else
- sleep(seconds)
- end
- end
- }
- }
- function getDefaultFunctions()
- return defaultFunctions
- end
- function setTest(value)
- 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 storage.testOutput then
- print("fct: turtle.forward()")
- end
- if storage.direction == 0 then
- storage.pos[1] = storage.pos[1] + 1
- elseif storage.direction == 1 then
- storage.pos[3] = storage.pos[3] + 1
- elseif storage.direction == 2 then
- storage.pos[1] = storage.pos[1] - 1
- elseif storage.direction == 3 then
- storage.pos[3] = storage.pos[3] - 1
- end
- return true
- end,
- back = function()
- if storage.testOutput then
- print("fct: turtle.back()")
- end
- if storage.direction == 0 then
- storage.pos[1] = storage.pos[1] - 1
- elseif storage.direction == 1 then
- storage.pos[3] = storage.pos[3] - 1
- elseif storage.direction == 2 then
- storage.pos[1] = storage.pos[1] + 1
- elseif storage.direction == 3 then
- storage.pos[3] = storage.pos[3] + 1
- end
- return true
- end,
- up = function()
- if storage.testOutput then
- print("fct: turtle.up()")
- end
- storage.pos[2] = storage.pos[2] + 1
- return true
- end,
- down = function()
- if storage.testOutput then
- print("fct: turtle.down()")
- end
- if test then
- storage.pos[2] = storage.pos[2] - 1
- end
- return true
- end,
- turnLeft = function()
- if storage.testOutput then
- print("fct: turtle.turnLeft()")
- end
- storage.direction = storage.direction - 1
- if storage.direction < 0 then
- storage.direction = storage.direction + 4
- end
- return true
- end,
- turnRight = function()
- if storage.testOutput then
- print("fct: turtle.turnRight()")
- end
- storage.direction = storage.direction + 1
- if storage.direction > 3 then
- storage.direction = storage.direction - 4
- end
- return true
- end,
- select = function(id)
- if storage.testOutput then
- print("fct: turtle.select(" .. id .. ")")
- end
- return true
- end,
- place = function()
- if storage.testOutput then
- print("fct: turtle.place()")
- end
- return true
- end,
- placeUp = function()
- if storage.testOutput then
- print("fct: turtle.placeUp()")
- end
- return true
- end,
- placeDown = function()
- if storage.testOutput then
- print("fct: turtle.placeDown()")
- end
- return true
- end,
- dig = function()
- if storage.testOutput then
- print("fct: turtle.dig()")
- end
- return true
- end,
- digUp = function()
- if storage.testOutput then
- print("fct: turtle.digUp()")
- end
- return true
- end,
- digDown = function()
- if storage.testOutput then
- print("fct: turtle.digDown()")
- end
- return true
- end,
- attack = function()
- if storage.testOutput then
- print("fct: turtle.attack()")
- end
- return true
- end,
- attackUp = function()
- if storage.testOutput then
- print("fct: turtle.attackUp()")
- end
- return true
- end,
- attackDown = function()
- if storage.testOutput then
- print("fct: turtle.attackDown()")
- end
- return true
- end,
- detect = function()
- if storage.space ~= nil then
- local pos = storage.pos
- local o = storage.direction
- if o == 0 then
- if #storage.space > pos[1] then
- return #storage.space[pos[1] + 1][pos[2]][pos[3]] == AREA_SET
- end
- return false
- elseif o == 1 then
- if #storage.space[1][1] > pos[3] then
- return #storage.space[pos[1]][pos[2]][pos[3] + 1] == AREA_SET
- end
- return false
- elseif o == 2 then
- if #storage.space < pos[1] then
- return #storage.space[pos[1] - 1][pos[2]][pos[3]] == AREA_SET
- end
- return false
- elseif o == 3 then
- if #storage.space[1][1] < pos[3] then
- return #storage.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 storage.space ~= nil then
- local pos = storage.pos
- if #storage.space[1] > pos[2] then
- return #storage.space[pos[1]][pos[2] + 1][pos[3]] == AREA_SET
- end
- return false
- else
- return false;
- end
- end,
- detectDown = function()
- if storage.space ~= nil then
- local pos = storage.pos
- if #storage.space[1] < pos[2] then
- return #storage.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 storage.pos[1], storage.pos[2], storage.pos[3]
- end
- }
- _G.peripheral = {
- call = function(arg1, arg2)
- if storage.testOutput then
- print("fct: peripheral.call(" .. arg1 .. ", " .. arg2 .. ")")
- end
- end
- }
- end
- end
- setTest(test)
- return {
- api = defaultFunctions,
- onInitStorage = function(_storage, _save)
- storage = _storage
- DFE.setStorageData(storage)
- save = _save
- if not storage.activeNetworks then
- storage.activeNetworks = {}
- storage.currentCallId = 0
- storage.responses = {}
- storage.callConfirmations = {}
- storage.confirmationTimeout = CONFIRMATION_TIMEOUT
- end
- if test and storage.pos == nil then
- storage.pos = { 0, 0, 0 }
- end
- if storage.direction == nil then
- storage.direction = 0
- end
- if storage.nullOr == nil then
- storage.nullOr = 0
- end
- if storage.testOutput == nil then
- storage.testOutput = true
- end
- end,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement