View difference between Paste ID: 9EYbErKf and LNNBBqmk
SHOW: | | - or go back to the newest paste.
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"
47+
TYPE_WARP = "warpdriveShipCore"
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
272+
for _, side in #peripherals do
273-
    if peripheral.getType(peripherals[i]) == TYPE_WARP and table.contains(peripheral.getMethods(peripherals[i]),'set_mode') then
273+
  //for i = 1,#peripherals do
274
   if peripheral.getType(peripherals) == TYPE_WARP and table.contains(peripheral.getMethods(peripherals[i]),'set_mode') then
275
      table.insert(warps,{ID = peripherals[i],core = peripheral.wrap(peripherals[i])})
276
      if (SData["CurrentCore"]~="" and SData["CurrentCore"]==peripherals[i]) then
277
        warp = warps[#warps]["core"]
278
        curCoreIndex = #warps
279
      end
280
      warps[#warps]["core"].set_mode(0)
281
    end
282
  end
283
end
284
285
function getCurCore()
286
  return warps[curCoreIndex]["core"]
287
end
288
289
GFront, GRight, GUp, GBack, GLeft, GDown, IsInHyper, X, Y, Z, Weight = nil
290
function getCoreInfo(core)
291
  core = core or getCurCore()
292
  GFront, GRight, GUp = core.dim_getp()
293
  GBack, GLeft, GDown = core.dim_getn()
294
  IsInHyper = core.is_in_hyperspace()
295
  repeat
296
    X = core.get_x()
297
    sleep(0.3)
298
  until X ~= nil
299
  Y = core.get_y()
300
  Z = core.get_z()
301
  Weight = core.get_ship_size()
302
  if Weight==nil then
303
    Weight = 1
304
  end
305
306
end
307
308
function SaveData()
309
  local file = fs.open("shipdata.txt", "w")
310
  file.writeLine(textutils.serialize(SData))
311
  file.close()
312
end
313
314
function ReadData()
315
  local file = fs.open("shipdata.txt", "r")
316
  SData = textutils.unserialize(file.readAll())
317
  file.close()
318
end
319
320
function Explode(d, p)
321
  local t, ll
322
  t = {}
323
  ll = 0
324
  if(#p == 1) then return {p} end
325
  while true do
326
    local l = string.find(p ,d, ll, true)
327
    if l ~= nil then
328
      table.insert(t, string.sub(p, ll, l-1))
329
      ll = l+1
330
    else
331
      table.insert(t, string.sub(p, ll))
332
      break
333
    end
334
  end
335
  return t
336
end
337
338
function round(num, idp)
339
  local mult = 10^(idp or 0)
340
  return math.floor(num * mult + 0.5) / mult
341
end
342
343
function getCoreOrientation(core)
344
  core = core or getCurCore()
345
  local dx = core.get_dx()
346
  local dz = core.get_dz()
347
  if not(dx==0) then
348
    if dx > 0 then
349
      return DIRECTION_EAST
350
    elseif dx < 0 then
351
      return DIRECTION_WEST
352
    end
353
  else
354
    if dz > 0 then
355
      return DIRECTION_SOUTH
356
    elseif dz < 0 then
357
      return DIRECTION_NORTH
358
    end
359
  end
360
  return -1
361
end
362
363
MinimumDistance,RealDistance = nil
364
function CalcRealDistance()
365
  if IsInHyper then
366
    RealDistance = SData.Distance * 100
367
    MinimumDistance = 1
368
  else
369
    if SData.Direction == DIRECTION_UP or SData.Direction == DIRECTION_DOWN then
370
      MinimumDistance = GUp + GDown
371
      RealDistance = SData.Distance + MinimumDistance
372
    elseif SData.Direction == DIRECTION_SOUTH or SData.Direction == DIRECTION_NORTH then
373
      MinimumDistance = GFront + GBack
374
      RealDistance = SData.Distance + MinimumDistance
375
    elseif SData.Direction == DIRECTION_EAST or SData.Direction == DIRECTION_WEST then
376
      MinimumDistance = GLeft + GRight
377
      RealDistance = SData.Distance + MinimumDistance
378
    end
379
    MinimumDistance = MinimumDistance + 1
380
  end
381
end
382
383
function CalcNewCoords(cx, cy, cz)
384
  local res = {x=cx, y=cy, z=cz}
385
  if SData.Direction == DIRECTION_UP then
386
    res.y = res.y + RealDistance
387
  elseif SData.Direction == DIRECTION_DOWN then
388
    res.y = res.y - RealDistance
389
  end
390
  local dx = warp.get_dx()
391
  local dz = warp.get_dz()
392
  if dx ~= 0 then
393
    if SData.Direction == WARP_FORWARD then
394
      res.x = res.x + (RealDistance * dx)
395
    elseif SData.Direction == WARP_BACK then
396
      res.x = res.x - (RealDistance * dx)
397
    elseif SData.Direction == WARP_LEFT then
398
      res.z = res.z + (RealDistance * dx)
399
    elseif SData.Direction == WARP_RIGHT then
400
      res.z = res.z - (RealDistance * dx)
401
    end
402
  else
403
    if SData.Direction == WARP_FORWARD then
404
      res.z = res.z + (RealDistance * dz)
405
    elseif SData.Direction == WARP_BACK then
406
      res.z = res.z - (RealDistance * dz)
407
    elseif SData.Direction == WARP_LEFT then
408
      res.x = res.x + (RealDistance * dz)
409
    elseif SData.Direction == WARP_RIGHT then
410
      res.x = res.x - (RealDistance * dz)
411
    end
412
  end
413
  return res
414
end
415
416
function ShowInfo()
417
  ShowTitle(Title)
418
  local core = getCurCore();
419
  local core_id = Explode("_",warps[curCoreIndex]["ID"])
420
  if core_id[2] then
421
    core_id = core_id[2]
422
  else
423
    core_id = warps[curCoreIndex]["ID"]
424
  end
425
  Show("Core: "..SData.Shipname.."_"..core_id)
426
  Show(" x, y, z          = "..X..", "..Y..", "..Z)
427
  local energy = core.get_energy_level()
428
  Show(" Energy           = "..math.floor(energy / 1000000).." % ("..energy.."EU)")
429
  Show(" Attached players = "..core.get_attached_players())
430
  local heading = getDirectionName(getCoreOrientation(core))
431
  while type(heading) ~= "string" do
432
    heading = getDirectionName(getCoreOrientation(core))
433
    sleep(0.1)
434
  end
435
  Show(" Heading          = "..heading)
436
  Show("Dimensions:")
437
  Show(" Front, Right, Up = "..GFront..", "..GRight..", "..GUp)
438
  Show(" Back, Left, Down = "..GBack..", "..GLeft..", "..GDown)
439
  Show(" Size             = "..Weight.." blocks")
440
  Show("Warp data:")
441
  Show(" Direction        = "..warp_names[SData.Direction].." ")
442
  local dest = CalcNewCoords(X, Y, Z)
443
  if table.contains(core, 'get_energy_required') then
444
    local JumpCost = core.get_energy_required(RealDistance)
445
	local JumpInfo = " ("..JumpCost.."EU, "..math.floor(energy/JumpCost).." jumps)"
446
  else
447
    local JumpInfo = ' ';
448
  end
449
  Show(" Distance         = "..tostring(RealDistance)..tostring(JumpInfo))
450
  Show(" Dest.coordinates = "..dest.x..", "..dest.y..", "..dest.z)
451
  local summons = " Summon after (M) = No" 
452
  if SData.Summon then
453
    summons = " Summon after (M) = Yes"
454
  end
455
  Show(summons)
456
end
457
458
function Confirm(message)
459
  message = message or "Are you sure?"
460
  ShowWarning(message.." (y/n)")
461
  local event, keycode = os.pullEvent("key")
462
  if keycode == 21 then
463
    return true
464
  else
465
    return false
466
  end
467
end
468
469
function SetDistance()
470
  Clear()
471
  ShowTitle("<====  Set distance  ====>")
472
  SData.Distance = 0
473
  CalcRealDistance()
474
  MaximumDistance = MinimumDistance + 1280
475
  if IsInHyper then
476
    term.write("Distance * 100 (min "..MinimumDistance..", max "..MaximumDistance.."): ")
477
  else
478
    term.write("Distance (min "..MinimumDistance..", max "..MaximumDistance.."): ")
479
  end
480
  sleep(0.3)
481
  SData.Distance = tonumber(read())
482
  if SData.Distance == nil then SData.Distance = 1 end
483
  if SData.Distance < MinimumDistance or SData.Distance > MaximumDistance then
484
    SData.Distance = 1
485
    ShowWarning("Wrong distance. Try again.")
486
    os.pullEvent("key")
487
    CalcRealDistance()
488
  else
489
    if not IsInHyper then
490
      SData.Distance = SData.Distance - RealDistance
491
    end
492
    CalcRealDistance()
493
  end
494
end
495
496
function SetDirection()
497
  local drun = true
498
  while(drun) do
499
    Clear()
500
    ShowTitle("<==== Set direction ====>")
501
    Show(" Direction        = "..warp_names[SData.Direction].." ")
502
    term.setCursorPos(1, 16)
503
    SetColorTitle()
504
    ShowMenu("Use directional keys")
505
    ShowMenu("W/S keys for Up/Down")
506
    ShowMenu("Enter - confirm")
507
    SetColorDeflt()
508
    local event, keycode = os.pullEvent("key")
509
    if keycode == Key["up_arrow"] then
510
      SData.Direction = WARP_FORWARD
511
    elseif keycode == Key["w"] then
512
      SData.Direction = WARP_UP
513
    elseif keycode == Key["left_arrow"] then
514
      SData.Direction = WARP_LEFT
515
    elseif keycode == Key["right_arrow"] then
516
      SData.Direction = WARP_RIGHT
517
    elseif keycode == Key["down_arrow"] then
518
      SData.Direction = WARP_BACK
519
    elseif keycode == Key["s"] then
520
      SData.Direction = WARP_DOWN
521
    elseif keycode == Key["enter"] then
522
      drun = false
523
    end
524
  end
525
end
526
527
function SetDimensions()
528
  local _GFront, _GRight, _GUp, _GBack, _GLeft, _GDown
529
  Clear()
530
  sleep(0.3)
531
  ShowTitle("<== Set dimensions for "..SData["CurrentCore"].." ==>")
532
533
  term.write(" Front ("..GFront..") : ")
534
  _GFront = tonumber(read()) or GFront
535
536
  term.write(" Right ("..GRight..") : ")
537
  _GRight = tonumber(read()) or GRight
538
539
  term.write(" Up    ("..GUp..") : ")
540
  _GUp = tonumber(read()) or GUp
541
542
  term.write(" Back  ("..GBack..") : ")
543
  _GBack = tonumber(read()) or GBack
544
545
  term.write(" Left  ("..GLeft..") : ")
546
  _GLeft = tonumber(read()) or GLeft
547
548
  term.write(" Down  ("..GDown..") : ")
549
  _GDown = tonumber(read()) or GDown
550
551
  term.write("Setting dimensions...")
552
  getCurCore().dim_setp(_GFront, _GRight, _GUp)
553
  getCurCore().dim_setn(_GBack, _GLeft, _GDown)
554
  
555
  GFront = _GFront
556
  GRight = _GRight
557
  GUp = _GUp
558
  GBack = _GBack
559
  GLeft = _GLeft
560
  GDown = _GDown
561
562
  if #warps<2 or not(Confirm("Calculate other cores?")) then
563
    return
564
  end
565
566
  local curCoords = {x=getCurCore().get_x(),y=getCurCore().get_y(),z=getCurCore().get_z()}
567
  hlog("CurCoords: x="..curCoords['x'].." y="..curCoords['y'].." z="..curCoords['z'] )
568
  hlog("CurDimensions: F="..GFront.." R="..GRight.."  U="..GUp )
569
  hlog("CurDimensions: B="..GBack.."  L="..GLeft.." D="..GDown )
570
  local curCoreOrient = getCoreOrientation(getCurCore())
571
572
  for i,wcore in pairs(warps) do
573
    local core = wcore['core']
574
    if not(wcore["ID"]==warps[curCoreIndex]["ID"]) then
575
      local dx,dy,dz
576
      local _GFront, _GRight, _GUp, _GBack, _GLeft, _GDown
577
      local coreOrient = getCoreOrientation(core)
578
      hlog('Found not current core '..wcore["ID"])
579
      local coreCoords = {x=core.get_x(),y=core.get_y(),z=core.get_z()}
580
      hlog(wcore["ID"].." Coords: x="..coreCoords['x'].." y="..coreCoords['y'].." z="..coreCoords['z'] )
581
      dx = coreCoords['x']-curCoords['x']
582
      dy = coreCoords['y']-curCoords['y']
583
      dz = coreCoords['z']-curCoords['z']
584
      hlog("Diff: x="..dx.." y="..dy.." z="..dz)
585
      _GUp = GUp - dy
586
      _GDown = GDown + dy
587
588
      local nextIteration = false
589
      if coreOrient == curCoreOrient then
590
        if coreOrient == DIRECTION_NORTH then
591
          _GFront = GFront + dz
592
          _GRight = GRight - dx
593
          _GLeft = GLeft + dx
594
          _GBack = GBack - dz
595
        elseif coreOrient == DIRECTION_SOUTH then
596
          _GFront = GFront - dz
597
          _GRight = GRight + dx
598
          _GLeft = GLeft - dx
599
          _GBack = GBack + dz
600
        elseif coreOrient == DIRECTION_EAST then
601
          _GFront = GFront - dx
602
          _GRight = GRight - dz
603
          _GLeft = GLeft + dz
604
          _GBack = GBack + dx
605
        elseif coreOrient == DIRECTION_WEST then
606
          _GFront = GFront + dx
607
          _GRight = GRight + dz
608
          _GLeft = GLeft - dz
609
          _GBack = GBack - dx
610
        end
611
      else
612
        print("Can't calculate differntly oriented core.")
613
        if not(Confirm("Skip this one and continue?")) then
614
          return
615
        end
616
        nextIteration = true
617
      end
618
      if not(nextIteration) then
619
        hlog("NewDimensions: F=".._GFront.." R=".._GRight.."  U=".._GUp )
620
        hlog("NewDimensions: B=".._GBack.."  L=".._GLeft.." D=".._GDown )
621
        core.dim_setp(_GFront, _GRight, _GUp)
622
        core.dim_setn(_GBack, _GLeft, _GDown)
623
      end
624
    end
625
  end
626
627
628
end
629
630
function chooseWarpCore()
631
  Clear()
632
  sleep(0.3)
633
  ShowTitle("<==== Select warpcore ====>")
634
  getCoreInfo()
635
  Show("Current core: " .. warps[curCoreIndex]["ID"])
636
  Show(" x, y, z          = "..X..", "..Y..", "..Z)
637
  local energy = warp.get_energy_level()
638
  Show(" Energy           = "..math.floor(energy / 1000000).." % ("..energy.."EU)")
639
  Show(" Attached players = "..warp.get_attached_players())
640
  Show("Dimensions:")
641
  Show(" Front, Right, Up = "..GFront..", "..GRight..", "..GUp)
642
  Show(" Back, Left, Down = "..GBack..", "..GLeft..", "..GDown)
643
  Show(" Size             = "..Weight.." blocks")
644
645
  ShowMenu("Press <Left> or <Right> to switch core")
646
  ShowMenu("Press <Enter> to select core")
647
  local event, keycode = os.pullEvent("key")
648
  if keycode == Key["right_arrow"] or keycode == Key["numenter"] then
649
    return
650
  elseif keycode == Key["left_arrow"] or keycode == Key["num4"] then -- Left
651
    if curCoreIndex==1 then
652
      curCoreIndex = #warps
653
  else
654
    curCoreIndex = curCoreIndex-1
655
  end
656
  elseif keycode == Key["right_arrow"] or keycode == Key["num6"] then -- Right
657
    if curCoreIndex==#warps then
658
      curCoreIndex = 1
659
  else
660
    curCoreIndex = curCoreIndex+1
661
  end
662
  end
663
  warp = warps[curCoreIndex]["core"]
664
  SData["CurrentCore"] = warps[curCoreIndex]["ID"]
665
  chooseWarpCore()
666
end
667
668
669
function Warp()
670
  powerOffAllCores()
671
  -- rs.setOutput(Alarm, false)
672
  sleep(1)
673
  getCurCore().set_direction(SData.Direction)
674
  getCurCore().set_distance(SData.Distance)
675
  if IsInHyper then
676
    getCurCore().set_mode(2)
677
  else
678
    getCurCore().set_mode(1)
679
  end
680
  getCurCore().do_jump()
681
end
682
683
function HyperJump()
684
  -- rs.setOutput(Alarm, true)
685
  if Confirm() then
686
    -- rs.setOutput(Alarm, false)
687
    powerOffAllCores()
688
    getCurCore().set_mode(5)
689
    getCurCore().do_jump()
690
  end
691
  -- rs.setOutput(Alarm, false)
692
end
693
694
function Summon()
695
  Clear()
696
  ShowTitle("<==== Summon players ====>")
697
  local players = Explode(",", warp.get_attached_players())
698
  for i = 1, #players do
699
    Show(i..". "..players[i])
700
  end
701
  SetColorTitle()
702
  ShowMenu("Enter player number")
703
  ShowMenu("or press enter to summon everyone")
704
  SetColorDeflt()
705
  sleep(0.3)
706
  term.write(":")
707
  local input = read()
708
  if input == "" then
709
    warp.summon_all()
710
  else
711
    input = tonumber(input)
712
    warp.summon(input - 1)
713
  end
714
end
715
716
function JumpToBeacon()
717
  Clear()
718
  ShowTitle("<==== Jump to beacon ====>")
719
  sleep(0.3)
720
  term.write("Enter beacon frequency: ")
721
  local freq = tostring(read())
722
  -- rs.setOutput(Alarm, true)
723
  if Confirm() then
724
    powerOffAllCores()
725
    -- rs.setOutput(Alarm, false)
726
    getCurCore().set_mode(4)
727
    getCurCore().set_beacon_frequency(freq)
728
    getCurCore().do_jump()
729
  end
730
  -- rs.setOutput(Alarm, false)
731
end
732
733
function JumpToGate()
734
  Clear()
735
  ShowTitle("<==== Jump to JumpGate ====>")
736
  sleep(0.3)
737
  term.write("Enter jumpgate name: ")
738
  local name = tostring(read())
739
  -- rs.setOutput(Alarm, true)
740
  if Confirm() then
741
    powerOffAllCores()
742
    -- rs.setOutput(Alarm, false)
743
    getCurCore().set_mode(6)
744
    getCurCore().set_target_jumpgate(name)
745
    getCurCore().do_jump()
746
  end
747
  -- rs.setOutput(Alarm, false)
748
end
749
750
function SetShipName()
751
  Clear()
752
  ShowTitle("<==== Set ship name ====>")
753
  sleep(0.3)
754
  term.write("Enter ship name: ")
755
  SData.Shipname = tostring(read())
756
  os.setComputerLabel(SData.Shipname)
757
  for i,wcore in pairs(warps) do
758
    local core_id = Explode("_",wcore["ID"])
759
    core_id = core_id[2]
760
    if not(core_id) then
761
      wcore['core'].set_core_frequency(SData.Shipname..'_'..wcore["ID"])
762
    else
763
      wcore['core'].set_core_frequency(SData.Shipname..'_'..core_id)
764
    end
765
  end
766
  SaveData()
767
  os.reboot()
768
end
769
770
function powerOnCore(core)
771
  core = core or getCurCore()
772
  core.set_mode(1)
773
end
774
775
function powerOffAllCores()
776
  for i = 1,#warps do
777
    warps[i]["core"].set_mode(0)
778
  end
779
end
780
781
function powerOnAllCores()
782
  for i = 1,#warps do
783
    warps[i]["core"].set_mode(1)
784
  end
785
end
786
787
function checkStartup()
788
  if not(fs.exists("startup")) then
789
    shell.run("rm","startup")
790
    local sfile= fs.open("startup", "w") or error("Cannot open file startup", 2)
791
    sfile.write("shell.run(\"mcwarp\")")
792
    sfile.close()
793
  end
794
end
795
796
function updateSoftware()
797
  Clear()
798
  if not(fs.exists("san")) then
799
    shell.run("pastebin","get wf2EBfvT san")
800
  end
801
  shell.run("san"," update san")
802
  shell.run("san"," update mcwarp")
803
  if not(fs.exists("launcher")) then
804
    shell.run("rm","startup")
805
    local sfile= fs.open("startup", "w") or error("Cannot open file startup", 2)
806
    sfile.write("shell.run(\"mcwarp\")")
807
    sfile.close()
808
  end
809
  os.reboot()
810
end
811
812
-- TODO: make this work
813
function Navigation()
814
  Clear()
815
  ShowTitle("<==== Navigator ====>")
816
  Show("Current x, y, z          = "..X..", "..Y..", "..Z)
817
  term.write("Enter target X: ")
818
end
819
820
function main()
821
  while(mainloop) do
822
    Clear()
823
    ShowInfo()
824
    term.setCursorPos(1, 16)
825
    SetColorTitle()
826
    local hyper_text = ''
827
    if getCurCore().is_in_hyperspace() then
828
      hyper_text = "from (H)yperspace, "
829
    elseif getCurCore().is_in_space() then
830
      hyper_text = "to (H)yperspace, "
831
    end
832
    ShowMenu("(D)imensions, Ship (N)ame, (S)et warp data")
833
    ShowMenu("(J)ump, (G)ate jump, (B)eacon jump, (C)rew")
834
    ShowMenu(hyper_text.."E(X)it, (R)eboot, (U)pdate")
835
    if #warps>1 then
836
      ShowMenu("            <- Choose warpcore ->")
837
    end
838
    SetColorDeflt()
839
    while not(doLoop) do
840
      os.queueEvent("randomEvent")
841
      os.pullEvent()
842
      sleep(.5)
843
      getCoreInfo()
844
    end
845
    sleep(.1)
846
  end
847
end
848
849
function eventListener()
850
  while mainloop do
851
    local event, keycode = os.pullEvent("key")
852
    doLoop = false
853
    sleep(.2)
854
    if keycode == Key["s"] then
855
      SetDirection()
856
      SetDistance()
857
      SaveData()
858
    elseif keycode == Key["m"] then
859
      if SData.Summon then
860
        SData.Summon = false
861
      else
862
        SData.Summon = true
863
      end
864
      SaveData()
865
    elseif keycode == Key["d"] then
866
      SetDimensions()
867
      SaveData()
868
    elseif keycode == Key["j"] then
869
      -- rs.setOutput(Alarm, true)
870
      if Confirm() then
871
        Warp()
872
      end
873
      -- rs.setOutput(Alarm, false)
874
    elseif keycode == Key["c"] then
875
      Summon()
876
    elseif keycode == Key["b"] then
877
      JumpToBeacon()
878
    elseif #warps>1 and (keycode == Key["left_arrow"] or keycode == Key["right_arrow"]) then
879
      powerOffAllCores()
880
      if keycode == Key["left_arrow"] then
881
        if curCoreIndex==1 then
882
          curCoreIndex = #warps
883
        else
884
          curCoreIndex = curCoreIndex-1
885
        end
886
      elseif keycode == Key["right_arrow"] then -- Right
887
        if curCoreIndex==#warps then
888
          curCoreIndex = 1
889
        else
890
          curCoreIndex = curCoreIndex+1
891
        end
892
      end
893
      SData["CurrentCore"] = warps[curCoreIndex]["ID"]
894
      warp = getCurCore()
895
      powerOnCore(getCurCore())
896
      SaveData()
897
      getCoreInfo()
898
    elseif keycode == Key["w"] then
899
      chooseWarpCore()
900
    elseif keycode == Key["z"] then
901
      powerOnAllCores()
902
      ShowWarning("All cores powered on")
903
      sleep(2)
904
    elseif keycode == Key["a"] then
905
      powerOffAllCores()
906
      ShowWarning("All cores powered off")
907
      sleep(2)
908
    elseif keycode == Key["u"] then
909
      updateSoftware()
910
    elseif keycode == Key["r"] then
911
      os.reboot()
912
    elseif keycode == Key["g"] then
913
      JumpToGate()
914
    elseif keycode == Key["h"] and (getCurCore().is_in_hyperspace() or getCurCore().is_in_space()) then
915
      HyperJump()
916
    elseif keycode == Key["x"] then
917
      mainloop = false
918
      gracefulExit()
919
    elseif keycode == Key["n"] then
920
      SetShipName()
921
    end
922
    doLoop = true
923
  end
924
end
925
926
function gracefulExit()
927
  if SData.Summon then
928
    SData.Summon = false
929
    SaveData()
930
  end
931
  Clear()
932
  print("wish you good")
933
  for i = 1,#warps do
934
    warps[i]["core"].set_mode(0)
935
  end
936
  sleep(0.5)
937
  error()
938
end
939
940
-- Main code starts here
941
if fs.exists("shipdata.txt") then
942
  ReadData()
943
else
944
  SData = {
945
    Summon = false,
946
    Distance = 1,
947
    Direction = 0,
948
    Shipname = "",
949
    CurrentCore = ""
950
  }
951
end
952
953
SetColorDeflt()
954
checkStartup()
955
warps = {}
956
957
findWarps()
958
959
if #warps <1 then
960
  ShowWarning("No warpcore controllers detected")
961
  --os.pullEvent("key")
962
  --error()
963
  sleep(1)
964
  os.reboot()
965
end
966
967
if warp == nil then
968
  SData["CurrentCore"] = warps[1]["ID"]
969
  warp=warps[1]["core"]
970
  curCoreIndex=1
971
end
972
973
drawSplash()
974
975
local heading = getDirectionName(getCoreOrientation(core))
976
while type(heading) ~= "string" do
977
  heading = getDirectionName(getCoreOrientation(core))
978
  sleep(0.1)
979
end
980
981
  
982
if SData.Shipname == "" then
983
  SetShipName()
984
end
985
986
Title = "<Jump-S "..version.." \""..SData.Shipname.."\">"
987
988
if SData.Summon then
989
  warp.summon_all()
990
end
991
992
getCoreInfo()
993
CalcRealDistance()
994
995
-- warp.set_mode(1)
996
powerOnAllCores()
997
998
mainloop = true
999
doLoop = true
1000
1001
parallel.waitForAny(eventListener, main)