Advertisement
Calame

Test turtlefleet

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