Author Topic: (FIXED) Problems with the Ju87 Stuka (and other aircraft) AI  (Read 5019 times)

Offline Drawde

  • Jr. Member
  • **
  • Posts: 160
    • View Profile
The AI seems to be capable of flying most of the aircraft in FH2 reasonably well - they don't crash too often and can strafe ground targets and engage enemy aircraft. However, the Ju87 Stuka (and possibly the Beaufighter bomber version as well, though I haven't tested this myself - the Stuka is a lot more common in maps) seems to be an exception to this. It never drops its bombs, and rarely manages to stay in the air long enough to get the chance to do so!

I've been trying to fix this problem, or at least find out why it happens, without much success! I've looked through the Stuka data files, comparing them to those for other FH2 aircraft, and have tried changing some of them and flying in the gunner's seat of AI-piloted Stukas to see what the AI does. So far I've got no further to fixing the problem, although I think I've found its main cause.
There are a number of issues in the Stuka AI data files (see below), but what seems to be the main cause of the problem is the AI control input parameters (pitch, roll + yaw scale and limits) in objects.ai. These values, along with the input sensitivity (identical for every FH2 aircraft including the Stuka) seem to control how the AI flies the aircraft.

These are the default values for the Stuka:
Quote
aiTemplatePlugIn.pitchScale           0.0110
aiTemplatePlugIn.rollScale            0.0220
aiTemplatePlugIn.yawScale             0.0175
aiTemplatePlugIn.maxRollAngle         0.5000
aiTemplatePlugIn.maxClimbAngle        0.1700

and these are the values for every other aircraft in FH2 (except the Ju52, which has the same max roll and climb angle as the Stuka):

Quote
aiTemplatePlugIn.pitchScale           0.0420
aiTemplatePlugIn.rollScale            0.0680
aiTemplatePlugIn.yawScale             0.0275
aiTemplatePlugIn.maxRollAngle         0.9900
aiTemplatePlugIn.maxClimbAngle        0.3333

With the default values, an AI-piloted Stuka flies and manouevers so sluggishly that it eventually ends up flying out of the playable map area and getting killed. Occasionally it'll attempt to strafe ground targets or even dogfight with enemy planes, but won't drop bombs (the bomb issue seems to be completely unrelated, though).
However, if you replace the default values with the standard ones from another aircraft, the Stuka flies wildly and erratically, almost as if the bot piloting it is attempting to do aerobatics, and will eventually (usually sooner rather than later) end up spiralling or nose-diving into the ground. They rarely (if ever) seem to make any attempt to attack targets on the ground.

Does anyone have any clues as to why this is happening with the Stuka but not, as far as I can see, with any other aircraft type in FH2? I'd really appreciate some help from anyone with more experience coding aircraft AI in BF2.

Finally, the other issues I spotted in the FH2 Stuka data:

- Bombs have a lower AI strength than MGs vs. many target types
- Bombs don't have the AI fire input key set (PIAltFire) in weapons.ai
- The AI input key to select the rear gunner position is PIMenuSelect2 rather than PIPositionSelect2 (conflicting with the bomb select key)
- The 250kg bomb has an extremely high minimum range (500)

These may be the reason for the AI not dropping bombs, but until the flight issues have been sorted out it's hard to say for certain!

The aiTemplatePlugIn.equipmentTypeName in objects.ai is also set as "Bomber"; not sure if this is necessary, as the "vanilla" BF2 aircraft, all set as "Fighter", never had any problems dropping bombs when piloted by the AI. Changing it to "Fighter" doesn't seem to affect the Stuka's behaviour much, if at all.
« Last Edit: 13-02-2010, 12:02:31 by Drawde »

Offline Raziel

  • Jr. Member
  • **
  • Posts: 859
  • Bullet Magnet
    • View Profile
Re: Problems with the Ju87 Stuka (and other aircraft) AI
« Reply #1 on: 12-02-2010, 13:02:14 »
I am no modder but can't you just check out the coding for a BF2 aircraft and compare it to the ones FH2 aircraft use. As you said in Bf2 vanilla the aircraft fired different types of ammo - bullets, missiles and bombs so...maybe it will give you new ideas to experiment with! Just a humble suggestion!

Offline cannonfodder

  • Full Member
  • ***
  • Posts: 1.228
    • View Profile
Re: Problems with the Ju87 Stuka (and other aircraft) AI
« Reply #2 on: 12-02-2010, 15:02:52 »
I tried changing those values to what they are in the AIX Stuka...
Quote
aiTemplatePlugIn.pitchScale           0.0010
aiTemplatePlugIn.rollScale            0.0020
aiTemplatePlugIn.yawScale             0.0175
aiTemplatePlugIn.maxRollAngle         0.7900
aiTemplatePlugIn.maxClimbAngle        1.5690
...and it seems to be a bit better. It climbs higher, but it's still going into those suicide dives and about 2/3 are still hitting the deck on the first one.

Quote
...I'd really appreciate some help from anyone with more experience coding aircraft AI in BF2...
I'd say your best bet would be to post the problem over at the BFSP forum: http://www.battlefieldsingleplayer.com/forum/index.php?showforum=25

Offline djinn

  • Masterspammer
  • ****
  • Posts: 5.723
    • View Profile
Re: Problems with the Ju87 Stuka (and other aircraft) AI
« Reply #3 on: 12-02-2010, 18:02:33 »
True, but unless i'm wrong, the stuka issue thread already exists there. So expect dissapointment. Then again, alot of the new faces weren't there then. Legiondcx for instance.

If fixed though, it will be the proudest triumph in fh2, comparable to bizness' version in 2.0, where that particular bird did all but strafe.

Offline cannonfodder

  • Full Member
  • ***
  • Posts: 1.228
    • View Profile
Re: Problems with the Ju87 Stuka (and other aircraft) AI
« Reply #4 on: 13-02-2010, 03:02:23 »
You mean the "I need guinea pigs..." thread?

That's the only thread I could find on the Stuka.

The link I provided points to the 'Advanced Topics' section, not the 'FH' section...at a guess I'd say more people will see it there.

Offline djinn

  • Masterspammer
  • ****
  • Posts: 5.723
    • View Profile
Re: Problems with the Ju87 Stuka (and other aircraft) AI
« Reply #5 on: 13-02-2010, 09:02:57 »
Oh, right... it was mgs and arty... not the Stuka..

sorry - false info. I guess the Stuka wasn't an issue then. But something about is mentioned in the Redsand/WinterHilf callaboration - Might need to search for it though



Offline Drawde

  • Jr. Member
  • **
  • Posts: 160
    • View Profile
Re: Problems with the Ju87 Stuka (and other aircraft) AI
« Reply #6 on: 13-02-2010, 12:02:17 »
After a lot of tedious checking through and comparing of the BF2 and FH2.25 aircraft + bomb files, and making various changes (none of which worked, until this one), I think I've finally solved the problem.  The erratic kamikaze flight of AI bombers and their inability to drop bombs both have the same cause - and it's a really stupid bug, but one which is thankfully very easy to fix.

This is what the cause of the problem appears to be:
- FH2's bomb 3D models (the German ones, at least) are modelled back-to-front (no idea why)
- As a simple fix for this, the "dummy" bomb models (which also act as launch points for the actual dropped bomb projectiles) attached to aircraft are rotated 180 degrees.
- This doesn't affect the bomb aiming or dropping physics, but DOES affect AI bots attempting to use the bombs. It completely messes up their aiming + targeting AI, so they not only can't drop bombs, but inevitably end up crashing their plane in a futile attempt to aim the bombs at ground targets  ::)

To fix the issue, open the .tweak file for a bomber, and where you find the code which adds the "dummy" bomb models, for example:
Quote
ObjectTemplate.addTemplate sc500_dummy
ObjectTemplate.setPosition 5.18669/-0.553727/2.59724
ObjectTemplate.setRotation 180/0/0

- add "rem " to the beginning of the ObjectTemplate.setRotation line. Do this for all the dummy bombs attached to the aircraft. 

The German SC50, 250 and 500 (which are also used by the British Hurricane and Beaufighter) definitely have this problem.  I haven't yet tested whether the US bombs (on the P-51 and P-47) have the same issue.
The one problem with this fix is that the visible bombs on aircraft models are now back-to-front! One way to fix this could be to add invisible (and non-rotated) bomb-launching "hardpoints" at the same location as the bomb models, and keep the 180-degree rotation for the bomb models. I'll try this idea out soon.

When I made this change (not really expecting it to do anything) and started up FH2, I knew it had worked before I even saw an AI plane, as I got multiple Ju87 and Beaufighter kill messages within a minute of loading El Alamein  ;D Working AI bombers seems to make a big difference to how SP games play out, AI ground vehicles tend to have a hard time attacking defended positions and AT guns. I suspect it'll make a particular difference on maps like Siege of Tobruk and Invasion of Crete where the Germans rely on aircraft as their "artillery" and the Allies don't have any fighters to defend with.

Some notes on the bomber AI:

- They're quite aggressive attacking with bombs and will drop them quite accurately, though possibly not as accurately as in vanilla BF2 (this may be because of the additional "deviation" inaccuracy I added to the bomb AI files)
- Bots never run out of bombs, just like in BF1. I'm guessing the AI is hard-coded to never run out of ammo for weapons.
- For the above reason, they never seem to strafe with their guns - bombs have a higher AI strength, and they never run out, so no reason to ever use guns. Not a huge problem with the Stuka, but more so for the Allied fighter-bombers which have a lot more firepower. I'm not sure if this is fixable, giving the guns a higher AI strength than bombs would probably mean they don't use bombs at all - but I'll try it out anyway.
- The Stuka doesn't dive-bomb its targets (the previous "diving" behavious was just a bug caused by the reversed bombs), and I'm not sure if it's possible to get the AI to use the dive siren, but, again, I'll try all the same.

Offline Raziel

  • Jr. Member
  • **
  • Posts: 859
  • Bullet Magnet
    • View Profile
Re: Problems with the Ju87 Stuka (and other aircraft) AI
« Reply #7 on: 13-02-2010, 16:02:01 »
After a lot of tedious checking through and comparing of the BF2 and FH2.25 aircraft + bomb files, and making various changes (none of which worked, until this one), I think I've finally solved the problem.  The erratic kamikaze flight of AI bombers and their inability to drop bombs both have the same cause - and it's a really stupid bug, but one which is thankfully very easy to fix.

This is what the cause of the problem appears to be:
- FH2's bomb 3D models (the German ones, at least) are modelled back-to-front (no idea why)
- As a simple fix for this, the "dummy" bomb models (which also act as launch points for the actual dropped bomb projectiles) attached to aircraft are rotated 180 degrees.
- This doesn't affect the bomb aiming or dropping physics, but DOES affect AI bots attempting to use the bombs. It completely messes up their aiming + targeting AI, so they not only can't drop bombs, but inevitably end up crashing their plane in a futile attempt to aim the bombs at ground targets  ::)

To fix the issue, open the .tweak file for a bomber, and where you find the code which adds the "dummy" bomb models, for example:
Quote
ObjectTemplate.addTemplate sc500_dummy
ObjectTemplate.setPosition 5.18669/-0.553727/2.59724
ObjectTemplate.setRotation 180/0/0

- add "rem " to the beginning of the ObjectTemplate.setRotation line. Do this for all the dummy bombs attached to the aircraft. 

The German SC50, 250 and 500 (which are also used by the British Hurricane and Beaufighter) definitely have this problem.  I haven't yet tested whether the US bombs (on the P-51 and P-47) have the same issue.
The one problem with this fix is that the visible bombs on aircraft models are now back-to-front! One way to fix this could be to add invisible (and non-rotated) bomb-launching "hardpoints" at the same location as the bomb models, and keep the 180-degree rotation for the bomb models. I'll try this idea out soon.

When I made this change (not really expecting it to do anything) and started up FH2, I knew it had worked before I even saw an AI plane, as I got multiple Ju87 and Beaufighter kill messages within a minute of loading El Alamein  ;D Working AI bombers seems to make a big difference to how SP games play out, AI ground vehicles tend to have a hard time attacking defended positions and AT guns. I suspect it'll make a particular difference on maps like Siege of Tobruk and Invasion of Crete where the Germans rely on aircraft as their "artillery" and the Allies don't have any fighters to defend with.

