Computer Craft のメモ
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
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
|
for i = 0, 9 do
turtle.turnRight();
end
turtle.placeUp(
"Hello World !\n"
.. "Hello CC !!");
sleep(3.5);
turtle.digUp();
turtle.turnLeft();
turtle.turnLeft();
2回転半して、看板を出します。
それだけ。
local args = { ... }; local file = args[1]; if nil == file then print("usage:"); print("upload filename"); elseif not fs.exists("/" .. file) then print("No such file."); elseif not fs.isDir("/disk") then print("Disk drive (or disk) not found."); else if fs.exists("/disk/" .. file) then fs.delete("/disk/" .. file); end fs.copy("/" .. file, "/disk/" .. file); end