AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Math Equation (https://forums.alliedmods.net/showthread.php?t=275497)

PartialCloning 12-01-2015 10:56

Math Equation
 
I am trying to figure out how I can calculate something based on a few variables.

We have:
1. Max points = 20 points.
2. Minimum points = 1 point.
3. Jumps required to complete challenge and achieve max points = 4 jumps.

Now lets say the player completes the challenge with just 2 jumps. He gets 20 points because that's the maximum he can get. Now lets say he completes the challenge in 8 jumps. He gets 10 points because he completed the challenge with double the required jumps so he gets half the max points. If he completes the challenge in a million jumps, he gets 1 point, that's the minimum he can get.

Here's my question, what if the player completes the challenge in 5 or 6 or ..etc jumps? Is there a math term or equation for what I'm trying to achieve? It might sound basic to some of you but I've never been the brightest when it comes to maths.

jimaway 12-01-2015 12:30

Re: Math Equation
 
earnedpoints = maxjumps / jumpsmade * maxpoints
if (earnedpoints > maxpoints) earnedpoints = maxpoints
if (earnedpoints < 1) earnedpoints = 1

PartialCloning 12-01-2015 12:43

Re: Math Equation
 
Thank you.

Bugsy 12-01-2015 22:12

Re: Math Equation
 
Quote:

Originally Posted by jimaway (Post 2367940)
earnedpoints = maxjumps / jumpsmade * maxpoints
if (earnedpoints > maxpoints) earnedpoints = maxpoints
if (earnedpoints < 1) earnedpoints = 1

This does not work for me, did you test this?

I made an attempt and I think it gives what you want:
PHP Code:

#include <amxmodx>

public plugin_init() 
{
    const 
MinPoints 1;
    const 
MaxPoints 20;
    const 
MaxJumps 4;
    
    new 
earnedPoints;
    
    for ( new 
iJumps iJumps <= 20 iJumps++ )
    {
        
earnedPoints clampfloatround( ( float( ( MaxJumps ) - iJumps ) / MaxJumps ) * floatMaxPoints ) ) , MinPoints MaxPoints );
        
server_print"%d Jumps = %d points" iJumps earnedPoints );
    }


Output
Code:


1 Jumps = 20 points
2 Jumps = 20 points
3 Jumps = 20 points
4 Jumps = 20 points
5 Jumps = 18 points
6 Jumps = 15 points
7 Jumps = 13 points
8 Jumps = 10 points
9 Jumps = 8 points
10 Jumps = 5 points
11 Jumps = 3 points
12 Jumps = 1 points
13 Jumps = 1 points
14 Jumps = 1 points
15 Jumps = 1 points
16 Jumps = 1 points
17 Jumps = 1 points
18 Jumps = 1 points
19 Jumps = 1 points
20 Jumps = 1 points

What jimaway posted:
PHP Code:

#include <amxmodx>

public plugin_init() 
{
    const 
MinPoints 1;
    const 
MaxPoints 20;
    const 
MaxJumps 4;
    
    new 
earnedPoints;
    
    for ( new 
iJumps iJumps <= 20 iJumps++ )
    {
        
earnedPoints clampMaxJumps iJumps MaxPoints MinPoints MaxPoints );
        
server_print"%d Jumps = %d points" iJumps earnedPoints );
    }


Output
Code:

1 Jumps = 20 points
2 Jumps = 20 points
3 Jumps = 20 points
4 Jumps = 20 points
5 Jumps = 1 points
6 Jumps = 1 points
7 Jumps = 1 points
8 Jumps = 1 points
9 Jumps = 1 points
10 Jumps = 1 points
11 Jumps = 1 points
12 Jumps = 1 points
13 Jumps = 1 points
14 Jumps = 1 points
15 Jumps = 1 points
16 Jumps = 1 points
17 Jumps = 1 points
18 Jumps = 1 points
19 Jumps = 1 points
20 Jumps = 1 points


fysiks 12-02-2015 00:25

Re: Math Equation
 
I think Jimaway's formula is correct, however, his code is not.

If 8 jumps give you 1/2 max points (10) then 16 jumps should give you 1/4 max points (5).

PHP Code:

public cmdTest()
{
    new 
iTargetJumps 4
    
new iMaxPoints 20
    
new iMinPoints 1
    
new iPointsEarned

    
for( new iJumps 1iJumps 40iJumps++)
    {
        
iPointsEarned clamp(floatround(float(iTargetJumps iMaxPoints) / float(iJumps)), iMinPointsiMaxPoints)
        
server_print("%d Jumps = %d Points"iJumpsiPointsEarned)
    }


Output:

Code:

1 Jumps = 20 Points
2 Jumps = 20 Points
3 Jumps = 20 Points
4 Jumps = 20 Points
5 Jumps = 16 Points
6 Jumps = 13 Points
7 Jumps = 11 Points
8 Jumps = 10 Points
9 Jumps = 9 Points
10 Jumps = 8 Points
11 Jumps = 7 Points
12 Jumps = 7 Points
13 Jumps = 6 Points
14 Jumps = 6 Points
15 Jumps = 5 Points
16 Jumps = 5 Points
17 Jumps = 5 Points
18 Jumps = 4 Points
19 Jumps = 4 Points
20 Jumps = 4 Points
21 Jumps = 4 Points
22 Jumps = 4 Points
23 Jumps = 3 Points
24 Jumps = 3 Points
25 Jumps = 3 Points
26 Jumps = 3 Points
27 Jumps = 3 Points
28 Jumps = 3 Points
29 Jumps = 3 Points
30 Jumps = 3 Points
31 Jumps = 3 Points
32 Jumps = 3 Points
33 Jumps = 2 Points
34 Jumps = 2 Points
35 Jumps = 2 Points
36 Jumps = 2 Points
37 Jumps = 2 Points
38 Jumps = 2 Points
39 Jumps = 2 Points


PartialCloning 12-02-2015 00:30

Re: Math Equation
 
It works, but you have to turn them into floats like you did in your method. But hey the more methods the better.

Quote:

Originally Posted by ot_207 (Post 1632436)
When you use 50 and 100 simply without dots the compiler uses them as integers.

So basically when you do this
50/100 you will do a integer division and the result will be 0
To fix the float problem you need all the numbers to have dots in order to count as floats.
In your situation the code should be:

(50.0/100.0)*100.0


Bugsy 12-02-2015 06:52

Re: Math Equation
 
Good stuff. Ok so now you have two methods to choose from. One consistently reduces points after MaxJumps (my method), while the other spreads out the reduced points in a long range (fysiks). I don't know what the output from jimaways looks like.

jimaway 12-02-2015 07:11

Re: Math Equation
 
Quote:

Originally Posted by Bugsy (Post 2368120)
Good stuff. Ok so now you have two methods to choose from. One consistently reduces points after MaxJumps (my method), while the other spreads out the reduced points in a long range (fysiks). I don't know what the output from jimaways looks like.

fysiks used the same formula.
no i didn't test my code before posting, but like PartialCloning mentioned you had to use floats


All times are GMT -4. The time now is 18:08.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.