Raised This Month: $ Target: $400
 0% 

[code] infinity loop


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 12-28-2015 , 14:58   Re: [code] infinity loop
Reply With Quote #1

Quote:
Originally Posted by JusTGo View Post
when all players selected it will show 0 and then you need to call it again to show a random number can you help making never shows 0.
Done.

Code:
public display_random_reset( id ) {     new random = GetRandomPlayer()     if( random == 0 )     {         ResetSelected()         random = GetRandomPlayer()     }     server_print( "Random ID = %d ~ g_Min = %d ~ g_Max = %d", random, g_Min, g_Max ); }
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 12-29-2015 at 02:13. Reason: fixing re-selection bug
addons_zz is offline
JusTGo
Veteran Member
Join Date: Mar 2013
Old 12-29-2015 , 07:39   Re: [code] infinity loop
Reply With Quote #2

Quote:
Originally Posted by addons_zz View Post
Done.

Code:
public display_random_reset( id ) {     new random = GetRandomPlayer()     if( random == 0 )     {         ResetSelected()         random = GetRandomPlayer()     }     server_print( "Random ID = %d ~ g_Min = %d ~ g_Max = %d", random, g_Min, g_Max ); }
ty again i edited it a bit cause when no players connected it will print 0 is this right ?

Code:
public display_random_reset( id ) {     new random = GetRandomPlayer()     // all players have been selected clearing selection and picking a new random     if( random == -1 )     {         ResetSelected()         random = GetRandomPlayer()         if(random)             server_print( "Random ID = %d ~ g_Min = %d ~ g_Max = %d", random, g_Min, g_Max );     }     else if (random == 0)     {     server_print( "No players Found" );         ResetSelected() // no players in server or met the rules of selection     }     else     {         server_print( "Random ID = %d ~ g_Min = %d ~ g_Max = %d", random, g_Min, g_Max );     } }


Code:
GetRandomPlayer() {     //No players are connected or all have been selected already     if( !g_Players         || ( g_Players == g_Selected ) )     {         return 0; // no players to select     }         new iRandomID;         //Keep looping while there is players that haven't yet been selected.     while( g_Players != g_Selected )     {         //Get random player id         iRandomID = random_num( g_Min, g_Max );                 //If player is connected and has not yet been selected         if( ( g_Players & ( 1 << iRandomID ) )             && !( g_Selected & ( 1 << iRandomID ) ) )         {             //Set bit for this ID in g_Selected bit-field so the player will now be considered selected             g_Selected |= ( 1 << iRandomID );             return ( iRandomID + 1 );         }     }         //All players have already been selected.     return -1; // making this -1 so we now we will know when all players are selected }

edit:
is this the right way of making to pick from specific team ?
Code:
GetMaxMin( &iMin, &iMax ) {     for( new i = 0; i < g_MaxPlayers; i++ )     {         if( g_Players & ( 1 << i ) && is_user_alive(i) && cs_get_user_team(i) == CS_TEAM_CT )         {             if( i < iMin )             {                 iMin = i;             }                         if( i > iMax )             {                 iMax = i;             }         }     } }
__________________

Last edited by JusTGo; 12-29-2015 at 07:46.
JusTGo is offline
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 12-29-2015 , 17:02   Re: [code] infinity loop
Reply With Quote #3

Quote:
Originally Posted by JusTGo View Post
ty again i edited it a bit cause when no players connected it will print 0 is this right ?
Yes. Nice work.

Quote:
Originally Posted by JusTGo View Post
is this the right way of making to pick from specific team ?
Code:
GetMaxMin( &iMin, &iMax ) {     for( new i = 0; i < g_MaxPlayers; i++ )     {         if( g_Players & ( 1 << i ) && is_user_alive(i) && cs_get_user_team(i) == CS_TEAM_CT )         {             if( i < iMin )             {                 iMin = i;             }                         if( i > iMax )             {                 iMax = i;             }         }     } }
Do not mix the min and max ids with player team. Why do you checked if the player was alive? Dead players do not have team?
If the player was not connect, he would not be selected because every time he disconnect/connect he is added/removed from the selection.

The better point I think to do this is here:
Code:
GetRandomPlayer() {     //No players are connected or all have been selected already     if( !g_Players         || ( g_Players == g_Selected ) )     {         return 0;     }         new iRandomID;         //Keep looping while there is players that haven't yet been selected.     while( g_Players != g_Selected )     {         //Get random player id         iRandomID = random_num( g_Min, g_Max );                 //If player is connected and has not yet been selected         if( ( g_Players & ( 1 << iRandomID ) )             && !( g_Selected & ( 1 << iRandomID ) ) )         {             //Set bit for this ID in g_Selected bit-field so the player will now be considered selected             g_Selected |= ( 1 << iRandomID );                         if( cs_get_user_team( iRandomID + 1 ) == CS_TEAM_CT )             {                 return ( iRandomID + 1 );             }             else             {                 return GetRandomPlayer()             }         }     }         //All players have already been selected.     return -1; // making this -1 so we now we will know when all players are selected }
I attached the full code.
Attached Files
File Type: sma Get Plugin or Get Source (get_random_player.sma - 563 views - 3.5 KB)
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 12-29-2015 at 17:06. Reason: better info
addons_zz is offline
JusTGo
Veteran Member
Join Date: Mar 2013
Old 12-30-2015 , 06:03   Re: [code] infinity loop
Reply With Quote #4

Quote:
Originally Posted by addons_zz View Post
Yes. Nice work.


Do not mix the min and max ids with player team. Why do you checked if the player was alive? Dead players do not have team?
If the player was not connect, he would not be selected because every time he disconnect/connect he is added/removed from the selection.



I attached the full code.
ty again, i need the played to be alive && ct same time cause he can be in ct team and his dead so i used:
Code:
GetRandomPlayer() {     //No players(alive && ct) or all have been selected already     if( !g_Players || !GetAliveCt()         /*|| ( g_Players == g_Selected )*/ ) // so it will not return 0 when all players selected selected.     {         return 0;     }         new iRandomID;         //Keep looping while there is players that haven't yet been selected.     while( g_Players != g_Selected )     {         //Get random player id         iRandomID = random_num( g_Min, g_Max );                 //If player is connected and has not yet been selected         if( ( g_Players & ( 1 << iRandomID ) )             && !( g_Selected & ( 1 << iRandomID ) ) )         {             //Set bit for this ID in g_Selected bit-field so the player will now be considered selected             g_Selected |= ( 1 << iRandomID );                         if( is_user_alive( iRandomID + 1 ) && cs_get_user_team( iRandomID + 1 ) == CS_TEAM_CT )             {                 return ( iRandomID + 1 );             }             else             {                 return GetRandomPlayer()             }         }     }         //All players have already been selected.     /*if( g_Players == g_Selected )*/ // no need for this check the while loop will already do that for us.     return -1; // making this -1 so we now we will know when all players are selected } GetAliveCt() {     new iCt, id         for (id = 1; id <= g_MaxPlayers; id++)     {         if (is_user_alive( id ) && cs_get_user_team( id ) == CS_TEAM_CT)             iCt++     }         return iCt; }

its working perfect now but just some small issuses like for ex:
you call radom 3 times it gives you: 7 3 1 the you call it another time it gives your 7 3 1 its rare but happens or other thing for ex you get: 7 3 1 next time you do random you get 1 7 3 so you have the number 1 selected twice in a row but i guess thats how random_num() works.

edit:
https://forums.alliedmods.net/showpo...91&postcount=7
Code:
GetMaxMin( &iMin, &iMax ) {     for( new i = 0; i < g_MaxPlayers; i++ )     {         if( g_Players & ( 1 << i ) )         {             if( i < iMin )             {                 iMin = i;             }                         if( i > iMax )             {                 iMax = i;             }         }     } }

is this okay ?
__________________

Last edited by JusTGo; 12-30-2015 at 06:05.
JusTGo is offline
Old 12-30-2015, 07:39
addons_zz
This message has been deleted by addons_zz. Reason: fix erros
Reply



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:57.


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