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