Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Conditional Hooks
- $Application: FS2_Open
- $On Game Init:
- [
- TurretHotkey = {}
- function TurretHotkey:Init()
- self.Enabled = true
- self.Keys = {"F5","F6","F7","F8","F9","F10","F11","F12"}
- self.ActiveKey = 0
- self.CycleIndex = nil
- self.LastKey = 0
- self.Selected = 0
- self.Number = 0
- self.Targets = {} -- self.Targets[keyindex][j] => ship (object signature), subsys (string)
- end
- function TurretHotkey:KeyLookup(key, required)
- local value
- ba.print("Beginning key lookup...\nKey is: " .. key .. "\n")
- for j = 1, #self.Keys do
- --ba.print("At index:" .. j .. ", value:" .. self.Keys[j] .. "...")
- if self.Keys[j] == string.upper(key) then
- --ba.print("MATCH!\n")
- value = j
- return value, true
- else
- --ba.print("NO MATCH!\n")
- end
- end
- if not value and required then
- return self:KeyLookup("F9"), false
- end
- if not value and not required then
- return self.ActiveKey, false
- end
- end
- function TurretHotkey:Add(ship, subsys, text, key)
- if not self.Enabled then
- self:Init()
- end
- if not ship then
- ba.print("TurretHotkey:Add(): No ship defined!\n")
- return
- end
- if not subsys then
- ba.print("TurretHotkey:Add(): No subsys defined!\n")
- return
- end
- local t = {}
- if mn.Ships[ship]:isValid() then
- local thisShip = mn.Ships[ship]
- t.Ship = thisShip:getSignature()
- if thisShip[subsys]:isValid() then
- local thisSubsys = thisShip[subsys]
- t.Subsys = thisSubsys:getModelName()
- else
- return
- end
- if showinfo then
- t.ShowInfo = true
- else
- t.ShowInfo = nil
- end
- if text then
- t.Text = text
- end
- self.Number = self.Number + 1
- t.Number = self.Number
- index = self:KeyLookup(key or "F9", true)
- ba.print("Added [" .. thisShip.Name .. "]:[" .. tostring(t.Subsys) .. "] to index " .. tostring(index) .. "\n")
- end
- if not self.Targets[index] then
- ba.print("Key not used before, creating group...\n")
- self.Targets[index] = {}
- end
- self.Targets[index][#self.Targets[index]+1] = t
- end
- function TurretHotkey:Rem(ship, subsys)
- if not self.Enabled then
- return
- end
- if not ship then
- ba.print("TurretHotkey:Rem(): No ship defined!\n")
- return
- end
- for i=1, #self.Keys do
- if self.Targets[i] then
- local thisEntry = self.Targets[i]
- for j=#thisEntry, 1, -1 do
- local thisShip = mn.getObjectFromSignature(thisEntry[j].Ship)
- if thisShip:isValid() and thisShip.Name == ship then
- if not subsys then
- table.remove(thisEntry,j)
- elseif subsys == self.Targets.subsys then
- if self.Selected == thisGroup[j].Number then
- self:CycleTargets(i)
- end
- table.remove(thisEntry,j)
- end
- end
- end
- end
- end
- end
- function TurretHotkey:Pressed(key)
- local flag = false
- self.LastKey = self.ActiveKey
- self.ActiveKey, flag = self:KeyLookup(key)
- if self.LastKey ~= self.ActiveKey then
- self.CycleIndex = nil
- end
- if flag then
- ba.print("Attempting to cycle...\n")
- self:CycleTargets(self.ActiveKey)
- end
- end
- function TurretHotkey:CycleTargets(key)
- self.CycleIndex = self.CycleIndex or 1
- local thisGroup = self.Targets[key]
- if thisGroup and #thisGroup > 0 then
- for i=1, #thisGroup do
- if self.CycleIndex == i then
- local thisEntry = thisGroup[i]
- if thisEntry then
- local thisShip = mn.getObjectFromSignature(thisEntry.Ship)
- if thisShip:isValid() then
- local thisSubsys = thisShip[thisEntry.Subsys]
- if thisSubsys.HitpointsLeft > 0 then
- ad.playGameSound(8)
- hv.Player.TargetSubsystem = thisSubsys
- self.Selected = thisEntry.Number
- if i+1 <= #thisGroup then
- self.CycleIndex = self.CycleIndex+1
- else
- self.CycleIndex = 1
- end
- end
- return
- end
- end
- end
- end
- end
- end
- function TurretHotkey:Prune()
- for i=1, #self.Keys do
- local thisGroup = self.Targets[i]
- if thisGroup then
- for j=#thisGroup, 1, -1 do
- local thisShip = mn.getObjectFromSignature(thisGroup[j].Ship)
- if not thisShip:isValid() then
- table.remove(self.Targets[i],j)
- else
- local thisSubsys = thisShip[thisGroup[j].Subsys]
- if thisSubsys.HitpointsLeft == 0 then
- if self.Selected == thisGroup[j].Number then
- self:CycleTargets(i)
- end
- table.remove(self.Targets[i],j)
- end
- end
- end
- end
- end
- end
- function TurretHotkey:Draw()
- --gr.drawString("Coords:",100,100)
- for i=1, #self.Keys do
- local thisGroup = self.Targets[i]
- if self.ActiveKey == i and thisGroup then
- for j=1, #thisGroup do
- local thisEntry = thisGroup[j]
- local thisShip = mn.getObjectFromSignature(thisEntry.Ship)
- if thisShip:isValid() then
- local thisSubsys = thisShip[thisEntry.Subsys]
- if thisSubsys:isValid() then
- if thisSubsys.HitpointsLeft == 0 then
- gr.setColor(128,128,128,200)
- else
- gr.setColor(255,255,255,200)
- end
- local x1, y1, x2, y2 = gr.drawSubsystemTargetingBrackets(thisSubsys, true, false)
- --gr.setColor(255,255,255,255)
- --gr.drawString(tostring(x1) .. "," .. tostring(y1) .. "," .. tostring(x2) .. "," .. tostring(y2))
- if x1 and y1 and x2 and y2 then
- gr.CurrentFont = gr.Fonts[1]
- local name = thisEntry.Text
- if name then
- local r,g,b = thisShip.Team:getColor()
- if self.Selected ~= thisEntry.Number then
- r = r * 0.75
- g = g * 0.75
- b = b * 0.75
- end
- gr.setColor(r,g,b,200)
- if gr.getStringWidth(name) < ((x2-x1)+100) then
- gr.drawString(name, x1, y1 - 10)
- end
- end
- local health = (thisSubsys.HitpointsLeft/thisSubsys.HitpointsMax) * 100
- local healthDisplay = string.format("%d%%",health)
- if gr.getStringWidth(healthDisplay) < ((x2-x1)+50) then
- gr.drawString(healthDisplay, x1, y2 + 5)
- end
- end
- end
- end
- end
- end
- end
- end
- ]
- $State: GS_STATE_GAME_PLAY
- $On Gameplay Start:
- [
- if TurretHotkey.Enabled then
- TurretHotkey.Enabled = nil
- end
- ]
- $On State End:
- [
- TurretHotkey.Enabled = nil
- ]
- $On Key Released:
- [
- if TurretHotkey.Enabled then
- --ba.print("Key pressed: " .. hv.Key .. "\n")
- TurretHotkey:Pressed(hv.Key)
- end
- ]
- $On Frame:
- [
- if TurretHotkey.Enabled then
- TurretHotkey:Draw()
- TurretHotkey:Prune() --Ideally if there was a On Subsystem Destroyed hook, we'd put this there but oh well!
- --PrintDebug(TurretHotkey)
- end
- ]
- #End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement