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