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