infiniteblock

Untitled

Sep 18th, 2021
770
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. if warpdriveCommons then os.unloadAPI("warpdriveCommons") end
  3. if not os.loadAPI("warpdrive/warpdriveCommons") then error("missing warpdriveCommons") end
  4. local w = warpdriveCommons.w
  5.  
  6. local data
  7.  
  8. ----------- Transporter room support
  9.  
  10. local transporter
  11.  
  12. function transporter_read(parData)
  13.   data = parData
  14. end
  15.  
  16. function transporter_name(parName)
  17.   if transporter == nil or transporter.isInterfaced() == nil then
  18.     return ''
  19.   end
  20.   return transporter.name(parName)
  21. end
  22.  
  23. function transporter_boot()
  24.   if transporter ~= nil then
  25.     w.writeLn("Booting Transporter...")
  26.     local  _, _, _, _, _ = transporter.state()
  27.   end
  28. end
  29.  
  30. function transporter_key(character, keycode)
  31.   if character == 's' or character == 'S' then -- S
  32.     transporter_start()
  33.     return true
  34.   elseif character == 'p' or character == 'P' then -- P
  35.     transporter_stop()
  36.     return true
  37.   elseif character == 'l' or character == 'L' then -- L
  38.     transporter_lock()
  39.     return true
  40.   elseif character == 'e' or character == 'E' then -- E
  41.     transporter_energize()
  42.     return true
  43.   elseif character == '-' then -- -
  44.     local energyFactor = transporter.energyFactor()
  45.     transporter.energyFactor(energyFactor - 0.2)
  46.     return true
  47.   elseif character == '+' then -- +
  48.     local energyFactor = transporter.energyFactor()
  49.     transporter.energyFactor(energyFactor + 0.2)
  50.     return true
  51.   elseif character == 'c' or character == 'C' then -- C
  52.     transporter_config()
  53.     return true
  54.   end
  55.   return false
  56. end
  57.  
  58. function transporter_page()
  59.   w.page_begin(w.data_getName() .. " - Transporter room controls")
  60.  
  61.   w.setCursorPos(1, 2)
  62.   if transporter == nil or transporter.isInterfaced() == nil then
  63.     w.setColorDisabled()
  64.     w.write("Transporter core not detected")
  65.   else
  66.     w.setCursorPos(1, 2)
  67.     w.setColorDisabled()
  68.     local _, signature = transporter.name()
  69.     if (signature ~= nil) then
  70.       w.writeCentered(2, "Signature is " .. signature)
  71.     end
  72.    
  73.     w.setCursorPos(1, 3)
  74.     w.setColorNormal()
  75.     w.write("Beam frequency  = ")
  76.     local beamFrequency = transporter.beamFrequency()
  77.     w.write(w.format_integer(beamFrequency, 6))
  78.    
  79.     w.setCursorPos(1, 4)
  80.     w.setColorNormal()
  81.     w.write("Remote location = ")
  82.     local remoteLocation = { transporter.remoteLocation() }
  83.     if remoteLocation[3] ~= nil then
  84.       w.write(w.format_integer(remoteLocation[1]) .. ", " .. w.format_integer(remoteLocation[2]) .. ", " .. w.format_integer(remoteLocation[3]))
  85.     elseif remoteLocation[1] ~= nil then
  86.       w.writeCentered(5, remoteLocation[1])
  87.     else
  88.       w.write("-not defined-")
  89.     end
  90.    
  91.     w.setCursorPos(1, 6)
  92.     w.setColorNormal()
  93.     local energyFactor_PC = transporter.energyFactor() * 100
  94.     w.write("Energy factor   = " .. w.format_float(math.floor(energyFactor_PC * 10) / 10, 6) .. " %")
  95.    
  96.     w.setCursorPos(1, 7)
  97.     w.setColorNormal()
  98.     w.write("Energy required = ")
  99.     local energyStored, energyMax, energyUnits = transporter.getEnergyStatus()
  100.     local success, energyAcquiring, energyEnergizing = transporter.getEnergyRequired()
  101.     if success then
  102.       w.write(w.format_integer(energyAcquiring) .. " " .. energyUnits .. "/t then " .. w.format_integer(energyEnergizing) .. " " .. energyUnits .. "/t")
  103.     else
  104.       w.write("???")
  105.     end
  106.    
  107.     w.setCursorPos(1, 8)
  108.     w.setColorNormal()
  109.     w.write("Energy stored   = ")
  110.     transporter_page_writeEnergy(energyStored, energyMax, energyUnits)
  111.    
  112.     w.setCursorPos(1, 9)
  113.     w.setColorNormal()
  114.     w.write("  -----------------------------------------------")
  115.    
  116.     w.setCursorPos(1, 10)
  117.     w.setColorNormal()
  118.     w.write("Room is ")
  119.     local isEnabled = transporter.enable()
  120.     if isEnabled then w.setColorSuccess() else w.setColorNormal() end
  121.     w.write(w.format_boolean(isEnabled, "enabled", "disabled"))
  122.     w.setColorNormal()
  123.     w.write(".")
  124.    
  125.     w.setCursorPos(28, 10)
  126.     w.setColorNormal()
  127.     w.write("Locking is ")
  128.     local isLockRequested = transporter.lock()
  129.     if isLockRequested then w.setColorSuccess() else w.setColorNormal() end
  130.     w.write(w.format_boolean(isLockRequested, "requested", "on hold"))
  131.     w.setColorNormal()
  132.     w.write(".")
  133.    
  134.     w.setCursorPos(1, 11)
  135.     w.setColorNormal()
  136.     w.write("Lock strength is ")
  137.     local lockStrength = transporter.getLockStrength()
  138.     transporter_page_writeLockStrength(lockStrength)
  139.    
  140.     w.setCursorPos(28, 11)
  141.     w.setColorNormal()
  142.     w.write("Energize is ")
  143.     local isEnergizeRequested = transporter.energize()
  144.     if isEnergizeRequested then w.setColorSuccess() else w.setColorNormal() end
  145.     w.write(w.format_boolean(isEnergizeRequested, "requested", "on hold"))
  146.     w.setColorNormal()
  147.     w.write(".")
  148.    
  149.     w.setCursorPos(1, 13)
  150.     w.setColorNormal()
  151.     w.write("Status is ")
  152.     local _, state, _, _, isJammed = transporter.state()
  153.     if isJammed then
  154.       w.setColorBad()
  155.     end
  156.     w.writeMultiLine(state)
  157.     w.setColorNormal()
  158.     w.write(".")
  159.   end
  160.  
  161.   w.setCursorPos(1, 16)
  162.   w.setColorControl()
  163.   w.writeFullLine(" Start/stoP transporter (S/P), Configuration (C)")
  164.   w.writeFullLine(" Lock (L), Energize (E), Energy factor (+/-)")
  165. end
  166.  
  167. function transporter_page_writeEnergy(energyStored, energyMax, energyUnits)
  168.   w.setCursorPos(19, 8)
  169.   w.setColorNormal()
  170.   w.write(w.format_integer(energyStored, 8) .. " / " .. energyMax .. " " .. energyUnits .. "  ")
  171. end
  172.  
  173. function transporter_page_writeLockStrength(lockStrength)
  174.   w.setCursorPos(18, 11)
  175.   if lockStrength > 0.01 then
  176.     local percent = math.floor(lockStrength * 1000) / 10
  177.     if percent >= 80.0 then
  178.       w.setColorSuccess()
  179.     else
  180.       w.setColorWarning()
  181.     end
  182.     w.write(w.format_float(percent) .. " %  ")
  183.   else
  184.     w.setColorDisabled()
  185.     w.write(w.format_string("-none-", 7))
  186.   end
  187.   w.setColorNormal()
  188.   w.write(".")
  189. end
  190.  
  191. function transporter_setEnergyFactor()
  192.   if transporter ~= nil then
  193.     data.transporter_energyFactor = transporter.energyFactor(data.transporter_energyFactor)
  194.   end
  195. end
  196.  
  197. function transporter_start()
  198.   if transporter ~= nil then
  199.     transporter.enable(true)
  200.   end
  201. end
  202.  
  203. function transporter_stop()
  204.   if transporter ~= nil then
  205.     transporter.enable(false)
  206.   end
  207. end
  208.  
  209. function transporter_lock()
  210.   if transporter ~= nil then
  211.     transporter.lock(not transporter.lock())
  212.   end
  213. end
  214.  
  215. function transporter_energize()
  216.   if transporter ~= nil then
  217.     transporter.energize(not transporter.energize())
  218.   end
  219. end
  220.  
  221. local transporter_configPageLoaded = false
  222. function transporter_pulse(actual)
  223.   local lockStrength = actual
  224.   if transporter == nil or transporter.isInterfaced() == nil then
  225.     w.reboot()
  226.   end
  227.   if w.page_getCallbackDisplay() == transporter_page and (not transporter_configPageLoaded) then
  228.     transporter_page_writeLockStrength(lockStrength)
  229.    
  230.     local energyStored, energyMax, energyUnits = transporter.getEnergyStatus()
  231.     transporter_page_writeEnergy(energyStored, energyMax, energyUnits)
  232.   end
  233. end
  234.  
  235. function transporter_config()
  236.   transporter_configPageLoaded = true
  237.   w.page_begin(w.data_getName() .. " - Transporter room configuration")
  238.  
  239.   w.setCursorPos(1, 2)
  240.   if transporter == nil or transporter.isInterfaced() == nil then
  241.     w.setColorDisabled()
  242.     w.write("Transporter core not detected")
  243.   else
  244.    
  245.     w.setCursorPos(1, 6)
  246.     w.setColorHelp()
  247.     w.writeFullLine(" Enter a number between 100 and 1000 %.")
  248.    
  249.     w.setCursorPos(1, 4)
  250.     w.setColorNormal()
  251.     local energyFactor_PC = transporter.energyFactor() * 100
  252.     w.write("Energy factor (" .. math.floor(energyFactor_PC * 10) / 10 .. " %): ")
  253.     energyFactor_PC = w.input_readInteger(energyFactor_PC)
  254.     energyFactor_PC = transporter.energyFactor(energyFactor_PC / 100) * 100
  255.     w.setCursorPos(1, 5)
  256.     w.writeLn("Energy factor set to " .. math.floor(energyFactor_PC * 10) / 10 .. " %")
  257.     w.writeFullLine(" ")
  258.    
  259.     w.setCursorPos(1, 7)
  260.     w.setColorNormal()
  261.     local remoteLocation = { transporter.remoteLocation() }
  262.     local remoteLocation_default = "-"
  263.     if remoteLocation[3] ~= nil then
  264.       w.write("Remote location is currently set to " .. w.format_integer(remoteLocation[1]) .. ", " .. w.format_integer(remoteLocation[2]) .. ", " .. w.format_integer(remoteLocation[3]))
  265.     elseif remoteLocation[1] ~= nil then
  266.       w.writeLn("Remote location is currently set to ")
  267.       w.write(remoteLocation[1])
  268.       remoteLocation_default = remoteLocation[1]
  269.     else
  270.       w.write("Remote location is currently undefined")
  271.       remoteLocation[1] = "???"
  272.     end
  273.    
  274.     w.setCursorPos(1, 13)
  275.     w.setColorHelp()
  276.     w.writeFullLine(" Enter - as player name to use coordinates.")
  277.     w.writeFullLine(" Press Enter to save your selection.")
  278.    
  279.     w.setCursorPos(1, 10)
  280.     w.setColorNormal()
  281.     w.write("Signature, player name or dash: ")
  282.     w.setCursorPos(1, 11)
  283.     local remoteLocation_new = w.input_readText(remoteLocation_default)
  284.    
  285.     w.setCursorPos(1, 13)
  286.     w.setColorNormal()
  287.     w.writeFullLine(" ")
  288.     w.writeFullLine(" ")
  289.    
  290.     if remoteLocation_new ~= "-" then
  291.       remoteLocation = { transporter.remoteLocation(remoteLocation_new) }
  292.     else
  293.       if #remoteLocation == 1 then
  294.         remoteLocation[1], remoteLocation[2], remoteLocation[3] = transporter.getLocalPosition()
  295.       end
  296.      
  297.       w.setCursorPos(1, 10)
  298.       w.setColorNormal()
  299.       w.writeFullLine(" ")
  300.       w.writeFullLine(" ")
  301.      
  302.       -- X
  303.       w.setCursorPos(1, 12)
  304.       w.setColorHelp()
  305.       w.writeFullLine(" Enter absolute X coordinate.")
  306.      
  307.       w.setCursorPos(1, 10)
  308.       w.setColorNormal()
  309.       w.write("Remote location X coordinate (" .. w.format_integer(remoteLocation[1]) .. "): ")
  310.       remoteLocation[1] = w.input_readInteger(remoteLocation[1])
  311.      
  312.       w.setCursorPos(1, 12)
  313.       w.setColorNormal()
  314.       w.writeFullLine(" ")
  315.      
  316.       -- Y
  317.       w.setCursorPos(1, 13)
  318.       w.setColorHelp()
  319.       w.writeFullLine(" Enter Y < 0 to target below planet.")
  320.       w.writeFullLine(" Enter Y > 256 to target space above.")
  321.      
  322.       w.setCursorPos(1, 11)
  323.       w.setColorNormal()
  324.       w.write("Remote location Y coordinate (" .. w.format_integer(remoteLocation[2]) .. "): ")
  325.       remoteLocation[2] = w.input_readInteger(remoteLocation[2])
  326.      
  327.       w.setCursorPos(1, 13)
  328.       w.setColorNormal()
  329.       w.writeFullLine(" ")
  330.       w.writeFullLine(" ")
  331.      
  332.       -- Z
  333.       w.setCursorPos(1, 14)
  334.       w.setColorHelp()
  335.       w.writeFullLine(" Enter absolute Z coordinate.")
  336.      
  337.       w.setCursorPos(1, 12)
  338.       w.setColorNormal()
  339.       w.write("Remote location Z coordinate (" .. w.format_integer(remoteLocation[3]) .. "): ")
  340.       remoteLocation[3] = w.input_readInteger(remoteLocation[3])
  341.      
  342.       w.setCursorPos(1, 14)
  343.       w.setColorNormal()
  344.       w.writeFullLine(" ")
  345.      
  346.       remoteLocation = { transporter.remoteLocation(remoteLocation[1], remoteLocation[2], remoteLocation[3]) }
  347.     end
  348.    
  349.     w.setCursorPos(1, 13)
  350.     if remoteLocation[3] ~= nil then
  351.       w.write("Remote location updated to " .. w.format_integer(remoteLocation[1]) .. ", " .. w.format_integer(remoteLocation[2]) .. ", " .. w.format_integer(remoteLocation[3]))
  352.     elseif remoteLocation[1] ~= nil then
  353.       w.write("Remote location updated to " .. remoteLocation[1])
  354.     else
  355.       w.write("Remote location updated to invalid (?)")
  356.     end
  357.   end
  358.   transporter_configPageLoaded = false
  359. end
  360.  
  361. function transporter_register()
  362.   w.device_register("warpdriveTransporterCore",
  363.       function(deviceType, address, wrap) transporter = wrap end,
  364.       function() end)
  365.   w.event_register("transporterPulse"     , function(eventName, param) transporter_pulse(param)                                               return false end )
  366.   w.event_register("transporterEnergizing", function(eventName, param) w.status_showWarning("Transporting "          .. param .. " entities") return false end )
  367.   w.event_register("transporterSuccess"   , function(eventName, param) w.status_showWarning("Transported "           .. param) return false end )
  368.   w.event_register("transporterFailure"   , function(eventName, param) w.status_showWarning("Failed transportation " .. param) return false end )
  369.   w.data_register("transporter", transporter_read, nil, transporter_name)
  370. end
  371.  
  372. ----------- connections status
  373.  
  374. function connections_key(character, keycode)
  375.   if character == 'n' or character == 'N' then -- N
  376.     w.data_setName()
  377.     return true
  378.   end
  379.   return false
  380. end
  381.  
  382. function connections_page(isBooting)
  383.   w.page_begin(w.data_getName() .. " - Connections")
  384.  
  385.   w.writeLn("")
  386.  
  387.   local monitors = w.device_getMonitors()
  388.   if #monitors == 0 then
  389.     w.setColorDisabled()
  390.     w.writeLn("No Monitor detected")
  391.   elseif #monitors == 1 then
  392.     w.setColorSuccess()
  393.     w.writeLn("1 monitor detected")
  394.   else
  395.     w.setColorSuccess()
  396.     w.writeLn(#monitors .. " Monitors detected")
  397.   end
  398.  
  399.   if transporter == nil or transporter.isInterfaced() == nil then
  400.     w.setColorDisabled()
  401.     w.writeLn("No Transporter core detected")
  402.   else
  403.     w.setColorSuccess()
  404.     w.writeLn("Transporter core detected")
  405.     if isBooting then
  406.       transporter_boot()
  407.     end
  408.   end
  409.  
  410.   w.writeLn("")
  411.   w.setColorNormal()
  412.   w.writeLn("This is a keyboard controlled user interface.")
  413.   w.write("Key controls are written like so: ")
  414.   w.setColorControl()
  415.   w.write("Action (key)")
  416.   w.setColorNormal()
  417.   w.writeLn(".")
  418.   w.write("For example, typing ")
  419.   w.setColorControl()
  420.   w.write(" 1 ")
  421.   w.setColorNormal()
  422.   w.writeLn(" will open Transporter room controls.")
  423.  
  424.   w.setCursorPos(1, 17)
  425.   w.setColorControl()
  426.   w.writeFullLine(" Change computer Name (N)")
  427. end
  428.  
  429. ----------- Boot sequence
  430.  
  431. w.page_setEndText(" Home (0), Transporter room (1)")
  432. w.page_register('0', connections_page, connections_key)
  433. w.page_register('1', transporter_page, transporter_key)
  434. transporter_register()
  435.  
  436. w.boot()
  437. local success, message = pcall(w.run)
  438. if not success then
  439.   print("failed with message")
  440.   print(message)
  441.   w.sleep(3.0)
  442.   print("rebooting...")
  443.   w.reboot()
  444. else
  445.   w.close()
  446. end
RAW Paste Data