]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_freezetag.qc
51d9ca531323da20b8bc00177a347c70fb1f4b58
[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.frozen != 1 && 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(NOTIF_ALL, world, MSG_CENTER, APP_TEAM_NUM_4(winner.team, CENTER_FREEZETAG_ROUND_WIN_));
39                 Send_Notification(NOTIF_ALL, world, MSG_INFO, APP_TEAM_NUM_4(winner.team, INFO_FREEZETAG_ROUND_WIN_));
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.frozen)
58                 return;
59         
60         Freeze(self, 0, 1, TRUE);
61
62         if(attacker == self)
63         {
64                 // you froze your own dumb self
65                 // counted as "suicide" already
66                 PlayerScore_Add(self, SP_SCORE, -1);
67         }
68         else if(attacker.classname == "player")
69         {
70                 // got frozen by an enemy
71                 // counted as "kill" and "death" already
72                 PlayerScore_Add(self, SP_SCORE, -1);
73                 PlayerScore_Add(attacker, SP_SCORE, +1);
74         }
75         else
76         {
77                 // nothing - got frozen by the game type rules themselves
78         }
79 }
80
81
82 // ================
83 // Bot player logic
84 // ================
85
86 void() havocbot_role_ft_freeing;
87 void() havocbot_role_ft_offense;
88
89 void havocbot_goalrating_freeplayers(float ratingscale, vector org, float sradius)
90 {
91         entity head;
92         float distance;
93
94         FOR_EACH_PLAYER(head)
95         {
96                 if ((head != self) && (head.team == self.team))
97                 {
98                         if (head.frozen == 1)
99                         {
100                                 distance = vlen(head.origin - org);
101                                 if (distance > sradius)
102                                         continue;
103                                 navigation_routerating(head, ratingscale, 2000);
104                         }
105                         else
106                         {
107                                 // If teamate is not frozen still seek them out as fight better
108                                 // in a group.
109                                 navigation_routerating(head, ratingscale/3, 2000);
110                         }
111                 }
112         }
113 }
114
115 void havocbot_role_ft_offense()
116 {
117         entity head;
118         float unfrozen;
119
120         if(self.deadflag != DEAD_NO)
121                 return;
122
123         if (!self.havocbot_role_timeout)
124                 self.havocbot_role_timeout = time + random() * 10 + 20;
125
126         // Count how many players on team are unfrozen.
127         unfrozen = 0;
128         FOR_EACH_PLAYER(head)
129         {
130                 if ((head.team == self.team) && (head.frozen != 1))
131                         unfrozen++;
132         }
133
134         // If only one left on team or if role has timed out then start trying to free players.
135         if (((unfrozen == 0) && (self.frozen != 1)) || (time > self.havocbot_role_timeout))
136         {
137                 dprint("changing role to freeing\n");
138                 self.havocbot_role = havocbot_role_ft_freeing;
139                 self.havocbot_role_timeout = 0;
140                 return;
141         }
142
143         if (time > self.bot_strategytime)
144         {
145                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
146
147                 navigation_goalrating_start();
148                 havocbot_goalrating_items(10000, self.origin, 10000);
149                 havocbot_goalrating_enemyplayers(20000, self.origin, 10000);
150                 havocbot_goalrating_freeplayers(9000, self.origin, 10000);
151                 //havocbot_goalrating_waypoints(1, self.origin, 1000);
152                 navigation_goalrating_end();
153         }
154 }
155
156 void havocbot_role_ft_freeing()
157 {
158         if(self.deadflag != DEAD_NO)
159                 return;
160
161         if (!self.havocbot_role_timeout)
162                 self.havocbot_role_timeout = time + random() * 10 + 20;
163
164         if (time > self.havocbot_role_timeout)
165         {
166                 dprint("changing role to offense\n");
167                 self.havocbot_role = havocbot_role_ft_offense;
168                 self.havocbot_role_timeout = 0;
169                 return;
170         }
171
172         if (time > self.bot_strategytime)
173         {
174                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
175
176                 navigation_goalrating_start();
177                 havocbot_goalrating_items(8000, self.origin, 10000);
178                 havocbot_goalrating_enemyplayers(10000, self.origin, 10000);
179                 havocbot_goalrating_freeplayers(20000, self.origin, 10000);
180                 //havocbot_goalrating_waypoints(1, self.origin, 1000);
181                 navigation_goalrating_end();
182         }
183 }
184
185
186 // ==============
187 // Hook Functions
188 // ==============
189
190 MUTATOR_HOOKFUNCTION(freezetag_RemovePlayer)
191 {
192         if(self.frozen != 1 && self.health >= 1)
193         {
194                 if(self.team == NUM_TEAM_1)
195                         --redalive;
196                 else if(self.team == NUM_TEAM_2)
197                         --bluealive;
198                 else if(self.team == NUM_TEAM_3)
199                         --yellowalive;
200                 else if(self.team == NUM_TEAM_4)
201                         --pinkalive;
202                 --totalalive;
203         }
204
205         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)
206                 freezetag_CheckWinner();
207
208         Unfreeze(self);
209
210         return 1;
211 }
212
213 MUTATOR_HOOKFUNCTION(freezetag_PlayerDies)
214 {
215         if(self.frozen != 1)
216         {
217                 if(self.team == NUM_TEAM_1)
218                         --redalive;
219                 else if(self.team == NUM_TEAM_2)
220                         --bluealive;
221                 else if(self.team == NUM_TEAM_3)
222                         --yellowalive;
223                 else if(self.team == NUM_TEAM_4)
224                         --pinkalive;
225                 --totalalive;
226
227                 freezetag_Freeze(frag_attacker);
228         }
229
230         if(frag_attacker == frag_target || frag_attacker == world)
231         {
232                 if(frag_target.classname == STR_PLAYER)
233                         Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_SELF);
234                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_FREEZETAG_SELF, frag_target.netname);
235         }
236         else
237         {
238                 if(frag_target.classname == STR_PLAYER)
239                         Send_Notification(NOTIF_ONE, frag_target, MSG_CENTER, CENTER_FREEZETAG_FROZEN, frag_attacker.netname);
240                 if(frag_attacker.classname == STR_PLAYER)
241                         Send_Notification(NOTIF_ONE, frag_attacker, MSG_CENTER, CENTER_FREEZETAG_FREEZE, frag_target.netname);
242                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_FREEZETAG_FREEZE, frag_target.netname, frag_attacker.netname);
243         }
244
245         frag_target.health = 1; // "respawn" the player :P
246
247         freezetag_CheckWinner();
248
249         return 1;
250 }
251
252 MUTATOR_HOOKFUNCTION(freezetag_PlayerSpawn)
253 {
254         Unfreeze(self); // start by making sure that all ice blocks are removed
255
256         if(total_players == 1 && time > game_starttime) // only one player active on server, start a new match immediately
257         if(!next_round && warmup && (time < warmup - autocvar_g_freezetag_warmup || time > warmup)) // not awaiting next round
258         {
259                 next_round = time;
260                 return 1;
261         }
262         if(warmup && time > warmup) // spawn too late, freeze player
263         {
264                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_FREEZETAG_SPAWN_LATE);
265                 freezetag_Freeze(world);
266         }
267
268         return 1;
269 }
270
271 MUTATOR_HOOKFUNCTION(freezetag_GiveFragsForKill)
272 {
273         frag_score = 0; // no frags counted in Freeze Tag
274         return 1;
275 }
276
277 MUTATOR_HOOKFUNCTION(freezetag_PlayerPreThink)
278 {
279         float n;
280         vector revive_extra_size;
281
282         revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;   
283         
284         entity o;
285         o = world;
286         n = 0;
287         FOR_EACH_PLAYER(other) if(self != other)
288         {
289                 if(other.frozen != 1)
290                 {
291                         if(other.team == self.team)
292                         {
293                                 if(boxesoverlap(self.absmin - revive_extra_size, self.absmax + revive_extra_size, other.absmin, other.absmax))
294                                 {
295                                         if(!o)
296                                                 o = other;
297                                         ++n;
298                                 }
299                         }
300                 }
301         }
302
303         if(n && self.frozen == 1) // OK, there is at least one teammate reviving us
304         {
305                 self.revive_progress = bound(0, self.revive_progress + frametime * autocvar_g_freezetag_revive_speed, 1);
306                 self.health = max(1, self.revive_progress * autocvar_g_balance_health_start);
307
308                 if(self.revive_progress >= 1)
309                 {
310                         Unfreeze(self);
311
312                         // EVERY team mate nearby gets a point (even if multiple!)
313                         FOR_EACH_PLAYER(other) if(self != other)
314                         {
315                                 if(!other.frozen)
316                                 {
317                                         if(other.team == self.team)
318                                         {
319                                                 if(boxesoverlap(self.absmin - revive_extra_size, self.absmax + revive_extra_size, other.absmin, other.absmax))
320                                                 {
321                                                         PlayerScore_Add(other, SP_FREEZETAG_REVIVALS, +1);
322                                                         PlayerScore_Add(other, SP_SCORE, +1);
323                                                 }
324                                         }
325                                 }
326                         }
327
328                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_FREEZETAG_REVIVED, o.netname);
329                         Send_Notification(NOTIF_ONE, o, MSG_CENTER, CENTER_FREEZETAG_REVIVE, self.netname);
330                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_FREEZETAG_REVIVE, self.netname, o.netname);
331                 }
332
333                 // now find EVERY teammate within reviving radius, set their revive_progress values correct
334                 FOR_EACH_PLAYER(other) if(self != other)
335                 {
336                         if(!other.frozen)
337                         {
338                                 if(other.team == self.team)
339                                 {
340                                         if(boxesoverlap(self.absmin - revive_extra_size, self.absmax + revive_extra_size, other.absmin, other.absmax))
341                                                 other.revive_progress = self.revive_progress;
342                                 }
343                         }
344                 }
345         }
346         else if(!n && self.frozen == 1) // only if no teammate is nearby will we reset
347         {
348                 self.revive_progress = bound(0, self.revive_progress - frametime * autocvar_g_freezetag_revive_clearspeed, 1);
349                 self.health = max(1, self.revive_progress * autocvar_g_balance_health_start);
350         }
351         else if(!n && !self.frozen)
352         {
353                 self.revive_progress = 0; // thawing nobody
354         }
355
356         return 1;
357 }
358
359 MUTATOR_HOOKFUNCTION(freezetag_PlayerDamage_Calculate)
360 {
361     if(g_freezetag)
362     {
363         if(frag_target.frozen == 1 && frag_deathtype != DEATH_HURTTRIGGER)
364         {
365             frag_damage = 0;
366             frag_force = frag_force * autocvar_g_freezetag_frozen_force;
367         }
368     }
369     return 1;
370 }
371
372 MUTATOR_HOOKFUNCTION(freezetag_BotRoles)
373 {
374         if not(self.deadflag)
375         {
376                 if (random() < 0.5)
377                         self.havocbot_role = havocbot_role_ft_freeing;
378                 else
379                         self.havocbot_role = havocbot_role_ft_offense;
380         }
381         
382         return TRUE;
383 }
384
385 MUTATOR_DEFINITION(gamemode_freezetag)
386 {
387         MUTATOR_HOOK(MakePlayerObserver, freezetag_RemovePlayer, CBC_ORDER_ANY);
388         MUTATOR_HOOK(ClientDisconnect, freezetag_RemovePlayer, CBC_ORDER_ANY);
389         MUTATOR_HOOK(PlayerDies, freezetag_PlayerDies, CBC_ORDER_ANY);
390         MUTATOR_HOOK(PlayerSpawn, freezetag_PlayerSpawn, CBC_ORDER_ANY);
391         MUTATOR_HOOK(GiveFragsForKill, freezetag_GiveFragsForKill, CBC_ORDER_FIRST);
392         MUTATOR_HOOK(PlayerPreThink, freezetag_PlayerPreThink, CBC_ORDER_FIRST);
393         MUTATOR_HOOK(PlayerDamage_Calculate, freezetag_PlayerDamage_Calculate, CBC_ORDER_ANY);
394         MUTATOR_HOOK(HavocBot_ChooseRule, freezetag_BotRoles, CBC_ORDER_ANY);
395
396         MUTATOR_ONADD
397         {
398                 if(time > 1) // game loads at time 1
399                         error("This is a game type and it cannot be added at runtime.");
400                 freezetag_Initialize();
401         }
402
403         MUTATOR_ONROLLBACK_OR_REMOVE
404         {
405                 // we actually cannot roll back freezetag_Initialize here
406                 // BUT: we don't need to! If this gets called, adding always
407                 // succeeds.
408         }
409
410         MUTATOR_ONREMOVE
411         {
412                 print("This is a game type and it cannot be removed at runtime.");
413                 return -1;
414         }
415
416         return 0;
417 }