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