Advertisement
giintv

JumpDrive 1.6.2

Nov 11th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.96 KB | None | 0 0
  1.    
  2.  
  3.     print("loading...")
  4.      
  5.     -- set alarm side if you need this
  6.     Alarm = "top"
  7.      
  8.     Style = {
  9.      CDeflt = colors.white,
  10.      BGDeflt = colors.blue,
  11.      CTitle = colors.black,
  12.      BGTitle = colors.cyan,
  13.      CWarn = colors.white,
  14.      BGWarn = colors.red
  15.     }
  16.      
  17.     function SetColorDeflt()
  18.      term.setBackgroundColor(Style.BGDeflt)
  19.      term.setTextColor(Style.CDeflt)
  20.     end
  21.      
  22.     function SetColorTitle()
  23.      term.setBackgroundColor(Style.BGTitle)
  24.      term.setTextColor(Style.CTitle)
  25.     end
  26.      
  27.     function SetColorWarn()
  28.      term.setBackgroundColor(Style.BGWarn)
  29.      term.setTextColor(Style.CWarn)
  30.     end
  31.      
  32.     function Clear()
  33.      term.clear()
  34.      term.setCursorPos(1,1)
  35.     end
  36.      
  37.     function Show(Text)
  38.      term.write(Text)
  39.      local xt,yt = term.getCursorPos()
  40.      term.setCursorPos(1, yt+1)
  41.     end
  42.      
  43.     function ShowTitle(Text)
  44.      SetColorTitle()
  45.      term.setCursorPos(12, 1)
  46.      Show(Text)
  47.      SetColorDeflt()
  48.     end
  49.      
  50.     function ShowMenu(Text)
  51.      term.write(Text)
  52.      local xt, yt = term.getCursorPos()
  53.      for i = xt, 51 do
  54.       term.write(" ")
  55.      end
  56.      term.setCursorPos(1, yt+1)
  57.     end
  58.      
  59.     function ShowWarning(Text)
  60.       SetColorWarn()
  61.       term.setCursorPos(10, 19)
  62.       term.write(" "..Text.." ")
  63.       SetColorDeflt()
  64.     end
  65.      
  66.     function SaveData()
  67.      local file = fs.open("shipdata.txt", "w")
  68.      file.writeLine(textutils.serialize(SData))
  69.      file.close()
  70.     end
  71.      
  72.     function ReadData()
  73.      local file = fs.open("shipdata.txt", "r")
  74.      SData = textutils.unserialize(file.readAll())
  75.      file.close()
  76.     end
  77.      
  78.     function Explode(d, p)
  79.      local t, ll
  80.      t = {}
  81.      ll = 0
  82.      if(#p == 1) then return {p} end
  83.      while true do
  84.       l = string.find(p ,d, ll, true)
  85.       if l ~= nil then
  86.        table.insert(t, string.sub(p, ll, l-1))
  87.        ll = l+1
  88.       else
  89.        table.insert(t, string.sub(p, ll))
  90.        break
  91.       end
  92.      end
  93.      return t
  94.     end
  95.      
  96.     function ShowDirection()
  97.      if SData.Direction == 1 then
  98.       Show(" Direction        = Up")
  99.      elseif SData.Direction == 2 then
  100.       Show(" Direction        = Down")
  101.      elseif SData.Direction == 0 then
  102.       Show(" Direction        = Front")
  103.      elseif SData.Direction == 180 then
  104.       Show(" Direction        = Back")
  105.      elseif SData.Direction == 90 then
  106.       Show(" Direction        = Left")
  107.      elseif SData.Direction == 255 then
  108.       Show(" Direction        = Right")
  109.      end
  110.     end
  111.      
  112.     function CalcRealDistance()
  113.      if IsInHyper then
  114.       RealDistance = SData.Distance * 100
  115.       MinimumDistance = 1
  116.       JumpCost = (1000 * Weight) + (1000 * SData.Distance)
  117.      else
  118.       if SData.Direction == 1 or SData.Direction == 2 then
  119.        MinimumDistance = GUp + GDown
  120.        RealDistance = SData.Distance + MinimumDistance
  121.       elseif SData.Direction == 0 or SData.Direction == 180 then
  122.        MinimumDistance = GFront + GBack
  123.        RealDistance = SData.Distance + MinimumDistance
  124.       elseif SData.Direction == 90 or SData.Direction == 255 then
  125.        MinimumDistance = GLeft + GRight
  126.        RealDistance = SData.Distance + MinimumDistance
  127.       end
  128.       MinimumDistance = MinimumDistance + 1
  129.       JumpCost = (10 * Weight) + (100 * SData.Distance)
  130.      end
  131.     end
  132.      
  133.     function CalcNewCoords(cx, cy, cz)
  134.      local res = {x=cx, y=cy, z=cz}
  135.      if SData.Direction == 1 then
  136.       res.y = res.y + RealDistance
  137.      elseif SData.Direction == 2 then
  138.       res.y = res.y - RealDistance
  139.      end
  140.      local dx = warp.get_dx()
  141.      local dz = warp.get_dz()
  142.      if dx ~= 0 then
  143.       if SData.Direction == 0 then
  144.        res.x = res.x + (RealDistance * dx)
  145.       elseif SData.Direction == 180 then
  146.        res.x = res.x - (RealDistance * dx)
  147.       elseif SData.Direction == 90 then
  148.        res.z = res.z + (RealDistance * dx)
  149.       elseif SData.Direction == 255 then
  150.        res.z = res.z - (RealDistance * dx)
  151.       end
  152.      else
  153.       if SData.Direction == 0 then
  154.        res.z = res.z + (RealDistance * dz)
  155.       elseif SData.Direction == 180 then
  156.        res.z = res.z - (RealDistance * dz)
  157.       elseif SData.Direction == 90 then
  158.        res.x = res.x + (RealDistance * dz)
  159.       elseif SData.Direction == 255 then
  160.        res.x = res.x - (RealDistance * dz)
  161.       end
  162.      end
  163.      return res
  164.     end
  165.      
  166.     function ShowInfo()
  167.      ShowTitle(Title)
  168.      Show("Core:")
  169.      Show(" x, y, z          = "..X..", "..Y..", "..Z)
  170.      local energy = warp.get_energy_level()
  171.      Show(" Energy           = "..math.floor(energy / 1000000).." % ("..energy.."EU)")
  172.      Show(" Attached players = "..warp.get_attached_players())
  173.      Show("Dimensions:")
  174.      Show(" Front, Right, Up = "..GFront..", "..GRight..", "..GUp)
  175.      Show(" Back, Left, Down = "..GBack..", "..GLeft..", "..GDown)
  176.      Show(" Size             = "..Weight.." blocks")
  177.      Show("Warp data:")
  178.      ShowDirection()
  179.      local dest = CalcNewCoords(X, Y, Z)
  180.      Show(" Distance         = "..RealDistance.." ("..JumpCost.."EU, "..math.floor(energy/JumpCost).." jumps)")
  181.      Show(" Dest.coordinates = "..dest.x..", "..dest.y..", "..dest.z)
  182.      if SData.Summon then
  183.       Show(" Summon after     = Yes")
  184.      else
  185.       Show(" Summon after     = No")
  186.      end
  187.     end
  188.      
  189.     function Confirm()
  190.      ShowWarning("Are you sure? (y/n)")
  191.      local event, keycode = os.pullEvent("key")
  192.      if keycode == 21 then
  193.       return true
  194.      else
  195.       return false
  196.      end
  197.     end
  198.      
  199.     function Warp()
  200.      rs.setOutput(Alarm, false)
  201.      sleep(1)
  202.      warp.set_direction(SData.Direction)
  203.      if IsInHyper then
  204.       warp.set_mode(2)
  205.      else
  206.       warp.set_mode(1)
  207.      end
  208.      warp.do_jump()
  209.     end
  210.      
  211.     function SetDistance()
  212.      Clear()
  213.      ShowTitle("<====  Set distance  ====>")
  214.      SData.Distance = 0
  215.      CalcRealDistance()
  216.      MaximumDistance = MinimumDistance + 127
  217.      if IsInHyper then
  218.       term.write("Distance * 100 (min "..MinimumDistance..", max "..MaximumDistance.."): ")
  219.      else
  220.       term.write("Distance (min "..MinimumDistance..", max "..MaximumDistance.."): ")
  221.      end
  222.      sleep(0.3)
  223.      SData.Distance = tonumber(read())
  224.      if SData.Distance == nil then SData.Distance = 1 end
  225.      if SData.Distance < MinimumDistance or SData.Distance > MaximumDistance then
  226.       SData.Distance = 1
  227.       ShowWarning("Wrong distance. Try again.")
  228.       os.pullEvent("key")
  229.       CalcRealDistance()
  230.      else
  231.       if not IsInHyper then
  232.        SData.Distance = SData.Distance - RealDistance
  233.       end
  234.       warp.set_distance(SData.Distance)
  235.       CalcRealDistance()
  236.      end
  237.     end
  238.      
  239.     function SetDirection()
  240.      local drun = true
  241.      while(drun) do
  242.       Clear()
  243.       ShowTitle("<==== Set direction ====>")
  244.       ShowDirection()
  245.       term.setCursorPos(1, 16)
  246.       SetColorTitle()
  247.       ShowMenu("Use directional keys")
  248.       ShowMenu("W/S keys for Up/Down")
  249.       ShowMenu("Enter - confirm")
  250.       SetColorDeflt()
  251.       local event, keycode = os.pullEvent("key")
  252.       if keycode == 200 then
  253.        SData.Direction = 0
  254.       elseif keycode == 17 then
  255.        SData.Direction = 1
  256.       elseif keycode == 203 then
  257.        SData.Direction = 90
  258.       elseif keycode == 205 then
  259.        SData.Direction = 255
  260.       elseif keycode == 208 then
  261.        SData.Direction = 180
  262.       elseif keycode == 31 then
  263.        SData.Direction = 2
  264.       elseif keycode == 28 then
  265.        drun = false
  266.       end
  267.      end
  268.     end
  269.      
  270.     function SetDimensions()
  271.      Clear()
  272.      sleep(0.3)
  273.      ShowTitle("<==== Set dimensions ====>")
  274.      term.write(" Front ("..GFront..") : ")
  275.      GFront = tonumber(read())
  276.      term.write(" Right ("..GRight..") : ")
  277.      GRight = tonumber(read())
  278.      term.write(" Up    ("..GUp..") : ")
  279.      GUp = tonumber(read())
  280.      term.write(" Back  ("..GBack..") : ")
  281.      GBack = tonumber(read())
  282.      term.write(" Left  ("..GLeft..") : ")
  283.      GLeft = tonumber(read())
  284.      term.write(" Down  ("..GDown..") : ")
  285.      GDown = tonumber(read())
  286.      term.write("Setting dimensions...")
  287.      warp.dim_setp(GFront, GRight, GUp)
  288.      warp.dim_setn(GBack, GLeft, GDown)
  289.     end
  290.      
  291.     function Summon()
  292.      Clear()
  293.      ShowTitle("<==== Summon players ====>")
  294.      local players = Explode(",", warp.get_attached_players())
  295.      for i = 1, #players do
  296.       Show(i..". "..players[i])
  297.      end
  298.      SetColorTitle()
  299.      ShowMenu("Enter player number")
  300.      ShowMenu("or press enter to summon everyone")
  301.      SetColorDeflt()
  302.      sleep(0.3)
  303.      term.write(":")
  304.      local input = read()
  305.      if input == "" then
  306.       warp.summon_all()
  307.      else
  308.       input = tonumber(input)
  309.       warp.summon(input - 1)
  310.      end
  311.     end
  312.      
  313.     function JumpToBeacon()
  314.      Clear()
  315.      ShowTitle("<==== Jump to beacon ====>")
  316.      sleep(0.3)
  317.      term.write("Enter beacon frequency: ")
  318.      local freq = tostring(read())
  319.      rs.setOutput(Alarm, true)
  320.      if Confirm() then
  321.       rs.setOutput(Alarm, false)
  322.       warp.set_mode(4)
  323.       warp.set_beacon_frequency(freq)
  324.       warp.do_jump()
  325.      end
  326.      rs.setOutput(Alarm, false)
  327.     end
  328.      
  329.     function JumpToGate()
  330.      Clear()
  331.      ShowTitle("<==== Jump to JumpGate ====>")
  332.      sleep(0.3)
  333.      term.write("Enter jumpgate name: ")
  334.      local name = tostring(read())
  335.      rs.setOutput(Alarm, true)
  336.      if Confirm() then
  337.       rs.setOutput(Alarm, false)
  338.       warp.set_mode(6)
  339.       warp.set_target_jumpgate(name)
  340.       warp.do_jump()
  341.      end
  342.      rs.setOutput(Alarm, false)
  343.     end
  344.      
  345.     function Turn()
  346.      SetDirection()
  347.      CalcRealDistance()
  348.      rs.setOutput(Alarm, true)
  349.      if Confirm() then
  350.       rs.setOutput(Alarm, false)
  351.       warp.set_mode(1)
  352.       warp.set_direction(SData.Direction)
  353.       warp.set_distance(MinimumDistance + 1)
  354.       warp.do_jump(1)  
  355.      end
  356.      rs.setOutput(Alarm, false)
  357.     end
  358.      
  359.     function SetShipName()
  360.      Clear()
  361.      ShowTitle("<==== Set ship name ====>")
  362.      sleep(0.3)
  363.      term.write("Enter ship name: ")
  364.      SData.Shipname = tostring(read())
  365.      os.setComputerLabel(SData.Shipname)
  366.      warp.set_core_frequency(SData.Shipname)
  367.      SaveData()
  368.      os.reboot()
  369.     end
  370.      
  371.     if fs.exists("shipdata.txt") then
  372.      ReadData()
  373.     else
  374.      SData = {
  375.       Summon = false,
  376.       Distance = 1,
  377.       Direction = 0,
  378.       Shipname = ""
  379.      }
  380.     end
  381.      
  382.     SetColorDeflt()
  383.      
  384.     Side = { "bottom", "top", "back", "left", "right" }
  385.     for i = 1,5 do
  386.      if peripheral.getType(Side[i]) == "warpcore" then
  387.       warp = peripheral.wrap(Side[i])
  388.       break
  389.      end
  390.     end
  391.      
  392.     if type(warp) ~= "table" then
  393.      ShowWarning("No warpcore controller detected")
  394.      os.pullEvent("key")
  395.      os.shutdown()
  396.     end
  397.      
  398.     if SData.Shipname == "" then
  399.      SetShipName()
  400.     end
  401.      
  402.     Title = "<Jump-S 1.6.2 \""..SData.Shipname.."\">"
  403.      
  404.     if SData.Summon then
  405.      warp.summon_all()
  406.     end
  407.      
  408.     GFront, GRight, GUp = warp.dim_getp()
  409.     GBack, GLeft, GDown = warp.dim_getn()
  410.     IsInHyper = warp.is_in_hyperspace()
  411.     repeat
  412.      X = warp.get_x()
  413.      sleep(0.3)
  414.     until X ~= nil
  415.     Y = warp.get_y()
  416.     Z = warp.get_z()
  417.     Weight = warp.get_ship_size()
  418.      
  419.     CalcRealDistance()
  420.      
  421.     warp.set_mode(1)
  422.      
  423.     mainloop = true
  424.     while(mainloop) do
  425.      Clear()
  426.      ShowInfo()
  427.      term.setCursorPos(1, 15)
  428.      SetColorTitle()
  429.      ShowMenu("D - Dimensions, M - Toggle summon, N - Ship name")
  430.      ShowMenu("S - Set Warp Data, J - Jump, G - Jump to JumpGate")
  431.      ShowMenu("B - Jump to Beacon, H -Jump to Hyperspace")
  432.      ShowMenu("C - Summon, T - Turn, X - Shutdown")
  433.      SetColorDeflt()
  434.      local event, keycode = os.pullEvent("key")
  435.      if keycode == 31 then
  436.       SetDirection()
  437.       SetDistance()
  438.       SaveData()
  439.      elseif keycode == 50 then
  440.       if SData.Summon then
  441.        SData.Summon = false
  442.       else
  443.        SData.Summon = true
  444.       end
  445.       SaveData()
  446.      elseif keycode == 32 then
  447.       SetDimensions()
  448.       SaveData()
  449.      elseif keycode == 36 then
  450.       rs.setOutput(Alarm, true)
  451.       if Confirm() then
  452.        Warp()
  453.       end
  454.       rs.setOutput(Alarm, false)
  455.      elseif keycode == 46 then
  456.       Summon()
  457.      elseif keycode == 48 then
  458.       JumpToBeacon()
  459.      elseif keycode == 34 then
  460.       JumpToGate()
  461.      elseif keycode == 35 then
  462.       rs.setOutput(Alarm, true)
  463.       if Confirm() then
  464.        rs.setOutput(Alarm, false)
  465.        warp.set_mode(5)
  466.        warp.do_jump()
  467.       end
  468.       rs.setOutput(Alarm, false)
  469.      elseif keycode == 20 then
  470.       Turn()
  471.      elseif keycode == 45 then
  472.       mainloop = false
  473.      elseif keycode == 49 then
  474.       SetShipName()
  475.      end
  476.     end
  477.      
  478.     if SData.Summon then
  479.      SData.Summon = false
  480.      SaveData()
  481.     end
  482.     Clear()
  483.     print("wish you good")
  484.     warp.set_mode(0)
  485.     sleep(0.5)
  486.     os.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement