Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- LOG_FILE_NAME = "debug.log";
- local function log(txt)
- local hFile;
- if fs.exists(LOG_FILE_NAME) then
- hFile = fs.open(LOG_FILE_NAME, "a");
- else
- hFile = fs.open(LOG_FILE_NAME, "w");
- end
- hFile.writeLine(txt);
- hFile.close();
- end
- function split(src, sepalator)
- local ret = { }
- local tmp = ""
- for i = 1, #src do
- if sepalator == src:sub(i, i) then
- table.insert(ret, tmp)
- tmp = ""
- else
- tmp = tmp .. src:sub(i, i)
- end
- end
- if "" ~= tmp then
- table.insert(ret, tmp)
- end
- return ret;
- end
- local function loopHead()
- -- do nothing
- end
- local function loopTail(args, status)
- local loopCnt = tonumber(args[2])
- local headIdx = args[3]
- local tailIdx = args[4]
- local loopData = status.loopInfo[tailIdx]
- if nil == loopData then
- status.loopInfo[tailIdx] = { currentIteration = 1 }
- loopData = status.loopInfo[tailIdx]
- end
- local currIter = loopData.currentIteration
- if loopCnt <= currIter then
- status.loopInfo[tailIdx].currentIteration = 1
- status.currentStep = tailIdx + 1
- else
- status.loopInfo[tailIdx].currentIteration = currIter + 1
- status.currentStep = headIdx
- end
- return status
- end
- local function myReboot()
- print("reboot!")
- end
- local function mySleep(millisec)
- sleep(millisec / 1000)
- end
- local function createMySetRedStoneOutputFunc(side)
- return function(value)
- local dirName = { [0] = "up" , [1] = "front", [2] = "down",
- [3] = "left", [4] = "back" , [5] = "right" }
- local onOff = { [0] = false, [1] = true }
- rs.setOutput(dirName[side], onOff[value])
- -- TODO status
- end
- end
- local function vacuum(slotNo)
- turtle.select(slotNo)
- for i = 1, 16 do
- if slotNo ~= i and turtle.compareTo(i) then
- turtle.select(i)
- turtle.transferTo(slotNo, turtle.getItemSpace(slotNo))
- if 0 == turtle.getItemSpace(slotNo) then
- break
- end
- turtle.select(slotNo)
- end
- end
- turtle.select(slotNo)
- end
- local doAllCurrentSelectIdx
- local function createMyDrop(dropFunc)
- return function()
- if 0 == turtle.getItemCount(doAllCurrentSelectIdx) then
- return true
- else
- return dropFunc()
- end
- end
- end
- local function createDoAllFunc(func, needSelect)
- return function()
- for i = 1, 16 do
- if needSelect then
- turtle.select(i)
- doAllCurrentSelectIdx = i
- end
- func(i)
- end
- end
- end
- local function createRetryFunc(func, sleepSec, sleepMsg)
- return function()
- while true do
- if not func() then
- print(sleepMsg)
- term.write("(sleep " .. tostring(sleepSec) .. ")")
- local cnt = math.floor(sleepSec)
- local fraction = sleepSec - cnt
- for i = 1, cnt do
- term.write(".")
- sleep(1)
- end
- if 0 < fraction then
- term.write(".")
- sleep(fraction)
- end
- else
- break
- end
- end
- end
- end
- local commands = {
- _fuel = { numArgs = 1, func = turtle.refuel },
- _reboot = { numArgs = 0, func = myReboot },
- _loopHead = { numArgs = 0, func = loopHead },
- _loopTail = { numArgs = 1, func = loopTail },
- l = { numArgs = 0, func = turtle.turnLeft },
- r = { numArgs = 0, func = turtle.turnRight },
- f = { numArgs = 0, func = createRetryFunc(turtle.forward, 1, "cannot move forward") },
- b = { numArgs = 0, func = createRetryFunc(turtle.back , 1, "cannot move back" ) },
- u = { numArgs = 0, func = createRetryFunc(turtle.up , 1, "cannot move up" ) },
- d = { numArgs = 0, func = createRetryFunc(turtle.down , 1, "cannot move down" ) },
- s = { numArgs = 1, func = turtle.select },
- p0 = { numArgs = 0, func = turtle.placeUp },
- p1 = { numArgs = 0, func = turtle.place },
- p2 = { numArgs = 0, func = turtle.placeDown },
- t0 = { numArgs = 0, func = turtle.dropUp },
- t1 = { numArgs = 0, func = turtle.drop },
- t2 = { numArgs = 0, func = turtle.dropDown },
- e0 = { numArgs = 0, func = turtle.digUp },
- e1 = { numArgs = 0, func = turtle.dig },
- e2 = { numArgs = 0, func = turtle.digDown },
- z = { numArgs = 1, func = mySleep },
- o1 = { numArgs = 1, func = createMySetRedStoneOutputFunc(1) },
- o2 = { numArgs = 1, func = createMySetRedStoneOutputFunc(2) },
- o3 = { numArgs = 1, func = createMySetRedStoneOutputFunc(3) },
- o4 = { numArgs = 1, func = createMySetRedStoneOutputFunc(4) },
- o5 = { numArgs = 1, func = createMySetRedStoneOutputFunc(5) },
- o6 = { numArgs = 1, func = createMySetRedStoneOutputFunc(6) },
- v = { numArgs = 1, func = vacuum },
- k0 = { numArgs = 0, func = turtle.suckUp },
- k1 = { numArgs = 0, func = turtle.suck },
- k2 = { numArgs = 0, func = turtle.suckDown },
- a0 = { numArgs = 0, func = turtle.attackUp },
- a1 = { numArgs = 0, func = turtle.attack },
- a2 = { numArgs = 0, func = turtle.attackDown },
- T0 = { numArgs = 0, func = createDoAllFunc(createRetryFunc(createMyDrop(turtle.dropUp ), 10, "cannot drop up") , true) },
- T1 = { numArgs = 0, func = createDoAllFunc(createRetryFunc(createMyDrop(turtle.drop ), 10, "cannot drop forward"), true) },
- T2 = { numArgs = 0, func = createDoAllFunc(createRetryFunc(createMyDrop(turtle.dropDown), 10, "cannot drop down") , true) },
- K0 = { numArgs = 0, func = createDoAllFunc(turtle.suckUp , false) },
- K1 = { numArgs = 0, func = createDoAllFunc(turtle.suck , false) },
- K2 = { numArgs = 0, func = createDoAllFunc(turtle.suckDown, false) },
- }
- local function findLoopHead(cmdList)
- local closeCnt = 1
- for i = #cmdList, 1, -1 do
- local cmdName = cmdList[i][1]
- local tmp = 0
- if "_loopHead" == cmdName then
- tmp = -1
- elseif "_loopTail" == cmdName then
- tmp = 1
- end
- if 0 ~= tmp then
- closeCnt = closeCnt + tmp
- if 0 == closeCnt then
- return i
- end
- end
- end
- error("Correspondence of a parenthesis is inaccurate. (cmdIdx = " .. #cmdList .. ")")
- end
- local function parse(script)
- local replTmp = { }
- local tmp = script .. "\n"
- tmp = tmp:gsub("%-%-%[%[[^]]*%-%-%]%]", "")
- tmp = tmp:gsub("%-%-[^\n]*\n", "")
- tmp = tmp:gsub("%(", "{loopHead} ")
- tmp = tmp:gsub("%)%s*(%d+)", "{loopTail}%1")
- local j = 0
- for i = 1, 2 do
- for key, v in pairs(commands) do
- if (1 == i and "_" == key:sub(1, 1)) or
- (2 == i and "_" ~= key:sub(1, 1)) then
- local tempLabel = "{" .. tostring(j) .. "}"
- j = j + 1
- local keyPtrn
- if "_" == key:sub(1, 1) then
- keyPtrn = "({[^}]*" .. key:sub(2) .. "[^{]*})"
- else
- keyPtrn = "(" .. key .. ")"
- end
- if 0 == v.numArgs then
- tmp = tmp:gsub(keyPtrn, tempLabel .. " ")
- replTmp[tempLabel] = key
- elseif 1 == v.numArgs then
- tmp = tmp:gsub(keyPtrn .. "%s*(%d+)", tempLabel .. ",%2 ")
- replTmp[tempLabel] = key
- elseif 2 == v.numArgs then
- tmp = tmp:gsub(keyPtrn .. "%s*(%d+)%s*,%s(%d+)", tempLabel .. ",%2,%3 ")
- replTmp[tempLabel] = key
- end
- end
- end
- end
- for key, v in pairs(replTmp) do
- tmp = tmp:gsub(key, v)
- end
- tmp = tmp:gsub("^%s+", "")
- tmp = tmp:gsub("%s*$", " ")
- tmp = tmp:gsub("%s+" , " ")
- local stIdx = 1
- local idx
- local params
- rslt = { }
- while true do
- idx = tmp:find(" ", stIdx, true)
- if nil == idx then
- break
- end
- params = split(tmp:sub(stIdx, idx - 1), ",")
- if "_loopTail" == params[1] then
- table.insert(params, findLoopHead(rslt))
- table.insert(params, #rslt + 1)
- end
- table.insert(rslt, params)
- stIdx = idx + 1
- end
- return rslt
- end
- local function createInitialStatus()
- return {
- currentStep = 1,
- loopInfo = { }
- }
- end
- local function doCommand(v, status)
- if "_loopTail" ~= v[1] then
- if 1 == #v then
- commands[v[1]].func()
- elseif 2 == #v then
- commands[v[1]].func(tonumber(v[2]))
- elseif 3 == #v then
- commands[v[1]].func(tonumber(v[2]), tonumber(v[3]))
- end
- status.currentStep = status.currentStep + 1
- if "_reboot" == v[1] then
- return status, true
- else
- return status
- end
- else
- status = commands[v[1]].func(v, status)
- return status
- end
- end
- local TINY_TR_SCRIPT = "tiny_tr.script"
- local TINY_TR_CONFIG = "tiny_tr.config"
- local TINY_TR_STATUS = "tiny_tr.status"
- local function runScript(scriptTbl, status, saveStatusFlg)
- local rebootFlg = false
- while true do
- if status.currentStep <= #scriptTbl then
- local v = scriptTbl[math.floor(status.currentStep)]
- status, rebootFlg = doCommand(v, status)
- if saveStatusFlg then
- fileOverwrite(TINY_TR_STATUS, textutils.serialize(status))
- end
- if rebootFlg then
- os.reboot()
- end
- else
- status.currentStep = 1
- break
- end
- end
- return status
- end
- local CONFIG_TEMPLATE =[[
- INFINITE_LOOP = false
- ]]
- -- ------------------------------------
- -- main
- -- ------------------------------------
- local args = { ... }
- local scriptTbl
- if 1 <= #args then
- local makeOnly = false
- local srcFile
- if 2 <= #args then
- if "-c" == args[1] then
- makeOnly = true
- srcFile = args[2]
- else
- print("bad argument.")
- return
- end
- else
- srcFile = args[1]
- end
- scriptTbl = parse(fileReadAll(srcFile))
- fileOverwrite(TINY_TR_SCRIPT, textutils.serialize(scriptTbl))
- if not fs.exists(TINY_TR_CONFIG) then
- fileOverwrite(TINY_TR_CONFIG, CONFIG_TEMPLATE)
- end
- fs.delete(TINY_TR_STATUS)
- fileOverwrite(TINY_TR_STATUS, createInitialStatus())
- if not makeOnly then
- runScript(scriptTbl, createInitialStatus(), false)
- end
- print("tinyTr2 was completed.")
- else
- scriptTbl = textutils.unserialize(fileReadAll(TINY_TR_SCRIPT))
- local status = textutils.unserialize(fileReadAll(TINY_TR_STATUS))
- dofile(TINY_TR_CONFIG)
- while true do
- status = runScript(scriptTbl, status, true)
- if not INFINITE_LOOP then
- print("tinyTr2 was completed.")
- break
- else
- print("infinite loop.")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement