Hard Light Productions Forums

FreeSpace Releases => Scripting Releases => Topic started by: Goober5000 on September 08, 2017, 02:35:33 pm

Title: Weapon hit detection script
Post by: Goober5000 on September 08, 2017, 02:35:33 pm
This script was inspired by one that m!m originally created for Scroll.  I ended up almost entirely rewriting it as our needs expanded beyond the specific use case that m!m wrote it for.  With this script, you can monitor ships and subsystems for any hits by a particular class of weapon.
http://scroll.hard-light.net/downloads/weapon_hit-sct.tbm

There are two main functions to use:

WeaponHit:addHitDef(weapon_class_name, ship_name, subsys_name)
Call this function at the beginning of the mission add the "hit definition" consisting of the weapon class, an optional ship (or nil for all ships), and an optional subsystem (or nil for a hit anywhere on the ship).  The game will update these definitions whenever a weapon hit is detected.

WeaponHit:checkHit(variables, weapon_class_name, ship_name, subsys_name)
Call this function periodically to check for hits.  If any hit is detected, and you supply one or more variables, those variables will be populated with the results.  Possible variables are age, recording the age in seconds since a hit was detected, and x, y, and z, recording the world coordinates of the hit.


Example:

Code: [Select]
$Formula: ( when
   ( true )
   ( script-eval-block
      "WeaponHit:addHitDef('TAG-B', "
      "'Arachnas', 'turret02')"
   )
)
+Name: Initial setup
+Repeat Count: 1
+Interval: 1

Then:

Code: [Select]
$Formula: ( when
   ( and
      ( perform-actions
         ( true )
         ( script-eval-block
            "WeaponHit:checkHit({age='hit-Ar"
            "achnas-turret02-age'}, 'TAG-B',"
            "'Arachnas', 'turret02')"
         )
      )
      ( = @hit-Arachnas-turret02-age[-1] 0 )
   )
   ( send-message
      "#Command"
      "High"
      "Tag successful"
   )
)
+Name: TAG successful
+Repeat Count: 1
+Interval: 1



Title: Re: Weapon hit detection script
Post by: Woolie Wool on January 26, 2019, 10:24:05 am
This script crashes FSO in recent builds of FS2_Open whenever a ship targeted by the player is hit. Can it be updated?
Title: Re: Weapon hit detection script
Post by: Goober5000 on January 27, 2019, 12:10:45 am
This script crashes FSO in recent builds of FS2_Open whenever a ship targeted by the player is hit. Can it be updated?

It certainly can, but I'm having trouble reproducing the bug.  The crash was first reported by the Inferno team, and when I asked them for more information, they told me they hadn't been using the script recently.

The fact that it specifically crashes when a ship is hit that is targeted by the player is the first useful bit of information I've received so far, so thanks. :yes:  Would you be able to put together a simple mission that reliably reproduces the crash?
Title: Re: Weapon hit detection script
Post by: AdmiralRalwood on January 28, 2019, 10:28:46 am
If memory serves, I once had a crash with this while testing a mod and bizarrely got it to go away by changing hv.Weapon to hv.Object; since both are supposed to refer to the exact same thing, this was extremely confusing, and trying to debug Lua from the C++ code is exceedingly difficult.
Title: Re: Weapon hit detection script
Post by: Goober5000 on January 29, 2019, 05:47:54 pm
That's what I asked the Inferno people to try.  The problem is, of the places in the code where hv.Object is supplied but hv.Weapon isn't, none of them should be getting called for any use cases of the script.
Title: Re: Weapon hit detection script
Post by: Woolie Wool on February 01, 2019, 01:20:10 pm
Sorry about this, I got this script mixed up with BlasterNT's hit indicator, which is the script that is causing me problems.
Title: Re: Weapon hit detection script
Post by: Goober5000 on March 04, 2019, 04:22:25 pm
Thanks to some excellent debugging and detective work by Axem, this bug should be fixed in the next nightly build.