]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/teamplay.qc
new cvar bot_vs_human: set to positive value to make an all-bot blue team, set to...
[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;
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_tdm = cvar("g_tdm");
327         g_keyhunt = cvar("g_keyhunt");
328         g_onslaught = cvar("g_onslaught");
329 }
330
331 string GetClientVersionMessage(float v) {
332         local string versionmsg;
333         if (v == 1) {
334                 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";
335                 // either that or someone wants to be funny
336         } else if (v != cvar("gameversion")) {
337                 if(v < cvar("gameversion")) {
338                         versionmsg = "^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8";
339                 } else {
340                         versionmsg = "^3This server is using an outdated Nexuiz version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8";
341                 }
342         } else {
343                 versionmsg = "^2client version and server version are compatible.^8";
344         }
345         return strzone(versionmsg);
346
347 }
348
349
350 void PrintWelcomeMessage(entity pl)
351 {
352         string s, mutator, modifications, padding;
353
354         /*if(self.welcomemessage_time > time)
355                 return;
356         self.welcomemessage_time = time + 0.8; */
357
358         if(self.cvar_scr_centertime == 0) return;
359         if(self.welcomemessage_time > time) return;
360         self.welcomemessage_time = time + self.cvar_scr_centertime * 0.6;
361
362         if(cvar("g_campaign"))
363         {
364                 centerprint(pl, campaign_message);
365                 return;
366         }
367
368         if(self.classname == "observer")
369         {
370                 if(cvar("g_lms") && self.frags <= 0 && self.frags > -666)
371                         return centerprint(self, strcat(NEWLINES, "^1You have no more lives left\nwait for next round\n\n\n^7press attack to spectate other players"));
372                 else if(cvar("g_lms") && self.frags == -666)
373                         return centerprint(self, strcat(NEWLINES, "^1Match has already begun\nwait for next round\n\n\n^7press attack to spectate other players"));
374         }
375         else if(self.classname == "spectator")
376         {
377                 if ((cvar("g_lms") && self.frags < 1) || cvar("g_arena"))
378                         return centerprint(self, strcat(NEWLINES, "spectating ", self.enemy.netname, "\n\n\n^7press attack for next player\npress attack2 for free fly mode"));
379                 else
380                         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"));
381         }
382
383
384         if(cvar("g_minstagib"))
385                 mutator = "^2Minstagib ^1";
386         else if(cvar("g_instagib"))
387                 mutator = "^2Instagib ^1";
388         else if(cvar("g_rocketarena"))
389                 mutator = "^2Rocketarena ^1";
390         else if(cvar("g_nixnex"))
391                 mutator = "^2No Items Nexuiz ^1";
392
393         if(cvar("g_midair")) {
394                 // to protect against unheedingly made changes
395                 if (modifications) {
396                         modifications = strcat(modifications, ", ");
397                 }
398                 modifications = "midair";
399         }
400         if(cvar("g_vampire")) {
401                 if (modifications) {
402                         modifications = strcat(modifications, ", ");
403                 }
404                 modifications = strcat(modifications, "vampire");
405         }
406         if(cvar("g_laserguided_missile")) {
407                 if (modifications) {
408                         modifications = strcat(modifications, ", ");
409                 }
410                 modifications = strcat(modifications, "laser-guided-missiles");
411         }
412
413         local string versionmessage;
414         versionmessage = GetClientVersionMessage(self.version);
415
416         s = strcat(s, NEWLINES, "This is Nexuiz ", cvar_string("g_nexuizversion"), "\n", versionmessage);
417         s = strcat(s, "^8\n\nmatch type is ^1", mutator, gamemode_name, "^8\n");
418
419         if(modifications != "")
420                 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
421
422         if((self.classname == "observer" || self.classname == "spectator") && self.version == cvar("gameversion")) {
423                 if(!cvar("g_arena"))
424                         s = strcat(s,"^7\n\n\npress jump to play\npress attack to spectate other players\n\n");
425                 else if(player_count < 2 && arena_roundbased)
426                 {
427                         s = strcat(s, "\n\n\n^1waiting for second player to start match^7\n\n");
428                 }
429                 else
430                 {
431                         s = strcat(s, "\n\n\n");
432                         if(champion)
433                                 s = strcat(s, "^7current champion is: ", champion.netname, "\n\n");
434                         s = strcat(s,"^7press attack to spectate other players\n\n");
435                 }
436         }
437
438
439         s = strzone(s);
440
441         if (cvar("g_grappling_hook"))
442                 s = strcat(s, "\n\n^8grappling hook is enabled, press 'e' to use it\n");
443
444         if (cvar_string("_mutatormsg") != "") {
445                 s = strcat(s, "\n\n^8special gameplay tips: ^7", cvar_string("_mutatormsg"));
446         }
447
448         if (cvar_string("_motd") != "") {
449                 s = strcat(s, "\n\n^8MOTD: ^7", cvar_string("_motd"));
450         }
451
452         s = strcat(s, "\n");
453         if(cvar("fraglimit"))
454         {
455                 padding = "";
456                 if(cvar("timelimit"))
457                         padding = "        ";
458                         //        " minutes"
459                 s = strcat(s, "\n^8frag limit: ^7", cvar_string("fraglimit"), padding);
460         }
461         if(cvar("timelimit"))
462                 s = strcat(s, "\n^8time limit: ^7", cvar_string("timelimit"), " minutes");
463
464         s = strzone(s);
465
466         centerprint(pl, s);
467         //sprint(pl, s);
468
469         strunzone(s);
470 }
471
472
473 void SetPlayerColors(entity pl, float _color)
474 {
475         /*string s;
476         s = ftos(cl);
477         stuffcmd(pl, strcat("color ", s, " ", s, "\n")  );
478         pl.team = cl + 1;
479         //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
480         pl.clientcolors = 16*cl + cl;*/
481
482         float pants, shirt;
483         pants = _color & 0x0F;
484         shirt = _color & 0xF0;
485
486
487         if(teamplay) {
488                 setcolor(pl, 16*pants + pants);
489         } else {
490                 setcolor(pl, shirt + pants);
491         }
492 }
493
494 void SetPlayerTeam(entity pl, float t, float s, float noprint)
495 {
496         float _color;
497
498         if(t == 4)
499                 _color = COLOR_TEAM4 - 1;
500         else if(t == 3)
501                 _color = COLOR_TEAM3 - 1;
502         else if(t == 2)
503                 _color = COLOR_TEAM2 - 1;
504         else
505                 _color = COLOR_TEAM1 - 1;
506
507         SetPlayerColors(pl,_color);
508
509         if(!noprint && t != s)
510         {
511                 //bprint(pl.netname, " has changed to ", TeamNoName(t), "\n");
512                 bprint(pl.netname, "^7 has changed from ", TeamNoName(s), " to ", TeamNoName(t), "\n");
513         }
514
515         if(t != s)
516                 LogTeamchange(pl);
517 }
518
519
520
521
522
523
524 // set c1...c4 to show what teams are allowed
525 void CheckAllowedTeams (entity for_whom)
526 {
527         string teament_name;
528         float dm;
529         entity head;
530
531 //      if(!dom && !ctf)
532 //              dm = 1;
533
534         c1 = c2 = c3 = c4 = -1;
535         cb1 = cb2 = cb3 = cb4 = 0;
536
537         // onslaught is special
538         if(g_onslaught)
539         {
540                 head = findchain(classname, "onslaught_generator");
541                 while (head)
542                 {
543                         if (head.team == COLOR_TEAM1) c1 = 0;
544                         if (head.team == COLOR_TEAM2) c2 = 0;
545                         if (head.team == COLOR_TEAM3) c3 = 0;
546                         if (head.team == COLOR_TEAM4) c4 = 0;
547                         head = head.chain;
548                 }
549                 return;
550         }
551
552         if(g_domination)
553                 teament_name = "dom_team";
554         else if(g_ctf)
555                 teament_name = "ctf_team";
556         else if(g_tdm)
557                 teament_name = "tdm_team";
558         else
559         {
560                 // cover anything else by treating it like tdm with no teams spawned
561                 if(g_keyhunt)
562                         dm = kh_teams;
563                 else
564                         dm = cvar("g_tdm_teams");
565                 if(dm < 2)
566                         error("g_tdm_teams < 2, not enough teams to play team deathmatch\n");
567
568                 if(dm >= 4)
569                 {
570                         c1 = c2 = c3 = c4 = 0;
571                 }
572                 else if(dm >= 3)
573                 {
574                         c1 = c2 = c3 = 0;
575                 }
576                 else// if(dm >= 2)
577                 {
578                         c1 = c2 = 0;
579                 }
580                 return;
581         }
582
583         // first find out what teams are allowed
584         head = find(world, classname, teament_name);
585         while(head)
586         {
587                 if(!(g_domination && head.netname == ""))
588                 {
589                         if(head.team == COLOR_TEAM1)
590                         {
591                                 c1 = 0;
592                         }
593                         if(head.team == COLOR_TEAM2)
594                         {
595                                 c2 = 0;
596                         }
597                         if(head.team == COLOR_TEAM3)
598                         {
599                                 c3 = 0;
600                         }
601                         if(head.team == COLOR_TEAM4)
602                         {
603                                 c4 = 0;
604                         }
605                 }
606                 head = find(head, classname, teament_name);
607         }
608
609         if(cvar("bot_vs_human") > 0)
610         {
611                 // bots are all blue
612                 if(clienttype(for_whom) == CLIENTTYPE_BOT)
613                         c1 = c3 = c4 = -1;
614                 else
615                         c2 = -1;
616         }
617         else if(cvar("bot_vs_human") < 0)
618         {
619                 // bots are all red
620                 if(clienttype(for_whom) == CLIENTTYPE_BOT)
621                         c2 = c3 = c4 = -1;
622                 else
623                         c1 = -1;
624         }
625 }
626
627 float PlayerValue(entity p)
628 {
629         if(IsTeamBalanceForced() == 1)
630                 return 1;
631         return 1;
632 }
633
634 // c1...c4 should be set to -1 (not allowed) or 0 (allowed).
635 // teams that are allowed will now have their player counts stored in c1...c4
636 void GetTeamCounts(entity ignore)
637 {
638         entity head;
639         float value, bvalue;
640         // now count how many players are on each team already
641
642         // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
643         // also remember the lowest-scoring player
644
645         FOR_EACH_PLAYER(head)
646         {
647                 if(head != ignore)// && head.netname != "")
648                 {
649                         value = PlayerValue(head);
650                         if(clienttype(head) == CLIENTTYPE_BOT)
651                                 bvalue = value;
652                         else
653                                 bvalue = 0;
654                         if(head.team == COLOR_TEAM1)
655                         {
656                                 if(c1 >= 0)
657                                 {
658                                         c1 = c1 + value;
659                                         cb1 = cb1 + bvalue;
660                                 }
661                         }
662                         if(head.team == COLOR_TEAM2)
663                         {
664                                 if(c2 >= 0)
665                                 {
666                                         c2 = c2 + value;
667                                         cb2 = cb2 + bvalue;
668                                 }
669                         }
670                         if(head.team == COLOR_TEAM3)
671                         {
672                                 if(c3 >= 0)
673                                 {
674                                         c3 = c3 + value;
675                                         cb3 = cb3 + bvalue;
676                                 }
677                         }
678                         if(head.team == COLOR_TEAM4)
679                         {
680                                 if(c4 >= 0)
681                                 {
682                                         c4 = c4 + value;
683                                         cb4 = cb4 + bvalue;
684                                 }
685                         }
686                 }
687         }
688 }
689
690 // returns # of smallest team (1, 2, 3, 4)
691 // NOTE: Assumes CheckAllowedTeams has already been called!
692 float FindSmallestTeam(entity pl, float ignore_pl)
693 {
694         float totalteams, smallestteam, smallestteam_count, smallestteam_score, balance_type;
695         totalteams = 0;
696
697         // find out what teams are available
698         //CheckAllowedTeams();
699
700         // make sure there are at least 2 teams to join
701         if(c1 >= 0)
702                 totalteams = totalteams + 1;
703         if(c2 >= 0)
704                 totalteams = totalteams + 1;
705         if(c3 >= 0)
706                 totalteams = totalteams + 1;
707         if(c4 >= 0)
708                 totalteams = totalteams + 1;
709
710         if(cvar("bot_vs_human"))
711                 totalteams += 1;
712
713         if(totalteams <= 1)
714         {
715                 if(g_domination)
716                         error("Too few teams available for domination\n");
717                 else if(g_ctf)
718                         error("Too few teams available for ctf\n");
719                 else if(g_keyhunt)
720                         error("Too few teams available for key hunt\n");
721                 else
722                         error("Too few teams available for team deathmatch\n");
723         }
724
725
726         // count how many players are in each team
727         if(ignore_pl)
728                 GetTeamCounts(pl);
729         else
730                 GetTeamCounts(world);
731
732         // c1...c4 now have counts of each team
733         // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
734
735         smallestteam = 0;
736         smallestteam_count = 999999999;
737         smallestteam_score = 999999999;
738
739         // 2 gives priority to what team you're already on, 1 goes in order
740         // 2 doesn't seem to work though...
741         balance_type = 1;
742
743         if(bots_would_leave)
744         //if(pl.classname != "player")
745         if(clienttype(pl) != CLIENTTYPE_BOT)
746         {
747                 c1 -= cb1 * 255.0/256;
748                 c2 -= cb2 * 255.0/256;
749                 c3 -= cb3 * 255.0/256;
750                 c4 -= cb4 * 255.0/256;
751         }
752
753         if(balance_type == 1)
754         {
755                 if(c1 >= 0 && (c1 < smallestteam_count || (c1 <= smallestteam_count && team1_score < smallestteam_score)))
756                 {
757                         smallestteam = 1;
758                         smallestteam_count = c1;
759                         smallestteam_score = team1_score;
760                 }
761                 if(c2 >= 0 && (c2 < smallestteam_count || (c2 <= smallestteam_count && team2_score < smallestteam_score)))
762                 {
763                         smallestteam = 2;
764                         smallestteam_count = c2;
765                         smallestteam_score = team2_score;
766                 }
767                 if(c3 >= 0 && (c3 < smallestteam_count || (c3 <= smallestteam_count && team3_score < smallestteam_score)))
768                 {
769                         smallestteam = 3;
770                         smallestteam_count = c3;
771                         smallestteam_score = team3_score;
772                 }
773                 if(c4 >= 0 && (c4 < smallestteam_count || (c4 <= smallestteam_count && team4_score < smallestteam_score)))
774                 {
775                         smallestteam = 4;
776                         smallestteam_count = c4;
777                         smallestteam_score = team4_score;
778                 }
779         }
780         else
781         {
782                 if(c1 >= 0 && (c1 < smallestteam_count ||
783                                         (c1 == smallestteam_count && self.team == COLOR_TEAM1) ) )
784                 {
785                         smallestteam = 1;
786                         smallestteam_count = c1;
787                 }
788                 if(c2 >= 0 && c2 < (c2 < smallestteam_count ||
789                                         (c2 == smallestteam_count && self.team == COLOR_TEAM2) ) )
790                 {
791                         smallestteam = 2;
792                         smallestteam_count = c2;
793                 }
794                 if(c3 >= 0 && c3 < (c3 < smallestteam_count ||
795                                         (c3 == smallestteam_count && self.team == COLOR_TEAM3) ) )
796                 {
797                         smallestteam = 3;
798                         smallestteam_count = c3;
799                 }
800                 if(c4 >= 0 && c4 < (c4 < smallestteam_count ||
801                                         (c4 == smallestteam_count && self.team == COLOR_TEAM4) ) )
802                 {
803                         smallestteam = 4;
804                         smallestteam_count = c4;
805                 }
806         }
807
808         return smallestteam;
809 }
810
811 float JoinBestTeam(entity pl, float only_return_best)
812 {
813         float smallest, selectedteam;
814
815         // don't join a team if we're not playing a team game
816         if(!cvar("teamplay") && !g_domination && !g_ctf && !g_keyhunt)
817                 return 0;
818
819         // find out what teams are available
820         CheckAllowedTeams(pl);
821
822         if(cvar("g_domination"))
823         {
824                 if(cvar("g_domination_default_teams") < 3)
825                         c3 = 999999999;
826                 if(cvar("g_domination_default_teams") < 4)
827                         c4 = 999999999;
828         }
829
830         // if we don't care what team he ends up on, put him on whatever team he entered as.
831         // if he's not on a valid team, then let other code put him on the smallest team
832         if(!cvar("g_campaign") && !cvar("g_balance_teams") && !cvar("g_balance_teams_force"))
833         {
834                 if(     c1 >= 0 && pl.team == COLOR_TEAM1)
835                         selectedteam = pl.team;
836                 else if(c2 >= 0 && pl.team == COLOR_TEAM2)
837                         selectedteam = pl.team;
838                 else if(c3 >= 0 && pl.team == COLOR_TEAM3)
839                         selectedteam = pl.team;
840                 else if(c4 >= 0 && pl.team == COLOR_TEAM4)
841                         selectedteam = pl.team;
842                 else
843                         selectedteam = -1;
844                 if(selectedteam > 0)
845                 {
846                         if(!only_return_best)
847                         {
848                                 SetPlayerColors(pl, selectedteam - 1);
849                                 LogTeamchange(pl);
850                         }
851                         return selectedteam;
852                 }
853                 // otherwise end up on the smallest team (handled below)
854         }
855
856         smallest = FindSmallestTeam(pl, TRUE);
857
858
859         if(!only_return_best)
860         {
861                 TeamchangeFrags(self);
862                 if(smallest == 1)
863                 {
864                         SetPlayerColors(pl, COLOR_TEAM1 - 1);
865                 }
866                 else if(smallest == 2)
867                 {
868                         SetPlayerColors(pl, COLOR_TEAM2 - 1);
869                 }
870                 else if(smallest == 3)
871                 {
872                         SetPlayerColors(pl, COLOR_TEAM3 - 1);
873                 }
874                 else if(smallest == 4)
875                 {
876                         SetPlayerColors(pl, COLOR_TEAM4 - 1);
877                 }
878                 else
879                 {
880                         error("smallest team: invalid team\n");
881                 }
882                 LogTeamchange(pl);
883                 if(pl.deadflag == DEAD_NO)
884                         Damage(pl, pl, pl, 100000, DEATH_TEAMCHANGE, pl.origin, '0 0 0');
885         }
886
887         return smallest;
888 }
889
890
891 void SV_ChangeTeam(float _color)
892 {
893         float scolor, dcolor, steam, dteam, dbotcount, scount, dcount;
894
895         // in normal deathmatch we can just apply the color and we're done
896         if(!cvar("teamplay")) {
897                 SetPlayerColors(self, _color);
898                 return;
899         }
900
901         scolor = self.clientcolors & 0x0F;
902         dcolor = _color & 0x0F;
903
904         if(scolor == COLOR_TEAM1 - 1)
905                 steam = 1;
906         else if(scolor == COLOR_TEAM2 - 1)
907                 steam = 2;
908         else if(scolor == COLOR_TEAM3 - 1)
909                 steam = 3;
910         else if(scolor == COLOR_TEAM4 - 1)
911                 steam = 4;
912         if(dcolor == COLOR_TEAM1 - 1)
913                 dteam = 1;
914         else if(dcolor == COLOR_TEAM2 - 1)
915                 dteam = 2;
916         else if(dcolor == COLOR_TEAM3 - 1)
917                 dteam = 3;
918         else if(dcolor == COLOR_TEAM4 - 1)
919                 dteam = 4;
920
921         // remap invalid teams in dom & ctf
922         /*
923         if(cvar("g_ctf") && dteam == 3)
924                 dteam = 2;
925         else if(cvar("g_ctf") && dteam == 4)
926                 dteam = 1;
927         else if((cvar("g_domination") && cvar("g_domination_default_teams") < 3) || (cvar("g_tdm") && cvar("g_tdm_teams") < 3))
928         {
929                 if(dteam == 3)
930                         dteam = 2;
931                 else if(dteam == 4)
932                         dteam = 1;
933         }
934         else if((cvar("g_domination") && cvar("g_domination_default_teams") < 4) || (cvar("g_tdm") && cvar("g_tdm_teams") < 4))
935         {
936                 if(dteam == 4)
937                         dteam = 1;
938         }
939         */
940
941         CheckAllowedTeams(self);
942
943         if(dteam == 1 && c1 < 0) dteam = 4;
944         if(dteam == 4 && c4 < 0) dteam = 3;
945         if(dteam == 3 && c3 < 0) dteam = 2;
946         if(dteam == 2 && c2 < 0) dteam = 1;
947
948         // not changing teams
949         if(scolor == dcolor)
950         {
951                 //bprint("same team change\n");
952                 SetPlayerTeam(self, dteam, steam, TRUE);
953                 return;
954         }
955
956         if(cvar("teamplay"))
957         {
958                 if(cvar("g_campaign") || cvar("g_changeteam_banned"))
959                 {
960                         sprint(self, "Team changes not allowed\n");
961                         return; // changing teams is not allowed
962                 }
963
964                 if(!cvar("g_campaign") && cvar("g_balance_teams_prevent_imbalance"))
965                 {
966                         // only allow changing to a smaller or equal size team
967
968                         // find out what teams are available
969                         //CheckAllowedTeams();
970                         // count how many players on each team
971                         GetTeamCounts(world);
972
973                         // get desired team
974                         if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
975                         {
976                                 dcount = c1;
977                                 dbotcount = cb1;
978                         }
979                         else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
980                         {
981                                 dcount = c2;
982                                 dbotcount = cb2;
983                         }
984                         else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
985                         {
986                                 dcount = c3;
987                                 dbotcount = cb3;
988                         }
989                         else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
990                         {
991                                 dcount = c4;
992                                 dbotcount = cb4;
993                         }
994                         else
995                         {
996                                 sprint(self, "Cannot change to an invalid team\n");
997
998                                 return;
999                         }
1000
1001                         // get starting team
1002                         if(steam == 1)//scolor == COLOR_TEAM1 - 1)
1003                                 scount = c1;
1004                         else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
1005                                 scount = c2;
1006                         else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
1007                                 scount = c3;
1008                         else if(steam == 4)//scolor == COLOR_TEAM4 - 1)
1009                                 scount = c4;
1010
1011                         if(scount) // started at a valid, nonempty team
1012                         {
1013                                 // check if we're trying to change to a larger team that doens't have bots to swap with
1014                                 if(dcount >= scount && dbotcount <= 0)
1015                                 {
1016                                         sprint(self, "Cannot change to a larger team\n");
1017                                         return; // can't change to a larger team
1018                                 }
1019                         }
1020                 }
1021         }
1022
1023 //      bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
1024
1025         if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1026         {
1027                 // reduce frags during a team change
1028                 TeamchangeFrags(self);
1029         }
1030
1031         SetPlayerTeam(self, dteam, steam, FALSE);
1032
1033         if(cvar("teamplay") && self.classname == "player" && steam != dteam)
1034         {
1035                 // kill player when changing teams
1036                 if(self.deadflag == DEAD_NO)
1037                         Damage(self, self, self, 100000, DEATH_TEAMCHANGE, self.origin, '0 0 0');
1038         }
1039 }
1040
1041 void ShufflePlayerOutOfTeam (float source_team)
1042 {
1043         float smallestteam, smallestteam_count, steam;
1044         float lowest_bot_score, lowest_player_score;
1045         entity head, lowest_bot, lowest_player, selected;
1046
1047         smallestteam = 0;
1048         smallestteam_count = 999999999;
1049
1050         if(c1 >= 0 && c1 < smallestteam_count)
1051         {
1052                 smallestteam = 1;
1053                 smallestteam_count = c1;
1054         }
1055         if(c2 >= 0 && c2 < smallestteam_count)
1056         {
1057                 smallestteam = 2;
1058                 smallestteam_count = c2;
1059         }
1060         if(c3 >= 0 && c3 < smallestteam_count)
1061         {
1062                 smallestteam = 3;
1063                 smallestteam_count = c3;
1064         }
1065         if(c4 >= 0 && c4 < smallestteam_count)
1066         {
1067                 smallestteam = 4;
1068                 smallestteam_count = c4;
1069         }
1070
1071         if(!smallestteam)
1072         {
1073                 bprint("warning: no smallest team\n");
1074                 return;
1075         }
1076
1077         if(source_team == 1)
1078                 steam = COLOR_TEAM1;
1079         else if(source_team == 2)
1080                 steam = COLOR_TEAM2;
1081         else if(source_team == 3)
1082                 steam = COLOR_TEAM3;
1083         else if(source_team == 4)
1084                 steam = COLOR_TEAM4;
1085
1086         lowest_bot = world;
1087         lowest_bot_score = 999999999;
1088         lowest_player = world;
1089         lowest_player_score = 999999999;
1090
1091         // find the lowest-scoring player & bot of that team
1092         FOR_EACH_PLAYER(head)
1093         {
1094                 if(head.team == steam)
1095                 {
1096                         if(head.isbot)
1097                         {
1098                                 if(head.frags < lowest_bot_score)
1099                                 {
1100                                         lowest_bot = head;
1101                                         lowest_bot_score = head.frags;
1102                                 }
1103                         }
1104                         else
1105                         {
1106                                 if(head.frags < lowest_player_score)
1107                                 {
1108                                         lowest_player = head;
1109                                         lowest_player_score = head.frags;
1110                                 }
1111                         }
1112                 }
1113         }
1114
1115         // prefers to move a bot...
1116         if(lowest_bot != world)
1117                 selected = lowest_bot;
1118         // but it will move a player if it has to
1119         else
1120                 selected = lowest_player;
1121         // don't do anything if it couldn't find anyone
1122         if(!selected)
1123         {
1124                 bprint("warning: couldn't find a player to move from team\n");
1125                 return;
1126         }
1127
1128         // smallest team gains a member
1129         if(smallestteam == 1)
1130         {
1131                 c1 = c1 + 1;
1132         }
1133         else if(smallestteam == 2)
1134         {
1135                 c2 = c2 + 1;
1136         }
1137         else if(smallestteam == 3)
1138         {
1139                 c3 = c3 + 1;
1140         }
1141         else if(smallestteam == 4)
1142         {
1143                 c4 = c4 + 1;
1144         }
1145         else
1146         {
1147                 bprint("warning: destination team invalid\n");
1148                 return;
1149         }
1150         // source team loses a member
1151         if(source_team == 1)
1152         {
1153                 c1 = c1 + 1;
1154         }
1155         else if(source_team == 2)
1156         {
1157                 c2 = c2 + 2;
1158         }
1159         else if(source_team == 3)
1160         {
1161                 c3 = c3 + 3;
1162         }
1163         else if(source_team == 4)
1164         {
1165                 c4 = c4 + 4;
1166         }
1167         else
1168         {
1169                 bprint("warning: source team invalid\n");
1170                 return;
1171         }
1172
1173         // move the player to the new team
1174         TeamchangeFrags(selected);
1175         SetPlayerTeam(selected, smallestteam, source_team, FALSE);
1176
1177         if(selected.deadflag == DEAD_NO)
1178                 Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE, selected.origin, '0 0 0');
1179         centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
1180 }
1181
1182 float lastRebalanceInfo;
1183 void CauseRebalance(float source_team, float howmany_toomany)
1184 {
1185         float steam;
1186         entity head;
1187
1188         if(IsTeamBalanceForced() == 1)
1189         {
1190                 bprint("Rebalancing Teams\n");
1191                 ShufflePlayerOutOfTeam(source_team);
1192         }
1193         else
1194         {
1195                 if(howmany_toomany < cvar("g_balance_teams_complain"))
1196                         return;
1197                 if(time < lastRebalanceInfo + 90)
1198                         return;
1199                 lastRebalanceInfo = time;
1200                 if(source_team == 1)
1201                         steam = COLOR_TEAM1;
1202                 else if(source_team == 2)
1203                         steam = COLOR_TEAM2;
1204                 else if(source_team == 3)
1205                         steam = COLOR_TEAM3;
1206                 else if(source_team == 4)
1207                         steam = COLOR_TEAM4;
1208                 ServerConsoleEcho(strcat("Team ", ftos(source_team), " too large, complaining."), TRUE);
1209                 FOR_EACH_REALPLAYER(head)
1210                 {
1211                         if(head.team == steam)
1212                         {
1213                                 sprint(head, "\{1}\{13}^3SERVER NOTICE:^7 One of you please change teams!\n");
1214                                 centerprint_atprio(head, CENTERPRIO_REBALANCE, "^3SERVER NOTICE:\n\n^7Someone of you please change teams!");
1215                         }
1216                 }
1217         }
1218 }
1219
1220 // part of g_balance_teams_force
1221 // occasionally perform an audit of the teams to make
1222 // sure they're more or less balanced in player count.
1223 void AuditTeams()
1224 {
1225         float numplayers, numteams, smallest, toomany;
1226         float balance;
1227         balance = IsTeamBalanceForced();
1228         if(balance == 0)
1229                 return;
1230
1231         if(audit_teams_time > time)
1232                 return;
1233
1234         audit_teams_time = time + 4 + random();
1235
1236 //      bprint("Auditing teams\n");
1237
1238         CheckAllowedTeams(world);
1239         GetTeamCounts(world);
1240
1241
1242         numteams = numplayers = smallest = 0;
1243         if(c1 >= 0)
1244         {
1245                 numteams = numteams + 1;
1246                 numplayers = numplayers + c1;
1247                 smallest = c1;
1248         }
1249         if(c2 >= 0)
1250         {
1251                 numteams = numteams + 1;
1252                 numplayers = numplayers + c2;
1253                 if(c2 < smallest)
1254                         smallest = c2;
1255         }
1256         if(c3 >= 0)
1257         {
1258                 numteams = numteams + 1;
1259                 numplayers = numplayers + c3;
1260                 if(c3 < smallest)
1261                         smallest = c3;
1262         }
1263         if(c4 >= 0)
1264         {
1265                 numteams = numteams + 1;
1266                 numplayers = numplayers + c4;
1267                 if(c4 < smallest)
1268                         smallest = c4;
1269         }
1270
1271         if(numplayers <= 0)
1272                 return; // no players to move around
1273         if(numteams < 2)
1274                 return; // don't bother shuffling if for some reason there aren't any teams
1275
1276         toomany = smallest + 1;
1277
1278         if(c1 && c1 > toomany)
1279                 CauseRebalance(1, c1 - toomany);
1280         if(c2 && c2 > toomany)
1281                 CauseRebalance(2, c2 - toomany);
1282         if(c3 && c3 > toomany)
1283                 CauseRebalance(3, c3 - toomany);
1284         if(c4 && c4 > toomany)
1285                 CauseRebalance(4, c4 - toomany);
1286
1287         // if teams are still unbalanced, balance them further in the next audit,
1288         // which will happen sooner (keep doing rapid audits until things are in order)
1289         audit_teams_time = time + 0.7 + random()*0.3;
1290 }
1291
1292
1293
1294 /*void(entity e, float first) UpdateTeamScore =
1295 {
1296         clientno = e.FIXME;
1297         if(first)
1298         {
1299                 WriteByte (MSG_ALL, SVC_UPDATENAME);
1300                 WriteByte (MSG_ALL, clientno);
1301                 WriteString (MSG_ALL, e.netname);
1302
1303                 WriteByte (MSG_ALL, SVC_UPDATECOLORS);
1304                 WriteByte (MSG_ALL, clientno);
1305                 WriteByte (MSG_ALL, e.b_shirt * 16 + who.b_pants);
1306         }
1307
1308         WriteByte (MSG_ALL, SVC_UPDATEFRAGS);
1309         WriteByte (MSG_ALL, clientno);
1310         WriteShort (MSG_ALL, e.frags + 10000);
1311 };
1312
1313 */
1314
1315
1316 // code from here on is just to support maps that don't have team entities
1317 void tdm_spawnteam (string teamname, float teamcolor)
1318 {
1319         local entity e;
1320         e = spawn();
1321         e.classname = "tdm_team";
1322         e.netname = teamname;
1323         e.cnt = teamcolor;
1324         e.team = e.cnt + 1;
1325 };
1326
1327 // spawn some default teams if the map is not set up for tdm
1328 void() tdm_spawnteams =
1329 {
1330         float numteams;
1331
1332         numteams = cvar("g_tdm_teams");
1333
1334         tdm_spawnteam("Red", COLOR_TEAM1-1);
1335         tdm_spawnteam("Blue", COLOR_TEAM2-1);
1336 };
1337
1338 void() tdm_delayedinit =
1339 {
1340         self.think = SUB_Remove;
1341         self.nextthink = time;
1342         // if no teams are found, spawn defaults
1343         if (find(world, classname, "tdm_team") == world)
1344                 tdm_spawnteams();
1345 };
1346
1347 void() tdm_init =
1348 {
1349         local entity e;
1350         e = spawn();
1351         e.think = tdm_delayedinit;
1352         e.nextthink = time + 0.1;
1353 };
1354
1355