Advertisement
Guest User

startup

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