Advertisement
Guest User

startup

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