OmegaRogue

WarpDrive Transporter network

Feb 19th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.04 KB | None | 0 0
  1. if not term.isColor() then
  2.  
  3.   print("Advanced computer required")
  4.  
  5.   exit()
  6.  
  7. end
  8.  
  9. print("loading...")
  10.  
  11.  
  12.  
  13. monitor_textScale = 0.5
  14.  
  15.  
  16.  
  17. Style = {
  18.  
  19.   CDefault = colors.white,
  20.  
  21.   BGDefault = colors.blue,
  22.  
  23.  
  24.  
  25.   CTitle = colors.black,
  26.  
  27.   BGTitle = colors.cyan,
  28.  
  29.  
  30.  
  31.   CWarning = colors.white,
  32.  
  33.   BGWarning = colors.red,
  34.  
  35.  
  36.  
  37.   CSuccess = colors.white,
  38.  
  39.   BGSuccess = colors.lime,
  40.  
  41.  
  42.  
  43.   CDisabled = colors.gray,
  44.  
  45.   BGDisabled = colors.blue
  46.  
  47. }
  48.  
  49.  
  50.  
  51. ----------- Monitor support
  52.  
  53.  
  54.  
  55. function SetMonitorColorFrontBack(frontColor, backgroundColor)
  56.  
  57.   term.setBackgroundColor(backgroundColor)
  58.  
  59.   term.setTextColor(frontColor)
  60.  
  61.   if monitors ~= nil then
  62.  
  63.     for key,monitor in pairs(monitors) do
  64.  
  65.       monitor.setTextColor(frontColor)
  66.  
  67.       monitor.setBackgroundColor(backgroundColor)
  68.  
  69.     end
  70.  
  71.   end
  72.  
  73. end
  74.  
  75.  
  76.  
  77. function Write(text)
  78.  
  79.   term.write(text)
  80.  
  81.   if monitors ~= nil then
  82.  
  83.     for key,monitor in pairs(monitors) do
  84.  
  85.       if key ~= data.radar_monitorIndex then
  86.  
  87.         monitor.write(text)
  88.  
  89.       end
  90.  
  91.     end
  92.  
  93.   end
  94.  
  95. end
  96.  
  97.  
  98.  
  99. function SetCursorPos(x, y)
  100.  
  101.   term.setCursorPos(x, y)
  102.  
  103.   if monitors ~= nil then
  104.  
  105.     for key,monitor in pairs(monitors) do
  106.  
  107.       if key ~= data.radar_monitorIndex then
  108.  
  109.         monitor.setCursorPos(x, y)
  110.  
  111.       end
  112.  
  113.     end
  114.  
  115.   end
  116.  
  117. end
  118.  
  119.  
  120.  
  121. function SetColorDefault()
  122.  
  123.   SetMonitorColorFrontBack(Style.CDefault, Style.BGDefault)
  124.  
  125. end
  126.  
  127.  
  128.  
  129. function SetColorTitle()
  130.  
  131.   SetMonitorColorFrontBack(Style.CTitle, Style.BGTitle)
  132.  
  133. end
  134.  
  135.  
  136.  
  137. function SetColorWarning()
  138.  
  139.   SetMonitorColorFrontBack(Style.CWarning, Style.BGWarning)
  140.  
  141. end
  142.  
  143.  
  144.  
  145. function SetColorSuccess()
  146.  
  147.   SetMonitorColorFrontBack(Style.CSuccess, Style.BGSuccess)
  148.  
  149. end
  150.  
  151.  
  152.  
  153. function SetColorDisabled()
  154.  
  155.   SetMonitorColorFrontBack(Style.CDisabled, Style.BGDisabled)
  156.  
  157. end
  158.  
  159.  
  160.  
  161. function Clear()
  162.  
  163.   clearWarningTick = -1
  164.  
  165.   SetColorDefault()
  166.  
  167.   term.clear()
  168.  
  169.   if monitors ~= nil then
  170.  
  171.     for key,monitor in pairs(monitors) do
  172.  
  173.       if key ~= data.radar_monitorIndex then
  174.  
  175.         monitor.clear()
  176.  
  177.       end
  178.  
  179.     end
  180.  
  181.   end
  182.  
  183.   SetCursorPos(1, 1)
  184.  
  185. end
  186.  
  187.  
  188.  
  189. function ClearLine()
  190.  
  191.   SetColorDefault()
  192.  
  193.   term.clearLine()
  194.  
  195.   if monitors ~= nil then
  196.  
  197.     for key,monitor in pairs(monitors) do
  198.  
  199.       if key ~= data.radar_monitorIndex then
  200.  
  201.         monitor.clearLine()
  202.  
  203.       end
  204.  
  205.     end
  206.  
  207.   end
  208.  
  209.   SetCursorPos(1, 1)
  210.  
  211. end
  212.  
  213.  
  214.  
  215. function WriteLn(text)
  216.  
  217.   Write(text)
  218.  
  219.   local x, y = term.getCursorPos()
  220.  
  221.   local width, height = term.getSize()
  222.  
  223.   if y > height - 1 then
  224.  
  225.     y = 1
  226.  
  227.   end
  228.  
  229.   SetCursorPos(1, y + 1)
  230.  
  231. end
  232.  
  233.  
  234.  
  235. function WriteCentered(y, text)
  236.  
  237.   SetCursorPos((51 - text:len()) / 2, y)
  238.  
  239.   term.write(text)
  240.  
  241.   if monitors ~= nil then
  242.  
  243.     for key,monitor in pairs(monitors) do
  244.  
  245.       if key ~= data.radar_monitorIndex then
  246.  
  247.         local sizeX, sizeY = monitor.getSize()
  248.  
  249.         monitor.setCursorPos((sizeX - text:len()) / 2, y)
  250.  
  251.         monitor.write(text)
  252.  
  253.       end
  254.  
  255.     end
  256.  
  257.   end
  258.  
  259.   local xt, yt = term.getCursorPos()
  260.  
  261.   SetCursorPos(1, yt + 1)
  262.  
  263. end
  264.  
  265.  
  266.  
  267. function ShowTitle(text)
  268.  
  269.   Clear()
  270.  
  271.   SetColorTitle()
  272.  
  273.   WriteCentered(1, text)
  274.  
  275.   SetColorDefault()
  276.  
  277. end
  278.  
  279.  
  280.  
  281. function ShowMenu(text)
  282.  
  283.   Write(text)
  284.  
  285.   local sizeX, sizeY = term.getSize()
  286.  
  287.   local xt, yt = term.getCursorPos()
  288.  
  289.   for i = xt, sizeX do
  290.  
  291.     Write(" ")
  292.  
  293.   end
  294.  
  295.   SetCursorPos(1, yt + 1)
  296.  
  297. end
  298.  
  299.  
  300.  
  301. local clearWarningTick = -1
  302.  
  303. function ShowWarning(text)
  304.  
  305.   local sizeX, sizeY = term.getSize()
  306.  
  307.   SetCursorPos(1, sizeY)
  308.  
  309.   ClearLine()
  310.  
  311.   SetColorWarning()
  312.  
  313.   SetCursorPos((sizeX - text:len() - 2) / 2, sizeY)
  314.  
  315.   Write(" " .. text .. " ")
  316.  
  317.   SetColorDefault()
  318.  
  319.   clearWarningTick = 5
  320.  
  321. end
  322.  
  323. function ClearWarning()
  324.  
  325.   if clearWarningTick > 0 then
  326.  
  327.     clearWarningTick = clearWarningTick - 1
  328.  
  329.   elseif clearWarningTick == 0 then
  330.  
  331.     SetColorDefault()
  332.  
  333.     local sizeX, sizeY = term.getSize()
  334.  
  335.     SetCursorPos(1, sizeY)
  336.  
  337.     ClearLine()
  338.  
  339.     clearWarningTick = -1
  340.  
  341.   end
  342.  
  343. end
  344.  
  345.  
  346.  
  347. ----------- Formatting & popups
  348.  
  349.  
  350.  
  351. function FormatFloat(value, nbchar)
  352.  
  353.   local str = "?"
  354.  
  355.   if value ~= nil then
  356.  
  357.     str = string.format("%g", value)
  358.  
  359.   end
  360.  
  361.   if nbchar ~= nil then
  362.  
  363.     str = string.sub("               " .. str, -nbchar)
  364.  
  365.   end
  366.  
  367.   return str
  368.  
  369. end
  370.  
  371. function FormatInteger(value, nbchar)
  372.  
  373.   local str = "?"
  374.  
  375.   if value ~= nil then
  376.  
  377.     str = string.format("%d", value)
  378.  
  379.   end
  380.  
  381.   if nbchar ~= nil then
  382.  
  383.     str = string.sub("               " .. str, -nbchar)
  384.  
  385.   end
  386.  
  387.   return str
  388.  
  389. end
  390.  
  391.  
  392.  
  393. function boolToYesNo(bool)
  394.  
  395.   if bool then
  396.  
  397.     return "YES"
  398.  
  399.   else
  400.  
  401.     return "no"
  402.  
  403.   end
  404.  
  405. end
  406.  
  407.  
  408.  
  409. function readInputNumber(currentValue)
  410.  
  411.   local inputAbort = false
  412.  
  413.   local input = string.format(currentValue)
  414.  
  415.   if input == "0" then
  416.  
  417.     input = ""
  418.  
  419.   end
  420.  
  421.   local x, y = term.getCursorPos()
  422.  
  423.   repeat
  424.  
  425.     ClearWarning()
  426.  
  427.     SetColorDefault()
  428.  
  429.     SetCursorPos(x, y)
  430.  
  431.     Write(input .. "            ")
  432.  
  433.     input = string.sub(input, -9)
  434.  
  435.    
  436.  
  437.     local params = { os.pullEventRaw() }
  438.  
  439.     local eventName = params[1]
  440.  
  441.     local address = params[2]
  442.  
  443.     if address == nil then address = "none" end
  444.  
  445.     if eventName == "key" then
  446.  
  447.       local keycode = params[2]
  448.  
  449.       if keycode >= 2 and keycode <= 10 then -- 1 to 9
  450.  
  451.         input = input .. string.format(keycode - 1)
  452.  
  453.       elseif keycode == 11 or keycode == 82 then -- 0 & keypad 0
  454.  
  455.         input = input .. "0"
  456.  
  457.       elseif keycode >= 79 and keycode <= 81 then -- keypad 1 to 3
  458.  
  459.         input = input .. string.format(keycode - 78)
  460.  
  461.       elseif keycode >= 75 and keycode <= 77 then -- keypad 4 to 6
  462.  
  463.         input = input .. string.format(keycode - 71)
  464.  
  465.       elseif keycode >= 71 and keycode <= 73 then -- keypad 7 to 9
  466.  
  467.         input = input .. string.format(keycode - 64)
  468.  
  469.       elseif keycode == 14 then -- Backspace
  470.  
  471.         input = string.sub(input, 1, string.len(input) - 1)
  472.  
  473.       elseif keycode == 211 then -- Delete
  474.  
  475.         input = ""
  476.  
  477.       elseif keycode == 28 then -- Enter
  478.  
  479.         inputAbort = true
  480.  
  481.       elseif keycode == 74 or keycode == 12 or keycode == 49 then -- - on numeric keypad or - on US top or n letter
  482.  
  483.         if string.sub(input, 1, 1) == "-" then
  484.  
  485.           input = string.sub(input, 2)
  486.  
  487.         else
  488.  
  489.           input = "-" .. input
  490.  
  491.         end
  492.  
  493.       elseif keycode == 78 then -- +
  494.  
  495.         if string.sub(input, 1, 1) == "-" then
  496.  
  497.           input = string.sub(input, 2)
  498.  
  499.         end
  500.  
  501.       else
  502.  
  503.         ShowWarning("Key " .. keycode .. " is invalid")
  504.  
  505.       end
  506.  
  507.     elseif eventName == "terminate" then
  508.  
  509.       inputAbort = true
  510.  
  511.     elseif not common_event(eventName, params[2]) then
  512.  
  513.       ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  514.  
  515.     end
  516.  
  517.   until inputAbort
  518.  
  519.   SetCursorPos(1, y + 1)
  520.  
  521.   if input == "" or input == "-" then
  522.  
  523.     return currentValue
  524.  
  525.   else
  526.  
  527.     return tonumber(input)
  528.  
  529.   end
  530.  
  531. end
  532.  
  533.  
  534.  
  535. function readInputText(currentValue)
  536.  
  537.   local inputAbort = false
  538.  
  539.   local input = string.format(currentValue)
  540.  
  541.   local x, y = term.getCursorPos()
  542.  
  543.   Write(input)
  544.  
  545.   os.pullEventRaw() -- skip first char event
  546.  
  547.   repeat
  548.  
  549.     ClearWarning()
  550.  
  551.     SetColorDefault()
  552.  
  553.     SetCursorPos(x, y)
  554.  
  555.     Write(input .. "                              ")
  556.  
  557.     input = string.sub(input, -30)
  558.  
  559.    
  560.  
  561.     local params = { os.pullEventRaw() }
  562.  
  563.     local eventName = params[1]
  564.  
  565.     local address = params[2]
  566.  
  567.     if address == nil then address = "none" end
  568.  
  569.     if eventName == "key" then
  570.  
  571.       local keycode = params[2]
  572.  
  573.       if keycode == 14 then -- Backspace
  574.  
  575.         input = string.sub(input, 1, string.len(input) - 1)
  576.  
  577.       elseif keycode == 211 then -- Delete
  578.  
  579.         input = ""
  580.  
  581.       elseif keycode == 28 then -- Enter
  582.  
  583.         inputAbort = true
  584.  
  585.       else
  586.  
  587.         ShowWarning("Key " .. keycode .. " is invalid")
  588.  
  589.       end
  590.  
  591.     elseif eventName == "char" then
  592.  
  593.       local char = params[2]
  594.  
  595.       if char >= ' ' and char <= '~' then -- 1 to 9
  596.  
  597.         input = input .. char
  598.  
  599.       else
  600.  
  601.         ShowWarning("Char #" .. string.byte(char) .. " is invalid")
  602.  
  603.       end
  604.  
  605.     elseif eventName == "terminate" then
  606.  
  607.       inputAbort = true
  608.  
  609.     elseif not common_event(eventName, params[2]) then
  610.  
  611.       ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  612.  
  613.     end
  614.  
  615.   until inputAbort
  616.  
  617.   SetCursorPos(1, y + 1)
  618.  
  619.   if input == "" then
  620.  
  621.     return currentValue
  622.  
  623.   else
  624.  
  625.     return input
  626.  
  627.   end
  628.  
  629. end
  630.  
  631.  
  632.  
  633. function readConfirmation(msg)
  634.  
  635.   if msg == nil then
  636.  
  637.     ShowWarning("Are you sure? (y/n)")
  638.  
  639.   else
  640.  
  641.     ShowWarning(msg)
  642.  
  643.   end
  644.  
  645.   repeat
  646.  
  647.     local params = { os.pullEventRaw() }
  648.  
  649.     local eventName = params[1]
  650.  
  651.     local address = params[2]
  652.  
  653.     if address == nil then address = "none" end
  654.  
  655.     if eventName == "key" then
  656.  
  657.       local keycode = params[2]
  658.  
  659.       if keycode == 21 then -- Y
  660.  
  661.         return true
  662.  
  663.       else
  664.  
  665.         return false
  666.  
  667.       end
  668.  
  669.     elseif eventName == "terminate" then
  670.  
  671.       return false
  672.  
  673.     elseif not common_event(eventName, params[2]) then
  674.  
  675.       ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  676.  
  677.     end
  678.  
  679.   until false
  680.  
  681. end
  682.  
  683.  
  684.  
  685. ----------- commons: menu, event handlers, etc.
  686.  
  687.  
  688.  
  689. function common_event(eventName, param)
  690.  
  691.   if eventName == "redstone" then
  692.  
  693.     redstone_event(param)
  694.  
  695.   elseif eventName == "timer" then
  696.  
  697.   elseif eventName == "shipCoreCooldownDone" then
  698.  
  699.     ShowWarning("Ship core cooldown done")
  700.  
  701.   elseif eventName == "char" then
  702.  
  703.   elseif eventName == "key_up" then
  704.  
  705.   elseif eventName == "mouse_click" then
  706.  
  707.   elseif eventName == "mouse_up" then
  708.  
  709.   elseif eventName == "mouse_drag" then
  710.  
  711.   elseif eventName == "monitor_touch" then
  712.  
  713.   elseif eventName == "monitor_resize" then
  714.  
  715.   elseif eventName == "peripheral" then
  716.  
  717.   elseif eventName == "peripheral_detach" then
  718.  
  719.   else
  720.  
  721.     return false
  722.  
  723.   end
  724.  
  725.   return true
  726.  
  727. end
  728.  
  729.  
  730.  
  731. function menu_common()
  732.  
  733.   SetCursorPos(1, 18)
  734.  
  735.   SetColorTitle()
  736.  
  737.   ShowMenu("0 Connections, 1 Ship core, X Exit")
  738.  
  739. end
  740.  
  741.  
  742.  
  743. ----------- Redstone support
  744.  
  745.  
  746.  
  747. local tblRedstoneState = {-- Remember redstone state on each side
  748.  
  749.   ["top"] = rs.getInput("top"),
  750.  
  751.   ["front"] = rs.getInput("front"),
  752.  
  753.   ["left"] = rs.getInput("left"),
  754.  
  755.   ["right"] = rs.getInput("right"),
  756.  
  757.   ["back"] = rs.getInput("back"),
  758.  
  759.   ["bottom"] = rs.getInput("bottom"),
  760.  
  761. }
  762.  
  763. local tblSides = {-- list all sides and offset coordinates
  764.  
  765.   ["top"   ] = { 3, 1},
  766.  
  767.   ["front" ] = { 1, 3},
  768.  
  769.   ["left"  ] = { 3, 3},
  770.  
  771.   ["right" ] = { 5, 3},
  772.  
  773.   ["back"  ] = { 5, 5},
  774.  
  775.   ["bottom"] = { 3, 5},
  776.  
  777. }
  778.  
  779.  
  780.  
  781. function redstone_event()
  782.  
  783.   -- Event only returns nil so we need to check sides manually
  784.  
  785.   local message = ""
  786.  
  787.   for side, state in pairs(tblRedstoneState) do
  788.  
  789.     if rs.getInput(side) ~= state then
  790.  
  791.       -- print(side .. " is now " .. tostring(rs.getInput(side)))
  792.  
  793.       message = message .. side .. " "
  794.  
  795.       tblRedstoneState[side] = rs.getInput(side)
  796.  
  797.     end
  798.  
  799.   end
  800.  
  801.   if message ~= "" then
  802.  
  803.     message = "Redstone changed on " .. message
  804.  
  805.     showWarning(message)
  806.  
  807.   end
  808.  
  809. end
  810.  
  811.  
  812.  
  813. ----------- Configuration
  814.  
  815.  
  816.  
  817. function data_save()
  818.  
  819.   local file = fs.open("shipdata.txt", "w")
  820.  
  821.   if file ~= nil then
  822.  
  823.     file.writeLine(textutils.serialize(data))
  824.  
  825.     file.close()
  826.  
  827.   else
  828.  
  829.     ShowWarning("No file system")
  830.  
  831.     os.sleep(3)
  832.  
  833.   end
  834.  
  835. end
  836.  
  837.  
  838.  
  839. function data_read()
  840.  
  841.   data = { }
  842.  
  843.   if fs.exists("shipdata.txt") then
  844.  
  845.     local file = fs.open("shipdata.txt", "r")
  846.  
  847.     data = textutils.unserialize(file.readAll())
  848.  
  849.     file.close()
  850.  
  851.     if data == nil then data = {}; end
  852.  
  853.   end
  854.  
  855.   if data.core_summon == nil then data.core_summon = false; end
  856.  
  857. end
  858.  
  859.  
  860.  
  861. function data_setName()
  862.  
  863.   if ship ~= nil then
  864.  
  865.     ShowTitle("<==== Set ship name ====>")
  866.  
  867.   else
  868.  
  869.     ShowTitle("<==== Set name ====>")
  870.  
  871.   end
  872.  
  873.  
  874.  
  875.   SetCursorPos(1, 2)
  876.  
  877.   Write("Enter ship name: ")
  878.  
  879.   label = readInputText(label)
  880.  
  881.   os.setComputerLabel(label)
  882.  
  883.   if ship ~= nil then
  884.  
  885.     ship.coreFrequency(label)
  886.  
  887.   end
  888.  
  889.   os.reboot()
  890.  
  891. end
  892.  
  893.  
  894.  
  895. function string_split(source, sep)
  896.  
  897.   local sep = sep or ":"
  898.  
  899.   local fields = {}
  900.  
  901.   local pattern = string.format("([^%s]+)", sep)
  902.  
  903.   source:gsub(pattern, function(c) fields[#fields + 1] = c end)
  904.  
  905.   return fields
  906.  
  907. end
  908.  
  909.  
  910.  
  911. ----------- Ship support
  912.  
  913.  
  914.  
  915. source = {0,1,0}
  916.  
  917. dest = {0,1,0}
  918.  
  919. lock_power = 0
  920.  
  921. power_boost = 0
  922.  
  923. energy_required = 0
  924.  
  925.  
  926.  
  927.  
  928. function transporterBoot()
  929.  
  930.  
  931.  
  932.  
  933.   Write("Booting Transporter")
  934.  
  935.  
  936.  
  937.  
  938.  
  939.   WriteLn("...")
  940.  
  941.   energy_required = transporter.getEnergyRequired()
  942.  
  943.   lock_power = transporter.lockStrength()
  944.   transporter_source = { transporter.source() }
  945.   transporter_dest = { transporter.dest() }
  946.  
  947. function core_writeMovement()
  948.  
  949.   local message = " Movement         = "
  950.  
  951.   local count = 0
  952.  
  953.   if core_movement[1] > 0 then
  954.  
  955.     message = message .. core_movement[1] .. " front"
  956.  
  957.     count = count + 1
  958.  
  959.   elseif core_movement[1] < 0 then
  960.  
  961.     message = message .. (- core_movement[1]) .. " back"
  962.  
  963.     count = count + 1
  964.  
  965.   end
  966.  
  967.   if core_movement[2] > 0 then
  968.  
  969.     if count > 0 then message = message .. ", "; end
  970.  
  971.     message = message .. core_movement[2] .. " up"
  972.  
  973.     count = count + 1
  974.  
  975.   elseif core_movement[2] < 0 then
  976.  
  977.     if count > 0 then message = message .. ", "; end
  978.  
  979.     message = message .. (- core_movement[2]) .. " down"
  980.  
  981.     count = count + 1
  982.  
  983.   end
  984.  
  985.   if core_movement[3] > 0 then
  986.  
  987.     if count > 0 then message = message .. ", "; end
  988.  
  989.     message = message .. core_movement[3] .. " right"
  990.  
  991.     count = count + 1
  992.  
  993.   elseif core_movement[3] < 0 then
  994.  
  995.     if count > 0 then message = message .. ", "; end
  996.  
  997.     message = message .. (- core_movement[3]) .. " left"
  998.  
  999.     count = count + 1
  1000.  
  1001.   end
  1002.  
  1003.  
  1004.  
  1005.   if core_rotationSteps == 1 then
  1006.  
  1007.     if count > 0 then message = message .. ", "; end
  1008.  
  1009.     message = message .. "Turn right"
  1010.  
  1011.     count = count + 1
  1012.  
  1013.   elseif core_rotationSteps == 2 then
  1014.  
  1015.     if count > 0 then message = message .. ", "; end
  1016.  
  1017.     message = message .. "Turn back"
  1018.  
  1019.     count = count + 1
  1020.  
  1021.   elseif core_rotationSteps == 3 then
  1022.  
  1023.     if count > 0 then message = message .. ", "; end
  1024.  
  1025.     message = message .. "Turn left"
  1026.  
  1027.     count = count + 1
  1028.  
  1029.   end
  1030.  
  1031.  
  1032.  
  1033.   if count == 0 then
  1034.  
  1035.     message = message .. "(none)"
  1036.  
  1037.   end
  1038.  
  1039.   WriteLn(message)
  1040.  
  1041. end
  1042.  
  1043.  
  1044.  
  1045. function core_writeRotation()
  1046.  
  1047.   if core_rotationSteps == 0 then
  1048.  
  1049.     WriteLn(" Rotation         = Front")
  1050.  
  1051.   elseif core_rotationSteps == 1 then
  1052.  
  1053.     WriteLn(" Rotation         = Right +90")
  1054.  
  1055.   elseif core_rotationSteps == 2 then
  1056.  
  1057.     WriteLn(" Rotation         = Back 180")
  1058.  
  1059.   elseif core_rotationSteps == 3 then
  1060.  
  1061.     WriteLn(" Rotation         = Left -90")
  1062.  
  1063.   end
  1064.  
  1065. end
  1066.  
  1067.  
  1068.  
  1069. function core_computeNewCoordinates(cx, cy, cz)
  1070.  
  1071.   local res = { x = cx, y = cy, z = cz }
  1072.  
  1073.   local dx, dy, dz = ship.getOrientation()
  1074.  
  1075.   local worldMovement = { x = 0, y = 0, z = 0 }
  1076.  
  1077.   worldMovement.x = dx * core_movement[1] - dz * core_movement[3]
  1078.  
  1079.   worldMovement.y = core_movement[2]
  1080.  
  1081.   worldMovement.z = dz * core_movement[1] + dx * core_movement[3]
  1082.  
  1083.   core_actualDistance = math.ceil(math.sqrt(worldMovement.x * worldMovement.x + worldMovement.y * worldMovement.y + worldMovement.z * worldMovement.z))
  1084.  
  1085.   energy_required = transporter.energyRequired()
  1086.  
  1087.   res.x = res.x + worldMovement.x
  1088.  
  1089.   res.y = res.y + worldMovement.y
  1090.  
  1091.   res.z = res.z + worldMovement.z
  1092.  
  1093.   return res
  1094.  
  1095. end
  1096.  
  1097.  
  1098.  
  1099. function core_warp()
  1100.  
  1101.   -- rs.setOutput(alarm_side, true)
  1102.  
  1103.   if readConfirmation() then
  1104.  
  1105.     -- rs.setOutput(alarm_side, false)
  1106.  
  1107.     transporter.source(transporter_source[1], transporter_source[2], transporter_source[3])
  1108.     transporter.dest(transporter_dest[1], transporter_dest[2], transporter_dest[3])
  1109.     transporter.energize()
  1110.  
  1111.     -- ship = nil
  1112.  
  1113.   end
  1114.  
  1115.   -- rs.setOutput(alarm_side, false)
  1116.  
  1117. end
  1118.  
  1119.  
  1120.  
  1121. function core_page_setMovement()
  1122.  
  1123.   ShowTitle("<==== Set dest ====>")
  1124.  
  1125.   SetCursorPos(1, 15)
  1126.  
  1127.   SetColorTitle()
  1128.  
  1129.   ShowMenu("Enter 0 to keep position on that axis")
  1130.  
  1131.   ShowMenu("Use - or n keys to move in opposite direction")
  1132.  
  1133.   ShowMenu("Press Enter to confirm")
  1134.  
  1135.   SetColorDefault()
  1136.  
  1137.   SetCursorPos(1, 3)
  1138.  
  1139.  
  1140.  
  1141.   core_movement[1] = core_page_setDistanceAxis(2, "Forward" , "Front", "Back", core_movement[1], math.abs(core_front + core_back + 1))
  1142.  
  1143.   core_movement[2] = core_page_setDistanceAxis(4, "Vertical", "Up"   , "Down", core_movement[2], math.abs(core_up + core_down + 1))
  1144.  
  1145.   core_movement[3] = core_page_setDistanceAxis(6, "Lateral" , "Right", "Left", core_movement[3], math.abs(core_left + core_right + 1))
  1146.  
  1147.   core_movement = { ship.movement(core_movement[1], core_movement[2], core_movement[3]) }
  1148.  
  1149. end
  1150.  
  1151.  
  1152.  
  1153. function core_page_setDistanceAxis(line, axis, positive, negative, userEntry, shipLength)
  1154.  
  1155.   local maximumDistance = shipLength + 127
  1156.  
  1157.   if core_isInHyper and line ~= 3 then
  1158.  
  1159.     maximumDistance = shipLength + 127 * 100
  1160.  
  1161.   end
  1162.  
  1163.   SetColorDisabled()
  1164.  
  1165.   SetCursorPos(3, line + 1)
  1166.  
  1167.   Write(positive .. " is " .. ( shipLength + 1) .. ", maximum is " ..  maximumDistance .. "      ")
  1168.  
  1169.   SetCursorPos(3, line + 2)
  1170.  
  1171.   Write(negative .. " is " .. (-shipLength - 1) .. ", maximum is " .. -maximumDistance .. "      ")
  1172.  
  1173.  
  1174.  
  1175.   SetColorDefault()
  1176.  
  1177.   repeat
  1178.  
  1179.     SetCursorPos(1, line)
  1180.  
  1181.     Write(axis .. " movement: ")
  1182.  
  1183.     userEntry = readInputNumber(userEntry)
  1184.  
  1185.     if userEntry ~= 0 and (math.abs(userEntry) <= shipLength or math.abs(userEntry) > maximumDistance) then
  1186.  
  1187.       ShowWarning("Wrong distance. Try again.")
  1188.  
  1189.     end
  1190.  
  1191.   until userEntry == 0 or (math.abs(userEntry) > shipLength and math.abs(userEntry) <= maximumDistance)
  1192.  
  1193.   SetCursorPos(1, line + 1)
  1194.  
  1195.   ClearLine()
  1196.  
  1197.   SetCursorPos(1, line + 2)
  1198.  
  1199.   ClearLine()
  1200.  
  1201.  
  1202.  
  1203.   return userEntry
  1204.  
  1205. end
  1206.  
  1207.  
  1208.  
  1209.  
  1210. function core_page()
  1211.  
  1212.   ShowTitle(label .. " - Transporter status")
  1213.  
  1214.   if ship ~= nil then
  1215.  
  1216.     -- WriteLn("")
  1217.  
  1218.     X, Y, Z = transporter.source()
  1219.  
  1220.     WriteLn("Source:")
  1221.  
  1222.     WriteLn(" x, y, z          = " .. X .. ", " .. Y .. ", " .. Z)
  1223.    
  1224.     X, Y, Z = transporter.dest()
  1225.  
  1226.     WriteLn("Destination:")
  1227.  
  1228.     WriteLn(" x, y, z          = " .. X .. ", " .. Y .. ", " .. Z)
  1229.  
  1230.     local energy_required = transporter.energyRequired()
  1231.  
  1232.  
  1233.     WriteLn(" Energy           = " .. energy_required .. " % (" .. energy .. "EU)")
  1234.  
  1235.  
  1236.     -- WriteLn("")
  1237.     -- WriteLn("")
  1238.  
  1239.     WriteLn("Transport data:")
  1240.  
  1241.     core_writeMovement()
  1242.  
  1243.     local dest = core_computeNewCoordinates(X, Y, Z)
  1244.  
  1245.     WriteLn(" Distance         = " .. core_actualDistance .. " (" .. core_jumpCost .. "EU, " .. math.floor(energy / core_jumpCost) .. " jumps)")
  1246.  
  1247.     WriteLn(" Dest.coordinates = " .. FormatInteger(dest.x) .. ", " .. FormatInteger(dest.y) .. ", " .. FormatInteger(dest.z))
  1248.  
  1249.     if data.core_summon then
  1250.  
  1251.       WriteLn(" Summon after     = Yes")
  1252.  
  1253.     else
  1254.  
  1255.       WriteLn(" Summon after     = No")
  1256.  
  1257.     end
  1258.  
  1259.   else
  1260.  
  1261.     ShowWarning("No ship controller detected")
  1262.  
  1263.   end
  1264.  
  1265.  
  1266.  
  1267.   SetCursorPos(1, 15)
  1268.  
  1269.   SetColorTitle()
  1270.  
  1271.   ShowMenu("D - Dimensions, N - set ship Name, M - set Movement")
  1272.  
  1273.   ShowMenu("J - Jump, G - jump through Gate, B - jump to Beacon")
  1274.  
  1275.   ShowMenu("H - Hyperspace, C - summon Crew, T - Toggle summon")
  1276.  
  1277. end
  1278.  
  1279.  
  1280.  
  1281. function core_key(char, keycode)
  1282.  
  1283.   if keycode == 50 then -- M
  1284.  
  1285.     core_page_setMovement()
  1286.  
  1287.     core_page_setRotation()
  1288.  
  1289.     return true
  1290.  
  1291.   elseif keycode == 20 then -- T
  1292.  
  1293.     if data.core_summon then
  1294.  
  1295.       data.core_summon = false
  1296.  
  1297.     else
  1298.  
  1299.       data.core_summon = true
  1300.  
  1301.     end
  1302.  
  1303.     data_save()
  1304.  
  1305.     return true
  1306.  
  1307.   elseif keycode == 32 then -- D
  1308.  
  1309.     core_page_setDimensions()
  1310.  
  1311.     return true
  1312.  
  1313.   elseif keycode == 36 then -- J
  1314.  
  1315.     core_warp()
  1316.  
  1317.     return true
  1318.  
  1319.   elseif keycode == 46 then -- C
  1320.  
  1321.     core_page_summon()
  1322.  
  1323.     return true
  1324.  
  1325.   elseif keycode == 48 then -- B
  1326.  
  1327.     core_page_jumpToBeacon()
  1328.  
  1329.     return true
  1330.  
  1331.   elseif keycode == 34 then -- G
  1332.  
  1333.     core_page_jumpToGate()
  1334.  
  1335.     return true
  1336.  
  1337.   elseif keycode == 35 then -- H
  1338.  
  1339.     -- rs.setOutput(alarm_side, true)
  1340.  
  1341.     if readConfirmation() then
  1342.  
  1343.       -- rs.setOutput(alarm_side, false)
  1344.  
  1345.       ship.mode(5)
  1346.  
  1347.       ship.jump()
  1348.  
  1349.       -- ship = nil
  1350.  
  1351.     end
  1352.  
  1353.     -- rs.setOutput(alarm_side, false)
  1354.  
  1355.     return true
  1356.  
  1357.   elseif keycode == 49 then
  1358.  
  1359.     data_setName()
  1360.  
  1361.     return true
  1362.  
  1363.   end
  1364.  
  1365.   return false
  1366.  
  1367. end
  1368.  
  1369.  
  1370.  
  1371. ----------- Boot sequence
  1372.  
  1373. math.randomseed(os.time())
  1374.  
  1375. label = os.getComputerLabel()
  1376.  
  1377. if not label then
  1378.  
  1379.   label = "" .. os.getComputerID()
  1380.  
  1381. end
  1382.  
  1383.  
  1384.  
  1385. -- read configuration
  1386.  
  1387. data_read()
  1388.  
  1389. Clear()
  1390.  
  1391. print("data_read...")
  1392.  
  1393.  
  1394.  
  1395. -- initial scanning
  1396.  
  1397. monitors = {}
  1398.  
  1399. ShowTitle(label .. " - Connecting...")
  1400.  
  1401. WriteLn("")
  1402.  
  1403.  
  1404.  
  1405. sides = peripheral.getNames()
  1406.  
  1407. ship = nil
  1408.  
  1409. for key,side in pairs(sides) do
  1410.  
  1411.   os.sleep(0)
  1412.  
  1413.   Write("Checking " .. side .. " ")
  1414.  
  1415.   local componentType = peripheral.getType(side)
  1416.  
  1417.   Write(componentType .. " ")
  1418.  
  1419.   if componentType == "monitor" then
  1420.  
  1421.     Write("wrapping!")
  1422.  
  1423.     lmonitor = peripheral.wrap(side)
  1424.  
  1425.     table.insert(monitors, lmonitor)
  1426.  
  1427.     lmonitor.setTextScale(monitor_textScale)
  1428.  
  1429.   elseif componentType == "warpdriveShipController" then
  1430.  
  1431.     Write("wrapping!")
  1432.  
  1433.     ship = peripheral.wrap(side)
  1434.  
  1435.   end
  1436.  
  1437.   WriteLn("")
  1438.  
  1439. end
  1440.  
  1441.  
  1442.  
  1443. if not os.getComputerLabel() and ship ~= nil then
  1444.  
  1445.   data_setName()
  1446.  
  1447. end
  1448.  
  1449.  
  1450.  
  1451. -- peripherals status
  1452.  
  1453. function connections_page()
  1454.  
  1455.   ShowTitle(label .. " - Connections")
  1456.  
  1457.  
  1458.  
  1459.   WriteLn("")
  1460.  
  1461.   if ship == nil then
  1462.  
  1463.     SetColorDisabled()
  1464.  
  1465.     WriteLn("No ship controller detected")
  1466.  
  1467.   else
  1468.  
  1469.     SetColorSuccess()
  1470.  
  1471.     WriteLn("Ship controller detected")
  1472.  
  1473.   end
  1474.  
  1475.  
  1476.  
  1477.   WriteLn("")
  1478.  
  1479.   SetColorTitle()
  1480.  
  1481.   WriteLn("Please refer to below menu for keyboard controls")
  1482.  
  1483.   WriteLn("For example, press 1 to access Ship core page")
  1484.  
  1485. end
  1486.  
  1487.  
  1488.  
  1489. -- peripheral boot up
  1490.  
  1491. Clear()
  1492.  
  1493. connections_page()
  1494.  
  1495. SetColorDefault()
  1496.  
  1497. WriteLn("")
  1498.  
  1499. os.sleep(0)
  1500.  
  1501. core_boot()
  1502.  
  1503. os.sleep(0)
  1504.  
  1505.  
  1506.  
  1507. -- main loop
  1508.  
  1509. abort = false
  1510.  
  1511. refresh = true
  1512.  
  1513. page = connections_page
  1514.  
  1515. keyHandler = nil
  1516.  
  1517. repeat
  1518.  
  1519.   ClearWarning()
  1520.  
  1521.   if refresh then
  1522.  
  1523.     Clear()
  1524.  
  1525.     page()
  1526.  
  1527.     menu_common()
  1528.  
  1529.     refresh = false
  1530.  
  1531.   end
  1532.  
  1533.   params = { os.pullEventRaw() }
  1534.  
  1535.   eventName = params[1]
  1536.  
  1537.   address = params[2]
  1538.  
  1539.   if address == nil then address = "none" end
  1540.  
  1541.   -- WriteLn("...")
  1542.  
  1543.   -- WriteLn("Event '" .. eventName .. "', " .. address .. ", " .. params[3] .. ", " .. params[4] .. " received")
  1544.  
  1545.   -- os.sleep(0.2)
  1546.  
  1547.   if eventName == "key" then
  1548.  
  1549.     keycode = params[2]
  1550.  
  1551.     if char == 88 or char == 120 or keycode == 45 then -- x for eXit
  1552.  
  1553.       os.pullEventRaw()
  1554.  
  1555.       abort = true
  1556.  
  1557.     elseif char == 48 or keycode == 11 or keycode == 82 then -- 0
  1558.  
  1559.       page = connections_page
  1560.  
  1561.       keyHandler = nil
  1562.  
  1563.       refresh = true
  1564.  
  1565.     elseif char == 49 or keycode == 2 or keycode == 79 then -- 1
  1566.  
  1567.       page = core_page
  1568.  
  1569.       keyHandler = core_key
  1570.  
  1571.       refresh = true
  1572.  
  1573.     elseif keyHandler ~= nil and keyHandler(char, keycode) then
  1574.  
  1575.       refresh = true
  1576.  
  1577.       os.sleep(0)
  1578.  
  1579.     elseif char == 0 then -- control chars
  1580.  
  1581.       refresh = false
  1582.  
  1583.       os.sleep(0)
  1584.  
  1585.     else
  1586.  
  1587.       ShowWarning("Key " .. keycode .. " is invalid")
  1588.  
  1589.       os.sleep(0.2)
  1590.  
  1591.     end
  1592.  
  1593.   elseif eventName == "terminate" then
  1594.  
  1595.     abort = true
  1596.  
  1597.   elseif not common_event(eventName, params[2]) then
  1598.  
  1599.     ShowWarning("Event '" .. eventName .. "', " .. address .. " is unsupported")
  1600.  
  1601.     refresh = true
  1602.  
  1603.     os.sleep(0.2)
  1604.  
  1605.   end
  1606.  
  1607. until abort
  1608.  
  1609.  
  1610.  
  1611. -- exiting
  1612.  
  1613. if data.core_summon then
  1614.  
  1615.   data.core_summon = false
  1616.  
  1617.   data_save()
  1618.  
  1619. end
  1620.  
  1621.  
  1622.  
  1623. if ship ~= nil then
  1624.  
  1625.   ship.mode(0)
  1626.  
  1627. end
  1628.  
  1629.  
  1630.  
  1631. -- clear screens on exit
  1632.  
  1633. SetMonitorColorFrontBack(colors.white, colors.black)
  1634.  
  1635. term.clear()
  1636.  
  1637. if monitors ~= nil then
  1638.  
  1639.   for key,monitor in pairs(monitors) do
  1640.  
  1641.     monitor.clear()
  1642.  
  1643.   end
  1644.  
  1645. end
  1646.  
  1647. SetCursorPos(1, 1)
  1648.  
  1649. WriteLn("Program terminated")
  1650.  
  1651. WriteLn("Type startup to restart it")
Add Comment
Please, Sign In to add comment