Hitscan

Hitscanning is a method of performing damage calculations for video games. In Wolfenstein 3D, it is used to calculate the damage dealt by the Pistol, Machine Gun, and Chain Gun.
==Damage formula (enemy shooting)==
Hitscanning is unique among damage in Wolfenstein 3D in that not one but two calculations are performed, one to determine if a hit actually occurred, and another for damage dealt.
===Variables===
The following variables are used:
;rand
:Random integer between and including the numbers 0 and 255. Every time this variable is mentioned is a different call to the function, and a different random number, even within formulas.
;dist
:The distance between the player and the enemy. Multiplied by two-thirds if the enemy shooting is Hans Grösse or an SS ("ss are better shots").
;hitchance
:Output of the first formula, used to determine if a hit occurred.
;base damage
:Output of the second formula and input of TakeDamage, will be divided by four if the player is on lowest difficulty level.
===Hit detection===
hitchance =
\begin{cases} 
256-(dist \times 16), & \mbox{if player is walking and can see enemy} \\
256-(dist \times 8), & \mbox{if player is walking and cannot see enemy} \\
160-(dist \times 16), & \mbox{if player is running and can see enemy} \\
160-(dist \times 8), & \mbox{if player is running and cannot see enemy}
\end{cases}
===Damage calculation===
\mbox{base damage} = 
\begin{cases} 
\lfloor rand \div 4\rfloor, & \mbox{if } rand < hitchance \mbox{ and } dist < 2 \\
\lfloor rand \div 8\rfloor, & \mbox{if } rand < hitchance \mbox{ and } 2 < dist < 4 \\
\lfloor rand \div 16\rfloor, & \mbox{if } rand < hitchance \mbox{ and } 4 < dist \\
0, & \mbox{if } rand > hitchance
\end{cases}
Note the floor function around the division—due to the use of bit shift rather than true division, the output is rounded down. This allows the player's health to never stray from being a whole number. The maximum damage (255/4) is therefore 63 rather than 63.75. The minimum is zero, even with a successful hit.
As with all damage to the player, the actual damage received is one-fourth of the base damage if the player is playing at "Can I play, Daddy?" difficulty. Save for godmode, the two are otherwise equal.
===Graph===
==Damage formula (player shooting)==
The formula for the player's shots also has a double calculation, though simpler. The same variables as the previous case are used.
\mbox{base damage} = 
\begin{cases} 
rand \div 4, & \mbox{if } dist < 2 \\
rand \div 6, & \mbox{if } 2 < dist < 4 \\
rand \div 6, & \mbox{if } 4 < dist \mbox{ and } (rand \div 12) > dist \\
0, & \mbox{if } 4 < dist \mbox{ and } (rand \div 12) < dist
\end{cases}
==References==
Category:Wolfenstein 3D weapons