infiniteblock

Untitled

Mar 31st, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.55 KB | None | 0 0
  1. if warpdriveCommons then os.unloadAPI("warpdriveCommons") end
  2. if not os.loadAPI("warpdrive/warpdriveCommons") then error("missing warpdriveCommons") end
  3. local w = warpdriveCommons.w
  4.  
  5. local data
  6.  
  7. ----------- Force field support
  8.  
  9. local ffield_projectorAddresses = {}
  10. local ffield_projectors = {}
  11. local ffield_projector_indexSelected = 1
  12. local ffield_projector_indexFirstLine = 1
  13. local ffield_projector_lines = 10
  14.  
  15. local ffield_relayAddresses = {}
  16. local ffield_relays = {}
  17. local ffield_relay_indexSelected = 1
  18. local ffield_relay_indexFirstLine = 1
  19. local ffield_relay_lines = 10
  20.  
  21. function ffield_boot(isDetailed)
  22. if #ffield_projectorAddresses == 0 and #ffield_relayAddresses == 0 then
  23. return
  24. end
  25.  
  26. if isDetailed == nil then
  27. isDetailed = true
  28. end
  29.  
  30. if isDetailed then
  31. w.write("Booting Force field projectors and relays")
  32.  
  33. w.writeLn("...")
  34. w.sleep(0.1)
  35. end
  36.  
  37. -- getting projectors parameters
  38. ffield_projectors = {}
  39. for key, address in pairs(ffield_projectorAddresses) do
  40. local device = w.device_get(address)
  41. local x, y, z = device.getLocalPosition()
  42. local beamFrequency = device.beamFrequency()
  43. -- local isEnabled = device.enable()
  44. local status, isEnabled, isConnected, isPowered, shape, energy = device.state()
  45. -- @TODO add tier reporting
  46. local projector = {
  47. address = address,
  48. device = device,
  49. position = { x = x, y = y, z = z },
  50. name = "-not defined-",
  51. beamFrequency = beamFrequency,
  52. shape = shape,
  53. isEnabled = isEnabled }
  54. if isDetailed then
  55. w.writeLn(ffield_projector_getDescription(projector))
  56. end
  57. table.insert(ffield_projectors, projector)
  58. end
  59.  
  60. -- getting relays parameters
  61. ffield_relays = {}
  62. for key, address in pairs(ffield_relayAddresses) do
  63. local device = w.device_get(address)
  64. local x, y, z = device.getLocalPosition()
  65. local beamFrequency = device.beamFrequency()
  66. local isEnabled = device.enable()
  67. local relay = {
  68. address = address,
  69. device = device,
  70. position = { x = x, y = y, z = z },
  71. name = "-not defined-",
  72. beamFrequency = beamFrequency,
  73. isEnabled = isEnabled }
  74. if isDetailed then
  75. w.writeLn(ffield_relay_getDescription(relay))
  76. end
  77. table.insert(ffield_relays, relay)
  78. end
  79. end
  80.  
  81. function ffield_save()
  82. -- nothing
  83. end
  84.  
  85. function ffield_read(parData)
  86. data = parData
  87. end
  88.  
  89. function ffield_projector_getDescription(projector)
  90. if projector == nil or projector.device == nil then
  91. return "~invalid~"
  92. end
  93. local description = "#" .. w.format_integer(projector.beamFrequency, 5)
  94. .. " @ (" .. w.format_integer(projector.position.x, 7) .. " " .. w.format_integer(projector.position.y, 3) .. " " .. w.format_integer(projector.position.z, 7) .. ") "
  95. .. w.format_string(projector.shape, 10)
  96. .. " "
  97. if projector.isEnabled then
  98. description = description .. "Enabled"
  99. else
  100. description = description .. "Disabled"
  101. end
  102. return description
  103. end
  104.  
  105. function ffield_projector_getIndexes()
  106. if ffield_projectors ~= nil then
  107. if ffield_projector_indexSelected > #ffield_projectors then
  108. ffield_projector_indexSelected = 1
  109. elseif ffield_projector_indexSelected < 1 then
  110. ffield_projector_indexSelected = #ffield_projectors
  111. end
  112. if ffield_projector_indexFirstLine > ffield_projector_indexSelected then
  113. ffield_projector_indexFirstLine = ffield_projector_indexSelected
  114. elseif ffield_projector_indexFirstLine + ffield_projector_lines < ffield_projector_indexSelected then
  115. ffield_projector_indexFirstLine = ffield_projector_indexSelected - ffield_projector_lines
  116. end
  117. return ffield_projector_indexFirstLine, ffield_projector_indexSelected
  118. else
  119. return 1, 1
  120. end
  121. end
  122.  
  123. function ffield_projector_get(index)
  124. local indexToUse = index
  125. local projector
  126.  
  127. if ffield_projectors ~= nil then
  128. if indexToUse > #ffield_projectors then
  129. indexToUse = 1
  130. elseif indexToUse < 1 then
  131. indexToUse = #ffield_projectors
  132. end
  133. projector = ffield_projectors[indexToUse]
  134. end
  135.  
  136. if projector == nil then
  137. ffield_boot(false)
  138. w.status_showWarning("Invalid projector index " .. index)
  139. projector = {
  140. address = "-",
  141. device = nil,
  142. position = { x = 0, y = 0, z = 0 },
  143. name = "-",
  144. beamFrequency = -1,
  145. shape = "NONE",
  146. isEnabled = false }
  147. end
  148.  
  149. return projector
  150. end
  151.  
  152. function ffield_projector_getSelected()
  153. return ffield_projector_get(ffield_projector_indexSelected)
  154. end
  155.  
  156. function ffield_enable(projectorOrRelay, enable)
  157. if projectorOrRelay == nil or projectorOrRelay.device == nil then
  158. return
  159. end
  160. local enableToApply = enable
  161. if enableToApply == nil then
  162. enableToApply = not projectorOrRelay.device.enable()
  163. end
  164. projectorOrRelay.isEnabled = projectorOrRelay.device.enable(enableToApply)
  165. return projectorOrRelay.isEnabled
  166. end
  167.  
  168. function ffield_projector_key(character, keycode)
  169. if character == 's' or character == 'S' then
  170. for key, projector in pairs(ffield_projectors) do
  171. ffield_enable(projector, true)
  172. end
  173. return true
  174. elseif character == 'p' or character == 'P' then
  175. for key, projector in pairs(ffield_projectors) do
  176. ffield_enable(projector, false)
  177. end
  178. return true
  179. elseif character == 'e' or character == 'E' then
  180. local projector = ffield_projector_getSelected()
  181. if projector ~= nil and projector.device ~= nil then
  182. ffield_enable(projector)
  183. end
  184. return true
  185. elseif character == 'c' or character == 'C' then -- C or keycode == 46
  186. ffield_projector_config()
  187. w.data_save()
  188. return true
  189. elseif keycode == 200 or keycode == 203 or character == '-' then -- Up or Left or -
  190. ffield_projector_indexSelected = ffield_projector_indexSelected - 1
  191. return true
  192. elseif keycode == 208 or keycode == 205 or character == '+' then -- Down or Right or +
  193. ffield_projector_indexSelected = ffield_projector_indexSelected + 1
  194. return true
  195. end
  196. return false
  197. end
  198.  
  199. function ffield_projector_page()
  200. w.page_begin(w.data_getName() .. " - Force field projectors")
  201.  
  202. -- w.setCursorPos(1, 2)
  203. if #ffield_projectors == 0 then
  204. w.setColorDisabled()
  205. w.writeCentered(2, "No force field projector defined, connect one and reboot!")
  206. else
  207. w.setColorNormal()
  208. local indexFirstLine, indexSelected = ffield_projector_getIndexes()
  209. w.writeCentered(2, "Force field projector " .. indexSelected .. " of " .. #ffield_projectors .. " is selected")
  210. local indexLastLine = math.min(indexFirstLine + ffield_projector_lines, #ffield_projectors)
  211. for indexCurrent = indexFirstLine, indexLastLine do
  212. if indexCurrent == indexSelected then
  213. w.setColorSelected()
  214. w.clearLine()
  215. w.write(">")
  216. else
  217. w.setColorNormal()
  218. w.write(" ")
  219. end
  220. local projector = ffield_projector_get(indexCurrent)
  221. local description = ffield_projector_getDescription(projector)
  222. w.write(description)
  223. w.writeLn("")
  224. end
  225. end
  226.  
  227. w.setCursorPos(1, 14)
  228. w.setColorNormal()
  229. w.write(" -----------------------------------------------")
  230.  
  231. w.setCursorPos(1, 15)
  232. w.setColorControl()
  233. w.writeFullLine(" Start/stoP all force field projectors (S/P)")
  234. w.writeFullLine(" Configure (C) or togglE (E) selected projector")
  235. w.writeFullLine(" select force field projector (Up, Down)")
  236. end
  237.  
  238. function ffield_projector_config()
  239. local projector = ffield_projector_getSelected()
  240. if projector == nil then
  241. return
  242. end
  243. w.page_begin(w.data_getName() .. " - Projector configuration")
  244.  
  245. w.setCursorPos(1, 2)
  246. w.setColorNormal()
  247. projector.position.x, projector.position.y, projector.position.z = projector.device.getLocalPosition()
  248. w.write("Projector @ " .. w.format_integer(projector.position.x, 7) .. " " .. w.format_integer(projector.position.y, 3) .. " " .. w.format_integer(projector.position.z, 7))
  249.  
  250. -- name
  251. w.setCursorPos(1, 4)
  252. w.setColorHelp()
  253. w.writeFullLine(" Press enter to validate.")
  254. w.setCursorPos(1, 3)
  255. w.setColorNormal()
  256. w.writeLn("Name (" .. projector.name .. "):")
  257. projector.name = w.input_readText(projector.name)
  258. w.setCursorPos(1, 3)
  259. w.clearLine()
  260. w.writeLn("Name set to " .. projector.name)
  261. w.clearLine()
  262.  
  263. w.setColorDisabled()
  264.  
  265. -- beam frequency
  266. projector.beamFrequency = projector.device.beamFrequency()
  267. w.setCursorPos(1, 6)
  268. w.setColorHelp()
  269. w.writeFullLine(" Enter a number between 0 and 65000.")
  270. local frequency
  271. repeat
  272. w.setCursorPos(1, 5)
  273. w.setColorNormal()
  274. w.clearLine()
  275. w.write("Beam frequency (" .. w.format_integer(projector.beamFrequency, 5) .. "): ")
  276. frequency = w.input_readInteger(projector.beamFrequency)
  277. if frequency ~= 0 and (frequency < 0 or frequency > 65000) then
  278. w.status_showWarning("This is not a valid beam frequency. Try again.")
  279. end
  280. until frequency > 0 and frequency <= 65000
  281. w.setCursorPos(1, 4)
  282. w.clearLine()
  283. projector.beamFrequency = projector.device.beamFrequency(frequency)
  284. w.write("Beam frequency set to " .. projector.beamFrequency)
  285. w.setCursorPos(1, 5)
  286. w.clearLine()
  287. w.setCursorPos(1, 6)
  288. w.clearLine()
  289.  
  290. -- translation
  291. ffield_config_xyz(5, "Translation", "%", "translation where 0 is centered", projector.device.translation, 100)
  292.  
  293. -- rotation
  294. ffield_config_xyz(6, "Rotation in deg", "", "rotation where 0 is centered", projector.device.rotation, 1)
  295.  
  296. -- scale
  297. ffield_config_xyz(7, "Min scale", "%", "min scale where -100 is full scale", projector.device.min, 100)
  298. ffield_config_xyz(8, "Max scale", "%", "max scale where 100 is full scale", projector.device.max, 100)
  299. end
  300.  
  301. function ffield_config_xyz(yCursor, title, unit, help, method, factor)
  302. w.setCursorPos(1, yCursor + 1)
  303. local x, y, z = method()
  304. x = factor * x
  305. y = factor * y
  306. z = factor * z
  307. w.write(title .. " is currently set to " .. w.format_integer(x, 4) .. unit .. " " .. w.format_integer(y, 4) .. unit .. " " .. w.format_integer(z, 4) .. unit)
  308.  
  309. w.setCursorPos(1, 16)
  310. w.setColorHelp()
  311. w.writeFullLine(" Enter X " .. help)
  312.  
  313. w.setCursorPos(1, yCursor + 3)
  314. w.setColorNormal()
  315. w.write(title .. " along X axis (" .. w.format_integer(x) .. "): ")
  316. x = w.input_readInteger(x)
  317.  
  318. w.setCursorPos(1, 16)
  319. w.setColorHelp()
  320. w.writeFullLine(" Enter Y " .. help)
  321.  
  322. w.setCursorPos(1, yCursor + 4)
  323. w.setColorNormal()
  324. w.write(title .. " along Y axis (" .. w.format_integer(y) .. "): ")
  325. y = w.input_readInteger(y)
  326.  
  327. w.setCursorPos(1, 16)
  328. w.setColorHelp()
  329. w.writeFullLine(" Enter Z " .. help)
  330.  
  331. w.setCursorPos(1, yCursor + 5)
  332. w.setColorNormal()
  333. w.write(title .. " along Z axis (" .. w.format_integer(z) .. "): ")
  334. z = w.input_readInteger(z)
  335.  
  336. w.setCursorPos(1, 16)
  337. w.clearLine()
  338.  
  339. x, y, z = method(x / factor, y / factor, z / factor)
  340. x = factor * x
  341. y = factor * y
  342. z = factor * z
  343. w.setCursorPos(1, yCursor)
  344. w.setColorNormal()
  345. w.write(title .. " set to " .. w.format_integer(x, 4) .. unit .. " " .. w.format_integer(y, 4) .. unit .. " " .. w.format_integer(z, 4) .. unit)
  346. w.setCursorPos(1, yCursor + 1)
  347. w.clearLine()
  348. w.setCursorPos(1, yCursor + 3)
  349. w.clearLine()
  350. w.setCursorPos(1, yCursor + 4)
  351. w.clearLine()
  352. w.setCursorPos(1, yCursor + 5)
  353. w.clearLine()
  354. end
  355.  
  356. function ffield_relay_getDescription(relay)
  357. if relay == nil or relay.device == nil then
  358. return "~invalid~"
  359. end
  360. local description = "#" .. w.format_integer(relay.beamFrequency, 5)
  361. .. " @ (" .. w.format_integer(relay.position.x, 7) .. " " .. w.format_integer(relay.position.y, 3) .. " " .. w.format_integer(relay.position.z, 7) .. ") "
  362. if relay.isEnabled then
  363. description = description .. "Enabled"
  364. else
  365. description = description .. "Disabled"
  366. end
  367. return description
  368. end
  369.  
  370. function ffield_relay_getIndexes()
  371. if ffield_relays ~= nil then
  372. if ffield_relay_indexSelected > #ffield_relays then
  373. ffield_relay_indexSelected = 1
  374. elseif ffield_relay_indexSelected < 1 then
  375. ffield_relay_indexSelected = #ffield_relays
  376. end
  377. if ffield_relay_indexFirstLine > ffield_relay_indexSelected then
  378. ffield_relay_indexFirstLine = ffield_relay_indexSelected
  379. elseif ffield_relay_indexFirstLine + ffield_relay_lines < ffield_relay_indexSelected then
  380. ffield_relay_indexFirstLine = ffield_relay_indexSelected - ffield_relay_lines
  381. end
  382. return ffield_relay_indexFirstLine, ffield_relay_indexSelected
  383. else
  384. return 1, 1
  385. end
  386. end
  387.  
  388. function ffield_relay_get(index)
  389. local indexToUse = index
  390. local relay
  391.  
  392. if ffield_relays ~= nil then
  393. if indexToUse > #ffield_relays then
  394. indexToUse = 1
  395. elseif indexToUse < 1 then
  396. indexToUse = #ffield_relays
  397. end
  398. relay = ffield_relays[indexToUse]
  399. end
  400.  
  401. if relay == nil then
  402. ffield_boot(false)
  403. w.status_showWarning("Invalid relay index " .. index)
  404. relay = {
  405. address = "-",
  406. device = nil,
  407. position = { x = 0, y = 0, z = 0 },
  408. name = "-",
  409. beamFrequency = -1,
  410. isEnabled = false }
  411. end
  412.  
  413. return relay
  414. end
  415.  
  416. function ffield_relay_getSelected()
  417. return ffield_relay_get(ffield_relay_indexSelected)
  418. end
  419.  
  420. function ffield_relay_key(character, keycode)
  421. if character == 's' or character == 'S' then
  422. for key, relay in pairs(ffield_relays) do
  423. ffield_enable(relay, true)
  424. end
  425. return true
  426. elseif character == 'p' or character == 'P' then
  427. for key, relay in pairs(ffield_relays) do
  428. ffield_enable(relay, false)
  429. end
  430. return true
  431. elseif character == 'e' or character == 'E' then
  432. local relay = ffield_relay_getSelected()
  433. ffield_enable(relay)
  434. return true
  435. elseif character == 'c' or character == 'C' then -- C or keycode == 46
  436. ffield_relay_config()
  437. w.data_save()
  438. return true
  439. elseif keycode == 200 or keycode == 203 or character == '-' then -- Up or Left or -
  440. ffield_relay_indexSelected = ffield_relay_indexSelected - 1
  441. return true
  442. elseif keycode == 208 or keycode == 205 or character == '+' then -- Down or Right or +
  443. ffield_relay_indexSelected = ffield_relay_indexSelected + 1
  444. return true
  445. end
  446. return false
  447. end
  448.  
  449. function ffield_relay_page()
  450. w.page_begin(w.data_getName() .. " - Force field relays")
  451.  
  452. -- w.setCursorPos(1, 2)
  453. if #ffield_relays == 0 then
  454. w.setColorDisabled()
  455. w.writeCentered(2, "No force field relay defined, connect one and reboot!")
  456. else
  457. w.setColorNormal()
  458. local indexFirstLine, indexSelected = ffield_relay_getIndexes()
  459. w.writeCentered(2, "Force field relay " .. indexSelected .. " of " .. #ffield_relays .. " is selected")
  460. local indexLastLine = math.min(indexFirstLine + ffield_relay_lines, #ffield_relays)
  461. for indexCurrent = indexFirstLine, indexLastLine do
  462. if indexCurrent == indexSelected then
  463. w.setColorSelected()
  464. w.clearLine()
  465. w.write(">")
  466. else
  467. w.setColorNormal()
  468. w.write(" ")
  469. end
  470. local relay = ffield_relay_get(indexCurrent)
  471. local description = ffield_relay_getDescription(relay)
  472. w.write(description)
  473. w.writeLn("")
  474. end
  475. end
  476.  
  477. w.setCursorPos(1, 14)
  478. w.setColorNormal()
  479. w.write(" -----------------------------------------------")
  480.  
  481. w.setCursorPos(1, 15)
  482. w.setColorControl()
  483. w.writeFullLine(" Start/stoP all force field relays (S/P)")
  484. w.writeFullLine(" Configure (C) or togglE (E) selected relay")
  485. w.writeFullLine(" select force field relay (Up, Down)")
  486. end
  487.  
  488. function ffield_relay_config()
  489. local relay = ffield_relay_getSelected()
  490. if relay == nil then
  491. return
  492. end
  493. w.page_begin(w.data_getName() .. " - Relay configuration")
  494.  
  495. w.setCursorPos(1, 2)
  496. w.setColorNormal()
  497. relay.position.x, relay.position.y, relay.position.z = relay.device.getLocalPosition()
  498. w.write("Relay @ " .. w.format_integer(relay.position.x, 7) .. " " .. w.format_integer(relay.position.y, 3) .. " " .. w.format_integer(relay.position.z, 7))
  499.  
  500. -- name
  501. w.setCursorPos(1, 4)
  502. w.setColorHelp()
  503. w.writeFullLine(" Press enter to validate.")
  504. w.setCursorPos(1, 3)
  505. w.setColorNormal()
  506. w.writeLn("Name (" .. relay.name .. "):")
  507. relay.name = w.input_readText(relay.name)
  508. w.setCursorPos(1, 3)
  509. w.clearLine()
  510. w.writeLn("Name set to " .. relay.name)
  511. w.clearLine()
  512.  
  513. w.setColorDisabled()
  514.  
  515. -- beam frequency
  516. relay.beamFrequency = relay.device.beamFrequency()
  517. w.setCursorPos(1, 6)
  518. w.setColorHelp()
  519. w.writeFullLine(" Enter a number between 0 and 65000.")
  520. local frequency
  521. repeat
  522. w.setCursorPos(1, 5)
  523. w.setColorNormal()
  524. w.clearLine()
  525. w.write("Beam frequency (" .. w.format_integer(relay.beamFrequency, 5) .. "): ")
  526. frequency = w.input_readInteger(relay.beamFrequency)
  527. if frequency ~= 0 and (frequency < 0 or frequency > 65000) then
  528. w.status_showWarning("This is not a valid beam frequency. Try again.")
  529. end
  530. until frequency > 0 and frequency <= 65000
  531. w.setCursorPos(1, 4)
  532. w.clearLine()
  533. relay.beamFrequency = relay.device.beamFrequency(frequency)
  534. w.write("Beam frequency set to " .. relay.beamFrequency)
  535. w.setCursorPos(1, 5)
  536. w.clearLine()
  537. w.setCursorPos(1, 6)
  538. w.clearLine()
  539. end
  540.  
  541. function ffield_register()
  542. w.device_register("warpdriveForceFieldProjector",
  543. function(deviceType, address, wrap) table.insert(ffield_projectorAddresses, address) end,
  544. function() end)
  545. w.device_register("warpdriveForceFieldRelay",
  546. function(deviceType, address, wrap) table.insert(ffield_relayAddresses, address) end,
  547. function() end)
  548. w.data_register("ffield", ffield_read, ffield_save, nil)
  549. end
  550.  
  551. ----------- connections status
  552.  
  553. function connections_page(isBooting)
  554. w.page_begin(w.data_getName() .. " - Connections")
  555.  
  556. w.writeLn("")
  557.  
  558. local monitors = w.device_getMonitors()
  559. if #monitors == 0 then
  560. w.setColorDisabled()
  561. w.writeLn("No Monitor detected")
  562. elseif #monitors == 1 then
  563. w.setColorSuccess()
  564. w.writeLn("1 monitor detected")
  565. else
  566. w.setColorSuccess()
  567. w.writeLn(#monitors .. " Monitors detected")
  568. end
  569.  
  570. if #ffield_projectorAddresses == 0 then
  571. w.setColorDisabled()
  572. w.writeLn("No force field projector detected")
  573. elseif #ffield_projectorAddresses == 1 then
  574. w.setColorSuccess()
  575. w.writeLn("1 force field projector detected")
  576. else
  577. w.setColorSuccess()
  578. w.writeLn(#ffield_projectorAddresses .. " force field projectors detected")
  579. end
  580.  
  581. if #ffield_relayAddresses == 0 then
  582. w.setColorDisabled()
  583. w.writeLn("No force field relay detected")
  584. elseif #ffield_relayAddresses == 1 then
  585. w.setColorSuccess()
  586. w.writeLn("1 force field relay detected")
  587. else
  588. w.setColorSuccess()
  589. w.writeLn(#ffield_relayAddresses .. " force field relays detected")
  590. end
  591.  
  592. if isBooting then
  593. ffield_boot()
  594. end
  595.  
  596. w.writeLn("")
  597. w.setColorNormal()
  598. w.writeLn("This is a keyboard controlled user interface.")
  599. w.write("Key controls are written like so: ")
  600. w.setColorControl()
  601. w.write("Action (key)")
  602. w.setColorNormal()
  603. w.writeLn(".")
  604. w.write("For example, typing ")
  605. w.setColorControl()
  606. w.write(" 1 ")
  607. w.setColorNormal()
  608. w.writeLn(" will open Force field projectors.")
  609. end
  610.  
  611. ----------- Boot sequence
  612.  
  613. w.page_setEndText(" Home (0), Projectors (1), Relays (2)")
  614. w.page_register('0', connections_page, nil)
  615. w.page_register('1', ffield_projector_page, ffield_projector_key)
  616. w.page_register('2', ffield_relay_page, ffield_relay_key)
  617. ffield_register()
  618.  
  619. w.boot()
  620. w.run()
  621. w.close()
Add Comment
Please, Sign In to add comment