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