1 #include "gamemode_freezetag.qh"
2 #ifndef GAMEMODE_FREEZETAG_H
3 #define GAMEMODE_FREEZETAG_H
5 int autocvar_g_freezetag_point_limit;
6 int autocvar_g_freezetag_point_leadlimit;
7 bool autocvar_g_freezetag_team_spawns;
8 void freezetag_Initialize();
10 REGISTER_MUTATOR(ft, false)
14 if (time > 1) // game loads at time 1
15 error("This is a game type and it cannot be added at runtime.");
16 freezetag_Initialize();
19 SetLimits(autocvar_g_freezetag_point_limit, autocvar_g_freezetag_point_leadlimit, autocvar_timelimit_override, -1);
21 if (autocvar_g_freezetag_team_spawns)
22 have_team_spawns = -1; // request team spawns
25 MUTATOR_ONROLLBACK_OR_REMOVE
27 // we actually cannot roll back freezetag_Initialize here
28 // BUT: we don't need to! If this gets called, adding always
34 LOG_INFO("This is a game type and it cannot be removed at runtime.");
41 .float freezetag_frozen_time;
42 .float freezetag_frozen_timeout;
43 const float ICE_MAX_ALPHA = 1;
44 const float ICE_MIN_ALPHA = 0.1;
45 float freezetag_teams;
47 .float reviving; // temp var
49 float autocvar_g_freezetag_revive_extra_size;
50 float autocvar_g_freezetag_revive_speed;
51 bool autocvar_g_freezetag_revive_nade;
52 float autocvar_g_freezetag_revive_nade_health;
57 float autocvar_g_freezetag_frozen_maxtime;
58 float autocvar_g_freezetag_revive_clearspeed;
59 float autocvar_g_freezetag_round_timelimit;
60 int autocvar_g_freezetag_teams;
61 int autocvar_g_freezetag_teams_override;
62 float autocvar_g_freezetag_warmup;
64 const float SP_FREEZETAG_REVIVALS = 4;
65 void freezetag_ScoreRules(int teams)
67 ScoreRules_basics(teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, true); // SFL_SORT_PRIO_PRIMARY
68 ScoreInfo_SetLabel_PlayerScore(SP_FREEZETAG_REVIVALS, "revivals", 0);
69 ScoreRules_basics_end();
72 void freezetag_count_alive_players()
74 total_players = redalive = bluealive = yellowalive = pinkalive = 0;
75 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
78 case NUM_TEAM_1: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++redalive; break;
79 case NUM_TEAM_2: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++bluealive; break;
80 case NUM_TEAM_3: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++yellowalive; break;
81 case NUM_TEAM_4: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++pinkalive; break;
84 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
85 it.redalive_stat = redalive;
86 it.bluealive_stat = bluealive;
87 it.yellowalive_stat = yellowalive;
88 it.pinkalive_stat = pinkalive;
91 eliminatedPlayers.SendFlags |= 1;
93 #define FREEZETAG_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
94 #define FREEZETAG_ALIVE_TEAMS_OK() (FREEZETAG_ALIVE_TEAMS() == NumTeams(freezetag_teams))
96 float freezetag_CheckTeams()
98 static float prev_missing_teams_mask;
99 if(FREEZETAG_ALIVE_TEAMS_OK())
101 if(prev_missing_teams_mask > 0)
102 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
103 prev_missing_teams_mask = -1;
106 if(total_players == 0)
108 if(prev_missing_teams_mask > 0)
109 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
110 prev_missing_teams_mask = -1;
113 int missing_teams_mask = 0;
114 if(freezetag_teams & BIT(0))
115 missing_teams_mask += (!redalive) * 1;
116 if(freezetag_teams & BIT(1))
117 missing_teams_mask += (!bluealive) * 2;
118 if(freezetag_teams & BIT(2))
119 missing_teams_mask += (!yellowalive) * 4;
120 if(freezetag_teams & BIT(3))
121 missing_teams_mask += (!pinkalive) * 8;
122 if(prev_missing_teams_mask != missing_teams_mask)
124 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
125 prev_missing_teams_mask = missing_teams_mask;
130 float freezetag_getWinnerTeam()
132 float winner_team = 0;
134 winner_team = NUM_TEAM_1;
137 if(winner_team) return 0;
138 winner_team = NUM_TEAM_2;
142 if(winner_team) return 0;
143 winner_team = NUM_TEAM_3;
147 if(winner_team) return 0;
148 winner_team = NUM_TEAM_4;
152 return -1; // no player left
155 float freezetag_CheckWinner()
157 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
159 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
160 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
161 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
162 it.freezetag_frozen_timeout = 0;
165 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
169 if(FREEZETAG_ALIVE_TEAMS() > 1)
172 int winner_team = freezetag_getWinnerTeam();
175 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
176 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
177 TeamScore_AddToTeam(winner_team, ST_SCORE, +1);
179 else if(winner_team == -1)
181 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
182 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
185 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
186 it.freezetag_frozen_timeout = 0;
189 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
193 entity freezetag_LastPlayerForTeam(entity this)
195 entity last_pl = NULL;
196 FOREACH_CLIENT(IS_PLAYER(it) && it != this, LAMBDA(
198 if(!STAT(FROZEN, it))
199 if(SAME_TEAM(it, this))
208 void freezetag_LastPlayerForTeam_Notify(entity this)
210 if(round_handler_IsActive())
211 if(round_handler_IsRoundStarted())
213 entity pl = freezetag_LastPlayerForTeam(this);
215 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
219 void freezetag_Add_Score(entity targ, entity attacker)
223 // you froze your own dumb targ
224 // counted as "suicide" already
225 PlayerScore_Add(targ, SP_SCORE, -1);
227 else if(IS_PLAYER(attacker))
229 // got frozen by an enemy
230 // counted as "kill" and "death" already
231 PlayerScore_Add(targ, SP_SCORE, -1);
232 PlayerScore_Add(attacker, SP_SCORE, +1);
234 // else nothing - got frozen by the game type rules themselves
237 void freezetag_Freeze(entity targ, entity attacker)
239 if(STAT(FROZEN, targ))
242 if(autocvar_g_freezetag_frozen_maxtime > 0)
243 targ.freezetag_frozen_timeout = time + autocvar_g_freezetag_frozen_maxtime;
245 Freeze(targ, 0, 1, true);
247 freezetag_count_alive_players();
249 freezetag_Add_Score(targ, attacker);
252 void freezetag_Unfreeze(entity this)
254 this.freezetag_frozen_time = 0;
255 this.freezetag_frozen_timeout = 0;
260 float freezetag_isEliminated(entity e)
262 if(IS_PLAYER(e) && (STAT(FROZEN, e) == 1 || IS_DEAD(e)))
272 void(entity this) havocbot_role_ft_freeing;
273 void(entity this) havocbot_role_ft_offense;
275 void havocbot_goalrating_freeplayers(entity this, float ratingscale, vector org, float sradius)
277 FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), LAMBDA(
278 if (STAT(FROZEN, it) == 1)
280 if(vdist(it.origin - org, >, sradius))
282 navigation_routerating(this, it, ratingscale, 2000);
286 // If teamate is not frozen still seek them out as fight better
288 navigation_routerating(this, it, ratingscale/3, 2000);
293 void havocbot_role_ft_offense(entity this)
298 if (!this.havocbot_role_timeout)
299 this.havocbot_role_timeout = time + random() * 10 + 20;
301 // Count how many players on team are unfrozen.
303 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, this) && !(STAT(FROZEN, it) != 1), LAMBDA(unfrozen++));
305 // If only one left on team or if role has timed out then start trying to free players.
306 if (((unfrozen == 0) && (!STAT(FROZEN, this))) || (time > this.havocbot_role_timeout))
308 LOG_TRACE("changing role to freeing\n");
309 this.havocbot_role = havocbot_role_ft_freeing;
310 this.havocbot_role_timeout = 0;
314 if (time > this.bot_strategytime)
316 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
318 navigation_goalrating_start(this);
319 havocbot_goalrating_items(this, 10000, this.origin, 10000);
320 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 10000);
321 havocbot_goalrating_freeplayers(this, 9000, this.origin, 10000);
322 //havocbot_goalrating_waypoints(1, this.origin, 1000);
323 navigation_goalrating_end(this);
327 void havocbot_role_ft_freeing(entity this)
332 if (!this.havocbot_role_timeout)
333 this.havocbot_role_timeout = time + random() * 10 + 20;
335 if (time > this.havocbot_role_timeout)
337 LOG_TRACE("changing role to offense\n");
338 this.havocbot_role = havocbot_role_ft_offense;
339 this.havocbot_role_timeout = 0;
343 if (time > this.bot_strategytime)
345 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
347 navigation_goalrating_start(this);
348 havocbot_goalrating_items(this, 8000, this.origin, 10000);
349 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
350 havocbot_goalrating_freeplayers(this, 20000, this.origin, 10000);
351 //havocbot_goalrating_waypoints(1, this.origin, 1000);
352 navigation_goalrating_end(this);
361 void ft_RemovePlayer(entity this)
363 this.health = 0; // neccessary to update correctly alive stats
364 if(!STAT(FROZEN, this))
365 freezetag_LastPlayerForTeam_Notify(this);
366 freezetag_Unfreeze(this);
367 freezetag_count_alive_players();
370 MUTATOR_HOOKFUNCTION(ft, ClientDisconnect)
372 entity player = M_ARGV(0, entity);
374 ft_RemovePlayer(player);
378 MUTATOR_HOOKFUNCTION(ft, MakePlayerObserver)
380 entity player = M_ARGV(0, entity);
382 ft_RemovePlayer(player);
385 MUTATOR_HOOKFUNCTION(ft, PlayerDies)
387 entity frag_attacker = M_ARGV(1, entity);
388 entity frag_target = M_ARGV(2, entity);
389 float frag_deathtype = M_ARGV(2, float);
391 if(round_handler_IsActive())
392 if(round_handler_CountdownRunning())
394 if(STAT(FROZEN, frag_target))
395 freezetag_Unfreeze(frag_target);
396 freezetag_count_alive_players();
397 return true; // let the player die so that he can respawn whenever he wants
400 // Cases DEATH_TEAMCHANGE and DEATH_AUTOTEAMCHANGE are needed to fix a bug whe
401 // you succeed changing team through the menu: you both really die (gibbing) and get frozen
402 if(ITEM_DAMAGE_NEEDKILL(frag_deathtype)
403 || frag_deathtype == DEATH_TEAMCHANGE.m_id || frag_deathtype == DEATH_AUTOTEAMCHANGE.m_id)
405 // let the player die, he will be automatically frozen when he respawns
406 if(STAT(FROZEN, frag_target) != 1)
408 freezetag_Add_Score(frag_target, frag_attacker);
409 freezetag_count_alive_players();
410 freezetag_LastPlayerForTeam_Notify(frag_target);
413 freezetag_Unfreeze(frag_target); // remove ice
414 frag_target.health = 0; // Unfreeze resets health
415 frag_target.freezetag_frozen_timeout = -2; // freeze on respawn
419 if(STAT(FROZEN, frag_target))
422 freezetag_Freeze(frag_target, frag_attacker);
423 freezetag_LastPlayerForTeam_Notify(frag_target);
425 if(frag_attacker == frag_target || frag_attacker == NULL)
427 if(IS_PLAYER(frag_target))
428 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_SELF);
429 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_SELF, frag_target.netname);
433 if(IS_PLAYER(frag_target))
434 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_FROZEN, frag_attacker.netname);
435 if(IS_PLAYER(frag_attacker))
436 Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_FREEZETAG_FREEZE, frag_target.netname);
437 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_FREEZE, frag_target.netname, frag_attacker.netname);
443 MUTATOR_HOOKFUNCTION(ft, PlayerSpawn)
445 entity player = M_ARGV(0, entity);
447 if(player.freezetag_frozen_timeout == -1) // if PlayerSpawn is called by reset_map_players
448 return true; // do nothing, round is starting right now
450 if(player.freezetag_frozen_timeout == -2) // player was dead
452 freezetag_Freeze(player, NULL);
456 freezetag_count_alive_players();
458 if(round_handler_IsActive())
459 if(round_handler_IsRoundStarted())
461 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_SPAWN_LATE);
462 freezetag_Freeze(player, NULL);
468 MUTATOR_HOOKFUNCTION(ft, reset_map_players)
470 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
472 it.freezetag_frozen_timeout = -1;
473 PutClientInServer(it);
474 it.freezetag_frozen_timeout = 0;
476 freezetag_count_alive_players();
480 MUTATOR_HOOKFUNCTION(ft, GiveFragsForKill, CBC_ORDER_FIRST)
482 M_ARGV(2, float) = 0; // no frags counted in Freeze Tag
486 MUTATOR_HOOKFUNCTION(ft, PlayerPreThink, CBC_ORDER_FIRST)
491 entity player = M_ARGV(0, entity);
493 if(STAT(FROZEN, player) == 1)
496 player.pauseregen_finished = time + autocvar_g_balance_pause_health_regen;
499 if(round_handler_IsActive())
500 if(!round_handler_IsRoundStarted())
507 //if(STAT(FROZEN, player))
508 //if(player.freezetag_frozen_timeout > 0 && time < player.freezetag_frozen_timeout)
509 //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);
511 if(player.freezetag_frozen_timeout > 0 && time >= player.freezetag_frozen_timeout)
515 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
517 FOREACH_CLIENT(IS_PLAYER(it) && it != player, LAMBDA(
518 if(STAT(FROZEN, it) == 0)
520 if(SAME_TEAM(it, player))
521 if(boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
525 if(STAT(FROZEN, player) == 1)
533 if(n && STAT(FROZEN, player) == 1) // OK, there is at least one teammate reviving us
535 player.revive_progress = bound(0, player.revive_progress + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
536 player.health = max(1, player.revive_progress * ((warmup_stage) ? warmup_start_health : start_health));
538 if(player.revive_progress >= 1)
540 freezetag_Unfreeze(player);
541 freezetag_count_alive_players();
545 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_AUTO_REVIVED, autocvar_g_freezetag_frozen_maxtime);
546 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_AUTO_REVIVED, player.netname, autocvar_g_freezetag_frozen_maxtime);
550 // EVERY team mate nearby gets a point (even if multiple!)
551 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, LAMBDA(
552 PlayerScore_Add(it, SP_FREEZETAG_REVIVALS, +1);
553 PlayerScore_Add(it, SP_SCORE, +1);
554 nades_GiveBonus(it,autocvar_g_nades_bonus_score_low);
557 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
558 Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, player.netname);
559 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_REVIVED, player.netname, o.netname);
562 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, LAMBDA(
563 it.revive_progress = player.revive_progress;
567 else if(!n && STAT(FROZEN, player) == 1) // only if no teammate is nearby will we reset
569 player.revive_progress = bound(0, player.revive_progress - frametime * autocvar_g_freezetag_revive_clearspeed, 1);
570 player.health = max(1, player.revive_progress * ((warmup_stage) ? warmup_start_health : start_health));
572 else if(!n && !STAT(FROZEN, player))
574 player.revive_progress = 0; // thawing nobody
580 MUTATOR_HOOKFUNCTION(ft, SetStartItems)
582 start_items &= ~IT_UNLIMITED_AMMO;
583 //start_health = warmup_start_health = cvar("g_lms_start_health");
584 //start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
585 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
586 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
587 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
588 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
589 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
590 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
593 MUTATOR_HOOKFUNCTION(ft, HavocBot_ChooseRole)
595 entity bot = M_ARGV(0, entity);
600 bot.havocbot_role = havocbot_role_ft_freeing;
602 bot.havocbot_role = havocbot_role_ft_offense;
608 MUTATOR_HOOKFUNCTION(ft, GetTeamCount, CBC_ORDER_EXCLUSIVE)
610 M_ARGV(0, float) = freezetag_teams;
613 MUTATOR_HOOKFUNCTION(ft, SetWeaponArena)
615 // most weapons arena
616 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
617 M_ARGV(0, string) = "most";
620 void freezetag_Initialize()
622 freezetag_teams = autocvar_g_freezetag_teams_override;
623 if(freezetag_teams < 2)
624 freezetag_teams = autocvar_g_freezetag_teams;
625 freezetag_teams = bound(2, freezetag_teams, 4);
628 if(freezetag_teams >= 1) teams |= BIT(0);
629 if(freezetag_teams >= 2) teams |= BIT(1);
630 if(freezetag_teams >= 3) teams |= BIT(2);
631 if(freezetag_teams >= 4) teams |= BIT(3);
633 freezetag_teams = teams; // now set it?
634 freezetag_ScoreRules(freezetag_teams);
636 round_handler_Spawn(freezetag_CheckTeams, freezetag_CheckWinner, func_null);
637 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
639 EliminatedPlayers_Init(freezetag_isEliminated);