Advertisement
Demos_

MinecraftLua_FlowerCompV2

Jan 19th, 2020
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 KB | None | 0 0
  1. -- CompFlowerV2
  2.  
  3. -- Layout
  4. -- =======
  5.  
  6. -- [-] = wall; [x] = grass; [T] = turtle; [p] = piston
  7. --
  8. -- top view                    
  9.  
  10. -- [-][-][-][-][-][-][-][-][-]
  11. -- [-][x][x][x][x][x][x][x][-]
  12. -- [-][x][x][x][x][x][x][x][-]
  13. -- [-][x][x][x][x][x][x][x][-]
  14. -- [-][x][x][x][T][x][x][x][-]
  15. -- [-][x][x][x][x][x][x][x][-]
  16. -- [-][x][x][x][x][x][x][x][-]
  17. -- [-][x][x][x][x][x][x][x][-]
  18. -- [-][-][-][-][-][-][-][-][-]
  19.  
  20. -- side view
  21.  
  22. -- [-]         [T]         [-]
  23. -- [-]                     [-]
  24. -- [p][x][x][x][x][x][x][x]   [p]
  25. -- [-][-][-][-][-][-][-][-][-]
  26.  
  27. -- a computer and a monitor to control that also need a chest near the turtle
  28. -- for coal if the turtle need to move and bone meal + botania bone meal
  29. --
  30.  
  31. -- Basic behaviour
  32. -- ===============
  33.  
  34. -- Init the screen (3 buttons: vanilla bone meal, botania bone meal, stop)
  35. -- wait for button to be push
  36. --   send order stop (to stop the current turtle task even if none)
  37. --   ask if turtle is ready
  38. --   send the order pushed
  39. -- back to wait for button push
  40.  
  41.  
  42. --DEBUG
  43.  
  44. DEBUG = true
  45. if DEBUG then
  46.     print("[DEBUG] Start of CompFlowerV2")
  47. end
  48.  
  49. -- Need the button api
  50. -- button api from Siarko => pastebin get Lcz2CpsY button
  51.  
  52. os.loadAPI("button")
  53.  
  54. -- modem on the left of the computer
  55.  
  56. rednetHostname = "compFlower"
  57. rednet.open("left")
  58. rednetProtocol = "flower"
  59. rednetTimeout = 40 -- sec
  60. rednet.host(rednetProtocol,rednetHostname)
  61.  
  62. tryAck = 0
  63. tryMax = 5
  64.  
  65. -- choosing a hostname for the turtle for easy com
  66. hostnameTurtle = "turtFlower"
  67. idTurtle = -1
  68. if DEBUG then
  69.     print("[DEBUG] rednet => prot : " ..rednetProtocol.."; hostname : "..rednetHostname.." timeout : ",rednetTimeout)
  70. end
  71.  
  72. -- Monitors on the right of the computer (using two monitors for look)
  73. m = peripheral.wrap("right")
  74.  
  75. -- set the monitor for the button api
  76. button.setMonitor(m)
  77.  
  78. -- allBtnsName = every btn name
  79. allBtnsName = {}
  80. allBtnsName[1] = "Bone Meal"
  81. allBtnsName[2] = "Botania"
  82. allBtnsName[3] = "Stop"
  83.  
  84. idBtnStop = 3
  85. idActiveBtn = idBtnStop
  86. myButtons = {}
  87. -- small screen should'nt have more than three
  88.  
  89.  
  90.  
  91.  
  92. function InitScreen()
  93.     if DEBUG then
  94.         print("[DEBUG] Start of the function InitScreen")
  95.     end
  96.  
  97.     -- m to access monitor
  98.     m.clear()
  99.  
  100.     cptOffsetBtn = 0
  101.    
  102.     for i,v in pairs(allBtnsName) do
  103.         myButtons[i] = button.create()
  104.         -- button.setText("Value",bool:resize)
  105.         myButtons[i].setText(" "..v.." ",true)
  106.         myButtons[i].setAlign("center")
  107.         myButtons[i].setPos(2,i+cptOffsetBtn)
  108.         myButtons[i].setColor(colors.gray)
  109.         myButtons[i].setBlinkColor(colors.lime)
  110.         myButtons[i].onClickReturn(i)
  111.         cptOffsetBtn = cptOffsetBtn+1
  112.     end
  113.    
  114.     myButtons[idBtnStop].setColor(colors.lime)
  115.     idActiveBtn = idBtnStop
  116.    
  117. end
  118.  
  119. function FindTurtleId()
  120.     if DEBUG then
  121.         print("[DEBUG] Start function FindTurtleId")
  122.     end
  123.     local id
  124.     while idTurtle == -1 do
  125.         id = rednet.lookup(rednetProtocol,hostnameTurtle)
  126.         if DEBUG then
  127.             print("[DEBUG] id = ",id)
  128.         end
  129.         if id then
  130.             if DEBUG then
  131.                 print("[DEBUG] idTurtle = ",id)
  132.             end
  133.             idTurtle = id
  134.         end
  135.     end
  136. end
  137.  
  138. function StopPlz()
  139.     gotAnswer = false
  140.     while not gotAnswer do
  141.         rednet.send(idTurtle,"stop",rednetProtocol)
  142.         id,msg,p,d = rednet.receive(rednetProtocol,3)
  143.         if msg == true then
  144.             gotAnswer = msg
  145.         end
  146.     end
  147. end
  148.  
  149. function SendOrder(id)
  150.    
  151.     if DEBUG then
  152.         print("[DEBUG] Start function SendOrder id => ",id)
  153.     end
  154.     if id == -1 then
  155.         return;
  156.     end
  157.     StopPlz()
  158.     if id ~= idBtnStop then
  159.         rednet.send(idTurtle,id,rednetProtocol)
  160.         idC,ack,p,d = rednet.receive(rednetProtocol,rednetTimeout)
  161.         HandleAck(ack,id)
  162.     else
  163.         HandleAck(true,id)
  164.     end
  165. end
  166.  
  167.  
  168. function HandleAck(ack,id)
  169.     if DEBUG then
  170.         print("[DEBUG] Start function HandleAck ack, id = ",ack,v)
  171.     end
  172.     -- define possible ack
  173.     myButtons[idActiveBtn].setColor(colors.gray)
  174.     if ack then
  175.         myButtons[id].setColor(colors.lime)
  176.         idActiveBtn = id
  177.     else
  178.         -- put everythin on stop state
  179.         myButtons[id].setColor(colors.red)
  180.         idActiveBtn = id
  181.     end
  182. end
  183.  
  184. InitScreen()
  185. FindTurtleId()
  186. while true do
  187.   e,v = pcall(button.await,myButtons)
  188.   SendOrder(v or -1)
  189. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement