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