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 = "patissier2.dat"
- local funcTable = { }
- local scriptList = { }
- local properties = { }
- local 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
- local function rep(count, ...)
- return { blockType = "REPEAT",
- repeatCount = count,
- action = toNameList(toFlatList({...})),
- currentLoop = 1,
- currentStep = 1 }
- end
- local 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
- local function breakLast()
- -- break when last
- end
- local function swOddEven(oddList, evenList)
- -- switchOddEven
- return { blockType = "SW_ODD_EVEN",
- [ "oddList"] = rep(1, oddList),
- ["evenList"] = rep(1, evenList) }
- end
- local function anotherLast(ordinaryList, anotherList)
- -- another when last
- return { blockType = "ANOTHER_LAST",
- ["ordinaryList"] = rep(1, ordinaryList),
- [ "anotherList"] = rep(1, anotherList)}
- end
- local 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
- local function executeScript()
- doAction(scriptList, 0)
- debug("[ executeScript() ] end")
- end
- registFunction("breakLast", breakLast)
- registFunction("swOddEven", swOddEven)
- registFunction("anotherLast", anotherLast)
- -- ------------------------------------
- -- application
- -- ------------------------------------
- local f = function() while not turtle.forward() do end end
- local b = turtle.back
- local l = turtle.turnLeft
- local r = turtle.turnRight
- local u = turtle.up
- local d = turtle.down
- local e1 = turtle.dig
- registFunction("f", f)
- registFunction("b", b)
- registFunction("l", l)
- registFunction("r", r)
- registFunction("u", u)
- registFunction("d", d)
- registFunction("e1", e1)
- local function test()
- print("this is a test.")
- end
- -- ------------------------------------
- -- main
- -- ------------------------------------
- debug("----------------------------------------")
- debug("-- patissier2")
- debug("----------------------------------------")
- registFunction("test", test)
- --<<test 1>>
- --registScript(f,l,r,b)
- --<<test 2>>
- --local rotDig = {l,e1,l,e1,l,e1,l,e1}
- --registScript(u,rotDig,d)
- --<<test 3>>
- --registScript(rep(4,f,f,r))
- --<<test 4>>
- --registScript(rep(4,f,f,r),f,f,u,d,b,b)
- --<<test 5>>
- --registScript(rep(4,f,f,r),rep(4,l,b,b))
- --<<test 6>>
- --registScript(rep(4,rep(3,f),rep(3,b),r,f,l))
- --<<test 7>>
- --registScript(rep(4,rep(3,f),rep(3,b),breakLast,r,f,l))
- --<<test 8>>
- --registScript(rep(4,rep(3,f),swOddEven({r,f,r},{l,f,l})))
- --<<test 9>>
- --registScript(rep(4,rep(3,f),rep(3,b),anotherLast({r,f,l},{l,rep(3,f),r})))
- --<<test 10>>
- registScript(rep(4,rep(4,f,f,f,r)))
- debug("==========")
- debug(textutils.serialize(scriptList))
- debug("==========")
- loadData()
- executeScript()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement