Author Topic: [Documentation] Scout/Sniper AI  (Read 2181 times)

Offline ohartenstein23

  • Commander
  • *****
  • Posts: 1718
  • Flamethrowers fry cyberdisk circuits
    • View Profile
[Documentation] Scout/Sniper AI
« on: August 28, 2017, 03:44:59 pm »
Scout/Sniper AI is my latest OXCE+ project, adding squadsight capabilities for the aliens; now they can camp in smoke just like you!  The code adds two new unit parameters that control this behavior:
  • spotter: -1 - When a unit has a nonzero scout value, it sets a flag on any units it spots that allows sniper-enabled enemy units to "know" about the spotted target.  The value placed here determines the number of turns the knowledge of the spotted target is available, -1 sets the value to the unit's intelligence score and 0 disables this behavior.
  • sniper: 75 - This determines the percent probability that the AI unit will act on knowledge of targets spotted by scouts.  When active, the sniper AI will pick from a list of the spotted targets and attempt to attack them whether or not the sniper can 'see' the target.

The choice of attack type depends on the sniper's weapons, accuracy, intelligence, and aggression.  The AI calculates a score for each firing mode available on the weapon as well as considering throwing a grenade, and then picks the attack based on which mode it thinks will be the most efficient.  The exact formula is:
Spoiler:
score = number of shots * accuracy / percent TU cost of the attack, where accuracy will be modified by the UFO extender formula if that option is turned on (recommended).
The score is then modified by intelligence: score = score * RNG::(100 - int_coeff * (10 - intelligence), 100 + int_coeff * (10 - intelligence)), and is further modified by aggression for auto shot:
 score = score * (100 + aggro_coeff * (aggression - 1))

The coefficients are explained below.

How this score is affected by intelligence and aggression is determined by these global parameters:
  • fireChoiceIntelCoeff: 5 - This parameter is a measure of how much a unit's intelligence matters when determining what the most efficient firing mode is; lower intelligence means more randomness in the choice, while 10 intelligence means the unit will always pick the most efficient mode.  Defaults to 5, which leads to a 50%-150% RNG roll modifier to score for 0 intelligence, reducing linearly to 100%-100% at 10 intelligence.
  • fireChoiceAggroCoeff: 5 - This parameter measures how much a unit's aggression bumps up the chances of using auto shot - aggression above 1 will be an increasing multiplier to the efficiency score for auto shot, while aggression of 0 will be a decrease.  Default value is 5.

The ruleset for using these values will look like this:
Code: [Select]
units:
  - type: STR_SECTOID_SOLDIER
    sniper: 75 # Sectoid soldiers have a 75% chance of taking a sniper action when the AI has determined it will attack
  - type: STR_SECTOID_NAGIVATOR
    spotter: -1 # Sectoid navigators will spot for the soldiers, giving them as many turns to act on your soldiers as the navigator's intelligence

ai:
  fireChoiceIntelCoeff: 10 # Make the choice of attack more RNG dependent
  fireChoiceAggroCoeff: 1 # Aggression doesn't matter as much

The source code is available on my GitHub page.

Edit 171110: This is an official part of OXCE+, so the links for my repository above are deprecated.  This post will remain for documentation.
« Last Edit: July 17, 2018, 02:37:12 pm by Meridian »

Offline ohartenstein23

  • Commander
  • *****
  • Posts: 1718
  • Flamethrowers fry cyberdisk circuits
    • View Profile
Re: [EXE] Scout/Sniper AI
« Reply #1 on: October 04, 2017, 05:48:51 pm »
After cleaning up some of the code and adding some comments at Meridian's suggestion, I've also added a setting for an accuracy penalty for actions taken with no line-of-sight to the target.  This is a percentage multiplier on accuracy when firing on both empty tiles and units to which the firing unit has no LOS, set by the following ruleset options:

  • noLOSAccuracyPenaltyGlobal: 50 - This sets a global multiplier on accuracy when having no LOS, the example of 50 multiplies the accuracy by 50%.  Defaults to -1 to disable this feature unless specifically set on single items.
  • noLOSAccuracyPenalty: 75 - Same as the global setting, but only on individual weapons.  Overrides the global setting when not at the default of -1.

When a player unit fires on a target with a weapon that has this penalty, an alert will show, similar to the no line of fire message, using the string STR_NO_LOS_PENALTY.

A sample ruleset implementing this feature might look like this:
Code: [Select]
noLOSAccuracyPenaltyGlobal: 50

items:
  - type: STR_RIFLE
    noLOSAccuracyPenalty: 90 # makes the rifle better than most for sniping

extraStrings:
  - type: en-US
    strings:
      STR_NO_LOS_PENALTY: "Accuracy loss for no LOS!"

An amended commit is available at the link in the first post.

Edit: I made a mistake in my first post, the ruleset parameter "scout" should actually be "spotter."  I've amended the documentation to match, sorry.

Offline mumble

  • Colonel
  • ****
  • Posts: 335
    • View Profile
Re: [EXE] Scout/Sniper AI
« Reply #2 on: October 09, 2017, 05:29:11 am »
is it possible to exempt the LOS accuracy loss for shots within 1 tile of the target? One unfortunate side effect of this is trying to blow open certain segments of walls on purpose enforces the accuracy drop, which seems unrealistic when I'm only targeting DIRECTLY on the other side of the wall...

Online Meridian

  • Global Moderator
  • Commander
  • ***
  • Posts: 5557
  • Aaand we're back!
    • View Profile
    • My Wiki
Re: [EXE] Scout/Sniper AI
« Reply #3 on: January 31, 2018, 08:38:21 pm »
UPDATE:

The LOS accuracy penalty has been reworked.

Current status:
1. If aiming at a unit, the vanilla LOS (line of sight) is checked... if you can see the unit (red enemy indicator box), there is no penalty, if you cannot see the unit (green enemy indicator box), there is penalty; LOF (line of fire) doesn't matter

This is 100% consistent with vanilla definition of LOS.

2. If aiming at a tile without a unit (or at a tile, which has a unit that you don't yet know about), vanilla LOF (line of fire) is checked and some parts of LOS checks are done too, not all (LOS checks done are: max view distance, day/night visibility, smoke). LOS checks not done are: psi vision, heat vision, camouflage/anti-camouflage. Arcing weapons and throwing considers LOF of a projectile-weapon (i.e. a straight line, not a parabola)!

There is no definition of LOS for tiles without a unit in vanilla... the algorithm above is the best definition I could think of.

PS: The aiming cursor (if extender accuracy user option is turned on) does consider the penalty now and correctly shows the real accuracy used for calculations.

PS2: A pretty nice side effect is that if a penalty is set, you can use it to test vanilla LOF check without actually shooting... some people may find it amusing... have fun

Offline Eddie

  • Colonel
  • ****
  • Posts: 495
    • View Profile
Re: [Documentation] Scout/Sniper AI
« Reply #4 on: July 19, 2018, 04:02:44 am »
I recently played a night mission against sniper/spotter enabled enemies (Spartans in Piratez). The AI did things that felt a bit... cheaty.
Basically, cover of darkness does not work anymore. As soon as you land a hit on a spotter, whether he sees you or not, the shooter is marked as a valid sniper target. Then for two to three turns, the AI has maphack on that gal. You can run to the other end of the map in the cover of darkness, you still get hit with sniper fire (if there is line of fire). Save is attached if you want to experience what I mean.
Am I exaggerating or is that how the code works?

Behaviour that feels cheaty:
- Shooting a spotter without him seeing you still makes you a sniper target.
- Changing your position to a location no enemy can see does not save you.

I can see the logic of shooting in a direction from where you hear/see shots coming. But the targeting is too precise for that, and moving away/out of sight should save you. The AI did also throw grenades at me, which felt far more appropriate.

So... what is your opinion on this?

Offline ohartenstein23

  • Commander
  • *****
  • Posts: 1718
  • Flamethrowers fry cyberdisk circuits
    • View Profile
Re: [Documentation] Scout/Sniper AI
« Reply #5 on: July 19, 2018, 04:47:46 am »
The code is working exactly as intended, what you're running into are balance concerns.  If you think 2-3 turns of having to hide a soldier in cover is too punishing, then explain the reasoning to Dioxine.  Otherwise, you might have to learn new tactics to counteract it.

Included with this code is the ability to define a hefty accuracy penalty on these shots when you can't directly see the target, and this affects the AI too - so while they make take the shots, it's a lesser chance of being hit by them.

Offline Eddie

  • Colonel
  • ****
  • Posts: 495
    • View Profile
Re: [Documentation] Scout/Sniper AI
« Reply #6 on: July 19, 2018, 01:19:19 pm »
What actually bothered me was not the balance but the logic of it. It took me a while before I could figure out what is going on. I made sure all my gals are in cover of darkness, but I still got shot. I thought there are some faint light sources I kept overlooking.

I can think of a counter tactic that is equally unlogical. You can use soldiers that have not been marked to form a wall. Hiding behind them will deny line of fire and prevent sniping. Not sure about grenades though.

There might be a way to improve the logic of the Scout/Sniper behaviour. Have two different types of target awareness. Targets that have actually been spotted this round behave as is now. The second class is targets that are marked but out of sight. These get handled differently by the AI.
Possible differences:
- Less likely to shoot, more likely to use grenade/explosive.
- Grenades targeted not on actual location but on the tile where the spotting occured / last known location.
- When a night mission, throw flare at the location.
- More accuracy penalty (shooting blindly into the dark).

If the AI can throw flares at you it would be more logical and even more dangerous.

I have no idea what the code looks like, please forgive me if I make suggestions that are impossible to implement.

Online Meridian

  • Global Moderator
  • Commander
  • ***
  • Posts: 5557
  • Aaand we're back!
    • View Profile
    • My Wiki
Re: [Documentation] Scout/Sniper AI
« Reply #7 on: July 19, 2018, 01:28:29 pm »
I can think of a counter tactic that is equally unlogical. You can use soldiers that have not been marked to form a wall. Hiding behind them will deny line of fire and prevent sniping. Not sure about grenades though.

Sniper is not limited to sniping only (based on modder's decision).
They can just as easily shoot at your living wall.

There might be a way to improve the logic of the Scout/Sniper behaviour. Have two different types of target awareness. Targets that have actually been spotted this round behave as is now. The second class is targets that are marked but out of sight. These get handled differently by the AI.

That's exactly how it is implemented.
And it is up to the modder to decide the weight for type 1 and type 2.

I have no idea what the code looks like, please forgive me if I make suggestions that are impossible to implement.

Already implemented this way... unless there's a bug of course.