axembin

Remote Warhead Detonation

Nov 30th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.41 KB | None | 0 0
  1. #Conditional Hooks
  2. $Application: FS2_Open
  3. $On Game Init:
  4. [
  5.    
  6. RCD = {} --Empty table object to get us started. Read up on Lua's Object Oriented flavored methods for more info on this sort of thing.
  7.  
  8. function RCD:Init()
  9.  
  10.     --But just a quick FYI, when we're within a function using that special colon operator, like we are right now, "self" is a special local variable that fills in for the parent object. So if we do "self.Enabled = true" this is the same as "RCD.Enabled = true"
  11.    
  12.     --The colon operator also inserts self into the first argument of the function. Again, check out the lua documentation for more on that!
  13.    
  14.     --We don't need to worry about defining variables as local if we're defining variables that are members of a larger table object. These variables are already local to just this table/object/class/whatever anyway!
  15.    
  16.     self.Enabled = true     --We may wish to stop our script, so let's have an enabled variable
  17.     self.List = {}          --List will contain the list of ships that we hit and the local offsets of the hit.
  18.     self.ExecuteKey = "D"   --When we hit the ExecuteKey, we will detonate all charges
  19.     self.CheatKey = "5"     --Cheating to give ourselves the weapon... for test purposes of course!
  20.  
  21. end
  22.    
  23. function RCD:AddToList(weapon, object)
  24.  
  25.     --We're expected to get a weapon and the object that the weapon has hit
  26.  
  27.     local t = {} --t is going to be our temporary table before we officially insert it into self.List
  28.    
  29.     t.Damage = weapon.Class.Damage --Damage taken from the weapon, note that in the table the hull/shield/subsystem factors are all zero, so the initial hit will do no damage, but the actual damage will still get stored in here
  30.    
  31.     --Time for some 3d math, folks!
  32.     --Small 3d math function reference
  33.     --To go from a local relative offset to a global world vector:
  34.     --globalVector = referenceOrientation:unrotateVector(localVector)
  35.  
  36.     --To go from a global world vector to a local relative offset:
  37.     --localVector = referenceOrientation:rotateVector(globalVector)
  38.    
  39.     --weapon.Position is a global world vector, and because our target is going to be flying around a lot, when we choose to detonate, that position will be too far away to damage our target. So we will use those functions to discover the local relative position to the hit object, and when we want to detonate we'll run the function in reverse to get the global world coordinates again.
  40.  
  41.     t.Offset = object.Orientation:rotateVector(weapon.Position) - object.Position
  42.    
  43.     t.Object = object:getSignature() --Rather than saving an entire ship handle, which can get big and clumsy, we'll just grab the object signature
  44.  
  45.     self.List[#self.List+1] = t --table[#table+1] is a easy build up a continous numerical table
  46.  
  47. end
  48.    
  49. function RCD:CauseDamage()
  50.    
  51.     --There is no createExplosion() function, so we'll have to use the SEXP with mn.runSEXP()
  52.    
  53.     local listLength = #self.List --First find out how many charges we've got to blow up
  54.    
  55.     for i = 1, listLength do
  56.    
  57.         local boom = self.List[i] --self.List[i] is so tedious to type
  58.         local target = mn.getObjectFromSignature(boom.Object) --We need this to get the global position
  59.         local pos = target.Position + target.Orientation:unrotateVector(boom.Offset) --And now we unrotate the offset from last time
  60.        
  61.         --Boom!
  62.         mn.runSEXP("( explosion-effect " .. pos.x .. " " .. pos.y .. " " .. pos.z .. " " .. boom.Damage .. " 10 100 10 100 0 0 7 )")
  63.        
  64.     end
  65.    
  66.     --After we've gone through the entire list, we will revert the list back to an empty object
  67.     self.List = {}
  68.  
  69. end
  70. ]
  71.  
  72. $On Gameplay Start:
  73. [
  74.     --On Gameplay Start will only occur right after the briefing is done, as gameplay begins.
  75.     RCD:Init()
  76. ]
  77.  
  78. $Weapon class: Harpoon-RC#Tutorial
  79. $On Ship Collision:
  80. [
  81.  
  82.     --For the Harpoon-RC#Tutorial, upon hitting a ship we should...
  83.     --Add it to a list!
  84.     if RCD.Enabled then
  85.         RCD:AddToList(hv.Weapon, hv.Object)
  86.     end
  87.    
  88. ]
  89.  
  90. $State: GS_STATE_GAME_PLAY
  91. $On Key Released:
  92. [
  93.    
  94.     --During gameplay, when any key is released, this block of code will be run. The important hook variable here is hv.Key, so we check that to see if it matches any keys and if so, we'll do run some functions.
  95.  
  96.     if RCD.Enabled then
  97.         if hv.Key == RCD.ExecuteKey then
  98.             RCD:CauseDamage()
  99.         end
  100.        
  101.         --Comment out this part if we want to not allow cheating, this is only for testing anyway...
  102.         if hv.Key == RCD.CheatKey then
  103.             hv.Player.SecondaryBanks[1].WeaponClass = tb.WeaponClasses["Harpoon-RC#Tutorial"]
  104.         end
  105.     end
  106. ]
  107.  
  108. #End
Advertisement
Add Comment
Please, Sign In to add comment