Home   Help Search Login Register  

Author Topic: Tank smoke grenade script  (Read 2757 times)

0 Members and 1 Guest are viewing this topic.

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Tank smoke grenade script
« on: 12 May 2005, 23:24:20 »
Here is the tank smoke grenade script I've been working on.   It's still pretty rough.   It adds an action allowing you to fire smoke grenades from your tank.   They do not provide a "magic cloak":  I haven't done any investigation but I suspect that it doesn't affect knowsAbout values of the opposition.     If an enemy tank has hit you once, it will probably hit you again.   In other words, get the smoke out early and it will do you some good.   Leave it too late and, well, it's too late.

Works from any position in the tank.   Works whatever weapon you have selected.    Works if you have no ammo.    Works only if the tank canFire.    Comes complete with sound effect.   It should work (more or less) with any BIS vehicle that has a machine gun though I haven't tested them all.  

Requires:-
OFP v1.96
JAM2

I know addons are bad for beta scripts, but the AI can see through BIS smoke grenades and they can't see through the JAM ones.  If you don't have JAM you can still try the script:  simply open the file smoke2.sqs and delete the relevant (commented) line near the top.   Also remove the reference to JAM from the addons section fof the mission.sqm.   Remember that the AI can see through the smoke.

Zip file attached.   Unzip to get a mission folder which you should play in the mission editor.   The demo mission is just you in a T80 with an M60 turning up.    The script is commented but not especially tidy.

How it works:
- Action added in init.sqs
- The action calls a script (smoke1)
- The script adds a fired eventHandler and fires a MG round
- The EH immediately calls the second script (smoke2)
- Second script detects the direction the turret is facing and fires a salvo of 6 smoke shells
- Ignore smokeold.sqs, it's just for history


Known issues:
- doesn't work for AI yet
- script is not particularly robust
- script is not particularly flexible
- you get infinite salvos
- can't tell what weapon you had selected before script called, so you may end up with a different one
- salvo does not vary with vehicle
- smokeshells appear correctly from the launchers on a T80 in special cases, but the trigometry has made my head spin so in general they don't appear in quite the right place.    They do always fire in the direction the turret is facing.   Uncomment the setAccTime line to slow things down so you can see what I mean.    Help with this point would be appreciated.

I suspect it won't be hard to break this script and I'd be interested to hear about it's weaknesses.   I'm not much of a scripter so suggestions and comments about the code are welcomed.    Also the effects - is 6 the right number?  Right distance away?  Right angles?   And so on.

If you have any thoughts on how the AI might use this script I'd be glad to hear them.    Making AI tanks use it will be easy, the questions are under what conditions and should other aspects of their behaviour be scripted too.

« Last Edit: 12 May 2005, 23:46:51 by macguba »
Plenty of reviewed ArmA missions for you to play

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Tank smoke grenade script
« Reply #1 on: 13 May 2005, 00:53:49 »
Just had a quick look at smoke1.sqs

I believe the M1Abrams should be a type1 not a type2 tank.  Will you allow a Vulcan or a Shilka to generate smoke?

Offline penguinman

  • Contributing Member
  • **
  • Money is worthless, just paper, ink, and threads
Re:Tank smoke grenade script
« Reply #2 on: 13 May 2005, 01:16:51 »
you should probably script it automaticly for AI by if the tanks have gotton the order to retreat by the leader the comanders will each pop some smoke before going where their comander has told them.
 it would just be like a normal retreat except right when they get the order to retreat they will smoke before they turn around and flee.


you should use the script on infantry to except inted of creating the smoke grenade just have the leader throw smoke(if he has any) in the opisite direction they are retreating. could be?

nice script

Offline Pilot

  • Contributing Member
  • **
Re:Tank smoke grenade script
« Reply #3 on: 13 May 2005, 03:21:15 »
Nice script!

One question, though.  When I fire the smoke, six shells are launched, but the ammo count in the upper left hand corner only reduces by one, is this right?

Quote
Also the effects - is 6 the right number?  Right distance away?  Right angles?
Everything seemed fine to me.

Quote
If you have any thoughts on how the AI might use this script I'd be glad to hear them.
Maybe check to see if the tank is fleeing, and if true, then shoot of the smoke?

-Student Pilot

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:Tank smoke grenade script
« Reply #4 on: 13 May 2005, 09:17:24 »
@THobson - I'll check again but I did run through them and I believe only the T80 had the 50 calibre.      The script won't work on Shilka or Vulcan because they don't have machine guns:  you'd have to fire the main gun to get the turret dirction.   The models in the game do not have smoke launchers anyway ... but then neither do M2s, boats, M113s, BMPs or the jeep/mg.  ;D


