]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/teamplay.qc
Add highly experimental RTS game-mode (for testing purposes)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / teamplay.qc
1 string cache_mutatormsg;
2 string cache_lastmutatormsg;
3
4 // client counts for each team
5 float c1, c2, c3, c4;
6 // # of bots on those teams
7 float cb1, cb2, cb3, cb4;
8
9 //float audit_teams_time;
10
11 void TeamchangeFrags(entity e)
12 {
13         PlayerScore_Clear(e);
14 }
15
16 vector TeamColor(float teem)
17 {
18         switch(teem)
19         {
20                 case COLOR_TEAM1:
21                         return '1 0.0625 0.0625';
22                 case COLOR_TEAM2:
23                         return '0.0625 0.0625 1';
24                 case COLOR_TEAM3:
25                         return '1 1 0.0625';
26                 case COLOR_TEAM4:
27                         return '1 0.0625 1';
28                 default:
29                         return '1 1 1';
30         }
31 }
32
33 string TeamName(float t)
34 {
35         return strcat(Team_ColorName(t), " Team");
36 }
37 string ColoredTeamName(float t)
38 {
39         return strcat(Team_ColorCode(t), Team_ColorName(t), " Team^7");
40 }
41 string TeamNoName(float t)
42 {
43         // fixme: Search for team entities and get their .netname's!
44         if(t == 1)
45                 return "Red Team";
46         if(t == 2)
47                 return "Blue Team";
48         if(t == 3)
49                 return "Yellow Team";
50         if(t == 4)
51                 return "Pink Team";
52         return "Neutral Team";
53 }
54
55 void runematch_init();
56 void tdm_init();
57 void entcs_init();
58
59 void LogTeamchange(float player_id, float team_number, float type)
60 {
61         if(!autocvar_sv_eventlog)
62                 return;
63
64         if(player_id < 1)
65                 return;
66
67         GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
68 }
69
70 void default_delayedinit()
71 {
72         if(!scores_initialized)
73                 ScoreRules_generic();
74 }
75
76 void ActivateTeamplay()
77 {
78         serverflags |= SERVERFLAG_TEAMPLAY;
79         teamplay = 1;
80 }
81
82 void InitGameplayMode()
83 {
84         float fraglimit_override, timelimit_override, leadlimit_override, qualifying_override;
85
86         qualifying_override = -1;
87
88         VoteReset();
89
90         // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
91         get_mi_min_max(1);
92         world.mins = mi_min;
93         world.maxs = mi_max;
94
95         MapInfo_LoadMapSettings(mapname);
96         teamplay = 0;
97         serverflags &~= SERVERFLAG_TEAMPLAY;
98
99         if not(cvar_value_issafe(world.fog))
100         {
101                 print("The current map contains a potentially harmful fog setting, ignored\n");
102                 world.fog = string_null;
103         }
104         if(MapInfo_Map_fog != "")
105                 if(MapInfo_Map_fog == "none")
106                         world.fog = string_null;
107                 else
108                         world.fog = strzone(MapInfo_Map_fog);
109         clientstuff = strzone(MapInfo_Map_clientstuff);
110
111         MapInfo_ClearTemps();
112
113         // set both here, gamemode can override it later
114         timelimit_override = autocvar_timelimit_override;
115         fraglimit_override = autocvar_fraglimit_override;
116         leadlimit_override = autocvar_leadlimit_override;
117         gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
118
119         if(g_dm)
120         {
121         }
122
123         if(g_tdm)
124         {
125                 ActivateTeamplay();
126                 tdm_init();
127                 if(autocvar_g_tdm_team_spawns)
128                         have_team_spawns = -1; // request team spawns
129         }
130
131         if(g_domination)
132         {
133                 ActivateTeamplay();
134                 fraglimit_override = autocvar_g_domination_point_limit;
135                 leadlimit_override = autocvar_g_domination_point_leadlimit;
136                 MUTATOR_ADD(gamemode_domination);
137                 have_team_spawns = -1; // request team spawns
138         }
139
140         if(g_ctf)
141         {
142                 ActivateTeamplay();
143                 fraglimit_override = autocvar_capturelimit_override;
144                 leadlimit_override = autocvar_captureleadlimit_override;
145                 MUTATOR_ADD(gamemode_ctf);
146                 have_team_spawns = -1; // request team spawns
147         }
148     
149         if(g_td)
150         {
151                 fraglimit_override = 0; // no limits in TD - it's a survival mode
152                 leadlimit_override = 0;
153                 MUTATOR_ADD(gamemode_td);
154         }
155         
156         if(g_rts)
157         {
158                 fraglimit_override = 0;
159                 leadlimit_override = 0;
160                 MUTATOR_ADD(gamemode_rts);
161         }
162
163         if(g_runematch)
164         {
165                 // ActivateTeamplay();
166                 fraglimit_override = autocvar_g_runematch_point_limit;
167                 leadlimit_override = autocvar_g_runematch_point_leadlimit;
168                 runematch_init();
169         }
170
171         if(g_lms)
172         {
173                 fraglimit_override = autocvar_g_lms_lives_override;
174                 leadlimit_override = 0; // not supported by LMS
175                 if(fraglimit_override == 0)
176                         fraglimit_override = -1;
177                 lms_lowest_lives = 9999;
178                 lms_next_place = 0;
179                 ScoreRules_lms();
180         }
181
182         if(g_arena)
183         {
184                 fraglimit_override = autocvar_g_arena_point_limit;
185                 leadlimit_override = autocvar_g_arena_point_leadlimit;
186                 maxspawned = autocvar_g_arena_maxspawned;
187                 if(maxspawned < 2)
188                         maxspawned = 2;
189                 arena_roundbased = autocvar_g_arena_roundbased;
190         }
191
192         if(g_ca)
193         {
194                 ActivateTeamplay();
195                 fraglimit_override = autocvar_g_ca_point_limit;
196                 leadlimit_override = autocvar_g_ca_point_leadlimit;
197                 precache_sound("ctf/red_capture.wav");
198                 precache_sound("ctf/blue_capture.wav");
199         }
200         if(g_keyhunt)
201         {
202                 ActivateTeamplay();
203                 fraglimit_override = autocvar_g_keyhunt_point_limit;
204                 leadlimit_override = autocvar_g_keyhunt_point_leadlimit;
205                 MUTATOR_ADD(gamemode_keyhunt);
206         }
207
208         if(g_freezetag)
209         {
210                 ActivateTeamplay();
211                 fraglimit_override = autocvar_g_freezetag_point_limit;
212                 leadlimit_override = autocvar_g_freezetag_point_leadlimit;
213                 MUTATOR_ADD(gamemode_freezetag);
214         }
215
216         if(g_assault)
217         {
218                 ActivateTeamplay();
219                 ScoreRules_assault();
220                 have_team_spawns = -1; // request team spawns
221         }
222
223         if(g_onslaught)
224         {
225                 ActivateTeamplay();
226                 have_team_spawns = -1; // request team spawns
227                 MUTATOR_ADD(gamemode_onslaught);
228         }
229
230         if(g_race)
231         {
232
233                 if(autocvar_g_race_teams)
234                 {
235                         ActivateTeamplay();
236                         race_teams = bound(2, autocvar_g_race_teams, 4);
237                         have_team_spawns = -1; // request team spawns
238                 }
239                 else
240                         race_teams = 0;
241
242                 qualifying_override = autocvar_g_race_qualifying_timelimit_override;
243                 fraglimit_override = autocvar_g_race_laps_limit;
244                 leadlimit_override = 0; // currently not supported by race
245         }
246
247         if(g_cts)
248         {
249                 g_race_qualifying = 1;
250                 fraglimit_override = 0;
251                 leadlimit_override = 0;
252         }
253
254         if(g_nexball)
255         {
256         fraglimit_override = autocvar_g_nexball_goallimit;
257         leadlimit_override = autocvar_g_nexball_goalleadlimit;
258         ActivateTeamplay();
259         have_team_spawns = -1; // request team spawns
260             MUTATOR_ADD(gamemode_nexball);
261         }
262         
263         if(g_keepaway)
264         {
265                 MUTATOR_ADD(gamemode_keepaway);
266         }
267
268         if(teamplay)
269                 entcs_init();
270
271         cache_mutatormsg = strzone("");
272         cache_lastmutatormsg = strzone("");
273
274         // enforce the server's universal frag/time limits
275         if(!autocvar_g_campaign)
276         {
277                 if(fraglimit_override >= 0)
278                         cvar_set("fraglimit", ftos(fraglimit_override));
279                 if(timelimit_override >= 0)
280                         cvar_set("timelimit", ftos(timelimit_override));
281                 if(leadlimit_override >= 0)
282                         cvar_set("leadlimit", ftos(leadlimit_override));
283                 if(qualifying_override >= 0)
284                         cvar_set("g_race_qualifying_timelimit", ftos(qualifying_override));
285         }
286
287         if(g_race)
288         {
289                 // we need to find out the correct value for g_race_qualifying
290                 if(autocvar_g_campaign)
291                 {
292                         g_race_qualifying = 1;
293                 }
294                 else if(!autocvar_g_campaign && autocvar_g_race_qualifying_timelimit > 0)
295                 {
296                         g_race_qualifying = 2;
297                         race_fraglimit = autocvar_fraglimit;
298                         race_leadlimit = autocvar_leadlimit;
299                         race_timelimit = autocvar_timelimit;
300                         cvar_set("fraglimit", "0");
301                         cvar_set("leadlimit", "0");
302                         cvar_set("timelimit", ftos(autocvar_g_race_qualifying_timelimit));
303                 }
304                 else
305                         g_race_qualifying = 0;
306         }
307
308         if(g_race || g_cts)
309         {
310                 if(g_race_qualifying)
311                         independent_players = 1;
312
313                 ScoreRules_race();
314         }
315
316         InitializeEntity(world, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
317 }
318
319 string GetClientVersionMessage() {
320         string versionmsg;
321         if (self.version_mismatch) {
322                 if(self.version < autocvar_gameversion) {
323                         versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
324                 } else {
325                         versionmsg = "^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
326                 }
327         } else {
328                 versionmsg = "^2client version and server version are compatible.^8";
329         }
330         return versionmsg;
331 }
332
333 string getwelcomemessage(void)
334 {
335         string s, modifications, motd;
336
337         ret_string = "";
338         MUTATOR_CALLHOOK(BuildMutatorsPrettyString);
339         modifications = ret_string;
340         
341         if(g_minstagib)
342                 modifications = strcat(modifications, ", MinstaGib");
343         if(g_weaponarena)
344         {
345                 if(g_weaponarena_random)
346                         modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
347                 else
348                         modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
349         }
350         if(autocvar_g_start_weapon_laser == 0)
351                 modifications = strcat(modifications, ", No start weapons");
352         if(autocvar_sv_gravity < 800)
353                 modifications = strcat(modifications, ", Low gravity");
354         if(g_cloaked && !g_cts)
355                 modifications = strcat(modifications, ", Cloaked");
356         if(g_grappling_hook)
357                 modifications = strcat(modifications, ", Hook");
358         if(g_midair)
359                 modifications = strcat(modifications, ", Midair");
360         if(g_pinata)
361                 modifications = strcat(modifications, ", Piñata");
362         if(g_weapon_stay && !g_cts)
363                 modifications = strcat(modifications, ", Weapons stay");
364         if(g_bloodloss > 0)
365                 modifications = strcat(modifications, ", Blood loss");
366         if(g_jetpack)
367                 modifications = strcat(modifications, ", Jet pack");
368         if(autocvar_g_powerups == 0)
369                 modifications = strcat(modifications, ", No powerups");
370         if(autocvar_g_powerups > 0)
371                 modifications = strcat(modifications, ", Powerups");
372         modifications = substring(modifications, 2, strlen(modifications) - 2);
373
374         string versionmessage;
375         versionmessage = GetClientVersionMessage();
376
377         s = strcat("This is Xonotic ", autocvar_g_xonoticversion, "\n", versionmessage);
378         s = strcat(s, "^8\n\nmatch type is ^1", gamemode_name, "^8\n");
379
380         if(modifications != "")
381                 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
382
383         if (g_grappling_hook)
384                 s = strcat(s, "\n\n^3grappling hook^8 is enabled, press 'e' to use it\n");
385
386         if(cache_lastmutatormsg != autocvar_g_mutatormsg)
387         {
388                 if(cache_lastmutatormsg)
389                         strunzone(cache_lastmutatormsg);
390                 if(cache_mutatormsg)
391                         strunzone(cache_mutatormsg);
392                 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
393                 cache_mutatormsg = strzone(cache_lastmutatormsg);
394         }
395
396         if (cache_mutatormsg != "") {
397                 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
398         }
399
400         motd = autocvar_sv_motd;
401         if (motd != "") {
402                 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
403         }
404         return s;
405 }
406
407 void SetPlayerColors(entity pl, float _color)
408 {
409         /*string s;
410         s = ftos(cl);
411         stuffcmd(pl, strcat("color ", s, " ", s, "\n")  );
412         pl.team = cl + 1;
413         //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
414         pl.clientcolors = 16*cl + cl;*/
415
416         float pants, shirt;
417         pants = _color & 0x0F;
418         shirt = _color & 0xF0;
419
420
421         if(teamplay) {
422                 setcolor(pl, 16*pants + pants);
423         } else {
424                 setcolor(pl, shirt + pants);
425         }
426 }
427
428 void SetPlayerTeam(entity pl, float t, float s, float noprint)
429 {
430         float _color;
431
432         if(t == 4)
433                 _color = COLOR_TEAM4 - 1;
434         else if(t == 3)
435                 _color = COLOR_TEAM3 - 1;
436         else if(t == 2)
437                 _color = COLOR_TEAM2 - 1;
438         else
439                 _color = COLOR_TEAM1 - 1;
440
441         SetPlayerColors(pl,_color);
442
443         if(t != s) {
444                 LogTeamchange(pl.playerid, pl.team, 3);  // log manual team join
445
446                 if(!noprint)
447                 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
448         }
449
450 }
451
452 // set c1...c4 to show what teams are allowed
453 void CheckAllowedTeams (entity for_whom)
454 {
455         float dm;
456         entity head;
457         string teament_name;
458
459         c1 = c2 = c3 = c4 = -1;
460         cb1 = cb2 = cb3 = cb4 = 0;
461
462         teament_name = string_null;
463         if(g_onslaught)
464         {
465                 // onslaught is special
466                 head = findchain(classname, "onslaught_generator");
467                 while (head)
468                 {
469                         if (head.team == COLOR_TEAM1) c1 = 0;
470                         if (head.team == COLOR_TEAM2) c2 = 0;
471                         if (head.team == COLOR_TEAM3) c3 = 0;
472                         if (head.team == COLOR_TEAM4) c4 = 0;
473                         head = head.chain;
474                 }
475         }
476         else if(g_domination)
477                 teament_name = "dom_team";
478         else if(g_ctf)
479                 teament_name = "ctf_team";
480         else if(g_tdm)
481                 teament_name = "tdm_team";
482         else if(g_nexball)
483                 teament_name = "nexball_team";
484         else if(g_assault)
485                 c1 = c2 = 0; // Assault always has 2 teams
486         else
487         {
488                 // cover anything else by treating it like tdm with no teams spawned
489                 if(g_race)
490                         dm = race_teams;
491                 else
492                         dm = 2;
493
494                 ret_float = dm;
495                 MUTATOR_CALLHOOK(GetTeamCount);
496                 dm = ret_float;
497
498                 if(dm >= 4)
499                         c1 = c2 = c3 = c4 = 0;
500                 else if(dm >= 3)
501                         c1 = c2 = c3 = 0;
502                 else
503                         c1 = c2 = 0;
504         }
505
506         // find out what teams are allowed if necessary
507         if(teament_name)
508         {
509                 head = find(world, classname, teament_name);
510                 while(head)
511                 {
512                         if(!(g_domination && head.netname == ""))
513                         {
514                                 if(head.team == COLOR_TEAM1)
515                                         c1 = 0;
516                                 else if(head.team == COLOR_TEAM2)
517                                         c2 = 0;
518                                 else if(head.team == COLOR_TEAM3)
519                                         c3 = 0;
520                                 else if(head.team == COLOR_TEAM4)
521                                         c4 = 0;
522                         }
523                         head = find(head, classname, teament_name);
524                 }
525         }
526
527         // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
528         if(c3==-1 && c4==-1)
529         if(autocvar_bot_vs_human && for_whom)
530         {
531                 if(autocvar_bot_vs_human > 0)
532                 {
533                         // bots are all blue
534                         if(clienttype(for_whom) == CLIENTTYPE_BOT)
535                                 c1 = c3 = c4 = -1;
536                         else
537                                 c2 = -1;
538                 }
539                 else
540                 {
541                         // bots are all red
542                         if(clienttype(for_whom) == CLIENTTYPE_BOT)
543                                 c2 = c3 = c4 = -1;
544                         else
545                                 c1 = -1;
546                 }
547         }
548
549         // if player has a forced team, ONLY allow that one
550         if(self.team_forced == COLOR_TEAM1 && c1 >= 0)
551                 c2 = c3 = c4 = -1;
552         else if(self.team_forced == COLOR_TEAM2 && c2 >= 0)
553                 c1 = c3 = c4 = -1;
554         else if(self.team_forced == COLOR_TEAM3 && c3 >= 0)
555                 c1 = c2 = c4 = -1;
556         else if(self.team_forced == COLOR_TEAM4 && c4 >= 0)
557                 c1 = c2 = c3 = -1;
558 }
559
560 float PlayerValue(entity p)
561 {
562         return 1;
563         // FIXME: it always returns 1...
564 }
565
566 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
567 // teams that are allowed will now have their player counts stored in c1...c4
568 void GetTeamCounts(entity ignore)
569 {
570         entity head;
571         float value, bvalue;
572         // now count how many players are on each team already
573
574         // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
575         // also remember the lowest-scoring player
576
577         FOR_EACH_CLIENT(head)
578         {
579                 float t;
580                 if(head.classname == "player")
581                         t = head.team;
582                 else if(head.team_forced > 0)
583                         t = head.team_forced; // reserve the spot
584                 else
585                         continue;
586                 if(head != ignore)// && head.netname != "")
587                 {
588                         value = PlayerValue(head);
589                         if(clienttype(head) == CLIENTTYPE_BOT)
590                                 bvalue = value;
591                         else
592                                 bvalue = 0;
593                         if(t == COLOR_TEAM1)
594                         {
595                                 if(c1 >= 0)
596                                 {
597                                         c1 = c1 + value;
598                                         cb1 = cb1 + bvalue;
599                                 }
600                         }
601                         if(t == COLOR_TEAM2)
602                         {
603                                 if(c2 >= 0)
604                                 {
605                                         c2 = c2 + value;
606                                         cb2 = cb2 + bvalue;
607                                 }
608                         }
609                         if(t == COLOR_TEAM3)
610                         {
611                                 if(c3 >= 0)
612                                 {
613                                         c3 = c3 + value;
614                                         cb3 = cb3 + bvalue;
615                                 }
616                         }
617                         if(t == COLOR_TEAM4)
618                         {
619                                 if(c4 >= 0)
620                                 {
621                                         c4 = c4 + value;
622                                         cb4 = cb4 + bvalue;
623                                 }
624                         }
625                 }
626         }
627
628         // if the player who has a forced team has not joined yet, reserve the spot
629         if(autocvar_g_campaign)
630         {
631                 switch(autocvar_g_campaign_forceteam)
632                 {
633                         case 1: if(c1 == cb1) ++c1; break;
634                         case 2: if(c2 == cb2) ++c2; break;
635                         case 3: if(c3 == cb3) ++c3; break;
636                         case 4: if(c4 == cb4) ++c4; break;
637                 }
638         }
639 }
640
641 float TeamSmallerEqThanTeam(float ta, float tb, entity e)
642 {
643         // we assume that CheckAllowedTeams and GetTeamCounts have already been called
644         float f;
645         float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
646
647         switch(ta)
648         {
649                 case 1: ca = c1; cba = cb1; sa = team1_score; break;
650                 case 2: ca = c2; cba = cb2; sa = team2_score; break;
651                 case 3: ca = c3; cba = cb3; sa = team3_score; break;
652                 case 4: ca = c4; cba = cb4; sa = team4_score; break;
653         }
654         switch(tb)
655         {
656                 case 1: cb = c1; cbb = cb1; sb = team1_score; break;
657                 case 2: cb = c2; cbb = cb2; sb = team2_score; break;
658                 case 3: cb = c3; cbb = cb3; sb = team3_score; break;
659                 case 4: cb = c4; cbb = cb4; sb = team4_score; break;
660         }
661
662         // invalid
663         if(ca < 0 || cb < 0)
664                 return FALSE;
665
666         // equal
667         if(ta == tb)
668                 return TRUE;
669
670         if(clienttype(e) == CLIENTTYPE_REAL)
671         {
672                 if(bots_would_leave)
673                 {
674                         ca -= cba * 0.999;
675                         cb -= cbb * 0.999;
676                 }
677         }
678         
679         // keep teams alive (teams of size 0 always count as smaller, ignoring score)
680         if(ca < 1)
681                 if(cb >= 1)
682                         return TRUE;
683         if(ca >= 1)
684                 if(cb < 1)
685                         return FALSE;
686
687         // first, normalize
688         f = max(ca, cb, 1);
689         ca /= f;
690         cb /= f;
691         f = max(sa, sb, 1);
692         sa /= f;
693         sb /= f;
694
695         // the more we're at the end of the match, the more take scores into account
696         f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
697         ca += (sa - ca) * f;
698         cb += (sb - cb) * f;
699
700         return ca <= cb;
701 }
702
703 // returns # of smallest team (1, 2, 3, 4)
704 // NOTE: Assumes CheckAllowedTeams has already been called!
705 float FindSmallestTeam(entity pl, float ignore_pl)
706 {
707         float totalteams, t;
708         totalteams = 0;
709
710         // find out what teams are available
711         //CheckAllowedTeams();
712
713         // make sure there are at least 2 teams to join
714         if(c1 >= 0)
715                 totalteams = totalteams + 1;
716         if(c2 >= 0)
717                 totalteams = totalteams + 1;
718         if(c3 >= 0)
719                 totalteams = totalteams + 1;
720         if(c4 >= 0)
721                 totalteams = totalteams + 1;
722
723         if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
724                 totalteams += 1;
725
726         if(totalteams <= 1)
727         {
728                 if(autocvar_g_campaign && pl && clienttype(pl) == CLIENTTYPE_REAL)
729                         return 1; // special case for campaign and player joining
730                 else if(g_domination)
731                         error("Too few teams available for domination\n");
732                 else if(g_ctf)
733                         error("Too few teams available for ctf\n");
734                 else if(g_keyhunt)
735                         error("Too few teams available for key hunt\n");
736                 else if(g_freezetag)
737                         error("Too few teams available for freeze tag\n");
738                 else
739                         error("Too few teams available for team deathmatch\n");
740         }
741
742         // count how many players are in each team
743         if(ignore_pl)
744                 GetTeamCounts(pl);
745         else
746                 GetTeamCounts(world);
747
748         RandomSelection_Init();
749         
750         t = 1;
751         if(TeamSmallerEqThanTeam(2, t, pl))
752                 t = 2;
753         if(TeamSmallerEqThanTeam(3, t, pl))
754                 t = 3;
755         if(TeamSmallerEqThanTeam(4, t, pl))
756                 t = 4;
757
758         // now t is the minimum, or A minimum!
759         if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
760                 RandomSelection_Add(world, 1, string_null, 1, 1);
761         if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
762                 RandomSelection_Add(world, 2, string_null, 1, 1);
763         if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
764                 RandomSelection_Add(world, 3, string_null, 1, 1);
765         if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
766                 RandomSelection_Add(world, 4, string_null, 1, 1);
767
768         return RandomSelection_chosen_float;
769 }
770
771 float JoinBestTeam(entity pl, float only_return_best, float forcebestteam)
772 {
773         float smallest, selectedteam;
774
775         // don't join a team if we're not playing a team game
776         if(!teamplay)
777                 return 0;
778
779         // find out what teams are available
780         CheckAllowedTeams(pl);
781
782         // if we don't care what team he ends up on, put him on whatever team he entered as.
783         // if he's not on a valid team, then let other code put him on the smallest team
784         if(!forcebestteam)
785         {
786                 if(     c1 >= 0 && pl.team == COLOR_TEAM1)
787                         selectedteam = pl.team;
788                 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
789                         selectedteam = pl.team;
790                 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
791                         selectedteam = pl.team;
792                 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
793                         selectedteam = pl.team;
794                 else
795                         selectedteam = -1;
796
797                 if(selectedteam > 0)
798                 {
799                         if(!only_return_best)
800                         {
801                                 SetPlayerColors(pl, selectedteam - 1);
802
803                                 // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
804                                 // when JoinBestTeam is called by cl_client.qc/ClientConnect the player_id is 0 the log attempt is rejected
805                                 LogTeamchange(pl.playerid, pl.team, 99);
806                         }
807                         return selectedteam;
808                 }
809                 // otherwise end up on the smallest team (handled below)
810         }
811
812         smallest = FindSmallestTeam(pl, TRUE);
813
814         if(!only_return_best && !pl.bot_forced_team)
815         {
816                 TeamchangeFrags(self);
817                 if(smallest == 1)
818                 {
819                         SetPlayerColors(pl, COLOR_TEAM1 - 1);
820                 }
821                 else if(smallest == 2)
822                 {
823                         SetPlayerColors(pl, COLOR_TEAM2 - 1);
824                 }
825                 else if(smallest == 3)
826                 {
827                         SetPlayerColors(pl, COLOR_TEAM3 - 1);
828                 }
829                 else if(smallest == 4)
830                 {
831                         SetPlayerColors(pl, COLOR_TEAM4 - 1);
832                 }
833                 else
834                 {
835                         error("smallest team: invalid team\n");
836                 }
837
838                 LogTeamchange(pl.playerid, pl.team, 2); // log auto join
839
840                 if(pl.deadflag == DEAD_NO)
841                         Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
842         }
843
844         return smallest;
845 }
846
847 //void() ctf_playerchanged;
848 void SV_ChangeTeam(float _color)
849 {
850         float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
851
852         // in normal deathmatch we can just apply the color and we're done
853         if(!teamplay) {
854                 SetPlayerColors(self, _color);
855                 return;
856         }
857
858         scolor = self.clientcolors & 0x0F;
859         dcolor = _color & 0x0F;
860
861         if(scolor == COLOR_TEAM1 - 1)
862                 steam = 1;
863         else if(scolor == COLOR_TEAM2 - 1)
864                 steam = 2;
865         else if(scolor == COLOR_TEAM3 - 1)
866                 steam = 3;
867         else // if(scolor == COLOR_TEAM4 - 1)
868                 steam = 4;
869         if(dcolor == COLOR_TEAM1 - 1)
870                 dteam = 1;
871         else if(dcolor == COLOR_TEAM2 - 1)
872                 dteam = 2;
873         else if(dcolor == COLOR_TEAM3 - 1)
874                 dteam = 3;
875         else // if(dcolor == COLOR_TEAM4 - 1)
876                 dteam = 4;
877
878         CheckAllowedTeams(self);
879
880         if(dteam == 1 && c1 < 0) dteam = 4;
881         if(dteam == 4 && c4 < 0) dteam = 3;
882         if(dteam == 3 && c3 < 0) dteam = 2;
883         if(dteam == 2 && c2 < 0) dteam = 1;
884
885         // not changing teams
886         if(scolor == dcolor)
887         {
888                 //bprint("same team change\n");
889                 SetPlayerTeam(self, dteam, steam, TRUE);
890                 return;
891         }
892
893         if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && self.wasplayer)) {
894                 sprint(self, "Team changes not allowed\n");
895                 return; // changing teams is not allowed
896         }
897
898         // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
899         if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
900         {
901                 GetTeamCounts(self);
902                 if(!TeamSmallerEqThanTeam(dteam, steam, self))
903                 {
904                         sprint(self, "Cannot change to a larger/better/shinier team\n");
905                         return;
906                 }
907         }
908
909 //      bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
910
911         if(self.classname == "player" && steam != dteam)
912         {
913                 // reduce frags during a team change
914                 TeamchangeFrags(self);
915         }
916
917         SetPlayerTeam(self, dteam, steam, FALSE);
918
919         if(self.classname == "player" && steam != dteam)
920         {
921                 // kill player when changing teams
922                 if(self.deadflag == DEAD_NO)
923                         Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
924         }
925 }
926
927 void ShufflePlayerOutOfTeam (float source_team)
928 {
929         float smallestteam, smallestteam_count, steam;
930         float lowest_bot_score, lowest_player_score;
931         entity head, lowest_bot, lowest_player, selected;
932
933         smallestteam = 0;
934         smallestteam_count = 999999999;
935
936         if(c1 >= 0 && c1 < smallestteam_count)
937         {
938                 smallestteam = 1;
939                 smallestteam_count = c1;
940         }
941         if(c2 >= 0 && c2 < smallestteam_count)
942         {
943                 smallestteam = 2;
944                 smallestteam_count = c2;
945         }
946         if(c3 >= 0 && c3 < smallestteam_count)
947         {
948                 smallestteam = 3;
949                 smallestteam_count = c3;
950         }
951         if(c4 >= 0 && c4 < smallestteam_count)
952         {
953                 smallestteam = 4;
954                 smallestteam_count = c4;
955         }
956
957         if(!smallestteam)
958         {
959                 bprint("warning: no smallest team\n");
960                 return;
961         }
962
963         if(source_team == 1)
964                 steam = COLOR_TEAM1;
965         else if(source_team == 2)
966                 steam = COLOR_TEAM2;
967         else if(source_team == 3)
968                 steam = COLOR_TEAM3;
969         else // if(source_team == 4)
970                 steam = COLOR_TEAM4;
971
972         lowest_bot = world;
973         lowest_bot_score = 999999999;
974         lowest_player = world;
975         lowest_player_score = 999999999;
976
977         // find the lowest-scoring player & bot of that team
978         FOR_EACH_PLAYER(head)
979         {
980                 if(head.team == steam)
981                 {
982                         if(head.isbot)
983                         {
984                                 if(head.totalfrags < lowest_bot_score)
985                                 {
986                                         lowest_bot = head;
987                                         lowest_bot_score = head.totalfrags;
988                                 }
989                         }
990                         else
991                         {
992                                 if(head.totalfrags < lowest_player_score)
993                                 {
994                                         lowest_player = head;
995                                         lowest_player_score = head.totalfrags;
996                                 }
997                         }
998                 }
999         }
1000
1001         // prefers to move a bot...
1002         if(lowest_bot != world)
1003                 selected = lowest_bot;
1004         // but it will move a player if it has to
1005         else
1006                 selected = lowest_player;
1007         // don't do anything if it couldn't find anyone
1008         if(!selected)
1009         {
1010                 bprint("warning: couldn't find a player to move from team\n");
1011                 return;
1012         }
1013
1014         // smallest team gains a member
1015         if(smallestteam == 1)
1016         {
1017                 c1 = c1 + 1;
1018         }
1019         else if(smallestteam == 2)
1020         {
1021                 c2 = c2 + 1;
1022         }
1023         else if(smallestteam == 3)
1024         {
1025                 c3 = c3 + 1;
1026         }
1027         else if(smallestteam == 4)
1028         {
1029                 c4 = c4 + 1;
1030         }
1031         else
1032         {
1033                 bprint("warning: destination team invalid\n");
1034                 return;
1035         }
1036         // source team loses a member
1037         if(source_team == 1)
1038         {
1039                 c1 = c1 + 1;
1040         }
1041         else if(source_team == 2)
1042         {
1043                 c2 = c2 + 2;
1044         }
1045         else if(source_team == 3)
1046         {
1047                 c3 = c3 + 3;
1048         }
1049         else if(source_team == 4)
1050         {
1051                 c4 = c4 + 4;
1052         }
1053         else
1054         {
1055                 bprint("warning: source team invalid\n");
1056                 return;
1057         }
1058
1059         // move the player to the new team
1060         TeamchangeFrags(selected);
1061         SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1062
1063         if(selected.deadflag == DEAD_NO)
1064                 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1065         centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1066 }
1067
1068 // code from here on is just to support maps that don't have team entities
1069 void tdm_spawnteam (string teamname, float teamcolor)
1070 {
1071         entity e;
1072         e = spawn();
1073         e.classname = "tdm_team";
1074         e.netname = teamname;
1075         e.cnt = teamcolor;
1076         e.team = e.cnt + 1;
1077 }
1078
1079 // spawn some default teams if the map is not set up for tdm
1080 void tdm_spawnteams()
1081 {
1082         float numteams;
1083
1084         numteams = autocvar_g_tdm_teams_override;
1085         if(numteams < 2)
1086                 numteams = autocvar_g_tdm_teams;
1087         numteams = bound(2, numteams, 4);
1088
1089         tdm_spawnteam("Red", COLOR_TEAM1-1);
1090         tdm_spawnteam("Blue", COLOR_TEAM2-1);
1091         if(numteams >= 3)
1092                 tdm_spawnteam("Yellow", COLOR_TEAM3-1);
1093         if(numteams >= 4)
1094                 tdm_spawnteam("Pink", COLOR_TEAM4-1);
1095 }
1096
1097 void tdm_delayedinit()
1098 {
1099         // if no teams are found, spawn defaults
1100         if (find(world, classname, "tdm_team") == world)
1101                 tdm_spawnteams();
1102 }
1103
1104 void tdm_init()
1105 {
1106         InitializeEntity(world, tdm_delayedinit, INITPRIO_GAMETYPE);
1107 }