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