@penguinman - thanks for that, it's just the kind of thing I was looking for.    You could indeed do this for infantry and I suspect somebody already has.    Doing it for tanks is harder which is why nobody (AFAIK) has done it before, at least not to the extent of a fully functional script.


@Student pilot - the ammo count to which you refer is the machine gun, not the smoke.  At present there is no ammo count for the smoke and when there is it will appear in the Action menu.    The MG fires one round so that the script can determine the direction of the turret.    You don't notice because the sound is hidden and there is the thump of the smoke launchers instead.    I tried to add back one MG round but without success.     If you are out of MG ammo the script adds it back (and takes it away afterwards) though, so it will still work.  

Thanks everybody.  :)


@THobson, you're absolutely right about the MG.    Can't believe I ever doubted you.
« Last Edit: 13 May 2005, 09:22:21 by macguba »
Plenty of reviewed ArmA missions for you to play

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Tank smoke grenade script
« Reply #5 on: 13 May 2005, 11:57:24 »
Quote
but then neither do M2s, boats, M113s, BMPs or the jeep/mg.
 That's what made me ask the question. :)

I was forced to become an expert on re-arming tanks recently ::)

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:Tank smoke grenade script
« Reply #6 on: 22 May 2005, 01:48:55 »
Haven't checked it out yet, but just a quick thing I noticed browsing the scripts:

Quote
; arrays of BIS vehicles depending on their machine guns
_type1 = ["T80"]
_type2 = ["M1Abrams", "T72", "M60", "T55G", "BMP", "BMP2", "Bradley"]
_type3 = ["M113", "JeepMG", "BoatE", "BoatW", "M2StaticMGE", "M2StaticMG"]

; figure out what vehicle we have and what MG we need
? (typeOf _tank) in _type1 : _mg = "MachineGun12_7"
? (typeOf _tank) in _type2 : _mg = "MachineGun7_6"
? (typeOf _tank) in _type3 : _mg = "Browning"

Instead of using all these wacky conditionals, why not just check if the vehicle hasweapon a certain weapon? Then you only have to create one array: of available MGs, and then not worry about the vehicle type. Makes it easier to get the script working with other addons. Something like this:

Code: [Select]
_mgs = ["MachineGun12_7", "MachineGun7_6", "Browning"]
_i = 0
;loop thru array of known MGs to see which one tank has
#findWpn
? _tank hasweapon (_mgs select _i) : _mg = (_mgs select _i); goto "foundWpn"
_i = _i + 1
? _i < count _mgs : goto "findWpn"
;tank has no known MG--exit script
exit
;we have found tank's mg
#foundWpn
....

One problem is that there can be different classnames for the vehicle weapon, and the vehicle weapon's magazine. So perhaps you would have to put 2 entries into the "_mgs" array; one for the weaponname, and one for the mag name:

Code: [Select]
_mgs = [ ["MachineGun12_7","MachineGun12_7"], ["MachineGun7_6","MachineGun7_6"], ["Browning","Browning"] ]
You would then have to adjust the first code I posted if you did this.

One way to have limited ammo would be to use a global variable to store how much "ammo" each tank has. This could be done thru an array of 2-entry arrays:

mcgb_tnk_smoke = [ [tank, ammo], [tank2, ammo] ]

Then, search thru the first element of each entry array to find the right tank, and the second entry of the array will be the tank's ammo. You could use a modified version of this function to do that. Just edit this line:
Code: [Select]
if (_item == (_array select _index)) then {_found = TRUE} else {_index = _index + 1};To look like this:
Code: [Select]
if (_item == ( (_array select _index) select 0)) then {_found = TRUE} else {_index = _index + 1};You could also use fragorl's get/setproperty functions for the same effect:
http://www.ofpec.com/editors/funcref.php?filter_func=64

Also, keep in mind that the 'hint' and 'playsounds' will be visible/audible for players even when the AI uses this script...

============

As for ideas on when the AI should deploy the smoke... I really have no idea. Checking the "isfleeing" (?) value for the tank's group, as suggested, and making them pop smoke to cover a retreat seems like a very simple and effecient way to do it.

Another, more complex idea might be to check for locations of known enemies, and pop smoke on one side whenever the tank is being attacked from 2 or more directions.

Or maybe pop smoke whenever the tank is hit, or whenever it is damaged beyond a certain point.

=======

I think Keycat made a script for this as well, although his only worked for players, IIRC. Someone posted one on the official forums a while ago, I know, but I have no idea where. Maybe a search would turn something up, I dunno.

« Last Edit: 22 May 2005, 01:56:41 by General Barron »
HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:Tank smoke grenade script
« Reply #7 on: 22 May 2005, 10:45:47 »
GB, thanks for that.   Currently the script is not very general, so your comments are extremely welcome.    Hints and playsounds will obviously come out for the AI version.

Ideas on when to pop also very welcome.   I hadn't thought of the 2 sided attack one.    

I had in my mind that a global array was probably the way forward for controlling the ammo, but I've never done anything quite like that so thanks for the pointers.   Still need to figure out how to handle rearming of course.    ;D

But the best part of all is that you didn't appear to find any bugs or stupid errors.  ;D
Plenty of reviewed ArmA missions for you to play

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:Tank smoke grenade script
« Reply #8 on: 31 May 2005, 03:31:32 »
Alright, I finally played around with the script in-game, and it's great! Looks awesome (with the T80 at least), and it works like a charm.

I ran into one bug, although I doubt it can be fixed: when I am in the gunner seat and I use the script, the smoke will always pop out of the front of the tank (not the turret). This means a player gunner cannot control the direction the smoke gets fired. Again, I suspect an OFP 'feature' at work, but perhaps try having the vehicle's gunner fire, instead of the vehicle itself.

Actually, there is another, major bug in the script. Currently, if you use the smoke action, then immediately fire some other weapon, that other weapon will also fire smoke for a second or so. Its really bad with the MG.

This problem occurs because of the 1 second delay between when the EH is added and when it is removed. You also remove the first EH added to the tank, whether or not that was the EH added by this script. So if a mission editor was running some other fired EH on that tank, then this script wouldn't work properly at all, and would mess up that other EH.

------------------------------

The best way to eliminate both of these problems is with a handy little trick that I've been using more and more in scripts. Basically, you set up a self-removing EH. The trick comes from using a combination of the format command, as well as the way that EH's index themselves when added and removed. Essentially you just get the index of the EH you will be adding, and then you put that number into the EH's code via the 'format' command:

First off, getting the EH's index. When you add the first EH of a type to an object, it starts with idx 0. The next added EH then gets idx 1, and so on. However, when you REMOVE an EH at, say, idx 1, then all EHs of idx > 1 have their idxs REDUCED by one. Contrast this to action idxs, which always stay the same, even after removing other actions.

So, to get the index of an EH we are about to add, we can just add an EH, store the returned idx, then remove that EH:

_idx = _tank addeventhandler ["fired", {}]
_tank removeEventHandler ["fired",_idx]

Okay, now we have the index of the NEXT EH we are about to add. So now we can 'imbed' that number into that EH we are going to add, via the format command. The EH in the following example will make a hint appear, and then remove itself IMMEDIATELY:

_tank addeventhandler [ "fired", format[{hint "ping!"; (_this select 0) removeEventHandler["fired", %1]}, _idx] ]

Notice that the 'format' command will FIRST format the code string in the EH, and will replace the %1 with the value of the variable _idx.

So, as long as no EHs are removed after we add this EH, the EH will remove itself right as soon as it fires. Note that if an EH was removed before the one we just added goes off, then the _idx we 'passed' to the EH might no longer be valid (because of the way OFP decrements all indexes after removing EHs). This means one should only use this trick when you know there will only be a short delay between when the EH is added and when it is removed. In your case, this will work perfectly.

----------------

So, for your script, it could look like this:

Code: [Select]
; if the tank has no mg ammo it gets some
_removeMag = false
? (_tank ammo _mg) < 1 : _tank addMagazine _mg; _removeMag=true

; this gives the tank an EH from which we can extract the direction of the turret
_tank addEventHandler ["fired",{[_this select 0, %1, %2]  exec "smoke2.sqs"}, _idx, _removeMag]]

~0.01

; now make the tank fire silently so the EH can pick up the direction
0 fadeSound 0
0 fadeMusic 0.5
_tank selectWeapon _mg
_tank fire _mg
exit

Note that I also imbedded the variable _removeMag into the EH, so the magazine is removed exactly once the weapon is fired. The following parameters will be passed to smoke2.sqs:

[tank, EH idx, removeMag?]

I hope that all made sense. It might seem like a complicated workaround, but it is actually fairly simple, and once you understand how it works, you can suddenly do a whole lot more with EHs.
HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:Tank smoke grenade script
« Reply #9 on: 31 May 2005, 11:03:27 »
GB thanks for that.   I knew about this problem, although I hadn't experienced it.    The trouble is that the script required some small delays, so that one thing had time to "register" before the script moved onto the next line.    I dont' have time at present but I'll look into this as soon as I come back to the script.   Subtleties like EH number I also knew would have to be fixed.

However it works fine for me when I'm gunner.    The smoke always appears in front of the turret.    Need to look into that.     Does the smoke appear in front of the turret when you are driver or commander?     Are there any particular orientations of hull or turret where this occurs?
Plenty of reviewed ArmA missions for you to play