Some notes on the bomber AI:

- They're quite aggressive attacking with bombs and will drop them quite accurately, though possibly not as accurately as in vanilla BF2 (this may be because of the additional "deviation" inaccuracy I added to the bomb AI files)
- Bots never run out of bombs, just like in BF1. I'm guessing the AI is hard-coded to never run out of ammo for weapons.
- For the above reason, they never seem to strafe with their guns - bombs have a higher AI strength, and they never run out, so no reason to ever use guns. Not a huge problem with the Stuka, but more so for the Allied fighter-bombers which have a lot more firepower. I'm not sure if this is fixable, giving the guns a higher AI strength than bombs would probably mean they don't use bombs at all - but I'll try it out anyway.
- The Stuka doesn't dive-bomb its targets (the previous "diving" behavious was just a bug caused by the reversed bombs), and I'm not sure if it's possible to get the AI to use the dive siren, but, again, I'll try all the same.

Drawde!! You are one hell of a coder/moder!!! Congratz on getting hold of this problem and solving it!! WOOHOHHHOOO!!!  :D

Offline cannonfodder

  • Full Member
  • ***
  • Posts: 1.228
    • View Profile
Re: Problems with the Ju87 Stuka (and other aircraft) AI
« Reply #8 on: 13-02-2010, 17:02:59 »
...This is what the cause of the problem appears to be:
- FH2's bomb 3D models (the German ones, at least) are modelled back-to-front (no idea why)...
Classic... ;D

Quote
...- They're quite aggressive attacking with bombs and will drop them quite accurately, though possibly not as accurately as in vanilla BF2 (this may be because of the additional "deviation" inaccuracy I added to the bomb AI files)...
Yeah, accurate alright. Due to the way they slow down and let their bombs go from such a low altitude, combined with the blast radius, I don't think the added deviation is having any noticeable effect...I played a whole round of El Al. watching/riding in the Stukas and I didn't see them miss once... :P

Quote
...they never seem to strafe with their guns - bombs have a higher AI strength...
What if you set the weapon strength vs infantry to 0 for the bombs? That way, in theory, they'd strafe troops and bomb the crap outta everything else...

Quote
...The Stuka doesn't dive-bomb its targets (the previous "diving" behavious was just a bug caused by the reversed bombs), and I'm not sure if it's possible to get the AI to use the dive siren, but, again, I'll try all the same.
The AIX Stuka doesn't either, I'll have to change those pitch/roll scale values back to default to be sure, but at the moment it's behaving just like it...i.e. it "dives" toward it's target at about 30 degrees.


General observations (after one round):

 - Didn't see much of the Beau, but it was just as devastating as the Stuka when it was around...

 - Got chased by a Spit a few times while I was riding in the back of the Stuka...great fun, but the chase is over almost as soon as it starts due to the speed difference...

And my favourite...

 - Sitting in the back as the pilot flys toward Mit. Ridge (heading NW after take-off). He loses some altitude, looks like we're going to hit the ridge...then he levels out, swoops over it and flattens the 88 and the bot sitting on it...poor bludger never even seen us comin'... ;D

Drawde!! You are one hell of a coder/moder!!! Congratz on getting hold of this problem and solving it!! WOOHOHHHOOO!!!  :D
Yes, yes he is.

Good onya, Drawde... :)

Offline djinn

  • Masterspammer
  • ****
  • Posts: 5.723
    • View Profile
hats of to you, Dawde!

You just solved an issue any and and every AI coder has struggled with since Stuka's were put in in 2.0... Its little wonder why even Bizness couldn't figure it out... Technically, its not even an AI issue!

Maybe, making the Stuka consider bombs as high priority for vehicles and mg as high priority for infantry and 'soft vehicles might make them use mgs at least for some targets

EDIT:
Tried it out and was quite impressed with Stuka's diving on targets... Issue is, they do fly low like CannonFodder hinted. Much lower than BF2 vanilla bots fly I think... They are killed hundred percent of the time they go anywhere close to an AA base and they just NEED to bomb AAs so they fly straight at it - I wonder if its possible to make their 'criuse' level way higher, even higher than scouts... The need to drop bombs will take care of their high g-dive...

Love their alternative use of bombs.. Had a Stuka drop one 250kg bomb on one tank and then switch and drop  100kg bombs on another tank
« Last Edit: 13-02-2010, 20:02:15 by djinn »

Offline :| Hi

  • Masterspammer
  • ****
  • Posts: 4.944
    • View Profile
Drawde, I love you and your coding skills <3


Stukas that work shall be fun to play with >:D

[2:06:54 PM] Tolga: cant use tha shit underwater -Tolga on the G3

Offline cannonfodder

  • Full Member
  • ***
  • Posts: 1.228
    • View Profile
...They are killed hundred percent of the time they go anywhere close to an AA base and they just NEED to bomb AAs so they fly straight at it - I wonder if its possible to make their 'criuse' level way higher, even higher than scouts... The need to drop bombs will take care of their high g-dive...

Love their alternative use of bombs.. Had a Stuka drop one 250kg bomb on one tank and then switch and drop  100kg bombs on another tank
My thoughts exactly after watching them on El Al...then I tried Inv. of Crete:

 - The AA and Stukas are quite evenly matched...quite a few times the pilot flew straight down the barrel of the Bofors and took it out (makes for a hairy ride in the back seat watching shells wizz past).

 - Dunno why, but they seem to be flying alot higher in Crete. Most of the action was taking place between the Airfield and the town, so it was pretty quiet everywhere else...until all of a sudden I'd hear a whistling sound, which gave me just enough time to empty my bowels before being catapulted into the stratosphere... ;D Three times this happened and I didn't hear the plane coming once!

 - And, get this, I saw the Stukas strafe on 4 occasions over two rounds, albeit in very short bursts...

The first time I was sitting in a Vickers, he strafed me then wheeled around and bombed me. Twice saw them strafe infantry sitting on the Bofors (saw this from both sides of the gun). And was in the back when the pilot strafed some troops coming down the hills...

Quote
they never seem to strafe with their guns - bombs have a higher AI strength, and they never run out, so no reason to ever use guns.
Maybe what I saw is what happens when the pilot has a target lined up but the bombs are still reloading? No bombs available so he resorts to MG's...

But that doesn't explain why it doesn't strafe on El Al.  :-\

Offline Zoologic

  • Masterspammer
  • ****
  • Posts: 4.141
  • In FH Since 0.67
    • View Profile
Great fix! Terrific job done!

They do strafe occasionally in El Al. I was in post overlooking Kidney Ridge, a Stuka turned into my position, strafed at me to no avail. But it manages to suppress me a bit while remaining in position and hope a bullet doesn't pass into me.

The Stuka flies so low, and easily get owned not only by AA guns, but also tanks. And i noticed static MGers do sometimes fire at them.

Offline djinn

  • Masterspammer
  • ****
  • Posts: 5.723
    • View Profile
I'm sure they can be made a tad more accurate later. But for now, they are mostly fine - Save for altitude

Their finest performance is in Mersa Matru and Crete, where they glide in pairs, confuse AA and destroy the most pertinent targets almost every time, supporting the attack directly - And their flying out of field and dying is actually a good balance since they are darn deadly and can at least do 2 or even 3 bomb-runs in one sortie before going out of combat area - A good compromise for their endless supply of bombs - And the bots get back in them often enough to make it feel like 'airfield reload time'


NOTE for the next version update: Please include this and perhaps get fighters firing HE again, and perhaps the typhoon, roockets
« Last Edit: 14-02-2010, 21:02:53 by djinn »

Offline Zoologic

  • Masterspammer
  • ****
  • Posts: 4.141
  • In FH Since 0.67
    • View Profile
The Stuka now nullifies the "invicible Matilda" threat.

I'm now getting pissed off playing as Brits in El Al and Tobruk  ;D But it is great... it show how much the bots can actually do against human player.

But as the Brits... the German FlaKvierling now become deadlier. It is frustrating, since the Beaufighter can't pass more than once above their target. I occasionally got a good laugh when seeing one dodging AA shells while the attacking fighter do a clumsy show behind it and eventually got TKed by ground fire.

Somehow, i feel that the fighters become less aggressive with this enhancement mod. But it is okay, since the ground AA can nix the bombers quite well.