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