Advertisement
kssr3951

turtleMoveApi

Feb 28th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.12 KB | None | 0 0
  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 = "turtleMove.dat"
  50. local funcTable = { }
  51. local scriptList = { }
  52. local properties = { }
  53. 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. function rep(count, ...)
  105.   return { blockType   = "REPEAT",
  106.            repeatCount = count,
  107.            action      = toNameList(toFlatList({...})),
  108.            currentLoop = 1,
  109.            currentStep = 1 }
  110. end
  111. 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. function breakLast()
  122.   -- break when last
  123. end
  124. 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. 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. 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. 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 function registFunctionAll(baseName, funcs)
  206.   for i = 1, #funcs do
  207.     registFunction(baseName .. tostring(i), funcs[i])
  208.   end
  209. end
  210. local function makeFunc1to16(func)
  211.   local funcs = { }
  212.   for i = 1, 16 do
  213.     funcs[i] =
  214.       function()
  215.         func(i)
  216.       end
  217.   end
  218.   return unpack(funcs)
  219. end
  220. local function makeExcavateFunc(detectFunc, digFunc)
  221.   return function()
  222.     if not detectFunc() then digFunc() return end
  223.     while not digFunc() do end
  224.   end
  225. end
  226. function vacuum(slotNo)
  227.   if 0 == turtle.getItemCount(slotNo) then
  228.     return
  229.   end
  230.   local result = false
  231.   turtle.select(slotNo)
  232.   for i = 1, 16 do
  233.     if slotNo ~= i and turtle.compareTo(i) and 1 < turtle.getItemCount(i) then
  234.       turtle.select(i)
  235.       turtle.transferTo(slotNo, turtle.getItemSpace(slotNo))
  236.       result = true
  237.       if 0 == turtle.getItemSpace(slotNo) then
  238.         break
  239.       end
  240.       turtle.select(slotNo)
  241.     end
  242.   end
  243.   turtle.select(slotNo)
  244.   return result
  245. end
  246. function makeAndRegistFunc(name, func, argArray)
  247.   local f
  248.   if nil == argArray then
  249.     f = function() func() end
  250.   else
  251.     f = function() func(unpack(argArray)) end
  252.   end
  253.   registFunction(name, f)
  254.   return f
  255. end
  256. function refuelPrompt()
  257.   local firstTime = true;
  258.   while true do
  259.     local rslt, msg;
  260.     if firstTime then
  261.       firstTime = false;
  262.       rslt = false;
  263.       msg  = "Out of fuel";
  264.     else
  265.       return true;
  266.     end
  267.     if not rslt and msg == "Out of fuel" then
  268.       print("Out of fuel. please select [r]efuel or [q]uit.");
  269.       print("(r/q)");
  270.       local ch = read();
  271.       if "q" == ch then
  272.         print("Are you sure you want to stop the script?");
  273.         print("(y/n)");
  274.         ch = read();
  275.         if "y" == ch then
  276.           error("out of fuel.");
  277.         end
  278.       elseif "r" == ch then
  279.         local selSlot = 1;
  280.         while true do
  281.           print("Please type current selected slot number.");
  282.           ch = read();
  283.           local num = tonumber(ch);
  284.           if nil ~= num and 1 <= num and num <= 16 then
  285.             selSlot = num;
  286.             break;
  287.           else
  288.             print("Wrong input. Please retry.");
  289.           end
  290.         end
  291.         while true do
  292.           print("please set fuel item and type slot number.");
  293.           print("When you finish refueling, Please type q.");
  294.           print("FuelLevel = " .. turtle.getFuelLevel());
  295.           ch = read();
  296.           local num = tonumber(ch);
  297.           if nil ~= num and 1 <= num and num <= 16 then
  298.             turtle.select(num);
  299.             local fRslt, fMsg = turtle.refuel();
  300.             if not fRslt then
  301.               print(fMsg);
  302.             end
  303.           elseif "q" == ch then
  304.             turtle.select(selSlot);
  305.             break;
  306.           else
  307.             print("Wrong input. Please retry.");
  308.           end
  309.         end
  310.       end
  311.     else
  312.       return rslt, msg;
  313.     end
  314.   end
  315. end
  316. f = function() while not turtle.forward() do end end
  317. b = function() while not turtle.back() do end end
  318. l = turtle.turnLeft
  319. r = turtle.turnRight
  320. u = function() while not turtle.up() do end end
  321. d = function() while not turtle.down() do end end
  322. e0 = makeExcavateFunc(turtle.detectUp, turtle.digUp)
  323. e1 = makeExcavateFunc(turtle.detect, turtle.dig)
  324. e2 = makeExcavateFunc(turtle.detectDown, turtle.digDown)
  325. k0 = turtle.suckUp
  326. k1 = turtle.suck
  327. k2 = turtle.suckDown
  328. t0 = turtle.dropUp
  329. t1 = turtle.drop
  330. t2 = turtle.dropDown
  331. p0 = turtle.placeUp
  332. p1 = turtle.place
  333. p2 = turtle.placeDown
  334. v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16 = makeFunc1to16(vacuum)
  335. s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16 = makeFunc1to16(turtle.select)
  336. function displayFuelLevel()
  337.   print("fuelLevel = " .. tostring(turtle.getFuelLevel()))
  338.   debug("fuelLevel = " .. tostring(turtle.getFuelLevel()))
  339. end
  340. registFunction("f", f)
  341. registFunction("b", b)
  342. registFunction("l", l)
  343. registFunction("r", r)
  344. registFunction("u", u)
  345. registFunction("d", d)
  346. registFunction("e0", e0)
  347. registFunction("e1", e1)
  348. registFunction("e2", e2)
  349. registFunction("k0", k0)
  350. registFunction("k1", k1)
  351. registFunction("k2", k2)
  352. registFunction("t0", t0)
  353. registFunction("t1", t1)
  354. registFunction("t2", t2)
  355. registFunction("p0", p0)
  356. registFunction("p1", p1)
  357. registFunction("p2", p2)
  358. registFunctionAll("v",{v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16})
  359. registFunctionAll("s", {s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16})
  360. registFunction("displayFuelLevel", displayFuelLevel)
  361. registFunction("refuelPrompt", refuelPrompt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement