Gaming AI, courage and retaliation This is a bit oblique but maybe some of you will find it fun and it's been wrecking my brain for a while. Here's the background of what I'm trying to achieve, but if you want to skip to the actual maths part of the problem skip to THE MATHS
So I have a computer controlled character in my game, and I want to calculate the probability R that he will retaliate when attacked (ranging from 0 to 100). This probability should be based on two variables, first the AI's remaining health H (the nearer to death, the more likely to run, again ranging from 0 to 100) and also the AI's courage C (which ranges from 0 to 100). At 0 courage, the AI will flee regardless of health. At 100 courage, the AI will retaliate regardless of health. At 50 courage, the chance of retaliation R will equal remaining health (R = H).
I know there are workarounds like writing endless if statements, but I'm after a clean formula that will give me R for all combinations of C and H. So far, I've approached the problem by drawing a line graph plotting R against H for some given value of C, and then modifying the result using C in some way. For example:
THE MATHS
https://i.stack.imgur.com/Y05Om.png
Here is a graph plotting two variables R and H. The relationship between the two variables will be modified by a third variable C, which ranges from 0 to 100. When C = 50, then R = H (the black line). When C = 100, R = 100 (the green line). When C = 75, the blue line will be true, such that a 45degree diagonal line drawn between the black line and the green line will always intersect the blue line at its halfway point (as shown by the red line).
Is there a formula that can calculate R for any given values of H and C?
It would be an extra helpful bonus if the same pattern can be mirrored the other side of the line R =H for values of C less than 50.
 A: You have specified nine important points:
$$\begin{array}{c|c|c}
\text{Courage} (C) & \text{Health} (H) & \text{Probability} (P) \\
\hline
0 & 0 & 0 \\
0 & 50 & 0 \\
0 & 100 & 0 \\
50 & 0 & 0 \\
50 & 50 & 50 \\
50 & 100 & 100 \\
100 & 0 & 100 \\
100 & 50 & 100 \\
100 & 100 & 100 \\
\end{array}$$
Looking at the data, it looks like a second-degree polynomial should work. So, let's fit the data to
$$\begin{aligned}
P(C, H) &= \sum_{i=0}^2 \sum_{j=0}^2 x_{i,j} C^i H^j \\
\;      &= x_{22} C^2 H^2 + x_{21} C^2 H + x_{20} C^2 + x_{12} C H^2 + x_{11} C H + x_{10} C + x_{02} H^2 + x_{01} H + x_{00}\\
\end{aligned}$$
Supplying the above polynomial and the nine known points to Sagemath solve(), we find a solution:
$$P(C, H) = -\frac{C^2 H}{2500} + \frac{C^2}{50} + \frac{C H}{25} - C = \frac{C \left ( 100 H - 2500 + C ( 50 - H ) \right )}{2500}$$

Do note that this function reaches $P(75, 100) = 112.5$ and $P(25, 0) = -12.5$, so you might wish to clamp the result to the the range 0 to 100.
You could implement this (in pseudocode) as
Function ProbabilityOfRetaliation(Courage, Health):
    Let  P = C * (100*H - 2500 + C*(50 - H)) / 2500
    If P < 0:
        Return 0
    Else If P <= 100:
        Return P
    Else:
        Return 100
    End If
End Function

Also note that the probability function is always linear in health (except for clipping, where the probability is clamped to range $[0, 100]$).
