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(float 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() == 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, world, 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, world, MSG_CENTER, CPID_MISSING_TEAMS);
110 prev_missing_teams_mask = -1;
113 float missing_teams_mask = (!redalive) + (!bluealive) * 2;
114 if(freezetag_teams >= 3) missing_teams_mask += (!yellowalive) * 4;
115 if(freezetag_teams >= 4) missing_teams_mask += (!pinkalive) * 8;
116 if(prev_missing_teams_mask != missing_teams_mask)
118 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
119 prev_missing_teams_mask = missing_teams_mask;
124 float freezetag_getWinnerTeam()
126 float winner_team = 0;
128 winner_team = NUM_TEAM_1;
131 if(winner_team) return 0;
132 winner_team = NUM_TEAM_2;
136 if(winner_team) return 0;
137 winner_team = NUM_TEAM_3;
141 if(winner_team) return 0;
142 winner_team = NUM_TEAM_4;
146 return -1; // no player left
149 float freezetag_CheckWinner()
151 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
153 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
154 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
155 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
156 it.freezetag_frozen_timeout = 0;
159 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
163 if(FREEZETAG_ALIVE_TEAMS() > 1)
166 int winner_team = freezetag_getWinnerTeam();
169 Send_Notification(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
170 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
171 TeamScore_AddToTeam(winner_team, ST_SCORE, +1);
173 else if(winner_team == -1)
175 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_TIED);
176 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_TIED);
179 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
180 it.freezetag_frozen_timeout = 0;
183 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
187 entity freezetag_LastPlayerForTeam()
189 entity last_pl = world;
190 FOREACH_CLIENT(IS_PLAYER(it) && it != self, LAMBDA(
192 if(!STAT(FROZEN, it))
193 if(SAME_TEAM(it, self))
202 void freezetag_LastPlayerForTeam_Notify()
204 if(round_handler_IsActive())
205 if(round_handler_IsRoundStarted())
207 entity pl = freezetag_LastPlayerForTeam();
209 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
213 void freezetag_Add_Score(entity attacker)
217 // you froze your own dumb self
218 // counted as "suicide" already
219 PlayerScore_Add(self, SP_SCORE, -1);
221 else if(IS_PLAYER(attacker))
223 // got frozen by an enemy
224 // counted as "kill" and "death" already
225 PlayerScore_Add(self, SP_SCORE, -1);
226 PlayerScore_Add(attacker, SP_SCORE, +1);
228 // else nothing - got frozen by the game type rules themselves
231 void freezetag_Freeze(entity attacker)
233 if(STAT(FROZEN, self))
236 if(autocvar_g_freezetag_frozen_maxtime > 0)
237 self.freezetag_frozen_timeout = time + autocvar_g_freezetag_frozen_maxtime;
239 Freeze(self, 0, 1, true);
241 freezetag_count_alive_players();
243 freezetag_Add_Score(attacker);
246 void freezetag_Unfreeze(entity attacker)
248 self.freezetag_frozen_time = 0;
249 self.freezetag_frozen_timeout = 0;
254 float freezetag_isEliminated(entity e)
256 if(IS_PLAYER(e) && (STAT(FROZEN, e) == 1 || IS_DEAD(e)))
266 void(entity this) havocbot_role_ft_freeing;
267 void(entity this) havocbot_role_ft_offense;
269 void havocbot_goalrating_freeplayers(entity this, float ratingscale, vector org, float sradius)
271 FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), LAMBDA(
272 if (STAT(FROZEN, it) == 1)
274 if(vdist(it.origin - org, >, sradius))
276 navigation_routerating(this, it, ratingscale, 2000);
280 // If teamate is not frozen still seek them out as fight better
282 navigation_routerating(this, it, ratingscale/3, 2000);
287 void havocbot_role_ft_offense(entity this)
292 if (!this.havocbot_role_timeout)
293 this.havocbot_role_timeout = time + random() * 10 + 20;
295 // Count how many players on team are unfrozen.
297 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, this) && !(STAT(FROZEN, it) != 1), LAMBDA(unfrozen++));
299 // If only one left on team or if role has timed out then start trying to free players.
300 if (((unfrozen == 0) && (!STAT(FROZEN, this))) || (time > this.havocbot_role_timeout))
302 LOG_TRACE("changing role to freeing\n");
303 this.havocbot_role = havocbot_role_ft_freeing;
304 this.havocbot_role_timeout = 0;
308 if (time > this.bot_strategytime)
310 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
312 navigation_goalrating_start(this);
313 havocbot_goalrating_items(this, 10000, this.origin, 10000);
314 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 10000);
315 havocbot_goalrating_freeplayers(this, 9000, this.origin, 10000);
316 //havocbot_goalrating_waypoints(1, this.origin, 1000);
317 navigation_goalrating_end(this);
321 void havocbot_role_ft_freeing(entity this)
326 if (!this.havocbot_role_timeout)
327 this.havocbot_role_timeout = time + random() * 10 + 20;
329 if (time > this.havocbot_role_timeout)
331 LOG_TRACE("changing role to offense\n");
332 this.havocbot_role = havocbot_role_ft_offense;
333 this.havocbot_role_timeout = 0;
337 if (time > this.bot_strategytime)
339 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
341 navigation_goalrating_start(this);
342 havocbot_goalrating_items(this, 8000, this.origin, 10000);
343 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
344 havocbot_goalrating_freeplayers(this, 20000, this.origin, 10000);
345 //havocbot_goalrating_waypoints(1, this.origin, 1000);
346 navigation_goalrating_end(this);
355 void ft_RemovePlayer(entity this)
357 this.health = 0; // neccessary to update correctly alive stats
358 if(!STAT(FROZEN, this))
359 freezetag_LastPlayerForTeam_Notify();
360 WITHSELF(this, freezetag_Unfreeze(world));
361 freezetag_count_alive_players();
364 MUTATOR_HOOKFUNCTION(ft, ClientDisconnect)
366 entity player = M_ARGV(0, entity);
368 ft_RemovePlayer(player);
372 MUTATOR_HOOKFUNCTION(ft, MakePlayerObserver)
374 entity player = M_ARGV(0, entity);
376 ft_RemovePlayer(player);
380 MUTATOR_HOOKFUNCTION(ft, PlayerDies)
382 entity frag_attacker = M_ARGV(1, entity);
383 entity frag_target = M_ARGV(2, entity);
384 float frag_deathtype = M_ARGV(2, float);
386 if(round_handler_IsActive())
387 if(round_handler_CountdownRunning())
389 if(STAT(FROZEN, frag_target))
390 WITHSELF(frag_target, freezetag_Unfreeze(world));
391 freezetag_count_alive_players();
392 return 1; // let the player die so that he can respawn whenever he wants
395 // Cases DEATH_TEAMCHANGE and DEATH_AUTOTEAMCHANGE are needed to fix a bug whe
396 // you succeed changing team through the menu: you both really die (gibbing) and get frozen
397 if(ITEM_DAMAGE_NEEDKILL(frag_deathtype)
398 || frag_deathtype == DEATH_TEAMCHANGE.m_id || frag_deathtype == DEATH_AUTOTEAMCHANGE.m_id)
400 // let the player die, he will be automatically frozen when he respawns
401 if(STAT(FROZEN, frag_target) != 1)
403 freezetag_Add_Score(frag_attacker);
404 freezetag_count_alive_players();
405 freezetag_LastPlayerForTeam_Notify();
408 WITHSELF(frag_target, freezetag_Unfreeze(world)); // remove ice
409 frag_target.health = 0; // Unfreeze resets health
410 frag_target.freezetag_frozen_timeout = -2; // freeze on respawn
414 if(STAT(FROZEN, frag_target))
417 WITHSELF(frag_target, freezetag_Freeze(frag_attacker));
418 freezetag_LastPlayerForTeam_Notify();
420 if(frag_attacker == frag_target || frag_attacker == world)
422 if(IS_PLAYER(frag_target))
423 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_SELF);
424 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_FREEZETAG_SELF, frag_target.netname);
428 if(IS_PLAYER(frag_target))
429 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_FROZEN, frag_attacker.netname);
430 if(IS_PLAYER(frag_attacker))
431 Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_FREEZETAG_FREEZE, frag_target.netname);
432 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_FREEZETAG_FREEZE, frag_target.netname, frag_attacker.netname);
438 MUTATOR_HOOKFUNCTION(ft, PlayerSpawn)
440 entity player = M_ARGV(0, entity);
442 if(player.freezetag_frozen_timeout == -1) // if PlayerSpawn is called by reset_map_players
443 return 1; // do nothing, round is starting right now
445 if(player.freezetag_frozen_timeout == -2) // player was dead
447 WITHSELF(player, freezetag_Freeze(world));
451 freezetag_count_alive_players();
453 if(round_handler_IsActive())
454 if(round_handler_IsRoundStarted())
456 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_SPAWN_LATE);
457 WITHSELF(player, freezetag_Freeze(world));
463 MUTATOR_HOOKFUNCTION(ft, reset_map_players)
465 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
467 it.freezetag_frozen_timeout = -1;
468 WITHSELF(it, PutClientInServer());
469 it.freezetag_frozen_timeout = 0;
471 freezetag_count_alive_players();
475 MUTATOR_HOOKFUNCTION(ft, GiveFragsForKill, CBC_ORDER_FIRST)
477 M_ARGV(2, float) = 0; // no frags counted in Freeze Tag
481 MUTATOR_HOOKFUNCTION(ft, PlayerPreThink, CBC_ORDER_FIRST)
486 entity player = M_ARGV(0, entity);
488 if(STAT(FROZEN, player) == 1)
491 player.pauseregen_finished = time + autocvar_g_balance_pause_health_regen;
494 if(round_handler_IsActive())
495 if(!round_handler_IsRoundStarted())
502 //if(STAT(FROZEN, player))
503 //if(player.freezetag_frozen_timeout > 0 && time < player.freezetag_frozen_timeout)
504 //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);
506 if(player.freezetag_frozen_timeout > 0 && time >= player.freezetag_frozen_timeout)
510 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
512 FOREACH_CLIENT(IS_PLAYER(it) && it != player, LAMBDA(
513 if(STAT(FROZEN, it) == 0)
515 if(SAME_TEAM(it, player))
516 if(boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
520 if(STAT(FROZEN, player) == 1)
528 if(n && STAT(FROZEN, player) == 1) // OK, there is at least one teammate reviving us
530 player.revive_progress = bound(0, player.revive_progress + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
531 player.health = max(1, player.revive_progress * ((warmup_stage) ? warmup_start_health : start_health));
533 if(player.revive_progress >= 1)
535 freezetag_Unfreeze(player);
536 freezetag_count_alive_players();
540 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_AUTO_REVIVED, autocvar_g_freezetag_frozen_maxtime);
541 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_FREEZETAG_AUTO_REVIVED, player.netname, autocvar_g_freezetag_frozen_maxtime);
545 // EVERY team mate nearby gets a point (even if multiple!)
546 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, LAMBDA(
547 PlayerScore_Add(it, SP_FREEZETAG_REVIVALS, +1);
548 PlayerScore_Add(it, SP_SCORE, +1);
549 nades_GiveBonus(it,autocvar_g_nades_bonus_score_low);
552 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
553 Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, player.netname);
554 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_FREEZETAG_REVIVED, player.netname, o.netname);
557 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, LAMBDA(
558 it.revive_progress = player.revive_progress;
562 else if(!n && STAT(FROZEN, player) == 1) // only if no teammate is nearby will we reset
564 player.revive_progress = bound(0, player.revive_progress - frametime * autocvar_g_freezetag_revive_clearspeed, 1);
565 player.health = max(1, player.revive_progress * ((warmup_stage) ? warmup_start_health : start_health));
567 else if(!n && !STAT(FROZEN, player))
569 player.revive_progress = 0; // thawing nobody
575 MUTATOR_HOOKFUNCTION(ft, SetStartItems)
577 start_items &= ~IT_UNLIMITED_AMMO;
578 //start_health = warmup_start_health = cvar("g_lms_start_health");
579 //start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
580 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
581 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
582 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
583 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
584 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
585 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
590 MUTATOR_HOOKFUNCTION(ft, HavocBot_ChooseRole)
592 entity bot = M_ARGV(0, entity);
597 bot.havocbot_role = havocbot_role_ft_freeing;
599 bot.havocbot_role = havocbot_role_ft_offense;
605 MUTATOR_HOOKFUNCTION(ft, GetTeamCount, CBC_ORDER_EXCLUSIVE)
607 M_ARGV(0, float) = freezetag_teams;
610 MUTATOR_HOOKFUNCTION(ft, SetWeaponArena)
612 // most weapons arena
613 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
614 M_ARGV(0, string) = "most";
617 void freezetag_Initialize()
619 freezetag_teams = autocvar_g_freezetag_teams_override;
620 if(freezetag_teams < 2)
621 freezetag_teams = autocvar_g_freezetag_teams;
622 freezetag_teams = bound(2, freezetag_teams, 4);
623 freezetag_ScoreRules(freezetag_teams);
625 round_handler_Spawn(freezetag_CheckTeams, freezetag_CheckWinner, func_null);
626 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
628 EliminatedPlayers_Init(freezetag_isEliminated);