Maximising Time Spent Running in a Video Game

57 Views Asked by At

Many video games have a running mechanic, often paired with some kind of stamina meter. If you are idle for a while, your stamina meter will be at its maximum possible value. Let's say the value of your stamina is at most $100$. So if we have a stamina function for running, then $$ S(t) = S(t_0) - kt $$ where $S(t)$ measures the amount of stamina remaining after having run for $t$ seconds, $S(t_0)$ is the amount of stamina you possess before running (at time $t_0$), and the $kt$ term (where $k\in\mathbb{R^+}$ is some constant) denotes how much stamina is being depleted for running, which I have safely assumed to be linear. Note that $S(t) \geq 0$ and so we know that there exists $l\in\mathbb{R}^+$ such that $S(l) = 0$, and hence, $t\in [0, l]$.

Similarly, we can create an analogous function for the amount of stamina recovered for not running. That is, $$ S^\ast(t) = S^\ast(t_1) + k't, $$ where $S^\ast(t)$ is the amount of stamina recovered for not running, $S^\ast(t_1)$ is the amount of stamina you have before deciding not to run at time $t_1$ and the $kt'$ term is how much stamina we're gaining back (where $k'\in\mathbb{R}^+$ is some constant). Note that $S^\ast(t) \leq 100$ and so know that there exists $l'\in \mathbb{R}^+$ such that $S(l') = 100$ and hence, $t\in[0,l']$.

As an example, if we suppose that $l = 10$ and $l' = 5$, then we could spend $10$s running, $5$s not running to recover all stamina, and then run for another $10$s. This would mean that for the total $25$s elapsed, we have spent $20$s of that time running, or approximately $83\%$ of the total time elapsed.

Question:

If my goal is to maximise the amount of time spent running in a span of $t$s, how long should I run for and how long should I wait in between to regain stamina? Note that you can only run if $S(t) > 0 $, that is, if there is some remaining amount of stamina.

Some thoughts:

The solution of this question probably hinges on creating some function $H$ which measures the amount of time spent running, given a sequence of running times, and a sequence of non-running times. However, I am completely unsure how to concretely create such a function, or how one would find the maximum of a function depending on two sequences. Another approach could be to construct some kind of algorithm to find a sequence of running times and non-running times given $k,k'$ or $l,l'$. However, I am once again unsure what kind of algorithm could help here.

2

There are 2 best solutions below

0
On

Here's progress towards an "obvious" answer, which is supposed to be the optimal strategy.

First of all it should be clear that, whatever sequence you use, it should be cyclic: a finite number of steps at the end of which your stamina bar goes back to the same point it started from. If it didn't, and it kept going down eventually you'd run out of stamina and have to just walk; conversely you'd be wasting time if it only went up because it would reach the cap.

Here I'm assuming your game character is running on an infinitely large level (possibly flat), so there's no destination to reach.

So let us consider the simplest sequence with only one cycle: we start from a certain amount of stamina $M$, run until we get to $R$, and then go back to $M$ walking. This implies that: $$ R = M - k_d x $$ $$ M = R + k_r y $$ Where $x, y$ are the running and walking times respectively and $k_d, k_r$ the stamina drain/recovery multipliers. Then by summing the two equations together we obtain a solution: $$ x = \frac{k_r}{k_d}y $$ Which relates the proportion of time spent walking with respect to time spent running. Clearly if the stamina regeneration is faster than drain ($k_r>k_d$) you want to spend more time running than walking. The converse situation is just as "obvious". Both situations are covered satisfactorily by this equation.

Now consider the simplest 2-cycle. I will be denoting with a subscript the variables that belong to each cycle. The idea of the cycle is that we run until we drain down to a certain amount $R_1$, walk until we get up to another amount $M_2$ which in principle doesn't need to be the same as the starting amount $M_1$, then we drain until $R_2$ before going back to $M_1$. Then, this cycle is determined by: $$ R_1 = M_1 - k_d x_1 $$ $$ M_2 = R_1 + k_r y_1 $$ $$ R_2 = M_2 - k_d x_2 $$ $$ M_1 = R_2 + k_r y_2 $$ But again if we sum all four equations we obtain: $$(x_1 + x_2) = \frac{k_r}{k_d}(y_1 + y_2)$$ Therefore, the total time spent running has the same proportion as before with the total time spent walking no matter what we do. This solution is independent of $M_{1,2}, R_{1,2}$: the fact that we are looking for a cyclic strategy forces us to have this ratio between running time and walking time.

It's easy to see that this would be the exact same for any $n$-cycle. Since any non-periodic strategy eventually becomes inefficient, I claim that you cannot do better than this and indeed you are forced to by the game: there's no clever strategy to maximise the ratio between time spent walking and time spent running.

Now many games add a "delay" $d$ of time before stamina regeneration starts. In this case clearly you want to wait at least until $d$, so $y > d$, otherwise you will never regain stamina. Then you can substitute in all the equations $y$ with $y-d$ to get your answer.

0
On

With your given assumptions this is very simple. You assumed that both stamina consumed during running and stamina recovered during not running are directly proportional to time. This means for every second you spend running you need to spend a fixed amount of time not running. The only constraints are that your stamina can't go below 0 (the game will probably enforce that automatically) and your stamina can't go above 100, meaning you don't want to not run for too long to avoid wasting stamina recovery.

Any mode of running and walking that never hits these two constraints will result in the exact same proportion of run time to walk time (as long as initial stamina= final stamina for your measured period). So you do not need to worry about some optimal strategy, as long as you don't waste any recovery time, all strategies are equal.