]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/teamplay.qc
minor Assault fixes
[divverent/nexuiz.git] / data / qcsrc / server / teamplay.qc
1 float GAME_DEATHMATCH           = 1;
2 float GAME_TEAM_DEATHMATCH      = 2;
3 float GAME_DOMINATION           = 3;
4 float GAME_CTF                  = 4;
5 float GAME_RUNEMATCH            = 5;
6 float GAME_LMS                  = 6;
7 float GAME_ARENA                = 7;
8 float GAME_KEYHUNT              = 8;
9 float GAME_ASSAULT              = 9;
10 float GAME_ONSLAUGHT    = 10;
11
12 // client counts for each team
13 float c1, c2, c3, c4;
14 // # of bots on those teams
15 float cb1, cb2, cb3, cb4;
16
17 float g_domination, g_ctf, g_tdm, g_keyhunt, g_onslaught, g_assault;
18
19 float audit_teams_time;
20
21 float() IsTeamBalanceForced = {
22         if(intermission_running)
23                 return 0; // no rebalancing whatsoever please
24         if(!cvar("teamplay"))
25                 return 0;
26         if(cvar("g_campaign"))
27                 return 0;
28         if(!cvar("g_balance_teams_force"))
29                 return -1;
30         return 1;
31 }
32
33 void TeamchangeFrags(entity e)
34 {
35         if(e.classname == "player")
36         {
37                 // reduce frags during a team change
38                 DistributeFragsAmongTeam(e, (e.clientcolors & 0x0F) + 1, 1 - cvar("g_changeteam_fragtransfer") / 100);
39         }
40 }
41
42 string TeamName(float t)
43 {
44         // fixme: Search for team entities and get their .netname's!
45         if(t == COLOR_TEAM1)
46                 return "Red Team";
47         if(t == COLOR_TEAM2)
48                 return "Blue Team";
49         if(t == COLOR_TEAM3)
50                 return "Yellow Team";
51         if(t == COLOR_TEAM4)
52                 return "Pink Team";
53         return "Neutral Team";
54 }
55 string ColoredTeamName(float t)
56 {
57         // fixme: Search for team entities and get their .netname's!
58         if(t == COLOR_TEAM1)
59                 return "^1Red Team^7";
60         if(t == COLOR_TEAM2)
61                 return "^4Blue Team^7";
62         if(t == COLOR_TEAM3)
63                 return "^3Yellow Team^7";
64         if(t == COLOR_TEAM4)
65                 return "^6Pink Team^7";
66         return "Neutral Team";
67 }
68 string TeamNoName(float t)
69 {
70         // fixme: Search for team entities and get their .netname's!
71         if(t == 1)
72                 return "Red Team";
73         if(t == 2)
74                 return "Blue Team";
75         if(t == 3)
76                 return "Yellow Team";
77         if(t == 4)
78                 return "Pink Team";
79         return "Neutral Team";
80 }
81
82 void dom_init();
83 void ctf_init();
84 void runematch_init();
85 void tdm_init();
86
87 void(entity pl) LogTeamchange =
88 {
89         string str;
90         if(!cvar("sv_eventlog"))
91                 return;
92         if(pl.playerid < 1)
93                 return;
94         str = strcat(":team:", ftos(pl.playerid), ":");
95         str = strcat(str, ftos(pl.team));
96         GameLogEcho(str, FALSE);
97 }
98
99 void ResetGameCvars()
100 {
101         cvar_set("g_tdm", "0");
102         cvar_set("g_domination", "0");
103         cvar_set("g_ctf", "0");
104         cvar_set("g_runematch", "0");
105         cvar_set("g_lms", "0");
106         cvar_set("g_arena", "0");
107         cvar_set("g_keyhunt", "0");
108         cvar_set("g_assault", "0");
109         cvar_set("g_onslaught", "0");
110         cvar_set("teamplay", "0");
111 }
112
113 void ActivateTeamplay()
114 {
115         float teamplay_default;
116         teamplay_default = cvar("teamplay_default");
117
118         if(teamplay_default)
119                 cvar_set("teamplay", ftos(teamplay_default));
120         else
121                 cvar_set("teamplay", "3");
122 }
123
124 void InitGameplayMode()
125 {
126         float fraglimit_override, timelimit_override;
127
128         VoteReset();
129
130         game = cvar ("gamecfg");        // load game options
131
132         // game cvars get reset before map changes
133         // then map's cfg sets them as desired
134
135         // FIXME: also set a message or game mode name to print to players when the join
136
137         // set both here, gamemode can override it later
138         timelimit_override = cvar("timelimit_override");
139         fraglimit_override = cvar("fraglimit_override");
140
141         if(game == GAME_DOMINATION || cvar("g_domination"))
142         {
143                 ResetGameCvars();
144                 game = GAME_DOMINATION;
145                 cvar_set("g_domination", "1");
146
147                 ActivateTeamplay();
148
149                 fraglimit_override = cvar("g_domination_point_limit");
150
151                 gamemode_name = "Domination";
152                 teams_matter = 1;
153         }
154         else if(game == GAME_CTF || cvar("g_ctf"))
155         {
156                 ResetGameCvars();
157                 game = GAME_CTF;
158                 cvar_set("g_ctf", "1");
159
160                 ActivateTeamplay();
161
162                 fraglimit_override = cvar("g_ctf_capture_limit");
163
164                 gamemode_name = "Capture the Flag";
165                 teams_matter = 1;
166         }
167         else if((game == GAME_RUNEMATCH || cvar("g_runematch")) && !cvar("g_minstagib"))
168         {
169                 game = GAME_RUNEMATCH;
170                 cvar_set("g_runematch", "1");
171
172                 if(cvar("deathmatch_force_teamplay"))
173                         ActivateTeamplay();
174
175                 fraglimit_override = cvar("g_runematch_point_limit");
176
177                 gamemode_name = "Rune Match";
178                 if(cvar("teamplay"))
179                         teams_matter = 1;
180                 else
181                         teams_matter = 0;
182         }
183         else if(game == GAME_DEATHMATCH || game == GAME_TEAM_DEATHMATCH || cvar("g_tdm"))
184         {
185                 if(!cvar("deathmatch"))
186                         cvar_set("deathmatch", "1");
187
188
189                 if(game == GAME_TEAM_DEATHMATCH || cvar("g_tdm") || cvar("deathmatch_force_teamplay"))
190                 {
191                         ResetGameCvars();
192                         game = GAME_TEAM_DEATHMATCH;
193                         gamemode_name = "Team Deathmatch";
194                         ActivateTeamplay();
195                         teams_matter = 1;
196                         cvar_set("g_tdm", "1");
197                 }
198                 else
199                 {
200                         ResetGameCvars();
201                         game = GAME_DEATHMATCH;
202                         gamemode_name = "Deathmatch";
203                         teams_matter = 0;
204                 }
205
206                 fraglimit_override = cvar("fraglimit_override");
207         }
208         else if(game == GAME_ASSAULT || cvar("g_assault"))
209         {
210                 ResetGameCvars();
211                 game = GAME_ASSAULT;
212                 gamemode_name = "Assault";
213                 ActivateTeamplay();
214                 teams_matter = 1;
215                 cvar_set("g_assault", "1");
216         }
217         else if(game == GAME_LMS || cvar("g_lms"))
218         {
219                 ResetGameCvars();
220                 game = GAME_LMS;
221                 cvar_set("g_lms", "1");
222                 fraglimit_override = cvar("g_lms_lives_override");
223                 if(fraglimit_override == 0)
224                         fraglimit_override = -1;
225                 gamemode_name = "Last Man Standing";
226                 teams_matter = 0;
227                 lms_lowest_lives = 999;
228         }
229         else if(game == GAME_ARENA || cvar("g_arena"))
230         {
231                 ResetGameCvars();
232                 game = GAME_ARENA;
233                 cvar_set("g_arena", "1");
234                 fraglimit_override = cvar("g_arena_point_limit");
235                 maxspawned = cvar("g_arena_maxspawned");
236                 if(maxspawned < 2)
237                         maxspawned = 2;
238                 arena_roundbased = cvar("g_arena_roundbased");
239                 gamemode_name = "Arena";
240                 teams_matter = 0;
241         }
242         else if(game == GAME_KEYHUNT || cvar("g_keyhunt"))
243         {
244                 ResetGameCvars();
245                 game = GAME_KEYHUNT;
246                 cvar_set("g_keyhunt", "1");
247                 fraglimit_override = cvar("g_keyhunt_point_limit");
248                 ActivateTeamplay();
249                 gamemode_name = "Key Hunt";
250                 teams_matter = 1;
251         }
252         else if(game == GAME_ONSLAUGHT || cvar("g_onslaught"))
253         {
254                 ResetGameCvars();
255                 game = GAME_ONSLAUGHT;
256                 cvar_set("g_onslaught", "1");
257
258                 ActivateTeamplay();
259
260                 gamemode_name = "Onslaught";
261                 teams_matter = 1;
262         }
263         else
264         {
265                 // we can only assume...
266                 ResetGameCvars();
267                 gamemode_name = "Deathmatch";
268                 teams_matter = 0;
269         }
270 /*      else if(game == GAME_TEAM_DEATHMATCH)
271         {
272                 if(!cvar("deathmatch"))
273                         cvar_set("deathmatch", "1");
274
275                 //if(!cvar("teamplay"))
276                 //      cvar_set("teamplay", "3");
277                 ActivateTeamplay();
278
279                 fraglimit_override = cvar("fraglimit_override");
280         }*/
281
282         // enforce the server's universal frag/time limits
283         if(!cvar("g_campaign"))
284         {
285                 if(fraglimit_override >= 0)
286                         cvar_set("fraglimit", ftos(fraglimit_override));
287                 if(timelimit_override >= 0)
288                         cvar_set("timelimit", ftos(timelimit_override));
289         }
290
291         if (game == GAME_DOMINATION)//cvar("g_domination"))
292                 dom_init();
293         else if (game == GAME_CTF)//cvar("g_ctf"))
294                 ctf_init();
295         else if (game == GAME_RUNEMATCH)//cvar("g_runematch"))
296                 runematch_init();
297         else if (game == GAME_TEAM_DEATHMATCH)//cvar("g_runematch"))
298                 tdm_init();
299         else if (game == GAME_KEYHUNT)//cvar("g_keyhunt"))
300                 kh_init();
301
302         // those mutators rule each other out
303         if(cvar("g_minstagib"))
304         {
305                 cvar_set("g_instagib", "0");
306                 cvar_set("g_rocketarena", "0");
307         }
308         if(cvar("g_instagib"))
309         {
310                 cvar_set("g_minstagib", "0");
311                 cvar_set("g_rocketarena", "0");
312         }
313         if(cvar("g_rocketarena"))
314         {
315                 cvar_set("g_instagib", "0");
316                 cvar_set("g_minstagib", "0");
317         }
318
319         registercvar("_motd", "");
320         registercvar("_mutatormsg", "");
321         cvar_set("_motd", wordwrap(cvar_string("sv_motd"), 50));
322         cvar_set("_mutatormsg", wordwrap(cvar_string("g_mutatormsg"), 50));
323
324         g_domination = cvar("g_domination");
325         g_ctf = cvar("g_ctf");
326         g_assault = cvar("g_assault");
327         g_tdm = cvar("g_tdm");
328         g_keyhunt = cvar("g_keyhunt");
329         g_onslaught = cvar("g_onslaught");
330 }
331
332 string GetClientVersionMessage(float v) {
333         local string versionmsg;
334         if (v == 1) {
335                 versionmsg = "^1client is too old to get versioninfo.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nUPDATE!!! (http://www.nexuiz.com)^8";
336                 // either that or someone wants to be funny
337         } else if (v != cvar("gameversion")) {
338                 if(v < cvar("gameversion")) {
339                         versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
340                 } else {
341                         versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
342                 }
343         } else {
344                 versionmsg = "^2client version and server version are compatible.^8";
345         }
346         return strzone(versionmsg);
347
348 }
349
350
351 void PrintWelcomeMessage(entity pl)
352 {
353         string s, mutator, modifications, padding;
354
355         /*if(self.welcomemessage_time > time)
356                 return;
357         self.welcomemessage_time = time + 0.8; */
358
359         if(self.cvar_scr_centertime == 0) return;
360         if(self.welcomemessage_time > time) return;
361         self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
362
363         if(cvar("g_campaign"))
364         {
365                 centerprint(pl, campaign_message);
366                 return;
367         }
368
369         if(self.classname == "observer")
370         {
371                 if(cvar("g_lms") && self.frags <= 0 && self.frags > -666)
372                         return centerprint(self, strcat(NEWLINES, "^1You have no more lives left\nwait for next round\n\n\n^7press attack to spectate other players"));
373                 else if(cvar("g_lms") && self.frags == -666)
374                         return centerprint(self, strcat(NEWLINES, "^1Match has already begun\nwait for next round\n\n\n^7press attack to spectate other players"));
375         }
376         else if(self.classname == "spectator")
377         {
378                 if ((cvar("g_lms") && self.frags < 1) || cvar("g_arena"))
379                         return centerprint(self, strcat(NEWLINES, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
380                 else
381                         return centerprint(self, strcat(NEWLINES, "spectating ", self.enemy.netname, "\n\n\n^7press jump to play\n^7press attack for next player\npress attack2 for free fly mode"));
382         }
383
384
385         if(cvar("g_minstagib"))
386                 mutator = "^2Minstagib ^1";
387         else if(cvar("g_instagib"))
388                 mutator = "^2Instagib ^1";
389         else if(cvar("g_rocketarena"))
390                 mutator = "^2Rocketarena ^1";
391         else if(cvar("g_nixnex"))
392                 mutator = "^2No Items Nexuiz ^1";
393
394         if(cvar("g_midair")) {
395                 // to protect against unheedingly made changes
396                 if (modifications) {
397                         modifications = strcat(modifications, ", ");
398                 }
399                 modifications = "midair";
400         }
401         if(cvar("g_vampire")) {
402                 if (modifications) {
403                         modifications = strcat(modifications, ", ");
404                 }
405                 modifications = strcat(modifications, "vampire");
406         }
407         if(cvar("g_laserguided_missile")) {
408                 if (modifications) {
409                         modifications = strcat(modifications, ", ");
410                 }
411                 modifications = strcat(modifications, "laser-guided-missiles");
412         }
413
414         local string versionmessage;
415         versionmessage = GetClientVersionMessage(self.version);
416
417         s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
418         s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
419
420         if(modifications != "")
421                 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
422
423         if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("gameversion")) {
424                 if(!cvar("g_arena"))
425                         s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
426                 else if(player_count < 2 && arena_roundbased)
427                 {
428                         s = strcat(s, "\n\n\n^1waiting for second player to start match^7\n\n");
429                 }
430                 else
431                 {
432                         s = strcat(s, "\n\n\n");
433                         if(champion)
434                                 s = strcat(s, "^7current champion is: ", champion.netname, "\n\n");
435                         s = strcat(s,"^7press attack to spectate other players\n\n");
436                 }
437         }
438
439
440         s = strzone(s);
441
442         if (cvar("g_grappling_hook"))
443                 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
444
445         if (cvar_string("_mutatormsg") != "") {
446                 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
447         }
448
449         if (cvar_string("_motd") != "") {
450                 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
451         }
452
453         s = strcat(s, "\n");
454         if(cvar("fraglimit"))
455         {
456                 padding = "";
457                 if(cvar("timelimit"))
458                         padding = "        ";
459                         //        " minutes"
460                 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
461         }
462         if(cvar("timelimit"))
463                 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
464
465         s = strzone(s);
466
467         centerprint(pl, s);
468         //sprint(pl, s);
469
470         strunzone(s);
471 }
472
473
474 void SetPlayerColors(entity pl, float _color)
475 {
476         /*string s;
477         s = ftos(cl);
478         stuffcmd(pl, strcat("color ", s, " ", s, "\n")  );
479         pl.team = cl + 1;
480         //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
481         pl.clientcolors = 16*cl + cl;*/
482
483         float pants, shirt;
484         pants = _color & 0x0F;
485         shirt = _color & 0xF0;
486
487
488         if(teamplay) {
489                 setcolor(pl, 16*pants + pants);
490         } else {
491                 setcolor(pl, shirt + pants);
492         }
493 }
494
495 void SetPlayerTeam(entity pl, float t, float s, float noprint)
496 {
497         float _color;
498
499         if(t == 4)
500                 _color = COLOR_TEAM4 - 1;
501         else if(t == 3)
502                 _color = COLOR_TEAM3 - 1;
503         else if(t == 2)
504                 _color = COLOR_TEAM2 - 1;
505         else
506                 _color = COLOR_TEAM1 - 1;
507
508         SetPlayerColors(pl,_color);
509
510         if(!noprint && t != s)
511         {
512                 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
513                 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
514         }
515
516         if(t != s)
517                 LogTeamchange(pl);
518 }
519
520
521
522
523
524
525 // set c1...c4 to show what teams are allowed
526 void CheckAllowedTeams (entity for_whom)
527 {
528         string teament_name;
529         float dm;
530         entity head;
531
532 //      if(!dom && !ctf)
533 //              dm = 1;
534
535         c1 = c2 = c3 = c4 = -1;
536         cb1 = cb2 = cb3 = cb4 = 0;
537
538         // onslaught is special
539         if(g_onslaught)
540         {
541                 head = findchain(classname, "onslaught_generator");
542                 while (head)
543                 {
544                         if (head.team == COLOR_TEAM1) c1 = 0;
545                         if (head.team == COLOR_TEAM2) c2 = 0;
546                         if (head.team == COLOR_TEAM3) c3 = 0;
547                         if (head.team == COLOR_TEAM4) c4 = 0;
548                         head = head.chain;
549                 }
550                 return;
551         }
552
553         if(g_domination)
554                 teament_name = "dom_team";
555         else if(g_ctf)
556                 teament_name = "ctf_team";
557         else if(g_tdm)
558                 teament_name = "tdm_team";
559         else if(g_assault)
560         {
561                 c1 = c2 = 0; // Assault always has 2 teams
562                 return;
563         }
564         else
565         {
566                 // cover anything else by treating it like tdm with no teams spawned
567                 if(g_keyhunt)
568                         dm = kh_teams;
569                 else
570                         dm = cvar("g_tdm_teams");
571                 if(dm < 2)
572                         error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
573
574                 if(dm >= 4)
575                 {
576                         c1 = c2 = c3 = c4 = 0;
577                 }
578                 else if(dm >= 3)
579                 {
580                         c1 = c2 = c3 = 0;
581                 }
582                 else// if(dm >= 2)
583                 {
584                         c1 = c2 = 0;
585                 }
586                 return;
587         }
588
589         // first find out what teams are allowed
590         head = find(world, classname, teament_name);
591         while(head)
592         {
593                 if(!(g_domination && head.netname == ""))
594                 {
595                         if(head.team == COLOR_TEAM1)
596                         {
597                                 c1 = 0;
598                         }
599                         if(head.team == COLOR_TEAM2)
600                         {
601                                 c2 = 0;
602                         }
603                         if(head.team == COLOR_TEAM3)
604                         {
605                                 c3 = 0;
606                         }
607                         if(head.team == COLOR_TEAM4)
608                         {
609                                 c4 = 0;
610                         }
611                 }
612                 head = find(head, classname, teament_name);
613         }
614
615         if(cvar("bot_vs_human") > 0)
616         {
617                 // bots are all blue
618                 if(clienttype(for_whom) == CLIENTTYPE_BOT)
619                         c1 = c3 = c4 = -1;
620                 else
621                         c2 = -1;
622         }
623         else if(cvar("bot_vs_human") < 0)
624         {
625                 // bots are all red
626                 if(clienttype(for_whom) == CLIENTTYPE_BOT)
627                         c2 = c3 = c4 = -1;
628                 else
629                         c1 = -1;
630         }
631 }
632
633 float PlayerValue(entity p)
634 {
635         if(IsTeamBalanceForced() == 1)
636                 return 1;
637         return 1;
638 }
639
640 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
641 // teams that are allowed will now have their player counts stored in c1...c4
642 void GetTeamCounts(entity ignore)
643 {
644         entity head;
645         float value, bvalue;
646         // now count how many players are on each team already
647
648         // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
649         // also remember the lowest-scoring player
650
651         FOR_EACH_PLAYER(head)
652         {
653                 if(head != ignore)// && head.netname != "")
654                 {
655                         value = PlayerValue(head);
656                         if(clienttype(head) == CLIENTTYPE_BOT)
657                                 bvalue = value;
658                         else
659                                 bvalue = 0;
660                         if(head.team == COLOR_TEAM1)
661                         {
662                                 if(c1 >= 0)
663                                 {
664                                         c1 = c1 + value;
665                                         cb1 = cb1 + bvalue;
666                                 }
667                         }
668                         if(head.team == COLOR_TEAM2)
669                         {
670                                 if(c2 >= 0)
671                                 {
672                                         c2 = c2 + value;
673                                         cb2 = cb2 + bvalue;
674                                 }
675                         }
676                         if(head.team == COLOR_TEAM3)
677                         {
678                                 if(c3 >= 0)
679                                 {
680                                         c3 = c3 + value;
681                                         cb3 = cb3 + bvalue;
682                                 }
683                         }
684                         if(head.team == COLOR_TEAM4)
685                         {
686                                 if(c4 >= 0)
687                                 {
688                                         c4 = c4 + value;
689                                         cb4 = cb4 + bvalue;
690                                 }
691                         }
692                 }
693         }
694 }
695
696 // returns # of smallest team (1, 2, 3, 4)
697 // NOTE: Assumes CheckAllowedTeams has already been called!
698 float FindSmallestTeam(entity pl, float ignore_pl)
699 {
700         float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
701         totalteams = 0;
702
703         // find out what teams are available
704         //CheckAllowedTeams();
705
706         // make sure there are at least 2 teams to join
707         if(c1 >= 0)
708                 totalteams = totalteams + 1;
709         if(c2 >= 0)
710                 totalteams = totalteams + 1;
711         if(c3 >= 0)
712                 totalteams = totalteams + 1;
713         if(c4 >= 0)
714                 totalteams = totalteams + 1;
715
716         if(cvar("bot_vs_human"))
717                 totalteams += 1;
718
719         if(totalteams <= 1)
720         {
721                 if(g_domination)
722                         error("Too few teams available for domination\n");
723                 else if(g_ctf)
724                         error("Too few teams available for ctf\n");
725                 else if(g_keyhunt)
726                         error("Too few teams available for key hunt\n");
727                 else
728                         error("Too few teams available for team deathmatch\n");
729         }
730
731
732         // count how many players are in each team
733         if(ignore_pl)
734                 GetTeamCounts(pl);
735         else
736                 GetTeamCounts(world);
737
738         // c1...c4 now have counts of each team
739         // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
740
741         smallestteam = 0;
742         smallestteam_count = 999999999;
743         smallestteam_score = 999999999;
744
745         // 2 gives priority to what team you're already on, 1 goes in order
746         // 2 doesn't seem to work though...
747         balance_type = 1;
748
749         if(bots_would_leave)
750         //if(pl.classname != "player")
751         if(clienttype(pl) != CLIENTTYPE_BOT)
752         {
753                 c1 -= cb1 * 255.0/256;
754                 c2 -= cb2 * 255.0/256;
755                 c3 -= cb3 * 255.0/256;
756                 c4 -= cb4 * 255.0/256;
757         }
758
759         if(balance_type == 1)
760         {
761                 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
762                 {
763                         smallestteam = 1;
764                         smallestteam_count = c1;
765                         smallestteam_score = team1_score;
766                 }
767                 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
768                 {
769                         smallestteam = 2;
770                         smallestteam_count = c2;
771                         smallestteam_score = team2_score;
772                 }
773                 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
774                 {
775                         smallestteam = 3;
776                         smallestteam_count = c3;
777                         smallestteam_score = team3_score;
778                 }
779                 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
780                 {
781                         smallestteam = 4;
782                         smallestteam_count = c4;
783                         smallestteam_score = team4_score;
784                 }
785         }
786         else
787         {
788                 if(c1 >= 0 && (c1 < smallestteam_count ||
789                                         (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
790                 {
791                         smallestteam = 1;
792                         smallestteam_count = c1;
793                 }
794                 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
795                                         (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
796                 {
797                         smallestteam = 2;
798                         smallestteam_count = c2;
799                 }
800                 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
801                                         (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
802                 {
803                         smallestteam = 3;
804                         smallestteam_count = c3;
805                 }
806                 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
807                                         (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
808                 {
809                         smallestteam = 4;
810                         smallestteam_count = c4;
811                 }
812         }
813
814         return smallestteam;
815 }
816
817 float JoinBestTeam(entity pl, float only_return_best)
818 {
819         float smallest, selectedteam;
820
821         // don't join a team if we're not playing a team game
822         if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
823                 return 0;
824
825         // find out what teams are available
826         CheckAllowedTeams(pl);
827
828         if(cvar("g_domination"))
829         {
830                 if(cvar("g_domination_default_teams") < 3)
831                         c3 = 999999999;
832                 if(cvar("g_domination_default_teams") < 4)
833                         c4 = 999999999;
834         }
835
836         // if we don't care what team he ends up on, put him on whatever team he entered as.
837         // if he's not on a valid team, then let other code put him on the smallest team
838         if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
839         {
840                 if(     c1 >= 0 && pl.team == COLOR_TEAM1)
841                         selectedteam = pl.team;
842                 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
843                         selectedteam = pl.team;
844                 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
845                         selectedteam = pl.team;
846                 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
847                         selectedteam = pl.team;
848                 else
849                         selectedteam = -1;
850                 if(selectedteam > 0)
851                 {
852                         if(!only_return_best)
853                         {
854                                 SetPlayerColors(pl, selectedteam - 1);
855                                 LogTeamchange(pl);
856                         }
857                         return selectedteam;
858                 }
859                 // otherwise end up on the smallest team (handled below)
860         }
861
862         smallest = FindSmallestTeam(pl, TRUE);
863
864
865         if(!only_return_best)
866         {
867                 TeamchangeFrags(self);
868                 if(smallest == 1)
869                 {
870                         SetPlayerColors(pl, COLOR_TEAM1 - 1);
871                 }
872                 else if(smallest == 2)
873                 {
874                         SetPlayerColors(pl, COLOR_TEAM2 - 1);
875                 }
876                 else if(smallest == 3)
877                 {
878                         SetPlayerColors(pl, COLOR_TEAM3 - 1);
879                 }
880                 else if(smallest == 4)
881                 {
882                         SetPlayerColors(pl, COLOR_TEAM4 - 1);
883                 }
884                 else
885                 {
886                         error("smallest team: invalid team\n");
887                 }
888                 LogTeamchange(pl);
889                 if(pl.deadflag == DEAD_NO)
890                         Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
891         }
892
893         return smallest;
894 }
895
896
897 void SV_ChangeTeam(float _color)
898 {
899         float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
900
901         // in normal deathmatch we can just apply the color and we're done
902         if(!cvar("teamplay")) {
903                 SetPlayerColors(self, _color);
904                 return;
905         }
906
907         scolor = self.clientcolors & 0x0F;
908         dcolor = _color & 0x0F;
909
910         if(scolor == COLOR_TEAM1 - 1)
911                 steam = 1;
912         else if(scolor == COLOR_TEAM2 - 1)
913                 steam = 2;
914         else if(scolor == COLOR_TEAM3 - 1)
915                 steam = 3;
916         else if(scolor == COLOR_TEAM4 - 1)
917                 steam = 4;
918         if(dcolor == COLOR_TEAM1 - 1)
919                 dteam = 1;
920         else if(dcolor == COLOR_TEAM2 - 1)
921                 dteam = 2;
922         else if(dcolor == COLOR_TEAM3 - 1)
923                 dteam = 3;
924         else if(dcolor == COLOR_TEAM4 - 1)
925                 dteam = 4;
926
927         // remap invalid teams in dom & ctf
928         /*
929         if(cvar("g_ctf") && dteam == 3)
930                 dteam = 2;
931         else if(cvar("g_ctf") && dteam == 4)
932                 dteam = 1;
933         else if((cvar("g_domination") && cvar("g_domination_default_teams") < 3) || (cvar("g_tdm") && cvar("g_tdm_teams") < 3))
934         {
935                 if(dteam == 3)
936                         dteam = 2;
937                 else if(dteam == 4)
938                         dteam = 1;
939         }
940         else if((cvar("g_domination") && cvar("g_domination_default_teams") < 4) || (cvar("g_tdm") && cvar("g_tdm_teams") < 4))
941         {
942                 if(dteam == 4)
943                         dteam = 1;
944         }
945         */
946
947         CheckAllowedTeams(self);
948
949         if(dteam == 1 && c1 < 0) dteam = 4;
950         if(dteam == 4 && c4 < 0) dteam = 3;
951         if(dteam == 3 && c3 < 0) dteam = 2;
952         if(dteam == 2 && c2 < 0) dteam = 1;
953
954         // not changing teams
955         if(scolor == dcolor)
956         {
957                 //bprint("same team change\n");
958                 SetPlayerTeam(self, dteam, steam, TRUE);
959                 return;
960         }
961
962         if(cvar("teamplay"))
963         {
964                 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
965                 {
966                         sprint(self, "Team changes not allowed\n");
967                         return; // changing teams is not allowed
968                 }
969
970                 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
971                 {
972                         // only allow changing to a smaller or equal size team
973
974                         // find out what teams are available
975                         //CheckAllowedTeams();
976                         // count how many players on each team
977                         GetTeamCounts(world);
978
979                         // get desired team
980                         if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
981                         {
982                                 dcount = c1;
983                                 dbotcount = cb1;
984                         }
985                         else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
986                         {
987                                 dcount = c2;
988                                 dbotcount = cb2;
989                         }
990                         else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
991                         {
992                                 dcount = c3;
993                                 dbotcount = cb3;
994                         }
995                         else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
996                         {
997                                 dcount = c4;
998                                 dbotcount = cb4;
999                         }
1000                         else
1001                         {
1002                                 sprint(self, "Cannot change to an invalid team\n");
1003
1004                                 return;
1005                         }
1006
1007                         // get starting team
1008                         if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1009                                 scount = c1;
1010                         else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1011                                 scount = c2;
1012                         else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1013                                 scount = c3;
1014                         else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1015                                 scount = c4;
1016
1017                         if(scount) // started at a valid, nonempty team
1018                         {
1019                                 // check if we're trying to change to a larger team that doens't have bots to swap with
1020                                 if(dcount >= scount && dbotcount <= 0)
1021                                 {
1022                                         sprint(self, "Cannot change to a larger team\n");
1023                                         return; // can't change to a larger team
1024                                 }
1025                         }
1026                 }
1027         }
1028
1029 //      bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1030
1031         if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1032         {
1033                 // reduce frags during a team change
1034                 TeamchangeFrags(self);
1035         }
1036
1037         SetPlayerTeam(self, dteam, steam, FALSE);
1038
1039         if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1040         {
1041                 // kill player when changing teams
1042                 if(self.deadflag == DEAD_NO)
1043                         Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1044         }
1045 }
1046
1047 void ShufflePlayerOutOfTeam (float source_team)
1048 {
1049         float smallestteam, smallestteam_count, steam;
1050         float lowest_bot_score, lowest_player_score;
1051         entity head, lowest_bot, lowest_player, selected;
1052
1053         smallestteam = 0;
1054         smallestteam_count = 999999999;
1055
1056         if(c1 >= 0 && c1 < smallestteam_count)
1057         {
1058                 smallestteam = 1;
1059                 smallestteam_count = c1;
1060         }
1061         if(c2 >= 0 && c2 < smallestteam_count)
1062         {
1063                 smallestteam = 2;
1064                 smallestteam_count = c2;
1065         }
1066         if(c3 >= 0 && c3 < smallestteam_count)
1067         {
1068                 smallestteam = 3;
1069                 smallestteam_count = c3;
1070         }
1071         if(c4 >= 0 && c4 < smallestteam_count)
1072         {
1073                 smallestteam = 4;
1074                 smallestteam_count = c4;
1075         }
1076
1077         if(!smallestteam)
1078         {
1079                 bprint("warning: no smallest team\n");
1080                 return;
1081         }
1082
1083         if(source_team == 1)
1084                 steam = COLOR_TEAM1;
1085         else if(source_team == 2)
1086                 steam = COLOR_TEAM2;
1087         else if(source_team == 3)
1088                 steam = COLOR_TEAM3;
1089         else if(source_team == 4)
1090                 steam = COLOR_TEAM4;
1091
1092         lowest_bot = world;
1093         lowest_bot_score = 999999999;
1094         lowest_player = world;
1095         lowest_player_score = 999999999;
1096
1097         // find the lowest-scoring player & bot of that team
1098         FOR_EACH_PLAYER(head)
1099         {
1100                 if(head.team == steam)
1101                 {
1102                         if(head.isbot)
1103                         {
1104                                 if(head.frags < lowest_bot_score)
1105                                 {
1106                                         lowest_bot = head;
1107                                         lowest_bot_score = head.frags;
1108                                 }
1109                         }
1110                         else
1111                         {
1112                                 if(head.frags < lowest_player_score)
1113                                 {
1114                                         lowest_player = head;
1115                                         lowest_player_score = head.frags;
1116                                 }
1117                         }
1118                 }
1119         }
1120
1121         // prefers to move a bot...
1122         if(lowest_bot != world)
1123                 selected = lowest_bot;
1124         // but it will move a player if it has to
1125         else
1126                 selected = lowest_player;
1127         // don't do anything if it couldn't find anyone
1128         if(!selected)
1129         {
1130                 bprint("warning: couldn't find a player to move from team\n");
1131                 return;
1132         }
1133
1134         // smallest team gains a member
1135         if(smallestteam == 1)
1136         {
1137                 c1 = c1 + 1;
1138         }
1139         else if(smallestteam == 2)
1140         {
1141                 c2 = c2 + 1;
1142         }
1143         else if(smallestteam == 3)
1144         {
1145                 c3 = c3 + 1;
1146         }
1147         else if(smallestteam == 4)
1148         {
1149                 c4 = c4 + 1;
1150         }
1151         else
1152         {
1153                 bprint("warning: destination team invalid\n");
1154                 return;
1155         }
1156         // source team loses a member
1157         if(source_team == 1)
1158         {
1159                 c1 = c1 + 1;
1160         }
1161         else if(source_team == 2)
1162         {
1163                 c2 = c2 + 2;
1164         }
1165         else if(source_team == 3)
1166         {
1167                 c3 = c3 + 3;
1168         }
1169         else if(source_team == 4)
1170         {
1171                 c4 = c4 + 4;
1172         }
1173         else
1174         {
1175                 bprint("warning: source team invalid\n");
1176                 return;
1177         }
1178
1179         // move the player to the new team
1180         TeamchangeFrags(selected);
1181         SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1182
1183         if(selected.deadflag == DEAD_NO)
1184                 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1185         centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1186 }
1187
1188 float lastRebalanceInfo;
1189 void CauseRebalance(float source_team, float howmany_toomany)
1190 {
1191         float steam;
1192         entity head;
1193
1194         if(IsTeamBalanceForced() == 1)
1195         {
1196                 bprint("Rebalancing Teams\n");
1197                 ShufflePlayerOutOfTeam(source_team);
1198         }
1199         else
1200         {
1201                 if(howmany_toomany < cvar("g_balance_teams_complain"))
1202                         return;
1203                 if(time < lastRebalanceInfo + 90)
1204                         return;
1205                 lastRebalanceInfo = time;
1206                 if(source_team == 1)
1207                         steam = COLOR_TEAM1;
1208                 else if(source_team == 2)
1209                         steam = COLOR_TEAM2;
1210                 else if(source_team == 3)
1211                         steam = COLOR_TEAM3;
1212                 else if(source_team == 4)
1213                         steam = COLOR_TEAM4;
1214                 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1215                 FOR_EACH_REALPLAYER(head)
1216                 {
1217                         if(head.team == steam)
1218                         {
1219                                 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1220                                 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1221                         }
1222                 }
1223         }
1224 }
1225
1226 // part of g_balance_teams_force
1227 // occasionally perform an audit of the teams to make
1228 // sure they're more or less balanced in player count.
1229 void AuditTeams()
1230 {
1231         float numplayers, numteams, smallest, toomany;
1232         float balance;
1233         balance = IsTeamBalanceForced();
1234         if(balance == 0)
1235                 return;
1236
1237         if(audit_teams_time > time)
1238                 return;
1239
1240         audit_teams_time = time + 4 + random();
1241
1242 //      bprint("Auditing teams\n");
1243
1244         CheckAllowedTeams(world);
1245         GetTeamCounts(world);
1246
1247
1248         numteams = numplayers = smallest = 0;
1249         if(c1 >= 0)
1250         {
1251                 numteams = numteams + 1;
1252                 numplayers = numplayers + c1;
1253                 smallest = c1;
1254         }
1255         if(c2 >= 0)
1256         {
1257                 numteams = numteams + 1;
1258                 numplayers = numplayers + c2;
1259                 if(c2 < smallest)
1260                         smallest = c2;
1261         }
1262         if(c3 >= 0)
1263         {
1264                 numteams = numteams + 1;
1265                 numplayers = numplayers + c3;
1266                 if(c3 < smallest)
1267                         smallest = c3;
1268         }
1269         if(c4 >= 0)
1270         {
1271                 numteams = numteams + 1;
1272                 numplayers = numplayers + c4;
1273                 if(c4 < smallest)
1274                         smallest = c4;
1275         }
1276
1277         if(numplayers <= 0)
1278                 return; // no players to move around
1279         if(numteams < 2)
1280                 return; // don't bother shuffling if for some reason there aren't any teams
1281
1282         toomany = smallest + 1;
1283
1284         if(c1 && c1 > toomany)
1285                 CauseRebalance(1, c1 - toomany);
1286         if(c2 && c2 > toomany)
1287                 CauseRebalance(2, c2 - toomany);
1288         if(c3 && c3 > toomany)
1289                 CauseRebalance(3, c3 - toomany);
1290         if(c4 && c4 > toomany)
1291                 CauseRebalance(4, c4 - toomany);
1292
1293         // if teams are still unbalanced, balance them further in the next audit,
1294         // which will happen sooner (keep doing rapid audits until things are in order)
1295         audit_teams_time = time + 0.7 + random()*0.3;
1296 }
1297
1298
1299
1300 /*void(entity e, float first) UpdateTeamScore =
1301 {
1302         clientno = e.FIXME;
1303         if(first)
1304         {
1305                 WriteByte (MSG_ALL, SVC_UPDATENAME);
1306                 WriteByte (MSG_ALL, clientno);
1307                 WriteString (MSG_ALL, e.netname);
1308
1309                 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1310                 WriteByte (MSG_ALL, clientno);
1311                 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1312         }
1313
1314         WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1315         WriteByte (MSG_ALL, clientno);
1316         WriteShort (MSG_ALL, e.frags + 10000);
1317 };
1318
1319 */
1320
1321
1322 // code from here on is just to support maps that don't have team entities
1323 void tdm_spawnteam (string teamname, float teamcolor)
1324 {
1325         local entity e;
1326         e = spawn();
1327         e.classname = "tdm_team";
1328         e.netname = teamname;
1329         e.cnt = teamcolor;
1330         e.team = e.cnt + 1;
1331 };
1332
1333 // spawn some default teams if the map is not set up for tdm
1334 void() tdm_spawnteams =
1335 {
1336         float numteams;
1337
1338         numteams = cvar("g_tdm_teams");
1339
1340         tdm_spawnteam("Red", COLOR_TEAM1-1);
1341         tdm_spawnteam("Blue", COLOR_TEAM2-1);
1342 };
1343
1344 void() tdm_delayedinit =
1345 {
1346         self.think = SUB_Remove;
1347         self.nextthink = time;
1348         // if no teams are found, spawn defaults
1349         if (find(world, classname, "tdm_team") == world)
1350                 tdm_spawnteams();
1351 };
1352
1353 void() tdm_init =
1354 {
1355         local entity e;
1356         e = spawn();
1357         e.think = tdm_delayedinit;
1358         e.nextthink = time + 0.1;
1359 };
1360
1361