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 float 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 float 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 float 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_freeplayers(entity this, float ratingscale, vector org, float sradius)
232 FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), {
233 if (STAT(FROZEN, it) == 1)
235 if(vdist(it.origin - org, >, sradius))
237 navigation_routerating(this, it, ratingscale, 2000);
239 else if(vdist(it.origin - org, >, 400)) // avoid gathering all teammates in one place
241 // If teamate is not frozen still seek them out as fight better
243 t = 0.2 * 150 / (GetResourceAmount(this, RESOURCE_HEALTH) + GetResourceAmount(this, RESOURCE_ARMOR));
244 navigation_routerating(this, it, t * ratingscale, 2000);
249 void havocbot_role_ft_offense(entity this)
254 if (!this.havocbot_role_timeout)
255 this.havocbot_role_timeout = time + random() * 10 + 20;
257 // Count how many players on team are unfrozen.
259 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, this) && !(STAT(FROZEN, it) != 1), { unfrozen++; });
261 // If only one left on team or if role has timed out then start trying to free players.
262 if (((unfrozen == 0) && (!STAT(FROZEN, this))) || (time > this.havocbot_role_timeout))
264 LOG_TRACE("changing role to freeing");
265 this.havocbot_role = havocbot_role_ft_freeing;
266 this.havocbot_role_timeout = 0;
270 if (navigation_goalrating_timeout(this))
272 navigation_goalrating_start(this);
273 havocbot_goalrating_items(this, 10000, this.origin, 10000);
274 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 10000);
275 havocbot_goalrating_freeplayers(this, 9000, this.origin, 10000);
276 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
277 navigation_goalrating_end(this);
279 navigation_goalrating_timeout_set(this);
283 void havocbot_role_ft_freeing(entity this)
288 if (!this.havocbot_role_timeout)
289 this.havocbot_role_timeout = time + random() * 10 + 20;
291 if (time > this.havocbot_role_timeout)
293 LOG_TRACE("changing role to offense");
294 this.havocbot_role = havocbot_role_ft_offense;
295 this.havocbot_role_timeout = 0;
299 if (navigation_goalrating_timeout(this))
301 navigation_goalrating_start(this);
302 havocbot_goalrating_items(this, 8000, this.origin, 10000);
303 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
304 havocbot_goalrating_freeplayers(this, 20000, this.origin, 10000);
305 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
306 navigation_goalrating_end(this);
308 navigation_goalrating_timeout_set(this);
317 void ft_RemovePlayer(entity this)
319 SetResourceAmountExplicit(this, RESOURCE_HEALTH, 0); // neccessary to update correctly alive stats
320 if(!STAT(FROZEN, this))
321 freezetag_LastPlayerForTeam_Notify(this);
323 freezetag_count_alive_players();
326 MUTATOR_HOOKFUNCTION(ft, ClientDisconnect)
328 entity player = M_ARGV(0, entity);
330 ft_RemovePlayer(player);
334 MUTATOR_HOOKFUNCTION(ft, MakePlayerObserver)
336 entity player = M_ARGV(0, entity);
338 ft_RemovePlayer(player);
341 MUTATOR_HOOKFUNCTION(ft, PlayerDies)
343 entity frag_attacker = M_ARGV(1, entity);
344 entity frag_target = M_ARGV(2, entity);
345 float frag_deathtype = M_ARGV(3, float);
347 if(round_handler_IsActive())
348 if(round_handler_CountdownRunning())
350 if(STAT(FROZEN, frag_target))
351 Unfreeze(frag_target);
352 freezetag_count_alive_players();
353 return true; // let the player die so that he can respawn whenever he wants
356 // Cases DEATH_TEAMCHANGE and DEATH_AUTOTEAMCHANGE are needed to fix a bug whe
357 // you succeed changing team through the menu: you both really die (gibbing) and get frozen
358 if(ITEM_DAMAGE_NEEDKILL(frag_deathtype)
359 || frag_deathtype == DEATH_TEAMCHANGE.m_id || frag_deathtype == DEATH_AUTOTEAMCHANGE.m_id)
361 // let the player die, he will be automatically frozen when he respawns
362 if(STAT(FROZEN, frag_target) != 1)
364 freezetag_Add_Score(frag_target, frag_attacker);
365 freezetag_count_alive_players();
366 freezetag_LastPlayerForTeam_Notify(frag_target);
369 Unfreeze(frag_target); // remove ice
370 SetResourceAmountExplicit(frag_target, RESOURCE_HEALTH, 0); // Unfreeze resets health
371 frag_target.freezetag_frozen_timeout = -2; // freeze on respawn
375 if(STAT(FROZEN, frag_target))
378 freezetag_Freeze(frag_target, frag_attacker);
379 freezetag_LastPlayerForTeam_Notify(frag_target);
381 if(frag_attacker == frag_target || frag_attacker == NULL)
383 if(IS_PLAYER(frag_target))
384 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_SELF);
385 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_SELF, frag_target.netname);
389 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_FREEZE, frag_target.netname, frag_attacker.netname);
395 MUTATOR_HOOKFUNCTION(ft, PlayerSpawn)
397 entity player = M_ARGV(0, entity);
399 if(player.freezetag_frozen_timeout == -1) // if PlayerSpawn is called by reset_map_players
400 return true; // do nothing, round is starting right now
402 if(player.freezetag_frozen_timeout == -2) // player was dead
404 freezetag_Freeze(player, NULL);
408 freezetag_count_alive_players();
410 if(round_handler_IsActive())
411 if(round_handler_IsRoundStarted())
413 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_SPAWN_LATE);
414 freezetag_Freeze(player, NULL);
420 MUTATOR_HOOKFUNCTION(ft, reset_map_players)
422 FOREACH_CLIENT(IS_PLAYER(it), {
423 CS(it).killcount = 0;
424 it.freezetag_frozen_timeout = -1;
425 PutClientInServer(it);
426 it.freezetag_frozen_timeout = 0;
428 freezetag_count_alive_players();
432 MUTATOR_HOOKFUNCTION(ft, GiveFragsForKill, CBC_ORDER_FIRST)
434 M_ARGV(2, float) = 0; // no frags counted in Freeze Tag
438 MUTATOR_HOOKFUNCTION(ft, Unfreeze)
440 entity targ = M_ARGV(0, entity);
441 targ.freezetag_frozen_time = 0;
442 targ.freezetag_frozen_timeout = 0;
444 freezetag_count_alive_players();
447 MUTATOR_HOOKFUNCTION(ft, PlayerPreThink, CBC_ORDER_FIRST)
452 if(round_handler_IsActive())
453 if(!round_handler_IsRoundStarted())
458 entity player = M_ARGV(0, entity);
459 //if(STAT(FROZEN, player))
460 //if(player.freezetag_frozen_timeout > 0 && time < player.freezetag_frozen_timeout)
461 //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);
463 if(player.freezetag_frozen_timeout > 0 && time >= player.freezetag_frozen_timeout)
467 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
469 FOREACH_CLIENT(IS_PLAYER(it) && it != player, {
470 if(STAT(FROZEN, it) == 0)
472 if(SAME_TEAM(it, player))
473 if(boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
477 if(STAT(FROZEN, player) == 1)
485 if(n && STAT(FROZEN, player) == 1) // OK, there is at least one teammate reviving us
487 STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
488 SetResourceAmountExplicit(player, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health)));
490 if(STAT(REVIVE_PROGRESS, player) >= 1)
493 freezetag_count_alive_players();
497 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_AUTO_REVIVED, autocvar_g_freezetag_frozen_maxtime);
498 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_AUTO_REVIVED, player.netname, autocvar_g_freezetag_frozen_maxtime);
502 // EVERY team mate nearby gets a point (even if multiple!)
503 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, {
504 GameRules_scoring_add(it, FREEZETAG_REVIVALS, +1);
505 GameRules_scoring_add(it, SCORE, +1);
506 nades_GiveBonus(it,autocvar_g_nades_bonus_score_low);
509 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
510 Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, player.netname);
511 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_REVIVED, player.netname, o.netname);
514 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, {
515 STAT(REVIVE_PROGRESS, it) = STAT(REVIVE_PROGRESS, player);
519 else if(!n && STAT(FROZEN, player) == 1) // only if no teammate is nearby will we reset
521 STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) - frametime * autocvar_g_freezetag_revive_clearspeed, 1);
522 SetResourceAmountExplicit(player, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health)));
524 else if(!n && !STAT(FROZEN, player))
526 STAT(REVIVE_PROGRESS, player) = 0; // thawing nobody
532 MUTATOR_HOOKFUNCTION(ft, SetStartItems)
534 start_items &= ~IT_UNLIMITED_AMMO;
535 //start_health = warmup_start_health = cvar("g_lms_start_health");
536 //start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
537 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
538 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
539 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
540 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
541 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
542 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
545 MUTATOR_HOOKFUNCTION(ft, HavocBot_ChooseRole)
547 entity bot = M_ARGV(0, entity);
552 bot.havocbot_role = havocbot_role_ft_freeing;
554 bot.havocbot_role = havocbot_role_ft_offense;
560 MUTATOR_HOOKFUNCTION(ft, TeamBalance_CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
562 M_ARGV(0, float) = freezetag_teams;
566 MUTATOR_HOOKFUNCTION(ft, SetWeaponArena)
568 // most weapons arena
569 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
570 M_ARGV(0, string) = "most";
573 MUTATOR_HOOKFUNCTION(ft, FragCenterMessage)
575 entity frag_attacker = M_ARGV(0, entity);
576 entity frag_target = M_ARGV(1, entity);
577 //float frag_deathtype = M_ARGV(2, float);
578 int kill_count_to_attacker = M_ARGV(3, int);
579 int kill_count_to_target = M_ARGV(4, int);
581 if(STAT(FROZEN, frag_target))
582 return; // target was already frozen, so this is just pushing them off the cliff
584 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));
585 Send_Notification(NOTIF_ONE, frag_target, MSG_CHOICE, CHOICE_FRAGGED_FREEZE, frag_attacker.netname, kill_count_to_target,
586 GetResourceAmount(frag_attacker, RESOURCE_HEALTH), GetResourceAmount(frag_attacker, RESOURCE_ARMOR), (IS_BOT_CLIENT(frag_attacker) ? -1 : CS(frag_attacker).ping));
591 void freezetag_Initialize()
593 freezetag_teams = autocvar_g_freezetag_teams_override;
594 if(freezetag_teams < 2)
595 freezetag_teams = cvar("g_freezetag_teams"); // read the cvar directly as it gets written earlier in the same frame
597 freezetag_teams = BITS(bound(2, freezetag_teams, 4));
598 GameRules_scoring(freezetag_teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, {
599 field(SP_FREEZETAG_REVIVALS, "revivals", 0);
602 round_handler_Spawn(freezetag_CheckTeams, freezetag_CheckWinner, func_null);
603 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
605 EliminatedPlayers_Init(freezetag_isEliminated);