DragonZBW

turtleCommand

Apr 16th, 2021 (edited)
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. -- PASTE FNbnMgtK
  2.  
  3. -- Open modem
  4. local modem = peripheral.find("modem")
  5. modem.open(2)
  6.  
  7. -- Load in software library
  8. local sl = require("softwareLib")
  9.  
  10. -- Set color scheme to default
  11. local function defaultColors()
  12. sl.fill(colors.lightBlue)
  13. sl.color(colors.black)
  14. end
  15.  
  16. -- Set colors scheme to submenu scheme
  17. local function subColors()
  18. sl.fill(colors.white)
  19. sl.color(colors.black)
  20. end
  21.  
  22. -- List connected turtles
  23. local function checkConnections()
  24. subColors()
  25. sl.panel(4,4,sl.getWidth() - 6, sl.getHeight() - 6)
  26. sl.xyCenteredText("Checking connections...")
  27. modem.transmit(1,2,"CheckConnection")
  28. defaultColors()
  29.  
  30. -- Wait for responses from turtles
  31. local numConnected = 0
  32. os.startTimer(3)
  33. local event, peripheral_name, channel, replyChannel, message, distance
  34. while event ~= "timer" do
  35. event, peripheral_name, channel, replyChannel, message, distance = os.pullEvent()
  36. if event == "modem_message" and message == "CheckConnection true" then
  37. numConnected = numConnected + 1
  38. end
  39. end
  40.  
  41. -- Display number of connected turtles
  42. sl.text(1,sl.getHeight(),numConnected .. (numConnected == 1 and " turtle" or " turtles") .. " connected")
  43. end
  44.  
  45. -- Sync turtles' individual maps
  46. local function syncMap()
  47. modem.transmit(1,2,"GetMap")
  48.  
  49. -- Wait for responses from turtles
  50. os.startTimer(5)
  51. local event, peripheral_name, channel, replyChannel, message, distance
  52. while event ~= "timer" do
  53. event, peripheral_name, channel, replyChannel, message, distance = os.pullEvent()
  54. if event == "modem_message" and string.sub(message, 1, 7) == "GetMap " then
  55.  
  56. end
  57. end
  58.  
  59. -- Sync maps
  60.  
  61. -- Set all turtles to use the new synced map
  62. end
  63.  
  64. -- Command turtles to go to a location
  65. local function move()
  66. local function goHome()
  67. local x,y,z = gps.locate()
  68. modem.transmit(1,2,"GoAbsolute " .. x .. " " .. y .. " " .. z)
  69. end
  70. local function moveRelative()
  71.  
  72. end
  73. local function moveAbsolute()
  74.  
  75. end
  76. -- Draw menu
  77. subColors()
  78. sl.panel(3,3,10,6)
  79. sl.text(4,4,"MOVE")
  80. sl.menu(4,5,1,
  81. {sl.menuOption("Home", goHome),
  82. sl.menuOption("Relative", moveRelative),
  83. sl.menuOption("Absolute", moveAbsolute)})
  84. defaultColors()
  85. end
  86.  
  87. -- Command turtles to explore
  88. local function explore()
  89. modem.transmit(1,2,"Explore")
  90. end
  91.  
  92. -- View the current map
  93. local function viewMap()
  94. -- Ask the user if they want to sync the map first
  95. subColors()
  96. sl.panel(4,4,sl.getWidth() - 6, sl.getHeight() - 6)
  97. sl.text(6,4,"SYNC FIRST?")
  98.  
  99. -- Sync the map first
  100. local function yes()
  101. syncMap()
  102. no()
  103. end
  104. -- Don't sync the map first, just view
  105. local function no()
  106.  
  107. -- Show map
  108.  
  109. -- Set color scheme back to normal
  110. defaultColors()
  111. end
  112.  
  113. -- Menu to sync map
  114. sl.xyCenteredMenu(1, {sl.menuOption("Yes", yes), sl.menuOption("No", no)})
  115. defaultColors()
  116. end
  117.  
  118. -- Show main menu
  119. local function mainMenu()
  120. -- BG
  121. defaultColors()
  122. sl.panel(0,0,sl.getWidth() + 2, sl.getHeight() + 2)
  123. sl.panel(1,1,sl.getWidth(), sl.getHeight() - 1)
  124.  
  125. -- Menu
  126. sl.text(3,1,"TURTLE COMMANDER")
  127.  
  128. checkConnections()
  129.  
  130. while true do
  131. sl.panel(1,1,sl.getWidth(), sl.getHeight() - 1)
  132. sl.text(3,1,"TURTLE COMMANDER")
  133. sl.xyCenteredMenu(1,
  134. {sl.menuOption("Check Connections", checkConnections),
  135. sl.menuOption("Sync Map", syncMap),
  136. sl.menuOption("Move", move),
  137. sl.menuOption("Explore", explore),
  138. sl.menuOption("View Map", viewMap)})
  139. end
  140. end
  141.  
  142. -- Program logic
  143. mainMenu()
Add Comment
Please, Sign In to add comment