pm2d

Jump-S 1.6.2

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