Advertisement
Guest User

minex-insall.lua

a guest
Sep 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.15 KB | None | 0 0
  1. -- A program to control multiple mining turtles.
  2. -- For UnbrokenMotion and Friends.
  3. -- Version 0.1.
  4. -- This code is probably a [redacted for discord] mess
  5.  
  6. local termWidth, termHeight = term.getSize()
  7. local termHalfWidth = math.floor(termWidth / 2)
  8. local termHalfHeight = math.floor(termHeight / 2)
  9.  
  10. -- Character table.
  11. -- If you have a better way of doing this, lemme know.
  12. local keyboard = {
  13.     [30] = 'a',
  14.     [48] = 'b',
  15.     [46] = 'c',
  16.     [32] = 'd',
  17.     [18] = 'e',
  18.     [33] = 'f',
  19.     [34] = 'g',
  20.     [35] = 'h',
  21.     [23] = 'i',
  22.     [36] = 'j',
  23.     [37] = 'k',
  24.     [38] = 'l',
  25.     [50] = 'm',
  26.     [49] = 'n',
  27.     [24] = 'o',
  28.     [25] = 'p',
  29.     [16] = 'q',
  30.     [19] = 'r',
  31.     [31] = 's',
  32.     [20] = 't',
  33.     [22] = 'u',
  34.     [47] = 'v',
  35.     [17] = 'w',
  36.     [45] = 'x',
  37.     [21] = 'y',
  38.     [44] = 'z',
  39.     [2] = '1',
  40.     [3] = '2',
  41.     [4] = '3',
  42.     [5] = '4',
  43.     [6] = '5',
  44.     [7] = '6',
  45.     [8] = '7',
  46.     [9] = '8',
  47.     [10] = '9',
  48.     [11] = '0',
  49.     [57] = ' '
  50. }
  51.  
  52. -- dependencies table, used to download additional scripts from pastebin or redwyn.moe during installation.
  53. local deps = {
  54.     serverMain = "abcdefgh",
  55.     turtleMain = "ijklmnop"
  56. }
  57.  
  58. -- Self explanatory, this function prints text centered on the screen.
  59. -- Or at least, it tries to.
  60. function centeredText(y, text)
  61.     local textLength = string.len(text)
  62.     local cursorXPos = termHalfWidth - math.floor(textLength / 2)
  63.     term.setCursorPos(cursorXPos, y)
  64.     term.write(text)
  65. end
  66.  
  67. -- A standard dialog screen.
  68. function dialog(text, bgColor, fgColor, btColor)
  69.     -- Set background color.
  70.     paintutils.drawFilledBox(1, 2, termWidth, termHeight, bgColor)
  71.     term.setBackgroundColor(bgColor)
  72.     term.setTextColor(fgColor)
  73.     centeredText(termHalfHeight, text)
  74.    
  75.     --draw buttons.
  76.     local buttonEnd = (termHalfWidth + 3)
  77.     local buttonStart = (termHalfWidth - 2)
  78.    
  79.     local yPos = (termHalfHeight + 2)
  80.  
  81.     paintutils.drawLine(buttonStart, yPos, buttonEnd, yPos, btColor)
  82.     term.setCursorPos(buttonStart+1, yPos)
  83.     term.write("Okay")
  84.    
  85.     --wait for input.
  86.     while true do
  87.         -- sets multiple variables using os.pullEvent, if the mouse if clicked.
  88.         local event, button, x, y = os.pullEvent("mouse_click")
  89.         -- Basic logic to check which button has been pressed.
  90.         if y == yPos then
  91.             if x >= buttonStart and x <= buttonEnd then
  92.                 break
  93.             end
  94.         end
  95.     end
  96. end
  97.  
  98. -- A y/n dialog screen.
  99. function truthDialog(text, bgColor, fgColor, btColor)
  100.     -- Set background color.
  101.     paintutils.drawFilledBox(1, 2, termWidth, termHeight, bgColor)
  102.     term.setBackgroundColor(bgColor)
  103.     term.setTextColor(fgColor)
  104.     centeredText(termHalfHeight, text)
  105.    
  106.     --draw buttons.
  107.     local yesEnd = (termHalfWidth - 1)
  108.     local yesStart = (yesEnd - 4)
  109.     local noStart = (termHalfWidth + 1)
  110.     local noEnd = (noStart + 3)
  111.     local yPos = (termHalfHeight + 2)
  112.  
  113.     paintutils.drawLine(yesStart, yPos, yesEnd, yPos, btColor)
  114.     paintutils.drawLine(noStart, yPos, noEnd, yPos, btColor)
  115.     term.setCursorPos(yesStart+1, yPos)
  116.     term.write("yes")
  117.    
  118.     term.setCursorPos(noStart+1, yPos)
  119.     term.write("no")
  120.    
  121.     --wait for input.
  122.     while true do
  123.         -- sets multiple variables using os.pullEvent, if the mouse is clicked.
  124.         local event, button, x, y = os.pullEvent("mouse_click")
  125.        
  126.         -- Basic logic to check which button has been pressed.
  127.         if y == yPos then
  128.             if x >= yesStart and x <= yesEnd then
  129.                 return true
  130.             elseif x >= noStart and x <= noEnd then
  131.                 return false
  132.             end
  133.         end
  134.     end
  135. end
  136.  
  137. -- A textfield dialog screen.
  138. function textDialog(text, bgColor, fgColor, btColor)
  139.     -- Set background color.
  140.     paintutils.drawFilledBox(1, 2, termWidth, termHeight, bgColor)
  141.     term.setBackgroundColor(bgColor)
  142.     term.setTextColor(fgColor)
  143.     centeredText(termHalfHeight, text)
  144.    
  145.     --draw buttons.
  146.     local buttonEnd = (termHalfWidth + 3)
  147.     local buttonStart = (termHalfWidth - 2)
  148.    
  149.     local yPos = (termHalfHeight + 5)
  150.  
  151.     paintutils.drawLine(1, yPos - 2, termWidth, yPos - 2, btColor)
  152.     paintutils.drawLine(buttonStart, yPos, buttonEnd, yPos, btColor)
  153.     term.setCursorPos(buttonStart+1, yPos)
  154.     term.write("Okay")
  155.    
  156.     --wait for input.
  157.     local content = ""
  158.     while true do
  159.         -- sets multiple variables using os.pullEvent, if key is pressed.
  160.         local event, key, mouseX, mouseY = os.pullEvent()
  161.         -- return stored value when enter key pressed
  162.         if event == "key" then
  163.             if key == keys.enter then
  164.                 return content
  165.         -- Otherwise, concatenate to the value.
  166.             elseif keyboard[key] ~= nil and string.len(content) < termWidth - 2 then
  167.                 content = content .. keyboard[key]
  168.                 -- display output.
  169.                 term.setCursorPos(2, yPos - 2)
  170.                 term.write(content)
  171.             end
  172.         end
  173.        
  174.         -- check if button clicked
  175.         -- Basic logic to check which button has been pressed.
  176.         if event == "mouse_click" then
  177.             if mouseY == yPos then
  178.                 if mouseX >= buttonStart and mouseX <= buttonEnd then
  179.                     return content
  180.                 end
  181.             end
  182.         end
  183.     end
  184. end
  185.  
  186. -- Setup start.
  187. dialog("Welcome to mineX!", colors.blue, colors.white, colors.lightBlue)
  188. dialog("Please follow the prompts.", colors.blue, colors.white, colors.lightBlue)
  189.  
  190. term.setCursorPos(1, termHeight)
  191. if turtle then
  192.     term.write("Searching for host...")
  193.     sleep(1)
  194.     local modem = peripheral.wrap("right")
  195.     modem.transmit(3, 1, "Turtle ping")
  196. else
  197.     dialog("Press okay to begin server setup.", colors.blue, colors.white, colors.lightBlue)
  198.    
  199.     -- Begin configuration.
  200.    
  201.     -- Assign a server label.
  202.     local newServerLabel = ""
  203.     local serverLabelConfirm = false
  204.     while serverLabelConfirm ~= true do
  205.         newServerLabel = textDialog("Please enter this server's label.", colors.blue, colors.white, colors.lightBlue)
  206.         serverLabelConfirm = truthDialog("Are you sure you'd like to use '" .. newServerLabel .. "'?", colors.blue, colors.white, colors.lightBlue)
  207.     end
  208.     os.setComputerLabel(newServerLabel)
  209.    
  210.     -- Set server port
  211.     local serverPort = 0
  212.     local serverPortConfirm = false
  213.     while serverPortConfirm ~= true do
  214.         serverPort = tonumber(textDialog("Please enter a server SEND port (1 - 65535)", colors.blue, colors.white, colors.lightBlue))
  215.        
  216.         -- Ensure the provided value is valid.
  217.         if serverPort > 0 and serverPort <= 65535 then
  218.             serverPortConfirm = truthDialog("Are you sure you want to use port " .. serverPort .. "?", colors.blue, colors.white, colors.lightBlue)
  219.         else
  220.             dialog("The port you have entered is invalid.", colors.blue, colors.white, colors.lightBlue)
  221.         end
  222.     end
  223. end
  224.  
  225. term.setBackgroundColor(colors.black)
  226. term.setTextColor(colors.white)
  227. term.clear()
  228. term.setCursorPos(1, termHeight)
  229. print("Minex setup complete!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement