Advertisement
NTins

NOT YOUR BUISNESS

May 4th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.82 KB | None | 0 0
  1. local component = require("component")
  2. local computer = require("computer")
  3. local term = require("term")
  4. local event = require("event")
  5. local fs = require("filesystem")
  6. local serialization = require("serialization")
  7.  
  8. if not term.isAvailable() then
  9. computer.beep()
  10. return
  11. end
  12.  
  13. Style = {
  14. CDefault = 0xFFFFFF,
  15. BGDefault = 0x0000FF,
  16.  
  17. CTitle = 0x000000,
  18. BGTitle = 0x00FFFF,
  19.  
  20. CWarning = 0xFFFFFF,
  21. BGWarning = 0xFF0000,
  22.  
  23. CSuccess = 0xFFFFFF,
  24. BGSuccess = 0x32CD32,
  25.  
  26. CDisabled = 0x808080,
  27. BGDisabled = 0x0000FF
  28. }
  29.  
  30. ----------- Monitor support
  31.  
  32. -- cache colors to reduce GPU load
  33. local gpu_frontColor = 0xFFFFFF
  34. local gpu_backgroundColor = 0x000000
  35. function SetMonitorColorFrontBack(frontColor, backgroundColor)
  36. if gpu_frontColor ~= frontColor then
  37. gpu_frontColor = frontColor
  38. component.gpu.setForeground(gpu_frontColor)
  39. end
  40. if gpu_backgroundColor ~= backgroundColor then
  41. gpu_backgroundColor = backgroundColor
  42. component.gpu.setBackground(gpu_backgroundColor)
  43. end
  44. end
  45.  
  46. function Write(text)
  47. if term.isAvailable() then
  48. local w, h = component.gpu.getResolution()
  49. if w then
  50. local xt, yt = term.getCursor()
  51. component.gpu.set(xt, yt, text)
  52. SetCursorPos(xt + #text, yt)
  53. end
  54. end
  55. end
  56.  
  57. function SetCursorPos(x, y)
  58. if term.isAvailable() then
  59. term.setCursor(x, y)
  60. end
  61. end
  62.  
  63. function SetColorDefault()
  64. SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
  65. end
  66.  
  67. function SetColorTitle()
  68. SetMonitorColorFrontBack(Style.CTitle, Style.BGTitle)
  69. end
  70.  
  71. function SetColorWarning()
  72. SetMonitorColorFrontBack(Style.CWarning, Style.BGWarning)
  73. end
  74.  
  75. function SetColorSuccess()
  76. SetMonitorColorFrontBack(Style.CSuccess, Style.BGSuccess)
  77. end
  78.  
  79. function SetColorDisabled()
  80. SetMonitorColorFrontBack(Style.CDisabled, Style.BGDisabled)
  81. end
  82.  
  83. function Clear()
  84. clearWarningTick = -1
  85. SetColorDefault()
  86. term.clear()
  87. SetCursorPos(1, 1)
  88. end
  89.  
  90. function ClearLine()
  91. SetColorDefault()
  92. term.clearLine()
  93. SetCursorPos(1, 1)
  94. end
  95.  
  96. function WriteLn(text)
  97. if term.isAvailable() then
  98. Write(text)
  99. local x, y = term.getCursor()
  100. local width, height = component.gpu.getResolution()
  101. if y > height - 1 then
  102. y = 1
  103. end
  104. SetCursorPos(1, y + 1)
  105. end
  106. end
  107.  
  108. function WriteCentered(y, text)
  109. if term.isAvailable() then
  110. local sizeX, sizeY = component.gpu.getResolution()
  111. if sizeX then
  112. component.gpu.set((sizeX - text:len()) / 2, y, text)
  113. end
  114. local xt, yt = term.getCursor()
  115. SetCursorPos(1, yt + 1)
  116. end
  117. end
  118.  
  119. function ShowTitle(text)
  120. Clear()
  121. SetColorTitle()
  122. WriteCentered(1, text)
  123. SetColorDefault()
  124. end
  125.  
  126. function ShowMenu(text)
  127. if term.isAvailable() then
  128. Write(text)
  129. local sizeX, sizeY = component.gpu.getResolution()
  130. local xt, yt = term.getCursor()
  131. for i = xt, sizeX do
  132. Write(" ")
  133. end
  134. SetCursorPos(1, yt + 1)
  135. end
  136. end
  137.  
  138. local clearWarningTick = -1
  139. function ShowWarning(text)
  140. if term.isAvailable() then
  141. local sizeX, sizeY = component.gpu.getResolution()
  142. SetColorWarning()
  143. SetCursorPos((sizeX - text:len() - 2) / 2, sizeY)
  144. Write(" " .. text .. " ")
  145. SetColorDefault()
  146. clearWarningTick = 5
  147. end
  148. end
  149. function ClearWarning()
  150. if clearWarningTick > 0 then
  151. clearWarningTick = clearWarningTick - 1
  152. elseif clearWarningTick == 0 then
  153. if term.isAvailable() then
  154. SetColorDefault()
  155. local sizeX, sizeY = component.gpu.getResolution()
  156. SetCursorPos(1, sizeY)
  157. ClearLine()
  158. clearWarningTick = -1
  159. end
  160. end
  161. end
  162.  
  163. ----------- Formatting & popups
  164.  
  165. function FormatFloat(value, nbchar)
  166. local str = "?"
  167. if value ~= nil then
  168. str = string.format("%g", value)
  169. end
  170. if nbchar ~= nil then
  171. str = string.sub(" " .. str, -nbchar)
  172. end
  173. return str
  174. end
  175. function FormatInteger(value, nbchar)
  176. local str = "?"
  177. if value ~= nil then
  178. str = string.format("%d", value)
  179. end
  180. if nbchar ~= nil then
  181. str = string.sub(" " .. str, -nbchar)
  182. end
  183. return str
  184. end
  185.  
  186. function boolToYesNo(bool)
  187. if bool then
  188. return "YES"
  189. else
  190. return "no"
  191. end
  192. end
  193.  
  194. function readInputNumber(currentValue)
  195. local inputAbort = false
  196. local input = string.format(currentValue)
  197. if input == "0" then
  198. input = ""
  199. end
  200. local x, y = term.getCursor()
  201. repeat
  202. ClearWarning()
  203. SetColorDefault()
  204. SetCursorPos(x, y)
  205. Write(input .. " ")
  206. input = string.sub(input, -9)
  207.  
  208. local params = { event.pull() }
  209. local eventName = params[1]
  210. local address = params[2]
  211. if address == nil then address = "none" end
  212. if eventName == "key_down" then
  213. local char = params[3]
  214. local keycode = params[4]
  215. if char >= 49 and char <= 57 then -- 1 to 9
  216. input = input .. string.format(char - 48)
  217. elseif keycode >= 2 and keycode <= 10 then -- 1 to 9
  218. input = input .. string.format(keycode - 1)
  219. elseif char == 48 or keycode == 11 then -- 0
  220. input = input .. "0"
  221. elseif char == 45 or char == 78 or char == 110
  222. or keycode == 74 or keycode == 12 or keycode == 49 then -- - on numeric keypad or - on US top or n letter
  223. if string.sub(input, 1, 1) == "-" then
  224. input = string.sub(input, 2)
  225. else
  226. input = "-" .. input
  227. end
  228. elseif char == 43 or keycode == 78 then -- +
  229. if string.sub(input, 1, 1) == "-" then
  230. input = string.sub(input, 2)
  231. end
  232. elseif char == 8 then -- Backspace
  233. input = string.sub(input, 1, string.len(input) - 1)
  234. elseif char == 0 and keycode == 211 then -- Delete
  235. input = ""
  236. elseif char == 13 then -- Enter
  237. inputAbort = true
  238. elseif char ~= 0 then
  239. ShowWarning("Key " .. char .. " " .. keycode .. " is invalid")
  240. end
  241. elseif eventName == "key_up" then
  242. -- drop it
  243. elseif eventName == "touch" then
  244. -- drop it
  245. elseif eventName == "drop" then
  246. -- drop it
  247. elseif eventName == "drag" then
  248. -- drop it
  249. elseif eventName == "interrupted" then
  250. inputAbort = true
  251. elseif not common_event(eventName, params[3]) then
  252. ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  253. end
  254. until inputAbort
  255. SetCursorPos(1, y + 1)
  256. if input == "" or input == "-" then
  257. return currentValue
  258. else
  259. return tonumber(input)
  260. end
  261. end
  262.  
  263. function readInputText(currentValue)
  264. local inputAbort = false
  265. local input = string.format(currentValue)
  266. local x, y = term.getCursor()
  267. repeat
  268. ClearWarning()
  269. SetColorDefault()
  270. SetCursorPos(x, y)
  271. Write(input .. " ")
  272. input = string.sub(input, -30)
  273.  
  274. local params = { event.pull() }
  275. local eventName = params[1]
  276. local address = params[2]
  277. if address == nil then address = "none" end
  278. if eventName == "key_down" then
  279. local char = params[3]
  280. local keycode = params[4]
  281. if char >= 32 and char <= 127 then -- any ASCII table minus controls and DEL
  282. input = input .. string.char(char)
  283. elseif char == 8 then -- Backspace
  284. input = string.sub(input, 1, string.len(input) - 1)
  285. elseif char == 0 and keycode == 211 then -- Delete
  286. input = ""
  287. elseif char == 13 then -- Enter
  288. inputAbort = true
  289. elseif char ~= 0 then
  290. ShowWarning("Key " .. char .. " " .. keycode .. " is invalid")
  291. end
  292. elseif eventName == "key_up" then
  293. -- drop it
  294. elseif eventName == "touch" then
  295. -- drop it
  296. elseif eventName == "drop" then
  297. -- drop it
  298. elseif eventName == "drag" then
  299. -- drop it
  300. elseif eventName == "interrupted" then
  301. inputAbort = true
  302. elseif not common_event(eventName, params[3]) then
  303. ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  304. end
  305. until inputAbort
  306. SetCursorPos(1, y + 1)
  307. if input == "" then
  308. return currentValue
  309. else
  310. return input
  311. end
  312. end
  313.  
  314. function readConfirmation()
  315. ShowWarning("Are you sure? (y/n)")
  316. repeat
  317. local params = { event.pull() }
  318. local eventName = params[1]
  319. local address = params[3]
  320. if address == nil then address = "none" end
  321. if eventName == "key_down" then
  322. local char = params[3]
  323. if char == 89 or char == 121 or keycode == 21 then -- Y
  324. return true
  325. else
  326. return false
  327. end
  328. elseif eventName == "key_up" then
  329. -- drop it
  330. elseif eventName == "touch" then
  331. -- drop it
  332. elseif eventName == "drop" then
  333. -- drop it
  334. elseif eventName == "drag" then
  335. -- drop it
  336. elseif eventName == "interrupted" then
  337. return false
  338. elseif not common_event(eventName, params[3]) then
  339. ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  340. end
  341. until false
  342. end
  343.  
  344. ----------- commons: menu, event handlers, etc.
  345.  
  346. function common_event(eventName, param)
  347. if eventName == "redstone" then
  348. -- redstone_event(param)
  349. elseif eventName == "timer" then
  350. elseif eventName == "shipCoreCooldownDone" then
  351. ShowWarning("Ship core cooldown done")
  352. elseif eventName == "component_added" then
  353. -- ShowWarning("Event '" .. eventName .. "', " .. param .. " is unsupported")
  354. elseif eventName == "component_removed" then
  355. -- ShowWarning("Event '" .. eventName .. "', " .. param .. " is unsupported")
  356. else
  357. return false
  358. end
  359. return true
  360. end
  361.  
  362. function menu_common()
  363. SetCursorPos(1, 23)
  364. SetColorTitle()
  365. ShowMenu("0 Connections, 1 Ship core, X Exit")
  366. end
  367.  
  368. ----------- Configuration
  369.  
  370. function data_save()
  371. local file = fs.open("shipdata.txt", "w")
  372. if file ~= nil then
  373. file:write(serialization.serialize(data))
  374. file:close()
  375. else
  376. ShowWarning("No file system")
  377. end
  378. end
  379.  
  380. function data_read()
  381. data = { }
  382. if fs.exists("shipdata.txt") then
  383. local file = fs.open("shipdata.txt", "r")
  384. local size = fs.size("shipdata.txt")
  385. local rawData = file:read(size)
  386. if rawData ~= nil then
  387. data = serialization.unserialize(rawData)
  388. end
  389. file:close()
  390. end
  391. if data.core_summon == nil then data.core_summon = false; end
  392. end
  393.  
  394. function data_setName()
  395. ShowTitle("<==== Set name ====>")
  396.  
  397. SetCursorPos(1, 2)
  398. Write("Enter ship name: ")
  399. label = readInputText(label)
  400. -- FIXME os.setComputerLabel(label)
  401. if ship ~= nil then
  402. ship.coreFrequency(label)
  403. end
  404. -- FIXME computer.shutdown(true)
  405. end
  406.  
  407. ----------- Ship support
  408.  
  409. core_front = 0
  410. core_right = 0
  411. core_up = 0
  412. core_back = 0
  413. core_left = 0
  414. core_down = 0
  415. core_isInHyper = false
  416. core_jumpCost = 0
  417. core_shipSize = 0
  418. core_movement = { 0, 0, 0 }
  419. core_rotationSteps = 0
  420.  
  421. function core_boot()
  422. if ship == nil then
  423. return
  424. end
  425.  
  426. Write("Booting Ship Core")
  427.  
  428. if data.core_summon then
  429. ship.summon_all()
  430. end
  431.  
  432. WriteLn("...")
  433. core_front, core_right, core_up = ship.dim_positive()
  434. core_back, core_left, core_down = ship.dim_negative()
  435. core_isInHyper = ship.isInHyperspace()
  436. core_rotationSteps = ship.rotationSteps()
  437. core_movement = { ship.movement() }
  438. if ship.direction ~= nil then
  439. ship.direction(666)
  440. ship.distance(0)
  441. end
  442. WriteLn("Ship core detected...")
  443.  
  444. repeat
  445. pos = ship.position()
  446. os.sleep(0.3)
  447. until pos ~= nil
  448. X, Y, Z = ship.position()
  449. WriteLn("Ship position triangulated...")
  450.  
  451. repeat
  452. isAttached = ship.isAttached()
  453. os.sleep(0.3)
  454. until isAttached ~= false
  455. WriteLn("Ship core linked...")
  456.  
  457. repeat
  458. core_shipSize = ship.getShipSize()
  459. os.sleep(0.3)
  460. until core_shipSize ~= nil
  461. WriteLn("Ship size updated...")
  462.  
  463. ship.mode(1)
  464. end
  465.  
  466. function core_writeMovement()
  467. local message = " Movement = "
  468. local count = 0
  469. if core_movement[1] > 0 then
  470. message = message .. core_movement[1] .. " front"
  471. count = count + 1
  472. elseif core_movement[1] < 0 then
  473. message = message .. (- core_movement[1]) .. " back"
  474. count = count + 1
  475. end
  476. if core_movement[2] > 0 then
  477. if count > 0 then message = message .. ", "; end
  478. message = message .. core_movement[2] .. " up"
  479. count = count + 1
  480. elseif core_movement[2] < 0 then
  481. if count > 0 then message = message .. ", "; end
  482. message = message .. (- core_movement[2]) .. " down"
  483. count = count + 1
  484. end
  485. if core_movement[3] > 0 then
  486. if count > 0 then message = message .. ", "; end
  487. message = message .. core_movement[3] .. " right"
  488. count = count + 1
  489. elseif core_movement[3] < 0 then
  490. if count > 0 then message = message .. ", "; end
  491. message = message .. (- core_movement[3]) .. " left"
  492. count = count + 1
  493. end
  494.  
  495. if core_rotationSteps == 1 then
  496. if count > 0 then message = message .. ", "; end
  497. message = message .. "Turn right"
  498. count = count + 1
  499. elseif core_rotationSteps == 2 then
  500. if count > 0 then message = message .. ", "; end
  501. message = message .. "Turn back"
  502. count = count + 1
  503. elseif core_rotationSteps == 3 then
  504. if count > 0 then message = message .. ", "; end
  505. message = message .. "Turn left"
  506. count = count + 1
  507. end
  508.  
  509. if count == 0 then
  510. message = message .. "(none)"
  511. end
  512. WriteLn(message)
  513. end
  514.  
  515. function core_writeRotation()
  516. if core_rotationSteps == 0 then
  517. WriteLn(" Rotation = Front")
  518. elseif core_rotationSteps == 1 then
  519. WriteLn(" Rotation = Right +90")
  520. elseif core_rotationSteps == 2 then
  521. WriteLn(" Rotation = Back 180")
  522. elseif core_rotationSteps == 3 then
  523. WriteLn(" Rotation = Left -90")
  524. end
  525. end
  526.  
  527. function core_computeNewCoordinates(cx, cy, cz)
  528. local res = { x = cx, y = cy, z = cz }
  529. local dx, dy, dz = ship.getOrientation()
  530. local worldMovement = { x = 0, y = 0, z = 0 }
  531. worldMovement.x = dx * core_movement[1] - dz * core_movement[3]
  532. worldMovement.y = core_movement[2]
  533. worldMovement.z = dz * core_movement[1] + dx * core_movement[3]
  534. core_actualDistance = math.ceil(math.sqrt(worldMovement.x * worldMovement.x + worldMovement.y * worldMovement.y + worldMovement.z * worldMovement.z))
  535. core_jumpCost = ship.getEnergyRequired(core_actualDistance)
  536. res.x = res.x + worldMovement.x
  537. res.y = res.y + worldMovement.y
  538. res.z = res.z + worldMovement.z
  539. return res
  540. end
  541.  
  542. function core_warp()
  543. -- rs.setOutput(alarm_side, true)
  544. if readConfirmation() then
  545. -- rs.setOutput(alarm_side, false)
  546. ship.movement(core_movement[1], core_movement[2], core_movement[3])
  547. ship.rotationSteps(core_rotationSteps)
  548. ship.mode(1)
  549. ship.jump()
  550. -- ship = nil
  551. end
  552. -- rs.setOutput(alarm_side, false)
  553. end
  554.  
  555. function core_page_setMovement()
  556. ShowTitle("<==== Set movement ====>")
  557. SetCursorPos(1, 20)
  558. SetColorTitle()
  559. ShowMenu("Enter 0 to keep position on that axis")
  560. ShowMenu("Use - or n keys to move in opposite direction")
  561. ShowMenu("Press Enter to confirm")
  562. SetColorDefault()
  563. SetCursorPos(1, 3)
  564.  
  565. core_movement[1] = core_page_setDistanceAxis(2, "Front", core_movement[1], math.abs(core_front + core_back + 1))
  566. core_movement[2] = core_page_setDistanceAxis(3, "Up" , core_movement[2], math.abs(core_up + core_down + 1))
  567. core_movement[3] = core_page_setDistanceAxis(4, "Right", core_movement[3], math.abs(core_left + core_right + 1))
  568. core_movement = { ship.movement(core_movement[1], core_movement[2], core_movement[3]) }
  569. end
  570.  
  571. function core_page_setDistanceAxis(line, axis, userEntry, shipLength)
  572. local maximumDistance = shipLength + 127
  573. if core_isInHyper and line ~= 3 then
  574. maximumDistance = shipLength + 127 * 100
  575. end
  576. repeat
  577. SetCursorPos(1, line)
  578. Write(axis .. " (min " .. (shipLength + 1) .. ", max " .. maximumDistance .. "): ")
  579. userEntry = readInputNumber(userEntry)
  580. if userEntry == 0 then
  581. return userEntry
  582. end
  583. if math.abs(userEntry) <= shipLength or math.abs(userEntry) > maximumDistance then
  584. ShowWarning("Wrong distance. Try again.")
  585. end
  586. until math.abs(userEntry) > shipLength and math.abs(userEntry) <= maximumDistance
  587.  
  588. return userEntry
  589. end
  590.  
  591. function core_page_setRotation()
  592. local inputAbort = false
  593. local drun = true
  594. repeat
  595. ShowTitle("<==== Set rotation ====>")
  596. core_writeRotation()
  597. SetCursorPos(1, 21)
  598. SetColorTitle()
  599. ShowMenu("Use directional keys")
  600. ShowMenu("Press Enter to confirm")
  601. SetColorDefault()
  602. local params = { event.pull() }
  603. local eventName = params[1]
  604. local address = params[2]
  605. if address == nil then address = "none" end
  606. if eventName == "key_down" then
  607. local char = params[3]
  608. local keycode = params[4]
  609. if keycode == 200 then
  610. core_rotationSteps = 0
  611. elseif keycode == 203 then
  612. core_rotationSteps = 3
  613. elseif keycode == 205 then
  614. core_rotationSteps = 1
  615. elseif keycode == 208 then
  616. core_rotationSteps = 2
  617. elseif keycode == 28 then
  618. inputAbort = true
  619. else
  620. ShowWarning("Key " .. char .. " " .. keycode .. " is invalid")
  621. end
  622. elseif eventName == "key_up" then
  623. -- drop it
  624. elseif eventName == "touch" then
  625. -- drop it
  626. elseif eventName == "drop" then
  627. -- drop it
  628. elseif eventName == "drag" then
  629. -- drop it
  630. elseif eventName == "interrupted" then
  631. inputAbort = true
  632. elseif not common_event(eventName, params[3]) then
  633. ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  634. end
  635. until inputAbort
  636. core_rotationSteps = ship.rotationSteps(core_rotationSteps)
  637. end
  638.  
  639. function core_page_setDimensions()
  640. ShowTitle("<==== Set dimensions ====>")
  641. Write(" Front (".. core_front ..") : ")
  642. core_front = readInputNumber(core_front)
  643. Write(" Right (".. core_right ..") : ")
  644. core_right = readInputNumber(core_right)
  645. Write(" Up (".. core_up ..") : ")
  646. core_up = readInputNumber(core_up)
  647. Write(" Back (".. core_back ..") : ")
  648. core_back = readInputNumber(core_back)
  649. Write(" Left (".. core_left ..") : ")
  650. core_left = readInputNumber(core_left)
  651. Write(" Down (".. core_down ..") : ")
  652. core_down = readInputNumber(core_down)
  653. Write("Setting dimensions...")
  654. core_front, core_right, core_up = ship.dim_positive(core_front, core_right, core_up)
  655. core_back, core_left, core_down = ship.dim_negative(core_back, core_left, core_down)
  656. core_shipSize = ship.getShipSize()
  657. if core_shipSize == nil then core_shipSize = 0 end
  658. end
  659.  
  660. function core_page_summon()
  661. ShowTitle("<==== Summon players ====>")
  662. local playersString, playersArray = ship.getAttachedPlayers()
  663. if #playersArray == 0 then
  664. WriteLn("~ no players registered ~")
  665. WriteLn("")
  666. SetColorTitle()
  667. ShowMenu("Press enter to exit")
  668. SetColorDefault()
  669. readInputNumber("")
  670. return
  671. end
  672.  
  673. for i = 1, #playersArray do
  674. WriteLn(i .. ". " .. playersArray[i])
  675. end
  676. SetColorTitle()
  677. ShowMenu("Enter player number")
  678. ShowMenu("or press enter to summon everyone")
  679. SetColorDefault()
  680.  
  681. Write(":")
  682. local input = readInputNumber("")
  683. if input == "" then
  684. ship.summon_all()
  685. else
  686. input = tonumber(input)
  687. ship.summon(input - 1)
  688. end
  689. end
  690.  
  691. function core_page_jumpToBeacon()
  692. ShowTitle("<==== Jump to beacon ====>")
  693.  
  694. Write("Enter beacon frequency: ")
  695. local freq = readInputText("")
  696. -- rs.setOutput(alarm_side, true)
  697. if readConfirmation() then
  698. -- rs.setOutput(alarm_side, false)
  699. ship.mode(4)
  700. ship.beaconFrequency(freq)
  701. ship.jump()
  702. -- ship = nil
  703. end
  704. -- rs.setOutput(alarm_side, false)
  705. end
  706.  
  707. function core_page_jumpToGate()
  708. ShowTitle("<==== Jump to Jumpgate ====>")
  709.  
  710. Write("Enter jumpgate name: ")
  711. local name = readInputText("")
  712. -- rs.setOutput(alarm_side, true)
  713. if readConfirmation() then
  714. -- rs.setOutput(alarm_side, false)
  715. ship.mode(6)
  716. ship.targetJumpgate(name)
  717. ship.jump()
  718. -- ship = nil
  719. end
  720. -- rs.setOutput(alarm_side, false)
  721. end
  722.  
  723. function core_page()
  724. ShowTitle(label .. " - Ship status")
  725. if ship ~= nil then
  726. WriteLn("")
  727. X, Y, Z = ship.position()
  728. WriteLn("Core:")
  729. WriteLn(" x, y, z = " .. "NOT YOUR BUISNESS")
  730. local energy, energyMax = ship.energy()
  731. if energy == nil then energy = 0 end
  732. if energyMax == nil then energyMax = 1 end
  733. WriteLn(" Energy = " .. math.floor(energy / energyMax * 100) .. " % (" .. energy .. "EU)")
  734. local playersString, playersArray = ship.getAttachedPlayers()
  735. if playersString == "" then players = "-" end
  736. WriteLn(" Attached players = " .. playersString)
  737. WriteLn("")
  738. WriteLn("Dimensions:")
  739. WriteLn(" Front, Right, Up = " .. FormatInteger(core_front) .. ", " .. FormatInteger(core_right) .. ", " .. FormatInteger(core_up))
  740. WriteLn(" Back, Left, Down = " .. FormatInteger(core_back) .. ", " .. FormatInteger(core_left) .. ", " .. FormatInteger(core_down))
  741. WriteLn(" Size = " .. core_shipSize .. " blocks")
  742. WriteLn("")
  743. WriteLn("Warp data:")
  744. core_writeMovement()
  745. local dest = core_computeNewCoordinates(X, Y, Z)
  746. WriteLn(" Distance = " .. core_actualDistance .. " (" .. core_jumpCost .. "EU, " .. math.floor(energy / core_jumpCost) .. " jumps)")
  747. WriteLn(" Dest.coordinates = " .. "he" .. ", " .. FormatInteger(dest.y) .. ", " .. "he")
  748. if data.core_summon then
  749. WriteLn(" Summon after = Yes")
  750. else
  751. WriteLn(" Summon after = No")
  752. end
  753. else
  754. ShowWarning("No ship controller detected")
  755. end
  756.  
  757. SetCursorPos(1, 20)
  758. SetColorTitle()
  759. ShowMenu("D - Dimensions, N - set ship Name, M - set Movement")
  760. ShowMenu("J - Jump, G - jump through Gate, B - jump to Beacon")
  761. ShowMenu("H - Hyperspace, C - summon Crew, T - Toggle summon")
  762. end
  763.  
  764. function core_key(char, keycode)
  765. if char == 77 or char == 109 then -- M
  766. core_page_setMovement()
  767. core_page_setRotation()
  768. data_save()
  769. return true
  770. elseif char == 84 or char == 116 then -- T
  771. if data.core_summon then
  772. data.core_summon = false
  773. else
  774. data.core_summon = true
  775. end
  776. data_save()
  777. return true
  778. elseif char == 68 or char == 100 then -- D
  779. core_page_setDimensions()
  780. data_save()
  781. return true
  782. elseif char == 74 or char == 106 then -- J
  783. core_warp()
  784. return true
  785. elseif char == 67 or char == 99 then -- C
  786. core_page_summon()
  787. return true
  788. elseif char == 66 or char == 98 then -- B
  789. core_page_jumpToBeacon()
  790. return true
  791. elseif char == 71 or char == 103 then -- G
  792. core_page_jumpToGate()
  793. return true
  794. elseif char == 72 or char == 104 then -- H
  795. -- rs.setOutput(alarm_side, true)
  796. if readConfirmation() then
  797. -- rs.setOutput(alarm_side, false)
  798. ship.mode(5)
  799. ship.jump()
  800. -- ship = nil
  801. end
  802. -- rs.setOutput(alarm_side, false)
  803. return true
  804. elseif char == 78 or char == 110 then
  805. data_setName()
  806. return true
  807. end
  808. return false
  809. end
  810.  
  811. ----------- Boot sequence
  812.  
  813. label = computer.address()
  814. if not label then
  815. label = "" .. computer.address()
  816. end
  817.  
  818. -- read configuration
  819. data_read()
  820.  
  821. -- initial scanning
  822. ShowTitle(label .. " - Connecting...")
  823. WriteLn("")
  824.  
  825. -- clear previous events
  826. repeat
  827. until event.pull(0) == nil
  828.  
  829. ship = nil
  830. for address, componentType in component.list() do
  831. os.sleep(0)
  832. if componentType == "warpdriveShipController" then
  833. WriteLn("Wrapping " .. componentType)
  834. ship = component.proxy(address)
  835. end
  836. end
  837.  
  838. if not computer.address() and ship ~= nil then
  839. data_setName()
  840. end
  841.  
  842. -- peripherals status
  843. function connections_page()
  844. ShowTitle(label .. " - Connections")
  845.  
  846. WriteLn("")
  847. if ship == nil then
  848. SetColorDisabled()
  849. WriteLn("No ship controller detected")
  850. else
  851. SetColorSuccess()
  852. WriteLn("Ship controller detected")
  853. end
  854. end
  855.  
  856. -- peripheral boot up
  857. Clear()
  858. connections_page()
  859. SetColorDefault()
  860. WriteLn("")
  861. os.sleep(0)
  862. core_boot()
  863. os.sleep(0)
  864.  
  865. -- main loop
  866. abort = false
  867. refresh = true
  868. page = connections_page
  869. keyHandler = nil
  870. repeat
  871. ClearWarning()
  872. if refresh then
  873. Clear()
  874. page()
  875. menu_common()
  876. refresh = false
  877. end
  878. params = { event.pull() }
  879. eventName = params[1]
  880. address = params[2]
  881. if address == nil then address = "none" end
  882. -- WriteLn("...")
  883. -- WriteLn("Event '" .. eventName .. "', " .. address .. ", " .. params[3] .. " received")
  884. -- os.sleep(0.2)
  885. if eventName == "key_down" then
  886. char = params[3]
  887. keycode = params[4]
  888. if char == 88 or char == 120 or keycode == 45 then -- x for eXit
  889. abort = true
  890. elseif char == 48 or keycode == 11 or keycode == 82 then -- 0
  891. page = connections_page
  892. keyHandler = nil
  893. refresh = true
  894. elseif char == 49 or keycode == 2 or keycode == 79 then -- 1
  895. page = core_page
  896. keyHandler = core_key
  897. refresh = true
  898. elseif keyHandler ~= nil and keyHandler(char, keycode) then
  899. refresh = true
  900. os.sleep(0)
  901. elseif char == 0 then -- control chars
  902. refresh = false
  903. os.sleep(0)
  904. else
  905. ShowWarning("Key " .. char .. " " .. keycode .. " is invalid")
  906. os.sleep(0.2)
  907. end
  908. -- func(unpack(params))
  909. -- abort, refresh = false, false
  910. elseif eventName == "char" then
  911. -- drop it
  912. elseif eventName == "key_up" then
  913. -- drop it
  914. elseif eventName == "touch" then
  915. -- drop it
  916. elseif eventName == "drop" then
  917. -- drop it
  918. elseif eventName == "drag" then
  919. -- drop it
  920. elseif eventName == "interrupted" then
  921. abort = true
  922. elseif not common_event(eventName, params[3]) then
  923. ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  924. refresh = true
  925. os.sleep(0.2)
  926. end
  927. until abort
  928.  
  929. -- exiting
  930. if data.core_summon then
  931. data.core_summon = false
  932. data_save()
  933. end
  934.  
  935. if ship ~= nil then
  936. ship.mode(0)
  937. end
  938.  
  939. -- clear screens on exit
  940. SetMonitorColorFrontBack(0xFFFFFF, 0x000000)
  941. term.clear()
  942. SetCursorPos(1, 1)
  943. Write("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement