サンプルプログラムについて
赤文字は実際に利用する際には変更しなければならない部分です、このままでは動作しません。
v1.33以前に作られたTurtleが移動するプログラムをv1.4以降で動かす場合、実行前に十分な燃料補給をしておくか、燃料補給の処理を追加する必要があります。
サンプルプログラム
パスワードドア
正しいパスワードを入力するとN秒間レッドストーン動力を出力するプログラムです。
password
に正しいパスワード、
N
にRS動力を出力する時間、
Output
にRS動力を出力する面を入れて下さい。
pass = ("password")
print("Enter password.")
a = read()
if a == pass then
print("Password correct!")
rs.setOutput("Output", true)
sleep(N)
rs.setOutput("Output", false)
else
print("Password incorrect!")
end
NOT回路
Input
から入力を受けている時は何も出力せず、入力を受けてない時は
Output
から出力するプログラムです。
プログラムを終了させる時はctrl+Tを利用して下さい。
if rs.getInput("Input") == false then
rs.setOutput("Output", true)
end
while true do
os.pullEvent("redstone")
if rs.getInput("Input") == true then
rs.setOutput("Output", false)
else
rs.setOutput("Output", true)
end
end
ランダムパルス回路
ランダムな間隔で0.2秒のパルスを
Output
から出力するプログラムです。
while true do
a = math.random(1, 10)
if a == 5 then
rs.setOutput("Output", true)
sleep(0.2)
rs.setOutput("Output", false)
sleep(0.2)
else
sleep(0.4)
end
end
ワイヤレスレッドストーンスイッチ
送信側でプログラムを起動する度に受信側でレッドストーンのON/OFFが切り替わるプログラムです。
modem
にはモデムを設置している面、
id
には受信側のコンピューターID又はラベル、
Output
にはRS動力を出力する面を入れて下さい。
送信側プログラム
rednet.open("modem")
rednet.send(id, "")
受信側プログラム
rednet.open("modem")
while true do
os.pullEvent("rednet_message")
rs.setOutput("Output", true)
os.pullEvent("rednet_message")
rs.setOutput("Output", false)
end
鉄道建設プログラム
マイニングタートル専用の自動レール敷設プログラムです。
プログラムを起動して、数字を入力するとそのブロック数レールを敷設します。
障害となるブロックは全て採掘し、足場が無ければ設置します。
タートルのインベントリの1番目に足場となるブロック、2番目にレールを入れて下さい。
なおスタックの都合上このプログラムでは65ブロック以上の敷設はできません。
またMOBにぶつかったりした場合正常に動作しない恐れがあります。
function reversedirection()
turtle.turnRight()
turtle.turnRight()
end
function dig(n)
turtle.up()
for i=1,n do
while turtle.detect() == true do
turtle.dig()
sleep(0.5)
end
turtle.forward()
turtle.digDown()
end
turtle.down()
end
function construction(n)
for i=1,n do
if turtle.detectDown() == false then
turtle.select(1)
turtle.placeDown()
turtle.up()
else
turtle.up()
end
turtle.select(2)
turtle.placeDown()
turtle.forward()
turtle.down()
end
end
a = read()
dig(a)
reversedirection()
construction(a)
reversedirection()
Turtle遠隔操作プログラム
WirelessTurtleを遠隔操作するプログラムです。
送信側のインターフェースを開いておく必要がある為実用性は疑問ですが、
rednetAPIを用いたプログラムの参考にして頂ければと思います。
modem
にはモデムの設置面、
id
には受信側Turtleのid又はラベルを入れて下さい。
送信側プログラム
function keyconvert(k)
if k == 17 then
return "Forward"
elseif k == 31 then
return "Back"
elseif k == 30 then
return "TurnLeft"
elseif k == 32 then
return "TurnRight"
elseif k == 42 then
return "Down"
elseif k == 57 then
return "Up"
elseif k == 18 then
return "Exit"
else
return "This key is not assigned."
end
end
print("w = Forward\ns = Back\na = TurnLeft\nd = TurnRight\nshift = Down\nspace = Up\ne = Exit")
rednet.open("modem")
while true do
a, b = os.pullEvent("key")
c = keyconvert(b)
if c == "This key is not assigned." then
print(c)
else
rednet.send(id, c)
print(c)
if c == "Exit" then break end
end
end
受信側プログラム
rednet.open("right")
while true do
a, b = rednet.receive()
if b == "Exit" then break end
if b == "Forward" then
turtle.forward()
elseif b == "Back" then
turtle.back()
elseif b == "TurnLeft" then
turtle.turnLeft()
elseif b == "TurnRight" then
turtle.turnRight()
elseif b == "Down" then
turtle.down()
elseif b == "Up" then
turtle.up()
end
end
チェック柄床・壁建設プログラム
指定した3種類のブロックで、タートルにギンガムチェック柄の床または壁を建設させるプログラムです。
インベントリの奇数番目のスロット、2, 6, 10, 14番目のスロット、および4, 8, 12, 16番目のスロットにはそれぞれ同種のブロックを配置してください。
建設の途中でブロック・モブなどにぶつかった場合、建設は中断されます。
関数定義部分
function turtle_go(f)
if f then
b = turtle.forward()
else
b = turtle.up()
end
if not b then
if turtle.getFuelLevel == 0 then
print("Fuel_Empty.")
os.queueEvent("terminate")
else
print("Faild_to_Move.")
os.queueEvent("terminate")
end
end
end
function checkItemCount(n)
if turtle.getItemCount == 0 then
if n == 1 then
for i = 1, 15, 2 do
if turtle.getItemCount(i) > 0 then
turtle.select(i)
turtle.transferTo(1)
turtle.select(1)
break
end
if i == 15 then
print("Materials_Empty.")
os.queueEvent("terminate")
end
end
else
for i = n, (n + 12), 4 do
if turtle.getItemCount(i) > 0 then
turtle.select(i)
turtle.transferTo(n)
turtle.select(n)
break
end
if i == n + 12 then
print("Materials_Empty.")
os.queueEvent("terminate")
end
end
end
end
end
function construct_line(type, count, odd)
for i = (odd + 1), (odd + count) do
if i % 2 == 1 then
turtle.select(1)
checkItemCount(1)
turtle.placeDown()
else
if type == 0 then
turtle.select(2)
checkItemCount(2)
turtle.placeDown()
else
turtle.select(4)
checkItemCount(4)
turtle.placeDown()
end
end
turtle_go(true)
end
end
function turtle_turn(mode)
if mode == 0 then
turtle.turnLeft()
turtle_go(true)
turtle.turnLeft()
turtle_go(true)
elseif mode == 1
turtle.turnRight()
turtle_go(true)
turtle.turnRight()
turtle_go(true)
else
turtle.turnRight()
turtle.turnRight()
turtle_go(false)
turtle_go(true)
end
end
function construct_plane(x, y, mode)
for i = 1, y do
if mode == "floor" then
if i % 2 == 1 then
construct_line(1, x, 0)
turtle_turn(0)
else
construct_line(2, x, x % 2)
turtle_turn(1)
end
elseif mode == "wall" then
if i % 2 == 1 then
construct_line(1, x, 0)
turtle_turn(2)
else
construct_line(2, x, x % 2)
turtle_turn(2)
end
else
print("Invaild_Mode.")
os.queueEvent("terminate")
end
end
print("Construction_Ended.")
end
実行部分
x
に建設部分の幅、
y
に建設部分の長さまたは高さをそれぞれ数値で、
mode
にfloor(床モード)またはwall(壁モード)を入れてください。
construct_plane(x, y, "mode")
簡易クァーリプログラム(個人用)
プログラムを動かす前にタートルのスロット2(1段目の左から2番目)に丸石を最低5つ入れてください。捨てるブロックの指定として使います。また、掘った穴を最後に埋めるためにも使います。
プログラムを起動させると現在高度を尋ねられます。打ち込むと不足分の石炭の数を計算してくれます。足りなかったら燃料を補充してから、プログラムを再度起動してください。
最終確認の質問で1を入力すると岩盤までタートルが掘ってくれます。岩盤に到達すると出発点まで戻り、イベントリ内(3~16スロット)にある丸石を捨ててから事故防止のため、穴の入り口を埋めてくれます。
function dig_program()
a = 0
while turtle.digDown() or turtle.detectDown()==false do
turtle.down()
turtle.dig()
a = a + 1
for i=1,3 do
turtle.turnRight()
turtle.dig()
end
end
for i=1,a-1 do
turtle.up()
end
turtle.select(2)
turtle.place()
for i=1,3 do
turtle.turnRight()
turtle.place()
end
turtle.up()
for i=3,16 do
turtle.select(2)
if turtle.compareTo(i) then
turtle.select(i)
turtle.dropDown()
end
end
turtle.placeDown()
turtle.select(1)
print("mission completed!")
end
print("enter current z")
height = read()
d_b = (height - 5) * 2
print("move blocks:"..d_b)
if (turtle.getFuelLevel() < d_b) then
print("short coal number:"..(d_b - turtle.getFuelLevel()) / 80)
else
print("current fuel level is "..turtle.getFuelLevel()..", go(1) or stop(0)?:")
if "1" == read() then
dig_program()
else
print("exit")
end
end
空洞、水、溶岩があってもちゃんと動いてくれます。しかし、運悪く下に生物がいる場合、現在高度の計算が狂ってしまうため、元の高度まで戻ってくれません。
無駄な記述が多いので、改変して使ってください。また、変な英語になっています。
タートルインベントリ選択ルーチン
タートルインベントリでブロックが存在するスロットを選択する関数です。
引数s(start)に選択したい最初のスロット番号を
引数e(end)に選択したい最後のスロット番号を渡して使用します。
例えばスロット3にだけブロックを置き、引数sに1を、引数eに6を渡して実行したとすると
スロット3が選択された状態になります。
また、もし引数で指定したスロットに一つもブロックがないとプログラムを強制終了するようにしていますので、
ブロックがなくなっても実行し続けたい場合は「if i == e then~end」を削除してください。
function select_f(s,e)
local v = 1
for i = s,e do
if turtle.getItemCount(i) ~= 0 then
v = i
break
end
if i == e then
write("Block null ")
print(i)
os.queueEvent("terminate")
end
end
turtle.select(v)
end
この関数は、2種類以上の各々64個以上のブロックを使用したい時に一番活躍します。
引数を変数にしてプログラムの冒頭で調整出来るようにすると、一層見やすく便利になるでしょう。
使用例)
block1_s = 1
block1_e = 5
block2_s = 6
block2_e = 10
function select_f(s,e)
中省略
end
while true do
select_f(block1_s,block1_e)
turtle.placeDown()
turtle.forward()
select_f(block2_s,block2_e)
turtle.placeDown()
turtle.forward()
sleep(0.05)
end
このようにプログラムすれば、スロット1~5のブロックとスロット6~10のブロックを
交互にひたすら設置していき、どちらかのブロックが尽きたら自動的に動作を終了する働きをします。
ブロック置換えプログラム等に使用したい場合は、この関数を改造して
指定スロットと比較(compareTo)し一致したスロットを選択するようにすれば、
タートルが拾ったブロックを設置せずに思い通りのブロックが設置できます。
関数の構文を1つずつ理解し、compareToの使用方法をググれば改造は容易にできるはずなので
あえて比較機能を付けた関数のサンプルは載せません。
ヒントは、比較スロットを指定する引数を増やすことと
compareToの使用方法をしっかり確認することです。
コメント
コメント欄の運営・編集方針に関してはコメント欄方針を参照してください。
このコメント欄はwikiの情報充実のため、追記がしやすいよう設けた物なので、編集が苦手な方は以下のコメントフォームへ書き込んで頂ければ有志でページに取り込みます。
編集に関わらない質問は一切受け付けておらず、一律して削除されます。
注意書きをよく読んだ上で、
質問掲示板または
非公式日本ユーザーフォーラムをご利用ください。
Wikiの運営に関連する話は
Wiki運営掲示板、雑談等は
非公式日本ユーザーフォーラムにてお願いします。
表示される親コメントには限りがあるので、返信の際は返信したいコメント横のチェックを付けて返信するようご協力お願いします。