Advertisement
kssr3951

tr (turtle runner)

Jun 10th, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.79 KB | None | 0 0
  1. dofile("lib_log");
  2. dofile("lib_turtle_0.2.4");
  3.  
  4. local manText = [[
  5. NAME
  6.   tr - Turtle Runner
  7.  
  8. SYNOPSIS
  9.   tr [options] [FILE]
  10.  
  11. DESCRIPTION
  12.   tr is a program which operates a turtle using the script which can be described by a few keystrokes.
  13.   The script for tr consists of COMMANDS in which a name is extremely short. For example, forward is defined as "f", turnLeft is "l" and placeDown is "p2".
  14.   Moreover, control syntax is also prepared for tr. (Although it is only a repetition.)
  15.   The following example is the script which perform tofu construction.
  16.  
  17.   s1 (up2 ((fp2)4 r (fp2)3 r)2)3 fr (fp2)2 lfp2l fp2 rf p2rf dd p0 blbbd s2p1 rflfr e1fe0f ll s3p1 lbl
  18.  
  19.   *** Set items as follows. ***
  20.   slot1 : block x 48
  21.   slot2 : bed   x  1
  22.   slot3 : door  x  1
  23.  
  24.   When tr is executed, without specifying a FILE, the prompt for script is displayed.
  25.   After the input of a script, the prompt for file name is displayed.
  26.   If you don't want to save, press the Enter key, with no input.
  27.  
  28.  options
  29.    --help
  30.      display this help and exit.
  31.  
  32.    --demo
  33.      interactive input mode with "tofu construction" example.
  34.  
  35.  FILE
  36.    optional. The path of a script file.
  37.  
  38.  COMMANDS
  39.    f - <f>orward
  40.    b - <b>ack
  41.    l - turn <l>eft
  42.    r - turn <r>ight
  43.    u - <u>p
  44.    d - <d>own
  45.    s[slot]
  46.      - <s>elect
  47.        slot  - 1 ~ 16
  48.    p[side]
  49.      - <p>lace
  50.        side  - 0:up, 1:front, 2:down
  51.    t[side],[count]
  52.      - <t>hrow (= drop)
  53.        side  - 0:up, 1:front, 2:down
  54.        count
  55.    e[side]
  56.      - <e>xcavate (= dig)
  57.        side  - 0:up, 1:front, 2:down
  58.    z[millisecond]
  59.      - <z>zz = sleep
  60.    o[side],[value]
  61.      - set redstone <o>utput
  62.        side  - 0:up, 1:front, 2:down,
  63.                3:left, 4:back, 5:right
  64.        value - 0:off, 1:on
  65.    k[side]
  66.      - suc<k>
  67.        side  - 0:up, 1:front, 2:down
  68.    a[side]
  69.      - <a>ttack
  70.        side  - 0:up, 1:front, 2:down
  71.    v[id]
  72.      - user e<v>ent
  73.        id    -
  74.  Control syntax
  75.    (xx)[loopCount]
  76.      - loop
  77.        loopCount - loop count.
  78.  
  79. AUTHOR
  80.  kssr (http://cct.blog.shinobi.jp)
  81.  
  82. REPORTING BUGS
  83.  Report bugs to <http://cct.blog.shinobi.jp>.
  84.  
  85. COPYRIGHT
  86.  Copyright (C) 2014 kssr (http://cct.blog.shinobi.jp)
  87.  
  88. SEE ALSO
  89.  http://cct.blog.shinobi.jp
  90. ]];
  91.  
  92. local function split(src, sepalator)
  93.  local ret = { };
  94.  local bgnIdx = 1;
  95.  repeat
  96.    local endIdx = src:find(sepalator, bgnIdx, true);
  97.    if nil == endIdx then
  98.      endIdx = #src + 1;
  99.    end
  100.    table.insert(ret, src:sub(bgnIdx, endIdx - 1));
  101.    bgnIdx = endIdx + 1;
  102.  until #src < bgnIdx;
  103.  
  104.  return ret;
  105. end
  106.  
  107. local KNtC = { };
  108. for i = 0, 256 do
  109.  local name = keys.getName(i);
  110.  if nil ~= name then
  111.    KNtC[name] = i;
  112.  end
  113. end
  114.  
  115. local pages = { };
  116. local function makePages()
  117.  
  118.  local w, h = term.getSize();
  119.  w = w - 1;
  120.  h = h - 1;
  121.  
  122.  local current = { };
  123.  local beginIdx = 1;
  124.  
  125.  local lines = split(manText, "\n");
  126.  for _, val in ipairs(lines) do
  127.    local indent = 0;
  128.    for j = 1, #val do
  129.      if " " ~= val:sub(j, j) then
  130.        indent = j - 1;
  131.        val = val:sub(j);
  132.        break;
  133.      end
  134.    end
  135.  
  136.    local x = 1;
  137.    local BLANK = " ";
  138.    while true do
  139.      if #val < x then
  140.        break;  
  141.      end
  142.      table.insert(current, BLANK:rep(indent) ..
  143.                            val:sub(x, math.min(x + w - indent - 1, #val)));
  144.      x = x + w - indent;
  145.      if h == #current then
  146.        table.insert(pages, current);
  147.        current = { };
  148.      end
  149.    end
  150.  end
  151.  if 0 < #current then
  152.    table.insert(pages, current);
  153.  end
  154. end
  155.  
  156. local function isOneOf(val, possibleValues)
  157.  for _, v in ipairs(possibleValues) do
  158.    if val == v then
  159.      return true;
  160.    end
  161.  end
  162.  return false;
  163. end
  164.  
  165. local pageIdx = 1;
  166. local function man()
  167.  while true do
  168.    term.clear();
  169.    term.setCursorPos(1, 1);
  170.    term.setBackgroundColor(colors.black);
  171.    term.setTextColor(colors.white);
  172.  
  173.    local w, h = term.getSize();
  174.    local BLANK = " ";
  175.    for i, val in ipairs(pages[pageIdx]) do
  176.      term.setCursorPos(1, i);
  177.      term.write(val .. string.rep(BLANK, w - #val));
  178.    end
  179.    for i = #pages[pageIdx] + 1, h do
  180.      term.setCursorPos(1, i);
  181.      term.write(string.rep(BLANK, w));
  182.    end
  183.  
  184.    term.setCursorPos(1, h);
  185.    term.setTextColor(colors.black);
  186.    term.setBackgroundColor(colors.white);
  187.    term.write(" [b]ack, [n]ext, [q]uit / page : " .. pageIdx .. " of " .. #pages .. ".");
  188.  
  189.    while true do
  190.      local event, scanCode = os.pullEvent("key");
  191.  
  192.      -- key name
  193.      -- http://computercraft.info/wiki/Keys_(API)
  194.      if isOneOf(scanCode, { KNtC.up, KNtC.left, KNtC.p, KNtC.b, KNtC.w, KNtC.a }) then
  195.        pageIdx = math.max(pageIdx - 1, 1);
  196.        break;
  197.      elseif isOneOf(scanCode, { KNtC.down, KNtC.right, KNtC.n, KNtC.f, KNtC.s, KNtC.d, KNtC.space }) then
  198.        pageIdx = math.min(pageIdx + 1, #pages);
  199.        break;
  200.      elseif isOneOf(scanCode, { KNtC.q, KNtC.e }) then
  201.        term.setBackgroundColor(colors.black);
  202.        term.setTextColor(colors.white);
  203.        term.clear();
  204.        term.setCursorPos(1, 1);
  205.        return;
  206.      end
  207.      sleep(0.5);
  208.    end
  209.  end
  210. end
  211.  
  212. local function interactiveScriptInputMode(history)
  213.  local input = history;
  214.  while true do
  215.    print("input command or type ? >");
  216.    if nil ~= input and "" ~= input then
  217.      print("** In order to re-display your script,");
  218.      print("** press the 'up' cursor key.");
  219.    end
  220.    input = read(nil, { input });
  221.    if string.find(input, "?", 1, true) then
  222.      man();
  223.    else
  224.      if nil ~= input then
  225.        script = input;
  226.      else
  227.        script = "";
  228.      end
  229.      break;
  230.    end
  231.  end
  232.  return script;
  233. end
  234.  
  235. local function interactiveFileNameInputMode()
  236.  local pass = false;
  237.  local input;
  238.  local exitFlg = false;
  239.  while true do
  240.    print("Input script name if you want to save. >");
  241.    input = read(nil, { input });
  242.  
  243.    if nil == input then
  244.      fileName = "";
  245.      pass = true;
  246.  
  247.    elseif "" ~= input and fs.exists(input) then
  248.      while true do
  249.        print("file exists. [o]verwrite, [n]ame other, [d]on't save, [q]uit");
  250.        
  251.        local ip2 = read();
  252.        if "o" == ip2 then
  253.          fileName = input;
  254.          pass = true;
  255.          break;
  256.          
  257.        elseif "n" == ip2 then
  258.          break;
  259.          
  260.        elseif "d" == ip2 then
  261.          print("Don't save.");
  262.          fileName = "";
  263.          pass = true;
  264.          break;
  265.          
  266.        elseif "q" == ip2 then
  267.          print("Quit.");
  268.          exitFlg = true;
  269.          pass = true;
  270.          break;
  271.  
  272.        else
  273.          print("Wrong input. retry.");
  274.        end
  275.      end
  276.    else
  277.      fileName = input;
  278.      break;
  279.    end
  280.    if pass then
  281.      break;
  282.    end
  283.  end
  284.  return fileName, exitFlg;
  285. end
  286.  
  287. local tArgs = { ... };
  288.  
  289. makePages();
  290.  
  291. local script;
  292. local fileName = "";
  293. local history = "";
  294. local exitFlg;
  295.  
  296. if isOneOf("--help", tArgs) then
  297.  man();
  298.  return;
  299.  
  300. elseif isOneOf("--demo", tArgs) then
  301.  history = "s1 (up2 ((fp2)4 r (fp2)3 r)2)3 " ..
  302.            "fr (fp2)2 lfp2l fp2 rf p2rf dd p0 " ..
  303.            "blbbd s2p1 rflfr e1fe0f ll s3p1 lbl";
  304.  
  305. elseif 1 == #tArgs then
  306.  fileName = tArgs[1];
  307.  
  308. end
  309.  
  310. if "" ~= fileName then
  311.  local hFile = fs.open(fileName, "r");
  312.  local txt = hFile.readAll();
  313.  hFile.close();
  314.  script = txt;
  315.  
  316. else
  317.  script = interactiveScriptInputMode(history);
  318.  fileName, exitFlg = interactiveFileNameInputMode();
  319.  
  320.  if exitFlg then
  321.    return;
  322.  end
  323.  
  324.  if "" ~= fileName then
  325.    local hFile = fs.open("/" .. fileName, "w");
  326.    hFile.writeLine(script);
  327.    hFile.close();
  328.  end
  329. end
  330.  
  331. if "" ~= script then
  332.  doCommand(script);
  333.  print("tr was completed.");
  334. else
  335.  print("script is empty.");
  336. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement