MadScience2728

MINESERVER

Feb 21st, 2021 (edited)
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Awake
  2. print("Mining server awake: Hello World!")
  3.  
  4. -- Setup wireless
  5. local modemSide = "left"
  6. local modem = peripheral.wrap(modemSide)
  7. -- 5 Server -> Client
  8. -- 6 Client -> Server
  9. -- 7 Server -> Pocket Computer
  10. modem.open(5)
  11. modem.open(6)
  12. modem.open(7)
  13. modem.open(8)
  14. print("Channel 5 and 6 are open, as well as 7 and 8")
  15.  
  16. -- Setup sending wireless messages
  17. function SendMessage(message)
  18.     modem.transmit(5, 6, message)
  19. end
  20. function SendMessageToPocketComputer(message)
  21.     modem.transmit(7, 7, message)
  22. end
  23.  
  24. --Pocket computer helper
  25. function printf(m)
  26.     print(m)
  27.     SendMessageToPocketComputer(m)
  28. end
  29. printf("Link to pocket computer established. Hello")
  30.  
  31. -- Setup receiving wireless messages
  32. function WaitForMessage(messageCompare)
  33.     while true do
  34.         local  
  35.             a,b,c,d,e,f = os.pullEvent("modem_message")
  36.         printf("Message received: " .. tostring(e))
  37.         if(e == messageCompare) then
  38.             break
  39.         end
  40.     end
  41. end
  42.  
  43. -- Get turtle amount from users
  44. printf("How many turtles are mining? >>")
  45. local desiredTurtleCount = tonumber(read())
  46.  
  47. -- Get cycle amount from users
  48. printf("How many cycles should they mine for? >>")
  49. local desiredCycleCount = tonumber(read())
  50.  
  51. -- Send awake signal
  52. printf("Sending awake signal to all turtles...")
  53. SendMessage("_ServerAwake")
  54.  
  55. -- Wait for all turtles to register
  56. -- Accept and continue from user
  57. printf("Waiting for turtles to register...")
  58. local turtleCount = 0
  59. while true do
  60.     WaitForMessage("_TurtleRegistered")
  61.     turtleCount = turtleCount + 1
  62.     printf("Turtle registered. Count is " .. tostring(turtleCount))
  63.     if(turtleCount == desiredTurtleCount) then break end
  64. end
  65.  
  66. -- Begin when user is ready
  67. printf("Press any key when ready to begin >>")
  68. read()
  69. SendMessage("_Begin")
  70.  
  71. -- Begin managing mine operations
  72. printf("Mining...")
  73. local readyCount = 0
  74. local cycleCount = 1
  75. while true do
  76.     printf("Starting mine cycle #" .. tostring(cycleCount))
  77.     modem.transmit(8, 8, "Starting mine cycle #" .. tostring(cycleCount))
  78.     SendMessage("_StartNextCycle")
  79.     printf("Waiting for turtles to be ready for next cycle...")
  80.     readyCount = 0
  81.     while true do
  82.         WaitForMessage("_ReadyForNextCycle")
  83.         readyCount = readyCount + 1
  84.         printf("A turtle is ready. Ready count is " .. tostring(readyCount))
  85.         if(readyCount == turtleCount) then
  86.             printf("All turtles ready")
  87.             break
  88.         end
  89.     end
  90.     if(cycleCount == desiredCycleCount) then
  91.         printf("All cycle complete")
  92.         break
  93.     end
  94.     cycleCount = cycleCount + 1
  95.     printf(">>")
  96.     printf(">>")
  97.     printf(">>")
  98. end
  99.  
  100. -- Done
  101. printf("Goodbye >>")
  102. read()
Add Comment
Please, Sign In to add comment