Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #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
  #2  
Old 09-10-2012, 12:23 AM
adam adam is offline
http://wiki.graal.us/
adam's Avatar
Join Date: Nov 2001
Posts: 2,247
adam has a spectacular aura about
Send a message via AIM to adam
I was an essential part of testing this script!
Quote:
Weapon/GUI-script Public/xXziroXx/TeamRandomizer added/updated by xXziroXx
[Public/xXziroXx/TeamRandomizer]: Added 'xXziroXx' to team '0'!
[Public/xXziroXx/TeamRandomizer]: Added 'Graal771034' to team '1'!
[Public/xXziroXx/TeamRandomizer]: Added 'MMM_P2P' to team '0'!
[Public/xXziroXx/TeamRandomizer]: Added 'adam' to team '1'!
[Public/xXziroXx/TeamRandomizer]: All 2 teams sorted evenly! 0 player(s) were left out
__________________
Rogue Shadow (TCN)(NAT)(Global Development Team)

For development help, contact the patrons of the #graaldt irc channel below, I am usually there.
Click Here to Join IRC Chat Now! -- irc.freenode.net Channel: #graaldt
Quote:
<Dustyshouri> no, RogueShadow is always talking about scripts lol
<Dustyshouri> in fact, he pretty much brought Graal back as a topic single-handedly
Reply With Quote
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 +2. The time now is 09:17 PM.


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