private int ToSquad(String name, int team)
{
int ret = 0;
try {
List<PlayerModel> teamList = null;
if (IsSQDM()) return 1; // SQDM, squad is always 1
teamList = GetTeam(team);
if (teamList == null) return 0;
int[] squads = new int[SQUAD_NAMES.Length];
// Build table of squad counts
int i = 0;
for (i = 0; i < squads.Length; ++i) {
squads[i] = 0;
}
foreach (PlayerModel p in teamList) {
i = p.Squad;
if (i < 0 || i >= squads.Length) continue;
squads[i] = squads[i] + 1;
}
// Find the biggest squad less than fMaxSquadSize (that isn't locked -- TODO)
int squad = 0;
int best = 0;
int atZero = 0;
int highOccupied = 0; // for scrambling time
for (int squadNum = 1; squadNum < squads.Length; ++squadNum) {
int n = squads[squadNum];
if (n == 0) {
if (atZero == 0) atZero = squadNum;
continue;
}
highOccupied = squadNum;
if (n >= fMaxSquadSize) continue;
if (n > best) {
squad = squadNum;
best = n;
}
}
// if no best squad, use empty squad with lowest slot number
if (squad == 0 && atZero != 0) {
ret = atZero;
} else {
// otherwise return the best squad
ret = squad;
}
// While scrambling, find the highest empty squad by three
if (fWhileScrambling) {
if (highOccupied > 0) {
i = highOccupied + 3;
while (i < squads.Length && squads[i] != 0) i = i + 1;
if (i < squads.Length) {
ret = i;
} else {
// Use the existing selected empty squad
ret = atZero;
}
} else {
// We just moved all the players out of squads!
ret = 0;
}
}
if (DebugLevel >= 6) {
String ss = "selected " + ret + " out of ";
for (int k = 1; k < squads.Length; ++k) {
if (squads[k] == 0) continue;
ss = ss + k + ":" + squads[k] + "/";
}
ss = ss + "-";
if (!fWhileScrambling) {
ConsoleDebug("ToSquad ^b" + name + "^n: " + ss);
} else {
ConsoleDebug("While scrambling, ToSquad ^b" + name + "^n: " + ss);
}
}
} catch (Exception e) {
ConsoleException(e);
}
return ret;
}