Advertisement
axembin

Turret Hotkey Script v2

Aug 22nd, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.63 KB | None | 0 0
  1. #Conditional Hooks
  2. $Application: FS2_Open
  3.  
  4. $On Game Init:
  5. [
  6.  
  7. TurretHotkey = {}
  8.  
  9. function TurretHotkey:Init()
  10.  
  11.     self.Enabled = true
  12.     self.Keys = {"F5","F6","F7","F8","F9","F10","F11","F12"}
  13.     self.ActiveKey = 0
  14.     self.CycleIndex = nil
  15.     self.LastKey = 0
  16.     self.Selected = 0
  17.     self.Number = 0
  18.     self.Targets = {} -- self.Targets[keyindex][j] => ship (object signature), subsys (string)
  19.  
  20. end
  21.  
  22. function TurretHotkey:KeyLookup(key, required)
  23.  
  24.     local value
  25.    
  26.     ba.print("Beginning key lookup...\nKey is: " .. key .. "\n")
  27.    
  28.     for j = 1, #self.Keys do
  29.         --ba.print("At index:" .. j .. ", value:" .. self.Keys[j] .. "...")
  30.         if self.Keys[j] == string.upper(key) then
  31.             --ba.print("MATCH!\n")
  32.             value = j
  33.             return value, true
  34.         else
  35.             --ba.print("NO MATCH!\n")
  36.         end
  37.     end
  38.    
  39.     if not value and required then
  40.         return self:KeyLookup("F9"), false
  41.     end
  42.    
  43.     if not value and not required then
  44.         return self.ActiveKey, false
  45.     end
  46.    
  47. end
  48.  
  49. function TurretHotkey:Add(ship, subsys, text, key)
  50.  
  51.     if not self.Enabled then
  52.         self:Init()
  53.     end
  54.  
  55.     if not ship then
  56.         ba.print("TurretHotkey:Add(): No ship defined!\n")
  57.         return
  58.     end
  59.    
  60.     if not subsys then
  61.         ba.print("TurretHotkey:Add(): No subsys defined!\n")
  62.         return
  63.     end
  64.    
  65.     local t = {}
  66.  
  67.     if mn.Ships[ship]:isValid() then
  68.         local thisShip = mn.Ships[ship]
  69.         t.Ship = thisShip:getSignature()
  70.        
  71.         if thisShip[subsys]:isValid() then
  72.             local thisSubsys = thisShip[subsys]
  73.             t.Subsys = thisSubsys:getModelName()
  74.         else
  75.             return
  76.         end
  77.        
  78.         if showinfo then
  79.             t.ShowInfo = true
  80.         else
  81.             t.ShowInfo = nil
  82.         end
  83.        
  84.         if text then
  85.             t.Text = text
  86.         end
  87.        
  88.         self.Number = self.Number + 1
  89.        
  90.         t.Number = self.Number
  91.        
  92.         index = self:KeyLookup(key or "F9", true)
  93.        
  94.         ba.print("Added [" .. thisShip.Name .. "]:[" .. tostring(t.Subsys) .. "] to index " .. tostring(index) .. "\n")
  95.        
  96.     end
  97.    
  98.     if not self.Targets[index] then
  99.         ba.print("Key not used before, creating group...\n")
  100.         self.Targets[index] = {}
  101.     end
  102.        
  103.     self.Targets[index][#self.Targets[index]+1] = t
  104.        
  105. end
  106.  
  107. function TurretHotkey:Rem(ship, subsys)
  108.  
  109.     if not self.Enabled then
  110.         return
  111.     end
  112.  
  113.     if not ship then
  114.         ba.print("TurretHotkey:Rem(): No ship defined!\n")
  115.         return
  116.     end
  117.    
  118.     for i=1, #self.Keys do
  119.         if self.Targets[i] then
  120.             local thisEntry = self.Targets[i]
  121.             for j=#thisEntry, 1, -1 do
  122.                 local thisShip = mn.getObjectFromSignature(thisEntry[j].Ship)
  123.                 if thisShip:isValid() and thisShip.Name == ship then
  124.                     if not subsys then
  125.                         table.remove(thisEntry,j)
  126.                     elseif subsys == self.Targets.subsys then
  127.                         if self.Selected == thisGroup[j].Number then
  128.                             self:CycleTargets(i)
  129.                         end
  130.                         table.remove(thisEntry,j)
  131.                     end
  132.                 end
  133.             end
  134.         end
  135.     end
  136.  
  137. end
  138.  
  139. function TurretHotkey:Pressed(key)
  140.  
  141.     local flag = false
  142.  
  143.     self.LastKey = self.ActiveKey
  144.     self.ActiveKey, flag = self:KeyLookup(key)
  145.    
  146.     if self.LastKey ~= self.ActiveKey then
  147.         self.CycleIndex = nil
  148.     end
  149.    
  150.     if flag then
  151.         ba.print("Attempting to cycle...\n")
  152.         self:CycleTargets(self.ActiveKey)
  153.     end
  154.  
  155. end
  156.  
  157. function TurretHotkey:CycleTargets(key)
  158.  
  159.     self.CycleIndex = self.CycleIndex or 1
  160.     local thisGroup = self.Targets[key]
  161.        
  162.     if thisGroup and #thisGroup > 0 then
  163.         for i=1, #thisGroup do
  164.             if self.CycleIndex == i then
  165.                 local thisEntry = thisGroup[i]
  166.                 if thisEntry then
  167.                 local thisShip = mn.getObjectFromSignature(thisEntry.Ship)
  168.                     if thisShip:isValid() then
  169.                         local thisSubsys = thisShip[thisEntry.Subsys]
  170.                        
  171.                         if thisSubsys.HitpointsLeft > 0 then
  172.                             ad.playGameSound(8)
  173.                             hv.Player.TargetSubsystem = thisSubsys
  174.                             self.Selected = thisEntry.Number
  175.                            
  176.                             if i+1 <= #thisGroup then
  177.                                 self.CycleIndex = self.CycleIndex+1
  178.                             else
  179.                                 self.CycleIndex = 1
  180.                             end
  181.                         end
  182.                        
  183.                         return
  184.                     end
  185.                 end    
  186.             end
  187.         end
  188.     end
  189.    
  190. end
  191.  
  192. function TurretHotkey:Prune()
  193.  
  194.     for i=1, #self.Keys do
  195.         local thisGroup = self.Targets[i]
  196.         if thisGroup then
  197.             for j=#thisGroup, 1, -1 do
  198.                 local thisShip = mn.getObjectFromSignature(thisGroup[j].Ship)
  199.                 if not thisShip:isValid() then
  200.                     table.remove(self.Targets[i],j)
  201.                 else
  202.                     local thisSubsys = thisShip[thisGroup[j].Subsys]
  203.                     if thisSubsys.HitpointsLeft == 0 then
  204.                         if self.Selected == thisGroup[j].Number then
  205.                             self:CycleTargets(i)
  206.                         end
  207.                         table.remove(self.Targets[i],j)
  208.                     end
  209.                 end
  210.             end
  211.         end
  212.     end
  213.  
  214. end
  215.  
  216.  
  217. function TurretHotkey:Draw()
  218.  
  219.     --gr.drawString("Coords:",100,100)
  220.  
  221.     for i=1, #self.Keys do
  222.         local thisGroup = self.Targets[i]
  223.         if self.ActiveKey == i and thisGroup then
  224.             for j=1, #thisGroup do
  225.                 local thisEntry = thisGroup[j]
  226.                 local thisShip = mn.getObjectFromSignature(thisEntry.Ship)     
  227.                 if thisShip:isValid() then             
  228.                     local thisSubsys = thisShip[thisEntry.Subsys]
  229.                    
  230.                     if thisSubsys:isValid() then
  231.                    
  232.                         if thisSubsys.HitpointsLeft == 0 then
  233.                             gr.setColor(128,128,128,200)
  234.                         else
  235.                             gr.setColor(255,255,255,200)
  236.                         end
  237.                        
  238.                         local x1, y1, x2, y2 = gr.drawSubsystemTargetingBrackets(thisSubsys, true, false)
  239.                        
  240.                         --gr.setColor(255,255,255,255)
  241.                         --gr.drawString(tostring(x1) .. "," .. tostring(y1) .. "," .. tostring(x2) .. "," .. tostring(y2))
  242.                        
  243.                         if x1 and y1 and x2 and y2 then
  244.                             gr.CurrentFont = gr.Fonts[1]
  245.                            
  246.                             local name = thisEntry.Text
  247.                            
  248.                             if name then                           
  249.                                 local r,g,b = thisShip.Team:getColor()
  250.                                
  251.                                 if self.Selected ~= thisEntry.Number then
  252.                                     r = r * 0.75
  253.                                     g = g * 0.75
  254.                                     b = b * 0.75
  255.                                 end
  256.                                                                
  257.                                 gr.setColor(r,g,b,200)
  258.  
  259.                                 if gr.getStringWidth(name) < ((x2-x1)+100) then
  260.                                     gr.drawString(name, x1, y1 - 10)
  261.                                 end
  262.                             end
  263.                            
  264.                             local health = (thisSubsys.HitpointsLeft/thisSubsys.HitpointsMax) * 100
  265.                             local healthDisplay = string.format("%d%%",health)
  266.                            
  267.                             if gr.getStringWidth(healthDisplay) < ((x2-x1)+50) then
  268.                                 gr.drawString(healthDisplay, x1, y2 + 5)
  269.                             end
  270.                         end
  271.                     end
  272.                 end
  273.             end
  274.         end
  275.     end
  276.  
  277. end
  278.  
  279. ]
  280.  
  281. $State: GS_STATE_GAME_PLAY
  282. $On Gameplay Start:
  283. [
  284.     if TurretHotkey.Enabled then
  285.         TurretHotkey.Enabled = nil
  286.     end
  287. ]
  288.  
  289. $On State End:
  290. [
  291.     TurretHotkey.Enabled = nil
  292. ]
  293.  
  294. $On Key Released:
  295. [
  296.     if TurretHotkey.Enabled then
  297.         --ba.print("Key pressed: " .. hv.Key .. "\n")
  298.         TurretHotkey:Pressed(hv.Key)
  299.     end
  300. ]
  301.  
  302. $On Frame:
  303. [
  304.     if TurretHotkey.Enabled then
  305.         TurretHotkey:Draw()
  306.         TurretHotkey:Prune() --Ideally if there was a On Subsystem Destroyed hook, we'd put this there but oh well!
  307.         --PrintDebug(TurretHotkey)
  308.     end
  309. ]
  310.  
  311. #End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement