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 void freezetag_ScoreRules(int teams)
66 ScoreRules_basics(teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, true); // SFL_SORT_PRIO_PRIMARY
67 ScoreInfo_SetLabel_PlayerScore(SP_FREEZETAG_REVIVALS, "revivals", 0);
68 ScoreRules_basics_end();
71 void freezetag_count_alive_players()
73 total_players = redalive = bluealive = yellowalive = pinkalive = 0;
74 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
77 case NUM_TEAM_1: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++redalive; break;
78 case NUM_TEAM_2: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++bluealive; break;
79 case NUM_TEAM_3: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++yellowalive; break;
80 case NUM_TEAM_4: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++pinkalive; break;
83 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
84 it.redalive_stat = redalive;
85 it.bluealive_stat = bluealive;
86 it.yellowalive_stat = yellowalive;
87 it.pinkalive_stat = pinkalive;
90 eliminatedPlayers.SendFlags |= 1;
92 #define FREEZETAG_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
93 #define FREEZETAG_ALIVE_TEAMS_OK() (FREEZETAG_ALIVE_TEAMS() == NumTeams(freezetag_teams))
95 float freezetag_CheckTeams()
97 static float prev_missing_teams_mask;
98 if(FREEZETAG_ALIVE_TEAMS_OK())
100 if(prev_missing_teams_mask > 0)
101 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
102 prev_missing_teams_mask = -1;
105 if(total_players == 0)
107 if(prev_missing_teams_mask > 0)
108 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
109 prev_missing_teams_mask = -1;
112 int missing_teams_mask = 0;
113 if(freezetag_teams & BIT(0))
114 missing_teams_mask += (!redalive) * 1;
115 if(freezetag_teams & BIT(1))
116 missing_teams_mask += (!bluealive) * 2;
117 if(freezetag_teams & BIT(2))
118 missing_teams_mask += (!yellowalive) * 4;
119 if(freezetag_teams & BIT(3))
120 missing_teams_mask += (!pinkalive) * 8;
121 if(prev_missing_teams_mask != missing_teams_mask)
123 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
124 prev_missing_teams_mask = missing_teams_mask;
129 float freezetag_getWinnerTeam()
131 float winner_team = 0;
133 winner_team = NUM_TEAM_1;
136 if(winner_team) return 0;
137 winner_team = NUM_TEAM_2;
141 if(winner_team) return 0;
142 winner_team = NUM_TEAM_3;
146 if(winner_team) return 0;
147 winner_team = NUM_TEAM_4;
151 return -1; // no player left
154 float freezetag_CheckWinner()
156 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
158 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
159 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
160 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
161 it.freezetag_frozen_timeout = 0;
164 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
168 if(FREEZETAG_ALIVE_TEAMS() > 1)
171 int winner_team = freezetag_getWinnerTeam();
174 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
175 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
176 TeamScore_AddToTeam(winner_team, ST_SCORE, +1);
178 else if(winner_team == -1)
180 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
181 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
184 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
185 it.freezetag_frozen_timeout = 0;
188 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
192 entity freezetag_LastPlayerForTeam(entity this)
194 entity last_pl = NULL;
195 FOREACH_CLIENT(IS_PLAYER(it) && it != this, LAMBDA(
197 if(!STAT(FROZEN, it))
198 if(SAME_TEAM(it, this))
207 void freezetag_LastPlayerForTeam_Notify(entity this)
209 if(round_handler_IsActive())
210 if(round_handler_IsRoundStarted())
212 entity pl = freezetag_LastPlayerForTeam(this);
214 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
218 void freezetag_Add_Score(entity targ, entity attacker)
222 // you froze your own dumb targ
223 // counted as "suicide" already
224 PlayerScore_Add(targ, SP_SCORE, -1);
226 else if(IS_PLAYER(attacker))
228 // got frozen by an enemy
229 // counted as "kill" and "death" already
230 PlayerScore_Add(targ, SP_SCORE, -1);
231 PlayerScore_Add(attacker, SP_SCORE, +1);
233 // else nothing - got frozen by the game type rules themselves
236 void freezetag_Freeze(entity targ, entity attacker)
238 if(STAT(FROZEN, targ))
241 if(autocvar_g_freezetag_frozen_maxtime > 0)
242 targ.freezetag_frozen_timeout = time + autocvar_g_freezetag_frozen_maxtime;
244 Freeze(targ, 0, 1, true);
246 freezetag_count_alive_players();
248 freezetag_Add_Score(targ, attacker);
251 void freezetag_Unfreeze(entity this)
253 this.freezetag_frozen_time = 0;
254 this.freezetag_frozen_timeout = 0;
259 float freezetag_isEliminated(entity e)
261 if(IS_PLAYER(e) && (STAT(FROZEN, e) == 1 || IS_DEAD(e)))
271 void(entity this) havocbot_role_ft_freeing;
272 void(entity this) havocbot_role_ft_offense;
274 void havocbot_goalrating_freeplayers(entity this, float ratingscale, vector org, float sradius)
276 FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), LAMBDA(
277 if (STAT(FROZEN, it) == 1)
279 if(vdist(it.origin - org, >, sradius))
281 navigation_routerating(this, it, ratingscale, 2000);
285 // If teamate is not frozen still seek them out as fight better
287 navigation_routerating(this, it, ratingscale/3, 2000);
292 void havocbot_role_ft_offense(entity this)
297 if (!this.havocbot_role_timeout)
298 this.havocbot_role_timeout = time + random() * 10 + 20;
300 // Count how many players on team are unfrozen.
302 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, this) && !(STAT(FROZEN, it) != 1), LAMBDA(unfrozen++));
304 // If only one left on team or if role has timed out then start trying to free players.
305 if (((unfrozen == 0) && (!STAT(FROZEN, this))) || (time > this.havocbot_role_timeout))
307 LOG_TRACE("changing role to freeing\n");
308 this.havocbot_role = havocbot_role_ft_freeing;
309 this.havocbot_role_timeout = 0;
313 if (time > this.bot_strategytime)
315 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
317 navigation_goalrating_start(this);
318 havocbot_goalrating_items(this, 10000, this.origin, 10000);
319 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 10000);
320 havocbot_goalrating_freeplayers(this, 9000, this.origin, 10000);
321 //havocbot_goalrating_waypoints(1, this.origin, 1000);
322 navigation_goalrating_end(this);
326 void havocbot_role_ft_freeing(entity this)
331 if (!this.havocbot_role_timeout)
332 this.havocbot_role_timeout = time + random() * 10 + 20;
334 if (time > this.havocbot_role_timeout)
336 LOG_TRACE("changing role to offense\n");
337 this.havocbot_role = havocbot_role_ft_offense;
338 this.havocbot_role_timeout = 0;
342 if (time > this.bot_strategytime)
344 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
346 navigation_goalrating_start(this);
347 havocbot_goalrating_items(this, 8000, this.origin, 10000);
348 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
349 havocbot_goalrating_freeplayers(this, 20000, this.origin, 10000);
350 //havocbot_goalrating_waypoints(1, this.origin, 1000);
351 navigation_goalrating_end(this);
360 void ft_RemovePlayer(entity this)
362 this.health = 0; // neccessary to update correctly alive stats
363 if(!STAT(FROZEN, this))
364 freezetag_LastPlayerForTeam_Notify(this);
365 freezetag_Unfreeze(this);
366 freezetag_count_alive_players();
369 MUTATOR_HOOKFUNCTION(ft, ClientDisconnect)
371 entity player = M_ARGV(0, entity);
373 ft_RemovePlayer(player);
377 MUTATOR_HOOKFUNCTION(ft, MakePlayerObserver)
379 entity player = M_ARGV(0, entity);
381 ft_RemovePlayer(player);
384 MUTATOR_HOOKFUNCTION(ft, PlayerDies)
386 entity frag_attacker = M_ARGV(1, entity);
387 entity frag_target = M_ARGV(2, entity);
388 float frag_deathtype = M_ARGV(2, float);
390 if(round_handler_IsActive())
391 if(round_handler_CountdownRunning())
393 if(STAT(FROZEN, frag_target))
394 freezetag_Unfreeze(frag_target);
395 freezetag_count_alive_players();
396 return true; // let the player die so that he can respawn whenever he wants
399 // Cases DEATH_TEAMCHANGE and DEATH_AUTOTEAMCHANGE are needed to fix a bug whe
400 // you succeed changing team through the menu: you both really die (gibbing) and get frozen
401 if(ITEM_DAMAGE_NEEDKILL(frag_deathtype)
402 || frag_deathtype == DEATH_TEAMCHANGE.m_id || frag_deathtype == DEATH_AUTOTEAMCHANGE.m_id)
404 // let the player die, he will be automatically frozen when he respawns
405 if(STAT(FROZEN, frag_target) != 1)
407 freezetag_Add_Score(frag_target, frag_attacker);
408 freezetag_count_alive_players();
409 freezetag_LastPlayerForTeam_Notify(frag_target);
412 freezetag_Unfreeze(frag_target); // remove ice
413 frag_target.health = 0; // Unfreeze resets health
414 frag_target.freezetag_frozen_timeout = -2; // freeze on respawn
418 if(STAT(FROZEN, frag_target))
421 freezetag_Freeze(frag_target, frag_attacker);
422 freezetag_LastPlayerForTeam_Notify(frag_target);
424 if(frag_attacker == frag_target || frag_attacker == NULL)
426 if(IS_PLAYER(frag_target))
427 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_SELF);
428 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_SELF, frag_target.netname);
432 if(IS_PLAYER(frag_target))
433 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_FROZEN, frag_attacker.netname);
434 if(IS_PLAYER(frag_attacker))
435 Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_FREEZETAG_FREEZE, frag_target.netname);
436 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_FREEZE, frag_target.netname, frag_attacker.netname);
442 MUTATOR_HOOKFUNCTION(ft, PlayerSpawn)
444 entity player = M_ARGV(0, entity);
446 if(player.freezetag_frozen_timeout == -1) // if PlayerSpawn is called by reset_map_players
447 return true; // do nothing, round is starting right now
449 if(player.freezetag_frozen_timeout == -2) // player was dead
451 freezetag_Freeze(player, NULL);
455 freezetag_count_alive_players();
457 if(round_handler_IsActive())
458 if(round_handler_IsRoundStarted())
460 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_SPAWN_LATE);
461 freezetag_Freeze(player, NULL);
467 MUTATOR_HOOKFUNCTION(ft, reset_map_players)
469 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
471 it.freezetag_frozen_timeout = -1;
472 PutClientInServer(it);
473 it.freezetag_frozen_timeout = 0;
475 freezetag_count_alive_players();
479 MUTATOR_HOOKFUNCTION(ft, GiveFragsForKill, CBC_ORDER_FIRST)
481 M_ARGV(2, float) = 0; // no frags counted in Freeze Tag
485 MUTATOR_HOOKFUNCTION(ft, PlayerPreThink, CBC_ORDER_FIRST)
490 entity player = M_ARGV(0, entity);
492 if(STAT(FROZEN, player) == 1)
495 player.pauseregen_finished = time + autocvar_g_balance_pause_health_regen;
498 if(round_handler_IsActive())
499 if(!round_handler_IsRoundStarted())
506 //if(STAT(FROZEN, player))
507 //if(player.freezetag_frozen_timeout > 0 && time < player.freezetag_frozen_timeout)
508 //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);
510 if(player.freezetag_frozen_timeout > 0 && time >= player.freezetag_frozen_timeout)
514 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
516 FOREACH_CLIENT(IS_PLAYER(it) && it != player, LAMBDA(
517 if(STAT(FROZEN, it) == 0)
519 if(SAME_TEAM(it, player))
520 if(boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
524 if(STAT(FROZEN, player) == 1)
532 if(n && STAT(FROZEN, player) == 1) // OK, there is at least one teammate reviving us
534 player.revive_progress = bound(0, player.revive_progress + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
535 player.health = max(1, player.revive_progress * ((warmup_stage) ? warmup_start_health : start_health));
537 if(player.revive_progress >= 1)
539 freezetag_Unfreeze(player);
540 freezetag_count_alive_players();
544 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_AUTO_REVIVED, autocvar_g_freezetag_frozen_maxtime);
545 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_AUTO_REVIVED, player.netname, autocvar_g_freezetag_frozen_maxtime);
549 // EVERY team mate nearby gets a point (even if multiple!)
550 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, LAMBDA(
551 PlayerScore_Add(it, SP_FREEZETAG_REVIVALS, +1);
552 PlayerScore_Add(it, SP_SCORE, +1);
553 nades_GiveBonus(it,autocvar_g_nades_bonus_score_low);
556 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
557 Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, player.netname);
558 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_REVIVED, player.netname, o.netname);
561 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, LAMBDA(
562 it.revive_progress = player.revive_progress;
566 else if(!n && STAT(FROZEN, player) == 1) // only if no teammate is nearby will we reset
568 player.revive_progress = bound(0, player.revive_progress - frametime * autocvar_g_freezetag_revive_clearspeed, 1);
569 player.health = max(1, player.revive_progress * ((warmup_stage) ? warmup_start_health : start_health));
571 else if(!n && !STAT(FROZEN, player))
573 player.revive_progress = 0; // thawing nobody
579 MUTATOR_HOOKFUNCTION(ft, SetStartItems)
581 start_items &= ~IT_UNLIMITED_AMMO;
582 //start_health = warmup_start_health = cvar("g_lms_start_health");
583 //start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
584 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
585 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
586 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
587 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
588 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
589 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
592 MUTATOR_HOOKFUNCTION(ft, HavocBot_ChooseRole)
594 entity bot = M_ARGV(0, entity);
599 bot.havocbot_role = havocbot_role_ft_freeing;
601 bot.havocbot_role = havocbot_role_ft_offense;
607 MUTATOR_HOOKFUNCTION(ft, GetTeamCount, CBC_ORDER_EXCLUSIVE)
609 M_ARGV(0, float) = freezetag_teams;
612 MUTATOR_HOOKFUNCTION(ft, SetWeaponArena)
614 // most weapons arena
615 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
616 M_ARGV(0, string) = "most";
619 void freezetag_Initialize()
621 freezetag_teams = autocvar_g_freezetag_teams_override;
622 if(freezetag_teams < 2)
623 freezetag_teams = autocvar_g_freezetag_teams;
624 freezetag_teams = bound(2, freezetag_teams, 4);
627 if(freezetag_teams >= 1) teams |= BIT(0);
628 if(freezetag_teams >= 2) teams |= BIT(1);
629 if(freezetag_teams >= 3) teams |= BIT(2);
630 if(freezetag_teams >= 4) teams |= BIT(3);
632 freezetag_teams = teams; // now set it?
633 freezetag_ScoreRules(freezetag_teams);
635 round_handler_Spawn(freezetag_CheckTeams, freezetag_CheckWinner, func_null);
636 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
638 EliminatedPlayers_Init(freezetag_isEliminated);