Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 09-09-2012, 11:13 PM
xXziroXx xXziroXx is offline
Master of Puppets
xXziroXx's Avatar
Join Date: May 2004
Location: Sweden
Posts: 5,288
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Send a message via AIM to xXziroXx Send a message via MSN to xXziroXx
Method of sorting players into teams

Didn't really want to put this in the code gallery since it's nothing fancy what so ever, but I figured it may serve some use to someone eventually.

PHP Code:
function onCreated()
{
  
// -- START EDIT
  
temp.objectList    allplayers// List of players to process
  
temp.numberOfTeams 2// Number of teams
  // -- END EDIT
  
  
temp.playerList = new[0];
  
// Generates a new list of objects we can work with
  
for (temp.objecttemp.objectList) {
    
// Let's avoid adding those silly RemoteControls
    
if (temp.object.level.name != "")
      continue;
    
    
temp.playerList.add(temp.object);
  }
  
  
// Amount of players
  
temp.amountOfPlayers temp.playerList.size();
  
// Array creation based on number of teams
  
temp.teams           = new[temp.numberOfTeams];
  
temp.team            0;
  
  
// Do we have enough players to make even teams?
  
if (temp.amountOfPlayers%temp.numberOfTeams)
    
temp.oddManOut true;
  
  
// Let's add players to the teams!
  
while (!temp.teamsGenerated) {
    
// Grab a random player from the list...
    
temp.object      randomString(temp.playerList);
    
temp.objectIndex temp.playerList.index(temp.object);
    
    
// Abort if an object wasn't found anymore
    
if (temp.objectIndex == -1)
      return echo(
"[" this.name "]: Index of object returned -1!");
    
    
// Add the player to the team array
    
temp.teams[temp.team].add(temp.object);
    
// Remove the player from the player list
    
temp.playerList.delete(temp.objectIndex);
    
    echo(
"[" this.name "]: Added '" temp.object "' to team '" temp.team "'!");
    
    
// Based on how many players are left, we may stop now.
    
temp.remainingPlayers temp.playerList.size();
    if (
temp.remainingPlayers == 0)
      break;
    else if (
temp.remainingPlayers == temp.numberOfTeams && temp.oddManOut)
      break;
    
    
// If we do have more players to work with, change the team for the next one.
    
temp.team ++;
    
temp.team %= temp.numberOfTeams;
  }
  
  echo(
"[" this.name "]: All" SPC temp.numberOfTeams SPC "teams sorted evenly!" SPC temp.remainingPlayers SPC "player(s) were left out" SPC (temp.remainingPlayers ":(" ":)"));

All you really need to do is provide a list of players and determine the amount of teams, and it'll do the rest of the work for you.
__________________

"A delayed game is eventually good, but a rushed game is forever bad." - Shigeru Miyamoto

Last edited by xXziroXx; 09-09-2012 at 11:49 PM..
Reply With Quote
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 +2. The time now is 10:17 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.