AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   I need "for loop" algorithm (https://forums.alliedmods.net/showthread.php?t=294973)

CreativeTR 03-12-2017 13:16

I need "for loop" algorithm
 
HTML Code:

#define ADD_EXP_PER_KILL 30
#define MAX_LEVEL 30

Operation:

Required experience for Level2: 30
Required experience for Level3: 90 (Previous level required exp + 30 x Previous Level)
Required experience for Level4: 180 (Previous level required exp + 30 x Previous Level)
Required experience for Level5: 300 (Previous level required exp + ADD_EXP_PER_KILL x Previous Level)

so

Required kill for level2: 1
Required kill for level3: 1+2 = total 3
Required kill for level4: 1+2+3 = total 6
Required kill for level5: 1+2+3+4 = total 10

HTML Code:

        if(currentExp >= 30 && currentExp < 90) {
                p_Level[id] = 2
        } else if(currentExp >
= 90 && currentExp < 180) {
                p_Level[id] = 3
        } else if(currentExp >
= 180 && currentExp < 300) {
                p_Level[id] = 4
        } else if(currentExp >
= 300 && currentExp < 450) {
                p_Level[id] = 5
        } else if(currentExp >
= 450 && currentExp < 630) {
                p_Level[id] = 6
        } else if(currentExp >
= 630 && currentExp < 840) {
                p_Level[id] = 7
        } else if(currentExp >
= 840 && currentExp < 1080) {
                p_Level[id] = 8
        } else if(currentExp >
= 1080 && currentExp < 1350) {
                p_Level[id] = 9
        } else if(currentExp >
= 1350 && currentExp < 1650) {
                p_Level[id] = 10
        } else if(currentExp >
= 1650 && currentExp < 1980) {
                p_Level[id] = 11
        } else if(currentExp >
= 1980 && currentExp < 2340) {
                p_Level[id] = 12
        } else if(currentExp >
= 2340 && currentExp < 2730) {
                p_Level[id] = 13
        } else if(currentExp >
= 2730 && currentExp < 3150) {
                p_Level[id] = 14
        } else if(currentExp >
= 3150 && currentExp < 3600) {
                p_Level[id] = 15
        } else if(currentExp >
= 3600 && currentExp < 4080) {
                p_Level[id] = 16
        } else if(currentExp >
= 4080 && currentExp < 4590) {
                p_Level[id] = 17
        } else if(currentExp >
= 4590 && currentExp < 5130) {
                p_Level[id] = 18
        } else if(currentExp >
= 5130 && currentExp < 5700) {
                p_Level[id] = 19
        } else if(currentExp >
= 5700 && currentExp < 6300) {
                p_Level[id] = 20
        } else if(currentExp >
= 6300 && currentExp < 6930) {
                p_Level[id] = 21
        } else if(currentExp >
= 6930 && currentExp < 7590) {
                p_Level[id] = 22
        } else if(currentExp >
= 7590 && currentExp < 8280) {
                p_Level[id] = 23
        } else if(currentExp >
= 8280 && currentExp < 9000) {
                p_Level[id] = 24
        } else if(currentExp >
= 9000 && currentExp < 9750) {
                p_Level[id] = 25
        } else if(currentExp >
= 9750 && currentExp < 10530) {
                p_Level[id] = 26
        } else if(currentExp >
= 10530 && currentExp < 11340) {
                p_Level[id] = 27
        } else if(currentExp >
= 11340 && currentExp < 12180) {
                p_Level[id] = 28
        } else if(currentExp >
= 12180 && currentExp < 13050) {
                p_Level[id] = 29
        } else if(currentExp >
= 13050) {
                p_Level[id] = 30
        }

Must be:

HTML Code:

#define MAX_LEVEL 30

public levelControl(id) {
        for(new i = 1; i <= MAX_LEVEL; i++) {
                        //algorithm
                        p_Level[i]
                        break;
        }
}

Help me, i couldn't calculate its algorithm

edon1337 03-12-2017 13:27

Re: I need "for loop" algorithm
 
Code:
for(new i = 1; i <= MAX_LEVEL; i++) {     p_Level[i++]     continue; }

This?

CreativeTR 03-12-2017 13:40

Re: I need "for loop" algorithm
 
of course not

example:

HTML Code:

for(new i = 1; i <= MAX_LEVEL; i++) {
                        if(currentExp >
= (currentExp*i) && currentExp <= (currentExp*i) + (currentExp*i)) {
                                p_Level[i]
                                break;
                        }
 }


EFFx 03-12-2017 14:07

Re: I need "for loop" algorithm
 
PHP Code:

enum LevelSettings
{
    
XPName[15],
    
XPRequired


PHP Code:

new const LEVELS[][LevelSettings]
{
    { 
"newbie"180 },
    { 
"veteran"300 },
    { 
"pro"450 }
}

new 
UserLevel[33]
new 
UserXP[33

PHP Code:

UserXP[id] += ADD_EXP_PER_KILL
if(UserXP[id] >= LEVELS[UserLevel[id]][XPRequired])
{
    
UserLevel[id] += 1


PHP Code:

client_print(idprint_chat"Your level: %s"LEVELS[UserLevel[id]][XPName]) 

Something similar at this.

fysiks 03-12-2017 14:18

Re: I need "for loop" algorithm
 
Quote:

Originally Posted by edon1337 (Post 2503083)
Code:
for(new i = 1; i <= MAX_LEVEL; i++) {     p_Level[i++]     continue; }

This?

Quote:

Originally Posted by CreativeTR (Post 2503092)
of course not

example:

HTML Code:

for(new i = 1; i <= MAX_LEVEL; i++) {
                        if(currentExp >
= (currentExp*i) && currentExp <= (currentExp*i) + (currentExp*i)) {
                                p_Level[i]
                                break;
                        }
 }


Neither of those make any sense. They don't actually do anything.

This function will give you the required XP for the supplied level:

Code:

stock getLevelXP(level)
{
        return level > 0 ? getLevelXP(level - 1) + ADD_EXP_PER_KILL * (level - 1) : 0
}

And you can use it like this:
Code:

PlayerLevel = XP > getLevelXP(PlayerLevel + 1) ? PlayerLevel + 1 : PlayerLevel
Note that it won't automatically detect a gain of more than 1 level between subsequent executions. So, you either need to compensate by calling it often enough that that situation never happens or use a loop to count up from PlayerLevel + 1 until XP > getLevelXP() returns false.

fysiks 03-12-2017 14:40

Re: I need "for loop" algorithm
 
Here is an alternative which does everything in a single function which is a direct replacement of your if elseif code:

Code:

stock getLevel(XP)
{
        new LevelXP = 0
        new iLevel = 0
        while( XP >= LevelXP )
        {
                LevelXP = LevelXP + ADD_EXP_PER_KILL * iLevel
                iLevel++
        }
       
        return iLevel - 1
}

Example:

Code:

p_Level[id] = getLevel(currentExp)

CreativeTR 03-12-2017 14:53

Re: I need "for loop" algorithm
 
Thanks it was very simple :)

EFFx 03-12-2017 15:00

Re: I need "for loop" algorithm
 
fysiks, what about my reply, is it what he asked for?

fysiks 03-12-2017 15:37

Re: I need "for loop" algorithm
 
Quote:

Originally Posted by EFFx (Post 2503138)
fysiks, what about my reply, is it what he asked for?

No.

PRoSToTeM@ 03-13-2017 05:59

Re: I need "for loop" algorithm
 
Level can be found using binary search.


All times are GMT -4. The time now is 17:50.

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