axembin

Heal Beam

Nov 30th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1. #Conditional Hooks
  2. $Application: FS2_Open
  3. $On Game Init:
  4. [
  5. function BeamHeal(ship) --We're expecting a ship type handle to be passed through
  6.    
  7.     if ship:isValid() then --Always check to see if these things are valid to be used!
  8.    
  9.         local healStep = ship.HitpointsMax * 0.002 --The heal rate is proportional to max health, so all ships heal to the max within the same amount of time. Alternatively healStep could be the damage of the weapon.
  10.         --I'll leave that as a fun excercise for you to try! (Hint: you'll need an additional argument to get passed through in the function)
  11.        
  12.         if ship.HitpointsLeft <= ship.HitpointsMax then --It would be a good idea to only try to heal ships that have less than max health!
  13.        
  14.         --The ship in question will get its HP healed by healStep, so we should take what the end result would be after healing and make sure it won't go over the maximum amount of HP. If it doesn't we can just heal like normal, but if it does go over, we should just make it cap at max HP.
  15.        
  16.         --Mind you we could just heal and check afterwards if HP is over MaxHP, but it's probably better to ensure we touch ship properties as little as possible.
  17.        
  18.             if ship.HitpointsLeft + healStep <= ship.HitpointsMax then
  19.                 ship.HitpointsLeft = ship.HitpointsLeft + healStep
  20.             else
  21.                 ship.HitpointsLeft = ship.HitpointsMax
  22.             end
  23.        
  24.         end
  25.        
  26.         --In a ship's handle, there are all the normal attributes, but if we use [] indices on the ship handle, we get the subsystem handles.
  27.         --So ship["communications"] should give us the communications subsystem. But does our ship have engine or engine01 and engine02?
  28.         --Instead of explicitly going after the names of subsystem, we can just go through the whole thing with numerical indices.
  29.            
  30.         --for you people with some lua knowledge already, "for k,v in pairs()" won't work for these "virtual indexed" type of tables (they are slow for real time actions, so don't use them if you can avoid it!)
  31.         --the best way is to go through these type of tables is to get the size of the table, assign it to a variable, then do a for loop through everything
  32.         --real time looping should always be done with a numerical loop!
  33.        
  34.         local numSubsys = #ship  --#ship is the number of subsystems on the ship that we should heal
  35.        
  36.         for i=1, numSubsys do
  37.        
  38.         --We'll do the same type of checks on HP here as we did with the main health.
  39.        
  40.             if ship[i].HitpointsLeft <= ship[i].HitpointsMax then
  41.                 if ship[i].HitpointsLeft + healStep <= ship[i].HitpointsMax then
  42.                     ship[i].HitpointsLeft = ship[i].HitpointsLeft + healStep
  43.                 else
  44.                     ship[i].HitpointsLeft = ship[i].HitpointsMax
  45.                 end
  46.             end
  47.        
  48.         end
  49.        
  50.     end
  51.    
  52. end
  53. ]
  54.  
  55. $Weapon class: HealBeam#Tutorial
  56. $On Ship Collision:
  57. [
  58. --[[
  59.     Here the the conditional hook is Weapon Class, and we've defined it to only look at the HealBeam#Tutorial class.
  60.     We change the $Weapon class: to weapons to make other weapons heal.
  61.     Our action is On Ship Collision, and one of the hook variables for that action is hv.Object, which is the Object that has been collided with. So this bit of code will only run with HealBeam#Tutorial collides with a ship!
  62. ]]--
  63.  
  64.     BeamHeal(hv.Object)
  65.        
  66. ]
  67. #End
Advertisement
Add Comment
Please, Sign In to add comment