Author Topic: Weapon hit detection script  (Read 4399 times)

0 Members and 1 Guest are viewing this topic.

Offline Goober5000

  • HLP Loremaster
  • 214
    • Goober5000 Productions
Weapon hit detection script
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




 

Offline Woolie Wool

  • 211
  • Fire main batteries
Re: Weapon hit detection script
This script crashes FSO in recent builds of FS2_Open whenever a ship targeted by the player is hit. Can it be updated?
16:46   Quanto   ****, a mosquito somehow managed to bite the side of my palm
16:46   Quanto   it itches like hell
16:46   Woolie   !8ball does Quanto have malaria
16:46   BotenAnna   Woolie: The outlook is good.
16:47   Quanto   D:

"did they use anesthetic when they removed your sense of humor or did you have to weep and struggle like a tiny baby"
--General Battuta

 

Offline Goober5000

  • HLP Loremaster
  • 214
    • Goober5000 Productions
Re: Weapon hit detection script
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?

 

Offline AdmiralRalwood

  • 211
  • The Cthulhu programmer himself!
    • Skype
    • Steam
    • Twitter
Re: Weapon hit detection script
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.
Ph'nglui mglw'nafh Codethulhu GitHub wgah'nagl fhtagn.

schrödinbug (noun) - a bug that manifests itself in running software after a programmer notices that the code should never have worked in the first place.

When you gaze long into BMPMAN, BMPMAN also gazes into you.

"I am one of the best FREDders on Earth" -General Battuta

<Aesaar> literary criticism is vladimir putin

<MageKing17> "There's probably a reason the code is the way it is" is a very dangerous line of thought. :P
<MageKing17> Because the "reason" often turns out to be "nobody noticed it was wrong".
(the very next day)
<MageKing17> this ****ing code did it to me again
<MageKing17> "That doesn't really make sense to me, but I'll assume it was being done for a reason."
<MageKing17> **** ME
<MageKing17> THE REASON IS PEOPLE ARE STUPID
<MageKing17> ESPECIALLY ME

<MageKing17> God damn, I do not understand how this is breaking.
<MageKing17> Everything points to "this should work fine", and yet it's clearly not working.
<MjnMixael> 2 hours later... "God damn, how did this ever work at all?!"
(...)
<MageKing17> so
<MageKing17> more than two hours
<MageKing17> but once again we have reached the inevitable conclusion
<MageKing17> How did this code ever work in the first place!?

<@The_E> Welcome to OpenGL, where standards compliance is optional, and error reporting inconsistent

<MageKing17> It was all working perfectly until I actually tried it on an actual mission.

<IronWorks> I am useful for FSO stuff again. This is a red-letter day!
* z64555 erases "Thursday" and rewrites it in red ink

<MageKing17> TIL the entire homing code is held up by shoestrings and duct tape, basically.

 

Offline Goober5000

  • HLP Loremaster
  • 214
    • Goober5000 Productions
Re: Weapon hit detection script
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.

 

Offline Woolie Wool

  • 211
  • Fire main batteries
Re: Weapon hit detection script
Sorry about this, I got this script mixed up with BlasterNT's hit indicator, which is the script that is causing me problems.
16:46   Quanto   ****, a mosquito somehow managed to bite the side of my palm
16:46   Quanto   it itches like hell
16:46   Woolie   !8ball does Quanto have malaria
16:46   BotenAnna   Woolie: The outlook is good.
16:47   Quanto   D:

"did they use anesthetic when they removed your sense of humor or did you have to weep and struggle like a tiny baby"
--General Battuta

 

Offline Goober5000

  • HLP Loremaster
  • 214
    • Goober5000 Productions
Re: Weapon hit detection script
Thanks to some excellent debugging and detective work by Axem, this bug should be fixed in the next nightly build.