Need Equation For A Game I'm Working On

bgavin

New member
Joined
Jan 21, 2020
Messages
3
Hello! First of all, I'm sorry if this isn't actually Calculus. I'm not the best at higher-level math, so please forgive me if I've put this in the wrong category.

I have a rough idea of what I want. Here's my problem:
The player character ingests a certain substance (we'll pretend it's caffeine to keep it G-rated) which kicks in after 30 minutes. Assume it takes 20 caffeine points (Caff) to feel any effects. 1 cup of coffee is equal to 100 Caff. If the player ingests 1 cup, I want there to be 20 Caff in their system at the 30-minute mark, but if they ingest 10 cups, I don't want there to be 20 Caff in their system at the 3-minute mark, if that makes sense. I don't want them to get 20 Caff before the 30-minute mark, regardless of how much they ingest. Also, I don't want them to get more than 40 Caff within the first hour. The remaining Caff should be released in their system at an increasing rate up to the 240-minute mark (4 hours). After that, the amount in their system should begin to taper off, reaching 0 any time between the 480-2880-minute mark (8-48 hours). The time it reaches 0 depends on how much is in their system at the 240-minute mark. If they have 100 Caff in their system, they should be at 0 at the 480 mark. Assume that the maximum they can have in their system is 10,000 Caff, which should reach 0 at the 2880-minute mark.
 
Hello! First of all, I'm sorry if this isn't actually Calculus. I'm not the best at higher-level math, so please forgive me if I've put this in the wrong category.

I have a rough idea of what I want. Here's my problem:
The player character ingests a certain substance (we'll pretend it's caffeine to keep it G-rated) which kicks in after 30 minutes. Assume it takes 20 caffeine points (Caff) to feel any effects. 1 cup of coffee is equal to 100 Caff. If the player ingests 1 cup, I want there to be 20 Caff in their system at the 30-minute mark, but if they ingest 10 cups, I don't want there to be 20 Caff in their system at the 3-minute mark, if that makes sense. I don't want them to get 20 Caff before the 30-minute mark, regardless of how much they ingest. Also, I don't want them to get more than 40 Caff within the first hour. The remaining Caff should be released in their system at an increasing rate up to the 240-minute mark (4 hours). After that, the amount in their system should begin to taper off, reaching 0 any time between the 480-2880-minute mark (8-48 hours). The time it reaches 0 depends on how much is in their system at the 240-minute mark. If they have 100 Caff in their system, they should be at 0 at the 480 mark. Assume that the maximum they can have in their system is 10,000 Caff, which should reach 0 at the 2880-minute mark.
So where is the problem? - give them coffee - as much as you want!!
 
Sorry, I meant to say that I need a formula that will give me the current Caff in the player's system at any given point in time, using these rules.
 
Welcome to free math help!

If caffeine takes 30 minutes to kick in each time it is consumed, then each "hit" necessarily has two variables associated with it: an amount and a time. While it's certainly possible to produce a function for multivariable data points, it's a bit higher than my pay grade...

If I were implementing this, I wouldn't try to craft a formula out of it: I'd use an object with the amount of caffeine and how long it's been in the system:
C:
struct Hit {
    float amount;
    float lifetime;
}
This lifetime field would be incremented with each interval of game time processed.

Each time a "hit" is consumed, another such object is added to the entity's hits collection. When determining how much the caffeine is affecting the entity's system "right now", consider only those "hits" whose lifetime >= 30, decay over time as desired, and clamp to some maximum value. Once the effective impact of a "hit" falls below some threshold, it can be removed from the collection.
 
Thanks for the reply, Mr. Bland! I might just go that route if no one comes up with an equation. Yours does seem like a sensible solution. The more I think about it, the more I like it. I suppose either way, I'd have to have some sort of object or variable associated with each individual hit. I'll probably give the thread until tomorrow and see what else someone comes up with.
 
I like Mr Bland's suggestion.

Here's an alternative method that might be fun to play around with. You could store the amount of caffeine in both the stomach (s) and blood (b).

Rough algorithm:-
- Caffeine in the blood affects the brain. So if "b>hyperThreshold" then the character is hyper. Coffee in the stomach "s" does not affect the character.
- For every coffee consumed increase "s" by 20
- Once a minute do the following:- transfer some coffee from "s" to "b". The amount to transfer is a constant, c1, so b=b + c1 and s=s - c1 (obviously keep s positive and so only transfer if s>=c1)
- Once a minute the liver will be removing a set amount of coffee from "b", so reduce b by a constant c2 (but only if b is positive).

This method might need some tweaking to find good values for constants c1 and c2 so that the system behaves like you desire. I guess you want 30*(c1-c2)=hyperThreshold. But you could play with the transfer rates, maybe making them bigger if there's a LOT of coffee in s. You could have the liver get less healthy if the character does this too often - just reduce c2 to simulate this!
 
Top