]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/teamplay.qc
Merge branch 'martin-t/snt' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / teamplay.qc
1 #include "teamplay.qh"
2
3 #include "client.qh"
4 #include "race.qh"
5 #include "scores.qh"
6 #include "scores_rules.qh"
7
8 #include "bot/api.qh"
9
10 #include "command/vote.qh"
11
12 #include "mutators/_mod.qh"
13
14 #include "../common/deathtypes/all.qh"
15 #include "../common/gamemodes/_mod.qh"
16 #include "../common/teams.qh"
17
18 void TeamchangeFrags(entity e)
19 {
20         PlayerScore_Clear(e);
21 }
22
23 void LogTeamchange(float player_id, float team_number, float type)
24 {
25         if(!autocvar_sv_eventlog)
26                 return;
27
28         if(player_id < 1)
29                 return;
30
31         GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
32 }
33
34 void default_delayedinit(entity this)
35 {
36         if(!scores_initialized)
37                 ScoreRules_generic();
38 }
39
40 void InitGameplayMode()
41 {
42         VoteReset();
43
44         // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
45         get_mi_min_max(1);
46         // assign reflectively to avoid "assignment to world" warning
47         int done = 0; for (int i = 0, n = numentityfields(); i < n; ++i) {
48             string k = entityfieldname(i); vector v = (k == "mins") ? mi_min : (k == "maxs") ? mi_max : '0 0 0';
49             if (v) {
50             putentityfieldstring(i, world, sprintf("%d %d %d", v));
51             if (++done == 2) break;
52         }
53         }
54         // currently, NetRadiant's limit is 131072 qu for each side
55         // distance from one corner of a 131072qu cube to the opposite corner is approx. 227023 qu
56         // set the distance according to map size but don't go over the limit to avoid issues with float precision
57         // in case somebody makes extremely large maps
58         max_shot_distance = min(230000, vlen(world.maxs - world.mins));
59
60         MapInfo_LoadMapSettings(mapname);
61         GameRules_teams(false);
62
63         if (!cvar_value_issafe(world.fog))
64         {
65                 LOG_INFO("The current map contains a potentially harmful fog setting, ignored");
66                 world.fog = string_null;
67         }
68         if(MapInfo_Map_fog != "")
69                 if(MapInfo_Map_fog == "none")
70                         world.fog = string_null;
71                 else
72                         world.fog = strzone(MapInfo_Map_fog);
73         clientstuff = strzone(MapInfo_Map_clientstuff);
74
75         MapInfo_ClearTemps();
76
77         gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
78
79         cache_mutatormsg = strzone("");
80         cache_lastmutatormsg = strzone("");
81
82         InitializeEntity(NULL, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
83 }
84
85 string GetClientVersionMessage(entity this)
86 {
87         if (CS(this).version_mismatch) {
88                 if(CS(this).version < autocvar_gameversion) {
89                         return strcat("This is Xonotic ", autocvar_g_xonoticversion,
90                                 "\n^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8");
91                 } else {
92                         return strcat("This is Xonotic ", autocvar_g_xonoticversion,
93                                 "\n^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8");
94                 }
95         } else {
96                 return strcat("Welcome to Xonotic ", autocvar_g_xonoticversion);
97         }
98 }
99
100 string getwelcomemessage(entity this)
101 {
102         MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
103         string modifications = M_ARGV(0, string);
104
105         if(g_weaponarena)
106         {
107                 if(g_weaponarena_random)
108                         modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
109                 else
110                         modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
111         }
112         else if(cvar("g_balance_blaster_weaponstartoverride") == 0)
113                 modifications = strcat(modifications, ", No start weapons");
114         if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
115                 modifications = strcat(modifications, ", Low gravity");
116         if(g_weapon_stay && !g_cts)
117                 modifications = strcat(modifications, ", Weapons stay");
118         if(g_jetpack)
119                 modifications = strcat(modifications, ", Jet pack");
120         if(autocvar_g_powerups == 0)
121                 modifications = strcat(modifications, ", No powerups");
122         if(autocvar_g_powerups > 0)
123                 modifications = strcat(modifications, ", Powerups");
124         modifications = substring(modifications, 2, strlen(modifications) - 2);
125
126         string versionmessage = GetClientVersionMessage(this);
127         string s = strcat(versionmessage, "^8\n^8\nmatch type is ^1", gamemode_name, "^8\n");
128
129         if(modifications != "")
130                 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
131
132         if(cache_lastmutatormsg != autocvar_g_mutatormsg)
133         {
134                 if(cache_lastmutatormsg)
135                         strunzone(cache_lastmutatormsg);
136                 if(cache_mutatormsg)
137                         strunzone(cache_mutatormsg);
138                 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
139                 cache_mutatormsg = strzone(cache_lastmutatormsg);
140         }
141
142         if (cache_mutatormsg != "") {
143                 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
144         }
145
146         string mutator_msg = "";
147         MUTATOR_CALLHOOK(BuildGameplayTipsString, mutator_msg);
148         mutator_msg = M_ARGV(0, string);
149
150         s = strcat(s, mutator_msg); // trust that the mutator will do proper formatting
151
152         string motd = autocvar_sv_motd;
153         if (motd != "") {
154                 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
155         }
156         return s;
157 }
158
159 void setcolor(entity this, int clr)
160 {
161 #if 0
162         this.clientcolors = clr;
163         this.team = (clr & 15) + 1;
164 #else
165         builtin_setcolor(this, clr);
166 #endif
167 }
168
169 void SetPlayerColors(entity pl, float _color)
170 {
171         /*string s;
172         s = ftos(cl);
173         stuffcmd(pl, strcat("color ", s, " ", s, "\n")  );
174         pl.team = cl + 1;
175         //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
176         pl.clientcolors = 16*cl + cl;*/
177
178         float pants, shirt;
179         pants = _color & 0x0F;
180         shirt = _color & 0xF0;
181
182
183         if(teamplay) {
184                 setcolor(pl, 16*pants + pants);
185         } else {
186                 setcolor(pl, shirt + pants);
187         }
188 }
189
190 void SetPlayerTeam(entity pl, float t, float s, float noprint)
191 {
192         float _color;
193
194         if(t == 4)
195                 _color = NUM_TEAM_4 - 1;
196         else if(t == 3)
197                 _color = NUM_TEAM_3 - 1;
198         else if(t == 2)
199                 _color = NUM_TEAM_2 - 1;
200         else
201                 _color = NUM_TEAM_1 - 1;
202
203         SetPlayerColors(pl,_color);
204
205         if(t != s) {
206                 LogTeamchange(pl.playerid, pl.team, 3);  // log manual team join
207
208                 if(!noprint)
209                         bprint(playername(pl, false), "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
210         }
211
212 }
213
214 // set c1...c4 to show what teams are allowed
215 void CheckAllowedTeams (entity for_whom)
216 {
217         int teams_mask = 0;
218
219         c1 = c2 = c3 = c4 = -1;
220         cb1 = cb2 = cb3 = cb4 = 0;
221
222         string teament_name = string_null;
223
224         bool mutator_returnvalue = MUTATOR_CALLHOOK(CheckAllowedTeams, teams_mask, teament_name, for_whom);
225         teams_mask = M_ARGV(0, float);
226         teament_name = M_ARGV(1, string);
227
228         if(!mutator_returnvalue)
229         {
230                 if(teams_mask & BIT(0)) c1 = 0;
231                 if(teams_mask & BIT(1)) c2 = 0;
232                 if(teams_mask & BIT(2)) c3 = 0;
233                 if(teams_mask & BIT(3)) c4 = 0;
234         }
235
236         // find out what teams are allowed if necessary
237         if(teament_name)
238         {
239                 entity head = find(NULL, classname, teament_name);
240                 while(head)
241                 {
242                         switch(head.team)
243                         {
244                                 case NUM_TEAM_1: c1 = 0; break;
245                                 case NUM_TEAM_2: c2 = 0; break;
246                                 case NUM_TEAM_3: c3 = 0; break;
247                                 case NUM_TEAM_4: c4 = 0; break;
248                         }
249
250                         head = find(head, classname, teament_name);
251                 }
252         }
253
254         // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
255         if(AvailableTeams() == 2)
256         if(autocvar_bot_vs_human && for_whom)
257         {
258                 if(autocvar_bot_vs_human > 0)
259                 {
260                         // find last team available
261
262                         if(IS_BOT_CLIENT(for_whom))
263                         {
264                                 if(c4 >= 0) { c3 = c2 = c1 = -1; }
265                                 else if(c3 >= 0) { c4 = c2 = c1 = -1; }
266                                 else { c4 = c3 = c1 = -1; }
267                                 // no further cases, we know at least 2 teams exist
268                         }
269                         else
270                         {
271                                 if(c1 >= 0) { c2 = c3 = c4 = -1; }
272                                 else if(c2 >= 0) { c1 = c3 = c4 = -1; }
273                                 else { c1 = c2 = c4 = -1; }
274                                 // no further cases, bots have one of the teams
275                         }
276                 }
277                 else
278                 {
279                         // find first team available
280
281                         if(IS_BOT_CLIENT(for_whom))
282                         {
283                                 if(c1 >= 0) { c2 = c3 = c4 = -1; }
284                                 else if(c2 >= 0) { c1 = c3 = c4 = -1; }
285                                 else { c1 = c2 = c4 = -1; }
286                                 // no further cases, we know at least 2 teams exist
287                         }
288                         else
289                         {
290                                 if(c4 >= 0) { c3 = c2 = c1 = -1; }
291                                 else if(c3 >= 0) { c4 = c2 = c1 = -1; }
292                                 else { c4 = c3 = c1 = -1; }
293                                 // no further cases, bots have one of the teams
294                         }
295                 }
296         }
297
298         if(!for_whom)
299                 return;
300
301         // if player has a forced team, ONLY allow that one
302         if(for_whom.team_forced == NUM_TEAM_1 && c1 >= 0)
303                 c2 = c3 = c4 = -1;
304         else if(for_whom.team_forced == NUM_TEAM_2 && c2 >= 0)
305                 c1 = c3 = c4 = -1;
306         else if(for_whom.team_forced == NUM_TEAM_3 && c3 >= 0)
307                 c1 = c2 = c4 = -1;
308         else if(for_whom.team_forced == NUM_TEAM_4 && c4 >= 0)
309                 c1 = c2 = c3 = -1;
310 }
311
312 float PlayerValue(entity p)
313 {
314         return 1;
315         // FIXME: it always returns 1...
316 }
317
318 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
319 // teams that are allowed will now have their player counts stored in c1...c4
320 void GetTeamCounts(entity ignore)
321 {
322         float value, bvalue;
323         // now count how many players are on each team already
324
325         // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
326         // also remember the lowest-scoring player
327
328         FOREACH_CLIENT(true, {
329                 float t;
330                 if(IS_PLAYER(it) || it.caplayer)
331                         t = it.team;
332                 else if(it.team_forced > 0)
333                         t = it.team_forced; // reserve the spot
334                 else
335                         continue;
336                 if(it != ignore)// && it.netname != "")
337                 {
338                         value = PlayerValue(it);
339                         if(IS_BOT_CLIENT(it))
340                                 bvalue = value;
341                         else
342                                 bvalue = 0;
343                         if(t == NUM_TEAM_1)
344                         {
345                                 if(c1 >= 0)
346                                 {
347                                         c1 = c1 + value;
348                                         cb1 = cb1 + bvalue;
349                                 }
350                         }
351                         else if(t == NUM_TEAM_2)
352                         {
353                                 if(c2 >= 0)
354                                 {
355                                         c2 = c2 + value;
356                                         cb2 = cb2 + bvalue;
357                                 }
358                         }
359                         else if(t == NUM_TEAM_3)
360                         {
361                                 if(c3 >= 0)
362                                 {
363                                         c3 = c3 + value;
364                                         cb3 = cb3 + bvalue;
365                                 }
366                         }
367                         else if(t == NUM_TEAM_4)
368                         {
369                                 if(c4 >= 0)
370                                 {
371                                         c4 = c4 + value;
372                                         cb4 = cb4 + bvalue;
373                                 }
374                         }
375                 }
376         });
377
378         // if the player who has a forced team has not joined yet, reserve the spot
379         if(autocvar_g_campaign)
380         {
381                 switch(autocvar_g_campaign_forceteam)
382                 {
383                         case 1: if(c1 == cb1) ++c1; break;
384                         case 2: if(c2 == cb2) ++c2; break;
385                         case 3: if(c3 == cb3) ++c3; break;
386                         case 4: if(c4 == cb4) ++c4; break;
387                 }
388         }
389 }
390
391 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
392 {
393         // we assume that CheckAllowedTeams and GetTeamCounts have already been called
394         float f;
395         float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
396
397         switch(ta)
398         {
399                 case 1: ca = c1; cba = cb1; sa = team1_score; break;
400                 case 2: ca = c2; cba = cb2; sa = team2_score; break;
401                 case 3: ca = c3; cba = cb3; sa = team3_score; break;
402                 case 4: ca = c4; cba = cb4; sa = team4_score; break;
403         }
404         switch(tb)
405         {
406                 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
407                 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
408                 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
409                 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
410         }
411
412         // invalid
413         if(ca < 0 || cb < 0)
414                 return false;
415
416         // equal
417         if(ta == tb)
418                 return true;
419
420         if(IS_REAL_CLIENT(e))
421         {
422                 if(bots_would_leave)
423                 {
424                         ca -= cba * 0.999;
425                         cb -= cbb * 0.999;
426                 }
427         }
428
429         // keep teams alive (teams of size 0 always count as smaller, ignoring score)
430         if(ca < 1)
431                 if(cb >= 1)
432                         return true;
433         if(ca >= 1)
434                 if(cb < 1)
435                         return false;
436
437         // first, normalize
438         f = max(ca, cb, 1);
439         ca /= f;
440         cb /= f;
441         f = max(sa, sb, 1);
442         sa /= f;
443         sb /= f;
444
445         // the more we're at the end of the match, the more take scores into account
446         f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
447         ca += (sa - ca) * f;
448         cb += (sb - cb) * f;
449
450         return ca <= cb;
451 }
452
453 // returns # of smallest team (1, 2, 3, 4)
454 // NOTE: Assumes CheckAllowedTeams has already been called!
455 float FindSmallestTeam(entity pl, float ignore_pl)
456 {
457         int totalteams = 0;
458         int t = 1; // initialize with a random team?
459         if(c4 >= 0) t = 4;
460         if(c3 >= 0) t = 3;
461         if(c2 >= 0) t = 2;
462         if(c1 >= 0) t = 1;
463
464         // find out what teams are available
465         //CheckAllowedTeams();
466
467         // make sure there are at least 2 teams to join
468         if(c1 >= 0)
469                 totalteams = totalteams + 1;
470         if(c2 >= 0)
471                 totalteams = totalteams + 1;
472         if(c3 >= 0)
473                 totalteams = totalteams + 1;
474         if(c4 >= 0)
475                 totalteams = totalteams + 1;
476
477         if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
478                 totalteams += 1;
479
480         if(totalteams <= 1)
481         {
482                 if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
483                         return 1; // special case for campaign and player joining
484                 else if(totalteams == 1) // single team
485                         LOG_TRACEF("Only 1 team available for %s, you may need to fix your map", MapInfo_Type_ToString(MapInfo_CurrentGametype()));
486                 else // no teams, major no no
487                         error(sprintf("No teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
488         }
489
490         // count how many players are in each team
491         if(ignore_pl)
492                 GetTeamCounts(pl);
493         else
494                 GetTeamCounts(NULL);
495
496         RandomSelection_Init();
497
498         if(TeamSmallerEqThanTeam(1, t, pl))
499                 t = 1;
500         if(TeamSmallerEqThanTeam(2, t, pl))
501                 t = 2;
502         if(TeamSmallerEqThanTeam(3, t, pl))
503                 t = 3;
504         if(TeamSmallerEqThanTeam(4, t, pl))
505                 t = 4;
506
507         // now t is the minimum, or A minimum!
508         if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
509                 RandomSelection_AddFloat(1, 1, 1);
510         if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
511                 RandomSelection_AddFloat(2, 1, 1);
512         if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
513                 RandomSelection_AddFloat(3, 1, 1);
514         if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
515                 RandomSelection_AddFloat(4, 1, 1);
516
517         return RandomSelection_chosen_float;
518 }
519
520 int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
521 {
522         float smallest, selectedteam;
523
524         // don't join a team if we're not playing a team game
525         if(!teamplay)
526                 return 0;
527
528         // find out what teams are available
529         CheckAllowedTeams(this);
530
531         // if we don't care what team he ends up on, put him on whatever team he entered as.
532         // if he's not on a valid team, then let other code put him on the smallest team
533         if(!forcebestteam)
534         {
535                 if(     c1 >= 0 && this.team == NUM_TEAM_1)
536                         selectedteam = this.team;
537                 else if(c2 >= 0 && this.team == NUM_TEAM_2)
538                         selectedteam = this.team;
539                 else if(c3 >= 0 && this.team == NUM_TEAM_3)
540                         selectedteam = this.team;
541                 else if(c4 >= 0 && this.team == NUM_TEAM_4)
542                         selectedteam = this.team;
543                 else
544                         selectedteam = -1;
545
546                 if(selectedteam > 0)
547                 {
548                         if(!only_return_best)
549                         {
550                                 SetPlayerColors(this, selectedteam - 1);
551
552                                 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
553                                 // when JoinBestTeam is called by client.qc/ClientConnect the player_id is 0 the log attempt is rejected
554                                 LogTeamchange(this.playerid, this.team, 99);
555                         }
556                         return selectedteam;
557                 }
558                 // otherwise end up on the smallest team (handled below)
559         }
560
561         smallest = FindSmallestTeam(this, true);
562
563         if(!only_return_best && !this.bot_forced_team)
564         {
565                 TeamchangeFrags(this);
566                 if(smallest == 1)
567                 {
568                         SetPlayerColors(this, NUM_TEAM_1 - 1);
569                 }
570                 else if(smallest == 2)
571                 {
572                         SetPlayerColors(this, NUM_TEAM_2 - 1);
573                 }
574                 else if(smallest == 3)
575                 {
576                         SetPlayerColors(this, NUM_TEAM_3 - 1);
577                 }
578                 else if(smallest == 4)
579                 {
580                         SetPlayerColors(this, NUM_TEAM_4 - 1);
581                 }
582                 else
583                 {
584                         error("smallest team: invalid team\n");
585                 }
586
587                 LogTeamchange(this.playerid, this.team, 2); // log auto join
588
589                 if(!IS_DEAD(this))
590                         Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
591         }
592
593         return smallest;
594 }
595
596 //void() ctf_playerchanged;
597 void SV_ChangeTeam(entity this, float _color)
598 {
599         float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
600
601         // in normal deathmatch we can just apply the color and we're done
602         if(!teamplay)
603                 SetPlayerColors(this, _color);
604
605         if(!IS_CLIENT(this))
606         {
607                 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
608                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
609                 return;
610         }
611
612         if(!teamplay)
613                 return;
614
615         scolor = this.clientcolors & 0x0F;
616         dcolor = _color & 0x0F;
617
618         if(scolor == NUM_TEAM_1 - 1)
619                 steam = 1;
620         else if(scolor == NUM_TEAM_2 - 1)
621                 steam = 2;
622         else if(scolor == NUM_TEAM_3 - 1)
623                 steam = 3;
624         else // if(scolor == NUM_TEAM_4 - 1)
625                 steam = 4;
626         if(dcolor == NUM_TEAM_1 - 1)
627                 dteam = 1;
628         else if(dcolor == NUM_TEAM_2 - 1)
629                 dteam = 2;
630         else if(dcolor == NUM_TEAM_3 - 1)
631                 dteam = 3;
632         else // if(dcolor == NUM_TEAM_4 - 1)
633                 dteam = 4;
634
635         CheckAllowedTeams(this);
636
637         if(dteam == 1 && c1 < 0) dteam = 4;
638         if(dteam == 4 && c4 < 0) dteam = 3;
639         if(dteam == 3 && c3 < 0) dteam = 2;
640         if(dteam == 2 && c2 < 0) dteam = 1;
641
642         // not changing teams
643         if(scolor == dcolor)
644         {
645                 //bprint("same team change\n");
646                 SetPlayerTeam(this, dteam, steam, true);
647                 return;
648         }
649
650         if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && CS(this).wasplayer)) {
651                 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
652                 return; // changing teams is not allowed
653         }
654
655         // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
656         if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
657         {
658                 GetTeamCounts(this);
659                 if(!TeamSmallerEqThanTeam(dteam, steam, this))
660                 {
661                         Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
662                         return;
663                 }
664         }
665
666 //      bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
667
668         if(IS_PLAYER(this) && steam != dteam)
669         {
670                 // reduce frags during a team change
671                 TeamchangeFrags(this);
672         }
673
674         MUTATOR_CALLHOOK(Player_ChangeTeam, this, steam, dteam);
675
676         SetPlayerTeam(this, dteam, steam, !IS_CLIENT(this));
677
678         if(IS_PLAYER(this) && steam != dteam)
679         {
680                 // kill player when changing teams
681                 if(!IS_DEAD(this))
682                         Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
683         }
684 }
685
686 void ShufflePlayerOutOfTeam (float source_team)
687 {
688         float smallestteam, smallestteam_count, steam;
689         float lowest_bot_score, lowest_player_score;
690         entity lowest_bot, lowest_player, selected;
691
692         smallestteam = 0;
693         smallestteam_count = 999999999;
694
695         if(c1 >= 0 && c1 < smallestteam_count)
696         {
697                 smallestteam = 1;
698                 smallestteam_count = c1;
699         }
700         if(c2 >= 0 && c2 < smallestteam_count)
701         {
702                 smallestteam = 2;
703                 smallestteam_count = c2;
704         }
705         if(c3 >= 0 && c3 < smallestteam_count)
706         {
707                 smallestteam = 3;
708                 smallestteam_count = c3;
709         }
710         if(c4 >= 0 && c4 < smallestteam_count)
711         {
712                 smallestteam = 4;
713                 smallestteam_count = c4;
714         }
715
716         if(!smallestteam)
717         {
718                 bprint("warning: no smallest team\n");
719                 return;
720         }
721
722         if(source_team == 1)
723                 steam = NUM_TEAM_1;
724         else if(source_team == 2)
725                 steam = NUM_TEAM_2;
726         else if(source_team == 3)
727                 steam = NUM_TEAM_3;
728         else // if(source_team == 4)
729                 steam = NUM_TEAM_4;
730
731         lowest_bot = NULL;
732         lowest_bot_score = 999999999;
733         lowest_player = NULL;
734         lowest_player_score = 999999999;
735
736         // find the lowest-scoring player & bot of that team
737         FOREACH_CLIENT(IS_PLAYER(it) && it.team == steam, {
738                 if(it.isbot)
739                 {
740                         if(it.totalfrags < lowest_bot_score)
741                         {
742                                 lowest_bot = it;
743                                 lowest_bot_score = it.totalfrags;
744                         }
745                 }
746                 else
747                 {
748                         if(it.totalfrags < lowest_player_score)
749                         {
750                                 lowest_player = it;
751                                 lowest_player_score = it.totalfrags;
752                         }
753                 }
754         });
755
756         // prefers to move a bot...
757         if(lowest_bot != NULL)
758                 selected = lowest_bot;
759         // but it will move a player if it has to
760         else
761                 selected = lowest_player;
762         // don't do anything if it couldn't find anyone
763         if(!selected)
764         {
765                 bprint("warning: couldn't find a player to move from team\n");
766                 return;
767         }
768
769         // smallest team gains a member
770         if(smallestteam == 1)
771         {
772                 c1 = c1 + 1;
773         }
774         else if(smallestteam == 2)
775         {
776                 c2 = c2 + 1;
777         }
778         else if(smallestteam == 3)
779         {
780                 c3 = c3 + 1;
781         }
782         else if(smallestteam == 4)
783         {
784                 c4 = c4 + 1;
785         }
786         else
787         {
788                 bprint("warning: destination team invalid\n");
789                 return;
790         }
791         // source team loses a member
792         if(source_team == 1)
793         {
794                 c1 = c1 + 1;
795         }
796         else if(source_team == 2)
797         {
798                 c2 = c2 + 2;
799         }
800         else if(source_team == 3)
801         {
802                 c3 = c3 + 3;
803         }
804         else if(source_team == 4)
805         {
806                 c4 = c4 + 4;
807         }
808         else
809         {
810                 bprint("warning: source team invalid\n");
811                 return;
812         }
813
814         // move the player to the new team
815         TeamchangeFrags(selected);
816         SetPlayerTeam(selected, smallestteam, source_team, false);
817
818         if(!IS_DEAD(selected))
819                 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE.m_id, selected.origin, '0 0 0');
820         Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);
821 }