# Visual scan

This article describes the routine used to detect whether the AI can see the player, and to alert it if this is the case.

Each frame, a probability check depending on lightgem brightness, player distance and the AI's visual acuity is used to detect whether the AI has spotted the player.

The base probability, clampVal is calculated from the light gem value itself. The value should represent the probability to be seen within half a second (so with a clampVal of 0.1, you would be spotted approximately once in 5 seconds). I used a polynomial function for this, since the AI were not sensitive enough with a linear increase. Here is it (for the other math freaks):

clampVal = -0.03 + 0.03 * lgem + 1 * 10^-3 * lgem^2 + 1.3 * 10^-4 * lgem^3 - 1.1 * 10^-5 * lgem^4 + 1.892 * 10^-7 * lgem^5

(The grey curve shows the probability before tweaking.)

The clampVal is scaled with the AI's visual acuity.

Within the clampDistance, the probability stays constant, and it decreases linearly towards zero at the safeDistance. Both distances are scaled with the clampVal, so that you have to be really close to the AI in ambient light to be spotted at all. The maximum values for full brightness are given by the Cvars "tdm_ai_sightmin" (default: 11 meters) and "tdm_ai_sightmax" (default: 60 m).

returnVal is the resulting probability depending on the player distance. With this probability, a random check is performed each frame whether the AI has spotted the player. If this succeeds, the AI has caught a glance of the player and becomes alerted.

The alert increase also depends on lightgem brightness, player distance and acuity and is currently between 4 and 13 units (currently hard coded, but will become a spawnarg soon). This means, that an idle AI should go to observant for a single low alert, but into searching if the player was spotted in bright light.

For debugging, you can use the Cvar

tdm_ai_visdist_show 1