]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_freezetag.qc
Merge remote-tracking branch 'origin/master' into samual/notification_rewrite
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_freezetag.qc
1 void freezetag_Initialize()
2 {
3         precache_model("models/ice/ice.md3");
4         warmup = time + autocvar_g_start_delay + autocvar_g_freezetag_warmup;
5         ScoreRules_freezetag();
6 }
7
8 void freezetag_CheckWinner()
9 {
10         if(time <= game_starttime) // game didn't even start yet! nobody can win in that case.
11                 return;
12
13         if(next_round || (time > warmup - autocvar_g_freezetag_warmup && time < warmup))
14                 return; // already waiting for next round to start
15
16         if((redalive >= 1 && bluealive >= 1)
17                 || (redalive >= 1 && yellowalive >= 1)
18                 || (redalive >= 1 && pinkalive >= 1)
19                 || (bluealive >= 1 && yellowalive >= 1)
20                 || (bluealive >= 1 && pinkalive >= 1)
21                 || (yellowalive >= 1 && pinkalive >= 1))
22                 return; // we still have active players on two or more teams, nobody won yet
23
24         entity e, winner;
25         winner = world;
26
27         FOR_EACH_PLAYER(e)
28         {
29                 if(e.freezetag_frozen == 0 && e.health >= 1) // here's one player from the winning team... good
30                 {
31                         winner = e;
32                         break; // break, we found the winner
33                 }
34         }
35
36         if(winner != world) // just in case a winner wasn't found
37         {
38                 Send_Notification_Legacy_Wrapper(NOTIF_ANY, world, MSG_CENTER, APP_TEAM_NUM_4(winner.team, CENTER_FREEZETAG_ROUND_WIN_), NO_STR_ARG, NO_STR_ARG, NO_FL_ARG, NO_FL_ARG, NO_FL_ARG);
39                 Send_Notification_Legacy_Wrapper(NOTIF_ANY, world, MSG_INFO, APP_TEAM_NUM_4(winner.team, INFO_FREEZETAG_ROUND_WIN_), NO_STR_ARG, NO_STR_ARG, NO_FL_ARG, NO_FL_ARG, NO_FL_ARG);
40                 TeamScore_AddToTeam(winner.team, ST_SCORE, +1);
41         }
42
43         next_round = time + 5;
44 }
45
46 // this is needed to allow the player to turn his view around (fixangle can't
47 // be used to freeze his view, as that also changes the angles), while not
48 // turning that ice object with the player
49 void freezetag_Ice_Think()
50 {
51         setorigin(self, self.owner.origin - '0 0 16');
52         self.nextthink = time;
53 }
54
55 void freezetag_Freeze(entity attacker)
56 {
57         if(self.freezetag_frozen)
58                 return;
59         self.freezetag_frozen = 1;
60         self.freezetag_revive_progress = 0;
61         self.health = 1;
62
63         entity ice;
64         ice = spawn();
65         ice.owner = self;
66         ice.classname = "freezetag_ice";
67         ice.think = freezetag_Ice_Think;
68         ice.nextthink = time;
69         ice.frame = floor(random() * 21); // ice model has 20 different looking frames
70         setmodel(ice, "models/ice/ice.md3");
71
72         entity oldself;
73         oldself = self;
74         self = ice;
75         freezetag_Ice_Think();
76         self = oldself;
77
78         RemoveGrapplingHook(self);
79
80         // add waypoint
81         WaypointSprite_Spawn("freezetag_frozen", 0, 0, self, '0 0 64', world, self.team, self, waypointsprite_attached, TRUE, RADARICON_WAYPOINT, '0.25 0.90 1');
82
83         if(attacker == self)
84         {
85                 // you froze your own dumb self
86                 // counted as "suicide" already
87                 PlayerScore_Add(self, SP_SCORE, -1);
88         }
89         else if(attacker.classname == "player")
90         {
91                 // got frozen by an enemy
92                 // counted as "kill" and "death" already
93                 PlayerScore_Add(self, SP_SCORE, -1);
94                 PlayerScore_Add(attacker, SP_SCORE, +1);
95         }
96         else
97         {
98                 // nothing - got frozen by the game type rules themselves
99         }
100 }
101
102 void freezetag_Unfreeze(entity attacker)
103 {
104         self.freezetag_frozen = 0;
105         self.freezetag_revive_progress = 0;
106         self.health = autocvar_g_balance_health_start;
107
108         // remove the ice block
109         entity ice;
110         for(ice = world; (ice = find(ice, classname, "freezetag_ice")); ) if(ice.owner == self)
111         {
112                 remove(ice);
113                 break;
114         }
115
116         // remove waypoint
117         if(self.waypointsprite_attached)
118                 WaypointSprite_Kill(self.waypointsprite_attached);
119 }
120
121
122 // ================
123 // Bot player logic
124 // ================
125
126 void() havocbot_role_ft_freeing;
127 void() havocbot_role_ft_offense;
128
129 void havocbot_goalrating_freeplayers(float ratingscale, vector org, float sradius)
130 {
131         entity head;
132         float distance;
133
134         FOR_EACH_PLAYER(head)
135         {
136                 if ((head != self) && (head.team == self.team))
137                 {
138                         if (head.freezetag_frozen)
139                         {
140                                 distance = vlen(head.origin - org);
141                                 if (distance > sradius)
142                                         continue;
143                                 navigation_routerating(head, ratingscale, 2000);
144                         }
145                         else
146                         {
147                                 // If teamate is not frozen still seek them out as fight better
148                                 // in a group.
149                                 navigation_routerating(head, ratingscale/3, 2000);
150                         }
151                 }
152         }
153 }
154
155 void havocbot_role_ft_offense()
156 {
157         entity head;
158         float unfrozen;
159
160         if(self.deadflag != DEAD_NO)
161                 return;
162
163         if (!self.havocbot_role_timeout)
164                 self.havocbot_role_timeout = time + random() * 10 + 20;
165
166         // Count how many players on team are unfrozen.
167         unfrozen = 0;
168         FOR_EACH_PLAYER(head)
169         {
170                 if ((head.team == self.team) && (!head.freezetag_frozen))
171                         unfrozen++;
172         }
173
174         // If only one left on team or if role has timed out then start trying to free players.
175         if (((unfrozen == 0) && (!self.freezetag_frozen)) || (time > self.havocbot_role_timeout))
176         {
177                 dprint("changing role to freeing\n");
178                 self.havocbot_role = havocbot_role_ft_freeing;
179                 self.havocbot_role_timeout = 0;
180                 return;
181         }
182
183         if (time > self.bot_strategytime)
184         {
185                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
186
187                 navigation_goalrating_start();
188                 havocbot_goalrating_items(10000, self.origin, 10000);
189                 havocbot_goalrating_enemyplayers(20000, self.origin, 10000);
190                 havocbot_goalrating_freeplayers(9000, self.origin, 10000);
191                 //havocbot_goalrating_waypoints(1, self.origin, 1000);
192                 navigation_goalrating_end();
193         }
194 }
195
196 void havocbot_role_ft_freeing()
197 {
198         if(self.deadflag != DEAD_NO)
199                 return;
200
201         if (!self.havocbot_role_timeout)
202                 self.havocbot_role_timeout = time + random() * 10 + 20;
203
204         if (time > self.havocbot_role_timeout)
205         {
206                 dprint("changing role to offense\n");
207                 self.havocbot_role = havocbot_role_ft_offense;
208                 self.havocbot_role_timeout = 0;
209                 return;
210         }
211
212         if (time > self.bot_strategytime)
213         {
214                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
215
216                 navigation_goalrating_start();
217                 havocbot_goalrating_items(8000, self.origin, 10000);
218                 havocbot_goalrating_enemyplayers(10000, self.origin, 10000);
219                 havocbot_goalrating_freeplayers(20000, self.origin, 10000);
220                 //havocbot_goalrating_waypoints(1, self.origin, 1000);
221                 navigation_goalrating_end();
222         }
223 }
224
225
226 // ==============
227 // Hook Functions
228 // ==============
229
230 MUTATOR_HOOKFUNCTION(freezetag_RemovePlayer)
231 {
232         if(self.freezetag_frozen == 0 && self.health >= 1)
233         {
234                 if(self.team == FL_TEAM_1)
235                         --redalive;
236                 else if(self.team == FL_TEAM_2)
237                         --bluealive;
238                 else if(self.team == FL_TEAM_3)
239                         --yellowalive;
240                 else if(self.team == FL_TEAM_4)
241                         --pinkalive;
242                 --totalalive;
243         }
244
245         if(total_players > 2) // only check for winners if we had more than two players (one of them left, don't let the other player win just because of that)
246                 freezetag_CheckWinner();
247
248         freezetag_Unfreeze(world);
249
250         return 1;
251 }
252
253 MUTATOR_HOOKFUNCTION(freezetag_PlayerDies)
254 {
255         if(self.freezetag_frozen == 0)
256         {
257                 if(self.team == FL_TEAM_1)
258                         --redalive;
259                 else if(self.team == FL_TEAM_2)
260                         --bluealive;
261                 else if(self.team == FL_TEAM_3)
262                         --yellowalive;
263                 else if(self.team == FL_TEAM_4)
264                         --pinkalive;
265                 --totalalive;
266
267                 freezetag_Freeze(frag_attacker);
268         }
269
270         if(frag_attacker == frag_target || frag_attacker == world)
271         {
272                 if(frag_target.classname == STR_PLAYER)
273                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_SELF, NO_STR_ARG, NO_STR_ARG, NO_FL_ARG, NO_FL_ARG, NO_FL_ARG);
274                 Send_Notification_Legacy_Wrapper(NOTIF_ANY, world, MSG_INFO, INFO_FREEZETAG_SELF, frag_target.netname, NO_STR_ARG, NO_FL_ARG, NO_FL_ARG, NO_FL_ARG);
275         }
276         else
277         {
278                 if(frag_target.classname == STR_PLAYER)
279                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_FROZEN, frag_attacker.netname, NO_STR_ARG, NO_FL_ARG, NO_FL_ARG, NO_FL_ARG);
280                 if(frag_attacker.classname == STR_PLAYER)
281                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_FREEZETAG_FREEZE, frag_target.netname, NO_STR_ARG, NO_FL_ARG, NO_FL_ARG, NO_FL_ARG);
282                 Send_Notification_Legacy_Wrapper(NOTIF_ANY, world, MSG_INFO, INFO_FREEZETAG_FREEZE, frag_target.netname, frag_attacker.netname, NO_FL_ARG, NO_FL_ARG, NO_FL_ARG);
283         }
284
285         frag_target.health = 1; // "respawn" the player :P
286
287         freezetag_CheckWinner();
288
289         return 1;
290 }
291
292 MUTATOR_HOOKFUNCTION(freezetag_PlayerSpawn)
293 {
294         freezetag_Unfreeze(world); // start by making sure that all ice blocks are removed
295
296         if(total_players == 1 && time > game_starttime) // only one player active on server, start a new match immediately
297         if(!next_round && warmup && (time < warmup - autocvar_g_freezetag_warmup || time > warmup)) // not awaiting next round
298         {
299                 next_round = time;
300                 return 1;
301         }
302         if(warmup && time > warmup) // spawn too late, freeze player
303         {
304                 Send_Notification_Legacy_Wrapper(NOTIF_ONE, self, MSG_CENTER, CENTER_FREEZETAG_SPAWN_LATE, NO_STR_ARG, NO_STR_ARG, NO_FL_ARG, NO_FL_ARG, NO_FL_ARG);
305                 freezetag_Freeze(world);
306         }
307
308         return 1;
309 }
310
311 MUTATOR_HOOKFUNCTION(freezetag_GiveFragsForKill)
312 {
313         frag_score = 0; // no frags counted in Freeze Tag
314         return 1;
315 }
316
317 MUTATOR_HOOKFUNCTION(freezetag_PlayerPreThink)
318 {
319         float n;
320         vector revive_extra_size;
321
322         revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
323
324         entity o;
325         o = world;
326         n = 0;
327         FOR_EACH_PLAYER(other) if(self != other)
328         {
329                 if(other.freezetag_frozen == 0)
330                 {
331                         if(other.team == self.team)
332                         {
333                                 if(boxesoverlap(self.absmin - revive_extra_size, self.absmax + revive_extra_size, other.absmin, other.absmax))
334                                 {
335                                         if(!o)
336                                                 o = other;
337                                         ++n;
338                                 }
339                         }
340                 }
341         }
342
343         if(n && self.freezetag_frozen) // OK, there is at least one teammate reviving us
344         {
345                 self.freezetag_revive_progress = bound(0, self.freezetag_revive_progress + frametime * autocvar_g_freezetag_revive_speed, 1);
346                 self.health = max(1, self.freezetag_revive_progress * autocvar_g_balance_health_start);
347
348                 if(self.freezetag_revive_progress >= 1)
349                 {
350                         freezetag_Unfreeze(self);
351
352                         // EVERY team mate nearby gets a point (even if multiple!)
353                         FOR_EACH_PLAYER(other) if(self != other)
354                         {
355                                 if(other.freezetag_frozen == 0)
356                                 {
357                                         if(other.team == self.team)
358                                         {
359                                                 if(boxesoverlap(self.absmin - revive_extra_size, self.absmax + revive_extra_size, other.absmin, other.absmax))
360                                                 {
361                                                         PlayerScore_Add(other, SP_FREEZETAG_REVIVALS, +1);
362                                                         PlayerScore_Add(other, SP_SCORE, +1);
363                                                 }
364                                         }
365                                 }
366                         }
367
368                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, self, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname, NO_STR_ARG, NO_FL_ARG, NO_FL_ARG, NO_FL_ARG);
369                         Send_Notification_Legacy_Wrapper(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, self.netname, NO_STR_ARG, NO_FL_ARG, NO_FL_ARG, NO_FL_ARG);
370                         Send_Notification_Legacy_Wrapper(NOTIF_ANY, world, MSG_INFO, INFO_FREEZETAG_REVIVE, self.netname, o.netname, NO_FL_ARG, NO_FL_ARG, NO_FL_ARG);
371                 }
372
373                 // now find EVERY teammate within reviving radius, set their revive_progress values correct
374                 FOR_EACH_PLAYER(other) if(self != other)
375                 {
376                         if(other.freezetag_frozen == 0)
377                         {
378                                 if(other.team == self.team)
379                                 {
380                                         if(boxesoverlap(self.absmin - revive_extra_size, self.absmax + revive_extra_size, other.absmin, other.absmax))
381                                                 other.freezetag_revive_progress = self.freezetag_revive_progress;
382                                 }
383                         }
384                 }
385         }
386         else if(!n && self.freezetag_frozen) // only if no teammate is nearby will we reset
387         {
388                 self.freezetag_revive_progress = bound(0, self.freezetag_revive_progress - frametime * autocvar_g_freezetag_revive_clearspeed, 1);
389                 self.health = max(1, self.freezetag_revive_progress * autocvar_g_balance_health_start);
390         }
391         else if(!n)
392         {
393                 self.freezetag_revive_progress = 0; // thawing nobody
394         }
395
396         return 1;
397 }
398
399 MUTATOR_HOOKFUNCTION(freezetag_PlayerPhysics)
400 {
401         if(self.freezetag_frozen)
402         {
403                 self.movement = '0 0 0';
404                 self.disableclientprediction = 1;
405         }
406         return 1;
407 }
408
409 MUTATOR_HOOKFUNCTION(freezetag_PlayerDamage_Calculate)
410 {
411     if(g_freezetag)
412     {
413         if(frag_target.freezetag_frozen == 1 && frag_deathtype != DEATH_HURTTRIGGER)
414         {
415             frag_damage = 0;
416             frag_force = frag_force * autocvar_g_freezetag_frozen_force;
417         }
418     }
419     return 1;
420 }
421
422 MUTATOR_HOOKFUNCTION(freezetag_ForbidThrowCurrentWeapon)
423 {
424         if (self.freezetag_frozen)
425                 return 1;
426         return 0;
427 }
428
429 MUTATOR_HOOKFUNCTION(freezetag_BotRoles)
430 {
431         if not(self.deadflag)
432         {
433                 if (random() < 0.5)
434                         self.havocbot_role = havocbot_role_ft_freeing;
435                 else
436                         self.havocbot_role = havocbot_role_ft_offense;
437         }
438         
439         return TRUE;
440 }
441
442 MUTATOR_DEFINITION(gamemode_freezetag)
443 {
444         MUTATOR_HOOK(MakePlayerObserver, freezetag_RemovePlayer, CBC_ORDER_ANY);
445         MUTATOR_HOOK(ClientDisconnect, freezetag_RemovePlayer, CBC_ORDER_ANY);
446         MUTATOR_HOOK(PlayerDies, freezetag_PlayerDies, CBC_ORDER_ANY);
447         MUTATOR_HOOK(PlayerSpawn, freezetag_PlayerSpawn, CBC_ORDER_ANY);
448         MUTATOR_HOOK(GiveFragsForKill, freezetag_GiveFragsForKill, CBC_ORDER_FIRST);
449         MUTATOR_HOOK(PlayerPreThink, freezetag_PlayerPreThink, CBC_ORDER_FIRST);
450         MUTATOR_HOOK(PlayerPhysics, freezetag_PlayerPhysics, CBC_ORDER_FIRST);
451         MUTATOR_HOOK(PlayerDamage_Calculate, freezetag_PlayerDamage_Calculate, CBC_ORDER_ANY);
452         MUTATOR_HOOK(ForbidThrowCurrentWeapon, freezetag_ForbidThrowCurrentWeapon, CBC_ORDER_FIRST); //first, last or any? dunno.
453         MUTATOR_HOOK(HavocBot_ChooseRule, freezetag_BotRoles, CBC_ORDER_ANY);
454
455         MUTATOR_ONADD
456         {
457                 if(time > 1) // game loads at time 1
458                         error("This is a game type and it cannot be added at runtime.");
459                 freezetag_Initialize();
460         }
461
462         MUTATOR_ONROLLBACK_OR_REMOVE
463         {
464                 // we actually cannot roll back freezetag_Initialize here
465                 // BUT: we don't need to! If this gets called, adding always
466                 // succeeds.
467         }
468
469         MUTATOR_ONREMOVE
470         {
471                 print("This is a game type and it cannot be removed at runtime.");
472                 return -1;
473         }
474
475         return 0;
476 }