Hard Light Productions Forums

Modding, Mission Design, and Coding => FS2 Open Coding - The Source Code Project (SCP) => Topic started by: ##UnknownPlayer## on July 17, 2002, 10:08:47 pm

Title: AI using the maxim on fighters
Post by: ##UnknownPlayer## on July 17, 2002, 10:08:47 pm
I've successfully fixed the problem with the AI using the maxim on fighters. If possible now, the AI will switch to a different weapon to attack any shielded target, and switch back to the maxim (if possible) to hit an unshielded target.

I've uploaded the changes to the CVS, they're all in aicode.cpp under ai_select_primary_weapon or search for 'unknownplayer',
Title: AI using the maxim on fighters
Post by: Bobboau on July 17, 2002, 10:29:29 pm
could you post a snipet
or explain how it works
Title: AI using the maxim on fighters
Post by: ##UnknownPlayer## on July 17, 2002, 10:45:37 pm
It's a pretty long piece of code, so I won't post it here, but I'll describe the basic process.

The heart of it depends upon this snippet here - which searches the banks of an AI ship and finds a weapon with a given flag - in this case WIF_SHUDDER
Code: [Select]

for (int i = 0; i < swp->num_primary_banks; i++)
{
if (swp->primary_bank_weapons[i] > -1) // Make sure there is a weapon
{
if (!(Weapon_info[swp->primary_bank_weapons[i]].wi_flags & WIF_SHUDDER & WIF_PUNCTURE))
{
swp->current_primary_bank = i; // Found a match, arm it
return i; // Exit function
}
}
}



Using this code, we can use it to search for different properties. However since the Maxim is the only gun that uses WIF_SHUDDER it is appropriate to use that as a search criteria.

If a match is found, the function arms that weapon and then returns. If no match is found, it will continue to the next if or for statement.

All I've done is put in a simple check to see if the target has a shield system (via the OF_NO_SHIELDS flag):

Code: [Select]

if (other_objp->flags & OF_NO_SHIELDS)
{
      ...
}


...and if so then we search for and try and arm the maxim.

In the actual code there is quite a few more conditions of course to try and not arm the maxim otherwise, and to try and not arm puncture weapons (disrupters - original use of the code) otherwise, but in essence this is it's functioning.

[EDIT] It's important to note that other_objp is a object structure that points to the ai's target.[/EDIT]
Title: AI using the maxim on fighters
Post by: IceFire on July 17, 2002, 10:47:46 pm
Shudder shouldn't be taken for granted seeing as other MODs may make use of that code and have a shield breaking weapon as that weapon.

Maybe we should specify "low shield" or something like that.
Title: AI using the maxim on fighters
Post by: ##UnknownPlayer## on July 17, 2002, 11:19:05 pm
I'm aware of the problems using shudder, it's just for the time being I didn't want to mess with adding in a new tag. Once we do add a separate tag, it'll be easy to change this to use that as the test.

On that note, what should the tag be?

[EDIT] The other advantage is that modded table files aren't needed for this to work

The other way I could change it is by making it look for the highest shield factor weapon. [/EDIT]
Title: AI using the maxim on fighters
Post by: Kazan on July 17, 2002, 11:24:02 pm
how about searching based upon the weapons shield-effectiveness coefficient
Title: AI using the maxim on fighters
Post by: ##UnknownPlayer## on July 18, 2002, 12:01:07 am
I just considered doing that, but it quickly becomes hard to account for all the possibilities. I've recoded it so that it will now work off the best hull damaging weapon if the target has no shield system. If it does, it just picks a non-puncture (i.e. non disrupter) weapon and if that fails it just picks the first weapon.

In the game, so long as it attacks ships without shields with the Maxim I'll be happy, but to make this a happy coding experience I'd tear the whole thing out and rewrite it to do a huge equation to calculate weapon effecitveness (something like energy consumption per shot * rof / etc. etc. etc.)
Title: AI using the maxim on fighters
Post by: Bobboau on July 18, 2002, 12:05:07 am
like this

Code: [Select]

if (swp->num_primary_banks > 1){ // if there is more than one bank

swp->current_primary_bank = 0;
for (int i = 0; i < swp->num_primary_banks; i++){

if (swp->primary_bank_weapons[i] > -1){  //if there is a weapon

if (Weapon_info[swp->primary_bank_weapons[i]].shield_factor > Weapon_info[swp->primary_bank_weapons[swp->current_primary_bank]].shield_factor) //if this weapon has a better shield factor use it
{
swp->current_primary_bank = i; //arm it
}
}
}
return i;
}else{
return 0;
}
}
Title: AI using the maxim on fighters
Post by: ##UnknownPlayer## on July 18, 2002, 12:45:12 am
But that has problems - the fighter would always use a Circe if one is loaded because it only checks for a shield system, not shield energy. You have to strike a balance, though in the code I just uploaded to the CVS there's plenty of room for a better shielded weapon choice (just edit the else part of the if statement)
Title: AI using the maxim on fighters
Post by: Bobboau on July 18, 2002, 12:51:21 am
well I was just mentioning code, for selecting weapons that would be better at damageing sheilds, all you have to do to that would be add an
if(target has sheilds and those sheilds are still fairly strong)
to the start of it
note the still fairly strong as oposed to completly dead becase prety much all weapons do some sheild damage and the sheolds will probly always have some degree of power in them
Title: AI using the maxim on fighters
Post by: ##UnknownPlayer## on July 18, 2002, 01:09:47 am
As far as I can tell that's more complex then you'd think. I don't fully understand how shields get calculated, but no where do they store an average, and there's no easy way to grab what side of the ship the AI is on without resorting to 3D maths - which I don't want to do yet.
Title: AI using the maxim on fighters
Post by: Bobboau on July 18, 2002, 02:20:45 am
get_shield_strength(shipp->objnum)
:)
Title: AI using the maxim on fighters
Post by: ##UnknownPlayer## on July 18, 2002, 03:44:27 am
I love it!

[EDIT]
I've rewritten the primary weapon select code again so that the fighter now 'intelligently' chooses which gun to use.

Above 50% shield integrity it'll use its better anti-shield guns,
below 50% it'll try and use more balanced guns and below 5% it'll go for hull destroying guns.
Title: AI using the maxim on fighters
Post by: Inquisitor on July 18, 2002, 08:36:10 am
Just make sure it's all commented.

By the way, all anyone has to do to see the code is browser the web based CVS here:
http://fs2source.warpcore.org/cgi-bin/cvsweb/cvsweb.cgi/

And this specific code is:
http://fs2source.warpcore.org/cgi-bin/cvsweb/cvsweb.cgi/fs2_open/code/Ship/aicode.cpp?annotate=2.5

Original documentation on this code is:
http://fs2source.warpcore.org/dox/AiCode_8cpp.html