忍者ブログ

cc-memo

Computer Craft のメモ

archer3

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

コメント

ただいまコメントを受けつけておりません。

archer3

archer2をさらに改良。繰り返し処理も「ミニ言語」?内で行えるようにした。
debug.logには、かなりお世話になった。


lib_log
LOG_FILE_NAME = "debug.log";
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


lib_turtle_0.2.0

dofile("lib_log");
local function myPlace(para1)
  if para1 == 0 then
    turtle.placeUp();
  elseif para1 == 1 then
    turtle.place();
  elseif para1 == 2 then
    turtle.placeDown();
  end
end
local function myThrow(para1, para2)
  if para1 == 0 then
    turtle.dropUp(para2);
  elseif para1 == 1 then
    turtle.drop(para2);
  elseif para1 == 2 then
    turtle.dropDown(para2);
  end
end
local function myExcavate(para1)
  if para1 == 0 then
    turtle.digUp();
  elseif para1 == 1 then
    turtle.dig();
  elseif para1 == 2 then
    turtle.digDown();
  end
end
local function myZzz(para1)
  sleep(para1 / 1000);
end
local function myOutput(para1, para2)
  local dir = { [0] = "top",  [1] = "front", [2] = "bottom",
                [3] = "left", [4] = "back",  [5] = "right" };
  if para2 == 1 then
    rs.setOutput(dir[para1], true);
  else
    rs.setOutput(dir[para1], false);
  end
end
local cmdInfo =
{
  f = { paraCnt = 0, func = turtle.forward;   }, -- [f]orward
  b = { paraCnt = 0, func = turtle.back;      }, -- [b]ack
  l = { paraCnt = 0, func = turtle.turnLeft;  }, -- turn [l]eft
  r = { paraCnt = 0, func = turtle.turnRight; }, -- turn [r]ight
  u = { paraCnt = 0, func = turtle.Up;        }, -- [u]p
  d = { paraCnt = 0, func = turtle.Down;      }, -- [d]own
  s = { paraCnt = 1, func = turtle.select;    }, -- [s]elect
  p = { paraCnt = 1, func = myPlace;          }, -- [p]lace
  t = { paraCnt = 2, func = myThrow;          }, -- [t]hrow = drop
  e = { paraCnt = 1, func = myExcavate;       }, -- [e]xcavate = dig
  z = { paraCnt = 1, func = myZzz;            }, -- [z]zz = sleep
  o = { paraCnt = 2, func = myOutput;         }, -- set [o]utput (redstone) 
};
local function getParam(command, idx)
  local buff = "";
  local ch = "";
  for i = idx, command:len() do
    ch = command:sub(i, i);
    --log("getParam i = " .. i .. " / ch =[" .. ch .. "]");
    if ch == "," then
      if buff == "" then
        return nil, i + 1;
      else
        return tonumber(buff), i + 1;
      end
    elseif nil ~= string.find("0123456789", ch, 1, true) then
      buff = buff .. ch;
    else
      if buff ~= "" then
        return tonumber(buff), i;
      end
    end
  end
  if buff == "" then
    return nil, command:len() + 1;
  else
    return tonumber(buff), command:len() + 1;
  end
end
local function get1Cmd(command, idx)
  local cmd = string.sub(command, idx, idx);
  local para1, para2, nextIdx, paraCnt;
  --log(idx .. " / " .. cmd);
  if nil ~= cmdInfo[cmd] then
    paraCnt = cmdInfo[cmd].paraCnt;
  else
    --log("unknown command [" .. cmd .. "]");
    error("unknown command [" .. cmd .. "]");
  end
  if -1 == paraCnt then
    return nil, nil, nil, idx + 1;
  elseif paraCnt == 0 then
    return cmd, nil, nil, idx + 1;
  elseif paraCnt == 1 then
    para1, nextIdx = getParam(command, idx + 1);
    return cmd, para1, nil, nextIdx;
  elseif paraCnt == 2 then
    para1, nextIdx = getParam(command, idx + 1);
    para2, nextIdx = getParam(command, nextIdx);
    return cmd, para1, para2, nextIdx;
  end
end
local function checkLoop(command)
  local kBgn = {""};
  local loopInfo = {};
  local ch, repeatCnt, nextIdx, kBgnIdx;
  for i = 1, command:len() do
    ch = command:sub(i, i);
    if "(" == ch then
      table.insert(kBgn, i);
    elseif ")" == ch then
      repeatCnt, nextIdx = getParam(command, i + 1);
      loopInfo[i] = { beginIdx   = table.remove(kBgn),
                      endIdx     = i,
                      nextIdx    = nextIdx,
                      repeatCnt  = repeatCnt,
                      currentCnt = 0 };
      --log("loopInfo[" .. i .. "]");
      --log("  beginIdx   = " .. loopInfo[i].beginIdx);
      --log("  endIdx     = " .. loopInfo[i].endIdx);
      --log("  nextIdx    = " .. loopInfo[i].nextIdx);
      --log("  repeatCnt  = " .. loopInfo[i].repeatCnt);
      --log("  currentCnt = " .. loopInfo[i].currentCnt);
      i = nextIdx;
    end
  end
  return loopInfo;
end
function doCommand(command)

  command = command .. " ";
  local loopInfo = checkLoop(command);
  --for i, dat in pairs(loopInfo) do
    --log("check loopInfo")
    --log("  i = " .. i);
    --log("  " .. dat.beginIdx .. " / " .. dat.endIdx .. " / " .. dat.repeatCnt);
  --end
  local cmd, para1, para2, nextIdx, ch;
  local i = 1;
  while true do
    ch = command:sub(i, i);
    --log("doCommand idx = " .. i .. " [" .. ch .. "]");
    if ")" == ch then
      --log("i = " .. i);
      --log(   loopInfo[i].beginIdx .. " / " .. loopInfo[i].endIdx .. " / "
      --    .. loopInfo[i].nextIdx  .. " / " .. loopInfo[i].repeatCnt);
      loopInfo[i].currentCnt = loopInfo[i].currentCnt + 1;
      if loopInfo[i].repeatCnt <= loopInfo[i].currentCnt then
        loopInfo[i].currentCnt = 0;
        i = loopInfo[i].nextIdx;
      else
        i = loopInfo[i].beginIdx;
      end
    elseif " " == ch then
      i = i + 1;
    elseif "\n" == ch then
      i = i + 1;
    elseif "(" == ch then
      i = i + 1;
    else
      cmd, para1, para2, nextIdx = get1Cmd(command, i);
      --log("  nextIdx = " .. nextIdx);
      i = nextIdx;
      if cmd ~= nil then
        cmdInfo[cmd].func(para1, para2);
      end
    end
    if command:len() <= i then
      break;
    end
  end
end


archer3
dofile("lib_turtle_0.2.0");
local NUM_SHOT = 3;
local NUM_STEP = 4;
local SHOT_WAIT = 0.2 * 1000;
local cmd = 
     "ll"
  .. "\n("
  .. "\n  s2 p0 s3 t0," .. NUM_SHOT
  .. "\n  ("
  .. "\n    o0,1"
  .. "\n    z" .. SHOT_WAIT
  .. "\n    o0,0"
  .. "\n    z" .. SHOT_WAIT
  .. "\n  )" .. NUM_SHOT
  .. "\n  s2 e0 (b)4"
  .. "\n)" .. NUM_STEP;
print("cmd:" .. cmd);
doCommand(cmd);
PR

コメント

プロフィール

HN:
kssr
性別:
非公開

Twitter

最古記事

(04/25)
(04/26)
(04/27)
(04/28)
(04/29)

アンテナ・ランキング