1 #include "sv_freezetag.qh"
3 #include <server/resources.qh>
5 float autocvar_g_freezetag_frozen_maxtime;
6 float autocvar_g_freezetag_revive_clearspeed;
7 float autocvar_g_freezetag_round_timelimit;
8 //int autocvar_g_freezetag_teams;
9 int autocvar_g_freezetag_teams_override;
10 float autocvar_g_freezetag_warmup;
12 void freezetag_count_alive_players()
15 for (int i = 1; i <= NUM_TEAMS; ++i)
17 Team_SetNumberOfAlivePlayers(Team_GetTeamFromIndex(i), 0);
19 FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it),
22 if (GetResource(it, RES_HEALTH) < 1 || STAT(FROZEN, it) == FROZEN_NORMAL)
26 entity team_ = Entity_GetTeam(it);
27 int num_alive = Team_GetNumberOfAlivePlayers(team_);
29 Team_SetNumberOfAlivePlayers(team_, num_alive);
31 FOREACH_CLIENT(IS_REAL_CLIENT(it),
33 STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(
35 STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(
36 Team_GetTeamFromIndex(2));
37 STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(
38 Team_GetTeamFromIndex(3));
39 STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(
40 Team_GetTeamFromIndex(4));
43 eliminatedPlayers.SendFlags |= 1;
46 bool freezetag_CheckTeams()
48 static float prev_missing_teams_mask;
49 if (Team_GetNumberOfAliveTeams() == NumTeams(freezetag_teams))
51 if(prev_missing_teams_mask > 0)
52 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
53 prev_missing_teams_mask = -1;
56 if(total_players == 0)
58 if(prev_missing_teams_mask > 0)
59 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
60 prev_missing_teams_mask = -1;
63 int missing_teams_mask = 0;
64 for (int i = 1; i <= NUM_TEAMS; ++i)
66 if ((freezetag_teams & Team_IndexToBit(i)) &&
67 (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
69 missing_teams_mask |= Team_IndexToBit(i);
72 if(prev_missing_teams_mask != missing_teams_mask)
74 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
75 prev_missing_teams_mask = missing_teams_mask;
80 int freezetag_getWinnerTeam()
83 if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1)) >= 1)
85 winner_team = NUM_TEAM_1;
87 for (int i = 2; i <= NUM_TEAMS; ++i)
89 if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) >= 1)
95 winner_team = Team_IndexToTeam(i);
102 return -1; // no player left
105 void nades_Clear(entity);
106 void nades_GiveBonus(entity player, float score);
108 bool freezetag_CheckWinner()
110 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
112 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
113 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
114 FOREACH_CLIENT(IS_PLAYER(it), {
115 it.freezetag_frozen_timeout = 0;
119 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
123 if (Team_GetNumberOfAliveTeams() > 1)
128 int winner_team = freezetag_getWinnerTeam();
131 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
132 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
133 TeamScore_AddToTeam(winner_team, ST_SCORE, +1);
135 else if(winner_team == -1)
137 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
138 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
141 FOREACH_CLIENT(IS_PLAYER(it), {
142 it.freezetag_frozen_timeout = 0;
147 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
151 entity freezetag_LastPlayerForTeam(entity this)
153 entity last_pl = NULL;
154 FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), {
155 if (STAT(FROZEN, it) != FROZEN_NORMAL && GetResource(it, RES_HEALTH) >= 1)
166 void freezetag_LastPlayerForTeam_Notify(entity this)
168 if(round_handler_IsActive())
169 if(round_handler_IsRoundStarted())
171 entity pl = freezetag_LastPlayerForTeam(this);
173 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
177 void freezetag_Add_Score(entity targ, entity attacker)
181 // you froze your own dumb targ
182 // counted as "suicide" already
183 GameRules_scoring_add(targ, SCORE, -1);
185 else if(IS_PLAYER(attacker))
187 // got frozen by an enemy
188 // counted as "kill" and "death" already
189 GameRules_scoring_add(targ, SCORE, -1);
190 GameRules_scoring_add(attacker, SCORE, +1);
192 // else nothing - got frozen by the game type rules themselves
195 // to be called when the player is frozen by freezetag (on death, spectator join etc), gives the score
196 void freezetag_Freeze(entity targ, entity attacker)
198 if(STAT(FROZEN, targ))
201 targ.freezetag_frozen_time = time;
202 if (autocvar_g_freezetag_revive_auto && autocvar_g_freezetag_frozen_maxtime > 0)
203 targ.freezetag_frozen_timeout = time + autocvar_g_freezetag_frozen_maxtime;
205 Freeze(targ, 0, FROZEN_NORMAL, true);
207 freezetag_count_alive_players();
209 freezetag_Add_Score(targ, attacker);
212 bool freezetag_isEliminated(entity e)
214 if(IS_PLAYER(e) && (STAT(FROZEN, e) == FROZEN_NORMAL || IS_DEAD(e)))
224 void(entity this) havocbot_role_ft_freeing;
225 void(entity this) havocbot_role_ft_offense;
227 void havocbot_goalrating_ft_freeplayers(entity this, float ratingscale, vector org, float sradius)
229 entity best_pl = NULL;
230 float best_dist2 = FLOAT_MAX;
231 FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), {
232 if (STAT(FROZEN, it) == FROZEN_NORMAL)
234 if(vdist(it.origin - org, >, sradius))
236 navigation_routerating(this, it, ratingscale, 2000);
239 && GetResource(it, RES_HEALTH) < GetResource(this, RES_HEALTH) + 30
240 && vlen2(it.origin - org) < best_dist2)
242 // If teamate is not frozen still seek them out as fight better
244 best_dist2 = vlen2(it.origin - org);
245 if (best_dist2 < 700 ** 2)
248 best_dist2 = 0; // already close to a teammate
255 navigation_routerating(this, best_pl, ratingscale / 2, 2000);
258 void havocbot_role_ft_offense(entity this)
263 if (!this.havocbot_role_timeout)
264 this.havocbot_role_timeout = time + random() * 10 + 20;
266 // Count how many players on team are unfrozen.
268 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, this) && STAT(FROZEN, it) != FROZEN_NORMAL, {
272 // If only one left on team or if role has timed out then start trying to free players.
273 if ((!unfrozen && STAT(FROZEN, this) != FROZEN_NORMAL) || time > this.havocbot_role_timeout)
275 LOG_TRACE("changing role to freeing");
276 this.havocbot_role = havocbot_role_ft_freeing;
277 this.havocbot_role_timeout = 0;
281 if (navigation_goalrating_timeout(this))
283 navigation_goalrating_start(this);
284 havocbot_goalrating_items(this, 12000, this.origin, 10000);
285 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
286 havocbot_goalrating_ft_freeplayers(this, 9000, this.origin, 10000);
287 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
288 navigation_goalrating_end(this);
290 navigation_goalrating_timeout_set(this);
294 void havocbot_role_ft_freeing(entity this)
299 if (!this.havocbot_role_timeout)
300 this.havocbot_role_timeout = time + random() * 10 + 20;
302 if (time > this.havocbot_role_timeout)
304 LOG_TRACE("changing role to offense");
305 this.havocbot_role = havocbot_role_ft_offense;
306 this.havocbot_role_timeout = 0;
310 if (navigation_goalrating_timeout(this))
312 navigation_goalrating_start(this);
313 havocbot_goalrating_items(this, 10000, this.origin, 10000);
314 havocbot_goalrating_enemyplayers(this, 5000, this.origin, 10000);
315 havocbot_goalrating_ft_freeplayers(this, 20000, this.origin, 10000);
316 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
317 navigation_goalrating_end(this);
319 navigation_goalrating_timeout_set(this);
328 void ft_RemovePlayer(entity this)
330 if (STAT(FROZEN, this) != FROZEN_NORMAL)
331 freezetag_LastPlayerForTeam_Notify(this);
332 Unfreeze(this, false);
334 SetResourceExplicit(this, RES_HEALTH, 0); // neccessary to correctly count alive players
335 freezetag_count_alive_players();
338 MUTATOR_HOOKFUNCTION(ft, ClientDisconnect)
340 entity player = M_ARGV(0, entity);
342 ft_RemovePlayer(player);
346 MUTATOR_HOOKFUNCTION(ft, MakePlayerObserver)
348 entity player = M_ARGV(0, entity);
350 ft_RemovePlayer(player);
353 MUTATOR_HOOKFUNCTION(ft, PlayerDies)
355 entity frag_attacker = M_ARGV(1, entity);
356 entity frag_target = M_ARGV(2, entity);
357 float frag_deathtype = M_ARGV(3, float);
359 if(round_handler_IsActive())
360 if(round_handler_CountdownRunning())
362 if (STAT(FROZEN, frag_target) == FROZEN_NORMAL)
363 Unfreeze(frag_target, true);
364 freezetag_count_alive_players();
365 frag_target.respawn_time = time;
366 frag_target.respawn_flags |= RESPAWN_FORCE;
370 // Cases DEATH_TEAMCHANGE and DEATH_AUTOTEAMCHANGE are needed to fix a bug whe
371 // you succeed changing team through the menu: you both really die (gibbing) and get frozen
372 if(ITEM_DAMAGE_NEEDKILL(frag_deathtype)
373 || frag_deathtype == DEATH_TEAMCHANGE.m_id || frag_deathtype == DEATH_AUTOTEAMCHANGE.m_id)
375 // let the player die, he will be automatically frozen when he respawns
376 if (STAT(FROZEN, frag_target) != FROZEN_NORMAL)
378 freezetag_Add_Score(frag_target, frag_attacker);
379 freezetag_count_alive_players();
380 freezetag_LastPlayerForTeam_Notify(frag_target);
383 Unfreeze(frag_target, false); // remove ice
384 frag_target.freezetag_frozen_timeout = -2; // freeze on respawn
388 if (STAT(FROZEN, frag_target) == FROZEN_NORMAL)
391 freezetag_Freeze(frag_target, frag_attacker);
392 freezetag_LastPlayerForTeam_Notify(frag_target);
394 if(frag_attacker == frag_target || frag_attacker == NULL)
396 if(IS_PLAYER(frag_target))
397 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_SELF);
398 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_SELF, frag_target.netname);
402 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_FREEZE, frag_target.netname, frag_attacker.netname);
408 MUTATOR_HOOKFUNCTION(ft, PlayerSpawn)
410 entity player = M_ARGV(0, entity);
412 if(player.freezetag_frozen_timeout == -1) // if PlayerSpawn is called by reset_map_players
413 return true; // do nothing, round is starting right now
415 if(player.freezetag_frozen_timeout == -2) // player was dead
417 freezetag_Freeze(player, NULL);
421 freezetag_count_alive_players();
423 if(round_handler_IsActive())
424 if(round_handler_IsRoundStarted())
426 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_SPAWN_LATE);
427 freezetag_Freeze(player, NULL);
433 MUTATOR_HOOKFUNCTION(ft, reset_map_players)
435 FOREACH_CLIENT(IS_PLAYER(it), {
436 CS(it).killcount = 0;
437 it.freezetag_frozen_timeout = -1;
438 PutClientInServer(it);
439 it.freezetag_frozen_timeout = 0;
441 freezetag_count_alive_players();
445 MUTATOR_HOOKFUNCTION(ft, GiveFragsForKill, CBC_ORDER_FIRST)
447 M_ARGV(2, float) = 0; // no frags counted in Freeze Tag
451 MUTATOR_HOOKFUNCTION(ft, Unfreeze)
453 entity targ = M_ARGV(0, entity);
454 targ.freezetag_frozen_time = 0;
455 targ.freezetag_frozen_timeout = 0;
458 MUTATOR_HOOKFUNCTION(ft, Damage_Calculate)
460 entity frag_attacker = M_ARGV(1, entity);
461 entity frag_target = M_ARGV(2, entity);
462 //float frag_deathtype = M_ARGV(3, float);
463 //float frag_damage = M_ARGV(4, float);
464 vector frag_force = M_ARGV(6, vector);
466 if (STAT(FROZEN, frag_target) == FROZEN_NORMAL && autocvar_g_freezetag_revive_auto_reducible
467 && autocvar_g_freezetag_frozen_maxtime > 0 && autocvar_g_freezetag_revive_auto)
470 if ((autocvar_g_freezetag_revive_auto_reducible > 0 || DIFF_TEAM(frag_attacker, frag_target))
471 && frag_target.freezetag_frozen_timeout > time)
473 if (fabs(autocvar_g_freezetag_revive_auto_reducible) == 1)
474 t = vlen(frag_force) * autocvar_g_freezetag_revive_auto_reducible_forcefactor;
475 frag_target.freezetag_frozen_timeout -= t;
476 if (frag_target.freezetag_frozen_timeout < time)
477 frag_target.freezetag_frozen_timeout = time;
486 // returns true if player is reviving it
487 #define IS_REVIVING(player, it, revive_extra_size) \
488 (it != player && !STAT(FROZEN, it) && !IS_DEAD(it) && SAME_TEAM(it, player) \
489 && boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
491 MUTATOR_HOOKFUNCTION(ft, PlayerPreThink, CBC_ORDER_FIRST)
496 if(round_handler_IsActive())
497 if(!round_handler_IsRoundStarted())
501 entity player = M_ARGV(0, entity);
502 //if (STAT(FROZEN, player) == FROZEN_NORMAL)
503 //if(player.freezetag_frozen_timeout > 0 && time < player.freezetag_frozen_timeout)
504 //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);
506 entity reviving_players_last = NULL;
507 entity reviving_players_first = NULL;
509 if(player.freezetag_frozen_timeout > 0 && time >= player.freezetag_frozen_timeout)
514 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
515 FOREACH_CLIENT(IS_PLAYER(it) && IS_REVIVING(player, it, revive_extra_size), {
516 if (reviving_players_last)
517 reviving_players_last.chain = it;
518 reviving_players_last = it;
519 if (!reviving_players_first)
520 reviving_players_first = it;
523 if (reviving_players_last)
524 reviving_players_last.chain = NULL;
527 float base_progress = 0;
528 if (STAT(FROZEN, player) == FROZEN_NORMAL && autocvar_g_freezetag_revive_auto
529 && autocvar_g_freezetag_frozen_maxtime > 0 && autocvar_g_freezetag_revive_auto_progress)
531 // NOTE if auto-revival is in progress, manual revive speed is reduced so that it always takes the same amount of time
532 base_progress = bound(0, (1 - (player.freezetag_frozen_timeout - time) / autocvar_g_freezetag_frozen_maxtime), 1);
535 if (!n) // no teammate nearby
537 if (STAT(FROZEN, player) == FROZEN_NORMAL)
539 STAT(REVIVE_PROGRESS, player) = bound(base_progress, STAT(REVIVE_PROGRESS, player) - frametime * autocvar_g_freezetag_revive_clearspeed * (1 - base_progress), 1);
540 SetResourceExplicit(player, RES_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health)));
542 else if (!STAT(FROZEN, player))
543 STAT(REVIVE_PROGRESS, player) = base_progress; // thawing nobody
545 else if (STAT(FROZEN, player) == FROZEN_NORMAL) // OK, there is at least one teammate reviving us
547 STAT(REVIVE_PROGRESS, player) = bound(base_progress, STAT(REVIVE_PROGRESS, player) + frametime * max(1/60, autocvar_g_freezetag_revive_speed * (1 - base_progress)), 1);
548 SetResourceExplicit(player, RES_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health)));
550 if(STAT(REVIVE_PROGRESS, player) >= 1)
552 float frozen_time = time - player.freezetag_frozen_time;
553 Unfreeze(player, false);
554 freezetag_count_alive_players();
558 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_AUTO_REVIVED, frozen_time);
559 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_AUTO_REVIVED, player.netname, frozen_time);
563 // EVERY team mate nearby gets a point (even if multiple!)
564 for(entity it = reviving_players_first; it; it = it.chain)
566 GameRules_scoring_add(it, FREEZETAG_REVIVALS, +1);
567 GameRules_scoring_add(it, SCORE, +1);
568 nades_GiveBonus(it, autocvar_g_nades_bonus_score_low);
571 entity first = reviving_players_first;
572 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_REVIVED, first.netname);
573 Send_Notification(NOTIF_ONE, first, MSG_CENTER, CENTER_FREEZETAG_REVIVE, player.netname);
574 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_REVIVED, player.netname, first.netname);
577 for(entity it = reviving_players_first; it; it = it.chain)
578 STAT(REVIVE_PROGRESS, it) = STAT(REVIVE_PROGRESS, player);
584 MUTATOR_HOOKFUNCTION(ft, SetStartItems)
586 start_items &= ~(IT_UNLIMITED_AMMO | IT_UNLIMITED_SUPERWEAPONS);
587 //start_health = warmup_start_health = cvar("g_lms_start_health");
588 //start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
589 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
590 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
591 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
592 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
593 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
594 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
597 MUTATOR_HOOKFUNCTION(ft, HavocBot_ChooseRole)
599 entity bot = M_ARGV(0, entity);
604 bot.havocbot_role = havocbot_role_ft_freeing;
606 bot.havocbot_role = havocbot_role_ft_offense;
609 // if bots spawn all at once assign them a more appropriated role after a while
610 if (time < CS(bot).jointime + 1)
611 bot.havocbot_role_timeout = time + 10 + random() * 10;
616 MUTATOR_HOOKFUNCTION(ft, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
618 M_ARGV(0, float) = freezetag_teams;
622 MUTATOR_HOOKFUNCTION(ft, SetWeaponArena)
624 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
625 M_ARGV(0, string) = autocvar_g_freezetag_weaponarena;
628 MUTATOR_HOOKFUNCTION(ft, FragCenterMessage)
630 entity frag_attacker = M_ARGV(0, entity);
631 entity frag_target = M_ARGV(1, entity);
632 //float frag_deathtype = M_ARGV(2, float);
633 int kill_count_to_attacker = M_ARGV(3, int);
634 int kill_count_to_target = M_ARGV(4, int);
636 if(STAT(FROZEN, frag_target) == FROZEN_NORMAL)
637 return; // target was already frozen, so this is just pushing them off the cliff
639 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));
640 Send_Notification(NOTIF_ONE, frag_target, MSG_CHOICE, CHOICE_FRAGGED_FREEZE, frag_attacker.netname, kill_count_to_target,
641 GetResource(frag_attacker, RES_HEALTH), GetResource(frag_attacker, RES_ARMOR), (IS_BOT_CLIENT(frag_attacker) ? -1 : CS(frag_attacker).ping));
646 MUTATOR_HOOKFUNCTION(ft, SV_ParseServerCommand)
648 string cmd_name = M_ARGV(0, string);
649 if (cmd_name == "shuffleteams")
650 shuffleteams_on_reset_map = !(round_handler_IsActive() && !round_handler_IsRoundStarted());
654 void freezetag_Initialize()
656 freezetag_teams = autocvar_g_freezetag_teams_override;
657 if(freezetag_teams < 2)
658 freezetag_teams = cvar("g_freezetag_teams"); // read the cvar directly as it gets written earlier in the same frame
660 freezetag_teams = BITS(bound(2, freezetag_teams, 4));
661 GameRules_scoring(freezetag_teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, {
662 field(SP_FREEZETAG_REVIVALS, "revivals", 0);
665 round_handler_Spawn(freezetag_CheckTeams, freezetag_CheckWinner, func_null);
666 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
668 EliminatedPlayers_Init(freezetag_isEliminated);