Yeah, I wasn't expecting it to be a 100% fool-proof solution, only a stepping stone.Nash wrote:There has to be a single expression I can use for this, instead of doing it like how PDF suggested because his way is just too messy and requires a lot of questionable steps...
[math] Formula for scaling numbers?
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
- Project Shadowcat
- Posts: 9369
- Joined: Thu Jul 14, 2005 8:33 pm
- Operating System Version (Optional): Windows 10
- Graphics Processor: nVidia with Vulkan support
- Location: Blacksburg, SC USA
- Contact:
Re: [math] Formula for scaling numbers?
Re: [math] Formula for scaling numbers?
The equation HotWax gave way back when works fine, which just a minor modification to account for the non-zero based offset.
For example:
Code: Select all
NewVal = NewMax * (OldVal - OldMin) / (OldMax - OldMin)
So, let's take this example. Say the user has 400 exp total. The min of the range is 350 and the max is 800. You want the value between 0 - 100 that the player is at, right? So...xample: I want to scale [350 - 800] to [0 - 100].
Code: Select all
NewVal = 100 * (400 - 350) / (800 - 350)
NewVal = 100 * 50/450
NewVal = 100 * 0.1111111...
NewVal = 11.111111...
NewVal = 11 // If rounding
Re: [math] Formula for scaling numbers?
Thanks Zippy. I appreciate your help. However there's one small problem... the script bombs out with a divide by zero error when the player is at level 1, because he has 0 experience. How do I get around this?
Here's the expression I use (printed first for testing purposes... once I get the expression right, I will know what to do with the result).
Player's current experience is recorded through an inventory class, "Experience".
player_expmin[] records what the current level's minimum experience is.
player_expnext[] records the experience required for the next level - the maximum.
EDIT:
Turns out I was calculating player_expmin[] incorrectly.
Thanks so much Zippy. I would have never been able to figure it out on my own even if I read this thread a million times.
Now to get some sleep and hopefully not show up late for work tomorrow... @_@
Here's the expression I use (printed first for testing purposes... once I get the expression right, I will know what to do with the result).
Code: Select all
print(d: 100 * (CheckInventory("Experience") - player_expmin[PlayerNumber()]) / (player_expnext[PlayerNumber()] - player_expmin[PlayerNumber()]));
player_expmin[] records what the current level's minimum experience is.
player_expnext[] records the experience required for the next level - the maximum.
EDIT:
Turns out I was calculating player_expmin[] incorrectly.
Thanks so much Zippy. I would have never been able to figure it out on my own even if I read this thread a million times.
Now to get some sleep and hopefully not show up late for work tomorrow... @_@
- Unknown_Assassin
- Posts: 2468
- Joined: Wed Apr 12, 2006 5:17 pm
- Location: Where dead carcasses lie
- Contact:
Re: [math] Formula for scaling numbers?
You could make the player have 1 experience at level 1. That way, you won't have the divide by zero error.Nash wrote:the script bombs out with a divide by zero error when the player is at level 1, because he has 0 experience. How do I get around this?
Re: [math] Formula for scaling numbers?
No, UA, that wasn't the problem. I was just calculcating the player's min exp incorrectly, which just screws up the math formula Zippy provided.
(I found out because I did try making the player start with 1 exp but the script still bombed out)
(I found out because I did try making the player start with 1 exp but the script still bombed out)