忍者ブログ

cc-memo

Computer Craft のメモ

archer2

×

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

コメント

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

archer2

archerのソースを短くしてみる。


lib_turtle_0.1.1
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.placeUp();
  elseif para1 == 1 then
    turtle.place();
  elseif para1 == 2 then
    turtle.placeDown();
  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
};
local function getParam(command, idx)
  local buff = "";
  local ch = "";
  for i = idx, string.len(command) do
    ch = string.sub(command, i, i);
    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 + 1;
      end
    end
  end
  if buff == "" then
    return nil, string.len(command) + 1;
  else
    return tonumber(buff), string.len(command) + 1;
  end
end
local function get1Cmd(command, idx)
  local cmd = string.sub(command, idx, idx);
  local para1, para2, nextIdx, paraCnt;
  if nil ~= cmdInfo[cmd] then
    paraCnt = cmdInfo[cmd].paraCnt;
  else
    print("unknown command [" .. cmd .. "]");
    exit();
  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
function doCommand(command)
  command = command .. " ";
  local cmd, para1, para2, nextIdx;
  for i = 1, string.len(command) do
    cmd, para1, para2, nextIdx = get1Cmd(command, i);
    if cmd ~= nil then
      cmdInfo.func(para1, para2);
    end
  end
end

archer2
dofile("lib_turtle_0.1.1");
local NUM_SHOT = 3;
local NUM_STEP = 4;
local SHOT_WAIT = 0.2;
doCommand("ll");
for i = 1, NUM_STEP do
  doCommand("s2 p0 s3 t0," .. NUM_SHOT);
  for j = 1, NUM_SHOT do
    rs.setOutput("top", true);
    sleep(SHOT_WAIT);
    rs.setOutput("top", false);
    sleep(SHOT_WAIT);
  end
  doCommand("s2 e0 bbbb");
end
PR

コメント

プロフィール

HN:
kssr
性別:
非公開

Twitter

最古記事

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

アンテナ・ランキング