Merge branch 'master' into terencehill/ft_autorevive_progress
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / freezetag / sv_freezetag.qc
1 #include "sv_freezetag.qh"
2
3 #include <server/elimination.qh>
4 #include <server/resources.qh>
5
6 float autocvar_g_freezetag_frozen_maxtime;
7 float autocvar_g_freezetag_revive_clearspeed;
8 float autocvar_g_freezetag_round_timelimit;
9 //int autocvar_g_freezetag_teams;
10 int autocvar_g_freezetag_teams_override;
11 float autocvar_g_freezetag_warmup;
12
13 void freezetag_count_alive_players()
14 {
15         total_players = 0;
16         for (int i = 1; i <= NUM_TEAMS; ++i)
17         {
18                 Team_SetNumberOfAlivePlayers(Team_GetTeamFromIndex(i), 0);
19         }
20         FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it),
21         {
22                 ++total_players;
23                 if (GetResource(it, RES_HEALTH) < 1 || STAT(FROZEN, it) == FROZEN_NORMAL)
24                 {
25                         continue;
26                 }
27                 entity team_ = Entity_GetTeam(it);
28                 int num_alive = Team_GetNumberOfAlivePlayers(team_);
29                 ++num_alive;
30                 Team_SetNumberOfAlivePlayers(team_, num_alive);
31         });
32         FOREACH_CLIENT(IS_REAL_CLIENT(it),
33         {
34                 STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(
35                         1));
36                 STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(
37                         Team_GetTeamFromIndex(2));
38                 STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(
39                         Team_GetTeamFromIndex(3));
40                 STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(
41                         Team_GetTeamFromIndex(4));
42         });
43
44         eliminatedPlayers.SendFlags |= 1;
45 }
46
47 bool freezetag_CheckTeams()
48 {
49         static float prev_missing_teams_mask;
50         if (Team_GetNumberOfAliveTeams() == NumTeams(freezetag_teams))
51         {
52                 if(prev_missing_teams_mask > 0)
53                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
54                 prev_missing_teams_mask = -1;
55                 return true;
56         }
57         if(total_players == 0)
58         {
59                 if(prev_missing_teams_mask > 0)
60                         Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
61                 prev_missing_teams_mask = -1;
62                 return false;
63         }
64         int missing_teams_mask = 0;
65         for (int i = 1; i <= NUM_TEAMS; ++i)
66         {
67                 if ((freezetag_teams & Team_IndexToBit(i)) &&
68                         (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
69                 {
70                         missing_teams_mask |= Team_IndexToBit(i);
71                 }
72         }
73         if(prev_missing_teams_mask != missing_teams_mask)
74         {
75                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
76                 prev_missing_teams_mask = missing_teams_mask;
77         }
78         return false;
79 }
80
81 int freezetag_getWinnerTeam()
82 {
83         int winner_team = 0;
84         if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1)) >= 1)
85         {
86                 winner_team = NUM_TEAM_1;
87         }
88         for (int i = 2; i <= NUM_TEAMS; ++i)
89         {
90                 if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) >= 1)
91                 {
92                         if (winner_team != 0)
93                         {
94                                 return 0;
95                         }
96                         winner_team = Team_IndexToTeam(i);
97                 }
98         }
99         if (winner_team)
100         {
101                 return winner_team;
102         }
103         return -1; // no player left
104 }
105
106 void nades_Clear(entity);
107 void nades_GiveBonus(entity player, float score);
108
109 bool freezetag_CheckWinner()
110 {
111         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
112         {
113                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
114                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
115                 FOREACH_CLIENT(IS_PLAYER(it), {
116                         it.freezetag_frozen_timeout = 0;
117                         nades_Clear(it);
118                 });
119                 game_stopped = true;
120                 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
121                 return true;
122         }
123
124         if (Team_GetNumberOfAliveTeams() > 1)
125         {
126                 return false;
127         }
128
129         int winner_team = freezetag_getWinnerTeam();
130         if(winner_team > 0)
131         {
132                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
133                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
134                 TeamScore_AddToTeam(winner_team, ST_FT_ROUNDS, +1);
135         }
136         else if(winner_team == -1)
137         {
138                 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
139                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
140         }
141
142         FOREACH_CLIENT(IS_PLAYER(it), {
143                 it.freezetag_frozen_timeout = 0;
144                 nades_Clear(it);
145         });
146
147         game_stopped = true;
148         round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
149         return true;
150 }
151
152 entity freezetag_LastPlayerForTeam(entity this)
153 {
154         entity last_pl = NULL;
155         FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), {
156                 if (STAT(FROZEN, it) != FROZEN_NORMAL && GetResource(it, RES_HEALTH) >= 1)
157                 {
158                         if (!last_pl)
159                                 last_pl = it;
160                         else
161                                 return NULL;
162                 }
163         });
164         return last_pl;
165 }
166
167 void freezetag_LastPlayerForTeam_Notify(entity this)
168 {
169         if(round_handler_IsActive())
170         if(round_handler_IsRoundStarted())
171         {
172                 entity pl = freezetag_LastPlayerForTeam(this);
173                 if(pl)
174                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
175         }
176 }
177
178 void freezetag_Add_Score(entity targ, entity attacker)
179 {
180         if(attacker == targ)
181         {
182                 // you froze your own dumb targ
183                 // counted as "suicide" already
184                 GameRules_scoring_add(targ, SCORE, -1);
185         }
186         else if(IS_PLAYER(attacker))
187         {
188                 // got frozen by an enemy
189                 // counted as "kill" and "death" already
190                 GameRules_scoring_add(targ, SCORE, -1);
191                 GameRules_scoring_add(attacker, SCORE, +1);
192         }
193         // else nothing - got frozen by the game type rules themselves
194 }
195
196 // to be called when the player is frozen by freezetag (on death, spectator join etc), gives the score
197 void freezetag_Freeze(entity targ, entity attacker)
198 {
199         if(STAT(FROZEN, targ))
200                 return;
201
202         targ.freezetag_frozen_time = time;
203         if (autocvar_g_freezetag_revive_auto && autocvar_g_freezetag_frozen_maxtime > 0)
204                 targ.freezetag_frozen_timeout = time + autocvar_g_freezetag_frozen_maxtime;
205
206         Freeze(targ, 0, FROZEN_NORMAL, true);
207
208         freezetag_count_alive_players();
209
210         freezetag_Add_Score(targ, attacker);
211 }
212
213 bool freezetag_isEliminated(entity e)
214 {
215         if(IS_PLAYER(e) && (STAT(FROZEN, e) == FROZEN_NORMAL || IS_DEAD(e)))
216                 return true;
217         return false;
218 }
219
220
221 // ================
222 // Bot player logic
223 // ================
224
225 void(entity this) havocbot_role_ft_freeing;
226 void(entity this) havocbot_role_ft_offense;
227
228 void havocbot_goalrating_ft_freeplayers(entity this, float ratingscale, vector org, float sradius)
229 {
230         entity best_pl = NULL;
231         float best_dist2 = FLOAT_MAX;
232         FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), {
233                 if (STAT(FROZEN, it) == FROZEN_NORMAL)
234                 {
235                         if(vdist(it.origin - org, >, sradius))
236                                 continue;
237                         navigation_routerating(this, it, ratingscale, 2000);
238                 }
239                 else if (best_dist2
240                         && GetResource(it, RES_HEALTH) < GetResource(this, RES_HEALTH) + 30
241                         && vlen2(it.origin - org) < best_dist2)
242                 {
243                         // If teamate is not frozen still seek them out as fight better
244                         // in a group.
245                         best_dist2 = vlen2(it.origin - org);
246                         if (best_dist2 < 700 ** 2)
247                         {
248                                 best_pl = NULL;
249                                 best_dist2 = 0; // already close to a teammate
250                         }
251                         else
252                                 best_pl = it;
253                 }
254         });
255         if (best_pl)
256                 navigation_routerating(this, best_pl, ratingscale / 2, 2000);
257 }
258
259 void havocbot_role_ft_offense(entity this)
260 {
261         if(IS_DEAD(this))
262                 return;
263
264         if (!this.havocbot_role_timeout)
265                 this.havocbot_role_timeout = time + random() * 10 + 20;
266
267         // Count how many players on team are unfrozen.
268         int unfrozen = 0;
269         FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, this) && STAT(FROZEN, it) != FROZEN_NORMAL, {
270                 unfrozen++;
271         });
272
273         // If only one left on team or if role has timed out then start trying to free players.
274         if ((!unfrozen && STAT(FROZEN, this) != FROZEN_NORMAL) || time > this.havocbot_role_timeout)
275         {
276                 LOG_TRACE("changing role to freeing");
277                 this.havocbot_role = havocbot_role_ft_freeing;
278                 this.havocbot_role_timeout = 0;
279                 return;
280         }
281
282         if (navigation_goalrating_timeout(this))
283         {
284                 navigation_goalrating_start(this);
285                 havocbot_goalrating_items(this, 12000, this.origin, 10000);
286                 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
287                 havocbot_goalrating_ft_freeplayers(this, 9000, this.origin, 10000);
288                 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
289                 navigation_goalrating_end(this);
290
291                 navigation_goalrating_timeout_set(this);
292         }
293 }
294
295 void havocbot_role_ft_freeing(entity this)
296 {
297         if(IS_DEAD(this))
298                 return;
299
300         if (!this.havocbot_role_timeout)
301                 this.havocbot_role_timeout = time + random() * 10 + 20;
302
303         if (time > this.havocbot_role_timeout)
304         {
305                 LOG_TRACE("changing role to offense");
306                 this.havocbot_role = havocbot_role_ft_offense;
307                 this.havocbot_role_timeout = 0;
308                 return;
309         }
310
311         if (navigation_goalrating_timeout(this))
312         {
313                 navigation_goalrating_start(this);
314                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
315                 havocbot_goalrating_enemyplayers(this, 5000, this.origin, 10000);
316                 havocbot_goalrating_ft_freeplayers(this, 20000, this.origin, 10000);
317                 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
318                 navigation_goalrating_end(this);
319
320                 navigation_goalrating_timeout_set(this);
321         }
322 }
323
324
325 // ==============
326 // Hook Functions
327 // ==============
328
329 void ft_RemovePlayer(entity this)
330 {
331         if (STAT(FROZEN, this) != FROZEN_NORMAL)
332                 freezetag_LastPlayerForTeam_Notify(this);
333         Unfreeze(this, false);
334
335         SetResourceExplicit(this, RES_HEALTH, 0); // neccessary to correctly count alive players
336         freezetag_count_alive_players();
337 }
338
339 MUTATOR_HOOKFUNCTION(ft, ClientDisconnect)
340 {
341         entity player = M_ARGV(0, entity);
342
343         ft_RemovePlayer(player);
344         return true;
345 }
346
347 MUTATOR_HOOKFUNCTION(ft, MakePlayerObserver)
348 {
349         entity player = M_ARGV(0, entity);
350
351         ft_RemovePlayer(player);
352 }
353
354 MUTATOR_HOOKFUNCTION(ft, PlayerDies)
355 {
356         entity frag_attacker = M_ARGV(1, entity);
357         entity frag_target = M_ARGV(2, entity);
358         float frag_deathtype = M_ARGV(3, float);
359
360         if(round_handler_IsActive())
361         if(round_handler_CountdownRunning())
362         {
363                 if (STAT(FROZEN, frag_target) == FROZEN_NORMAL)
364                         Unfreeze(frag_target, true);
365                 freezetag_count_alive_players();
366                 frag_target.respawn_time = time;
367                 frag_target.respawn_flags |= RESPAWN_FORCE;
368                 return true;
369         }
370
371         frag_target.respawn_time = time + 1;
372         frag_target.respawn_flags |= RESPAWN_FORCE;
373
374         // Cases DEATH_TEAMCHANGE and DEATH_AUTOTEAMCHANGE are needed to fix a bug whe
375         // you succeed changing team through the menu: you both really die (gibbing) and get frozen
376         if(ITEM_DAMAGE_NEEDKILL(frag_deathtype)
377                 || frag_deathtype == DEATH_TEAMCHANGE.m_id || frag_deathtype == DEATH_AUTOTEAMCHANGE.m_id)
378         {
379                 // let the player die, he will be automatically frozen when he respawns
380                 if (STAT(FROZEN, frag_target) != FROZEN_NORMAL)
381                 {
382                         freezetag_Add_Score(frag_target, frag_attacker);
383                         freezetag_count_alive_players();
384                         freezetag_LastPlayerForTeam_Notify(frag_target);
385                 }
386                 else
387                         Unfreeze(frag_target, false); // remove ice
388                 frag_target.freezetag_frozen_timeout = -2; // freeze on respawn
389                 return true;
390         }
391
392         if (STAT(FROZEN, frag_target) == FROZEN_NORMAL)
393                 return true;
394
395         freezetag_Freeze(frag_target, frag_attacker);
396         freezetag_LastPlayerForTeam_Notify(frag_target);
397
398         if(frag_attacker == frag_target || frag_attacker == NULL)
399         {
400                 if(IS_PLAYER(frag_target))
401                         Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_SELF);
402                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_SELF, frag_target.netname);
403         }
404         else
405         {
406                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_FREEZE, frag_target.netname, frag_attacker.netname);
407         }
408
409         return true;
410 }
411
412 MUTATOR_HOOKFUNCTION(ft, PlayerSpawn)
413 {
414         entity player = M_ARGV(0, entity);
415
416         if(player.freezetag_frozen_timeout == -1) // if PlayerSpawn is called by reset_map_players
417                 return true; // do nothing, round is starting right now
418
419         if(player.freezetag_frozen_timeout == -2) // player was dead
420         {
421                 freezetag_Freeze(player, NULL);
422                 return true;
423         }
424
425         freezetag_count_alive_players();
426
427         if(round_handler_IsActive())
428         if(round_handler_IsRoundStarted())
429         {
430                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_SPAWN_LATE);
431                 freezetag_Freeze(player, NULL);
432         }
433
434         return true;
435 }
436
437 MUTATOR_HOOKFUNCTION(ft, reset_map_players)
438 {
439         FOREACH_CLIENT(IS_PLAYER(it), {
440                 CS(it).killcount = 0;
441                 it.freezetag_frozen_timeout = -1;
442                 PutClientInServer(it);
443                 it.freezetag_frozen_timeout = 0;
444         });
445         freezetag_count_alive_players();
446         return true;
447 }
448
449 MUTATOR_HOOKFUNCTION(ft, GiveFragsForKill, CBC_ORDER_FIRST)
450 {
451         M_ARGV(2, float) = 0; // no frags counted in Freeze Tag
452         return true;
453 }
454
455 MUTATOR_HOOKFUNCTION(ft, Unfreeze)
456 {
457         entity targ = M_ARGV(0, entity);
458         targ.freezetag_frozen_time = 0;
459         targ.freezetag_frozen_timeout = 0;
460 }
461
462 MUTATOR_HOOKFUNCTION(ft, Damage_Calculate)
463 {
464         entity frag_attacker = M_ARGV(1, entity);
465         entity frag_target = M_ARGV(2, entity);
466         //float frag_deathtype = M_ARGV(3, float);
467         //float frag_damage = M_ARGV(4, float);
468         vector frag_force = M_ARGV(6, vector);
469
470         if (STAT(FROZEN, frag_target) == FROZEN_NORMAL && autocvar_g_freezetag_revive_auto_reducible
471                 && autocvar_g_freezetag_frozen_maxtime > 0 && autocvar_g_freezetag_revive_auto)
472         {
473                 float t = 0;
474                 if ((autocvar_g_freezetag_revive_auto_reducible < 0 || DIFF_TEAM(frag_attacker, frag_target))
475                         && frag_target.freezetag_frozen_timeout > time)
476                 {
477                         if (fabs(autocvar_g_freezetag_revive_auto_reducible) == 1)
478                                 t = vlen(frag_force) * autocvar_g_freezetag_revive_auto_reducible_forcefactor;
479                         frag_target.freezetag_frozen_timeout -= t;
480                         if (frag_target.freezetag_frozen_timeout < time)
481                                 frag_target.freezetag_frozen_timeout = time;
482                 }
483         }
484 }
485
486 #ifdef IS_REVIVING
487         #undef IS_REVIVING
488 #endif
489
490 // returns true if player is reviving it
491 #define IS_REVIVING(player, it, revive_extra_size) \
492         (it != player && !STAT(FROZEN, it) && !IS_DEAD(it) && SAME_TEAM(it, player) \
493         && boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
494
495 MUTATOR_HOOKFUNCTION(ft, PlayerPreThink, CBC_ORDER_FIRST)
496 {
497         if(game_stopped)
498                 return true;
499
500         if(round_handler_IsActive())
501         if(!round_handler_IsRoundStarted())
502                 return true;
503
504         entity player = M_ARGV(0, entity);
505         //if (STAT(FROZEN, player) == FROZEN_NORMAL)
506         //if(player.freezetag_frozen_timeout > 0 && time < player.freezetag_frozen_timeout)
507                 //player.iceblock.alpha = ICE_MIN_ALPHA + (ICE_MAX_ALPHA - ICE_MIN_ALPHA) * (player.freezetag_frozen_timeout - time) / (player.freezetag_frozen_timeout - player.freezetag_frozen_time);
508
509         if (!(frametime && IS_PLAYER(player)))
510                 return true;
511
512         entity reviving_players_last = NULL;
513         entity reviving_players_first = NULL;
514
515         int n = 0;
516         vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
517         FOREACH_CLIENT(IS_PLAYER(it) && IS_REVIVING(player, it, revive_extra_size), {
518                 if (reviving_players_last)
519                         reviving_players_last.chain = it;
520                 reviving_players_last = it;
521                 if (!reviving_players_first)
522                         reviving_players_first = it;
523                 ++n;
524         });
525         if (reviving_players_last)
526                 reviving_players_last.chain = NULL;
527
528         // allow normal revival during automatic revival
529         // (not allowing it IS_REVIVING should check freezetag_frozen_timeout too)
530         if (!n && player.freezetag_frozen_timeout > 0 && time >= player.freezetag_frozen_timeout)
531                 n = -1;
532
533         float base_progress = 0;
534         if  (STAT(FROZEN, player) == FROZEN_NORMAL && autocvar_g_freezetag_revive_auto
535                 && autocvar_g_freezetag_frozen_maxtime > 0 && autocvar_g_freezetag_revive_auto_progress)
536         {
537                 // NOTE if auto-revival is in progress, manual revive speed is reduced so that it always takes the same amount of time
538                 base_progress = bound(0, (1 - (player.freezetag_frozen_timeout - time) / autocvar_g_freezetag_frozen_maxtime), 1);
539         }
540
541         if (!n) // no teammate nearby
542         {
543                 if (STAT(FROZEN, player) == FROZEN_NORMAL)
544                         STAT(REVIVE_PROGRESS, player) = bound(base_progress, STAT(REVIVE_PROGRESS, player) - frametime * autocvar_g_freezetag_revive_clearspeed * (1 - base_progress), 1);
545                 else if (!STAT(FROZEN, player))
546                         STAT(REVIVE_PROGRESS, player) = base_progress; // thawing nobody
547         }
548         else if (STAT(FROZEN, player) == FROZEN_NORMAL) // OK, there is at least one teammate reviving us
549         {
550                 STAT(REVIVE_PROGRESS, player) = bound(base_progress, STAT(REVIVE_PROGRESS, player) + frametime * max(1/60, autocvar_g_freezetag_revive_speed * (1 - base_progress)), 1);
551
552                 if(STAT(REVIVE_PROGRESS, player) >= 1)
553                 {
554                         float frozen_time = time - player.freezetag_frozen_time;
555                         Unfreeze(player, false);
556                         SetResourceExplicit(player, RES_HEALTH, ((warmup_stage) ? warmup_start_health : start_health));
557                         freezetag_count_alive_players();
558
559                         if(n == -1)
560                         {
561                                 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_AUTO_REVIVED, frozen_time);
562                                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_AUTO_REVIVED, player.netname, frozen_time);
563                                 return true;
564                         }
565
566                         // EVERY team mate nearby gets a point (even if multiple!)
567                         for(entity it = reviving_players_first; it; it = it.chain)
568                         {
569                                 GameRules_scoring_add(it, FREEZETAG_REVIVALS, +1);
570                                 GameRules_scoring_add(it, SCORE, +1);
571                                 nades_GiveBonus(it, autocvar_g_nades_bonus_score_low);
572                         }
573
574                         entity first = reviving_players_first;
575                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_REVIVED, first.netname);
576                         Send_Notification(NOTIF_ONE, first, MSG_CENTER, CENTER_FREEZETAG_REVIVE, player.netname);
577                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_REVIVED, player.netname, first.netname);
578                 }
579
580                 for(entity it = reviving_players_first; it; it = it.chain)
581                         STAT(REVIVE_PROGRESS, it) = STAT(REVIVE_PROGRESS, player);
582         }
583
584         if (STAT(FROZEN, player) == FROZEN_NORMAL)
585         {
586                 entity player_wp = player.waypointsprite_attached;
587                 if (n > 0 || (n == 0 && STAT(REVIVE_PROGRESS, player) > 0.95))
588                 {
589                         WaypointSprite_UpdateSprites(player_wp, WP_Reviving, WP_Null, WP_Null);
590                         WaypointSprite_UpdateTeamRadar(player_wp, RADARICON_WAYPOINT, WP_REVIVING_COLOR);
591                 }
592                 else
593                 {
594                         WaypointSprite_UpdateSprites(player_wp, WP_Frozen, WP_Null, WP_Null);
595                         WaypointSprite_UpdateTeamRadar(player_wp, RADARICON_WAYPOINT, WP_FROZEN_COLOR);
596                 }
597
598                 WaypointSprite_UpdateMaxHealth(player_wp, 1);
599                 WaypointSprite_UpdateHealth(player_wp, STAT(REVIVE_PROGRESS, player));
600         }
601
602         return true;
603 }
604
605 MUTATOR_HOOKFUNCTION(ft, SetStartItems)
606 {
607         start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
608         //start_health       = warmup_start_health       = cvar("g_lms_start_health");
609         //start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
610         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
611         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
612         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
613         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
614         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
615         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
616 }
617
618 MUTATOR_HOOKFUNCTION(ft, HavocBot_ChooseRole)
619 {
620         entity bot = M_ARGV(0, entity);
621
622         if (!IS_DEAD(bot))
623         {
624                 if (random() < 0.5)
625                         bot.havocbot_role = havocbot_role_ft_freeing;
626                 else
627                         bot.havocbot_role = havocbot_role_ft_offense;
628         }
629
630         // if bots spawn all at once assign them a more appropriated role after a while
631         if (time < CS(bot).jointime + 1)
632                 bot.havocbot_role_timeout = time + 10 + random() * 10;
633
634         return true;
635 }
636
637 MUTATOR_HOOKFUNCTION(ft, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
638 {
639         M_ARGV(0, float) = freezetag_teams;
640         return true;
641 }
642
643 MUTATOR_HOOKFUNCTION(ft, SetWeaponArena)
644 {
645         if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
646                 M_ARGV(0, string) = autocvar_g_freezetag_weaponarena;
647 }
648
649 MUTATOR_HOOKFUNCTION(ft, FragCenterMessage)
650 {
651         entity frag_attacker = M_ARGV(0, entity);
652         entity frag_target = M_ARGV(1, entity);
653         //float frag_deathtype = M_ARGV(2, float);
654         int kill_count_to_attacker = M_ARGV(3, int);
655         int kill_count_to_target = M_ARGV(4, int);
656
657         if(STAT(FROZEN, frag_target) == FROZEN_NORMAL)
658                 return; // target was already frozen, so this is just pushing them off the cliff
659
660         Send_Notification(NOTIF_ONE, frag_attacker, MSG_CHOICE, CHOICE_FRAG_FREEZE, frag_target.netname, kill_count_to_attacker, (IS_BOT_CLIENT(frag_target) ? -1 : CS(frag_target).ping));
661         Send_Notification(NOTIF_ONE, frag_target, MSG_CHOICE, CHOICE_FRAGGED_FREEZE, frag_attacker.netname, kill_count_to_target,
662                 GetResource(frag_attacker, RES_HEALTH), GetResource(frag_attacker, RES_ARMOR), (IS_BOT_CLIENT(frag_attacker) ? -1 : CS(frag_attacker).ping));
663
664         return true;
665 }
666
667 MUTATOR_HOOKFUNCTION(ft, SV_ParseServerCommand)
668 {
669         string cmd_name = M_ARGV(0, string);
670         if (cmd_name == "shuffleteams")
671                 shuffleteams_on_reset_map = !(round_handler_IsActive() && !round_handler_IsRoundStarted());
672         return false;
673 }
674
675 MUTATOR_HOOKFUNCTION(ft, Scores_CountFragsRemaining)
676 {
677         // announce remaining frags
678         return true;
679 }
680
681 void freezetag_Initialize()
682 {
683         freezetag_teams = autocvar_g_freezetag_teams_override;
684         if(freezetag_teams < 2)
685                 freezetag_teams = cvar("g_freezetag_teams"); // read the cvar directly as it gets written earlier in the same frame
686
687         freezetag_teams = BITS(bound(2, freezetag_teams, 4));
688         GameRules_scoring(freezetag_teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, {
689                 field_team(ST_FT_ROUNDS, "rounds", SFL_SORT_PRIO_PRIMARY);
690                 field(SP_FREEZETAG_REVIVALS, "revivals", 0);
691         });
692
693         round_handler_Spawn(freezetag_CheckTeams, freezetag_CheckWinner, func_null);
694         round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
695
696         EliminatedPlayers_Init(freezetag_isEliminated);
697 }