Advertisement
Guest User

monitoring

a guest
Sep 1st, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.08 KB | None | 0 0
  1. mon = peripheral.wrap("top")
  2. distributor = 3
  3. rednet.open("right")
  4.  
  5. home = {}
  6. bayArm = {}
  7. warning = {}
  8.  
  9. bay = {}
  10. bay[1] = {}
  11. bay[2] = {}
  12. bay[3] = {}
  13.  
  14. bay[1][1] = 12
  15. bay[1][2] = 13
  16. bay[2][1] = 11
  17. bay[2][2] = 10
  18. bay[3][1] = 8
  19. bay[3][2] = 9
  20.  
  21. function cText(text,centering)
  22.   x,y = mon.getCursorPos()
  23.   x2,y2 = mon.getSize()
  24.   newX = math.ceil((x2*centering) - (text:len() / 2))
  25.   newY = y2
  26.   mon.setCursorPos(newX, newY)
  27.  
  28.   mon.write(text)
  29. end
  30.  
  31. function advancedText(screen,text,yVal,height,cent,tColor,bColor,b2Color,func,a1,a2,a3)
  32.   x,y = mon.getCursorPos()
  33.   x2,y2 = mon.getSize()
  34.   newX = math.ceil((x2*cent) - (text:len() / 2))
  35.   newY = yVal
  36.   mon.setCursorPos(newX,newY)
  37.  
  38.   curTableSize = table.getn(screen)
  39.   writeNum = curTableSize + 1
  40.   --print(writeNum)
  41.   --print(text)
  42.   screen[writeNum] = {}
  43.   screen[writeNum]["text"] = text
  44.   screen[writeNum]["x"] = newX
  45.   screen[writeNum]["y"] = newY
  46.   screen[writeNum]["x2"] = newX + text:len()
  47.   screen[writeNum]["y2"] = newY + (height - 1)
  48.   screen[writeNum]["color"] = tColor
  49.   if bColor ~= nil then
  50.     screen[writeNum]["bg1"] = bColor
  51.   else
  52.     screen[writeNum]["bg1"] = colors.black
  53.   end
  54.   if b2Color ~= nil then
  55.     screen[writeNum]["bg2"] = b2Color
  56.   else
  57.     screen[writeNum]["bg2"] = bColor
  58.   end
  59.   screen[writeNum]["function"] = func
  60.   screen[writeNum]["arg1"] = a1
  61.   screen[writeNum]["arg2"] = a2
  62.   screen[writeNum]["arg3"] = a3
  63.   screen[writeNum]["curBG"] = bColor
  64. end
  65.  
  66.  
  67.  
  68.  
  69. function displayScreen(screen, bgColor)
  70.   home = {}
  71.   bayArm = {}
  72.   curScreen = {}
  73.  
  74.   curScreen = screen
  75.  
  76.   mon.setBackgroundColor(bgColor)
  77.   mon.clear()
  78.  
  79.   for k, v in ipairs(screen) do
  80.     mon.setCursorPos(screen[k].x, screen[k].y)
  81.     boxHeight = (screen[k].y2 + 1) - screen[k].y
  82.     textLine = math.ceil(boxHeight/2)
  83.    
  84.     mon.setTextColor(screen[k].color)
  85.     mon.setBackgroundColor(screen[k].curBG)
  86.     for i = 1, boxHeight do
  87.       curX,curY = mon.getCursorPos()
  88.       if i == textLine then
  89.         mon.write(screen[k].text)
  90.       else
  91.         for q = 1, screen[k].text:len() do
  92.           mon.write(" ")
  93.         end
  94.       end
  95.       mon.setCursorPos(screen[k].x,curY+1)
  96.     end
  97.   end
  98.   curScreen = screen
  99.   getTouch(curScreen)
  100. end
  101.  
  102.    
  103.        
  104. function getTouch(screen)
  105.   print("waiting on touch")
  106.   event, monSide, xPos, yPos = os.pullEvent("monitor_touch")
  107.   print("I WAS TOUCHED")
  108.   for k,v in ipairs(screen) do
  109.     if xPos >= screen[k].x and xPos <= screen[k].x2 and yPos >= screen[k].y and yPos <= screen[k].y2 then
  110.       print(screen[k].text.." was touched")
  111.       if screen[k]["function"] ~= nil then
  112.         --print("Gonna be running: "..screen[k]["function"])
  113.         if screen[k].arg1 ~= nil then
  114.           if screen[k].arg2 ~= nil then
  115.             if screen[k].arg3 ~= nil then
  116.               print("Running 3 arg func")
  117.               screen[k]["function"](screen[k].arg1,screen[k].arg2,screen[k].arg3)
  118.             else
  119.               print("Running 2 arg func")
  120.               screen[k]["function"](screen[k].arg1,screen[k].arg2)
  121.             end
  122.           else
  123.             print("Running 1 arg func")
  124.             screen[k]["function"](screen[k].arg1)
  125.           end
  126.         else
  127.           print("Running no arg func")
  128.           screen[k]["function"]()
  129.         end
  130.       end
  131.     end
  132.   end    
  133.   displayScreen(curScreen, colors.black)
  134. end    
  135.  
  136.  
  137.  
  138. function getMissiles()
  139.  
  140.   rednet.send(distributor, "list")
  141.  
  142.   id,mes,dis = rednet.receive()
  143.  
  144.   if id == distributor then
  145.     missileTable = mes
  146.     myMissiles = {}
  147.     for k,v in ipairs(missileTable) do
  148.       writeValue = true
  149.       curTableVal = table.getn(myMissiles)
  150.       if curTableVal ~= 0 then
  151.         print("checking duplicates for: "..v)
  152.         writeValue = true
  153.         for j,q in ipairs(myMissiles) do
  154.           if v == q then
  155.             print("found duplicate")
  156.             writeValue = false
  157.           end
  158.         end
  159.         if writeValue then
  160.           curTableVal = table.getn(myMissiles)
  161.           myMissiles[curTableVal + 1] = v
  162.         end
  163.       else
  164.         print("Adding first missile")
  165.         myMissiles[1] = v
  166.       end
  167.     end
  168.     --for k,v in ipairs(myMissiles) do
  169.       --print(k..": "..v)
  170.     --end
  171.     return myMissiles
  172.   end
  173. end
  174.  
  175.  
  176.  
  177.  
  178. function getBays()
  179.   loadedBay = {}
  180.   loadedBay[1] = {}
  181.   loadedBay[2] = {}
  182.   loadedBay[3] = {}
  183.   for k,v in ipairs(bay) do
  184.     for l,q in ipairs(v) do
  185.       rednet.send(q,"report")
  186.       id, mes, dis = rednet.receive()
  187.       if id == q then
  188.         --print("K: "..k)
  189.         --print("L: "..l)
  190.         if mes == "EMPTY" then
  191.           loadedBay[k][l] = "UNARMED"
  192.         else
  193.           loadedBay[k][l] = mes
  194.         end
  195.       end
  196.     end
  197.   end
  198.   --for k,v in ipairs(loadedBay) do
  199.     --for l,q in ipairs(v) do
  200.       --print(l..": "..q)
  201.    -- end
  202.   --end
  203.   return loadedBay  
  204. end
  205.  
  206.  
  207.  
  208.  
  209. function armBay(bNum,lNum)
  210.   print("arming bay")
  211.   bayArm = {}
  212.   availMissiles = getMissiles()
  213.  
  214.   mon.clear()
  215.   mon.setCursorPos(1,1)
  216.   mon.write("Loading...")
  217.  
  218.   bayNum = tonumber(bNum)
  219.   launchNum = tonumber(lNum)
  220.  
  221.   advancedText(bayArm," Bay: "..bayNum.." Launcher: "..launchNum.." ",1,3,1/2,colors.white,colors.orange,colors.orange)
  222.  
  223.   rowNum = 5
  224.   colNum = 1
  225.   for k,v in ipairs(availMissiles) do
  226.     --print(v.name)
  227.     for token in string.gmatch(v,"[^%s]+") do
  228.       if token ~= "Missile" then
  229.         curText = token
  230.       end
  231.     end
  232.     advancedText(bayArm,curText,rowNum,1,(colNum/5),colors.white,colors.blue,colors.blue,sendMissile,curText,bayNum,launchNum)
  233.     if colNum == 4 then
  234.       rowNum = rowNum + 2
  235.       colNum = 1
  236.     else
  237.       colNum = colNum + 1
  238.     end
  239.   end  
  240.   displayScreen(bayArm,colors.black)
  241. end  
  242.  
  243.  
  244. function sendMissile(name,bay,comp)
  245.   local mesTable = {}
  246.   mesTable[1] = name
  247.   mesTable[2] = bay
  248.   mesTable[3] = comp
  249.   rednet.send(distributor,mesTable)
  250.   mon.setBackgroundColor(colors.black)
  251.   mon.clear()
  252.   mon.setCursorPos(1,1)
  253.   mon.write("Loading...")
  254.   sleep(7.25)
  255.   mainMenu()
  256. end
  257.  
  258.  
  259. function disarmMessage(bayNum,comp)
  260.   rednet.send(bay[bayNum][comp],"disarm")
  261.   sleep(.5)
  262.   mainMenu()
  263. end
  264.  
  265. function launchMessage(bayNum,comp)
  266.   rednet.send(bay[bayNum][comp],"launch")
  267.   mon.setBackgroundColor(colors.black)
  268.   mon.clear()
  269.   mon.setCursorPos(1,1)
  270.   cText("Launching",1/2)
  271.   sleep(5)
  272.   mainMenu()
  273. end
  274.  
  275. function launchAll()
  276.   mon.setBackgroundColor(colors.black)
  277.   mon.clear()
  278.   mon.setCursorPos(1,1)
  279.   cText("Fucking shit up...",1/2)
  280.   for k,v in ipairs(bay) do
  281.     rednet.send(bay[k][1], "launch")
  282.     rednet.send(bay[k][2], "launch")
  283.   end
  284.   sleep(10)
  285.   mainMenu()
  286. end
  287.  
  288. function mainMenu()
  289.  
  290.   --armedMissiles = getMissiles()
  291.   print("Checking armed bays")
  292.   armedBays = getBays()
  293.   print("Bays checked")
  294.  
  295.   advancedText(home," Missile Controller V1 ",1,3,1/2,colors.white,colors.red,colors.red)
  296.   numBays = table.getn(bay)
  297.  
  298.   launchNum = 1
  299.   for k,v in ipairs(armedBays) do
  300.     advancedText(home," Bay: "..k.." ",5,1,k/(numBays+1),colors.black,colors.orange,colors.orange)
  301.     advancedText(home,"Launcher "..launchNum,7,1,k/(numBays+1),colors.white,colors.blue,colors.red)
  302.     advancedText(home,"Launch All",1,1,7/8,colors.white,colors.magenta,colors.magenta,launchAll)
  303.     if v[1] ~= "UNARMED" then
  304.       for token in string.gmatch(v[1], "[^%s]+") do
  305.         if token ~= "Missile" then
  306.           curText = token
  307.         end
  308.       end
  309.       advancedText(home,curText,8,1,k/(numBays+1),colors.black,colors.green,colors.green)
  310.       advancedText(home," Launch ",10,1,k/(numBays+1),colors.black,colors.red,colors.red,launchMessage,k,launchNum)
  311.       advancedText(home," Disarm ",12,1,k/(numBays+1),colors.red,colors.lightGray,colors.lightGray,disarmMessage,k,launchNum)
  312.     else
  313.       advancedText(home," UNARMED ",8,1,k/(numBays+1),colors.black,colors.red,colors.red)
  314.       advancedText(home," Arm ",10,1,k/(numBays+1),colors.red,colors.lightGray,colors.lightGray,armBay,k,launchNum)
  315.     end  
  316.     launchNum = launchNum + 1
  317.     advancedText(home,"Launcher "..launchNum,14,1,k/(numBays+1),colors.white,colors.blue,colors.red)
  318.     if v[2] ~= "UNARMED" then
  319.       for token in string.gmatch(v[2], "[^%s]+") do
  320.         if token ~= "Missile" then
  321.           curText = token
  322.         end
  323.       end
  324.       advancedText(home,curText,15,1,k/(numBays+1),colors.black,colors.green,colors.green)
  325.       advancedText(home," Launch ",17,1,k/(numBays+1),colors.black,colors.red,colors.red,launchMessage,k,launchNum)
  326.       advancedText(home," Disarm ",19,1,k/(numBays+1),colors.red,colors.lightGray,colors.lightGray,disarmMessage,k,launchNum)
  327.     else
  328.       advancedText(home," UNARMED ",15,1,k/(numBays+1),colors.black,colors.red,colors.red)  
  329.       advancedText(home," Arm ",17,1,k/(numBays+1),colors.red,colors.lightGray,colors.lightGray,armBay,k,launchNum)
  330.     end
  331.     if launchNum == 1 then
  332.       launchNum = launchNum + 1
  333.     else
  334.       launchNum = 1
  335.     end
  336.   end
  337.   displayScreen(home,colors.black)
  338. end
  339.  
  340. --armBay(1,1)
  341. mainMenu()
  342. --advancedText(home,"  Missile Bays  ",1,3,1/2,colors.black,colors.red,colors.red)
  343. --displayScreen(home,colors.black)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement