Hard Light Productions Forums

Modding, Mission Design, and Coding => The Modding Workshop => Topic started by: tnathan475 on March 16, 2016, 03:10:50 pm

Title: Weapon range and AI?
Post by: tnathan475 on March 16, 2016, 03:10:50 pm
Having some difficulty getting the AI to shoot at the proper range(s) to target.
Now I know that weapon range should be velocity x lifetime - however I have a situation where the AI is still trying to fire at a target well out of range of it's weapon (this problem seems exasperated when increasing the velocity of the weapon/lowering the lifetime). So this makes me think I am forgetting something or there is another variable at work here I am not seeing. Is there anything in the AI tables that effects this?

What I am trying to accomplish is a weapon with a range of 750m and with a velocity of 1500. (faster and closer even if possible)
I have set the lifetime at 0.5 and the velocity of 1500 (even set +weapon range at 750) - but the AI will attempt to fire it's weapon at the target anyway well past 750m even as the weapons 'life' ends before getting to the target.

Any advice/help with this would be greatly appreciated.
Title: Re: Weapon range and AI?
Post by: Droid803 on March 16, 2016, 03:49:16 pm
http://www.hard-light.net/wiki/index.php/Weapons.tbl#.2BWeapon_Range: (http://www.hard-light.net/wiki/index.php/Weapons.tbl#.2BWeapon_Range:)

Has always worked for me.
Even with 0-lifetime weapons (dummies to trigger other things).
They will start fire a bit outside the range, but usually not too much?
Title: Re: Weapon range and AI?
Post by: FrikgFeek on March 16, 2016, 05:05:36 pm
Check this flag http://www.hard-light.net/wiki/index.php/Ai_profiles.tbl#.24AI_In_Range_Time:

The AI will try to predict when they'll be in range and start firing according to that flag(if it's set under 0 like it is by default).
Title: Re: Weapon range and AI?
Post by: tnathan475 on March 16, 2016, 05:32:48 pm
I set the velocity at 1300, the lifetime of .5 (which should give me a range of 650m) and changed the weapon range line to 650 as well. Made sure all the AI in Range Time are set to 0 (for my current difficulty it was already though)
And I retried my test mission.
I have 2 AI ships (hostile to one another) one pointed right at the other - they both have engines disabled so they cannot move. I have positioned them at 765.5m apart.
The AI ship pointed at the other one immediately begins firing frantically as soon as the mission starts - even though it's target is over 100m farther than the supposed weapon's range.
It's shots of course fall short of the target and the target is never hit - but the ai ship continuously fires at it.
Title: Re: Weapon range and AI?
Post by: Droid803 on March 16, 2016, 08:48:19 pm
What if increase the AI in-range time to a non-zero number (so as to delay it x seconds after they get in range and not before?)
Title: Re: Weapon range and AI?
Post by: tnathan475 on March 16, 2016, 09:38:48 pm
What if increase the AI in-range time to a non-zero number (so as to delay it x seconds after they get in range and not before?)

I suppose that would sort of work as a semi-work-around but wouldn't really fix things as ships may going different speeds all the time.
After further experimentation with this I can't make heads or tails of the current situation - I have a weapon with a velocity of 2000 and a lifetime of 0.325 (and +weapon range set to 650) - which should give me an effective range of 650m, compensated for the length of the laser (30) the max effective range should be 680. However a ship is able to fire at AND hit a ship past 700m.
Title: Re: Weapon range and AI?
Post by: FrikgFeek on March 16, 2016, 09:53:06 pm
Well, yes, the projectile is traveling 33.333... units per frame. So 0.325s is 19.5 frames assuming capped 60 fps meaning the projectile will travel 20 frames before fizzling out on the 21st. That's 666.6666... units travelled plus the length of the laser.
Title: Re: Weapon range and AI?
Post by: tnathan475 on March 16, 2016, 10:03:06 pm
Well, yes, the projectile is traveling 33.333... units per frame. So 0.325s is 19.5 frames assuming capped 60 fps meaning the projectile will travel 20 frames before fizzling out on the 21st. That's 666.6666... units travelled plus the length of the laser.

I am sorry I am not sure I understand what you mean.
If I do understand you correctly (and honestly I am not sure I do) then the max effective range of the weapon should be 666.66 + 30 (length of laser) = 696.66. But the ship is being hit past that.
Title: Re: Weapon range and AI?
Post by: AdmiralRalwood on March 16, 2016, 10:11:38 pm
Are you also accounting for the size of the ship?
Title: Re: Weapon range and AI?
Post by: tnathan475 on March 16, 2016, 10:20:42 pm
Are you also accounting for the size of the ship?

Yup I considered that as well - the ship its firing at has a depth of around 7 and the ship firing has a depth of 12.
Set the distance of target ship to 721m which is more than I should have to (696+19=715), takes a bit but the ship eventually gets hit and destroyed.
Title: Re: Weapon range and AI?
Post by: DahBlount on March 16, 2016, 10:58:40 pm
There is a bit of relevant code at line 8767 in aicode.cpp and based on the code, it looks like the distance it checks against before having the ai fire a primary weapon is generally going to be greater than the range the weapon should go given its lifetime and velocity.
Title: Re: Weapon range and AI?
Post by: tnathan475 on March 16, 2016, 11:39:08 pm
There is a bit of relevant code at line 8767 in aicode.cpp and based on the code, it looks like the distance it checks against before having the ai fire a primary weapon is generally going to be greater than the range the weapon should go given its lifetime and velocity.

Just took a look at that - unfortunately my knowledge of programming is limited and can't really make a whole lot of sense of what I am looking at -- anyway you can help translate the relevant portion for me DahBlount because it looks like you might have hit upon something here - but I am not sure what some of the variables correspond to ('pwip', 'scale' the '->' symbol)

I think this was the portion of the code you where talking about -

               scale = pwip->max_speed/(En_objp->phys_info.speed + pwip->max_speed);
               if (scale > 0.6f)
                  scale = (scale - 0.6f) * 1.5f;
               else
                  scale = 0.0f;
               if (dist_to_enemy < pwip->max_speed * (1.0f + scale)) {
                  if(ai_fire_primary_weapon(Pl_objp) == 1){
                     has_fired = 1;
                  }else{
                     has_fired = -1;
                  }
               }
            }

 
Title: Re: Weapon range and AI?
Post by: AdmiralRalwood on March 17, 2016, 03:00:50 am
Okay, so basically it's making a scale value based on the speed on the weapon compared to the speed of the target; the general idea is to make the AI less likely to fire if the enemy is far away and moving fast relative to the speed of the weapon it would be trying to shoot at it with. Confusingly, this means that the weapon's actual range is completely ignored for primary weapons (unless it's a ballistic primary and "$Primary Ammo Burst Multiplier:" is greater than 0, in which case the range affects the burst probability); while secondary weapons use weapon range to determine if it should fire at all, primary weapons use the above-quoted speed-versus-distance check instead of checking the weapon's range.
Title: Re: Weapon range and AI?
Post by: tnathan475 on March 17, 2016, 07:33:40 am
Okay, so basically it's making a scale value based on the speed on the weapon compared to the speed of the target; the general idea is to make the AI less likely to fire if the enemy is far away and moving fast relative to the speed of the weapon it would be trying to shoot at it with. Confusingly, this means that the weapon's actual range is completely ignored for primary weapons (unless it's a ballistic primary and "$Primary Ammo Burst Multiplier:" is greater than 0, in which case the range affects the burst probability); while secondary weapons use weapon range to determine if it should fire at all, primary weapons use the above-quoted speed-versus-distance check instead of checking the weapon's range.

So unfortunately I am guessing there is no real solution to my problem other than changing the code above and re-compiling the source code?
What do the variables 'pwip' and 'scale' represent in the above code?
Title: Re: Weapon range and AI?
Post by: AdmiralRalwood on March 17, 2016, 07:50:20 am
"scale" is the scaling factor I just mentioned; "pwip" is a pointer to the weapon info for the primary weapon the code is evaluating (the "->" operator retrieves an attribute of the object a pointer points to, so "pwip->max_speed" is the velocity of the primary weapon and "En_objp->phys_info.speed" is the current speed of the AI's current target).

And no, there's no way to fix this without changing the source; on the plus side, there are probably lots of other modders that could use weapon range checks with primary weapons, so it's less unlikely to make it into the source than any arbitrary feature request.
Title: Re: Weapon range and AI?
Post by: tnathan475 on March 17, 2016, 07:59:55 am
"scale" is the scaling factor I just mentioned; "pwip" is a pointer to the weapon info for the primary weapon the code is evaluating (the "->" operator retrieves an attribute of the object a pointer points to, so "pwip->max_speed" is the velocity of the primary weapon and "En_objp->phys_info.speed" is the current speed of the AI's current target).

And no, there's no way to fix this without changing the source; on the plus side, there are probably lots of other modders that could use weapon range checks with primary weapons, so it's less unlikely to make it into the source than any arbitrary feature request.

Thanks very much for this info AdmiralRalwood as well as everyone else - looks like I need to do some brushing up on C++. At least I have somewhat of a place to start from now - albeit only a partial understanding of that starting place.
It is good to know there is a chance (even if slim) of this making it into the code eventually as this is something that has annoyed me for a long time with many modding attempts - especially if i can't figure it out by myself by fumbling around with it.
Title: Re: Weapon range and AI?
Post by: Droid803 on March 17, 2016, 10:56:19 am
... while secondary weapons use weapon range to determine if it should fire at all...

That explains why I haven't had problems with that - i was testing using secondaries.
Title: Re: Weapon range and AI?
Post by: tnathan475 on March 17, 2016, 11:19:19 am
... while secondary weapons use weapon range to determine if it should fire at all...

That explains why I haven't had problems with that - i was testing using secondaries.

Yeah secondaries behave themselves - but thanks for the suggestions anyhow Droid803!