1 #include "freezetag.qh"
6 #include <server/resources.qh>
8 float autocvar_g_freezetag_frozen_maxtime;
9 float autocvar_g_freezetag_revive_clearspeed;
10 float autocvar_g_freezetag_round_timelimit;
11 //int autocvar_g_freezetag_teams;
12 int autocvar_g_freezetag_teams_override;
13 float autocvar_g_freezetag_warmup;
15 void freezetag_count_alive_players()
18 for (int i = 1; i <= NUM_TEAMS; ++i)
20 Team_SetNumberOfAlivePlayers(Team_GetTeamFromIndex(i), 0);
22 FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it),
25 if ((GetResourceAmount(it, RESOURCE_HEALTH) < 1) ||
26 (STAT(FROZEN, it) == 1))
30 entity team_ = Entity_GetTeam(it);
31 int num_alive = Team_GetNumberOfAlivePlayers(team_);
33 Team_SetNumberOfAlivePlayers(team_, num_alive);
35 FOREACH_CLIENT(IS_REAL_CLIENT(it),
37 STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(
39 STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(
40 Team_GetTeamFromIndex(2));
41 STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(
42 Team_GetTeamFromIndex(3));
43 STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(
44 Team_GetTeamFromIndex(4));
47 eliminatedPlayers.SendFlags |= 1;
50 #define FREEZETAG_ALIVE_TEAMS_OK() (Team_GetNumberOfAliveTeams() == NumTeams(freezetag_teams))
52 float freezetag_CheckTeams()
54 static float prev_missing_teams_mask;
55 if(FREEZETAG_ALIVE_TEAMS_OK())
57 if(prev_missing_teams_mask > 0)
58 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
59 prev_missing_teams_mask = -1;
62 if(total_players == 0)
64 if(prev_missing_teams_mask > 0)
65 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
66 prev_missing_teams_mask = -1;
69 int missing_teams_mask = 0;
70 for (int i = 1; i <= NUM_TEAMS; ++i)
72 if ((freezetag_teams & Team_IndexToBit(i)) &&
73 (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
75 missing_teams_mask |= Team_IndexToBit(i);
78 if(prev_missing_teams_mask != missing_teams_mask)
80 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
81 prev_missing_teams_mask = missing_teams_mask;
86 int freezetag_getWinnerTeam()
89 if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1)) >= 1)
91 winner_team = NUM_TEAM_1;
93 for (int i = 2; i <= NUM_TEAMS; ++i)
95 if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) >= 1)
101 winner_team = Team_IndexToTeam(i);
108 return -1; // no player left
111 void nades_Clear(entity);
112 void nades_GiveBonus(entity player, float score);
114 float freezetag_CheckWinner()
116 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
118 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
119 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
120 FOREACH_CLIENT(IS_PLAYER(it), {
121 it.freezetag_frozen_timeout = 0;
125 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
129 if (Team_GetNumberOfAliveTeams() > 1)
134 int winner_team = freezetag_getWinnerTeam();
137 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
138 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
139 TeamScore_AddToTeam(winner_team, ST_SCORE, +1);
141 else if(winner_team == -1)
143 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
144 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
147 FOREACH_CLIENT(IS_PLAYER(it), {
148 it.freezetag_frozen_timeout = 0;
153 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
157 entity freezetag_LastPlayerForTeam(entity this)
159 entity last_pl = NULL;
160 FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), {
161 if (!STAT(FROZEN, it) && GetResourceAmount(it, RESOURCE_HEALTH) >= 1)
172 void freezetag_LastPlayerForTeam_Notify(entity this)
174 if(round_handler_IsActive())
175 if(round_handler_IsRoundStarted())
177 entity pl = freezetag_LastPlayerForTeam(this);
179 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
183 void freezetag_Add_Score(entity targ, entity attacker)
187 // you froze your own dumb targ
188 // counted as "suicide" already
189 GameRules_scoring_add(targ, SCORE, -1);
191 else if(IS_PLAYER(attacker))
193 // got frozen by an enemy
194 // counted as "kill" and "death" already
195 GameRules_scoring_add(targ, SCORE, -1);
196 GameRules_scoring_add(attacker, SCORE, +1);
198 // else nothing - got frozen by the game type rules themselves
201 // to be called when the player is frozen by freezetag (on death, spectator join etc), gives the score
202 void freezetag_Freeze(entity targ, entity attacker)
204 if(STAT(FROZEN, targ))
207 if(autocvar_g_freezetag_frozen_maxtime > 0)
208 targ.freezetag_frozen_timeout = time + autocvar_g_freezetag_frozen_maxtime;
210 Freeze(targ, 0, 1, true);
212 freezetag_count_alive_players();
214 freezetag_Add_Score(targ, attacker);
217 float freezetag_isEliminated(entity e)
219 if(IS_PLAYER(e) && (STAT(FROZEN, e) == 1 || IS_DEAD(e)))
229 void(entity this) havocbot_role_ft_freeing;
230 void(entity this) havocbot_role_ft_offense;
232 void havocbot_goalrating_freeplayers(entity this, float ratingscale, vector org, float sradius)
235 FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), {
236 if (STAT(FROZEN, it) == 1)
238 if(vdist(it.origin - org, >, sradius))
240 navigation_routerating(this, it, ratingscale, 2000);
242 else if(vdist(it.origin - org, >, 400)) // avoid gathering all teammates in one place
244 // If teamate is not frozen still seek them out as fight better
246 t = 0.2 * 150 / (GetResourceAmount(this, RESOURCE_HEALTH) + GetResourceAmount(this, RESOURCE_ARMOR));
247 navigation_routerating(this, it, t * ratingscale, 2000);
252 void havocbot_role_ft_offense(entity this)
257 if (!this.havocbot_role_timeout)
258 this.havocbot_role_timeout = time + random() * 10 + 20;
260 // Count how many players on team are unfrozen.
262 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, this) && !(STAT(FROZEN, it) != 1), { unfrozen++; });
264 // If only one left on team or if role has timed out then start trying to free players.
265 if (((unfrozen == 0) && (!STAT(FROZEN, this))) || (time > this.havocbot_role_timeout))
267 LOG_TRACE("changing role to freeing");
268 this.havocbot_role = havocbot_role_ft_freeing;
269 this.havocbot_role_timeout = 0;
273 if (navigation_goalrating_timeout(this))
275 navigation_goalrating_start(this);
276 havocbot_goalrating_items(this, 10000, this.origin, 10000);
277 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 10000);
278 havocbot_goalrating_freeplayers(this, 9000, this.origin, 10000);
279 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
280 navigation_goalrating_end(this);
282 navigation_goalrating_timeout_set(this);
286 void havocbot_role_ft_freeing(entity this)
291 if (!this.havocbot_role_timeout)
292 this.havocbot_role_timeout = time + random() * 10 + 20;
294 if (time > this.havocbot_role_timeout)
296 LOG_TRACE("changing role to offense");
297 this.havocbot_role = havocbot_role_ft_offense;
298 this.havocbot_role_timeout = 0;
302 if (navigation_goalrating_timeout(this))
304 navigation_goalrating_start(this);
305 havocbot_goalrating_items(this, 8000, this.origin, 10000);
306 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
307 havocbot_goalrating_freeplayers(this, 20000, this.origin, 10000);
308 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
309 navigation_goalrating_end(this);
311 navigation_goalrating_timeout_set(this);
320 void ft_RemovePlayer(entity this)
322 SetResourceAmountExplicit(this, RESOURCE_HEALTH, 0); // neccessary to update correctly alive stats
323 if(!STAT(FROZEN, this))
324 freezetag_LastPlayerForTeam_Notify(this);
326 freezetag_count_alive_players();
329 MUTATOR_HOOKFUNCTION(ft, ClientDisconnect)
331 entity player = M_ARGV(0, entity);
333 ft_RemovePlayer(player);
337 MUTATOR_HOOKFUNCTION(ft, MakePlayerObserver)
339 entity player = M_ARGV(0, entity);
341 ft_RemovePlayer(player);
344 MUTATOR_HOOKFUNCTION(ft, PlayerDies)
346 entity frag_attacker = M_ARGV(1, entity);
347 entity frag_target = M_ARGV(2, entity);
348 float frag_deathtype = M_ARGV(3, float);
350 if(round_handler_IsActive())
351 if(round_handler_CountdownRunning())
353 if(STAT(FROZEN, frag_target))
354 Unfreeze(frag_target);
355 freezetag_count_alive_players();
356 return true; // let the player die so that he can respawn whenever he wants
359 // Cases DEATH_TEAMCHANGE and DEATH_AUTOTEAMCHANGE are needed to fix a bug whe
360 // you succeed changing team through the menu: you both really die (gibbing) and get frozen
361 if(ITEM_DAMAGE_NEEDKILL(frag_deathtype)
362 || frag_deathtype == DEATH_TEAMCHANGE.m_id || frag_deathtype == DEATH_AUTOTEAMCHANGE.m_id)
364 // let the player die, he will be automatically frozen when he respawns
365 if(STAT(FROZEN, frag_target) != 1)
367 freezetag_Add_Score(frag_target, frag_attacker);
368 freezetag_count_alive_players();
369 freezetag_LastPlayerForTeam_Notify(frag_target);
372 Unfreeze(frag_target); // remove ice
373 SetResourceAmountExplicit(frag_target, RESOURCE_HEALTH, 0); // Unfreeze resets health
374 frag_target.freezetag_frozen_timeout = -2; // freeze on respawn
378 if(STAT(FROZEN, frag_target))
381 freezetag_Freeze(frag_target, frag_attacker);
382 freezetag_LastPlayerForTeam_Notify(frag_target);
384 if(frag_attacker == frag_target || frag_attacker == NULL)
386 if(IS_PLAYER(frag_target))
387 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_SELF);
388 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_SELF, frag_target.netname);
392 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_FREEZE, frag_target.netname, frag_attacker.netname);
398 MUTATOR_HOOKFUNCTION(ft, PlayerSpawn)
400 entity player = M_ARGV(0, entity);
402 if(player.freezetag_frozen_timeout == -1) // if PlayerSpawn is called by reset_map_players
403 return true; // do nothing, round is starting right now
405 if(player.freezetag_frozen_timeout == -2) // player was dead
407 freezetag_Freeze(player, NULL);
411 freezetag_count_alive_players();
413 if(round_handler_IsActive())
414 if(round_handler_IsRoundStarted())
416 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_SPAWN_LATE);
417 freezetag_Freeze(player, NULL);
423 MUTATOR_HOOKFUNCTION(ft, reset_map_players)
425 FOREACH_CLIENT(IS_PLAYER(it), {
426 CS(it).killcount = 0;
427 it.freezetag_frozen_timeout = -1;
428 PutClientInServer(it);
429 it.freezetag_frozen_timeout = 0;
431 freezetag_count_alive_players();
435 MUTATOR_HOOKFUNCTION(ft, GiveFragsForKill, CBC_ORDER_FIRST)
437 M_ARGV(2, float) = 0; // no frags counted in Freeze Tag
441 MUTATOR_HOOKFUNCTION(ft, Unfreeze)
443 entity targ = M_ARGV(0, entity);
444 targ.freezetag_frozen_time = 0;
445 targ.freezetag_frozen_timeout = 0;
447 freezetag_count_alive_players();
450 MUTATOR_HOOKFUNCTION(ft, PlayerPreThink, CBC_ORDER_FIRST)
455 if(round_handler_IsActive())
456 if(!round_handler_IsRoundStarted())
461 entity player = M_ARGV(0, entity);
462 //if(STAT(FROZEN, player))
463 //if(player.freezetag_frozen_timeout > 0 && time < player.freezetag_frozen_timeout)
464 //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);
466 if(player.freezetag_frozen_timeout > 0 && time >= player.freezetag_frozen_timeout)
470 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
472 FOREACH_CLIENT(IS_PLAYER(it) && it != player, {
473 if(STAT(FROZEN, it) == 0)
475 if(SAME_TEAM(it, player))
476 if(boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
480 if(STAT(FROZEN, player) == 1)
488 if(n && STAT(FROZEN, player) == 1) // OK, there is at least one teammate reviving us
490 STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
491 SetResourceAmountExplicit(player, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health)));
493 if(STAT(REVIVE_PROGRESS, player) >= 1)
496 freezetag_count_alive_players();
500 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_AUTO_REVIVED, autocvar_g_freezetag_frozen_maxtime);
501 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_AUTO_REVIVED, player.netname, autocvar_g_freezetag_frozen_maxtime);
505 // EVERY team mate nearby gets a point (even if multiple!)
506 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, {
507 GameRules_scoring_add(it, FREEZETAG_REVIVALS, +1);
508 GameRules_scoring_add(it, SCORE, +1);
509 nades_GiveBonus(it,autocvar_g_nades_bonus_score_low);
512 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
513 Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, player.netname);
514 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_REVIVED, player.netname, o.netname);
517 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, {
518 STAT(REVIVE_PROGRESS, it) = STAT(REVIVE_PROGRESS, player);
522 else if(!n && STAT(FROZEN, player) == 1) // only if no teammate is nearby will we reset
524 STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) - frametime * autocvar_g_freezetag_revive_clearspeed, 1);
525 SetResourceAmountExplicit(player, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health)));
527 else if(!n && !STAT(FROZEN, player))
529 STAT(REVIVE_PROGRESS, player) = 0; // thawing nobody
535 MUTATOR_HOOKFUNCTION(ft, SetStartItems)
537 start_items &= ~IT_UNLIMITED_AMMO;
538 //start_health = warmup_start_health = cvar("g_lms_start_health");
539 //start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
540 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
541 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
542 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
543 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
544 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
545 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
548 MUTATOR_HOOKFUNCTION(ft, HavocBot_ChooseRole)
550 entity bot = M_ARGV(0, entity);
555 bot.havocbot_role = havocbot_role_ft_freeing;
557 bot.havocbot_role = havocbot_role_ft_offense;
563 MUTATOR_HOOKFUNCTION(ft, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
565 M_ARGV(0, float) = freezetag_teams;
569 MUTATOR_HOOKFUNCTION(ft, SetWeaponArena)
571 // most weapons arena
572 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
573 M_ARGV(0, string) = "most";
576 MUTATOR_HOOKFUNCTION(ft, FragCenterMessage)
578 entity frag_attacker = M_ARGV(0, entity);
579 entity frag_target = M_ARGV(1, entity);
580 //float frag_deathtype = M_ARGV(2, float);
581 int kill_count_to_attacker = M_ARGV(3, int);
582 int kill_count_to_target = M_ARGV(4, int);
584 if(STAT(FROZEN, frag_target))
585 return; // target was already frozen, so this is just pushing them off the cliff
587 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));
588 Send_Notification(NOTIF_ONE, frag_target, MSG_CHOICE, CHOICE_FRAGGED_FREEZE, frag_attacker.netname, kill_count_to_target,
589 GetResourceAmount(frag_attacker, RESOURCE_HEALTH), GetResourceAmount(frag_attacker, RESOURCE_ARMOR), (IS_BOT_CLIENT(frag_attacker) ? -1 : CS(frag_attacker).ping));
594 void freezetag_Initialize()
596 freezetag_teams = autocvar_g_freezetag_teams_override;
597 if(freezetag_teams < 2)
598 freezetag_teams = cvar("g_freezetag_teams"); // read the cvar directly as it gets written earlier in the same frame
600 freezetag_teams = BITS(bound(2, freezetag_teams, 4));
601 GameRules_scoring(freezetag_teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, {
602 field(SP_FREEZETAG_REVIVALS, "revivals", 0);
605 round_handler_Spawn(freezetag_CheckTeams, freezetag_CheckWinner, func_null);
606 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
608 EliminatedPlayers_Init(freezetag_isEliminated);