Register for your free account! | Forgot your password?

You last visited: Today at 23:14

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Random Selection

Discussion on Random Selection within the CO2 Private Server forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
abdeen's Avatar
 
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
Random Selection

Solved Thanks
abdeen is offline  
Old 10/24/2011, 00:11   #2
 
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,380
Random selection to select players...

Take a collection (list, dictionary, hashtable doesn't matter) of all eligible players and then just do a random from 0-collection length and then select (and remove) member from that index in the collection.


Example...

List<Player> players = new List<Player>();
foreach(Player p in Kernel.Clients.Values)
players.Add(p);

//You now have a list of all players. Most cases youd be checking maps and stuff but w/e!

Random rand = new Random();

//You want to select two players... I'm not sure how you want to do this either a like StartEvent(PlayerOne, PlayerTwo) or w/e but for now I'll just pull two player objects and store them

Player pOne, pTwo;

int selectedIndex = rand.Next(players.Length);
pOne = players[selectedIndex;
players.RemoveAt(selectedIndex);//this way we can never select same char twice
selectedIndex = rand.Next(players.Length);
pTwo = players[selectedIndex];
players.RemoveAt(selectedIndex);


And now you can do w/e you like with the two pulled player objects
pro4never is offline  
Thanks
2 Users
Old 10/24/2011, 00:32   #3
 
abdeen's Avatar
 
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
Solved Thanks
abdeen is offline  
Old 10/24/2011, 01:40   #4
 
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,380
It was an example...

Kernel.Clients was an example of where your source keeps track of all online players.

I forgot list uses count instead of length. Same thing though. It was example code, not something you can expect to just paste into your source and have it work.
pro4never is offline  
Thanks
1 User
Old 10/24/2011, 02:04   #5
 
abdeen's Avatar
 
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
Quote:
Originally Posted by pro4never View Post
It was an example...

Kernel.Clients was an example of where your source keeps track of all online players.

I forgot list uses count instead of length. Same thing though. It was example code, not something you can expect to just paste into your source and have it work.
so what i have to use instead of Player ? or do i need to create new class for it ?
i used GamePool instead of Client and worked
i used Entity instead of player " Player Not Players " and now i want to know what the mean of W/e and i wrote after

players.RemoveAt(selectedIndex);

client.Entity.Teleport(ID, X, Y);
but says : the name " client " dose not exist in the current context .

so what or how i can modify it to send the selected players to the map which i want ?


abdeen is offline  
Old 10/24/2011, 06:09   #6
 
thesamuraivega's Avatar
 
elite*gold: 0
Join Date: Oct 2009
Posts: 125
Received Thanks: 21
Hi,
My Random Selection my way to select 1 players from online players and send to terrorist state.

Code:
public static void RandomSelect()
        {
            List<Client.GameState> players = new List<Client.GameState>();
            foreach (Client.GameState p in ServerBase.Kernel.Clients.Values)
                players.Add(p);
            Random rand = new Random();
            Client.GameState pOne;
            int selectedIndex = rand.Next(players.[COLOR="Red"]Length[/COLOR]);
            pOne = players[selectedIndex];
            players.RemoveAt(selectedIndex);//this way we can never select same char twice
                pOne.Entity.AddFlag(Network.GamePackets.Update.Flags.Flashy);
                pOne.Entity.KillTheTerrorist_IsTerrorist = true;
                Conquer_Online_Server.Network.PacketHandler.WorldMessage("Ahora " + pOne.Entity.Name + " Es el terrorista!!!");
        }
int selectedIndex = rand.Next(players.Length);´

why?
thesamuraivega is offline  
Thanks
1 User
Old 10/24/2011, 07:11   #7
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Quote:
Originally Posted by thesamuraivega View Post
Hi,
My Random Selection my way to select 1 players from online players and send to terrorist state.

Code:
public static void RandomSelect()
        {
            List<Client.GameState> players = new List<Client.GameState>();
            foreach (Client.GameState p in ServerBase.Kernel.Clients.Values)
                players.Add(p);
            Random rand = new Random();
            Client.GameState pOne;
            int selectedIndex = rand.Next(players.[COLOR="Red"]Length[/COLOR]);
            pOne = players[selectedIndex];
            players.RemoveAt(selectedIndex);//this way we can never select same char twice
                pOne.Entity.AddFlag(Network.GamePackets.Update.Flags.Flashy);
                pOne.Entity.KillTheTerrorist_IsTerrorist = true;
                Conquer_Online_Server.Network.PacketHandler.WorldMessage("Ahora " + pOne.Entity.Name + " Es el terrorista!!!");
        }
int selectedIndex = rand.Next(players.Length);´

why?
players.Count.
BaussHacker is offline  
Thanks
1 User
Old 10/24/2011, 07:16   #8
 
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,380
Quote:
Originally Posted by thesamuraivega View Post
Hi,
My Random Selection my way to select 1 players from online players and send to terrorist state.

Code:
public static void RandomSelect()
        {
            List<Client.GameState> players = new List<Client.GameState>();
            foreach (Client.GameState p in ServerBase.Kernel.Clients.Values)
                players.Add(p);
            Random rand = new Random();
            Client.GameState pOne;
            int selectedIndex = rand.Next(players.[COLOR="Red"]Length[/COLOR]);
            pOne = players[selectedIndex];
            players.RemoveAt(selectedIndex);//this way we can never select same char twice
                pOne.Entity.AddFlag(Network.GamePackets.Update.Flags.Flashy);
                pOne.Entity.KillTheTerrorist_IsTerrorist = true;
                Conquer_Online_Server.Network.PacketHandler.WorldMessage("Ahora " + pOne.Entity.Name + " Es el terrorista!!!");
        }
int selectedIndex = rand.Next(players.Length);´

why?
That selects a random index for your selection.


Think of it like this... I have a deck of cards so I take a random number between 0 and the number of cards in the deck. I then take the Xth card in the deck.

That's what the code is doing exactly. I use an index based method and save the index simply because it makes pulling that user from the collection and then REMOVING him from the collection easier.

This is not needed if you're only taking 1 person though as you'd never be able to select the same person twice. This more has to do with taking multiple entries from small collections.

Lets say I have 4 players on a map and I need to at random pick two of them to fight.

There's a 1/4 chance for each person to be picked... but after I pick someone there's still a 1/4 chance of picking them AGAIN if I don't remove them from the possibilities.... which I do in the example I posted via RemoveAt


<edit>

oops sorry I was thinking you were asking the theory behind the code... not just why it was an 'error' (nothing in the code I posted is intended for use. It's an example rofl!)
pro4never is offline  
Thanks
1 User
Old 10/24/2011, 07:19   #9
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Quote:
Originally Posted by pro4never View Post
<edit>

oops sorry I was thinking you were asking the theory behind the code... not just why it was an 'error' (nothing in the code I posted is intended for use. It's an example rofl!)
Copy-paste. That's how people code these days.
BaussHacker is offline  
Thanks
3 Users
Old 10/24/2011, 17:18   #10
 
abdeen's Avatar
 
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
Solved Thanks
abdeen is offline  
Old 10/24/2011, 17:24   #11


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Please dont claim to have written something when you clearly didnt, it throws off any information that people present to you because they believe you have knowledge of the subject when in actual fact you know nothing about it >.<.
Korvacs is offline  
Thanks
1 User
Old 10/24/2011, 17:55   #12
 
abdeen's Avatar
 
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
Solved Thanks
abdeen is offline  
Old 10/24/2011, 18:03   #13


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Then how is it that Pro4never posted an example and you just tried to copy paste it into your source and then couldnt fix simple issues with it >.<!

And are you serious with that execute void??
Korvacs is offline  
Old 10/24/2011, 18:07   #14
 
abdeen's Avatar
 
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
Solved Thanks
abdeen is offline  
Reply


Similar Threads Similar Threads
Car Selection
10/08/2011 - Need for Speed World - 2 Replies
can some one tell me the best nfs car? it must be super fast and good with Curves
Pvp selection
09/24/2008 - Kal Online - 1 Replies
Maybe some ppl noticed (on int) that some guilds was selected so many times that it's almost impossible (aka: the propability of such event with REAL random script is extremly low) Is it possible to modify the selection? No doubt i dont wanna know how is it workin, just if its possible or not. Ty



All times are GMT +1. The time now is 23:14.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.