Advertisement
LifeGate

Cooking Turtle (ComputerCraft)

May 10th, 2013 (edited)
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.00 KB | None | 0 0
  1. --20210504 CC: Tweaked(Game version 1.16.5対応版)
  2. --メニューのキー取得方法改善、ケーキ作成時に現バージョンだとチェストにしまわない挙動を修正
  3.  
  4. -- タートルのディスプレイの大きさを取得。(一部文字列を画面の真ん中に配置するために使用)
  5. local w,h = term.getSize()
  6.  
  7. -- 繰り返し実行のための判断変数を初期化
  8. running = true
  9.  
  10. -- チェストの座標定義(配列番号がチェストの番号)(かまどは17番目に登録)
  11. -- x:y:z = 9x4x4の範囲。水平がX軸、奥行がY軸、高さがZ軸。
  12.  
  13. local material = {
  14. {3,1,4},
  15. {3,1,3},
  16. {3,1,2},
  17. {3,1,1},
  18. {5,1,4},
  19. {5,1,3},
  20. {5,1,2},
  21. {5,1,1},
  22. {7,1,4},
  23. {7,1,3},
  24. {7,1,2},
  25. {7,1,1},
  26. {9,1,4},
  27. {9,1,3},
  28. {9,1,2},
  29. {9,1,1},
  30. {1,1,2}
  31. }
  32.  
  33. -- 各材料の格納チェスト番号(座標)の割り当て。(2番目の要素の名前は現在は未使用。材料取ってきてという用途で使えるかも?)
  34. local fish, pork, beef, chicken = {1,"Fish"},{2,"Pork"}, {3,"Beef"}, {4,"Chiken"}
  35. local pumpkin, potato, cocoa = {6,"Pumpkin"}, {7,"Potato"}, {8,"Cocoa beans"}
  36. local egg, sugar, wheat, milk = {9,"Egg"}, {10,"Sugar"}, {11,"Wheat"}, {12,"Milk"}
  37. local mush_r, mush_w, boal = {14,"Red Mushroom"}, {15,"White Mushroom"}, {16,"Boal"}
  38. local furnace = {17,"Furnace"}
  39.  
  40. -- 完成品の定義。名前とレシピ番号を管理している。
  41. --local productQty = 11
  42. local cookedProduct = {
  43. {"Cooked Fish", "furnace", 1},
  44. {"Cooked Chicken", "furnace", 2},
  45. {"Cooked Porkchop", "furnace", 3},
  46. {"Steak", "furnace",4 },
  47. {"Baked Potato", "furnace", 5},
  48. {"Bread", "craft", 6},
  49. {"Mushroom Stew", "craft", 7},
  50. {"Pumpkin Pie", "craft", 8},
  51. {"Cake", "craft", 9},
  52. {"Cookie", "craft", 10},
  53. {"Exit",nil,nil}
  54. }
  55.  
  56. -- Receipe
  57. -- 完成品のレシピの登録
  58. --{nil, nil, nil, nil, nil, nil, nil, nil, nil},
  59. local receipe = {
  60. {fish, nil, nil, nil, nil, nil, nil, nil, nil},
  61. {chicken, nil, nil, nil, nil, nil, nil, nil, nil},
  62. {pork, nil, nil, nil, nil, nil, nil, nil, nil},
  63. {beef, nil, nil, nil, nil, nil, nil, nil, nil},
  64. {potato, nil, nil, nil, nil, nil, nil, nil, nil},
  65. {wheat, wheat, wheat, nil, nil, nil, nil, nil, nil},
  66. {mush_r, mush_w, boal, nil, nil, nil, nil, nil, nil},
  67. {pumpkin, sugar, egg, nil, nil, nil, nil, nil, nil},
  68. {milk, milk, milk, sugar, egg, sugar, wheat, wheat, wheat},
  69. {wheat, cocoa, wheat, nil, nil, nil, nil, nil, nil},
  70. }
  71.  
  72. -- メニューの構成
  73. local menuAreaNum = {3,6,6}
  74. local selectAreaTitle = {"Main menu", "Baked menus", "Cooked menus"}
  75. local menuItem = {
  76.     {
  77.         {"1. Baked menus", 2, true},
  78.         {"2. Cooked menus", 3, true},
  79.         {"3. Exit", 11, false},
  80.     },
  81.     {
  82.         {"1. Cooked Fish", 1, false},
  83.         {"2. Cooked Chicken", 2, false},
  84.         {"3. Cooked Porkchop", 3, false},
  85.         {"4. Steak", 4, false},
  86.         {"5. Baked Potato", 5, false},
  87.         {"6. Back", 1, true}
  88.     },
  89.     {
  90.         {"1. Bread", 6, false},
  91.         {"2. Mushroom Stew", 7, false},
  92.         {"3. Pumpkin Pie", 8, false},
  93.         {"4. Cake", 9, false},
  94.         {"5. Cookie", 10, false},
  95.         {"6. Back", 1, true}
  96.     }
  97. }
  98.  
  99.  
  100. -- メニューの描画処理
  101. local select = 1
  102. local selectArea = 1
  103.  
  104. local function printCentered(str, ypos)
  105.     term.setCursorPos(w/2 - #str/2, ypos)
  106.     term.write(str)
  107. end
  108.  
  109. function clearScreen()
  110.     term.clear()
  111.     term.setCursorPos(1,1)
  112.     term.clear()
  113. end
  114.  
  115. function drawHeader()
  116.     print("---------------------------------------")
  117.     print("--- Welcome to Turtle Restaurant !! ---")
  118.     print("---     Please order from menus     ---")
  119.     print("---------------------------------------")
  120. end
  121.  
  122. function drawMenu()
  123.     printCentered(selectAreaTitle[selectArea], 5)
  124.     print("")
  125.     print("")
  126.     for i = 1, menuAreaNum[selectArea] do
  127.         if select == i then
  128.             if i > 1 and selectArea == 1 then
  129.                 print("")
  130.             end
  131.             print("-> " .. menuItem[selectArea][i][1])
  132.         else
  133.             if i > 1 and selectArea == 1 then
  134.                 print("")
  135.             end
  136.             print("   " .. menuItem[selectArea][i][1])
  137.         end
  138.     end
  139. end
  140.  
  141. function runMenu()
  142.     while running do
  143.         clearScreen()
  144.         drawHeader()
  145.         drawMenu()
  146.        
  147.         local event, key = os.pullEvent("key")
  148.         if key == keys.w  or key == keys.up then
  149.             select = select - 1
  150.         end
  151.         if key == keys.s or key == keys.down then
  152.             select = select + 1
  153.         end
  154.        
  155.         if select == 0 then
  156.             select = menuAreaNum[selectArea]
  157.         end
  158.        
  159.         if select == menuAreaNum[selectArea] + 1 then
  160.             select = 1
  161.         end
  162.        
  163.         clearScreen()
  164.         if key == keys.enter then
  165.             if menuItem[selectArea][select][3] then
  166.                 selectArea = menuItem[selectArea][select][2]
  167.                 select = 1
  168.             else
  169.                 if cookedProduct[menuItem[selectArea][select][2]][1] ~= "Exit" then
  170.                     startProgram(cookedProduct[menuItem[selectArea][select][2]])
  171.                 else
  172.                     running = false
  173.                 end
  174.             end
  175.         end
  176.     end
  177. end
  178.  
  179. function startProgram(input)
  180.     print("Cooking now...")
  181.     cook(input)
  182. end
  183.  
  184. -- 座標変数の準備
  185. -- x:y:z = 9x4x4の範囲。水平がX軸、奥行がY軸、高さがZ軸。
  186. -- タートルの初期位置設定とカウンター位置。(初期位置=カウンター。帰ってくるときにカウンター位置を指定して帰ってくる)
  187. local xyz = {5, 4, 2}
  188. local counter = {5, 4, 2}
  189.  
  190. -- 移動の際の座標用(移動するたびに現在地をxyzに記憶、目的地をmxyzに設定して移動距離を計算する)
  191. local mxyz = {0, 0, 0}
  192.  
  193. -- タートルの向いている方向を保存する
  194. -- f,r,b,l = 前,後,右,左 (左右は、タートル視点でカウンターを基準)
  195. local f, b, r, l = 0, 1, 2, 3
  196. local direction = 0
  197.  
  198. -- 指定した方向へタートルの向きを変える。
  199. function setDirection(toDirection)
  200.     if direction ~= toDirection then
  201.         if toDirection == f then
  202.             if direction == b then
  203.                 turtle.turnLeft()
  204.                 turtle.turnLeft()
  205.             elseif direction == r then
  206.                 turtle.turnLeft()
  207.             elseif direction == l then
  208.                 turtle.turnRight()
  209.             end
  210.             direction = f
  211.         elseif toDirection == b then
  212.             if direction == f then
  213.                 turtle.turnLeft()
  214.                 turtle.turnLeft()
  215.             elseif direction == r then
  216.                 turtle.turnRight()
  217.             elseif direction == l then
  218.                 turtle.turnLeft()
  219.             end
  220.             direction = b
  221.         elseif toDirection == r then
  222.             if direction == l then
  223.                 turtle.turnLeft()
  224.                 turtle.turnLeft()
  225.             elseif direction == b then
  226.                 turtle.turnLeft()
  227.             elseif direction == f then
  228.                 turtle.turnRight()
  229.             end
  230.             direction = r
  231.         elseif toDirection == l then
  232.             if direction == r then
  233.                 turtle.turnLeft()
  234.                 turtle.turnLeft()
  235.             elseif direction == b then
  236.                 turtle.turnRight()
  237.             elseif direction == f then
  238.                 turtle.turnLeft()
  239.             end
  240.             direction = l
  241.         end
  242.     end
  243. end
  244.  
  245. --チェストの座標を材料の番号から取得する。
  246. function getCordinate(chestNum)
  247.     lx = material[chestNum][1]
  248.     ly = material[chestNum][2]
  249.     lz = material[chestNum][3]
  250.    
  251.     return {lx, ly, lz}
  252. end
  253.  
  254. -- X軸の移動を行う
  255. function move_x(fgx)
  256.     if fgx == 0 then
  257.         return true
  258.     end
  259.    
  260.     if fgx > 0 then
  261.         setDirection(l)
  262.         for i=1, fgx do
  263.             turtle.forward()
  264.         end
  265.     else
  266.         setDirection(r)
  267.         for i=1, -fgx do
  268.             turtle.forward()
  269.         end
  270.     end
  271. end
  272.  
  273. -- Y軸の移動を行う
  274. function move_y(fgy)
  275.     if fgy == 0 then
  276.         return true
  277.     end
  278.    
  279.     if fgy > 0 then
  280.         setDirection(f)
  281.         for i=1, fgy do
  282.             turtle.forward()
  283.         end
  284.     else
  285.         setDirection(b)
  286.         for i=1, -fgy do
  287.             turtle.forward()
  288.         end
  289.     end
  290. end
  291.  
  292. -- Z軸の移動を行う
  293. function move_z(fgz)
  294.     if fgz == 0 then
  295.         return true
  296.     end
  297.    
  298.     if fgz > 0 then
  299.         for i=1, fgz do
  300.             turtle.up()
  301.         end
  302.     else
  303.         for i=1, -fgz do
  304.             turtle.down()
  305.         end
  306.     end
  307. end
  308.  
  309. -- タートルを指定した座標まで移動する
  310. -- 引数はType, ChestInfo変数を用意して受け取る。
  311. -- Typeは0が通常移動で、1がカウンターへ戻る。
  312. -- ChestInfoはmaterialの配列要素を渡す。
  313. -- チェスト、かまどの一歩手前で止まる必要があるので、チェストへの移動時はY軸に+1して計算する。
  314. function moveTurtle(...)
  315.     local args = {...}
  316.    
  317.     if args[1] == 0 then
  318.         chestInfo = args[2]
  319.         mxyz = getCordinate(chestInfo[1])
  320.        
  321.         gx = mxyz[1] - xyz[1]
  322.         gy = mxyz[2] - xyz[2] + 1
  323.         gz = mxyz[3] - xyz[3]
  324.        
  325.         move_x(gx)
  326.         move_y(gy)
  327.         move_z(gz)
  328.        
  329.         xyz = {xyz[1] + gx, xyz[2] + gy, xyz[3] + gz}
  330.     elseif args[1] == 1 then
  331.         mxyz = counter
  332.        
  333.         gx = mxyz[1] - xyz[1]
  334.         gy = mxyz[2] - xyz[2]
  335.         gz = mxyz[3] - xyz[3]
  336.        
  337.         move_z(gz)
  338.         move_x(gx)
  339.         move_y(gy)
  340.        
  341.         xyz = {xyz[1] + gx, xyz[2] + gy, xyz[3] + gz}
  342.     end
  343. end
  344.  
  345. -- チェストから材料を一個取り出す。
  346. -- suck関数はスタック単位でしか扱えないため、一旦取り出した後1個だけ残してしまうように処理する。
  347. function pickMaterial(slotNum)
  348.     turtle.select(slotNum)
  349.     setDirection(b)
  350.     turtle.suck()
  351.     dropNum = turtle.getItemCount(slotNum) - 1
  352.     turtle.drop(dropNum)
  353. end
  354.  
  355. -- 材料をかまどに投入し、回収する。
  356. function furnaceMaterial()
  357.     moveTurtle(0, furnace)
  358.     setDirection(b)
  359.     turtle.up()
  360.     turtle.forward()
  361.     turtle.dropDown()
  362.     turtle.back()
  363.     turtle.down()
  364.     turtle.down()
  365.     turtle.forward()
  366.     turtle.select(16)
  367.     repeat
  368.     until turtle.suckUp()
  369.     turtle.back()
  370.     turtle.up()
  371. end
  372.  
  373. -- 材料を調達し、クラフトする。完成品は16番スロットに格納される。
  374. -- ケーキの時に出るバケツなど、余った素材がある場合は下のチェストに格納する。
  375. function cook(product)
  376.     for i=1, 9 do
  377.         if i >= 1 and i <= 3 then
  378.             pickSlotNum = i
  379.         elseif i >= 4 and i <= 6 then
  380.             pickSlotNum = i + 1
  381.         elseif i >= 7 and i <= 9 then
  382.             pickSlotNum = i + 2
  383.         end
  384.        
  385.         if receipe[product[3]][i] then
  386.             moveTurtle(0, receipe[product[3]][i])
  387.             pickMaterial(pickSlotNum)
  388.         end
  389.     end
  390.    
  391.     if product[2] == "craft" then
  392.         turtle.select(16)
  393.         turtle.craft(1)
  394.     elseif product[2] == "furnace" then
  395.         furnaceMaterial()
  396.     end
  397.    
  398.     moveTurtle(1)
  399.     turtle.forward()
  400.     turtle.select(16)
  401.     turtle.drop()
  402.     turtle.back()
  403.    
  404.  
  405.     for i=1, 16 do
  406.         turtle.select(i)
  407.         turtle.dropDown()
  408.     end
  409. end
  410.  
  411. -- EXITメニューが選択されるまで、繰り返し実行。
  412. while running == true do
  413.     runMenu()
  414. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement