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 ((GetResourceAmount(it, RESOURCE_HEALTH) < 1) ||
23 (STAT(FROZEN, it) == 1))
27 entity team_ = Entity_GetTeam(it);
28 int num_alive = Team_GetNumberOfAlivePlayers(team_);
30 Team_SetNumberOfAlivePlayers(team_, num_alive);
32 FOREACH_CLIENT(IS_REAL_CLIENT(it),
34 STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(
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));
44 eliminatedPlayers.SendFlags |= 1;
47 #define FREEZETAG_ALIVE_TEAMS_OK() (Team_GetNumberOfAliveTeams() == NumTeams(freezetag_teams))
49 bool freezetag_CheckTeams()
51 static float prev_missing_teams_mask;
52 if(FREEZETAG_ALIVE_TEAMS_OK())
54 if(prev_missing_teams_mask > 0)
55 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
56 prev_missing_teams_mask = -1;
59 if(total_players == 0)
61 if(prev_missing_teams_mask > 0)
62 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
63 prev_missing_teams_mask = -1;
66 int missing_teams_mask = 0;
67 for (int i = 1; i <= NUM_TEAMS; ++i)
69 if ((freezetag_teams & Team_IndexToBit(i)) &&
70 (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0))
72 missing_teams_mask |= Team_IndexToBit(i);
75 if(prev_missing_teams_mask != missing_teams_mask)
77 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
78 prev_missing_teams_mask = missing_teams_mask;
83 int freezetag_getWinnerTeam()
86 if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1)) >= 1)
88 winner_team = NUM_TEAM_1;
90 for (int i = 2; i <= NUM_TEAMS; ++i)
92 if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) >= 1)
98 winner_team = Team_IndexToTeam(i);
105 return -1; // no player left
108 void nades_Clear(entity);
109 void nades_GiveBonus(entity player, float score);
111 bool freezetag_CheckWinner()
113 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
115 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
116 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
117 FOREACH_CLIENT(IS_PLAYER(it), {
118 it.freezetag_frozen_timeout = 0;
122 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
126 if (Team_GetNumberOfAliveTeams() > 1)
131 int winner_team = freezetag_getWinnerTeam();
134 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
135 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
136 TeamScore_AddToTeam(winner_team, ST_SCORE, +1);
138 else if(winner_team == -1)
140 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
141 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
144 FOREACH_CLIENT(IS_PLAYER(it), {
145 it.freezetag_frozen_timeout = 0;
150 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
154 entity freezetag_LastPlayerForTeam(entity this)
156 entity last_pl = NULL;
157 FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), {
158 if (!STAT(FROZEN, it) && GetResourceAmount(it, RESOURCE_HEALTH) >= 1)
169 void freezetag_LastPlayerForTeam_Notify(entity this)
171 if(round_handler_IsActive())
172 if(round_handler_IsRoundStarted())
174 entity pl = freezetag_LastPlayerForTeam(this);
176 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
180 void freezetag_Add_Score(entity targ, entity attacker)
184 // you froze your own dumb targ
185 // counted as "suicide" already
186 GameRules_scoring_add(targ, SCORE, -1);
188 else if(IS_PLAYER(attacker))
190 // got frozen by an enemy
191 // counted as "kill" and "death" already
192 GameRules_scoring_add(targ, SCORE, -1);
193 GameRules_scoring_add(attacker, SCORE, +1);
195 // else nothing - got frozen by the game type rules themselves
198 // to be called when the player is frozen by freezetag (on death, spectator join etc), gives the score
199 void freezetag_Freeze(entity targ, entity attacker)
201 if(STAT(FROZEN, targ))
204 if(autocvar_g_freezetag_frozen_maxtime > 0)
205 targ.freezetag_frozen_timeout = time + autocvar_g_freezetag_frozen_maxtime;
207 Freeze(targ, 0, 1, true);
209 freezetag_count_alive_players();
211 freezetag_Add_Score(targ, attacker);
214 bool freezetag_isEliminated(entity e)
216 if(IS_PLAYER(e) && (STAT(FROZEN, e) == 1 || IS_DEAD(e)))
226 void(entity this) havocbot_role_ft_freeing;
227 void(entity this) havocbot_role_ft_offense;
229 void havocbot_goalrating_ft_freeplayers(entity this, float ratingscale, vector org, float sradius)
231 entity best_pl = NULL;
232 float best_dist2 = FLOAT_MAX;
233 FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), {
234 if (STAT(FROZEN, it) == 1)
236 if(vdist(it.origin - org, >, sradius))
238 navigation_routerating(this, it, ratingscale, 2000);
241 && GetResourceAmount(it, RESOURCE_HEALTH) < GetResourceAmount(this, RESOURCE_HEALTH) + 30
242 && vlen2(it.origin - org) < best_dist2)
244 // If teamate is not frozen still seek them out as fight better
246 best_dist2 = vlen2(it.origin - org);
247 if (best_dist2 < 700 ** 2)
250 best_dist2 = 0; // already close to a teammate
257 navigation_routerating(this, best_pl, ratingscale / 2, 2000);
260 void havocbot_role_ft_offense(entity this)
265 if (!this.havocbot_role_timeout)
266 this.havocbot_role_timeout = time + random() * 10 + 20;
268 // Count how many players on team are unfrozen.
270 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, this) && !STAT(FROZEN, it), { unfrozen++; });
272 // If only one left on team or if role has timed out then start trying to free players.
273 if ((unfrozen == 0 && !STAT(FROZEN, this)) || 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))
331 freezetag_LastPlayerForTeam_Notify(this);
334 SetResourceAmountExplicit(this, RESOURCE_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))
363 Unfreeze(frag_target);
364 freezetag_count_alive_players();
365 return true; // let the player die so that he can respawn whenever he wants
368 // Cases DEATH_TEAMCHANGE and DEATH_AUTOTEAMCHANGE are needed to fix a bug whe
369 // you succeed changing team through the menu: you both really die (gibbing) and get frozen
370 if(ITEM_DAMAGE_NEEDKILL(frag_deathtype)
371 || frag_deathtype == DEATH_TEAMCHANGE.m_id || frag_deathtype == DEATH_AUTOTEAMCHANGE.m_id)
373 // let the player die, he will be automatically frozen when he respawns
374 if(STAT(FROZEN, frag_target) != 1)
376 freezetag_Add_Score(frag_target, frag_attacker);
377 freezetag_count_alive_players();
378 freezetag_LastPlayerForTeam_Notify(frag_target);
381 Unfreeze(frag_target); // remove ice
382 SetResourceAmountExplicit(frag_target, RESOURCE_HEALTH, 0); // Unfreeze resets health
383 frag_target.freezetag_frozen_timeout = -2; // freeze on respawn
387 if(STAT(FROZEN, frag_target))
390 freezetag_Freeze(frag_target, frag_attacker);
391 freezetag_LastPlayerForTeam_Notify(frag_target);
393 if(frag_attacker == frag_target || frag_attacker == NULL)
395 if(IS_PLAYER(frag_target))
396 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_SELF);
397 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_SELF, frag_target.netname);
401 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_FREEZE, frag_target.netname, frag_attacker.netname);
407 MUTATOR_HOOKFUNCTION(ft, PlayerSpawn)
409 entity player = M_ARGV(0, entity);
411 if(player.freezetag_frozen_timeout == -1) // if PlayerSpawn is called by reset_map_players
412 return true; // do nothing, round is starting right now
414 if(player.freezetag_frozen_timeout == -2) // player was dead
416 freezetag_Freeze(player, NULL);
420 freezetag_count_alive_players();
422 if(round_handler_IsActive())
423 if(round_handler_IsRoundStarted())
425 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_SPAWN_LATE);
426 freezetag_Freeze(player, NULL);
432 MUTATOR_HOOKFUNCTION(ft, reset_map_players)
434 FOREACH_CLIENT(IS_PLAYER(it), {
435 CS(it).killcount = 0;
436 it.freezetag_frozen_timeout = -1;
437 PutClientInServer(it);
438 it.freezetag_frozen_timeout = 0;
440 freezetag_count_alive_players();
444 MUTATOR_HOOKFUNCTION(ft, GiveFragsForKill, CBC_ORDER_FIRST)
446 M_ARGV(2, float) = 0; // no frags counted in Freeze Tag
450 MUTATOR_HOOKFUNCTION(ft, Unfreeze)
452 entity targ = M_ARGV(0, entity);
453 targ.freezetag_frozen_time = 0;
454 targ.freezetag_frozen_timeout = 0;
456 freezetag_count_alive_players();
459 MUTATOR_HOOKFUNCTION(ft, PlayerPreThink, CBC_ORDER_FIRST)
464 if(round_handler_IsActive())
465 if(!round_handler_IsRoundStarted())
470 entity player = M_ARGV(0, entity);
471 //if(STAT(FROZEN, player))
472 //if(player.freezetag_frozen_timeout > 0 && time < player.freezetag_frozen_timeout)
473 //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);
475 if(player.freezetag_frozen_timeout > 0 && time >= player.freezetag_frozen_timeout)
479 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
481 FOREACH_CLIENT(IS_PLAYER(it) && it != player, {
482 if(STAT(FROZEN, it) == 0)
484 if(SAME_TEAM(it, player))
485 if(boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
489 if(STAT(FROZEN, player) == 1)
497 if(n && STAT(FROZEN, player) == 1) // OK, there is at least one teammate reviving us
499 STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
500 SetResourceAmountExplicit(player, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health)));
502 if(STAT(REVIVE_PROGRESS, player) >= 1)
505 freezetag_count_alive_players();
509 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_AUTO_REVIVED, autocvar_g_freezetag_frozen_maxtime);
510 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_AUTO_REVIVED, player.netname, autocvar_g_freezetag_frozen_maxtime);
514 // EVERY team mate nearby gets a point (even if multiple!)
515 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, {
516 GameRules_scoring_add(it, FREEZETAG_REVIVALS, +1);
517 GameRules_scoring_add(it, SCORE, +1);
518 nades_GiveBonus(it,autocvar_g_nades_bonus_score_low);
521 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
522 Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, player.netname);
523 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_REVIVED, player.netname, o.netname);
526 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, {
527 STAT(REVIVE_PROGRESS, it) = STAT(REVIVE_PROGRESS, player);
531 else if(!n && STAT(FROZEN, player) == 1) // only if no teammate is nearby will we reset
533 STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) - frametime * autocvar_g_freezetag_revive_clearspeed, 1);
534 SetResourceAmountExplicit(player, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health)));
536 else if(!n && !STAT(FROZEN, player))
538 STAT(REVIVE_PROGRESS, player) = 0; // thawing nobody
544 MUTATOR_HOOKFUNCTION(ft, SetStartItems)
546 start_items &= ~IT_UNLIMITED_AMMO;
547 //start_health = warmup_start_health = cvar("g_lms_start_health");
548 //start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
549 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
550 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
551 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
552 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
553 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
554 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
557 MUTATOR_HOOKFUNCTION(ft, HavocBot_ChooseRole)
559 entity bot = M_ARGV(0, entity);
564 bot.havocbot_role = havocbot_role_ft_freeing;
566 bot.havocbot_role = havocbot_role_ft_offense;
569 // if bots spawn all at once assign them a more appropriated role after a while
570 if (time < CS(bot).jointime + 1)
571 bot.havocbot_role_timeout = time + 10 + random() * 10;
576 MUTATOR_HOOKFUNCTION(ft, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
578 M_ARGV(0, float) = freezetag_teams;
582 MUTATOR_HOOKFUNCTION(ft, SetWeaponArena)
584 // most weapons arena
585 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
586 M_ARGV(0, string) = "most";
589 MUTATOR_HOOKFUNCTION(ft, FragCenterMessage)
591 entity frag_attacker = M_ARGV(0, entity);
592 entity frag_target = M_ARGV(1, entity);
593 //float frag_deathtype = M_ARGV(2, float);
594 int kill_count_to_attacker = M_ARGV(3, int);
595 int kill_count_to_target = M_ARGV(4, int);
597 if(STAT(FROZEN, frag_target))
598 return; // target was already frozen, so this is just pushing them off the cliff
600 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));
601 Send_Notification(NOTIF_ONE, frag_target, MSG_CHOICE, CHOICE_FRAGGED_FREEZE, frag_attacker.netname, kill_count_to_target,
602 GetResourceAmount(frag_attacker, RESOURCE_HEALTH), GetResourceAmount(frag_attacker, RESOURCE_ARMOR), (IS_BOT_CLIENT(frag_attacker) ? -1 : CS(frag_attacker).ping));
607 void freezetag_Initialize()
609 freezetag_teams = autocvar_g_freezetag_teams_override;
610 if(freezetag_teams < 2)
611 freezetag_teams = cvar("g_freezetag_teams"); // read the cvar directly as it gets written earlier in the same frame
613 freezetag_teams = BITS(bound(2, freezetag_teams, 4));
614 GameRules_scoring(freezetag_teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, {
615 field(SP_FREEZETAG_REVIVALS, "revivals", 0);
618 round_handler_Spawn(freezetag_CheckTeams, freezetag_CheckWinner, func_null);
619 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
621 EliminatedPlayers_Init(freezetag_isEliminated);