Advertisement
sanovskiy

Multicore Warp

Nov 16th, 2013
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 25.61 KB | None | 0 0
  1. version = "1.10.0 Multicore"
  2. -- set alarm side if you need this
  3. Alarm = "top"
  4. Style = {
  5.   CDeflt = colors.white,
  6.   BGDeflt = colors.gray,
  7.   CTitle = colors.black,
  8.   BGTitle = colors.lightGray,
  9.   CWarn = colors.white,
  10.   BGWarn = colors.red
  11. }
  12.  
  13. WARP_UP = 1
  14. WARP_DOWN = 2
  15. WARP_FORWARD = 0
  16. WARP_BACK = 180
  17. WARP_LEFT = 90
  18. WARP_RIGHT = 255
  19.  
  20. warp_names = {}
  21. warp_names[WARP_UP] = 'Up'
  22. warp_names[WARP_DOWN] = 'Down'
  23. warp_names[WARP_FORWARD] = 'Forward'
  24. warp_names[WARP_BACK] = 'Back'
  25. warp_names[WARP_LEFT] = 'Left'
  26. warp_names[WARP_RIGHT] = 'Right'
  27.  
  28. DIRECTION_SOUTH = 0
  29. DIRECTION_EAST = 90
  30. DIRECTION_NORTH = 180
  31. DIRECTION_WEST = 255
  32. DIRECTION_UP = 1
  33. DIRECTION_DOWN = 2
  34.  
  35. direction_names = {}
  36. direction_names[DIRECTION_SOUTH] = 'South'
  37. direction_names[DIRECTION_EAST] = 'East'
  38. direction_names[DIRECTION_NORTH] = 'North'
  39. direction_names[DIRECTION_WEST] = 'West'
  40. direction_names[DIRECTION_UP] = 'Up'
  41. direction_names[DIRECTION_DOWN] = 'Down'
  42.  
  43. function getDirectionName(dir)
  44.   return direction_names[dir]
  45. end
  46.  
  47. TYPE_WARP = "warpcore"
  48.  
  49. logger = fs.exists("disk")
  50. if logger then
  51.   file = fs.open("/disk/mcwarp.log","w")
  52.   file.close()
  53. end
  54.  
  55. function table.contains(table, element)
  56.   for _, value in pairs(table) do
  57.     if value == element then
  58.       return true
  59.     end
  60.   end
  61.   return false
  62. end
  63.  
  64. function hlog(data)
  65.   if fs.exists("disk") then
  66.     file = fs.open("/disk/mcwarp.log","a")
  67.     file.writeLine(data)
  68.     file.close()
  69.   end
  70. end
  71.  
  72. if true then -- for folding purpose
  73.   Key = {};
  74.   Key["backspace"] = 14
  75.   Key["tab"] = 15
  76.   Key["enter"] = 28
  77.   Key["lshift"] = 42
  78.   Key["lctrl"] = 29
  79.   Key["lalt"] = 56
  80.   Key["rshift"] = 54
  81.   Key["rctrl"] = 157
  82.   Key["ralt"] = 184
  83.   Key["pause"] = 197
  84.   Key["caps lock"] = 58
  85.   Key["esc"] = 1
  86.   Key["spacebar"] = 57
  87.   Key["page up"] = 201
  88.   Key["page down"] = 209
  89.   Key["end"] = 207
  90.   Key["home"] = 199
  91.   Key["left_arrow"] = 203
  92.   Key["up_arrow"] = 200
  93.   Key["right_arrow"] = 205
  94.   Key["down_arrow"] = 208
  95.   Key["insert"] = 210
  96.   Key["delete"] = 211
  97.   Key["0"] = 11
  98.   Key["1"] = 2
  99.   Key["2"] = 3
  100.   Key["3"] = 4
  101.   Key["4"] = 5
  102.   Key["5"] = 6
  103.   Key["6"] = 7
  104.   Key["7"] = 8
  105.   Key["8"] = 9
  106.   Key["9"] = 10
  107.   Key["a"] = 30
  108.   Key["b"] = 48
  109.   Key["c"] = 46
  110.   Key["d"] = 32
  111.   Key["e"] = 18
  112.   Key["f"] = 33
  113.   Key["g"] = 34
  114.   Key["h"] = 35
  115.   Key["i"] = 23
  116.   Key["j"] = 36
  117.   Key["k"] = 37
  118.   Key["l"] = 38
  119.   Key["m"] = 50
  120.   Key["n"] = 49
  121.   Key["o"] = 24
  122.   Key["p"] = 25
  123.   Key["q"] = 16
  124.   Key["r"] = 19
  125.   Key["s"] = 31
  126.   Key["t"] = 20
  127.   Key["u"] = 22
  128.   Key["v"] = 47
  129.   Key["w"] = 17
  130.   Key["x"] = 45
  131.   Key["y"] = 21
  132.   Key["z"] = 44
  133.   Key["lsuper"] = 219 -- Left Windows Key
  134.   Key["rsuper"] = 220 -- Right Windows Key
  135.   Key["menu"] = 221 -- Application key - located between the right Windows and Ctrl keys on most keyboards
  136.   -- Numpad numbers start
  137.   Key["num0"] = 82
  138.   Key["num1"] = 79
  139.   Key["num2"] = 80
  140.   Key["num3"] = 81
  141.   Key["num4"] = 75
  142.   Key["num5"] = 76
  143.   Key["num6"] = 77
  144.   Key["num7"] = 71
  145.   Key["num8"] = 72
  146.   Key["num9"] = 73
  147.   Key["num*"] = 55
  148.   Key["num+"] = 78
  149.   Key["num-"] = 74
  150.   Key["num."] = 83
  151.   Key["num/"] = 181
  152.   Key["numenter"] = 28
  153.   -- Numpad numbers end
  154.   Key["f1"] = 59
  155.   Key["f2"] = 60
  156.   Key["f3"] = 61
  157.   Key["f4"] = 62
  158.   Key["f5"] = 63
  159.   Key["f6"] = 64
  160.   Key["f7"] = 65
  161.   Key["f8"] = 66
  162.   Key["f9"] = 67
  163.   Key["f10"] = 68
  164.   Key["f11"] = 87
  165.   Key["f12"] = 88
  166.   Key["numlock"] = 69
  167.   Key["scrolllock"] = 70
  168.   Key[";"] = 39
  169.   Key["="] = 13
  170.   Key[","] = 51
  171.   Key["-"] = 12
  172.   Key["."] = 52
  173.   Key["/"] = 53
  174.   Key["'"] = 40
  175.   Key["["] = 26
  176.   Key["\\"] = 43
  177.   Key["]" ] = 27
  178.   Key["`"] = 41
  179. end
  180.  
  181. function drawSplash()
  182.   term.setBackgroundColor(colors.gray)
  183.   term.setTextColor(colors.white)
  184.   term.clear()
  185.   term.setCursorPos(1,4)
  186.   print("    __  ___      ____  _                    ")
  187.   print("   /  |/  /_  __/ / /_(_)________  ________ ")
  188.   print("  / /|_/ / / / / / __/ / ___/ __ \\/ ___/ _ \\")
  189.   print(" / /  / / /_/ / / /_/ / /__/ /_/ / /  /  __/")
  190.   print("/_/  /_/\\__,_/_//__/_/\\___/\\____/_/   \\___/ ")
  191.   print("      | |     / /___ __________  ___  _____ ")
  192.   print("      | | /| / / __ \\'/ ___/ __ \\/ _ \\/ ___/ ")
  193.   print("      | |/ |/ / /_/ / /  / /_/ /  __/ /     ")
  194.   print("      |__/|__/\\__,_/_/  / .___/\\___/_/      ")
  195.   print("                       /_/                  ")
  196.  
  197.   term.setBackgroundColor(colors.gray)
  198.   term.setTextColor(colors.white)
  199.   line = "                     LOADING                       "
  200.   term.setCursorPos(1,19)
  201.   term.write(line)
  202.   term.setBackgroundColor(colors.white)
  203.   term.setTextColor(colors.gray)
  204.   for i=1,51 do
  205.     term.setCursorPos(i,19)
  206.     term.write(string.sub(line,i,i))
  207.     if round(math.random()*100)%7 ~= 0 then
  208.       sleep(0.1)
  209.     end
  210.   end
  211.   term.setBackgroundColor(colors.gray)
  212. end
  213.  
  214. function SetColorDeflt()
  215.   term.setBackgroundColor(Style.BGDeflt)
  216.   term.setTextColor(Style.CDeflt)
  217. end
  218.  
  219. function SetColorTitle()
  220.   term.setBackgroundColor(Style.BGTitle)
  221.   term.setTextColor(Style.CTitle)
  222. end
  223.  
  224. function SetColorWarn()
  225.   term.setBackgroundColor(Style.BGWarn)
  226.   term.setTextColor(Style.CWarn)
  227. end
  228.  
  229. function Clear()
  230.   term.clear()
  231.   term.setCursorPos(1,1)
  232. end
  233.  
  234. function Show(Text)
  235.   term.write(Text)
  236.   local xt,yt = term.getCursorPos()
  237.   term.setCursorPos(1, yt+1)
  238. end
  239.  
  240. function ShowTitle(Text)
  241.   SetColorTitle()
  242.   local w, h = term.getSize()
  243.   local posX = math.floor((w-string.len(Text))/2)
  244.   if posX<0 then
  245.     posX=0
  246.   end
  247.   term.setCursorPos(posX,1)
  248.   Show(Text)
  249.   SetColorDeflt()
  250. end
  251.  
  252. function ShowMenu(Text)
  253.   term.write(Text)
  254.   local xt, yt = term.getCursorPos()
  255.   for i = xt, 51 do
  256.     term.write(" ")
  257.   end
  258.   term.setCursorPos(1, yt+1)
  259. end
  260.  
  261. function ShowWarning(Text)
  262.   SetColorWarn()
  263.   term.setCursorPos(10, 19)
  264.   term.write(" "..Text.." ")
  265.   SetColorDeflt()
  266. end
  267.  
  268. warps = {} -- all warp cores
  269.  
  270. function findWarps() -- find all warp cores
  271.   peripherals = peripheral.getNames()
  272.   for i = 1,#peripherals do
  273.     if peripheral.getType(peripherals[i]) == TYPE_WARP and table.contains(peripheral.getMethods(peripherals[i]),'set_mode') then
  274.       table.insert(warps,{ID = peripherals[i],core = peripheral.wrap(peripherals[i])})
  275.       if (SData["CurrentCore"]~="" and SData["CurrentCore"]==peripherals[i]) then
  276.         warp = warps[#warps]["core"]
  277.         curCoreIndex = #warps
  278.       end
  279.       warps[#warps]["core"].set_mode(0)
  280.     end
  281.   end
  282. end
  283.  
  284. function getCurCore()
  285.   return warps[curCoreIndex]["core"]
  286. end
  287.  
  288. GFront, GRight, GUp, GBack, GLeft, GDown, IsInHyper, X, Y, Z, Weight = nil
  289. function getCoreInfo(core)
  290.   core = core or getCurCore()
  291.   GFront, GRight, GUp = core.dim_getp()
  292.   GBack, GLeft, GDown = core.dim_getn()
  293.   IsInHyper = core.is_in_hyperspace()
  294.   repeat
  295.     X = core.get_x()
  296.     sleep(0.3)
  297.   until X ~= nil
  298.   Y = core.get_y()
  299.   Z = core.get_z()
  300.   Weight = core.get_ship_size()
  301.   if Weight==nil then
  302.     Weight = 1
  303.   end
  304.  
  305. end
  306.  
  307. function SaveData()
  308.   local file = fs.open("shipdata.txt", "w")
  309.   file.writeLine(textutils.serialize(SData))
  310.   file.close()
  311. end
  312.  
  313. function ReadData()
  314.   local file = fs.open("shipdata.txt", "r")
  315.   SData = textutils.unserialize(file.readAll())
  316.   file.close()
  317. end
  318.  
  319. function Explode(d, p)
  320.   local t, ll
  321.   t = {}
  322.   ll = 0
  323.   if(#p == 1) then return {p} end
  324.   while true do
  325.     local l = string.find(p ,d, ll, true)
  326.     if l ~= nil then
  327.       table.insert(t, string.sub(p, ll, l-1))
  328.       ll = l+1
  329.     else
  330.       table.insert(t, string.sub(p, ll))
  331.       break
  332.     end
  333.   end
  334.   return t
  335. end
  336.  
  337. function round(num, idp)
  338.   local mult = 10^(idp or 0)
  339.   return math.floor(num * mult + 0.5) / mult
  340. end
  341.  
  342. function getCoreOrientation(core)
  343.   core = core or getCurCore()
  344.   local dx = core.get_dx()
  345.   local dz = core.get_dz()
  346.   if not(dx==0) then
  347.     if dx > 0 then
  348.       return DIRECTION_EAST
  349.     elseif dx < 0 then
  350.       return DIRECTION_WEST
  351.     end
  352.   else
  353.     if dz > 0 then
  354.       return DIRECTION_SOUTH
  355.     elseif dz < 0 then
  356.       return DIRECTION_NORTH
  357.     end
  358.   end
  359.   return -1
  360. end
  361.  
  362. MinimumDistance,RealDistance = nil
  363. function CalcRealDistance()
  364.   if IsInHyper then
  365.     RealDistance = SData.Distance * 100
  366.     MinimumDistance = 1
  367.   else
  368.     if SData.Direction == DIRECTION_UP or SData.Direction == DIRECTION_DOWN then
  369.       MinimumDistance = GUp + GDown
  370.       RealDistance = SData.Distance + MinimumDistance
  371.     elseif SData.Direction == DIRECTION_SOUTH or SData.Direction == DIRECTION_NORTH then
  372.       MinimumDistance = GFront + GBack
  373.       RealDistance = SData.Distance + MinimumDistance
  374.     elseif SData.Direction == DIRECTION_EAST or SData.Direction == DIRECTION_WEST then
  375.       MinimumDistance = GLeft + GRight
  376.       RealDistance = SData.Distance + MinimumDistance
  377.     end
  378.     MinimumDistance = MinimumDistance + 1
  379.   end
  380. end
  381.  
  382. function CalcNewCoords(cx, cy, cz)
  383.   local res = {x=cx, y=cy, z=cz}
  384.   if SData.Direction == DIRECTION_UP then
  385.     res.y = res.y + RealDistance
  386.   elseif SData.Direction == DIRECTION_DOWN then
  387.     res.y = res.y - RealDistance
  388.   end
  389.   local dx = warp.get_dx()
  390.   local dz = warp.get_dz()
  391.   if dx ~= 0 then
  392.     if SData.Direction == WARP_FORWARD then
  393.       res.x = res.x + (RealDistance * dx)
  394.     elseif SData.Direction == WARP_BACK then
  395.       res.x = res.x - (RealDistance * dx)
  396.     elseif SData.Direction == WARP_LEFT then
  397.       res.z = res.z + (RealDistance * dx)
  398.     elseif SData.Direction == WARP_RIGHT then
  399.       res.z = res.z - (RealDistance * dx)
  400.     end
  401.   else
  402.     if SData.Direction == WARP_FORWARD then
  403.       res.z = res.z + (RealDistance * dz)
  404.     elseif SData.Direction == WARP_BACK then
  405.       res.z = res.z - (RealDistance * dz)
  406.     elseif SData.Direction == WARP_LEFT then
  407.       res.x = res.x + (RealDistance * dz)
  408.     elseif SData.Direction == WARP_RIGHT then
  409.       res.x = res.x - (RealDistance * dz)
  410.     end
  411.   end
  412.   return res
  413. end
  414.  
  415. function ShowInfo()
  416.   ShowTitle(Title)
  417.   local core = getCurCore();
  418.   local core_id = Explode("_",warps[curCoreIndex]["ID"])
  419.   if core_id[2] then
  420.     core_id = core_id[2]
  421.   else
  422.     core_id = warps[curCoreIndex]["ID"]
  423.   end
  424.   Show("Core: "..SData.Shipname.."_"..core_id)
  425.   Show(" x, y, z          = "..X..", "..Y..", "..Z)
  426.   local energy = core.get_energy_level()
  427.   Show(" Energy           = "..math.floor(energy / 1000000).." % ("..energy.."EU)")
  428.   Show(" Attached players = "..core.get_attached_players())
  429.   local heading = getDirectionName(getCoreOrientation(core))
  430.   while type(heading) ~= "string" do
  431.     heading = getDirectionName(getCoreOrientation(core))
  432.     sleep(0.1)
  433.   end
  434.   Show(" Heading          = "..heading)
  435.   Show("Dimensions:")
  436.   Show(" Front, Right, Up = "..GFront..", "..GRight..", "..GUp)
  437.   Show(" Back, Left, Down = "..GBack..", "..GLeft..", "..GDown)
  438.   Show(" Size             = "..Weight.." blocks")
  439.   Show("Warp data:")
  440.   Show(" Direction        = "..warp_names[SData.Direction].." ")
  441.   local dest = CalcNewCoords(X, Y, Z)
  442.   if table.contains(core, 'get_energy_required') then
  443.     local JumpCost = core.get_energy_required(RealDistance)
  444.     local JumpInfo = " ("..JumpCost.."EU, "..math.floor(energy/JumpCost).." jumps)"
  445.   else
  446.     local JumpInfo = ' ';
  447.   end
  448.   Show(" Distance         = "..tostring(RealDistance)..tostring(JumpInfo))
  449.   Show(" Dest.coordinates = "..dest.x..", "..dest.y..", "..dest.z)
  450.   local summons = " Summon after (M) = No"
  451.   if SData.Summon then
  452.     summons = " Summon after (M) = Yes"
  453.   end
  454.   Show(summons)
  455. end
  456.  
  457. function Confirm(message)
  458.   message = message or "Are you sure?"
  459.   ShowWarning(message.." (y/n)")
  460.   local event, keycode = os.pullEvent("key")
  461.   if keycode == 21 then
  462.     return true
  463.   else
  464.     return false
  465.   end
  466. end
  467.  
  468. function SetDistance()
  469.   Clear()
  470.   ShowTitle("<====  Set distance  ====>")
  471.   SData.Distance = 0
  472.   CalcRealDistance()
  473.   MaximumDistance = MinimumDistance + 1280
  474.   if IsInHyper then
  475.     term.write("Distance * 100 (min "..MinimumDistance..", max "..MaximumDistance.."): ")
  476.   else
  477.     term.write("Distance (min "..MinimumDistance..", max "..MaximumDistance.."): ")
  478.   end
  479.   sleep(0.3)
  480.   SData.Distance = tonumber(read())
  481.   if SData.Distance == nil then SData.Distance = 1 end
  482.   if SData.Distance < MinimumDistance or SData.Distance > MaximumDistance then
  483.     SData.Distance = 1
  484.     ShowWarning("Wrong distance. Try again.")
  485.     os.pullEvent("key")
  486.     CalcRealDistance()
  487.   else
  488.     if not IsInHyper then
  489.       SData.Distance = SData.Distance - RealDistance
  490.     end
  491.     CalcRealDistance()
  492.   end
  493. end
  494.  
  495. function SetDirection()
  496.   local drun = true
  497.   while(drun) do
  498.     Clear()
  499.     ShowTitle("<==== Set direction ====>")
  500.     Show(" Direction        = "..warp_names[SData.Direction].." ")
  501.     term.setCursorPos(1, 16)
  502.     SetColorTitle()
  503.     ShowMenu("Use directional keys")
  504.     ShowMenu("W/S keys for Up/Down")
  505.     ShowMenu("Enter - confirm")
  506.     SetColorDeflt()
  507.     local event, keycode = os.pullEvent("key")
  508.     if keycode == Key["up_arrow"] then
  509.       SData.Direction = WARP_FORWARD
  510.     elseif keycode == Key["w"] then
  511.       SData.Direction = WARP_UP
  512.     elseif keycode == Key["left_arrow"] then
  513.       SData.Direction = WARP_LEFT
  514.     elseif keycode == Key["right_arrow"] then
  515.       SData.Direction = WARP_RIGHT
  516.     elseif keycode == Key["down_arrow"] then
  517.       SData.Direction = WARP_BACK
  518.     elseif keycode == Key["s"] then
  519.       SData.Direction = WARP_DOWN
  520.     elseif keycode == Key["enter"] then
  521.       drun = false
  522.     end
  523.   end
  524. end
  525.  
  526. function SetDimensions()
  527.   local _GFront, _GRight, _GUp, _GBack, _GLeft, _GDown
  528.   Clear()
  529.   sleep(0.3)
  530.   ShowTitle("<== Set dimensions for "..SData["CurrentCore"].." ==>")
  531.  
  532.   term.write(" Front ("..GFront..") : ")
  533.   _GFront = tonumber(read()) or GFront
  534.  
  535.   term.write(" Right ("..GRight..") : ")
  536.   _GRight = tonumber(read()) or GRight
  537.  
  538.   term.write(" Up    ("..GUp..") : ")
  539.   _GUp = tonumber(read()) or GUp
  540.  
  541.   term.write(" Back  ("..GBack..") : ")
  542.   _GBack = tonumber(read()) or GBack
  543.  
  544.   term.write(" Left  ("..GLeft..") : ")
  545.   _GLeft = tonumber(read()) or GLeft
  546.  
  547.   term.write(" Down  ("..GDown..") : ")
  548.   _GDown = tonumber(read()) or GDown
  549.  
  550.   term.write("Setting dimensions...")
  551.   getCurCore().dim_setp(_GFront, _GRight, _GUp)
  552.   getCurCore().dim_setn(_GBack, _GLeft, _GDown)
  553.  
  554.   GFront = _GFront
  555.   GRight = _GRight
  556.   GUp = _GUp
  557.   GBack = _GBack
  558.   GLeft = _GLeft
  559.   GDown = _GDown
  560.  
  561.   if #warps<2 or not(Confirm("Calculate other cores?")) then
  562.     return
  563.   end
  564.  
  565.   local curCoords = {x=getCurCore().get_x(),y=getCurCore().get_y(),z=getCurCore().get_z()}
  566.   hlog("CurCoords: x="..curCoords['x'].." y="..curCoords['y'].." z="..curCoords['z'] )
  567.   hlog("CurDimensions: F="..GFront.." R="..GRight.."  U="..GUp )
  568.   hlog("CurDimensions: B="..GBack.."  L="..GLeft.." D="..GDown )
  569.   local curCoreOrient = getCoreOrientation(getCurCore())
  570.  
  571.   for i,wcore in pairs(warps) do
  572.     local core = wcore['core']
  573.     if not(wcore["ID"]==warps[curCoreIndex]["ID"]) then
  574.       local dx,dy,dz
  575.       local _GFront, _GRight, _GUp, _GBack, _GLeft, _GDown
  576.       local coreOrient = getCoreOrientation(core)
  577.       hlog('Found not current core '..wcore["ID"])
  578.       local coreCoords = {x=core.get_x(),y=core.get_y(),z=core.get_z()}
  579.       hlog(wcore["ID"].." Coords: x="..coreCoords['x'].." y="..coreCoords['y'].." z="..coreCoords['z'] )
  580.       dx = coreCoords['x']-curCoords['x']
  581.       dy = coreCoords['y']-curCoords['y']
  582.       dz = coreCoords['z']-curCoords['z']
  583.       hlog("Diff: x="..dx.." y="..dy.." z="..dz)
  584.       _GUp = GUp - dy
  585.       _GDown = GDown + dy
  586.  
  587.       local nextIteration = false
  588.       if coreOrient == curCoreOrient then
  589.         if coreOrient == DIRECTION_NORTH then
  590.           _GFront = GFront + dz
  591.           _GRight = GRight - dx
  592.           _GLeft = GLeft + dx
  593.           _GBack = GBack - dz
  594.         elseif coreOrient == DIRECTION_SOUTH then
  595.           _GFront = GFront - dz
  596.           _GRight = GRight + dx
  597.           _GLeft = GLeft - dx
  598.           _GBack = GBack + dz
  599.         elseif coreOrient == DIRECTION_EAST then
  600.           _GFront = GFront - dx
  601.           _GRight = GRight - dz
  602.           _GLeft = GLeft + dz
  603.           _GBack = GBack + dx
  604.         elseif coreOrient == DIRECTION_WEST then
  605.           _GFront = GFront + dx
  606.           _GRight = GRight + dz
  607.           _GLeft = GLeft - dz
  608.           _GBack = GBack - dx
  609.         end
  610.       else
  611.         print("Can't calculate differntly oriented core.")
  612.         if not(Confirm("Skip this one and continue?")) then
  613.           return
  614.         end
  615.         nextIteration = true
  616.       end
  617.       if not(nextIteration) then
  618.         hlog("NewDimensions: F=".._GFront.." R=".._GRight.."  U=".._GUp )
  619.         hlog("NewDimensions: B=".._GBack.."  L=".._GLeft.." D=".._GDown )
  620.         core.dim_setp(_GFront, _GRight, _GUp)
  621.         core.dim_setn(_GBack, _GLeft, _GDown)
  622.       end
  623.     end
  624.   end
  625.  
  626.  
  627. end
  628.  
  629. function chooseWarpCore()
  630.   Clear()
  631.   sleep(0.3)
  632.   ShowTitle("<==== Select warpcore ====>")
  633.   getCoreInfo()
  634.   Show("Current core: " .. warps[curCoreIndex]["ID"])
  635.   Show(" x, y, z          = "..X..", "..Y..", "..Z)
  636.   local energy = warp.get_energy_level()
  637.   Show(" Energy           = "..math.floor(energy / 1000000).." % ("..energy.."EU)")
  638.   Show(" Attached players = "..warp.get_attached_players())
  639.   Show("Dimensions:")
  640.   Show(" Front, Right, Up = "..GFront..", "..GRight..", "..GUp)
  641.   Show(" Back, Left, Down = "..GBack..", "..GLeft..", "..GDown)
  642.   Show(" Size             = "..Weight.." blocks")
  643.  
  644.   ShowMenu("Press <Left> or <Right> to switch core")
  645.   ShowMenu("Press <Enter> to select core")
  646.   local event, keycode = os.pullEvent("key")
  647.   if keycode == Key["right_arrow"] or keycode == Key["numenter"] then
  648.     return
  649.   elseif keycode == Key["left_arrow"] or keycode == Key["num4"] then -- Left
  650.     if curCoreIndex==1 then
  651.       curCoreIndex = #warps
  652.   else
  653.     curCoreIndex = curCoreIndex-1
  654.   end
  655.   elseif keycode == Key["right_arrow"] or keycode == Key["num6"] then -- Right
  656.     if curCoreIndex==#warps then
  657.       curCoreIndex = 1
  658.   else
  659.     curCoreIndex = curCoreIndex+1
  660.   end
  661.   end
  662.   warp = warps[curCoreIndex]["core"]
  663.   SData["CurrentCore"] = warps[curCoreIndex]["ID"]
  664.   chooseWarpCore()
  665. end
  666.  
  667.  
  668. function Warp()
  669.   powerOffAllCores()
  670.   -- rs.setOutput(Alarm, false)
  671.   sleep(1)
  672.   getCurCore().set_direction(SData.Direction)
  673.   getCurCore().set_distance(SData.Distance)
  674.   if IsInHyper then
  675.     getCurCore().set_mode(2)
  676.   else
  677.     getCurCore().set_mode(1)
  678.   end
  679.   getCurCore().do_jump()
  680. end
  681.  
  682. function HyperJump()
  683.   -- rs.setOutput(Alarm, true)
  684.   if Confirm() then
  685.     -- rs.setOutput(Alarm, false)
  686.     powerOffAllCores()
  687.     getCurCore().set_mode(5)
  688.     getCurCore().do_jump()
  689.   end
  690.   -- rs.setOutput(Alarm, false)
  691. end
  692.  
  693. function Summon()
  694.   Clear()
  695.   ShowTitle("<==== Summon players ====>")
  696.   local players = Explode(",", warp.get_attached_players())
  697.   for i = 1, #players do
  698.     Show(i..". "..players[i])
  699.   end
  700.   SetColorTitle()
  701.   ShowMenu("Enter player number")
  702.   ShowMenu("or press enter to summon everyone")
  703.   SetColorDeflt()
  704.   sleep(0.3)
  705.   term.write(":")
  706.   local input = read()
  707.   if input == "" then
  708.     warp.summon_all()
  709.   else
  710.     input = tonumber(input)
  711.     warp.summon(input - 1)
  712.   end
  713. end
  714.  
  715. function JumpToBeacon()
  716.   Clear()
  717.   ShowTitle("<==== Jump to beacon ====>")
  718.   sleep(0.3)
  719.   term.write("Enter beacon frequency: ")
  720.   local freq = tostring(read())
  721.   -- rs.setOutput(Alarm, true)
  722.   if Confirm() then
  723.     powerOffAllCores()
  724.     -- rs.setOutput(Alarm, false)
  725.     getCurCore().set_mode(4)
  726.     getCurCore().set_beacon_frequency(freq)
  727.     getCurCore().do_jump()
  728.   end
  729.   -- rs.setOutput(Alarm, false)
  730. end
  731.  
  732. function JumpToGate()
  733.   Clear()
  734.   ShowTitle("<==== Jump to JumpGate ====>")
  735.   sleep(0.3)
  736.   term.write("Enter jumpgate name: ")
  737.   local name = tostring(read())
  738.   -- rs.setOutput(Alarm, true)
  739.   if Confirm() then
  740.     powerOffAllCores()
  741.     -- rs.setOutput(Alarm, false)
  742.     getCurCore().set_mode(6)
  743.     getCurCore().set_target_jumpgate(name)
  744.     getCurCore().do_jump()
  745.   end
  746.   -- rs.setOutput(Alarm, false)
  747. end
  748.  
  749. function SetShipName()
  750.   Clear()
  751.   ShowTitle("<==== Set ship name ====>")
  752.   sleep(0.3)
  753.   term.write("Enter ship name: ")
  754.   SData.Shipname = tostring(read())
  755.   os.setComputerLabel(SData.Shipname)
  756.   for i,wcore in pairs(warps) do
  757.     local core_id = Explode("_",wcore["ID"])
  758.     core_id = core_id[2]
  759.     if not(core_id) then
  760.       wcore['core'].set_core_frequency(SData.Shipname..'_'..wcore["ID"])
  761.     else
  762.       wcore['core'].set_core_frequency(SData.Shipname..'_'..core_id)
  763.     end
  764.   end
  765.   SaveData()
  766.   os.reboot()
  767. end
  768.  
  769. function powerOnCore(core)
  770.   core = core or getCurCore()
  771.   core.set_mode(1)
  772. end
  773.  
  774. function powerOffAllCores()
  775.   for i = 1,#warps do
  776.     warps[i]["core"].set_mode(0)
  777.   end
  778. end
  779.  
  780. function powerOnAllCores()
  781.   for i = 1,#warps do
  782.     warps[i]["core"].set_mode(1)
  783.   end
  784. end
  785.  
  786. function checkStartup()
  787.   if not(fs.exists("startup")) then
  788.     shell.run("rm","startup")
  789.     local sfile= fs.open("startup", "w") or error("Cannot open file startup", 2)
  790.     sfile.write("shell.run(\"mcwarp\")")
  791.     sfile.close()
  792.   end
  793. end
  794.  
  795. function updateSoftware()
  796.   Clear()
  797.   if not(fs.exists("san")) then
  798.     shell.run("pastebin","get wf2EBfvT san")
  799.   end
  800.   shell.run("san"," update san")
  801.   shell.run("san"," update mcwarp")
  802.   if not(fs.exists("launcher")) then
  803.     shell.run("rm","startup")
  804.     local sfile= fs.open("startup", "w") or error("Cannot open file startup", 2)
  805.     sfile.write("shell.run(\"mcwarp\")")
  806.     sfile.close()
  807.   end
  808.   os.reboot()
  809. end
  810.  
  811. -- TODO: make this work
  812. function Navigation()
  813.   Clear()
  814.   ShowTitle("<==== Navigator ====>")
  815.   Show("Current x, y, z          = "..X..", "..Y..", "..Z)
  816.   term.write("Enter target X: ")
  817. end
  818.  
  819. function main()
  820.   while(mainloop) do
  821.     Clear()
  822.     ShowInfo()
  823.     term.setCursorPos(1, 16)
  824.     SetColorTitle()
  825.     local hyper_text = ''
  826.     if getCurCore().is_in_hyperspace() then
  827.       hyper_text = "from (H)yperspace, "
  828.     elseif getCurCore().is_in_space() then
  829.       hyper_text = "to (H)yperspace, "
  830.     end
  831.     ShowMenu("(D)imensions, Ship (N)ame, (S)et warp data")
  832.     ShowMenu("(J)ump, (G)ate jump, (B)eacon jump, (C)rew")
  833.     ShowMenu(hyper_text.."E(X)it, (R)eboot, (U)pdate")
  834.     if #warps>1 then
  835.       ShowMenu("            <- Choose warpcore ->")
  836.     end
  837.     SetColorDeflt()
  838.     while not(doLoop) do
  839.       os.queueEvent("randomEvent")
  840.       os.pullEvent()
  841.       sleep(.5)
  842.       getCoreInfo()
  843.     end
  844.     sleep(.1)
  845.   end
  846. end
  847.  
  848. function eventListener()
  849.   while mainloop do
  850.     local event, keycode = os.pullEvent("key")
  851.     doLoop = false
  852.     sleep(.2)
  853.     if keycode == Key["s"] then
  854.       SetDirection()
  855.       SetDistance()
  856.       SaveData()
  857.     elseif keycode == Key["m"] then
  858.       if SData.Summon then
  859.         SData.Summon = false
  860.       else
  861.         SData.Summon = true
  862.       end
  863.       SaveData()
  864.     elseif keycode == Key["d"] then
  865.       SetDimensions()
  866.       SaveData()
  867.     elseif keycode == Key["j"] then
  868.       -- rs.setOutput(Alarm, true)
  869.       if Confirm() then
  870.         Warp()
  871.       end
  872.       -- rs.setOutput(Alarm, false)
  873.     elseif keycode == Key["c"] then
  874.       Summon()
  875.     elseif keycode == Key["b"] then
  876.       JumpToBeacon()
  877.     elseif #warps>1 and (keycode == Key["left_arrow"] or keycode == Key["right_arrow"]) then
  878.       powerOffAllCores()
  879.       if keycode == Key["left_arrow"] then
  880.         if curCoreIndex==1 then
  881.           curCoreIndex = #warps
  882.         else
  883.           curCoreIndex = curCoreIndex-1
  884.         end
  885.       elseif keycode == Key["right_arrow"] then -- Right
  886.         if curCoreIndex==#warps then
  887.           curCoreIndex = 1
  888.         else
  889.           curCoreIndex = curCoreIndex+1
  890.         end
  891.       end
  892.       SData["CurrentCore"] = warps[curCoreIndex]["ID"]
  893.       warp = getCurCore()
  894.       powerOnCore(getCurCore())
  895.       SaveData()
  896.       getCoreInfo()
  897.     elseif keycode == Key["w"] then
  898.       chooseWarpCore()
  899.     elseif keycode == Key["z"] then
  900.       powerOnAllCores()
  901.       ShowWarning("All cores powered on")
  902.       sleep(2)
  903.     elseif keycode == Key["a"] then
  904.       powerOffAllCores()
  905.       ShowWarning("All cores powered off")
  906.       sleep(2)
  907.     elseif keycode == Key["u"] then
  908.       updateSoftware()
  909.     elseif keycode == Key["r"] then
  910.       os.reboot()
  911.     elseif keycode == Key["g"] then
  912.       JumpToGate()
  913.     elseif keycode == Key["h"] and (getCurCore().is_in_hyperspace() or getCurCore().is_in_space()) then
  914.       HyperJump()
  915.     elseif keycode == Key["x"] then
  916.       mainloop = false
  917.       gracefulExit()
  918.     elseif keycode == Key["n"] then
  919.       SetShipName()
  920.     end
  921.     doLoop = true
  922.   end
  923. end
  924.  
  925. function gracefulExit()
  926.   if SData.Summon then
  927.     SData.Summon = false
  928.     SaveData()
  929.   end
  930.   Clear()
  931.   print("wish you good")
  932.   for i = 1,#warps do
  933.     warps[i]["core"].set_mode(0)
  934.   end
  935.   sleep(0.5)
  936.   error()
  937. end
  938.  
  939. -- Main code starts here
  940. if fs.exists("shipdata.txt") then
  941.   ReadData()
  942. else
  943.   SData = {
  944.     Summon = false,
  945.     Distance = 1,
  946.     Direction = 0,
  947.     Shipname = "",
  948.     CurrentCore = ""
  949.   }
  950. end
  951.  
  952. SetColorDeflt()
  953. checkStartup()
  954. warps = {}
  955.  
  956. findWarps()
  957.  
  958. if #warps <1 then
  959.   ShowWarning("No warpcore controllers detected")
  960.   --os.pullEvent("key")
  961.   --error()
  962.   sleep(1)
  963.   os.reboot()
  964. end
  965.  
  966. if warp == nil then
  967.   SData["CurrentCore"] = warps[1]["ID"]
  968.   warp=warps[1]["core"]
  969.   curCoreIndex=1
  970. end
  971.  
  972. drawSplash()
  973.  
  974. local heading = getDirectionName(getCoreOrientation(core))
  975. while type(heading) ~= "string" do
  976.   heading = getDirectionName(getCoreOrientation(core))
  977.   sleep(0.1)
  978. end
  979.  
  980.  
  981. if SData.Shipname == "" then
  982.   SetShipName()
  983. end
  984.  
  985. Title = "<Jump-S "..version.." \""..SData.Shipname.."\">"
  986.  
  987. if SData.Summon then
  988.   warp.summon_all()
  989. end
  990.  
  991. getCoreInfo()
  992. CalcRealDistance()
  993.  
  994. -- warp.set_mode(1)
  995. powerOnAllCores()
  996.  
  997. mainloop = true
  998. doLoop = true
  999.  
  1000. parallel.waitForAny(eventListener, main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement