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