1 #include "freezetag.qh"
5 #include <server/resources.qh>
7 float autocvar_g_freezetag_frozen_maxtime;
8 float autocvar_g_freezetag_revive_clearspeed;
9 float autocvar_g_freezetag_round_timelimit;
10 //int autocvar_g_freezetag_teams;
11 int autocvar_g_freezetag_teams_override;
12 float autocvar_g_freezetag_warmup;
14 void freezetag_count_alive_players()
16 total_players = redalive = bluealive = yellowalive = pinkalive = 0;
17 FOREACH_CLIENT(IS_PLAYER(it), {
20 case NUM_TEAM_1: ++total_players; if(GetResourceAmount(it, RESOURCE_HEALTH) >= 1 && STAT(FROZEN, it) != 1) ++redalive; break;
21 case NUM_TEAM_2: ++total_players; if(GetResourceAmount(it, RESOURCE_HEALTH) >= 1 && STAT(FROZEN, it) != 1) ++bluealive; break;
22 case NUM_TEAM_3: ++total_players; if(GetResourceAmount(it, RESOURCE_HEALTH) >= 1 && STAT(FROZEN, it) != 1) ++yellowalive; break;
23 case NUM_TEAM_4: ++total_players; if(GetResourceAmount(it, RESOURCE_HEALTH) >= 1 && STAT(FROZEN, it) != 1) ++pinkalive; break;
26 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
27 STAT(REDALIVE, it) = redalive;
28 STAT(BLUEALIVE, it) = bluealive;
29 STAT(YELLOWALIVE, it) = yellowalive;
30 STAT(PINKALIVE, it) = pinkalive;
33 eliminatedPlayers.SendFlags |= 1;
35 #define FREEZETAG_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
36 #define FREEZETAG_ALIVE_TEAMS_OK() (FREEZETAG_ALIVE_TEAMS() == NumTeams(freezetag_teams))
38 float freezetag_CheckTeams()
40 static float prev_missing_teams_mask;
41 if(FREEZETAG_ALIVE_TEAMS_OK())
43 if(prev_missing_teams_mask > 0)
44 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
45 prev_missing_teams_mask = -1;
48 if(total_players == 0)
50 if(prev_missing_teams_mask > 0)
51 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_MISSING_TEAMS);
52 prev_missing_teams_mask = -1;
55 int missing_teams_mask = 0;
56 if(freezetag_teams & BIT(0))
57 missing_teams_mask += (!redalive) * 1;
58 if(freezetag_teams & BIT(1))
59 missing_teams_mask += (!bluealive) * 2;
60 if(freezetag_teams & BIT(2))
61 missing_teams_mask += (!yellowalive) * 4;
62 if(freezetag_teams & BIT(3))
63 missing_teams_mask += (!pinkalive) * 8;
64 if(prev_missing_teams_mask != missing_teams_mask)
66 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
67 prev_missing_teams_mask = missing_teams_mask;
72 float freezetag_getWinnerTeam()
74 float winner_team = 0;
76 winner_team = NUM_TEAM_1;
79 if(winner_team) return 0;
80 winner_team = NUM_TEAM_2;
84 if(winner_team) return 0;
85 winner_team = NUM_TEAM_3;
89 if(winner_team) return 0;
90 winner_team = NUM_TEAM_4;
94 return -1; // no player left
97 void nades_Clear(entity);
98 void nades_GiveBonus(entity player, float score);
100 float freezetag_CheckWinner()
102 if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
104 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_OVER);
105 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_OVER);
106 FOREACH_CLIENT(IS_PLAYER(it), {
107 it.freezetag_frozen_timeout = 0;
111 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
115 if(FREEZETAG_ALIVE_TEAMS() > 1)
118 int winner_team = freezetag_getWinnerTeam();
121 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
122 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
123 TeamScore_AddToTeam(winner_team, ST_SCORE, +1);
125 else if(winner_team == -1)
127 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
128 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
131 FOREACH_CLIENT(IS_PLAYER(it), {
132 it.freezetag_frozen_timeout = 0;
137 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
141 entity freezetag_LastPlayerForTeam(entity this)
143 entity last_pl = NULL;
144 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
145 if(GetResourceAmount(it, RESOURCE_HEALTH) >= 1)
146 if(!STAT(FROZEN, it))
147 if(SAME_TEAM(it, this))
156 void freezetag_LastPlayerForTeam_Notify(entity this)
158 if(round_handler_IsActive())
159 if(round_handler_IsRoundStarted())
161 entity pl = freezetag_LastPlayerForTeam(this);
163 Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
167 void freezetag_Add_Score(entity targ, entity attacker)
171 // you froze your own dumb targ
172 // counted as "suicide" already
173 GameRules_scoring_add(targ, SCORE, -1);
175 else if(IS_PLAYER(attacker))
177 // got frozen by an enemy
178 // counted as "kill" and "death" already
179 GameRules_scoring_add(targ, SCORE, -1);
180 GameRules_scoring_add(attacker, SCORE, +1);
182 // else nothing - got frozen by the game type rules themselves
185 void freezetag_Freeze(entity targ, entity attacker)
187 if(STAT(FROZEN, targ))
190 if(autocvar_g_freezetag_frozen_maxtime > 0)
191 targ.freezetag_frozen_timeout = time + autocvar_g_freezetag_frozen_maxtime;
193 Freeze(targ, 0, 1, true);
195 freezetag_count_alive_players();
197 freezetag_Add_Score(targ, attacker);
200 void freezetag_Unfreeze(entity this)
202 this.freezetag_frozen_time = 0;
203 this.freezetag_frozen_timeout = 0;
208 float freezetag_isEliminated(entity e)
210 if(IS_PLAYER(e) && (STAT(FROZEN, e) == 1 || IS_DEAD(e)))
220 void(entity this) havocbot_role_ft_freeing;
221 void(entity this) havocbot_role_ft_offense;
223 void havocbot_goalrating_ft_freeplayers(entity this, float ratingscale, vector org, float sradius)
225 entity best_pl = NULL;
226 float best_dist2 = FLOAT_MAX;
227 FOREACH_CLIENT(IS_PLAYER(it) && it != this && SAME_TEAM(it, this), {
228 if (STAT(FROZEN, it) == 1)
230 if(vdist(it.origin - org, >, sradius))
232 navigation_routerating(this, it, ratingscale, 2000);
235 && GetResourceAmount(it, RESOURCE_HEALTH) < GetResourceAmount(this, RESOURCE_HEALTH) + 30
236 && vlen2(it.origin - org) < best_dist2)
238 // If teamate is not frozen still seek them out as fight better
240 best_dist2 = vlen2(it.origin - org);
241 if (best_dist2 < 700 ** 2)
244 best_dist2 = 0; // already close to a teammate
251 navigation_routerating(this, best_pl, ratingscale / 2, 2000);
254 void havocbot_role_ft_offense(entity this)
259 if (!this.havocbot_role_timeout)
260 this.havocbot_role_timeout = time + random() * 10 + 20;
262 // Count how many players on team are unfrozen.
264 FOREACH_CLIENT(IS_PLAYER(it) && SAME_TEAM(it, this) && !STAT(FROZEN, it), { unfrozen++; });
266 // If only one left on team or if role has timed out then start trying to free players.
267 if ((unfrozen == 0 && !STAT(FROZEN, this)) || time > this.havocbot_role_timeout)
269 LOG_TRACE("changing role to freeing");
270 this.havocbot_role = havocbot_role_ft_freeing;
271 this.havocbot_role_timeout = 0;
275 if (navigation_goalrating_timeout(this))
277 navigation_goalrating_start(this);
278 havocbot_goalrating_items(this, 12000, this.origin, 10000);
279 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
280 havocbot_goalrating_ft_freeplayers(this, 9000, this.origin, 10000);
281 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
282 navigation_goalrating_end(this);
284 navigation_goalrating_timeout_set(this);
288 void havocbot_role_ft_freeing(entity this)
293 if (!this.havocbot_role_timeout)
294 this.havocbot_role_timeout = time + random() * 10 + 20;
296 if (time > this.havocbot_role_timeout)
298 LOG_TRACE("changing role to offense");
299 this.havocbot_role = havocbot_role_ft_offense;
300 this.havocbot_role_timeout = 0;
304 if (navigation_goalrating_timeout(this))
306 navigation_goalrating_start(this);
307 havocbot_goalrating_items(this, 10000, this.origin, 10000);
308 havocbot_goalrating_enemyplayers(this, 5000, this.origin, 10000);
309 havocbot_goalrating_ft_freeplayers(this, 20000, this.origin, 10000);
310 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
311 navigation_goalrating_end(this);
313 navigation_goalrating_timeout_set(this);
322 void ft_RemovePlayer(entity this)
324 SetResourceAmountExplicit(this, RESOURCE_HEALTH, 0); // neccessary to update correctly alive stats
325 if(!STAT(FROZEN, this))
326 freezetag_LastPlayerForTeam_Notify(this);
327 freezetag_Unfreeze(this);
328 freezetag_count_alive_players();
331 MUTATOR_HOOKFUNCTION(ft, ClientDisconnect)
333 entity player = M_ARGV(0, entity);
335 ft_RemovePlayer(player);
339 MUTATOR_HOOKFUNCTION(ft, MakePlayerObserver)
341 entity player = M_ARGV(0, entity);
343 ft_RemovePlayer(player);
346 MUTATOR_HOOKFUNCTION(ft, PlayerDies)
348 entity frag_attacker = M_ARGV(1, entity);
349 entity frag_target = M_ARGV(2, entity);
350 float frag_deathtype = M_ARGV(3, float);
352 if(round_handler_IsActive())
353 if(round_handler_CountdownRunning())
355 if(STAT(FROZEN, frag_target))
356 freezetag_Unfreeze(frag_target);
357 freezetag_count_alive_players();
358 return true; // let the player die so that he can respawn whenever he wants
361 // Cases DEATH_TEAMCHANGE and DEATH_AUTOTEAMCHANGE are needed to fix a bug whe
362 // you succeed changing team through the menu: you both really die (gibbing) and get frozen
363 if(ITEM_DAMAGE_NEEDKILL(frag_deathtype)
364 || frag_deathtype == DEATH_TEAMCHANGE.m_id || frag_deathtype == DEATH_AUTOTEAMCHANGE.m_id)
366 // let the player die, he will be automatically frozen when he respawns
367 if(STAT(FROZEN, frag_target) != 1)
369 freezetag_Add_Score(frag_target, frag_attacker);
370 freezetag_count_alive_players();
371 freezetag_LastPlayerForTeam_Notify(frag_target);
374 freezetag_Unfreeze(frag_target); // remove ice
375 SetResourceAmountExplicit(frag_target, RESOURCE_HEALTH, 0); // Unfreeze resets health
376 frag_target.freezetag_frozen_timeout = -2; // freeze on respawn
380 if(STAT(FROZEN, frag_target))
383 freezetag_Freeze(frag_target, frag_attacker);
384 freezetag_LastPlayerForTeam_Notify(frag_target);
386 if(frag_attacker == frag_target || frag_attacker == NULL)
388 if(IS_PLAYER(frag_target))
389 Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_SELF);
390 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_SELF, frag_target.netname);
394 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_FREEZE, frag_target.netname, frag_attacker.netname);
400 MUTATOR_HOOKFUNCTION(ft, PlayerSpawn)
402 entity player = M_ARGV(0, entity);
404 if(player.freezetag_frozen_timeout == -1) // if PlayerSpawn is called by reset_map_players
405 return true; // do nothing, round is starting right now
407 if(player.freezetag_frozen_timeout == -2) // player was dead
409 freezetag_Freeze(player, NULL);
413 freezetag_count_alive_players();
415 if(round_handler_IsActive())
416 if(round_handler_IsRoundStarted())
418 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_SPAWN_LATE);
419 freezetag_Freeze(player, NULL);
425 MUTATOR_HOOKFUNCTION(ft, reset_map_players)
427 FOREACH_CLIENT(IS_PLAYER(it), {
428 CS(it).killcount = 0;
429 it.freezetag_frozen_timeout = -1;
430 PutClientInServer(it);
431 it.freezetag_frozen_timeout = 0;
433 freezetag_count_alive_players();
437 MUTATOR_HOOKFUNCTION(ft, GiveFragsForKill, CBC_ORDER_FIRST)
439 M_ARGV(2, float) = 0; // no frags counted in Freeze Tag
443 MUTATOR_HOOKFUNCTION(ft, PlayerPreThink, CBC_ORDER_FIRST)
448 if(round_handler_IsActive())
449 if(!round_handler_IsRoundStarted())
454 entity player = M_ARGV(0, entity);
455 //if(STAT(FROZEN, player))
456 //if(player.freezetag_frozen_timeout > 0 && time < player.freezetag_frozen_timeout)
457 //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);
459 if(player.freezetag_frozen_timeout > 0 && time >= player.freezetag_frozen_timeout)
463 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
465 FOREACH_CLIENT(IS_PLAYER(it) && it != player, {
466 if(STAT(FROZEN, it) == 0)
468 if(SAME_TEAM(it, player))
469 if(boxesoverlap(player.absmin - revive_extra_size, player.absmax + revive_extra_size, it.absmin, it.absmax))
473 if(STAT(FROZEN, player) == 1)
481 if(n && STAT(FROZEN, player) == 1) // OK, there is at least one teammate reviving us
483 STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
484 SetResourceAmountExplicit(player, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health)));
486 if(STAT(REVIVE_PROGRESS, player) >= 1)
488 freezetag_Unfreeze(player);
489 freezetag_count_alive_players();
493 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_AUTO_REVIVED, autocvar_g_freezetag_frozen_maxtime);
494 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_AUTO_REVIVED, player.netname, autocvar_g_freezetag_frozen_maxtime);
498 // EVERY team mate nearby gets a point (even if multiple!)
499 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, {
500 GameRules_scoring_add(it, FREEZETAG_REVIVALS, +1);
501 GameRules_scoring_add(it, SCORE, +1);
502 nades_GiveBonus(it,autocvar_g_nades_bonus_score_low);
505 Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
506 Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, player.netname);
507 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_FREEZETAG_REVIVED, player.netname, o.netname);
510 FOREACH_CLIENT(IS_PLAYER(it) && it.reviving, {
511 STAT(REVIVE_PROGRESS, it) = STAT(REVIVE_PROGRESS, player);
515 else if(!n && STAT(FROZEN, player) == 1) // only if no teammate is nearby will we reset
517 STAT(REVIVE_PROGRESS, player) = bound(0, STAT(REVIVE_PROGRESS, player) - frametime * autocvar_g_freezetag_revive_clearspeed, 1);
518 SetResourceAmountExplicit(player, RESOURCE_HEALTH, max(1, STAT(REVIVE_PROGRESS, player) * ((warmup_stage) ? warmup_start_health : start_health)));
520 else if(!n && !STAT(FROZEN, player))
522 STAT(REVIVE_PROGRESS, player) = 0; // thawing nobody
528 MUTATOR_HOOKFUNCTION(ft, SetStartItems)
530 start_items &= ~IT_UNLIMITED_AMMO;
531 //start_health = warmup_start_health = cvar("g_lms_start_health");
532 //start_armorvalue = warmup_start_armorvalue = cvar("g_lms_start_armor");
533 start_ammo_shells = warmup_start_ammo_shells = cvar("g_lms_start_ammo_shells");
534 start_ammo_nails = warmup_start_ammo_nails = cvar("g_lms_start_ammo_nails");
535 start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
536 start_ammo_cells = warmup_start_ammo_cells = cvar("g_lms_start_ammo_cells");
537 start_ammo_plasma = warmup_start_ammo_plasma = cvar("g_lms_start_ammo_plasma");
538 start_ammo_fuel = warmup_start_ammo_fuel = cvar("g_lms_start_ammo_fuel");
541 MUTATOR_HOOKFUNCTION(ft, HavocBot_ChooseRole)
543 entity bot = M_ARGV(0, entity);
548 bot.havocbot_role = havocbot_role_ft_freeing;
550 bot.havocbot_role = havocbot_role_ft_offense;
553 // if bots spawn all at once assign them a more appropriated role after a while
554 if (CS(bot).jointime < time + 1)
555 bot.havocbot_role_timeout = time + 10 + random() * 10;
560 MUTATOR_HOOKFUNCTION(ft, CheckAllowedTeams, CBC_ORDER_EXCLUSIVE)
562 M_ARGV(0, float) = freezetag_teams;
565 MUTATOR_HOOKFUNCTION(ft, SetWeaponArena)
567 // most weapons arena
568 if(M_ARGV(0, string) == "0" || M_ARGV(0, string) == "")
569 M_ARGV(0, string) = "most";
572 MUTATOR_HOOKFUNCTION(ft, FragCenterMessage)
574 entity frag_attacker = M_ARGV(0, entity);
575 entity frag_target = M_ARGV(1, entity);
576 //float frag_deathtype = M_ARGV(2, float);
577 int kill_count_to_attacker = M_ARGV(3, int);
578 int kill_count_to_target = M_ARGV(4, int);
580 if(STAT(FROZEN, frag_target))
581 return; // target was already frozen, so this is just pushing them off the cliff
583 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));
584 Send_Notification(NOTIF_ONE, frag_target, MSG_CHOICE, CHOICE_FRAGGED_FREEZE, frag_attacker.netname, kill_count_to_target,
585 GetResourceAmount(frag_attacker, RESOURCE_HEALTH), GetResourceAmount(frag_attacker, RESOURCE_ARMOR), (IS_BOT_CLIENT(frag_attacker) ? -1 : CS(frag_attacker).ping));
590 void freezetag_Initialize()
592 freezetag_teams = autocvar_g_freezetag_teams_override;
593 if(freezetag_teams < 2)
594 freezetag_teams = cvar("g_freezetag_teams"); // read the cvar directly as it gets written earlier in the same frame
596 freezetag_teams = BITS(bound(2, freezetag_teams, 4));
597 GameRules_scoring(freezetag_teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, {
598 field(SP_FREEZETAG_REVIVALS, "revivals", 0);
601 round_handler_Spawn(freezetag_CheckTeams, freezetag_CheckWinner, func_null);
602 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
604 EliminatedPlayers_Init(freezetag_isEliminated);