Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ------------------------------------
- -- utility
- -- ------------------------------------
- local function fileReadAll(filePath)
- local hFile = fs.open(filePath, "r")
- local txt = hFile.readAll()
- hFile.close()
- return txt
- end
- local function fileOverwrite(fileName, text)
- local hFile = fs.open(fileName, "w")
- hFile.writeLine(text)
- hFile.close()
- end
- -- ---------------
- -- Logger
- -- ---------------
- -- do logging?
- DEBUG__DO_LOGGING = true
- -- local log
- DEBUG__LOCAL_ENABLED = true
- DEBUG__LOCAL_LOG_FILE_NAME = "debug.log"
- -- remote log
- DEBUG__REMOTE_ENABLED = false
- DEBUG__REMOTE_ADDR = 20
- DEBUG__REMOTE_MODEM_DIR = "right"
- function debug(txt)
- if DEBUG__DO_LOGGING then
- if DEBUG__LOCAL_ENABLED then
- local hFile
- if fs.exists(DEBUG__LOCAL_LOG_FILE_NAME) then
- hFile = fs.open(DEBUG__LOCAL_LOG_FILE_NAME, "a");
- else
- hFile = fs.open(DEBUG__LOCAL_LOG_FILE_NAME, "w");
- end
- hFile.writeLine(txt);
- hFile.close();
- end
- if DEBUG__REMOTE_ENABLED then
- rednet.open(DEBUG__REMOTE_MODEM_DIR)
- rednet.send(DEBUG__REMOTE_ADDR, txt)
- rednet.close()
- end
- end
- end
- -- ------------------------------------
- -- framework
- -- ------------------------------------
- local DATA_FILE_NAME = "turtleMove.dat"
- local funcTable = { }
- local scriptList = { }
- local properties = { }
- function registFunction(name, func)
- table.insert(funcTable, { ["name"] = name, ["func"] = func })
- end
- local function findFunc(element)
- for i, v in ipairs(funcTable) do
- if v.func == element then
- return v.name
- end
- end
- debug("findFunc() func is not registered.")
- error("findFunc() func is not registered.")
- end
- local function findFuncByName(name)
- for i, v in ipairs(funcTable) do
- if v.name == name then
- return v.func
- end
- end
- debug(indent .. "findFuncByName() name is not registered.")
- error(indent .. "findFuncByName() name is not registered.")
- end
- local function toFlatList(list)
- local rslt = { }
- for i, v in ipairs(list) do
- if "function" == type(v) then
- table.insert(rslt, v)
- elseif "table" == type(v) then
- if nil ~= v.blockType then
- table.insert(rslt, v)
- else
- for _, v in ipairs(toFlatList(v)) do
- table.insert(rslt, v)
- end
- end
- end
- end
- return rslt
- end
- local function toNameList(elementList)
- local list = { }
- for i, element in ipairs(elementList) do
- if "table" == type(element) then
- if nil ~= element.blockType then
- table.insert(list, element)
- end
- else
- table.insert(list, findFunc(element))
- end
- end
- return list
- end
- function rep(count, ...)
- return { blockType = "REPEAT",
- repeatCount = count,
- action = toNameList(toFlatList({...})),
- currentLoop = 1,
- currentStep = 1 }
- end
- function registScript(...)
- if nil == scriptList.action then
- scriptList.blockType = "REPEAT"
- scriptList.repeatCount = 1
- scriptList.action = { }
- scriptList.currentLoop = 1
- scriptList.currentStep = 1
- end
- table.insert(scriptList.action, rep(1, ...))
- end
- function breakLast()
- -- break when last
- end
- function swOddEven(oddList, evenList)
- -- switchOddEven
- return { blockType = "SW_ODD_EVEN",
- [ "oddList"] = rep(1, oddList),
- ["evenList"] = rep(1, evenList) }
- end
- function anotherLast(ordinaryList, anotherList)
- -- another when last
- return { blockType = "ANOTHER_LAST",
- ["ordinaryList"] = rep(1, ordinaryList),
- [ "anotherList"] = rep(1, anotherList)}
- end
- function loadData()
- if fs.exists(DATA_FILE_NAME) then
- local data = textutils.unserialize(fileReadAll(DATA_FILE_NAME))
- scriptList = data.scriptList
- return true
- else
- return false
- end
- end
- local function saveData()
- local data = { }
- data.scriptList = scriptList
- fileOverwrite(DATA_FILE_NAME, textutils.serialize(data))
- end
- local function doAction(block, level)
- local indent = string.rep(" ", level)
- local breakFlg = false
- for i = block.currentLoop, block.repeatCount do
- block.currentLoop = i
- for j = block.currentStep, #block.action do
- block.currentStep = j
- saveData()
- --debug(indent .. string.format("doAction(i, j) = %d, %d begin", i, j))
- local act = block.action[j]
- if "string" == type(act) then
- if "breakLast" == act then
- if i == block.repeatCount then
- breakFlg = true
- break
- end
- else
- --debug(indent .. "-> do [".. act .."]")
- local func = findFuncByName(act)
- func()
- end
- elseif "REPEAT" == act.blockType then
- doAction(act, level + 1)
- elseif "SW_ODD_EVEN" == act.blockType then
- if 1 == i % 2 then
- doAction(act.oddList, level + 1)
- else
- doAction(act.evenList, level + 1)
- end
- elseif "ANOTHER_LAST" == act.blockType then
- if i == block.repeatCount then
- doAction(act.anotherList, level + 1)
- else
- doAction(act.ordinaryList, level + 1)
- end
- end
- end
- block.currentStep = 1
- if breakFlg then
- break
- end
- end
- block.currentLoop = 1
- saveData()
- end
- function executeScript()
- doAction(scriptList, 0)
- --debug("[ executeScript() ] end")
- end
- registFunction("breakLast", breakLast)
- registFunction("swOddEven", swOddEven)
- registFunction("anotherLast", anotherLast)
- -- ------------------------------------
- -- application
- -- ------------------------------------
- local function registFunctionAll(baseName, funcs)
- for i = 1, #funcs do
- registFunction(baseName .. tostring(i), funcs[i])
- end
- end
- local function makeFunc1to16(func)
- local funcs = { }
- for i = 1, 16 do
- funcs[i] =
- function()
- func(i)
- end
- end
- return unpack(funcs)
- end
- local function makeExcavateFunc(detectFunc, digFunc)
- return function()
- if not detectFunc() then digFunc() return end
- while not digFunc() do end
- end
- end
- function vacuum(slotNo)
- if 0 == turtle.getItemCount(slotNo) then
- return
- end
- local result = false
- turtle.select(slotNo)
- for i = 1, 16 do
- if slotNo ~= i and turtle.compareTo(i) and 1 < turtle.getItemCount(i) then
- turtle.select(i)
- turtle.transferTo(slotNo, turtle.getItemSpace(slotNo))
- result = true
- if 0 == turtle.getItemSpace(slotNo) then
- break
- end
- turtle.select(slotNo)
- end
- end
- turtle.select(slotNo)
- return result
- end
- function makeAndRegistFunc(name, func, argArray)
- local f
- if nil == argArray then
- f = function() func() end
- else
- f = function() func(unpack(argArray)) end
- end
- registFunction(name, f)
- return f
- end
- function refuelPrompt()
- local firstTime = true;
- while true do
- local rslt, msg;
- if firstTime then
- firstTime = false;
- rslt = false;
- msg = "Out of fuel";
- else
- return true;
- end
- if not rslt and msg == "Out of fuel" then
- print("Out of fuel. please select [r]efuel or [q]uit.");
- print("(r/q)");
- local ch = read();
- if "q" == ch then
- print("Are you sure you want to stop the script?");
- print("(y/n)");
- ch = read();
- if "y" == ch then
- error("out of fuel.");
- end
- elseif "r" == ch then
- local selSlot = 1;
- while true do
- print("Please type current selected slot number.");
- ch = read();
- local num = tonumber(ch);
- if nil ~= num and 1 <= num and num <= 16 then
- selSlot = num;
- break;
- else
- print("Wrong input. Please retry.");
- end
- end
- while true do
- print("please set fuel item and type slot number.");
- print("When you finish refueling, Please type q.");
- print("FuelLevel = " .. turtle.getFuelLevel());
- ch = read();
- local num = tonumber(ch);
- if nil ~= num and 1 <= num and num <= 16 then
- turtle.select(num);
- local fRslt, fMsg = turtle.refuel();
- if not fRslt then
- print(fMsg);
- end
- elseif "q" == ch then
- turtle.select(selSlot);
- break;
- else
- print("Wrong input. Please retry.");
- end
- end
- end
- else
- return rslt, msg;
- end
- end
- end
- f = function() while not turtle.forward() do end end
- b = function() while not turtle.back() do end end
- l = turtle.turnLeft
- r = turtle.turnRight
- u = function() while not turtle.up() do end end
- d = function() while not turtle.down() do end end
- e0 = makeExcavateFunc(turtle.detectUp, turtle.digUp)
- e1 = makeExcavateFunc(turtle.detect, turtle.dig)
- e2 = makeExcavateFunc(turtle.detectDown, turtle.digDown)
- k0 = turtle.suckUp
- k1 = turtle.suck
- k2 = turtle.suckDown
- t0 = turtle.dropUp
- t1 = turtle.drop
- t2 = turtle.dropDown
- p0 = turtle.placeUp
- p1 = turtle.place
- p2 = turtle.placeDown
- v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16 = makeFunc1to16(vacuum)
- s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16 = makeFunc1to16(turtle.select)
- function displayFuelLevel()
- print("fuelLevel = " .. tostring(turtle.getFuelLevel()))
- debug("fuelLevel = " .. tostring(turtle.getFuelLevel()))
- end
- registFunction("f", f)
- registFunction("b", b)
- registFunction("l", l)
- registFunction("r", r)
- registFunction("u", u)
- registFunction("d", d)
- registFunction("e0", e0)
- registFunction("e1", e1)
- registFunction("e2", e2)
- registFunction("k0", k0)
- registFunction("k1", k1)
- registFunction("k2", k2)
- registFunction("t0", t0)
- registFunction("t1", t1)
- registFunction("t2", t2)
- registFunction("p0", p0)
- registFunction("p1", p1)
- registFunction("p2", p2)
- registFunctionAll("v",{v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16})
- registFunctionAll("s", {s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16})
- registFunction("displayFuelLevel", displayFuelLevel)
- registFunction("refuelPrompt", refuelPrompt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement