View difference between Paste ID: xY5FL1NE and gbiDEtYK
SHOW: | | - or go back to the newest paste.
1-
--Savage_Me55iah--
1+
--Script by Savage_Me55iah of CraftAU.com.au--
2
--Version 1.6--
3-
--local tArgs = { ... }
3+
--Needs corresponding "Savage Client v1.6" script @ http://pastebin.com/vbdYtpra--
4-
--local first_run = tArgs[1] or false
4+
--type "monitor client" to auto download--
5
6
--INSTRUCTIONS ON USE--
7
--save this program as startup or monitor and type in startup: shell.run("monitor")--
8
--example client computer: id-5 label-power
9
--to add example client type "montior add 5 power"
10
--to remove example client type "monitor remove 5 power"
11
--to show settings of buttons type "monitor settings"
12
--to adjust the settings of script type "edit settings", changing them here won't work once this program has been run once
13
--to reset everything type "monitor reset"
14
--buttons will not work without the online button on, turning the online button will shut off all other buttons
15
--connect the monitors using network cables and wired modems
16
17
18
local tArgs = { ... }
19
local command = tostring(tArgs[1]) or false
20
local name1 = tostring(tArgs[3]) or false
21
local id = tonumber(tArgs[2]) or false
22
local name2 = tostring(tArgs[5]) or false
23
local id2 = tonumber(tArgs[4]) or false
24
25
--TABLES--
26
print("tables")
27
28
names = {}
29
ids = {}
30
status = {}
31
32
function add_button(command, id, on_off)
33
  num = table.maxn(names) + 1
34
  names[num] = command
35
  ids[num] = id
36-
  ["heading"] = "Hello I am Jarvis at your service Sir...",
36+
37
end
38
39
add_button("online", 0, true)
40
41-
  ["refresh_rate"] = 60,
41+
42
  ["load_save_loc"] = "load_save_loc",
43
  ["settings"] = "settings",
44
  ["names"] = "names",
45
  ["ids"] = "ids",
46
  ["status"] = "status",
47
  ["eu"] = "eu",
48
  ["mj"] = "mj",
49
}
50
51-
  ["timeout"] = 10,
51+
52
  ["mon_side"] = "top",
53
  ["wireless_side"] = "left",
54
  ["remote_modem"] = "right",
55
  ["heading color"] = colors.yellow,
56
  ["heading background"] = colors.black,
57
  ["heading"] = "Savage Monitor v1.4",
58
  ["active color"] = colors.green,
59
  ["inactive color"] = colors.red,
60
  ["id"] = os.getComputerID(),
61
  ["label"] = "Server",
62
  ["refresh_rate"] = 10,
63
  ["text_scale"] = 1,
64
  ["button_width"] = 5,
65
  ["energy_stats"] = false,
66
  ["open_peripherals"] = false,
67
  ["direct_power"] = false,
68
  ["energy_room"] = 0,
69
  ["eu_id"] = 1000,
70
  ["mj_id"] = 1001,
71
  ["screen_saver"] = true,
72
  ["timeout"] = 50,
73
  ["image_x"] = 27,
74
  ["image_y"] = 9,
75
  ["client"] = "vbdYtpra"
76
}
77
78
local button_x1 = {
79
  [1] = 2
80-
if not fs.exists("settings") then print("Enter computer label: ") os.setComputerLabel(read() or settings["label"]) end
80+
81
82
local button_x2 = {
83
  [1] = 12
84
}
85
86
local button_y1 = {
87
  [1] = 4
88
}
89
90
local eu = {
91
  ["max"] = 0,
92
  ["current"] = 0,
93
  ["percent"] = 0
94
}
95
96
local mj = {
97
  ["max"] = 0,
98
  ["current"] = 0,
99
  ["percent"] = 0
100
}
101
102
if not fs.exists("settings") then print("Enter computer label: ") os.setComputerLabel(read() or settings["label"]) settings["label"] = os.getComputerLabel() end
103
104
--LOAD & SAVE FUNCTIONS--
105
106
function load()
107
  print("Loading...")
108
  local fileHandle = fs.open (load_save_loc["load_save_loc"], 'r')
109
  load_save_loc = textutils.unserialize (fileHandle.readAll())
110
  fileHandle.close()
111
112
  local fileHandle = fs.open (load_save_loc["settings"], 'r')
113
  settings = textutils.unserialize (fileHandle.readAll())
114
  fileHandle.close()
115
116
  local fileHandle = fs.open (load_save_loc["names"], 'r')
117
  names = textutils.unserialize (fileHandle.readAll())
118
  fileHandle.close()
119
120
  local fileHandle = fs.open (load_save_loc["ids"], 'r')
121
  ids = textutils.unserialize (fileHandle.readAll())
122
  fileHandle.close()
123
124
  local fileHandle = fs.open (load_save_loc["status"], 'r')
125
  status = textutils.unserialize (fileHandle.readAll())
126
  fileHandle.close()
127
128
  local fileHandle = fs.open (load_save_loc["eu"], 'r')
129
  eu = textutils.unserialize (fileHandle.readAll())
130
  fileHandle.close()
131
132
  local fileHandle = fs.open (load_save_loc["mj"], 'r')
133
  mj = textutils.unserialize (fileHandle.readAll())
134
  fileHandle.close()
135
end
136
137
function save()
138
  print("Saving...")
139
  local fileHandle = fs.open (load_save_loc["load_save_loc"], 'w')
140
  fileHandle.write (textutils.serialize (load_save_loc))
141
  fileHandle.close()
142
143
  local fileHandle = fs.open (load_save_loc["settings"], 'w')
144
  fileHandle.write (textutils.serialize (settings))
145
  fileHandle.close()
146
147
  local fileHandle = fs.open (load_save_loc["names"], 'w')
148
  fileHandle.write (textutils.serialize (names))
149
  fileHandle.close()
150
151
  local fileHandle = fs.open (load_save_loc["ids"], 'w')
152
  fileHandle.write (textutils.serialize (ids))
153
  fileHandle.close()
154
155
  local fileHandle = fs.open (load_save_loc["status"], 'w')
156
  fileHandle.write (textutils.serialize (status))
157
  fileHandle.close()
158
159
  local fileHandle = fs.open (load_save_loc["eu"], 'w')
160
  fileHandle.write (textutils.serialize (eu))
161
  fileHandle.close()
162
163
  local fileHandle = fs.open (load_save_loc["mj"], 'w')
164
  fileHandle.write (textutils.serialize (mj))
165
  fileHandle.close()
166
end
167
168
169
--PERLIMINARY WORKINGS--
170
171
function imagewrite(code)
172
  outputFile:write("/n")
173
  outputFile:write(code)
174
end
175
176
image_table = {[1] = "eeeeeeeeeeeeeeeeeeeeeeeeeee",
177
  [2] = "eeeeeffffffffeefffffefffffe",
178
  [3] = "eeeeff0000f0feef000ffff00fe",
179
  [4] = "eeeef0fffffffeef0f00f00f0fe",
180
  [5] = "eeeeff00ffeeeeef0ff000ff0fe",
181
  [6] = "efffffff0feeeeefffff0fffffe",
182
  [7] = "ef0f0000ffeeeeef0fefffef0fe",
183
  [8] = "efffffffffeeeeefffeeeeefffe",
184
  [9] = "eeeeeeeeeeeeeeeeeeeeeeeeeee"
185
}
186
187
if not fs.exists("image") then
188
  save()
189
190
--  fs.delete("startup")
191
--  outputFile = io.open("startup", "a")
192
--  text = tostring(shell.getRunningProgram())
193
--  outputFile:write("shell.run(" .. text .. ")")
194
--  outputFile:close()
195
196
  fs.delete("image")
197
  outputFile = io.open("image", "w")
198
199
 outputFile:write(image_table[1])
200
  for num = 2,table.maxn(image_table) do
201
    imagewrite(image_table[num])
202
  end
203
  num = nil
204
  outputFile:close()
205
else
206
  load()
207
end
208
209
function freespace()
210
  a = "locating free space..."
211
  print(a)
212
  freespace = table.maxn(names) + 1
213
  a = "free space located at: "
214
  print(a .. freespace)
215
end
216
217
function target()
218
  a = "locating target..."
219
  print(a)
220
  x = 1
221
  repeat
222
    x = x + 1
223
  until names[x] == name1 or names[x] == nil
224
  if names[x] == name1 then
225
    target = x
226
    a = "target located at: "
227
    print(a .. target)
228
  else
229
    print("target button not found")
230
    target = "not found"
231
  end
232
end
233
234
function add_button()
235
  a = "adding..."
236
  print(a)
237
  freespace()
238
  names[freespace] = name1
239
  ids[freespace] = id
240
  status[freespace] = false
241
  a = "add successful"
242
  print(a)
243
end
244
245
function remove_button()
246
  a = "removing..."
247
  print(a)
248
  target()
249-
oa_x = oa_x - 10
249+
  if target ~= "not found" then
250
    freespace()
251
    source = freespace - 1
252
    names[target] = names[source]
253
    ids[target] = ids[source]
254
    status[target] = status[source]
255
    names[source] = nil
256
    ids[source] = nil
257
    status[source] = nil
258
    a = "removal successful"
259
    print(a)
260
  end
261
end
262
  
263
function change_button()
264
  print("changing " .. name1 .. id .. " to " .. name2 .. id2)
265
  remove_button()
266
  name1 = name2
267
  id = id2
268
  add_button()
269
  print("change successful")
270
end
271
272
function show_settings()
273
  x = 2
274
  while names[x] ~= nil do
275
    print(x .. ": " .. names[x] .. " " .. ids[x])
276
    x = x + 1
277
  end
278
  os.pullEvent()
279
end
280
281
function save2()
282
  print("confirm save? Y/N : ")
283
  saving = read()
284
  saving = tostring(saving)
285
  if saving == "y" then
286
    save()
287
  end
288
end
289
290
if command == "add" then
291
  add_button()
292
  save2()
293
elseif command == "remove" then
294
  remove_button()
295
  save2()
296
--elseif command == "change" then
297
--  change_button()
298
--  save2()
299
elseif command == "settings" then
300
  show_settings()
301
elseif command == "client" then
302
  shell.run("pastebin get " .. variables["client"] .. " client")
303
end
304
305
function find_peripheral(t, e)
306
  e = e or nil
307
  sides = {[1] = "front", [2] = "back", [3] = "left", [4] = "right", [5] = "top", [6] = "bottom"}
308
  peripheral_found = false
309
  for num=1, 6 do
310
    type = peripheral.getType(sides[num])
311
    if type == t then
312
      if e == "wireless" then
313
        if peripheral.call(sides[num], "isWireless") then
314
          return sides[num]
315
        end
316
      elseif e == "remote" then
317
        if not peripheral.call(sides[num], "isWireless") then
318
          return sides[num]
319
        end
320
      elseif e == nil then
321
        return sides[num]
322
      end
323
    end
324
  end
325
  if peripheral_found ~= true then
326
    error("No " .. t .. " attached to the computer", 0)
327
  end
328
end
329
330
-- MODEM REMOTE CONNECTIONS --
331
332
mod = peripheral.wrap(find_peripheral("modem", "remote"))
333
local remote_peripherals = mod.getNamesRemote()
334
remote_types = {}
335
remote_monitors = {[1] = peripheral.wrap(find_peripheral("monitor"))}
336
remote_rec = {}
337
remote_eu = {}
338
a = 1 
339
if remote_monitors[1] == nil then b = 1 else b = 2 end
340
c = 1 
341
d = 1
342
while remote_peripherals[a] ~= nil do
343
  remote_types[a] = mod.getTypeRemote(remote_peripherals[a])
344
  if remote_types[a] == "monitor" then
345
    remote_monitors[b] = peripheral.wrap(remote_peripherals[a])
346
    b = b + 1
347
  end
348
  if settings["open_peripherals"] and settings["energy_stats"] and settings["direct_power"] then
349
    if remote_types[a] == "cofh_thermalexpansion_energycell" then
350
      remote_rec[c] = peripheral.wrap(remote_peripherals[a])
351
      c = c + 1
352
    end
353
    if remote_types[a] == "mfsu" then
354
      remote_eu[d] = peripheral.wrap(remote_peripherals[a])
355
      d = d + 1
356
    end
357
  end
358
  a = a + 1
359
end
360
361
rednet.open(find_peripheral("modem", "wireless"))
362
mon = remote_monitors[1]
363
mon.setTextScale(settings["text_scale"])
364
oa_x, oa_y = mon.getSize()
365
366
if settings["energy_stats"] and settings["open_peripherals"] then
367
  oa_x = oa_x - 10
368
end
369
370
reps_x = math.floor(oa_x / (settings["button_width"] + 1)) 
371
reps_y = math.floor((oa_y / 2) - 1)
372
total_buttons = reps_x * reps_y
373
print(oa_x .. "," .. oa_y .. " - " .. reps_x .. " x " .. reps_y .. " - " .. total_buttons)
374
375
--FUNCTIONS DRAWING MONITOR SCREEN--
376
377
print("Drawing mon screen")
378
379
function reset_text()
380
  print("Reset text")
381
  mon.setTextColor(colors.white)
382
  mon.setBackgroundColor(colors.black)
383
end
384
385
function heading()
386
  print("heading")
387
  mon.setCursorPos(2,2)
388
  mon.setTextColor(settings["heading color"])
389
  mon.setBackgroundColor(settings["heading background"])
390
  mon.write(settings["heading"])
391
  reset_text()
392
end
393
394
395
function button_draw(number, xpos, ypos)
396
  mon.setCursorPos(xpos, ypos)
397
  button_x1[number], button_y1[number] = mon.getCursorPos()
398
  if names[number] ~= nil then
399
    if status[number] then
400
      mon.setBackgroundColor(settings["active color"])
401
    else
402
      mon.setBackgroundColor(settings["inactive color"])
403
    end
404
    text = names[number]:upper()
405
    text_length = string.len(text) + 2
406
    if text_length > settings["button_width"] then
407
      settings["button_width"] = text_length
408
    end
409
    mon.write(" " .. text .. " ")
410
    currpos_x, currpos_y = mon.getCursorPos()
411
    while currpos_x ~= (settings["button_width"] + xpos) do
412
      mon.write(" ")
413
      currpos_x, currpos_y = mon.getCursorPos()
414
    end
415
    button_x2[z], y = mon.getCursorPos()
416
417
    reset_text()
418
    print("Writing button " .. number .. ": " .. names[number] .. ": " .. tostring(status[number]))
419
  end
420
end
421
422
-- ENERGY FUNCTIONS -- 
423
424
function box(color, left, top, right, bot)
425
  term.redirect(mon)
426
  paintutils.drawLine(right, top, left, top, color)
427-
--    print("drawing monitor")
427+
428
  paintutils.drawLine(right, bot, left, bot, color)
429
  paintutils.drawLine(right, top, right, bot, color)
430
  term.restore()
431
end
432
433-
energy_stats(x, y)
433+
434-
settings["energy_room"] = endx
434+
435-
x = endx + 1
435+
436
  if number > 999 then
437
num = number / 1000
438
text = "K"
439
    if num > 999 then
440
num = num / 1000
441
text = "M"
442
      if num > 999 then num = num / 1000
443
text = "B"
444
      end
445
    end
446
  num = math.floor(num)
447
  end
448
  a = num or 0
449
  text = text or 0
450
  text = a .. text
451
  return text
452
end
453-
  sleep(0.1)
453+
454
function energy_compile()
455
  local a = 1
456
  mj["max"] = 0
457
mj["current"] = 0
458
mj["percent"] = 0
459
  while remote_rec[a] ~= nil do
460
    mj["max"] = mj["max"] + remote_rec[a].getMaxEnergyStored("north")
461
    mj["current"] = mj["current"] + remote_rec[a].getEnergyStored("north")
462
    a = a + 1
463
  end
464
465
  local a = 1
466
  eu["max"] = 0
467
  eu["current"] = 0
468
  eu["percent"] = 0
469
  while remote_eu[a] ~= nil do
470
    eu["max"] = eu["max"] + remote_eu[a].getCapacity()
471
    eu["current"] = eu["current"] + remote_eu[a].getStored()
472
    a = a + 1
473
  end
474
end
475
476
477
function percentage(type)
478
  if type == "mj" then
479
    mj["percent"] = mj["current"] / mj["max"] * 100
480
    return math.floor(mj["percent"])
481
  elseif type == "eu" then
482
    eu["percent"] = eu["current"] / eu["max"] * 100
483
    return math.floor(eu["percent"])
484
  end
485
end
486
487
function greaterx()
488
  currx = mon.getCursorPos()
489
  if currx > endx then
490
    endx = currx
491
  end
492
end
493
494
function energy_stats(startx, starty)
495
  if settings["direct_power"] then
496
    energy_compile()
497
  end
498
499
  x = startx + 2
500
  y = starty + 2
501
502
  mon.setTextColor(colors.white)
503
504
  mon.setCursorPos(x,y)
505
  mon.write("EU: " .. decimal(eu["current"]))
506
  endx = mon.getCursorPos()
507
  y = y + 1
508
509
  mon.setCursorPos(x,y)
510
  mon.write("EU: " .. decimal(eu["max"]))
511
  greaterx()
512
  y = y + 1
513
514
515
  mon.setCursorPos(x,y)
516
  mon.write("EU: " .. percentage("eu") .. "%")
517
  greaterx()
518
  y = y + 2
519
520
521
  mon.setCursorPos(x,y)
522
  mon.write("MJ: " .. decimal(mj["current"]) )
523
  greaterx()
524
  y = y + 1
525
526
  mon.setCursorPos(x,y)
527
  mon.write("MJ: " .. decimal(mj["max"]) )
528
  greaterx()
529
  y = y + 1
530
531
 
532
  mon.setCursorPos(x,y)
533
  mon.write("MJ: " .. percentage("mj") .. "%")
534
  greaterx()
535
  endy = y + 2
536
  endx = endx + 1
537
  box(colors.white, startx, starty, endx, endy)
538
end
539
540
-- MONITOR SCREEN PROGRAM --
541
542
function mon_screen()
543
544
  screen = 1
545
  while remote_monitors[screen] ~= nil do
546
    mon = remote_monitors[screen]
547
548
    print("drawing monitor")
549
550
    mon.clear()
551
552
    heading()
553
  
554
    x = 2
555
    y = 4
556
557
    if settings["energy_stats"] then
558
      energy_stats(x, y)
559
      settings["energy_room"] = endx
560
      x = endx + 1
561
    end
562
  
563
    z = 1
564
565
    for num=1,reps_x do
566
      y = 4
567
568
      for num=1,reps_y do
569
        button_draw(z, x, y)
570
        y = y + 2
571
        z = z + 1
572
      end
573
574
      x = x + settings["button_width"] + 1
575
    end
576
    save()
577
    screen = screen + 1
578
  end
579
end
580
581
--FUNCTION BUTTON DETECT--
582
583
function select_button()
584
  z = 1
585
  button_pressed = nil
586
587
  repeat
588
    if names[z] ~= nil then
589
      if x > button_x1[z] and x < button_x2[z] and y == button_y1[z] then
590
        return z
591
      end
592
    end
593
    z = z + 1
594
  until z > total_buttons
595
end
596
597
function message_send()
598
  if status[button_pressed] then
599
    text = "deactivate"
600
    status[button_pressed] = not status[button_pressed]
601
  else
602
    text = "activate"
603
    status[button_pressed] = not status[button_pressed]
604
  end
605
606
  rednet.send(ids[button_pressed], text)
607
  print(names[button_pressed] .. " " .. ids[button_pressed] .. " " .. text)
608
609
  mon_screen()
610
end
611
612
function offline()
613
  status[1] = false
614
  a = 2
615
  while names[a] ~= nil do
616
    if status[a] then
617
      status[a] = false
618
      text = "deactivate"
619
      rednet.send(ids[a], text)
620
    end
621
    a = a + 1
622
  end
623
  mon_screen()
624
end
625
626
-- SCREEN SAVER --
627
628
function image_draw()
629
  screen = 1
630
  image = {}
631
  image = paintutils.loadImage("image")
632
  while remote_monitors[screen] ~= nil do
633
    print("image: " .. screen)
634
    mon = remote_monitors[screen]
635
    mon.clear()
636
    oa_x, oa_y = mon.getSize()
637
    term.redirect(mon)
638
    x = (oa_x / 2) - settings["image_x"]
639
    y = (oa_y / 2) - settings["image_y"]
640
    paintutils.drawImage(image, x, y)
641
    term.restore()
642
    reset_text()
643
    screen = screen + 1
644
  end
645
end
646
647
648
function screen_saver()
649
  print("screen Saver")
650
651
  image_draw()
652
  os.pullEvent("monitor_touch")
653
end
654
655
-- PROGRAM --
656
657
mon_screen()
658
while true do
659
  if settings["direct_power"] or settings["screen_saver"] then
660
    os.startTimer(settings["refresh_rate"])
661
  end
662
663
  event, id, x, y  = os.pullEvent()
664
665
  if event == "monitor_touch" then
666
    button_pressed = select_button()
667
    e = 1
668
    if names[button_pressed] ~= nil then
669
      if button_pressed ~= 1 then
670
        if status[1] then
671
          message_send()
672
        end
673
      else
674
675
        if status[1] then
676
          offline()
677
        else
678
          status[1] = true
679
--          startup_buttons
680
        end
681
      end
682
    end
683
684
    mon_screen()
685
686
  elseif event == "rednet_message" then
687
688
    if id == settings["eu_id"] then
689
      eu = textutils.unserialize (x)
690
    elseif id == settings["mj_id"] then
691
      mj = textutils.unserialize (x)
692
    end
693
694
    mon_screen()
695
696
  elseif event == "timer" then
697
    print("timer")
698
    if settings["screen_saver"] then
699
      e = e + 1
700
      print("Timeout: " .. e)
701
      if e == settings["timeout"] then
702
        screen_saver()
703
        e = 1
704
      end
705
    end
706
    mon_screen()
707
  end 
708
end