**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)Edit

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.^{[1]}

### VariablesEdit

The following variables are used:

*rand*- Random integer between and including the numbers 0 and 255.
^{[2]}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"
^{[3]}). *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.
^{[4]}

### Hit detectionEdit

$ 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 calculationEdit

$ \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.^{[4]} Save for godmode, the two are otherwise equal.

### GraphEdit

## Damage formula (player shooting)Edit

The formula for the player's shots also has a double calculation, though simpler. The same variables as the previous case are used.^{[5]}

$ \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} $

## ReferencesEdit

- ↑ WOLFSRC/WL_ACT2.C from the
*Wolfenstein 3D*source code on github.com, line 3444 - ↑ WOLFSRC/ID_US_A.ASM from the
*Wolfenstein 3D*source code on github.com - ↑ WOLFSRC/WL_ACT2.C from the
*Wolfenstein 3D*source code on github.com, line 3461 - ↑
^{4.0}^{4.1}WOLFSRC/WL_AGENT.C from the*Wolfenstein 3D*source code on github.com, line 392 - ↑ WOLFSRC/WL_AGENT.C from the
*Wolfenstein 3D*source code on github.com, line 1168