Advertisement
Calame

TurtleFleet

Dec 29th, 2020 (edited)
3,743
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.58 KB | None | 1 0
  1. ---------------------------
  2. -- TurtleFleet Installer --
  3. ---------------------------
  4. local width, height = term.getSize()
  5. local mid_x = math.ceil( width / 2 )
  6. local mid_y = math.ceil( height / 2 )
  7. local panel_x, panel_x2, panel_y, panel_y2 = mid_x - 16, mid_x + 16, mid_y - 3, mid_y + 3
  8. local yes_x = panel_x2 - 9
  9. local yes_x2 = panel_x2 - 1
  10. local btn_y = panel_y2
  11. local btn_y2 = panel_y2 + 2
  12. local no_x = panel_x + 1
  13. local no_x2 = panel_x + 9
  14.  
  15. local was_installed = false
  16. local theme = {}
  17.  
  18. local git_path = "https://raw.githubusercontent.com/Calame321/TurtleFleet/main/"
  19. local fleet_folder = "turtlefleet/"
  20. local all_files = {
  21.   "computer/computer_startup.lua",
  22.   "turtle/jobs/builder.lua",
  23.   "turtle/jobs/harvester.lua",
  24.   "turtle/jobs/lumberjack.lua",
  25.   "turtle/jobs/miner.lua",
  26.   "turtle/jobs/smelter.lua",
  27.   "turtle/advanced_turtle.lua",
  28.   "turtle/fleet_mode.lua",
  29.   "turtle/pathfind.lua",
  30.   "turtle/turtle_menu.lua",
  31.   "turtle/turtle_startup.lua",
  32.   "utils/update.lua",
  33. }
  34.  
  35. -- Center the text based on the total length
  36. function print_centered( text, from, to, line, back_color, fore_color )
  37.   back_color = back_color or theme.text_bg
  38.   fore_color = fore_color or theme.text_fg
  39.     local total_length = to - from
  40.   local half_text_length = #text / 2
  41.   local offset = math.ceil( ( ( total_length / 2 ) + from ) - half_text_length )
  42.   term.setCursorPos( offset, line )
  43.   term.setBackgroundColor( back_color )
  44.   term.setTextColor( fore_color )
  45.   term.write( text )
  46. end
  47.  
  48. -- Display a promt for the installation.
  49. function show_prompt()
  50.   if term.isColor() then
  51.     theme = {
  52.       bg = colors.white,
  53.       text_bg = colors.white,
  54.       text_fg = colors.blue,
  55.       title_bg = colors.lightBlue,
  56.       title_fg = colors.black,
  57.       window_border = colors.blue,
  58.       window_corners = colors.cyan,
  59.       window_text_fg = colors.green,
  60.       here_btn_bg = colors.green,
  61.       here_btn_fg = colors.orange,
  62.       disk_btn_bg = colors.brown,
  63.       disk_btn_fg = colors.yellow,
  64.       text_error = colors.red
  65.     }
  66.   else
  67.     theme = {
  68.       bg = colors.white,
  69.       text_bg = colors.white,
  70.       text_fg = colors.gray,
  71.       title_bg = colors.gray,
  72.       title_fg = colors.white,
  73.       window_border = colors.lightGray,
  74.       window_corners = colors.lime,
  75.       window_text_fg = colors.brown,
  76.       here_btn_bg = colors.brown,
  77.       here_btn_fg = colors.pink,
  78.       disk_btn_bg = colors.brown,
  79.       disk_btn_fg = colors.pink,
  80.       text_error = colors.white
  81.     }
  82.   end
  83.  
  84.   term.clear()
  85.   local lastBgColor = term.getBackgroundColor()
  86.  
  87.   -- Draw the panel.
  88.   paintutils.drawFilledBox( panel_x, panel_y, panel_x2, panel_y2, theme.bg )
  89.   -- Add a border.
  90.   paintutils.drawBox( panel_x - 1, panel_y - 1, panel_x2 + 1, panel_y2 + 1, theme.window_border )
  91.   paintutils.drawPixel( panel_x - 1, panel_y2 + 1, theme.window_corners )
  92.   paintutils.drawPixel( panel_x2 + 1, panel_y2 + 1, theme.window_corners )
  93.   -- Add box for the headder.
  94.   paintutils.drawFilledBox( panel_x - 1, panel_y - 1, panel_x2 + 1, panel_y + 1, theme.title_bg )
  95.   paintutils.drawLine( panel_x - 1, panel_y - 1, panel_x - 1, panel_y + 1, theme.window_corners )
  96.   paintutils.drawLine( panel_x2 + 1, panel_y - 1, panel_x2 + 1, panel_y + 1, theme.window_corners )
  97.   -- Add the title.
  98.   print_centered( "TurtleFleet Installer", panel_x, panel_x2, panel_y, theme.title_bg, theme.title_fg )
  99.   -- Add the text.
  100.   print_centered( "Where to install?", panel_x, panel_x2, panel_y + 3, nil, theme.window_text_fg )
  101.  
  102.   -- Button Here
  103.   paintutils.drawFilledBox( yes_x, btn_y, yes_x2, btn_y2, theme.here_btn_bg )
  104.   print_centered( "Here!", yes_x + 1, yes_x2, btn_y + 1, theme.here_btn_bg, theme.here_btn_fg )
  105.   print_centered( "\175" .. "    ", yes_x, yes_x2, btn_y2, theme.here_btn_bg, theme.here_btn_fg )
  106.  
  107.   -- Button Disk
  108.   paintutils.drawFilledBox( no_x, btn_y, no_x2, btn_y2, theme.disk_btn_bg )
  109.   print_centered( "Disk.", no_x + 1, no_x2, btn_y + 1, theme.disk_btn_bg, theme.disk_btn_fg )
  110.   print_centered( "\175" .. "   ", no_x, no_x2, btn_y2, theme.disk_btn_bg, theme.disk_btn_fg )
  111.  
  112.   term.setBackgroundColor( lastBgColor )
  113. end
  114.  
  115. -- Show installing message.
  116. function show_installing()
  117.   local _x = panel_x + 1
  118.   local _x2 = panel_x2 - 1
  119.   local _y = panel_y2
  120.   local _y2 = panel_y2 + 2
  121.   paintutils.drawFilledBox( _x, _y, _x2, _y2, theme.disk_btn_bg )
  122.   print_centered( "Installing...", _x + 1, _x2, _y + 1, theme.disk_btn_bg, theme.disk_btn_fg )
  123. end
  124.  
  125. -- Install the software on the current turtle or computer.
  126. function install_here()
  127.   fs.delete( "turtlefleet" )
  128.   fs.delete( "startup" )
  129.  
  130.   local to_install = {}
  131.   for i = 1, #all_files do
  132.     to_install[ git_path .. all_files[ i ] ] = fleet_folder .. all_files[ i ]
  133.   end
  134.  
  135.   to_install[ git_path .. "startup.lua" ] = "startup.lua"
  136.  
  137.   install_files( to_install )
  138.   was_installed = true
  139. end
  140.  
  141. -- Install the files on a disk for easy mass install.
  142. function install_disk()
  143.   fs.delete( "/disk/main_startup" )
  144.   fs.delete( "/disk/turtlefleet" )
  145.   fs.delete( "/disk/startup" )
  146.  
  147.   local to_install = {}
  148.   for i = 1, #all_files do
  149.     to_install[ git_path .. all_files[ i ] ] =  "/disk/" ..fleet_folder .. all_files[ i ]
  150.   end
  151.  
  152.   to_install[ git_path .. "startup.lua" ] = "/disk/main_startup.lua"
  153.   to_install[ git_path .. "disk_startup.lua" ] = "/disk/startup.lua"
  154.  
  155.   install_files( to_install )
  156.   was_installed = true
  157. end
  158.  
  159. -- Install the file from the dictionary.
  160. function install_files( files_to_install )
  161.   local i = 1
  162.  
  163.   for git_file, local_file in pairs( files_to_install ) do
  164.     local f = fs.open( local_file, "w" )
  165.     local w, m = http.get( git_file )
  166.  
  167.     if w then
  168.       f.write( w.readAll() )
  169.       f.flush()
  170.       f.close()
  171.  
  172.       local file_count = 0
  173.       for _ in pairs( files_to_install ) do file_count = file_count + 1 end
  174.       display_progress( i, file_count, local_file )
  175.  
  176.       i = i + 1
  177.     else
  178.       display_error( "Can't load '" .. local_file .. "' : " .. m )
  179.     end
  180.   end
  181. end
  182.  
  183. -- Display a progress bar.
  184. function display_progress( current, max, current_file )
  185.   local bar_width = 20
  186.   local progress = math.floor( current * bar_width / max )
  187.  
  188.   local bar = ""
  189.   local bar_fg = ""
  190.   local bar_bg = ""
  191.   for i = 1, bar_width do
  192.     if i <= progress then
  193.       bar = bar .. string.char( 128 )
  194.       bar_fg = bar_fg .. "0"
  195.       bar_bg = bar_bg .. "b"
  196.     else
  197.       bar = bar .. string.char( 127 )
  198.       bar_fg = bar_fg .. "3"
  199.       bar_bg = bar_bg .. "0"
  200.     end
  201.   end
  202.  
  203.   local total_length = panel_x2 - panel_x
  204.   local position_x = total_length / 2 - #bar / 2
  205.   term.setCursorPos( panel_x + position_x, panel_y + 4 )
  206.   term.setBackgroundColor( theme.bg )
  207.   term.setTextColor( theme.text_fg )
  208.  
  209.   if #current_file > 20 then
  210.     write( "..." .. string.sub( current_file, -17 ) )
  211.   else
  212.     write( current_file .. ( string.rep( " ", 20 - #current_file ) ) )
  213.   end
  214.  
  215.   term.setCursorPos( panel_x + position_x, panel_y + 3 )
  216.   term.blit( bar, bar_fg, bar_bg )
  217. end
  218.  
  219. -- Display an error if a file is not found.
  220. function display_error( message )
  221.   term.setCursorPos( 1, height )
  222.   term.setTextColor( theme.text_error )
  223.   term.write( message )
  224.   term.setTextColor( theme.text_fg )
  225. end
  226.  
  227. -- Show the prompt and listen to events.
  228. show_prompt()
  229.  
  230. while ( true ) do
  231.   local event, p1, p2, p3, p4 = os.pullEvent()
  232.  
  233.   -- If key pressed.
  234.   if ( event == "key" ) then
  235.     if keys.getName( p1 ) == 'h' then
  236.       show_installing()
  237.       install_here()
  238.       break
  239.     end
  240.  
  241.     if keys.getName( p1 ) == 'd' then
  242.       show_installing()
  243.       install_disk()
  244.       break
  245.     end
  246.   end
  247.  
  248.   -- If clicked.
  249.   if ( event == "mouse_click" ) then
  250.     local x = p2
  251.     local y = p3
  252.  
  253.     local here_was_clicked = x >= yes_x and x <= yes_x2 and y >= btn_y and y <= btn_y2
  254.     if ( here_was_clicked ) then
  255.       show_installing()
  256.       install_here()
  257.       break
  258.     end
  259.  
  260.     local disk_was_clicked = x >= no_x and x <= no_x2 and y >= btn_y and y <= btn_y2
  261.     if disk_was_clicked then
  262.       show_installing()
  263.       install_disk()
  264.       break
  265.     end
  266.   end
  267. end
  268.  
  269. -- Show the end message.
  270. function show_end()
  271.   local txt = "Installation Completed!"
  272.   local total_length = panel_x2 - panel_x
  273.   local position_x = total_length / 2 - #txt / 2
  274.   term.setCursorPos( panel_x + position_x + 1, panel_y + 4 )
  275.   write( txt )
  276.  
  277.   local _x = panel_x + 1
  278.   local _x2 = panel_x2 - 1
  279.   local _y = panel_y2
  280.   local _y2 = panel_y2 + 2
  281.   paintutils.drawFilledBox( _x, _y, _x2, _y2, theme.window_text_fg )
  282.   print_centered( "Press Enter to reboot.", _x + 1, _x2, _y + 1, theme.window_text_fg, theme.text_bg )
  283. end
  284.  
  285. show_end()
  286. read()
  287. os.reboot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement