Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Conditional Hooks
- $Application: FS2_Open
- $On Game Init:
- [
- 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.
- function RCD:Init()
- --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"
- --The colon operator also inserts self into the first argument of the function. Again, check out the lua documentation for more on that!
- --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!
- self.Enabled = true --We may wish to stop our script, so let's have an enabled variable
- self.List = {} --List will contain the list of ships that we hit and the local offsets of the hit.
- self.ExecuteKey = "D" --When we hit the ExecuteKey, we will detonate all charges
- self.CheatKey = "5" --Cheating to give ourselves the weapon... for test purposes of course!
- end
- function RCD:AddToList(weapon, object)
- --We're expected to get a weapon and the object that the weapon has hit
- local t = {} --t is going to be our temporary table before we officially insert it into self.List
- 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
- --Time for some 3d math, folks!
- --Small 3d math function reference
- --To go from a local relative offset to a global world vector:
- --globalVector = referenceOrientation:unrotateVector(localVector)
- --To go from a global world vector to a local relative offset:
- --localVector = referenceOrientation:rotateVector(globalVector)
- --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.
- t.Offset = object.Orientation:rotateVector(weapon.Position) - object.Position
- t.Object = object:getSignature() --Rather than saving an entire ship handle, which can get big and clumsy, we'll just grab the object signature
- self.List[#self.List+1] = t --table[#table+1] is a easy build up a continous numerical table
- end
- function RCD:CauseDamage()
- --There is no createExplosion() function, so we'll have to use the SEXP with mn.runSEXP()
- local listLength = #self.List --First find out how many charges we've got to blow up
- for i = 1, listLength do
- local boom = self.List[i] --self.List[i] is so tedious to type
- local target = mn.getObjectFromSignature(boom.Object) --We need this to get the global position
- local pos = target.Position + target.Orientation:unrotateVector(boom.Offset) --And now we unrotate the offset from last time
- --Boom!
- mn.runSEXP("( explosion-effect " .. pos.x .. " " .. pos.y .. " " .. pos.z .. " " .. boom.Damage .. " 10 100 10 100 0 0 7 )")
- end
- --After we've gone through the entire list, we will revert the list back to an empty object
- self.List = {}
- end
- ]
- $On Gameplay Start:
- [
- --On Gameplay Start will only occur right after the briefing is done, as gameplay begins.
- RCD:Init()
- ]
- $Weapon class: Harpoon-RC#Tutorial
- $On Ship Collision:
- [
- --For the Harpoon-RC#Tutorial, upon hitting a ship we should...
- --Add it to a list!
- if RCD.Enabled then
- RCD:AddToList(hv.Weapon, hv.Object)
- end
- ]
- $State: GS_STATE_GAME_PLAY
- $On Key Released:
- [
- --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.
- if RCD.Enabled then
- if hv.Key == RCD.ExecuteKey then
- RCD:CauseDamage()
- end
- --Comment out this part if we want to not allow cheating, this is only for testing anyway...
- if hv.Key == RCD.CheatKey then
- hv.Player.SecondaryBanks[1].WeaponClass = tb.WeaponClasses["Harpoon-RC#Tutorial"]
- end
- end
- ]
- #End
Advertisement
Add Comment
Please, Sign In to add comment