G2A Many GEOs
SHARE
TWEET

patissier2

kssr3951 Jul 13th, 2015 (edited) 193 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ------------------------------------
  2. -- utility
  3. -- ------------------------------------
  4. local function fileReadAll(filePath)
  5.   local hFile = fs.open(filePath, "r")
  6.   local txt = hFile.readAll()
  7.   hFile.close()
  8.   return txt
  9. end
  10. local function fileOverwrite(fileName, text)
  11.   local hFile = fs.open(fileName, "w")
  12.   hFile.writeLine(text)
  13.   hFile.close()
  14. end
  15. -- ---------------
  16. -- Logger
  17. -- ---------------
  18. -- do logging?
  19. DEBUG__DO_LOGGING           = true
  20. -- local log
  21. DEBUG__LOCAL_ENABLED        = true
  22. DEBUG__LOCAL_LOG_FILE_NAME  = "debug.log"
  23. -- remote log
  24. DEBUG__REMOTE_ENABLED       = false
  25. DEBUG__REMOTE_ADDR          = 20
  26. DEBUG__REMOTE_MODEM_DIR     = "right"
  27. function debug(txt)
  28.   if DEBUG__DO_LOGGING then
  29.     if DEBUG__LOCAL_ENABLED then
  30.       local hFile
  31.       if fs.exists(DEBUG__LOCAL_LOG_FILE_NAME) then
  32.         hFile = fs.open(DEBUG__LOCAL_LOG_FILE_NAME, "a");
  33.       else
  34.         hFile = fs.open(DEBUG__LOCAL_LOG_FILE_NAME, "w");
  35.       end
  36.       hFile.writeLine(txt);
  37.       hFile.close();
  38.     end
  39.     if DEBUG__REMOTE_ENABLED then
  40.       rednet.open(DEBUG__REMOTE_MODEM_DIR)
  41.       rednet.send(DEBUG__REMOTE_ADDR, txt)
  42.       rednet.close()
  43.     end
  44.   end
  45. end
  46. -- ------------------------------------
  47. -- framework
  48. -- ------------------------------------
  49. local DATA_FILE_NAME = "patissier2.dat"
  50. local funcTable = { }
  51. local scriptList = { }
  52. local properties = { }
  53. local function registFunction(name, func)
  54.   table.insert(funcTable, { ["name"] = name, ["func"] = func })
  55. end
  56. local function findFunc(element)
  57.   for i, v in ipairs(funcTable) do
  58.     if v.func == element then
  59.       return v.name
  60.     end
  61.   end
  62.   debug("findFunc() func is not registered.")
  63.   error("findFunc() func is not registered.")
  64. end
  65. local function findFuncByName(name)
  66.   for i, v in ipairs(funcTable) do
  67.     if v.name == name then
  68.       return v.func
  69.     end
  70.   end
  71.   debug(indent .. "findFuncByName() name is not registered.")
  72.   error(indent .. "findFuncByName() name is not registered.")
  73. end
  74. local function toFlatList(list)
  75.   local rslt = { }
  76.   for i, v in ipairs(list) do
  77.     if "function" == type(v) then
  78.       table.insert(rslt, v)
  79.     elseif "table" == type(v) then
  80.       if nil ~= v.blockType then
  81.         table.insert(rslt, v)
  82.       else
  83.         for _, v in ipairs(toFlatList(v)) do
  84.           table.insert(rslt, v)
  85.         end
  86.       end
  87.     end
  88.   end
  89.   return rslt
  90. end
  91. local function toNameList(elementList)
  92.   local list = { }
  93.   for i, element in ipairs(elementList) do
  94.     if "table" == type(element) then
  95.       if nil ~= element.blockType then
  96.         table.insert(list, element)
  97.       end
  98.     else
  99.       table.insert(list, findFunc(element))      
  100.     end
  101.   end
  102.   return list
  103. end
  104. local function rep(count, ...)
  105.   return { blockType   = "REPEAT",
  106.            repeatCount = count,
  107.            action      = toNameList(toFlatList({...})),
  108.            currentLoop = 1,
  109.            currentStep = 1 }
  110. end
  111. local function registScript(...)
  112.   if nil == scriptList.action then
  113.     scriptList.blockType   = "REPEAT"
  114.     scriptList.repeatCount = 1
  115.     scriptList.action      = { }
  116.     scriptList.currentLoop = 1
  117.     scriptList.currentStep = 1
  118.   end
  119.   table.insert(scriptList.action, rep(1, ...))
  120. end
  121. local function breakLast()
  122.   -- break when last
  123. end
  124. local function swOddEven(oddList, evenList)
  125.   -- switchOddEven
  126.   return { blockType        = "SW_ODD_EVEN",
  127.            [ "oddList"]   = rep(1,  oddList),
  128.            ["evenList"]   = rep(1, evenList) }
  129. end
  130. local function anotherLast(ordinaryList, anotherList)
  131.   -- another when last
  132.   return { blockType        = "ANOTHER_LAST",
  133.            ["ordinaryList"] = rep(1, ordinaryList),
  134.            [ "anotherList"] = rep(1,  anotherList)}
  135. end
  136. local function loadData()
  137.   if fs.exists(DATA_FILE_NAME) then
  138.     local data = textutils.unserialize(fileReadAll(DATA_FILE_NAME))
  139.     scriptList = data.scriptList
  140.     return true
  141.   else
  142.     return false
  143.   end
  144. end
  145. local function saveData()
  146.   local data = { }
  147.   data.scriptList = scriptList
  148.   fileOverwrite(DATA_FILE_NAME, textutils.serialize(data))
  149. end
  150. local function doAction(block, level)
  151.   local indent = string.rep("    ", level)
  152.   local breakFlg = false
  153.   for i = block.currentLoop, block.repeatCount do
  154.     block.currentLoop = i
  155.     for j = block.currentStep, #block.action do
  156.       block.currentStep = j
  157.       saveData()
  158.       debug(indent .. string.format("doAction(i, j) = %d, %d begin", i, j))
  159.       local act = block.action[j]
  160.       if "string" == type(act) then
  161.         if "breakLast" == act then
  162.           if i == block.repeatCount then
  163.             breakFlg = true
  164.             break
  165.           end
  166.         else
  167.           debug(indent .. "-> do [".. act .."]")
  168.           local func = findFuncByName(act)
  169.           func()
  170.         end
  171.       elseif "REPEAT" == act.blockType then
  172.         doAction(act, level + 1)
  173.       elseif "SW_ODD_EVEN" == act.blockType then
  174.         if 1 == i % 2 then
  175.           doAction(act.oddList, level + 1)
  176.         else
  177.           doAction(act.evenList, level + 1)
  178.         end
  179.       elseif "ANOTHER_LAST" == act.blockType then
  180.         if i == block.repeatCount then
  181.           doAction(act.anotherList, level + 1)
  182.         else
  183.           doAction(act.ordinaryList, level + 1)
  184.         end
  185.       end
  186.     end
  187.     block.currentStep = 1
  188.     if breakFlg then
  189.       break
  190.     end
  191.   end
  192.   block.currentLoop = 1
  193.   saveData()
  194. end
  195. local function executeScript()
  196.   doAction(scriptList, 0)
  197.   debug("[ executeScript() ] end")
  198. end
  199. registFunction("breakLast", breakLast)
  200. registFunction("swOddEven", swOddEven)
  201. registFunction("anotherLast", anotherLast)
  202. -- ------------------------------------
  203. -- application
  204. -- ------------------------------------
  205. local f = function() while not turtle.forward() do end end
  206. local b = turtle.back
  207. local l = turtle.turnLeft
  208. local r = turtle.turnRight
  209. local u = turtle.up
  210. local d = turtle.down
  211. local e1 = turtle.dig
  212. registFunction("f", f)
  213. registFunction("b", b)
  214. registFunction("l", l)
  215. registFunction("r", r)
  216. registFunction("u", u)
  217. registFunction("d", d)
  218. registFunction("e1", e1)
  219. local function test()
  220.   print("this is a test.")
  221. end
  222. -- ------------------------------------
  223. -- main
  224. -- ------------------------------------
  225. debug("----------------------------------------")
  226. debug("-- patissier2")
  227. debug("----------------------------------------")
  228. registFunction("test", test)
  229.  
  230. --<<test 1>>
  231. --registScript(f,l,r,b)
  232.  
  233. --<<test 2>>
  234. --local rotDig = {l,e1,l,e1,l,e1,l,e1}
  235. --registScript(u,rotDig,d)
  236.  
  237. --<<test 3>>
  238. --registScript(rep(4,f,f,r))
  239.  
  240. --<<test 4>>
  241. --registScript(rep(4,f,f,r),f,f,u,d,b,b)
  242.  
  243. --<<test 5>>
  244. --registScript(rep(4,f,f,r),rep(4,l,b,b))
  245.  
  246. --<<test 6>>
  247. --registScript(rep(4,rep(3,f),rep(3,b),r,f,l))
  248.  
  249. --<<test 7>>
  250. --registScript(rep(4,rep(3,f),rep(3,b),breakLast,r,f,l))
  251.  
  252. --<<test 8>>
  253. --registScript(rep(4,rep(3,f),swOddEven({r,f,r},{l,f,l})))
  254.  
  255. --<<test 9>>
  256. --registScript(rep(4,rep(3,f),rep(3,b),anotherLast({r,f,l},{l,rep(3,f),r})))
  257.  
  258. --<<test 10>>
  259. registScript(rep(4,rep(4,f,f,f,r)))
  260.  
  261. debug("==========")
  262. debug(textutils.serialize(scriptList))
  263. debug("==========")
  264. loadData()
  265. executeScript()
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top