1 #include "freezetag.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()
14 total_players = redalive = bluealive = yellowalive = pinkalive = 0;
15 FOREACH_CLIENT(IS_PLAYER(it), {
18 case NUM_TEAM_1: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++redalive; break;
19 case NUM_TEAM_2: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++bluealive; break;
20 case NUM_TEAM_3: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++yellowalive; break;
21 case NUM_TEAM_4: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++pinkalive; break;
24 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
25 STAT(REDALIVE, it) = redalive;
26 STAT(BLUEALIVE, it) = bluealive;
27 STAT(YELLOWALIVE, it) = yellowalive;
28 STAT(PINKALIVE, it) = pinkalive;
31 eliminatedPlayers.SendFlags |= 1;
33 #define FREEZETAG_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
34 #define FREEZETAG_ALIVE_TEAMS_OK() (FREEZETAG_ALIVE_TEAMS() == NumTeams(freezetag_teams))
36 float freezetag_CheckTeams()
38 static float prev_missing_teams_mask;
39 if(FREEZETAG_ALIVE_TEAMS_OK())
41 if(prev_missing_teams_mask > 0)
42 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
43 prev_missing_teams_mask = -1;
46 if(total_players == 0)
48 if(prev_missing_teams_mask > 0)
49 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
50 prev_missing_teams_mask = -1;
53 int missing_teams_mask = 0;
54 if(freezetag_teams & BIT(0))
55 missing_teams_mask += (!redalive) * 1;
56 if(freezetag_teams & BIT(1))
57 missing_teams_mask += (!bluealive) * 2;
58 if(freezetag_teams & BIT(2))
59 missing_teams_mask += (!yellowalive) * 4;
60 if(freezetag_teams & BIT(3))
61 missing_teams_mask += (!pinkalive) * 8;
62 if(prev_missing_teams_mask != missing_teams_mask)
64 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
65 prev_missing_teams_mask = missing_teams_mask;
70 float freezetag_getWinnerTeam()
72 float winner_team = 0;
74 winner_team = NUM_TEAM_1;
77 if(winner_team) return 0;
78 winner_team = NUM_TEAM_2;
82 if(winner_team) return 0;
83 winner_team = NUM_TEAM_3;
87 if(winner_team) return 0;
88 winner_team = NUM_TEAM_4;
92 return -1; // no player left
95 void nades_Clear(entity);
96 void nades_GiveBonus(entity player, float score);
98 float freezetag_CheckWinner()
100 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
102 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
103 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
104 FOREACH_CLIENT(IS_PLAYER(it), {
105 it.freezetag_frozen_timeout = 0;
109 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
113 if(FREEZETAG_ALIVE_TEAMS() > 1)
116 int winner_team = freezetag_getWinnerTeam();
119 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
120 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
121 TeamScore_AddToTeam(winner_team, ST_SCORE, +1);
123 else if(winner_team == -1)
125 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
126 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
129 FOREACH_CLIENT(IS_PLAYER(it), {
130 it.freezetag_frozen_timeout = 0;
135 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
139 entity freezetag_LastPlayerForTeam(entity this)
141 entity last_pl = NULL;
142 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
144 if(!STAT(FROZEN, it))
145 if(SAME_TEAM(it, this))
154 void freezetag_LastPlayerForTeam_Notify(entity this)
156 if(round_handler_IsActive())
157 if(round_handler_IsRoundStarted())
159 entity pl = freezetag_LastPlayerForTeam(this);
161 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
165 void freezetag_Add_Score(entity targ, entity attacker)
169 // you froze your own dumb targ
170 // counted as "suicide" already
171 GameRules_scoring_add(targ, SCORE, -1);
173 else if(IS_PLAYER(attacker))
175 // got frozen by an enemy
176 // counted as "kill" and "death" already
177 GameRules_scoring_add(targ, SCORE, -1);
178 GameRules_scoring_add(attacker, SCORE, +1);
180 // else nothing - got frozen by the game type rules themselves
183 void freezetag_Freeze(entity targ, entity attacker)
185 if(STAT(FROZEN, targ))
188 if(autocvar_g_freezetag_frozen_maxtime > 0)
189 targ.freezetag_frozen_timeout = time + autocvar_g_freezetag_frozen_maxtime;
191 Freeze(targ, 0, 1, true);
193 freezetag_count_alive_players();
195 freezetag_Add_Score(targ, attacker);
198 void freezetag_Unfreeze(entity this)
200 this.freezetag_frozen_time = 0;
201 this.freezetag_frozen_timeout = 0;
206 float freezetag_isEliminated(entity e)
208 if(IS_PLAYER(e) && (STAT(FROZEN, e) == 1 || IS_DEAD(e)))
218 void(entity this) havocbot_role_ft_freeing;
219 void(entity this) havocbot_role_ft_offense;
221 void havocbot_goalrating_freeplayers(entity this, float ratingscale, vector org, float sradius)
224 FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), {
225 if (STAT(FROZEN, it) == 1)
227 if(vdist(it.origin - org, >, sradius))
229 navigation_routerating(this, it, ratingscale, 2000);
231 else if(vdist(it.origin - org, >, 400)) // avoid gathering all teammates in one place
233 // If teamate is not frozen still seek them out as fight better
235 t = 0.2 * 150 / (this.health + this.armorvalue);
236 navigation_routerating(this, it, t * ratingscale, 2000);
241 void havocbot_role_ft_offense(entity this)
246 if (!this.havocbot_role_timeout)
247 this.havocbot_role_timeout = time + random() * 10 + 20;
249 // Count how many players on team are unfrozen.
251 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, this) && !(STAT(FROZEN, it) != 1), { unfrozen++; });
253 // If only one left on team or if role has timed out then start trying to free players.
254 if (((unfrozen == 0) && (!STAT(FROZEN, this))) || (time > this.havocbot_role_timeout))
256 LOG_TRACE("changing role to freeing");
257 this.havocbot_role = havocbot_role_ft_freeing;
258 this.havocbot_role_timeout = 0;
262 if (navigation_goalrating_timeout(this))
264 navigation_goalrating_start(this);
265 havocbot_goalrating_items(this, 10000, this.origin, 10000);
266 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 10000);
267 havocbot_goalrating_freeplayers(this, 9000, this.origin, 10000);
268 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
269 navigation_goalrating_end(this);
271 navigation_goalrating_timeout_set(this);
275 void havocbot_role_ft_freeing(entity this)
280 if (!this.havocbot_role_timeout)
281 this.havocbot_role_timeout = time + random() * 10 + 20;
283 if (time > this.havocbot_role_timeout)
285 LOG_TRACE("changing role to offense");
286 this.havocbot_role = havocbot_role_ft_offense;
287 this.havocbot_role_timeout = 0;
291 if (navigation_goalrating_timeout(this))
293 navigation_goalrating_start(this);
294 havocbot_goalrating_items(this, 8000, this.origin, 10000);
295 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
296 havocbot_goalrating_freeplayers(this, 20000, this.origin, 10000);
297 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
298 navigation_goalrating_end(this);
300 navigation_goalrating_timeout_set(this);
309 void ft_RemovePlayer(entity this)
311 this.health = 0; // neccessary to update correctly alive stats
312 if(!STAT(FROZEN, this))
313 freezetag_LastPlayerForTeam_Notify(this);
314 freezetag_Unfreeze(this);
315 freezetag_count_alive_players();
318 MUTATOR_HOOKFUNCTION(ft, ClientDisconnect)
320 entity player = M_ARGV(0, entity);
322 ft_RemovePlayer(player);
326 MUTATOR_HOOKFUNCTION(ft, MakePlayerObserver)
328 entity player = M_ARGV(0, entity);
330 ft_RemovePlayer(player);
333 MUTATOR_HOOKFUNCTION(ft, PlayerDies)
335 entity frag_attacker = M_ARGV(1, entity);
336 entity frag_target = M_ARGV(2, entity);
337 float frag_deathtype = M_ARGV(3, float);
339 if(round_handler_IsActive())
340 if(round_handler_CountdownRunning())
342 if(STAT(FROZEN, frag_target))
343 freezetag_Unfreeze(frag_target);
344 freezetag_count_alive_players();
345 return true; // let the player die so that he can respawn whenever he wants
348 // Cases DEATH_TEAMCHANGE and DEATH_AUTOTEAMCHANGE are needed to fix a bug whe
349 // you succeed changing team through the menu: you both really die (gibbing) and get frozen
350 if(ITEM_DAMAGE_NEEDKILL(frag_deathtype)
351 || frag_deathtype == DEATH_TEAMCHANGE.m_id || frag_deathtype == DEATH_AUTOTEAMCHANGE.m_id)
353 // let the player die, he will be automatically frozen when he respawns
354 if(STAT(FROZEN, frag_target) != 1)
356 freezetag_Add_Score(frag_target, frag_attacker);
357 freezetag_count_alive_players();
358 freezetag_LastPlayerForTeam_Notify(frag_target);
361 freezetag_Unfreeze(frag_target); // remove ice
362 frag_target.health = 0; // Unfreeze resets health
363 frag_target.freezetag_frozen_timeout = -2; // freeze on respawn
367 if(STAT(FROZEN, frag_target))
370 freezetag_Freeze(frag_target, frag_attacker);
371 freezetag_LastPlayerForTeam_Notify(frag_target);
373 if(frag_attacker == frag_target || frag_attacker == NULL)
375 if(IS_PLAYER(frag_target))
376 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_SELF);
377 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_SELF, frag_target.netname);
381 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_FREEZE, frag_target.netname, frag_attacker.netname);
387 MUTATOR_HOOKFUNCTION(ft, PlayerSpawn)
389 entity player = M_ARGV(0, entity);
391 if(player.freezetag_frozen_timeout == -1) // if PlayerSpawn is called by reset_map_players
392 return true; // do nothing, round is starting right now
394 if(player.freezetag_frozen_timeout == -2) // player was dead
396 freezetag_Freeze(player, NULL);
400 freezetag_count_alive_players();
402 if(round_handler_IsActive())
403 if(round_handler_IsRoundStarted())
405 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_SPAWN_LATE);
406 freezetag_Freeze(player, NULL);
412 MUTATOR_HOOKFUNCTION(ft, reset_map_players)
414 FOREACH_CLIENT(IS_PLAYER(it), {
415 CS(it).killcount = 0;
416 it.freezetag_frozen_timeout = -1;
417 PutClientInServer(it);
418 it.freezetag_frozen_timeout = 0;
420 freezetag_count_alive_players();
424 MUTATOR_HOOKFUNCTION(ft, GiveFragsForKill, CBC_ORDER_FIRST)
426 M_ARGV(2, float) = 0; // no frags counted in Freeze Tag
430 MUTATOR_HOOKFUNCTION(ft, PlayerPreThink, CBC_ORDER_FIRST)
435 if(round_handler_IsActive())
436 if(!round_handler_IsRoundStarted())
441 entity player = M_ARGV(0, entity);
442 //if(STAT(FROZEN, player))
443 //if(player.freezetag_frozen_timeout > 0 && time < player.freezetag_frozen_timeout)
444 //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);
446 if(player.freezetag_frozen_timeout > 0 && time >= player.freezetag_frozen_timeout)
450 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
452 FOREACH_CLIENT(IS_PLAYER(it) && it != player, {
453 if(STAT(FROZEN, it) == 0)
455 if(SAME_TEAM(it, player))
456 if(boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
460 if(STAT(FROZEN, player) == 1)
468 if(n && STAT(FROZEN, player) == 1) // OK, there is at least one teammate reviving us
470 STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
471 player.health = max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health));
473 if(STAT(REVIVE_PROGRESS, player) >= 1)
475 freezetag_Unfreeze(player);
476 freezetag_count_alive_players();
480 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_AUTO_REVIVED, autocvar_g_freezetag_frozen_maxtime);
481 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_AUTO_REVIVED, player.netname, autocvar_g_freezetag_frozen_maxtime);
485 // EVERY team mate nearby gets a point (even if multiple!)
486 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, {
487 GameRules_scoring_add(it, FREEZETAG_REVIVALS, +1);
488 GameRules_scoring_add(it, SCORE, +1);
489 nades_GiveBonus(it,autocvar_g_nades_bonus_score_low);
492 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
493 Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, player.netname);
494 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_REVIVED, player.netname, o.netname);
497 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, {
498 STAT(REVIVE_PROGRESS, it) = STAT(REVIVE_PROGRESS, player);
502 else if(!n && STAT(FROZEN, player) == 1) // only if no teammate is nearby will we reset
504 STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) - frametime * autocvar_g_freezetag_revive_clearspeed, 1);
505 player.health = max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health));
507 else if(!n && !STAT(FROZEN, player))
509 STAT(REVIVE_PROGRESS, player) = 0; // thawing nobody
515 MUTATOR_HOOKFUNCTION(ft, SetStartItems)
517 start_items &= ~IT_UNLIMITED_AMMO;
518 //start_health = warmup_start_health = cvar("g_lms_start_health");
519 //start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
520 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
521 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
522 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
523 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
524 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
525 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
528 MUTATOR_HOOKFUNCTION(ft, HavocBot_ChooseRole)
530 entity bot = M_ARGV(0, entity);
535 bot.havocbot_role = havocbot_role_ft_freeing;
537 bot.havocbot_role = havocbot_role_ft_offense;
543 MUTATOR_HOOKFUNCTION(ft, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
545 M_ARGV(0, float) = freezetag_teams;
548 MUTATOR_HOOKFUNCTION(ft, SetWeaponArena)
550 // most weapons arena
551 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
552 M_ARGV(0, string) = "most";
555 MUTATOR_HOOKFUNCTION(ft, FragCenterMessage)
557 entity frag_attacker = M_ARGV(0, entity);
558 entity frag_target = M_ARGV(1, entity);
559 //float frag_deathtype = M_ARGV(2, float);
560 int kill_count_to_attacker = M_ARGV(3, int);
561 int kill_count_to_target = M_ARGV(4, int);
563 if(STAT(FROZEN, frag_target))
564 return; // target was already frozen, so this is just pushing them off the cliff
566 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));
567 Send_Notification(NOTIF_ONE, frag_target, MSG_CHOICE, CHOICE_FRAGGED_FREEZE, frag_attacker.netname, kill_count_to_target, frag_attacker.health, frag_attacker.armorvalue, (IS_BOT_CLIENT(frag_attacker) ? -1 : CS(frag_attacker).ping));
572 void freezetag_Initialize()
574 freezetag_teams = autocvar_g_freezetag_teams_override;
575 if(freezetag_teams < 2)
576 freezetag_teams = cvar("g_freezetag_teams"); // read the cvar directly as it gets written earlier in the same frame
578 freezetag_teams = BITS(bound(2, freezetag_teams, 4));
579 GameRules_scoring(freezetag_teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, {
580 field(SP_FREEZETAG_REVIVALS, "revivals", 0);
583 round_handler_Spawn(freezetag_CheckTeams, freezetag_CheckWinner, func_null);
584 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
586 EliminatedPlayers_Init(freezetag_isEliminated);