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

Offline ohartenstein23

  • Commander
  • *****
  • Posts: 1933
  • 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: 1933
  • 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: 338
    • 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...

Offline Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 8974
    • View Profile
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

  • Commander
  • *****
  • Posts: 560
    • 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: 1933
  • 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

  • Commander
  • *****
  • Posts: 560
    • 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.

Offline Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 8974
    • View Profile
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.

Offline Ronios

  • Sergeant
  • **
  • Posts: 27
    • View Profile
Re: [Documentation] Scout/Sniper AI
« Reply #8 on: December 23, 2020, 11:32:22 am »
After 2.5 years, I would like to draw your attention once again to the fact that this feature, although it works as intended, is absolutely at odds with the logic. We're talking about night missions in XCF. I perfectly understand that I can hide my people behind walls / stones, etc. But there are a lot of situations when there is a bare field around and it is impossible to hide physically.

Option one: The rat came under the light of a flashlight on the ground and ran away 20 meters from this place on the same turn. This was noticed by the spotter and at the enemy turn, without flashing or detection, it is completely unknown where the sniper kills the rat.
Option two: Any hit on an enemy marked "spotter", as described above, automatically makes a target out of the character. Only the death of the spotter(but not always) can stop this madness and using grenades that don't triggering reaction.

I managed to see many other points and understood well how it works. Don't you think it's better to use not the "current position of the character" in any situation, but to write down the coordinates of where the target was last seen or where the shot was fired from?

You can say that I can change the settings so that the opponents will stop shooting in the dark and will only be at a visible target at the moment, and you will be right. I can do that. But this does not change the fact that this mechanic is initially built on strange logic. In general, I think the possibility that opponents will shoot at the places from where my agents were shooting is very natural and interesting and I don't want to lose that cool mechanic.

I beg you, please reconsider the approach to this feature and make it a pleasant and interesting and not annoying (especially for ironman)

As an option, you can show all opponents who hit and did not kill the agents, for several turns at any point on the map, for honesty, so to speak ;D.
« Last Edit: December 23, 2020, 01:45:59 pm by Ronios »

Offline ohartenstein23

  • Commander
  • *****
  • Posts: 1933
  • Flamethrowers fry cyberdisk circuits
    • View Profile
Re: [Documentation] Scout/Sniper AI
« Reply #9 on: December 23, 2020, 03:43:10 pm »
As I've said in other threads on the same topic, no. I am not going to neuter this feature to make it "more enjoyable", "more realistic", or "more logical". It fits within the logic of the current AI and how it behaves. I want to leave it as a tool for modders to provide challenge where previously there was just advantage to the player. It should be dangerous to deal with open fields and make you pause to consider whether to advance or retreat/abort.

To specifically answer you point about marking a location - this opens up the feature to be more exploitable. In your example with the rat, changing the feature to your suggestion means that you can tie up all snipers on the map indefinitely shooting at a light by just showing your rat then running it away every turn.

Offline Juku121

  • Commander
  • *****
  • Posts: 1790
  • We're all mad here.
    • View Profile
Re: [Documentation] Scout/Sniper AI
« Reply #10 on: December 21, 2021, 11:35:24 am »
And it is up to the modder to decide the weight for type 1 and type 2.
Does this reference the 'sniper: XX' chance? Because if so, I don't think it quite addresses the original question which essentially asked about having two different sniping behaviours, not two different behaviours for snipers.

If Meridian indeed meant two different sniping patterns, I'd be very interested to know how that works. Or worked, if it's been changed before or after.

Offline Vakrug

  • Colonel
  • ****
  • Posts: 318
    • View Profile
Re: [Documentation] Scout/Sniper AI
« Reply #11 on: June 10, 2023, 03:39:16 pm »
After searching for a while about how exactly Spotter/Sniper mechanics works, I stumbled upon this post. Since Spotter/Sniper mechanics is very counterintuitive, I was hoping at least here get a definitive answers.

This post will remain for documentation.
Sorry for rudeness, but this is very crappy documentation. Nothing in the first post indicates that a soldier gets spotted if it hits a spotter! Even after some discussion about this in the next posts. Do you think that it is such a small and insignificant detail, that is is not worth mentioning?

So what exactly makes a soldier being spotted by a spotter? There are lot of rumors out there, I will list some of them:
  • Does killing a spotter ends "being spotted"? Or a spotter needs to be "one shoted"? Or is it just a rumor?
  • Does hitting a spotter with a grenade prevents being spotted? What about mortars? What about rocket launchers? What about "rocket missed a target, but hit a wall, so a target still get damaged"? Is there other "silent" methods to kill?
  • Is there other "unimportant" details about this mechanics that is not present in documentation?

Offline Juku121

  • Commander
  • *****
  • Posts: 1790
  • We're all mad here.
    • View Profile
Re: [Documentation] Scout/Sniper AI
« Reply #12 on: June 10, 2023, 05:48:06 pm »
Does killing a spotter ends "being spotted"? Or a spotter needs to be "one shoted"? Or is it just a rumor?
Rumour.

Does hitting a spotter with a grenade prevents being spotted? What about mortars? What about rocket launchers? What
about "rocket missed a target, but hit a wall, so a target still get damaged"? Is there other "silent" methods to kill?
Generally, yes. To be spotted for snipers, you need to either 1) be 'seen' or 2) be hit with a 'projectile'. Rockets and mortars count, if they hit the enemy first. Grenades do not.

In practice, it's quite difficult to predict with precision what will and won't happen, especially since there is also the 'spotted' flag that follows somewhat different rules but can invite a spotter over, including enemies dying over several turns from bleeding; the general difficulty of determining whether an enemy sees your troops, or indeed knowing what kinds of things your opponent can or cannot see (psi-vision, night vision, heat vision, camo on either party, etc).
« Last Edit: June 10, 2023, 05:50:51 pm by Juku121 »

Offline Vakrug

  • Colonel
  • ****
  • Posts: 318
    • View Profile
Re: [Documentation] Scout/Sniper AI
« Reply #13 on: June 10, 2023, 07:29:23 pm »
To be spotted for snipers, you need to either 1) be 'seen' or 2) be hit with a 'projectile'.
So is it safe to kill with melee weapons (from behind, of course)? What about flamethrowers? I am not sure do they shoot projectiles or not.

If only this mechanics was better documented (including in game), so much interesting things could be done, like "silent" weapons (shurikens or spy pistols, for example) that don't cause being spotted.

Offline Juku121

  • Commander
  • *****
  • Posts: 1790
  • We're all mad here.
    • View Profile
Re: [Documentation] Scout/Sniper AI
« Reply #14 on: June 10, 2023, 08:10:14 pm »
Melee kills are 'safe' in the sense that if you kill an unaware spotter, that won't spot you for snipers. He/she/it will still spot you for regular AI when dying, no matter how sneaky you were. Never mind that stealthing up to an enemy and remaining unseen after the kill are no small feats.

If you mean XCF flamethrowers, they're basically short-range grenade launchers, and the sheet of flames is a projectile. Given how close you need to be and how inaccurate the weapon is, I'd guess it'd be an exercise in frustration trying to stay unsnipered by using one to kill enemies 'silently'.

No idea if other mods are different.

The whole engine isn't really setup for combat stealth, UI included. So while I share your frustrations regarding the sniper-spotter mechanic, this lack of stealth isn't really 'vanilla' sniper-spotter's fault.

I think Finnik's fork had some more stealth mechanics? And Xilmi's AI of course has its own spotting system, which specifically excludes this 'hit and get spotted' feature.