Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --20210504 CC: Tweaked(Game version 1.16.5対応版)
- --メニューのキー取得方法改善、ケーキ作成時に現バージョンだとチェストにしまわない挙動を修正
- -- タートルのディスプレイの大きさを取得。(一部文字列を画面の真ん中に配置するために使用)
- local w,h = term.getSize()
- -- 繰り返し実行のための判断変数を初期化
- running = true
- -- チェストの座標定義(配列番号がチェストの番号)(かまどは17番目に登録)
- -- x:y:z = 9x4x4の範囲。水平がX軸、奥行がY軸、高さがZ軸。
- local material = {
- {3,1,4},
- {3,1,3},
- {3,1,2},
- {3,1,1},
- {5,1,4},
- {5,1,3},
- {5,1,2},
- {5,1,1},
- {7,1,4},
- {7,1,3},
- {7,1,2},
- {7,1,1},
- {9,1,4},
- {9,1,3},
- {9,1,2},
- {9,1,1},
- {1,1,2}
- }
- -- 各材料の格納チェスト番号(座標)の割り当て。(2番目の要素の名前は現在は未使用。材料取ってきてという用途で使えるかも?)
- local fish, pork, beef, chicken = {1,"Fish"},{2,"Pork"}, {3,"Beef"}, {4,"Chiken"}
- local pumpkin, potato, cocoa = {6,"Pumpkin"}, {7,"Potato"}, {8,"Cocoa beans"}
- local egg, sugar, wheat, milk = {9,"Egg"}, {10,"Sugar"}, {11,"Wheat"}, {12,"Milk"}
- local mush_r, mush_w, boal = {14,"Red Mushroom"}, {15,"White Mushroom"}, {16,"Boal"}
- local furnace = {17,"Furnace"}
- -- 完成品の定義。名前とレシピ番号を管理している。
- --local productQty = 11
- local cookedProduct = {
- {"Cooked Fish", "furnace", 1},
- {"Cooked Chicken", "furnace", 2},
- {"Cooked Porkchop", "furnace", 3},
- {"Steak", "furnace",4 },
- {"Baked Potato", "furnace", 5},
- {"Bread", "craft", 6},
- {"Mushroom Stew", "craft", 7},
- {"Pumpkin Pie", "craft", 8},
- {"Cake", "craft", 9},
- {"Cookie", "craft", 10},
- {"Exit",nil,nil}
- }
- -- Receipe
- -- 完成品のレシピの登録
- --{nil, nil, nil, nil, nil, nil, nil, nil, nil},
- local receipe = {
- {fish, nil, nil, nil, nil, nil, nil, nil, nil},
- {chicken, nil, nil, nil, nil, nil, nil, nil, nil},
- {pork, nil, nil, nil, nil, nil, nil, nil, nil},
- {beef, nil, nil, nil, nil, nil, nil, nil, nil},
- {potato, nil, nil, nil, nil, nil, nil, nil, nil},
- {wheat, wheat, wheat, nil, nil, nil, nil, nil, nil},
- {mush_r, mush_w, boal, nil, nil, nil, nil, nil, nil},
- {pumpkin, sugar, egg, nil, nil, nil, nil, nil, nil},
- {milk, milk, milk, sugar, egg, sugar, wheat, wheat, wheat},
- {wheat, cocoa, wheat, nil, nil, nil, nil, nil, nil},
- }
- -- メニューの構成
- local menuAreaNum = {3,6,6}
- local selectAreaTitle = {"Main menu", "Baked menus", "Cooked menus"}
- local menuItem = {
- {
- {"1. Baked menus", 2, true},
- {"2. Cooked menus", 3, true},
- {"3. Exit", 11, false},
- },
- {
- {"1. Cooked Fish", 1, false},
- {"2. Cooked Chicken", 2, false},
- {"3. Cooked Porkchop", 3, false},
- {"4. Steak", 4, false},
- {"5. Baked Potato", 5, false},
- {"6. Back", 1, true}
- },
- {
- {"1. Bread", 6, false},
- {"2. Mushroom Stew", 7, false},
- {"3. Pumpkin Pie", 8, false},
- {"4. Cake", 9, false},
- {"5. Cookie", 10, false},
- {"6. Back", 1, true}
- }
- }
- -- メニューの描画処理
- local select = 1
- local selectArea = 1
- local function printCentered(str, ypos)
- term.setCursorPos(w/2 - #str/2, ypos)
- term.write(str)
- end
- function clearScreen()
- term.clear()
- term.setCursorPos(1,1)
- term.clear()
- end
- function drawHeader()
- print("---------------------------------------")
- print("--- Welcome to Turtle Restaurant !! ---")
- print("--- Please order from menus ---")
- print("---------------------------------------")
- end
- function drawMenu()
- printCentered(selectAreaTitle[selectArea], 5)
- print("")
- print("")
- for i = 1, menuAreaNum[selectArea] do
- if select == i then
- if i > 1 and selectArea == 1 then
- print("")
- end
- print("-> " .. menuItem[selectArea][i][1])
- else
- if i > 1 and selectArea == 1 then
- print("")
- end
- print(" " .. menuItem[selectArea][i][1])
- end
- end
- end
- function runMenu()
- while running do
- clearScreen()
- drawHeader()
- drawMenu()
- local event, key = os.pullEvent("key")
- if key == keys.w or key == keys.up then
- select = select - 1
- end
- if key == keys.s or key == keys.down then
- select = select + 1
- end
- if select == 0 then
- select = menuAreaNum[selectArea]
- end
- if select == menuAreaNum[selectArea] + 1 then
- select = 1
- end
- clearScreen()
- if key == keys.enter then
- if menuItem[selectArea][select][3] then
- selectArea = menuItem[selectArea][select][2]
- select = 1
- else
- if cookedProduct[menuItem[selectArea][select][2]][1] ~= "Exit" then
- startProgram(cookedProduct[menuItem[selectArea][select][2]])
- else
- running = false
- end
- end
- end
- end
- end
- function startProgram(input)
- print("Cooking now...")
- cook(input)
- end
- -- 座標変数の準備
- -- x:y:z = 9x4x4の範囲。水平がX軸、奥行がY軸、高さがZ軸。
- -- タートルの初期位置設定とカウンター位置。(初期位置=カウンター。帰ってくるときにカウンター位置を指定して帰ってくる)
- local xyz = {5, 4, 2}
- local counter = {5, 4, 2}
- -- 移動の際の座標用(移動するたびに現在地をxyzに記憶、目的地をmxyzに設定して移動距離を計算する)
- local mxyz = {0, 0, 0}
- -- タートルの向いている方向を保存する
- -- f,r,b,l = 前,後,右,左 (左右は、タートル視点でカウンターを基準)
- local f, b, r, l = 0, 1, 2, 3
- local direction = 0
- -- 指定した方向へタートルの向きを変える。
- function setDirection(toDirection)
- if direction ~= toDirection then
- if toDirection == f then
- if direction == b then
- turtle.turnLeft()
- turtle.turnLeft()
- elseif direction == r then
- turtle.turnLeft()
- elseif direction == l then
- turtle.turnRight()
- end
- direction = f
- elseif toDirection == b then
- if direction == f then
- turtle.turnLeft()
- turtle.turnLeft()
- elseif direction == r then
- turtle.turnRight()
- elseif direction == l then
- turtle.turnLeft()
- end
- direction = b
- elseif toDirection == r then
- if direction == l then
- turtle.turnLeft()
- turtle.turnLeft()
- elseif direction == b then
- turtle.turnLeft()
- elseif direction == f then
- turtle.turnRight()
- end
- direction = r
- elseif toDirection == l then
- if direction == r then
- turtle.turnLeft()
- turtle.turnLeft()
- elseif direction == b then
- turtle.turnRight()
- elseif direction == f then
- turtle.turnLeft()
- end
- direction = l
- end
- end
- end
- --チェストの座標を材料の番号から取得する。
- function getCordinate(chestNum)
- lx = material[chestNum][1]
- ly = material[chestNum][2]
- lz = material[chestNum][3]
- return {lx, ly, lz}
- end
- -- X軸の移動を行う
- function move_x(fgx)
- if fgx == 0 then
- return true
- end
- if fgx > 0 then
- setDirection(l)
- for i=1, fgx do
- turtle.forward()
- end
- else
- setDirection(r)
- for i=1, -fgx do
- turtle.forward()
- end
- end
- end
- -- Y軸の移動を行う
- function move_y(fgy)
- if fgy == 0 then
- return true
- end
- if fgy > 0 then
- setDirection(f)
- for i=1, fgy do
- turtle.forward()
- end
- else
- setDirection(b)
- for i=1, -fgy do
- turtle.forward()
- end
- end
- end
- -- Z軸の移動を行う
- function move_z(fgz)
- if fgz == 0 then
- return true
- end
- if fgz > 0 then
- for i=1, fgz do
- turtle.up()
- end
- else
- for i=1, -fgz do
- turtle.down()
- end
- end
- end
- -- タートルを指定した座標まで移動する
- -- 引数はType, ChestInfo変数を用意して受け取る。
- -- Typeは0が通常移動で、1がカウンターへ戻る。
- -- ChestInfoはmaterialの配列要素を渡す。
- -- チェスト、かまどの一歩手前で止まる必要があるので、チェストへの移動時はY軸に+1して計算する。
- function moveTurtle(...)
- local args = {...}
- if args[1] == 0 then
- chestInfo = args[2]
- mxyz = getCordinate(chestInfo[1])
- gx = mxyz[1] - xyz[1]
- gy = mxyz[2] - xyz[2] + 1
- gz = mxyz[3] - xyz[3]
- move_x(gx)
- move_y(gy)
- move_z(gz)
- xyz = {xyz[1] + gx, xyz[2] + gy, xyz[3] + gz}
- elseif args[1] == 1 then
- mxyz = counter
- gx = mxyz[1] - xyz[1]
- gy = mxyz[2] - xyz[2]
- gz = mxyz[3] - xyz[3]
- move_z(gz)
- move_x(gx)
- move_y(gy)
- xyz = {xyz[1] + gx, xyz[2] + gy, xyz[3] + gz}
- end
- end
- -- チェストから材料を一個取り出す。
- -- suck関数はスタック単位でしか扱えないため、一旦取り出した後1個だけ残してしまうように処理する。
- function pickMaterial(slotNum)
- turtle.select(slotNum)
- setDirection(b)
- turtle.suck()
- dropNum = turtle.getItemCount(slotNum) - 1
- turtle.drop(dropNum)
- end
- -- 材料をかまどに投入し、回収する。
- function furnaceMaterial()
- moveTurtle(0, furnace)
- setDirection(b)
- turtle.up()
- turtle.forward()
- turtle.dropDown()
- turtle.back()
- turtle.down()
- turtle.down()
- turtle.forward()
- turtle.select(16)
- repeat
- until turtle.suckUp()
- turtle.back()
- turtle.up()
- end
- -- 材料を調達し、クラフトする。完成品は16番スロットに格納される。
- -- ケーキの時に出るバケツなど、余った素材がある場合は下のチェストに格納する。
- function cook(product)
- for i=1, 9 do
- if i >= 1 and i <= 3 then
- pickSlotNum = i
- elseif i >= 4 and i <= 6 then
- pickSlotNum = i + 1
- elseif i >= 7 and i <= 9 then
- pickSlotNum = i + 2
- end
- if receipe[product[3]][i] then
- moveTurtle(0, receipe[product[3]][i])
- pickMaterial(pickSlotNum)
- end
- end
- if product[2] == "craft" then
- turtle.select(16)
- turtle.craft(1)
- elseif product[2] == "furnace" then
- furnaceMaterial()
- end
- moveTurtle(1)
- turtle.forward()
- turtle.select(16)
- turtle.drop()
- turtle.back()
- for i=1, 16 do
- turtle.select(i)
- turtle.dropDown()
- end
- end
- -- EXITメニューが選択されるまで、繰り返し実行。
- while running == true do
- runMenu()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement