Advertisement
infiniteblock

Untitled

Nov 12th, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.40 KB | None | 0 0
  1. if not term.isColor() then
  2. print("Advanced computer required")
  3. error()
  4. end
  5. print("loading...")
  6.  
  7. monitor_textScale = 0.5
  8.  
  9. Style = {
  10. CDefault = colors.black,
  11. BGDefault = colors.lightGray,
  12.  
  13. CTitle = colors.orange,
  14. BGTitle = colors.black,
  15.  
  16. CFooter = colors.white,
  17. BGFooter = colors.blue,
  18.  
  19. CWarning = colors.white,
  20. BGWarning = colors.red,
  21.  
  22. CSuccess = colors.white,
  23. BGSuccess = colors.lime,
  24.  
  25. CDisabled = colors.gray,
  26. BGDisabled = colors.blue,
  27.  
  28. CRadarmap = colors.gray,
  29. BGRadarmap = colors.green,
  30.  
  31. CRadarborder = colors.white,
  32. BGRadarborder = colors.black,
  33.  
  34. CRadarself = colors.white,
  35. BGRadarself = colors.lime,
  36. TextRadarself = "R",
  37.  
  38. CRadarother = colors.black,
  39. BGRadarother = colors.red,
  40. TextRadarother = "#"
  41. }
  42.  
  43. ----------- Monitor support
  44.  
  45. function SetMonitorColorFrontBack(frontColor, backgroundColor)
  46. term.setBackgroundColor(backgroundColor)
  47. term.setTextColor(frontColor)
  48. if monitors ~= nil then
  49. for key,monitor in pairs(monitors) do
  50. monitor.setTextColor(frontColor)
  51. monitor.setBackgroundColor(backgroundColor)
  52. end
  53. end
  54. end
  55.  
  56. function Write(text)
  57. term.write(text)
  58. if monitors ~= nil then
  59. for key,monitor in pairs(monitors) do
  60. if key ~= data.radar_monitorIndex then
  61. monitor.write(text)
  62. end
  63. end
  64. end
  65. end
  66. local function getCursorPos()
  67. local x, y = term.getCursorPos()
  68. return x, y
  69. end
  70.  
  71. function SetCursorPos(x, y)
  72. term.setCursorPos(x, y)
  73. if monitors ~= nil then
  74. for key,monitor in pairs(monitors) do
  75. if key ~= data.radar_monitorIndex then
  76. monitor.setCursorPos(x, y)
  77. end
  78. end
  79. end
  80. end
  81.  
  82. local function getResolution()
  83. local sizeX, sizeY = term.getSize()
  84. return sizeX, sizeY
  85. end
  86.  
  87. function SetColorDefault()
  88. SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
  89. end
  90.  
  91. function SetColorTitle()
  92. SetMonitorColorFrontBack(Style.CTitle, Style.BGTitle)
  93. end
  94.  
  95. function SetColorFooter()
  96. SetMonitorColorFrontBack(Style.CFooter, Style.BGFooter)
  97. end
  98.  
  99. function SetColorWarning()
  100. SetMonitorColorFrontBack(Style.CWarning, Style.BGWarning)
  101. end
  102.  
  103. function SetColorSuccess()
  104. SetMonitorColorFrontBack(Style.CSuccess, Style.BGSuccess)
  105. end
  106.  
  107. function SetColorDisabled()
  108. SetMonitorColorFrontBack(Style.CDisabled, Style.BGDisabled)
  109. end
  110.  
  111. function SetColorNormal()
  112. SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
  113. end
  114.  
  115. function SetColorRadarmap()
  116. SetMonitorColorFrontBack(Style.CRadarmap, Style.BGRadarmap)
  117. end
  118.  
  119. function SetColorRadarborder()
  120. SetMonitorColorFrontBack(Style.CRadarborder, Style.BGRadarborder)
  121. end
  122.  
  123. local function clear(colorFront, colorBack)
  124. clearWarningTick = -1
  125. if CDefault == nil or BGDefault == nil then
  126. SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
  127. else
  128. SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
  129. end
  130. term.clear()
  131. if monitors ~= nil then
  132. for key, monitor in pairs(monitors) do
  133. if key ~= data.radar_monitorIndex then
  134. monitor.clear()
  135. end
  136. end
  137. end
  138. SetCursorPos(1, 1)
  139. end
  140.  
  141. local function clearLine()
  142. term.clearLine()
  143. if monitors ~= nil then
  144. for key, monitor in pairs(monitors) do
  145. if key ~= data.radar_monitorIndex then
  146. monitor.clearLine()
  147. end
  148. end
  149. end
  150. local x, y = getCursorPos()
  151. SetCursorPos(1, y)
  152. end
  153.  
  154. function WriteLn(text)
  155. Write(text)
  156. local x, y = term.getCursorPos()
  157. local width, height = term.getSize()
  158. if y > height - 1 then
  159. y = 1
  160. end
  161. SetCursorPos(1, y + 1)
  162. end
  163.  
  164. function WriteCentered(y, text)
  165. local unused
  166. if text == nil then
  167. text = y
  168. unused, y = getCursorPos()
  169. end
  170.  
  171. SetCursorPos((51 - text:len()) / 2, y)
  172. term.write(text)
  173. if monitors ~= nil then
  174. for key,monitor in pairs(monitors) do
  175. if key ~= data.radar_monitorIndex then
  176. local sizeX, sizeY = monitor.getSize()
  177. if sizeX ~= nil then
  178. monitor.setCursorPos((sizeX - text:len()) / 2, y)
  179. monitor.write(text)
  180. end
  181. end
  182. end
  183. end
  184. local xt, yt = term.getCursorPos()
  185. SetCursorPos(1, y + 1)
  186. end
  187. local function page_begin(text)
  188. clear()
  189. SetColorTitle()
  190. SetCursorPos(1, 1)
  191. SetColorTitle()
  192. clearLine()
  193. WriteCentered(1, text)
  194. SetCursorPos(1, 2)
  195. SetColorNormal()
  196. end
  197. function ShowTitle(text)
  198. clear()
  199. SetCursorPos(0, 1)
  200. SetColorTitle()
  201. clearLine()
  202. WriteCentered(1, text)
  203. SetCursorPos(1, 2)
  204. SetColorDefault()
  205. end
  206.  
  207. function ShowMenu(text)
  208. Write(text)
  209. local sizeX, sizeY = term.getSize()
  210. local xt, yt = term.getCursorPos()
  211. for i = xt, sizeX do
  212. Write(" ")
  213. end
  214. SetCursorPos(1, yt + 1)
  215. end
  216.  
  217. local clearWarningTick = -1
  218. function ShowWarning(text)
  219. local sizeX, sizeY = term.getSize()
  220. SetCursorPos(1, sizeY)
  221. clearLine()
  222. SetColorWarning()
  223. SetCursorPos((sizeX - text:len() - 2) / 2, sizeY)
  224. Write(" " .. text .. " ")
  225. SetColorDefault()
  226. clearWarningTick = 5
  227. end
  228. function ClearWarning()
  229. if clearWarningTick > 0 then
  230. clearWarningTick = clearWarningTick - 1
  231. elseif clearWarningTick == 0 then
  232. SetColorDefault()
  233. local sizeX, sizeY = term.getSize()
  234. SetCursorPos(1, sizeY)
  235. clearLine()
  236. clearWarningTick = -1
  237. end
  238. end
  239.  
  240. ----------- Formatting & popups
  241.  
  242. function FormatFloat(value, nbchar)
  243. local str = "?"
  244. if value ~= nil then
  245. str = string.format("%g", value)
  246. end
  247. if nbchar ~= nil then
  248. str = string.sub(" " .. str, -nbchar)
  249. end
  250. return str
  251. end
  252. function FormatInteger(value, nbchar)
  253. local str = "?"
  254. if value ~= nil then
  255. str = string.format("%d", value)
  256. end
  257. if nbchar ~= nil then
  258. str = string.sub(" " .. str, -nbchar)
  259. end
  260. return str
  261. end
  262.  
  263. function boolToYesNo(bool)
  264. if bool then
  265. return "YES"
  266. else
  267. return "no"
  268. end
  269. end
  270.  
  271. function readInputNumber(currentValue)
  272. local inputAbort = false
  273. local input = string.format(currentValue)
  274. if input == "0" then
  275. input = ""
  276. end
  277. local x, y = term.getCursorPos()
  278. repeat
  279. ClearWarning()
  280. SetColorDefault()
  281. SetCursorPos(x, y)
  282. Write(input .. " ")
  283. input = string.sub(input, -9)
  284.  
  285. local params = { os.pullEventRaw() }
  286. local eventName = params[1]
  287. local address = params[2]
  288. if address == nil then address = "none" end
  289. if eventName == "key" then
  290. local keycode = params[2]
  291. if keycode >= 2 and keycode <= 10 then -- 1 to 9
  292. input = input .. string.format(keycode - 1)
  293. elseif keycode == 11 or keycode == 82 then -- 0 & keypad 0
  294. input = input .. "0"
  295. elseif keycode >= 79 and keycode <= 81 then -- keypad 1 to 3
  296. input = input .. string.format(keycode - 78)
  297. elseif keycode >= 75 and keycode <= 77 then -- keypad 4 to 6
  298. input = input .. string.format(keycode - 71)
  299. elseif keycode >= 71 and keycode <= 73 then -- keypad 7 to 9
  300. input = input .. string.format(keycode - 64)
  301. elseif keycode == 14 then -- Backspace
  302. input = string.sub(input, 1, string.len(input) - 1)
  303. elseif keycode == 211 then -- Delete
  304. input = ""
  305. elseif keycode == 28 then -- Enter
  306. inputAbort = true
  307. elseif keycode == 74 or keycode == 12 or keycode == 49 then -- - on numeric keypad or - on US top or n letter
  308. if string.sub(input, 1, 1) == "-" then
  309. input = string.sub(input, 2)
  310. else
  311. input = "-" .. input
  312. end
  313. elseif char == 43 or keycode == 78 then -- +
  314. if string.sub(input, 1, 1) == "-" then
  315. input = string.sub(input, 2)
  316. end
  317. else
  318. ShowWarning("Key " .. keycode .. " is invalid")
  319. end
  320. elseif eventName == "terminate" then
  321. inputAbort = true
  322. elseif not common_event(eventName, params[2]) then
  323. ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  324. end
  325. until inputAbort
  326. SetCursorPos(1, y + 1)
  327. if input == "" or input == "-" then
  328. return currentValue
  329. else
  330. return tonumber(input)
  331. end
  332. end
  333. function readInput(currentValue)
  334. local inputAbort = false
  335. local input = string.format(currentValue)
  336. local x, y = term.getCursorPos()
  337. Write(input)
  338. os.pullEventRaw() -- skip first char event
  339. repeat
  340. ClearWarning()
  341. SetColorDefault()
  342. SetCursorPos(x, y)
  343. Write(input .. " ")
  344. input = string.sub(input, -123)
  345.  
  346. local params = { os.pullEventRaw() }
  347. local eventName = params[1]
  348. local address = params[2]
  349. if address == nil then address = "none" end
  350. if eventName == "key" then
  351. local keycode = params[2]
  352. if keycode == 14 then -- Backspace
  353. input = string.sub(input, 1, string.len(input) - 1)
  354. elseif keycode == 211 then -- Delete
  355. input = ""
  356. elseif keycode == 28 then -- Enter
  357. inputAbort = true
  358. end
  359. elseif eventName == "char" then
  360. local char = params[2]
  361. if char >= ' ' and char <= '~' then -- 1 to 9
  362. input = input .. char
  363. end
  364. elseif eventName == "terminate" then
  365. inputAbort = true
  366. elseif eventName == "paste" then
  367. input = params[2]
  368. elseif not common_event(eventName, params[2]) then
  369. ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  370. end
  371. until inputAbort
  372. SetCursorPos(1, y + 1)
  373. if input == "" then
  374. return currentValue
  375. else
  376. return input
  377. end
  378. end
  379. function readInputText(currentValue)
  380. local inputAbort = false
  381. local input = string.format(currentValue)
  382. local x, y = term.getCursorPos()
  383. Write(input)
  384. os.pullEventRaw() -- skip first char event
  385. repeat
  386. ClearWarning()
  387. SetColorDefault()
  388. SetCursorPos(x, y)
  389. Write(input .. " ")
  390. input = string.sub(input, -30)
  391.  
  392. local params = { os.pullEventRaw() }
  393. local eventName = params[1]
  394. local address = params[2]
  395. if address == nil then address = "none" end
  396. if eventName == "key" then
  397. local keycode = params[2]
  398. if keycode == 14 then -- Backspace
  399. input = string.sub(input, 1, string.len(input) - 1)
  400. elseif keycode == 211 then -- Delete
  401. input = ""
  402. elseif keycode == 28 then -- Enter
  403. inputAbort = true
  404. else
  405. ShowWarning("Key " .. keycode .. " is invalid")
  406. end
  407. elseif eventName == "char" then
  408. local char = params[2]
  409. if char >= ' ' and char <= '~' then -- 1 to 9
  410. input = input .. char
  411. else
  412. ShowWarning("Char #" .. string.byte(char) .. " is invalid")
  413. end
  414. elseif eventName == "terminate" then
  415. inputAbort = true
  416. elseif not common_event(eventName, params[2]) then
  417. ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  418. end
  419. until inputAbort
  420. SetCursorPos(1, y + 1)
  421. if input == "" then
  422. return currentValue
  423. else
  424. return input
  425. end
  426. end
  427.  
  428. function readConfirmation(msg)
  429. if msg == nil then
  430. ShowWarning("Are you sure? (y/n)")
  431. else
  432. ShowWarning(msg)
  433. end
  434. repeat
  435. local params = { os.pullEventRaw() }
  436. local eventName = params[1]
  437. local address = params[2]
  438. if address == nil then address = "none" end
  439. if eventName == "key" then
  440. local keycode = params[2]
  441. if keycode == 21 then -- Y
  442. return true
  443. else
  444. return false
  445. end
  446. elseif eventName == "terminate" then
  447. return false
  448. elseif not common_event(eventName, params[2]) then
  449. ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  450. end
  451. until false
  452. end
  453.  
  454. ----------- commons: menu, event handlers, etc.
  455.  
  456. function common_event(eventName, param)
  457. if eventName == "redstone" then
  458. redstone_event(param)
  459. elseif eventName == "timer" then
  460. if param == radar_timerId then
  461. radar_timerEvent()
  462. end
  463. elseif eventName == "char" then
  464. elseif eventName == "key_up" then
  465. elseif eventName == "mouse_click" then
  466. elseif eventName == "mouse_up" then
  467. elseif eventName == "mouse_drag" then
  468. elseif eventName == "monitor_touch" then
  469. elseif eventName == "monitor_resize" then
  470. elseif eventName == "peripheral" then
  471. elseif eventName == "peripheral_detach" then
  472. else
  473. return false
  474. end
  475. return true
  476. end
  477.  
  478. function menu_common()
  479. SetCursorPos(1, 17)
  480. SetColorFooter()
  481. ShowMenu("0 Home 1 Shields 2 Webhook ")
  482. SetColorDefault()
  483. end
  484.  
  485. ----------- Redstone support
  486.  
  487. local tblRedstoneState = {-- Remember redstone state on each side
  488. ["top"] = rs.getInput("top"),
  489. ["front"] = rs.getInput("front"),
  490. ["left"] = rs.getInput("left"),
  491. ["right"] = rs.getInput("right"),
  492. ["back"] = rs.getInput("back"),
  493. ["bottom"] = rs.getInput("bottom"),
  494. }
  495. local tblSides = {-- list all sides and offset coordinates
  496. ["top" ] = { 3, 1},
  497. ["front" ] = { 1, 3},
  498. ["left" ] = { 3, 3},
  499. ["right" ] = { 5, 3},
  500. ["back" ] = { 5, 5},
  501. ["bottom"] = { 3, 5},
  502. }
  503.  
  504. function redstone_event()
  505. -- Event only returns nil so we need to check sides manually
  506. local message = ""
  507. for side, state in pairs(tblRedstoneState) do
  508. if rs.getInput(side) ~= state then
  509. -- print(side .. " is now " .. tostring(rs.getInput(side)))
  510. message = message .. side .. " "
  511. tblRedstoneState[side] = rs.getInput(side)
  512. end
  513. end
  514. if message ~= "" then
  515. message = "Redstone changed on " .. message
  516. showWarning(message)
  517. end
  518. end
  519.  
  520.  
  521. ----------- Shield support
  522.  
  523. forcefieldprojector_currentKey = 1
  524. function shield_key(char, keycode)
  525. if char == 83 or char == 115 or keycode == 31 then -- S
  526. projector_start()
  527. return true
  528. elseif char == 80 or char == 112 or keycode == 25 then -- P
  529. projector_stop()
  530. return true
  531. end
  532. return false
  533. end
  534.  
  535. function shield_page()
  536. SetCursorPos(0, 1)
  537. SetColorTitle()
  538. ShowTitle(label .. " - Shield Status")
  539. local forcefieldprojector = nil
  540. if forcefieldprojectors ~= nil then
  541. if forcefieldprojector_currentKey > #forcefieldprojectors then
  542. forcefieldprojector_currentKey = 1
  543. end
  544. forcefieldprojector = forcefieldprojectors[forcefieldprojector_currentKey]
  545. end
  546.  
  547. SetCursorPos(1, 2)
  548. if #forcefieldprojectors == 0 then
  549. SetColorDisabled()
  550. Write("No Force Field Projectors detected...")
  551. elseif forcefieldprojector == nil or forcefieldprojector.isInterfaced() == nil then
  552. SetColorWarning()
  553. Write("Force Field Projector " .. forcefieldprojector_currentKey .. " of " .. #forcefieldprojectors .. " is invalid")
  554. else
  555. SetColorDefault()
  556. Write("Force Field Projector " .. forcefieldprojector_currentKey .. " of " .. #forcefieldprojectors)
  557. local isAssemblyValid = forcefieldprojector.getAssemblyStatus()
  558. local energyStored, energyMax, energyUnits = forcefieldprojector.getEnergyStatus()
  559. local isEnabled = forcefieldprojector.enable()
  560. local tier = forcefieldprojector.getTier()
  561. -- local upgrades = forcefieldprojector.getUpgrades()
  562. local state = forcefieldprojector.getstate()
  563. if tier == 1 then
  564. tier1 = "Basic"
  565. tierpower = 103427
  566. elseif tier == 2 then
  567. tier1 = "Advanced"
  568. tierpower = 35284
  569. else
  570. tier1 = "Superior"
  571. tierpower = 610141
  572. end
  573. if not isAssemblyValid then
  574. SetColorWarning()
  575. SetCursorPos(1, 3)
  576. Write("Invalid assembly!")
  577. SetColorDefault()
  578. SetCursorPos(1, 4)
  579. print("Projector is missing a upgrade or shape.")
  580. else
  581. SetCursorPos(1, 4)
  582. Write("Assembly is valid")
  583.  
  584. if energyStored < tierpower then
  585. SetColorWarning()
  586. else
  587. SetColorDefault()
  588. end
  589. SetCursorPos(1, 6)
  590. Write("Energy level is " .. energyStored .. " " .. energyUnits)
  591. SetCursorPos(1, 7)
  592. Write("Tier is " .. tier1)
  593. Write("State is " .. state)
  594. SetCursorPos(1, 8)
  595. -- Write("Upgrades ")
  596. SetCursorPos(1, 9)
  597. -- Write(" "..(upgrades and 'true' or 'false'),0,1,2,3)
  598. SetCursorPos(1, 11)
  599. if isEnabled then
  600. if energyStored <= 500 then
  601. SetColorWarning()
  602. else
  603. SetColorSuccess()
  604. end
  605. Write("Projector is enabled")
  606. else
  607. SetColorWarning()
  608. Write("Projector is disabled")
  609. end
  610. end
  611. end
  612. os.sleep(0.1)
  613. forcefieldprojector_currentKey = forcefieldprojector_currentKey + 1
  614. SetColorDefault()
  615. SetCursorPos(1, 12)
  616. SetCursorPos(1, 13)
  617. SetColorFooter()
  618. SetCursorPos(1, 16)
  619. ShowMenu(" (S)tart All Projectors - sto(P) All Projector - s(T)art Projector - st(O)p Projector")
  620. SetColorDefault()
  621. end
  622.  
  623. function projector_start()
  624. for key,forcefieldprojector in pairs(forcefieldprojectors) do
  625. forcefieldprojector.enable(false)
  626. forcefieldprojector.enable(true)
  627. end
  628. end
  629.  
  630. function projector_stop()
  631. for key,forcefieldprojector in pairs(forcefieldprojectors) do
  632. forcefieldprojector.enable(false)
  633. end
  634. end
  635.  
  636. ----------- Configuration
  637.  
  638. function data_save()
  639. local file = fs.open(".shield", "w")
  640. if file ~= nil then
  641. file.writeLine(textutils.serialize(data))
  642. file.close()
  643. else
  644. ShowWarning("No file system")
  645. os.sleep(3)
  646. end
  647. end
  648.  
  649. function data_read()
  650. data = { }
  651. if fs.exists(".shield") then
  652. local file = fs.open(".shield", "r")
  653. data = textutils.unserialize(file.readAll())
  654. file.close()
  655. if data == nil then data = {}; end
  656. end
  657. if data.shield_hook == nil then data.auto_hook = 0; end
  658. end
  659.  
  660. function data_setName()
  661. if ship ~= nil then
  662. ShowTitle("<==== Set Shield name ====>")
  663. else
  664. ShowTitle("<==== Set name ====>")
  665. end
  666.  
  667. SetCursorPos(1, 2)
  668. Write("Enter ship name: ")
  669. label = readInputText(label)
  670. os.setComputerLabel(label)
  671. if ship ~= nil then
  672. ship.coreFrequency(label)
  673. end
  674. os.reboot()
  675. end
  676.  
  677. function string_split(source, sep)
  678. local sep = sep or ":"
  679. local fields = {}
  680. local pattern = string.format("([^%s]+)", sep)
  681. source:gsub(pattern, function(c) fields[#fields + 1] = c end)
  682. return fields
  683. end
  684.  
  685. function hooks_page_config()
  686. ShowTitle(label .. " - Webhook configuration")
  687.  
  688. SetCursorPos(1, 2)
  689. SetColorDefault()
  690. SetCursorPos(1, 3)
  691. Write("Ship Shield Webhook: ")
  692. hook_shieldset(readInput(data.shield_hook))
  693. data_save()
  694. end
  695. function hook_shieldset(newShield)
  696. data.shield_hook = newShield
  697. end
  698.  
  699. ----------- Boot sequence
  700. math.randomseed(os.time())
  701. label = os.getComputerLabel()
  702. if not label then
  703. label = "" .. os.getComputerID()
  704. end
  705.  
  706. -- read configuration
  707. data_read()
  708. clear()
  709. print("data_read...")
  710.  
  711. -- initial scanning
  712. monitors = {}
  713. ShowTitle(label .. " - Connecting...")
  714. WriteLn("")
  715.  
  716. sides = peripheral.getNames()
  717. forcefieldprojectors = {}
  718. warpdrivetransportercores = {}
  719. for key,side in pairs(sides) do
  720. os.sleep(0)
  721. Write("Checking " .. side .. " ")
  722. local componentType = peripheral.getType(side)
  723. Write(componentType .. " ")
  724. if componentType == "warpdriveForceFieldProjector" then
  725. Write("wrapping!")
  726. table.insert(forcefieldprojectors, peripheral.wrap(side))
  727. elseif componentType == "monitor" then
  728. Write("wrapping!")
  729. lmonitor = peripheral.wrap(side)
  730. table.insert(monitors, lmonitor)
  731. lmonitor.setTextScale(monitor_textScale)
  732. end
  733. WriteLn("")
  734. end
  735.  
  736. if not os.getComputerLabel() and (ship ~= nil or reactor ~= nil) then
  737. data_setName()
  738. end
  739.  
  740. -- peripherals status
  741. function connections_page()
  742. SetCursorPos(0, 1)
  743. SetColorTitle()
  744. ShowTitle(label .. " - Connections")
  745.  
  746. WriteLn("")
  747. if #monitors == 0 then
  748. SetColorWarning()
  749. WriteLn("No Monitor detected")
  750. elseif #monitors == 1 then
  751. SetColorSuccess()
  752. WriteLn("1 monitor detected")
  753. else
  754. SetColorSuccess()
  755. WriteLn(#monitors .. " Monitors detected")
  756. end
  757. if #forcefieldprojectors == 0 then
  758. SetColorWarning()
  759. WriteLn("No Force Field Pojectors detected")
  760. elseif #forcefieldprojectors == 1 then
  761. SetColorSuccess()
  762. WriteLn(#"1 Force Field Projector detected")
  763. else
  764. SetColorSuccess()
  765. WriteLn(#forcefieldprojectors .. " Force Field Projectors detected")
  766. end
  767.  
  768. WriteLn("")
  769. SetColorDefault()
  770. WriteLn("Please refer to below menu for keyboard controls")
  771. WriteLn("For example, press 1 to access Ship page")
  772. end
  773.  
  774. -- peripheral boot up
  775. clear()
  776. connections_page()
  777. SetColorDefault()
  778. WriteLn("")
  779. os.sleep(2)
  780.  
  781. -- main loop
  782. abort = false
  783. refresh = true
  784. page = shield_page
  785. keyHandler = shield_key
  786. repeat
  787. ClearWarning()
  788. if refresh then
  789. clear()
  790. page()
  791. menu_common()
  792. refresh = false
  793. end
  794. params = { os.pullEventRaw() }
  795. eventName = params[1]
  796. address = params[2]
  797. if address == nil then address = "none" end
  798. -- WriteLn("...")
  799. -- WriteLn("Event '" .. eventName .. "', " .. address .. ", " .. params[3] .. ", " .. params[4] .. " received")
  800. -- os.sleep(0.2)
  801. if eventName == "key" then
  802. keycode = params[2]
  803. if char == 88 or char == 120 or keycode == 45 then -- x for eXit
  804. os.pullEventRaw()
  805. abort = true
  806. elseif char == 48 or keycode == 11 or keycode == 82 then -- 0
  807. page = connections_page
  808. keyHandler = nil
  809. refresh = true
  810. elseif char == 49 or keycode == 2 or keycode == 79 then -- 1
  811. page = shield_page
  812. keyHandler = shield_key
  813. refresh = true
  814. elseif char == 50 or keycode == 3 or keycode == 80 then -- 2
  815. page = hooks_page_config
  816. keyHandler = hooks_page_key
  817. refresh = true
  818. elseif keyHandler ~= nil and keyHandler(char, keycode) then
  819. refresh = true
  820. os.sleep(0)
  821. elseif char == 0 then -- control chars
  822. refresh = false
  823. os.sleep(0)
  824. else
  825. ShowWarning("Key " .. keycode .. " is invalid")
  826. os.sleep(0.2)
  827. end
  828. elseif eventName == "reactorPulse" then
  829. reactor_pulse(params[2])
  830. refresh = (page == reactor_page)
  831. elseif eventName == "terminate" then
  832. abort = true
  833. elseif not common_event(eventName, params[2]) then
  834. ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  835. refresh = true
  836. os.sleep(0.2)
  837. end
  838. until abort
  839.  
  840. -- exiting
  841. if data.core_summon then
  842. data.core_summon = false
  843. data_save()
  844. end
  845.  
  846. if ship ~= nil then
  847. ship.enable()
  848. end
  849.  
  850. -- clear screens on exit
  851. SetMonitorColorFrontBack(colors.white, colors.black)
  852. term.clear()
  853. if monitors ~= nil then
  854. for key,monitor in pairs(monitors) do
  855. monitor.clear()
  856. end
  857. end
  858. SetCursorPos(1, 1)
  859. WriteLn("Program terminated")
  860. WriteLn("Type startup to restart it")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement