Advertisement
MadScience2728

DiamondTunnelServer

Mar 21st, 2022 (edited)
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --pastebin get p8QcFC8H startup
  2.  
  3. -- Awake
  4. print("Tunnel Server Alive: Hello World!")
  5.  
  6. -- Setup wireless
  7. local modem = peripheral.wrap("right")
  8. -- 210 Server -> Client
  9. -- 220 Client -> Server
  10. modem.open(210)
  11. modem.open(220)
  12. print("Channel 210 and 220 are open")
  13.  
  14. -- Setup sending wireless messages
  15. function SendMessage(message)
  16.     modem.transmit(210, 220, message)
  17. end
  18.  
  19. -- Setup receiving wireless messages
  20. function WaitForMessage(messageCompare)
  21.     while true do
  22.         local  
  23.             a,b,c,d,e,f = os.pullEvent("modem_message")
  24.         print("Message received: " .. tostring(e))
  25.         if(e == messageCompare) then
  26.             break
  27.         end
  28.     end
  29. end
  30.  
  31. -- Get turtle amount from user
  32. print("How many turtles are mining? >>")
  33. local desiredTurtleCount = tonumber(read())
  34.  
  35. -- Get skip checks from user
  36. print("Skip handshake? (y/n) >>")
  37. local skipHandshake = read()
  38.  
  39. if skipHandshake == "n" then
  40.     -- Send awake signal
  41.     print("Sending awake signal to all turtles...")
  42.     SendMessage("_ServerAwake")
  43.    
  44.     -- Wait for all turtles to register
  45.     print("Waiting for turtles to register...")
  46.     local turtleCount = 0
  47.     while true do
  48.         WaitForMessage("_TurtleRegistered")
  49.         turtleCount = turtleCount + 1
  50.         print("Turtle registered. Count is " .. tostring(turtleCount))
  51.         if(turtleCount == desiredTurtleCount) then break end
  52.     end
  53.     print("All turtles registered")
  54.    
  55.     -- Wait for all turtles to reset their y pos
  56.     print("Waiting for turtles to be ready to mine...")
  57.     local turtleCount = 0
  58.     while true do
  59.         WaitForMessage("_TurtleReadyToMine")
  60.         turtleCount = turtleCount + 1
  61.         print("Turtle ready to mine. Count is " .. tostring(turtleCount))
  62.         if(turtleCount == desiredTurtleCount) then break end
  63.     end
  64.     print("All turtles ready to mine")
  65.    
  66.     -- Begin managing mine operations
  67.     print("Mining...")
  68.     SendMessage("_Begin")    
  69. end
  70.  
  71. local readyCount = 0
  72. local cycleCount = 1
  73. local emptyCount = 0
  74. while true do
  75.     print("Starting mine cycle #" .. tostring(cycleCount))
  76.     if emptyCount == 64 then
  77.         SendMessage("_Empty")
  78.         emptyCount = 0
  79.     else
  80.         os.sleep(0.05)
  81.         SendMessage("_StartNextCycle")
  82.     end
  83.     print("Waiting for turtles to be ready for next cycle...")
  84.     readyCount = 0
  85.     while true do
  86.         WaitForMessage("_ReadyForNextCycle")
  87.         readyCount = readyCount + 1
  88.         print("A turtle is ready. Ready count is " .. tostring(readyCount))
  89.         if(readyCount == desiredTurtleCount) then
  90.             print("All turtles ready")
  91.             break
  92.         end
  93.     end
  94.     cycleCount = cycleCount + 1
  95.     emptyCount = emptyCount + 1
  96.     print(">>")
  97.     print(">>")
  98.     print(">>")
  99. end
  100.  
  101. -- Done
  102. print("Goodbye >>")
  103. read()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement