Advertisement
Guest User

startup

a guest
Jul 21st, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.44 KB | None | 0 0
  1. -- variables
  2.  
  3. local dColorB = colors.black
  4. local dColorF = colors.white
  5.  
  6. local monitor
  7. local modem
  8. local targetMonitor
  9. local transporter
  10. local mode = 0
  11. local x = 0
  12. local y = 0
  13. local z = 0
  14. local bL
  15. local bE
  16. local locked = false
  17. local energizeCountDown = -1
  18.  
  19. -- setup check
  20.  
  21. for _, name in ipairs(peripheral.getNames()) do
  22.  
  23.   if (peripheral.getType(name) == "monitor") then
  24.     monitor = peripheral.wrap(name)
  25.   end
  26.  
  27.   if (peripheral.getType(name) == "warpdriveTransporterCore") then
  28.     transporter = peripheral.wrap(name)
  29.   end
  30.  
  31.   if (peripheral.getType(name) == "warpdriveMonitor") then
  32.     targetMonitor = peripheral.wrap(name)
  33.   end
  34.  
  35.   if (peripheral.getType(name) == "modem") then
  36.     modem = peripheral.wrap(name)
  37.   end
  38.  
  39. end
  40.  
  41. assert(not (targetMonitor == nil))
  42. assert(not (monitor == nil))
  43. assert(not (transporter == nil))
  44. assert(not (modem == nil))
  45.  
  46. monitor.setBackgroundColor(dColorB)
  47. monitor.setTextColor(dColorF)
  48. monitor.setTextScale(1)
  49. monitor.clear()
  50.  
  51. targetMonitor.videoChannel(66)
  52. modem.open(3)
  53. transporter.beamFrequency(616)
  54. transporter.energyFactor(4)
  55. transporter.lock(false)
  56.  
  57. -- buttons
  58.  
  59. buttonsCount = 0
  60. buttons = {}
  61.  
  62. Button =
  63. {
  64.   x = 0,
  65.   y = 0,
  66.   h = 0,
  67.   w = 0,
  68.   text = "",
  69.   colorB = colors.gray,
  70.   colorF = colors.blue,
  71.   onClick
  72. }
  73.  
  74. function Button:new(o, x, y, h, w, text, colorB, colorF, onClick)
  75.  
  76.   o = o or {}
  77.  
  78.   o.x = x
  79.   o.y = y
  80.   o.h = h
  81.   o.w = w
  82.   o.text = text
  83.   o.colorB = colorB
  84.   o.colorF = colorF
  85.   o.onClick = onClick
  86.  
  87.   buttons[buttonsCount] = o
  88.   buttonsCount = buttonsCount + 1
  89.  
  90.   Button.draw(o)
  91.  
  92.   return o
  93. end
  94.  
  95. function Button.draw(self)
  96.   monitor.setBackgroundColor(self.colorB)
  97.  
  98.   for i = 0, self.h - 1 do
  99.     monitor.setCursorPos(self.x, self.y +i)
  100.     monitor.write(string.rep(" ", self.w))
  101.   end
  102.  
  103.   monitor.setTextColor(self.colorF)
  104.   monitor.setCursorPos(self.x + math.floor((self.w - string.len(self.text)) / 2), self.y + math.floor(self.h / 2))
  105.   monitor.write(self.text)
  106.  
  107.   monitor.setBackgroundColor(dColorB)
  108.   monitor.setTextColor(dColorF)
  109. end
  110.  
  111. -- drawing
  112.  
  113. monitor.setCursorPos(1, 1)
  114. monitor.write("Mode:")
  115. monitor.setCursorPos(1, 2)
  116. monitor.write("Target:")
  117.  
  118. function wipe(x, y, n)
  119.   monitor.setCursorPos(x, y)
  120.   monitor.write(string.rep(" ", n))
  121.   monitor.setCursorPos(x, y)
  122. end
  123.  
  124. function drawInfo()
  125.  
  126.   wipe(7, 1, 25)
  127.  
  128.   if (mode == 0) then
  129.     monitor.setTextColor(colors.green)
  130.     monitor.write("Transportation")
  131.   elseif (mode == 1) then
  132.     monitor.setTextColor(colors.red)
  133.     monitor.write("Combat")
  134.   end
  135.  
  136.   wipe(9, 2, 25)
  137.  
  138.   monitor.setTextColor(dColorF)
  139.   monitor.write(x.." "..y.." "..z)
  140.  
  141.   if (mode == 0) then
  142.     acquisitionEnergy, energizeEnergy = transporter.getEnergyRequired()
  143.  
  144.     wipe(1, 4, 40)
  145.     monitor.write("Lock Energy: "..acquisitionEnergy.."eu")
  146.     wipe(1, 5, 40)
  147.     monitor.write("Energizing Energy: "..energizeEnergy.."eu")
  148.    
  149.     strength = math.floor(transporter.getLockStrength() * 100)
  150.     wipe(1, 6, 40)
  151.     monitor.write("Lock strength: ")
  152.    
  153.     if (strength < 60) then
  154.       monitor.setTextColor(colors.red)
  155.     elseif (strength < 80) then
  156.       monitor.setTextColor(colors.orange)
  157.     else
  158.       monitor.setTextColor(colors.green)
  159.     end
  160.    
  161.     monitor.write(strength.."%")
  162.    
  163.     if (locked) then
  164.    
  165.       if (strength < 60) then
  166.         bE.colorB = colors.orange
  167.       else
  168.         bE.colorB = colors.green
  169.       end
  170.    
  171.       if (energizeCountDown < 0) then
  172.         bE.text = "ENERGIZE"
  173.       else
  174.         bE.text = math.floor(energizeCountDown)
  175.       end
  176.     else
  177.       bE.text = "ENERGIZE"
  178.       bE.colorB = colors.red
  179.     end
  180.    
  181.     Button.draw(bE)
  182.   end
  183. end
  184.  
  185. function touchEvent(colum, row)
  186.   for i = 0, buttonsCount - 1 do
  187.     if (buttons[i].x <= colum and buttons[i].x + buttons[i].w >= colum and buttons[i].y <= row and buttons[i].y + buttons[i].h >= row) then
  188.       buttons[i].onClick(buttons[i], true)
  189.     end
  190.   end
  191. end
  192.  
  193. for key, _ in pairs(transporter) do
  194.      --print(key)
  195. end
  196.  
  197. -- callBacks
  198.  
  199. function callBack()
  200.   drawInfo()
  201.  
  202.   if (mode == 0) then
  203.     if (energizeCountDown >= 0) then
  204.       energizeCountDown = energizeCountDown - 1
  205.    
  206.       if (energizeCountDown == 0) then
  207.         transporter.energize(true)
  208.       end
  209.     elseif (redstone.getInput("back")) then
  210.       energizeCountDown = 5
  211.     end
  212.   end
  213.  
  214. end
  215.  
  216. function transporterCallback()
  217.   transporter.remoteLocation(x, y, z)
  218. end
  219.  
  220. function lockButton()
  221.   locked = not locked
  222.  
  223.   if (locked) then
  224.     bL.colorB = colors.red
  225.     bL.text = "UNLOCK"
  226.   else
  227.     bL.colorB = colors.green
  228.     bL.text = "LOCK"
  229.   end
  230.  
  231.   transporter.lock(locked)
  232.   Button.draw(bL)
  233. end
  234.  
  235. function energizeButton()
  236.   if (locked and energizeCountDown < 0) then
  237.     energizeCountDown = 5
  238.   end
  239. end
  240.  
  241. -- buttons
  242.  
  243. bL = Button:new(nil, 2, 9, 3, 10, "LOCK", colors.green, colors.white, lockButton)
  244. bE = Button:new(nil, 14, 9, 3, 14, "ENERGIZE", colors.green, colors.white, energizeButton)
  245.  
  246. -- loop
  247.  
  248. local loop = true
  249. local interval = 1
  250.  
  251. local timer = os.startTimer(0)
  252.  
  253. while loop do
  254.   local event, arg1, arg2, arg3, arg4 = os.pullEvent()
  255.  
  256.   if (event == "timer") then
  257.     callBack()
  258.     os.startTimer(interval)
  259.   end
  260.  
  261.   if (event == "key") then
  262.     if (arg1  == keys.space) then
  263.       break
  264.     end
  265.   end
  266.  
  267.   if (event == "monitor_touch") then
  268.     touchEvent(arg2, arg3)  
  269.   end
  270.  
  271.   if (event == "modem_message") then
  272.     target = textutils.unserialize(arg4)
  273.     x = target.x
  274.     y = target.y
  275.     z = target.z
  276.  
  277.     transporterCallback()
  278.   end
  279. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement