Raised This Month: $ Target: $400
 0% 

I need "for loop" algorithm


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CreativeTR
Junior Member
Join Date: Mar 2017
Location: Turkey
Old 03-12-2017 , 13:16   I need "for loop" algorithm
Reply With Quote #1

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

Last edited by CreativeTR; 03-12-2017 at 13:45.
CreativeTR is offline
Send a message via Skype™ to CreativeTR
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 03-12-2017 , 13:27   Re: I need "for loop" algorithm
Reply With Quote #2

Code:
for(new i = 1; i <= MAX_LEVEL; i++) {     p_Level[i++]     continue; }

This?
__________________

Last edited by edon1337; 03-12-2017 at 13:28.
edon1337 is offline
CreativeTR
Junior Member
Join Date: Mar 2017
Location: Turkey
Old 03-12-2017 , 13:40   Re: I need "for loop" algorithm
Reply With Quote #3

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;
                        }
 }

Last edited by CreativeTR; 03-12-2017 at 13:42.
CreativeTR is offline
Send a message via Skype™ to CreativeTR
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 03-12-2017 , 14:07   Re: I need "for loop" algorithm
Reply With Quote #4

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.
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 03-12-2017 at 14:25.
EFFx is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-12-2017 , 14:18   Re: I need "for loop" algorithm
Reply With Quote #5

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

This?
Quote:
Originally Posted by CreativeTR View Post
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.
__________________

Last edited by fysiks; 03-12-2017 at 14:18.
fysiks is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-12-2017 , 14:40   Re: I need "for loop" algorithm
Reply With Quote #6

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)
__________________
fysiks is offline
CreativeTR
Junior Member
Join Date: Mar 2017
Location: Turkey
Old 03-12-2017 , 14:53   Re: I need "for loop" algorithm
Reply With Quote #7

Thanks it was very simple
CreativeTR is offline
Send a message via Skype™ to CreativeTR
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 03-12-2017 , 15:00   Re: I need "for loop" algorithm
Reply With Quote #8

fysiks, what about my reply, is it what he asked for?
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo
EFFx is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-12-2017 , 15:37   Re: I need "for loop" algorithm
Reply With Quote #9

Quote:
Originally Posted by EFFx View Post
fysiks, what about my reply, is it what he asked for?
No.
__________________
fysiks is offline
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 03-13-2017 , 05:59   Re: I need "for loop" algorithm
Reply With Quote #10

Level can be found using binary search.
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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