Demos_

MinecraftLua_FlowerTurtleV2

Jan 19th, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.11 KB | None | 0 0
  1. -- TurtFlowerV2
  2.  
  3. -- Basic behaviour
  4. -- ================
  5.  
  6. -- try to find idComp
  7. -- wait for order
  8. -- when get order use bone meal if empty grab from chest on top
  9. -- if it's good use it bellow and send "good" ack
  10. -- if not send "notgood" ack
  11. -- then wait for other order
  12.  
  13.  
  14. -- DEBUG
  15.  
  16. DEBUG = true
  17. if DEBUG then
  18.     print("[DEBUG] Start of CompFlowerV2")
  19. end
  20.  
  21. hostnameComp = "compFlower"
  22. hostnameCmd = "cmdFlower"
  23. idComp = -1
  24.  
  25. idCmd = -1
  26.  
  27. -- modem on the right of the turtle
  28.  
  29. rednetHostname = "turtFlower"
  30. rednet.open("right")
  31. rednetProtocol = "flower"
  32. rednet.host(rednetProtocol,rednetHostname)
  33.  
  34. idStop = 3
  35.  
  36. if DEBUG then
  37.     print("[DEBUG] rednet => prot : " ..rednetProtocol.."; hostname : "..rednetHostname)
  38. end
  39.  
  40. function FindCompId()
  41.     if DEBUG then
  42.         print("[DEBUG] Start function FindCompId")
  43.     end
  44.     local id
  45.     while idComp == -1 do
  46.         id = rednet.lookup(rednetProtocol,hostnameComp)
  47.         if id then
  48.             if DEBUG then
  49.                 print("[DEBUG] idComp = ",id)
  50.             end
  51.             idComp = id
  52.         end
  53.     end
  54. end
  55.  
  56. function FindCmdId()
  57.     if DEBUG then
  58.         print("[DEBUG] Start function FindCmdId")
  59.     end
  60.     local id
  61.     while idCmd == -1 do
  62.         id = rednet.lookup(rednetProtocol,hostnameCmd)
  63.         if id then
  64.             if DEBUG then
  65.                 print("[DEBUG] idCmd = ",id)
  66.             end
  67.             idCmd = id
  68.         end
  69.     end
  70. end
  71.  
  72. function ExecuteOrder(id)
  73.     gotStop = false
  74.     if DEBUG then
  75.         print("[DEBUG] Start of ExecuteOrder id = ",id)
  76.     end
  77.     if id == -1 or id == idStop or id == "stop" then
  78.         SendAck(true)
  79.         return
  80.     end
  81.     turtle.select(id)
  82.     turtle.suckUp()
  83.     if turtle.getItemCount() > 0 then
  84.             SendAck(true)
  85.         else
  86.             SendAck(false)
  87.             return
  88.         end
  89.     while not gotStop do
  90.         turtle.placeDown()
  91.         rednet.send(idCmd,true,rednetProtocol)
  92.         turtle.suckUp()
  93.         id,msg,p,d = rednet.receive(rednetProtocol,1)
  94.         if msg == "stop" then
  95.             gotStop = true
  96.             SendAck(true)
  97.         end
  98.     end
  99. end
  100.  
  101. function SendAck(ack)
  102.     if DEBUG then
  103.         print("[DEBUG] Start of SendAck ack = "..ack)
  104.     end
  105.     rednet.send(idComp,ack,rednetProtocol)
  106. end
  107.  
  108.  
  109. FindCompId()
  110. FindCmdId()
  111. while true do
  112.     id,msg,d,p = rednet.receive(rednetProtocol)
  113.     ExecuteOrder(msg or -1)
  114. end
Advertisement
Add Comment
Please, Sign In to add comment