# Spy detection

#### Bhruic

##### Emperor
This is (hopefully!) a complete summary of how spy detection and interception works. All numbers are assuming an unmodified version of the game. For the sake of consistency, this example will assume that "we" or "us" are the ones spying, and "they" or "them" are the ones defending.

There are a number of variables to take into consideration. The first is the intercept modifer. If we have an Open Borders treaty with them, we have "ESPIONAGE_SPY_NO_INTRUDE_INTERCEPT_MOD", or ESNIIM = 10 (it's stored as -90, but the actual value is used as "100 + ESNIIM"). If not, we have "ESPIONAGE_SPY_INTERCEPT_MOD", or ESIM = 25 (or -75). The next variable is the ratio of total espionage points between us and them. That is the complete total amount of espionage points ever put in, not merely the points we have put into them or vice versa. Let's call our total points as OME, and theirs as TME, and the ratio as REP. The formula for the ratio is: TME / (TME + OME). This number is multiplied by a variable called "ESPIONAGE_INTERCEPT_SPENDING_MAX", or EISM, which is defined as 25.

Next is counter-spying. Counter-spying can be accomplished in two ways - by them having a spy on the square we are on, or by them having built the Security Bureau in the city we are in. If that's the case, that triggers the "ESPIONAGE_INTERCEPT_COUNTERSPY" or EIC, value of 15. Next, is the possibility that they have ran the counter-intelligence mission against us. If that's the case, they triggered "ESPIONAGE_INTERCEPT_COUNTERESPIONAGE_MISSION", or EICM which is defined as 20. And finally, if our spy just moved onto the square, or if we already have a spy on that square, we trigger "ESPIONAGE_INTERCEPT_RECENT_MISSION", or EIRM, value of 15.

So to recap:
ESNIIM = 10
ESIM = 25
REP = TME / (TME + OME)
EISM = 25
EIC = 15
EICM = 20
EIRM = 15

The complete formulas, assuming all values are relevant is:
((EISM * REP) + EIC + EICM + EIRM) * ESNIIM (Open Borders)
((EISM * REP) + EIC + EICM + EIRM) * ESIM (No Open Borders)

Note that the:
((EISM * REP) + EIC + EICM + EIRM)
part of the formula is capped at a range of 0..100.

Once the formula is used to get a number, the game generates a random number from 0..9999. If the random number is greater or equal to the number generated by the formula, then the spy is not discovered. If the random number is less than the number from the formula, the spy is caught.

A couple examples to give some concrete numbers:
Our spy moves on to their tile where they have a spy. We don't have an open borders treaty with them. They have not run a counter-intelligence mission on us. We have generated a total EPs of 1000 over the game, they have generated 3000.

REP = TME / (TME + OME)
REP = 3000 / (3000 + 1000)
REP = 0.75

= ((EISM * REP) + EIC + EICM + EIRM) * ESIM
= ((25 * 0.75) + 15 + 0 + 15) * 25
= 1218.75
= 1218 (rounded down)

So percentage-wise, they have a 12.2% chance of detecting our spy.

Another example. Our spy has been sitting on a square for 2 turns, they have no spy on the square, they have not run a counter-intelligence mission, and we have an Open Borders treaty with them. Let's reverse the EPs from above (so we have generated 3000, they have generated 1000).

REP = TME / (TME + OME)
REP = 1000 / (1000 + 3000)
REP = 0.25

= ((EISM * REP) + EIC + EICM + EIRM) * ESNIIM
= ((25 * 0.25) + 0 + 0 + 0) * 10
= 62.5
= 62 (rounded down)

So percentage-wise, they have a 0.6% chance of detecting our spy.

Bh

#### _alphaBeta_

##### King
Excellent! Finally! Thanks again Bhruic for this writeup. You are the true hero of BTS in my book.

I find it very interesting that the ratio of between you and them is not relevant (the ratio you get when you mouse-over other civs' leaderheads). It's rather related to the ratio of the total amount of you've generated against all opponents in the game compared to other civs. Seems counter intuitive to me. Although it allows protection from everyone in case your concentrating all your against one opponent. I'll consider doing that now.

I also find it interesting how spies only defend the tile they're sitting on. I thought there would have been some kind of adjacent tile protection as well.

So, to increase the chance of finding spies in your territory one should:

1. Close borders
2. Station spies on important tiles. Only one spy helps, anymore are irrelevant. Once a security bureau is present in a city, there's no need for a spy there anymore.
3. Run counter-espionage missions

Only other question that comes to mind is what influences the % of success when performing an active mission? Do the same rules you've outlined apply?

##### giggling permanoob
thank you! so basically i got really ultra lucky catching a spy near a city where i was spy-less for miles.

so the active spy mission "Perform Counterespionage" not only increases the cost of their missions like the pedia says, but also increases my chance of catching them, correct?

EIC is always 0 or 15? so once i have a security bureau in a city, a spy standing there is wasting her time?

so, my dear friend , when are you pretty please going to write a summary on how to get the best value for your espionage points? the pedia mentions "religions present" as a factor but i didn't find out until yesterday or the day before that you get a bonus for picking a target city with your SR, and get an extra bonus discount for owning the holy city. knowing that i was able to steal two techs from zara, if i hadn't realized it i probably wouldn't have been able to afford the second. and i heard a rumor that the cost is affected (i think it goes down) the more you've spent on active missions? i dunno. anyway, if you (or anybody else who understands the factors) get bored in your copious free time, that would be lovely!

#### slaze

##### Deity
Question. You mention this formula...
Note that the:
((EISM * REP) + EISM + EIC + EICM + EIRM)
part of the formula is capped at a range of 0..100.

...but then you use this formula...
= ((EISM * REP) + EIC + EICM + EIRM) * ESIM
...What is the top formula used for? Or is it just the bottom?

Using the bottom formula it seems that that highest probability of detection would be
((25 * 1.0) + 15 + 20 + 15) * 25 = 1875, or 18.8%

Although a more realistic number (with equal espionage) might be
((25 * 0.5) + 15 + 20 + 15) * 25 = 1562.5, or 15.6%

It's funny cause it feels like I detect alot of enemy spies about one turn into my borders. I'm surprised to see it so low

Only one spy helps, anymore are irrelevant. Once a security bureau is present in a city, there's no need for a spy there anymore.

so once i have a security bureau in a city, a spy standing there is wasting her time?

It seems so. No more multiple spies per city anymore. Whoops

#### Roland Johansen

##### Deity
Great article. Very useful.

Okay, now to get some intuition for the strength of the various terms in the formula:

The basic chance that your spy is caught is a value between 0% and 2.5% when you have open borders and a value between 0% and 6.25% when you have closed borders. This chance is dependent on the total amount of espionage points produced by you and your target civilisation.
An enemy spy or the security bureau present in the tile of your spy increases the chance of being caught by 1.5% when you have open borders and 3.75% when you have closed borders.
A counterespionage mission performed against you increases the chance of being caught by 2% when you have open borders and 5% when you have closed borders.
When one of your spies has just moved or is on the same tile as another one of your spies, then it has a 1.5% increased chance of being caught with open borders and a 3.75% increased chance of being caught with closed borders.
These chances are summed up meaning that with open borders, the chance of being caught is a value between 0% and 7.5% and with closed borders, the chance of being caught is a value between 0% and 18.75%.

#### Bhruic

##### Emperor
Sorry, the second EISM in that one formula was a typo. I'll correct the original post.

Bh

#### Bhruic

##### Emperor
EIC is always 0 or 15?

EIC is always 15 - if it's used. The "0" I used in that one example was to indicate that EIC wasn't used for that particular example, so wasn't factored into the equation.

Bh

#### Roland Johansen

##### Deity
Sorry, the second EISM in that one formula was a typo. I'll correct the original post.

Bh

Ok, with so many variables, that is bound to happen.

By the way, if I understand the article correctly, then the term

((EISM * REP) + EIC + EICM + EIRM)

would have a value between 0 and 75 and so with the standard values of the variables the capping at a value between 0 and 100 has no effect. Did I understand that correctly?

Rince did some tests with the effects of adding multiple defending spies to a tile and there seemed to be a relation between the number of defending spies and the chance to capture a spy. Could it be that the variable EIC is added multiple times for each defending spy? That would explain his results very well and you also notice the effect of capping the mentioned term at a value of 100.

Here is the post that I'm referring to: table of spying results.

By the way, does movement inside a ship count as movement for the spy? Does movement inside a ship increase the chance of being caught or not?

#### reknaw

##### the Hopeless
Can a spy be caught if it's occupying an enemy tile that also contains your military units? I ask because I've been toying with the idea of including multiple spies in my SoDs to replace accuracy seige weapons, I don't think I would take the chance though if they could all be lost while en route.

#### Roland Johansen

##### Deity
Can a spy be caught if it's occupying an enemy tile that also contains your military units? I ask because I've been toying with the idea of including multiple spies in my SoDs to replace accuracy seige weapons, I don't think I would take the chance though if they could all be lost while en route.

I've read a report of this happening and there is nothing in the article that indicates that the stack protects the spy.

Some people think that spies are an effective fast way to lower the city defences. The chance of being caught is not that high and can be countered by using multiple spies (although that increases the chance of capturing one of them).

#### Bhruic

##### Emperor
would have a value between 0 and 75 and so with the standard values of the variables the capping at a value between 0 and 100 has no effect. Did I understand that correctly?

I think so, yes. I just mention the "capping" effect because it's in the code... You're right that standard values will never exceed the range.

Rince did some tests with the effects of adding multiple defending spies to a tile and there seemed to be a relation between the number of defending spies and the chance to capture a spy. Could it be that the variable EIC is added multiple times for each defending spy? That would explain his results very well and you also notice the effect of capping the mentioned term at a value of 100.

No, there's no evidence that multiple spies have any effect on things. Perhaps he simply didn't run enough tests for the randomness of it to truly factor in?

By the way, does movement inside a ship count as movement for the spy? Does movement inside a ship increase the chance of being caught or not?

No, they changed it in 3.13 so that spies can't be caught if they are in a ship.

Bh

#### Bhruic

##### Emperor
Only other question that comes to mind is what influences the % of success when performing an active mission? Do the same rules you've outlined apply?

The chance of success for a spy mission is an almost identical formula. In fact, it is an identical formula, although it's for the chance to be detected, not the chance to be successful.

If you recall in my first post, I list "ESPIONAGE_SPY_NO_INTRUDE_INTERCEPT_MOD" and "ESPIONAGE_SPY_INTERCEPT_MOD" as being 10 and 25, respectively? But the actual values in GlobalDefines.xml is -90 and -75, I just handled the fact it does 100 + X (where X is the -90 or -75) by short cutting it.

So the formula is identical, just replace the -90 or -75 with the difficulty modifier (DM) for that particular mission.

The percentage chance of being detected while performing a mission is:
(((EISM * REP) + EIC + EICM + EIRM) * (100 + DM)) / 100

The dividing by 100 is just to get it into percentage form. The chance of being successful, therefore, is:
100 - ((((EISM * REP) + EIC + EICM + EIRM) * (100 + DM)) / 100)

Bh

#### Roland Johansen

##### Deity
No, there's no evidence that multiple spies have any effect on things. Perhaps he simply didn't run enough tests for the randomness of it to truly factor in?

Maybe, but seeing those numbers, it is very unlikely (although spearman can win against tanks ). Maybe it was changed in 3.13 and the tests are from an older version.

No, they changed it in 3.13 so that spies can't be caught if they are in a ship.

Yes, I remember now. It was in the changelog.

#### watarimono

##### Chieftain
Bhruic, thanks for this information, and the other posters as well.

A spy has no range of detecting other spies, is this correct? The check runs only for the present occupied tile?

Since the Security Bureau is in the city, it only checks for enemy spies currently on the city tile, not also the city's cultural borders?

I wonder if it would break game balance to have a decreasing effect range for the Security Bureau -- limited to the FC perhaps.

#### pumpkin

##### Chieftain
I've been toying with the idea of including multiple spies in my SoDs to replace accuracy seige weapons...

I don't understand, what would you use the spies for in the above case?

#### Roland Johansen

##### Deity
I don't understand, what would you use the spies for in the above case?

A spy can create a 1 turn revolt in a city and then the city loses the defensive culture bonus for that turn. I don't use it, but if you're not so good with siege units or want to do quick attacks with mounted units, then it can be useful.

#### reknaw

##### the Hopeless
I find lately it can be quite difficult to get XP for seige units, and I rarely have more then 4 accuracy cats, so if I were want a second SoD I'd have to either use more seige units on bombarding, or take extra turns to get to 0%. The spy can do it in one turn, and has the added advantage of preventing whipping and drafting for that turn. Also I have used spies lately to pillage stategic resources that would be too hard to reach with my pillage stack in a timely fashion.

It is a shame that spies can be caught while in the same tile as a SoD, doesn't really make sense to me that they would be able to extract the spy from within an army.

EDIT: Oh yeah and the reason I'd have moer than one in the SoD is because they might fail, and even if they succeed they get moved back to my capital afterwards. So yeah probably not going to work for intercontinental combat.

#### UncleJJ

##### Deity
A spy can create a 1 turn revolt in a city and then the city loses the defensive culture bonus for that turn. I don't use it, but if you're not so good with siege units or want to do quick attacks with mounted units, then it can be useful.

This is the most overpowered use of a spy and EPs in my opinion. It is particularly bad in the late middle ages when a Castle and Walls giving 100% protection can be reduced to nought for a turn for just a few EPs. The alternative use of catapults to bombard the defence will only reduce the defence by 2% per turn per catpult or 4% with accuracy, so that's a long wait or a lot of catapults.

A spy costing 40 hammers and with a 75% chance is a lot better bet than building 8 catapults (costing 50 hammers each plus extra maintenance) and waiting several turns while the defence is reinforced and your stack is counterattacked increasing the WW. The EP points are not even lost if your spy fails so just send 2 or 3 spies into the castle and each has a 75% chance. In my mind it should be lot harder to break down the defences of a Castle and Walls than just the cultural defence of an open city yet as far as I know they're the same.

/Rant over

Great article. Thank you so much Bhruic for delving into the innermost workings and publishing this in such a concise way. Now I understand what to do... basically make lots of EPs as they help both espionage attack and defence and it doesn't matter too much where you target them. The Espionage system just got a lot easier to manage, as you only need worry about the passive effects and then investing enough against a particular civ if you want to use active missions, defence takes care of itself.

If you see a civ spending a lot on espionage you better make some effort to match it or his spies will run riot. The only trouble I have is the total spending number seems to be hidden from the players if the civ is using EPs on missions. What we can see from the glance screen is the ratio of current EPs versus our EPs. If either side has spent a lot of EPs on missions then it is hard to tell what chances of detection will be.

#### LlamaCat

##### Emperor
it would be nice if someone could write a spy summary in English

#### KrikkitTwo

##### Immortal
What Tag makes the Security Bureau "spy like"?

Is it the fact that it give Espionage Defense?

Did a little checking, that is it... if the city has an Espionage defense >0 it acts like a counter spy

Replies
15
Views
610
Replies
0
Views
570
Replies
45
Views
5K
Replies
2
Views
355
Replies
5
Views
279