Advertisement
Guest User

startup

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