]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_freezetag.qc
Merge branch 'master' into terencehill/quickmenu
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_freezetag.qc
1 #include "gamemode_freezetag.qh"
2 #include "../_all.qh"
3
4 #include "gamemode.qh"
5
6 const float SP_FREEZETAG_REVIVALS = 4;
7 void freezetag_ScoreRules(float teams)
8 {
9         ScoreRules_basics(teams, SFL_SORT_PRIO_PRIMARY, SFL_SORT_PRIO_PRIMARY, true); // SFL_SORT_PRIO_PRIMARY
10         ScoreInfo_SetLabel_PlayerScore(SP_FREEZETAG_REVIVALS, "revivals", 0);
11         ScoreRules_basics_end();
12 }
13
14 void freezetag_count_alive_players()
15 {
16         entity e;
17         total_players = redalive = bluealive = yellowalive = pinkalive = 0;
18         FOR_EACH_PLAYER(e)
19         {
20                 switch(e.team)
21                 {
22                         case NUM_TEAM_1: ++total_players; if(e.health >= 1 && e.frozen != 1) ++redalive; break;
23                         case NUM_TEAM_2: ++total_players; if(e.health >= 1 && e.frozen != 1) ++bluealive; break;
24                         case NUM_TEAM_3: ++total_players; if(e.health >= 1 && e.frozen != 1) ++yellowalive; break;
25                         case NUM_TEAM_4: ++total_players; if(e.health >= 1 && e.frozen != 1) ++pinkalive; break;
26                 }
27         }
28         FOR_EACH_REALCLIENT(e)
29         {
30                 e.redalive_stat = redalive;
31                 e.bluealive_stat = bluealive;
32                 e.yellowalive_stat = yellowalive;
33                 e.pinkalive_stat = pinkalive;
34         }
35
36         eliminatedPlayers.SendFlags |= 1;
37 }
38 #define FREEZETAG_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0))
39 #define FREEZETAG_ALIVE_TEAMS_OK() (FREEZETAG_ALIVE_TEAMS() == freezetag_teams)
40
41 float freezetag_CheckTeams()
42 {
43         static float prev_missing_teams_mask;
44         if(FREEZETAG_ALIVE_TEAMS_OK())
45         {
46                 if(prev_missing_teams_mask > 0)
47                         Kill_Notification(NOTIF_ALL, world, MSG_CENTER_CPID, CPID_MISSING_TEAMS);
48                 prev_missing_teams_mask = -1;
49                 return 1;
50         }
51         if(total_players == 0)
52         {
53                 if(prev_missing_teams_mask > 0)
54                         Kill_Notification(NOTIF_ALL, world, MSG_CENTER_CPID, CPID_MISSING_TEAMS);
55                 prev_missing_teams_mask = -1;
56                 return 0;
57         }
58         float missing_teams_mask = (!redalive) + (!bluealive) * 2;
59         if(freezetag_teams >= 3) missing_teams_mask += (!yellowalive) * 4;
60         if(freezetag_teams >= 4) missing_teams_mask += (!pinkalive) * 8;
61         if(prev_missing_teams_mask != missing_teams_mask)
62         {
63                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask);
64                 prev_missing_teams_mask = missing_teams_mask;
65         }
66         return 0;
67 }
68
69 float freezetag_getWinnerTeam()
70 {
71         float winner_team = 0;
72         if(redalive >= 1)
73                 winner_team = NUM_TEAM_1;
74         if(bluealive >= 1)
75         {
76                 if(winner_team) return 0;
77                 winner_team = NUM_TEAM_2;
78         }
79         if(yellowalive >= 1)
80         {
81                 if(winner_team) return 0;
82                 winner_team = NUM_TEAM_3;
83         }
84         if(pinkalive >= 1)
85         {
86                 if(winner_team) return 0;
87                 winner_team = NUM_TEAM_4;
88         }
89         if(winner_team)
90                 return winner_team;
91         return -1; // no player left
92 }
93
94 float freezetag_CheckWinner()
95 {
96         entity e;
97         if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
98         {
99                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_OVER);
100                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_OVER);
101                 FOR_EACH_PLAYER(e)
102                 {
103                         e.freezetag_frozen_timeout = 0;
104                         nades_Clear(e);
105                 }
106                 round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
107                 return 1;
108         }
109
110         if(FREEZETAG_ALIVE_TEAMS() > 1)
111                 return 0;
112
113         float winner_team;
114         winner_team = freezetag_getWinnerTeam();
115         if(winner_team > 0)
116         {
117                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM_4(winner_team, CENTER_ROUND_TEAM_WIN_));
118                 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM_4(winner_team, INFO_ROUND_TEAM_WIN_));
119                 TeamScore_AddToTeam(winner_team, ST_SCORE, +1);
120         }
121         else if(winner_team == -1)
122         {
123                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_ROUND_TIED);
124                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ROUND_TIED);
125         }
126
127         FOR_EACH_PLAYER(e)
128         {
129                 e.freezetag_frozen_timeout = 0;
130                 nades_Clear(e);
131         }
132         round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
133         return 1;
134 }
135
136 entity freezetag_LastPlayerForTeam()
137 {
138         entity pl, last_pl = world;
139         FOR_EACH_PLAYER(pl)
140         {
141                 if(pl.health >= 1)
142                 if(!pl.frozen)
143                 if(pl != self)
144                 if(pl.team == self.team)
145                 if(!last_pl)
146                         last_pl = pl;
147                 else
148                         return world;
149         }
150         return last_pl;
151 }
152
153 void freezetag_LastPlayerForTeam_Notify()
154 {
155         if(round_handler_IsActive())
156         if(round_handler_IsRoundStarted())
157         {
158                 entity pl = freezetag_LastPlayerForTeam();
159                 if(pl)
160                         Send_Notification(NOTIF_ONE, pl, MSG_CENTER, CENTER_ALONE);
161         }
162 }
163
164 void freezetag_Add_Score(entity attacker)
165 {
166         if(attacker == self)
167         {
168                 // you froze your own dumb self
169                 // counted as "suicide" already
170                 PlayerScore_Add(self, SP_SCORE, -1);
171         }
172         else if(IS_PLAYER(attacker))
173         {
174                 // got frozen by an enemy
175                 // counted as "kill" and "death" already
176                 PlayerScore_Add(self, SP_SCORE, -1);
177                 PlayerScore_Add(attacker, SP_SCORE, +1);
178         }
179         // else nothing - got frozen by the game type rules themselves
180 }
181
182 void freezetag_Freeze(entity attacker)
183 {
184         if(self.frozen)
185                 return;
186
187         if(autocvar_g_freezetag_frozen_maxtime > 0)
188                 self.freezetag_frozen_timeout = time + autocvar_g_freezetag_frozen_maxtime;
189
190         Freeze(self, 0, 1, true);
191
192         freezetag_count_alive_players();
193
194         freezetag_Add_Score(attacker);
195 }
196
197 void freezetag_Unfreeze(entity attacker)
198 {
199         self.freezetag_frozen_time = 0;
200         self.freezetag_frozen_timeout = 0;
201
202         Unfreeze(self);
203 }
204
205 float freezetag_isEliminated(entity e)
206 {
207         if(IS_PLAYER(e) && (e.frozen == 1 || e.deadflag != DEAD_NO))
208                 return true;
209         return false;
210 }
211
212
213 // ================
214 // Bot player logic
215 // ================
216
217 void() havocbot_role_ft_freeing;
218 void() havocbot_role_ft_offense;
219
220 void havocbot_goalrating_freeplayers(float ratingscale, vector org, float sradius)
221 {
222         entity head;
223         float distance;
224
225         FOR_EACH_PLAYER(head)
226         {
227                 if ((head != self) && (head.team == self.team))
228                 {
229                         if (head.frozen == 1)
230                         {
231                                 distance = vlen(head.origin - org);
232                                 if (distance > sradius)
233                                         continue;
234                                 navigation_routerating(head, ratingscale, 2000);
235                         }
236                         else
237                         {
238                                 // If teamate is not frozen still seek them out as fight better
239                                 // in a group.
240                                 navigation_routerating(head, ratingscale/3, 2000);
241                         }
242                 }
243         }
244 }
245
246 void havocbot_role_ft_offense()
247 {
248         entity head;
249         float unfrozen;
250
251         if(self.deadflag != DEAD_NO)
252                 return;
253
254         if (!self.havocbot_role_timeout)
255                 self.havocbot_role_timeout = time + random() * 10 + 20;
256
257         // Count how many players on team are unfrozen.
258         unfrozen = 0;
259         FOR_EACH_PLAYER(head)
260         {
261                 if ((head.team == self.team) && (head.frozen != 1))
262                         unfrozen++;
263         }
264
265         // If only one left on team or if role has timed out then start trying to free players.
266         if (((unfrozen == 0) && (!self.frozen)) || (time > self.havocbot_role_timeout))
267         {
268                 LOG_TRACE("changing role to freeing\n");
269                 self.havocbot_role = havocbot_role_ft_freeing;
270                 self.havocbot_role_timeout = 0;
271                 return;
272         }
273
274         if (time > self.bot_strategytime)
275         {
276                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
277
278                 navigation_goalrating_start();
279                 havocbot_goalrating_items(10000, self.origin, 10000);
280                 havocbot_goalrating_enemyplayers(20000, self.origin, 10000);
281                 havocbot_goalrating_freeplayers(9000, self.origin, 10000);
282                 //havocbot_goalrating_waypoints(1, self.origin, 1000);
283                 navigation_goalrating_end();
284         }
285 }
286
287 void havocbot_role_ft_freeing()
288 {
289         if(self.deadflag != DEAD_NO)
290                 return;
291
292         if (!self.havocbot_role_timeout)
293                 self.havocbot_role_timeout = time + random() * 10 + 20;
294
295         if (time > self.havocbot_role_timeout)
296         {
297                 LOG_TRACE("changing role to offense\n");
298                 self.havocbot_role = havocbot_role_ft_offense;
299                 self.havocbot_role_timeout = 0;
300                 return;
301         }
302
303         if (time > self.bot_strategytime)
304         {
305                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
306
307                 navigation_goalrating_start();
308                 havocbot_goalrating_items(8000, self.origin, 10000);
309                 havocbot_goalrating_enemyplayers(10000, self.origin, 10000);
310                 havocbot_goalrating_freeplayers(20000, self.origin, 10000);
311                 //havocbot_goalrating_waypoints(1, self.origin, 1000);
312                 navigation_goalrating_end();
313         }
314 }
315
316
317 // ==============
318 // Hook Functions
319 // ==============
320
321 MUTATOR_HOOKFUNCTION(freezetag_RemovePlayer)
322 {
323         self.health = 0; // neccessary to update correctly alive stats
324         if(!self.frozen)
325                 freezetag_LastPlayerForTeam_Notify();
326         freezetag_Unfreeze(world);
327         freezetag_count_alive_players();
328         return 1;
329 }
330
331 MUTATOR_HOOKFUNCTION(freezetag_PlayerDies)
332 {
333         if(round_handler_IsActive())
334         if(round_handler_CountdownRunning())
335         {
336                 if(self.frozen)
337                         freezetag_Unfreeze(world);
338                 freezetag_count_alive_players();
339                 return 1; // let the player die so that he can respawn whenever he wants
340         }
341
342         // Cases DEATH_TEAMCHANGE and DEATH_AUTOTEAMCHANGE are needed to fix a bug whe
343         // you succeed changing team through the menu: you both really die (gibbing) and get frozen
344         if(ITEM_DAMAGE_NEEDKILL(frag_deathtype)
345                 || frag_deathtype == DEATH_TEAMCHANGE || frag_deathtype == DEATH_AUTOTEAMCHANGE)
346         {
347                 // let the player die, he will be automatically frozen when he respawns
348                 if(self.frozen != 1)
349                 {
350                         freezetag_Add_Score(frag_attacker);
351                         freezetag_count_alive_players();
352                         freezetag_LastPlayerForTeam_Notify();
353                 }
354                 else
355                         freezetag_Unfreeze(world); // remove ice
356                 self.health = 0; // Unfreeze resets health
357                 self.freezetag_frozen_timeout = -2; // freeze on respawn
358                 return 1;
359         }
360
361         if(self.frozen)
362                 return 1;
363
364         freezetag_Freeze(frag_attacker);
365         freezetag_LastPlayerForTeam_Notify();
366
367         if(frag_attacker == frag_target || frag_attacker == world)
368         {
369                 if(IS_PLAYER(frag_target))
370                         Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_SELF);
371                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_FREEZETAG_SELF, frag_target.netname);
372         }
373         else
374         {
375                 if(IS_PLAYER(frag_target))
376                         Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_FROZEN, frag_attacker.netname);
377                 if(IS_PLAYER(frag_attacker))
378                         Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_FREEZETAG_FREEZE, frag_target.netname);
379                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_FREEZETAG_FREEZE, frag_target.netname, frag_attacker.netname);
380         }
381
382         return 1;
383 }
384
385 MUTATOR_HOOKFUNCTION(freezetag_PlayerSpawn)
386 {
387         if(self.freezetag_frozen_timeout == -1) // if PlayerSpawn is called by reset_map_players
388                 return 1; // do nothing, round is starting right now
389
390         if(self.freezetag_frozen_timeout == -2) // player was dead
391         {
392                 freezetag_Freeze(world);
393                 return 1;
394         }
395
396         freezetag_count_alive_players();
397
398         if(round_handler_IsActive())
399         if(round_handler_IsRoundStarted())
400         {
401                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_FREEZETAG_SPAWN_LATE);
402                 freezetag_Freeze(world);
403         }
404
405         return 1;
406 }
407
408 MUTATOR_HOOKFUNCTION(freezetag_reset_map_players)
409 {
410         FOR_EACH_PLAYER(self)
411         {
412                 self.killcount = 0;
413                 self.freezetag_frozen_timeout = -1;
414                 PutClientInServer();
415                 self.freezetag_frozen_timeout = 0;
416         }
417         freezetag_count_alive_players();
418         return 1;
419 }
420
421 MUTATOR_HOOKFUNCTION(freezetag_GiveFragsForKill)
422 {
423         frag_score = 0; // no frags counted in Freeze Tag
424         return 1;
425 }
426
427 MUTATOR_HOOKFUNCTION(freezetag_PlayerPreThink)
428 {
429         float n;
430
431         if(gameover)
432                 return 1;
433
434         if(self.frozen == 1)
435         {
436                 // keep health = 1
437                 self.pauseregen_finished = time + autocvar_g_balance_pause_health_regen;
438         }
439
440         if(round_handler_IsActive())
441         if(!round_handler_IsRoundStarted())
442                 return 1;
443
444         entity o;
445         o = world;
446         //if(self.frozen)
447         //if(self.freezetag_frozen_timeout > 0 && time < self.freezetag_frozen_timeout)
448                 //self.iceblock.alpha = ICE_MIN_ALPHA + (ICE_MAX_ALPHA - ICE_MIN_ALPHA) * (self.freezetag_frozen_timeout - time) / (self.freezetag_frozen_timeout - self.freezetag_frozen_time);
449
450         if(self.freezetag_frozen_timeout > 0 && time >= self.freezetag_frozen_timeout)
451                 n = -1;
452         else
453         {
454                 vector revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
455                 n = 0;
456                 FOR_EACH_PLAYER(other)
457                 if(self != other)
458                 if(other.frozen == 0)
459                 if(other.deadflag == DEAD_NO)
460                 if(SAME_TEAM(other, self))
461                 if(boxesoverlap(self.absmin - revive_extra_size, self.absmax + revive_extra_size, other.absmin, other.absmax))
462                 {
463                         if(!o)
464                                 o = other;
465                         if(self.frozen == 1)
466                                 other.reviving = true;
467                         ++n;
468                 }
469         }
470
471         if(n && self.frozen == 1) // OK, there is at least one teammate reviving us
472         {
473                 self.revive_progress = bound(0, self.revive_progress + frametime * max(1/60, autocvar_g_freezetag_revive_speed), 1);
474                 self.health = max(1, self.revive_progress * ((warmup_stage) ? warmup_start_health : start_health));
475
476                 if(self.revive_progress >= 1)
477                 {
478                         freezetag_Unfreeze(self);
479                         freezetag_count_alive_players();
480
481                         if(n == -1)
482                         {
483                                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_FREEZETAG_AUTO_REVIVED, autocvar_g_freezetag_frozen_maxtime);
484                                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_FREEZETAG_AUTO_REVIVED, self.netname, autocvar_g_freezetag_frozen_maxtime);
485                                 return 1;
486                         }
487
488                         // EVERY team mate nearby gets a point (even if multiple!)
489                         FOR_EACH_PLAYER(other)
490                         {
491                                 if(other.reviving)
492                                 {
493                                         PlayerScore_Add(other, SP_FREEZETAG_REVIVALS, +1);
494                                         PlayerScore_Add(other, SP_SCORE, +1);
495
496                                         nades_GiveBonus(other,autocvar_g_nades_bonus_score_low);
497                                 }
498                         }
499
500                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
501                         Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, self.netname);
502                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_FREEZETAG_REVIVED, self.netname, o.netname);
503                 }
504
505                 FOR_EACH_PLAYER(other)
506                 {
507                         if(other.reviving)
508                         {
509                                 other.revive_progress = self.revive_progress;
510                                 other.reviving = false;
511                         }
512                 }
513         }
514         else if(!n && self.frozen == 1) // only if no teammate is nearby will we reset
515         {
516                 self.revive_progress = bound(0, self.revive_progress - frametime * autocvar_g_freezetag_revive_clearspeed, 1);
517                 self.health = max(1, self.revive_progress * ((warmup_stage) ? warmup_start_health : start_health));
518         }
519         else if(!n && !self.frozen)
520         {
521                 self.revive_progress = 0; // thawing nobody
522         }
523
524         return 1;
525 }
526
527 MUTATOR_HOOKFUNCTION(freezetag_SetStartItems)
528 {
529         start_items &= ~IT_UNLIMITED_AMMO;
530         //start_health       = warmup_start_health       = cvar("g_lms_start_health");
531         //start_armorvalue   = warmup_start_armorvalue   = cvar("g_lms_start_armor");
532         start_ammo_shells  = warmup_start_ammo_shells  = cvar("g_lms_start_ammo_shells");
533         start_ammo_nails   = warmup_start_ammo_nails   = cvar("g_lms_start_ammo_nails");
534         start_ammo_rockets = warmup_start_ammo_rockets = cvar("g_lms_start_ammo_rockets");
535         start_ammo_cells   = warmup_start_ammo_cells   = cvar("g_lms_start_ammo_cells");
536         start_ammo_plasma  = warmup_start_ammo_plasma  = cvar("g_lms_start_ammo_plasma");
537         start_ammo_fuel    = warmup_start_ammo_fuel    = cvar("g_lms_start_ammo_fuel");
538
539         return 0;
540 }
541
542 MUTATOR_HOOKFUNCTION(freezetag_BotRoles)
543 {
544         if (!self.deadflag)
545         {
546                 if (random() < 0.5)
547                         self.havocbot_role = havocbot_role_ft_freeing;
548                 else
549                         self.havocbot_role = havocbot_role_ft_offense;
550         }
551
552         return true;
553 }
554
555 MUTATOR_HOOKFUNCTION(freezetag_GetTeamCount)
556 {
557         ret_float = freezetag_teams;
558         return false;
559 }
560
561 void freezetag_Initialize()
562 {
563         freezetag_teams = autocvar_g_freezetag_teams_override;
564         if(freezetag_teams < 2)
565                 freezetag_teams = autocvar_g_freezetag_teams;
566         freezetag_teams = bound(2, freezetag_teams, 4);
567         freezetag_ScoreRules(freezetag_teams);
568
569         round_handler_Spawn(freezetag_CheckTeams, freezetag_CheckWinner, func_null);
570         round_handler_Init(5, autocvar_g_freezetag_warmup, autocvar_g_freezetag_round_timelimit);
571
572         addstat(STAT_REDALIVE, AS_INT, redalive_stat);
573         addstat(STAT_BLUEALIVE, AS_INT, bluealive_stat);
574         addstat(STAT_YELLOWALIVE, AS_INT, yellowalive_stat);
575         addstat(STAT_PINKALIVE, AS_INT, pinkalive_stat);
576
577         EliminatedPlayers_Init(freezetag_isEliminated);
578 }
579
580 MUTATOR_DEFINITION(gamemode_freezetag)
581 {
582         MUTATOR_HOOK(MakePlayerObserver, freezetag_RemovePlayer, CBC_ORDER_ANY);
583         MUTATOR_HOOK(ClientDisconnect, freezetag_RemovePlayer, CBC_ORDER_ANY);
584         MUTATOR_HOOK(PlayerDies, freezetag_PlayerDies, CBC_ORDER_ANY);
585         MUTATOR_HOOK(PlayerSpawn, freezetag_PlayerSpawn, CBC_ORDER_ANY);
586         MUTATOR_HOOK(reset_map_players, freezetag_reset_map_players, CBC_ORDER_ANY);
587         MUTATOR_HOOK(GiveFragsForKill, freezetag_GiveFragsForKill, CBC_ORDER_FIRST);
588         MUTATOR_HOOK(PlayerPreThink, freezetag_PlayerPreThink, CBC_ORDER_FIRST);
589         MUTATOR_HOOK(SetStartItems, freezetag_SetStartItems, CBC_ORDER_ANY);
590         MUTATOR_HOOK(HavocBot_ChooseRole, freezetag_BotRoles, CBC_ORDER_ANY);
591         MUTATOR_HOOK(GetTeamCount, freezetag_GetTeamCount, CBC_ORDER_EXCLUSIVE);
592
593         MUTATOR_ONADD
594         {
595                 if(time > 1) // game loads at time 1
596                         error("This is a game type and it cannot be added at runtime.");
597                 freezetag_Initialize();
598         }
599
600         MUTATOR_ONROLLBACK_OR_REMOVE
601         {
602                 // we actually cannot roll back freezetag_Initialize here
603                 // BUT: we don't need to! If this gets called, adding always
604                 // succeeds.
605         }
606
607         MUTATOR_ONREMOVE
608         {
609                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
610                 return -1;
611         }
612
613         return 0;
614 }