Advertisement
Guest User

Untitled

a guest
Jun 27th, 2018
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #Conditional Hooks
  2. $Application: FS2_Open
  3.  
  4. $On Game Init:
  5.  
  6. [
  7.  
  8. function SwapClassTo(class, ship)
  9.  
  10. if mn.Ships[ship]:isValid() and tb.ShipClasses[class]:isValid() then --always check validity of game objects!
  11.  
  12. local thisShip = mn.Ships[ship] --Good form is to declare internal local objects
  13. local thisClass = tb.ShipClasses[class]
  14.  
  15. local priWeapons = {} --Declaring primary and secondary weapon tables as an empty object for later population
  16. local priAmmo = {}
  17. local secWeapons = {}
  18. local secAmmo = {}
  19.  
  20. --First let's figure out what weapons we have
  21. for i=1, #thisShip.PrimaryBanks do -- thisShip.PrimaryBanks has a # operator that returns the number of banks
  22. local thisBank = thisShip.PrimaryBanks[i]
  23. priWeapons[i] = thisBank.WeaponClass
  24. priAmmo[i] = thisBank.AmmoLeft / thisBank.AmmoMax --Storing ammo counts as a percent of max ammo so if ammo capacity changes we're all good
  25. end
  26.  
  27. for i=1, #thisShip.SecondaryBanks do -- SecondaryBanks too
  28. local thisBank = thisShip.SecondaryBanks[i]
  29. secWeapons[i] = thisBank.WeaponClass
  30. secAmmo[i] = thisBank.AmmoLeft / thisBank.AmmoMax
  31. end
  32.  
  33. --Now we switch the ship class
  34.  
  35. thisShip.Class = thisClass
  36.  
  37. --Now we swap back the weapon info
  38.  
  39. for i=1, #thisShip.PrimaryBanks do
  40. local thisBank = thisShip.PrimaryBanks[i]
  41. if ( priWeapons[i] ~= nil ) then
  42. thisBank.WeaponClass = priWeapons[i]
  43. thisBank.AmmoLeft = priAmmo[i] * thisBank.AmmoMax
  44. end
  45. end
  46.  
  47. for i=1, #thisShip.SecondaryBanks do
  48. local thisBank = thisShip.SecondaryBanks[i]
  49. if ( secWeapons[i] ~= nil ) then
  50. thisBank.WeaponClass = secWeapons[i]
  51. thisBank.AmmoLeft = secAmmo[i] * thisBank.AmmoMax
  52. end
  53. end
  54.  
  55.  
  56. end
  57.  
  58. end
  59.  
  60. ]
  61.  
  62. #End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement