Merge remote-tracking branch 'origin/divVerent/allow-override-item-model'
[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) // counted in arena.qc
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         string teamname;
26         winner = world;
27
28         FOR_EACH_PLAYER(e)
29         {
30                 if(e.freezetag_frozen == 0 && e.classname == "player" && e.health >= 1) // here's one player from the winning team... good
31                 {
32                         winner = e;
33                         break; // break, we found the winner
34                 }
35         }
36
37         if(winner != world) // just in case a winner wasn't found
38         {
39                 if(winner.team == COLOR_TEAM1)
40                         teamname = "^1Red Team";
41                 else if(winner.team == COLOR_TEAM2)
42                         teamname = "^4Blue Team";
43                 else if(winner.team == COLOR_TEAM3)
44                         teamname = "^3Yellow Team";
45                 else
46                         teamname = "^6Pink Team";
47                 FOR_EACH_PLAYER(e) {
48                         centerprint(e, strcat(teamname, "^5 wins the round, all other teams were frozen.\n"));
49                 }
50                 bprint(teamname, "^5 wins the round since all the other teams were frozen.\n");
51                 TeamScore_AddToTeam(winner.team, ST_SCORE, +1);
52         }
53
54         next_round = time + 5;
55 }
56
57 // this is needed to allow the player to turn his view around (fixangle can't
58 // be used to freeze his view, as that also changes the angles), while not
59 // turning that ice object with the player
60 void freezetag_Ice_Think()
61 {
62         setorigin(self, self.owner.origin - '0 0 16');
63         self.nextthink = time;
64 }
65
66 void freezetag_Freeze(entity attacker)
67 {
68         if(self.freezetag_frozen)
69                 return;
70         self.freezetag_frozen = 1;
71         self.freezetag_revive_progress = 0;
72         self.health = 1;
73
74         entity ice;
75         ice = spawn();
76         ice.owner = self;
77         ice.classname = "freezetag_ice";
78         ice.think = freezetag_Ice_Think;
79         ice.nextthink = time;
80         ice.frame = floor(random() * 21); // ice model has 20 different looking frames
81         setmodel(ice, "models/ice/ice.md3");
82
83         entity oldself;
84         oldself = self;
85         self = ice;
86         freezetag_Ice_Think();
87         self = oldself;
88
89         RemoveGrapplingHook(self);
90
91         // add waypoint
92         WaypointSprite_Spawn("freezetag_frozen", 0, 0, self, '0 0 64', world, self.team, self, waypointsprite_attached, TRUE, RADARICON_WAYPOINT, '0.25 0.90 1');
93
94         if(attacker == self)
95         {
96                 // you froze your own dumb self
97                 // counted as "suicide" already
98                 PlayerScore_Add(self, SP_SCORE, -1);
99         }
100         else if(attacker.classname == "player")
101         {
102                 // got frozen by an enemy
103                 // counted as "kill" and "death" already
104                 PlayerScore_Add(self, SP_SCORE, -1);
105                 PlayerScore_Add(attacker, SP_SCORE, +1);
106         }
107         else
108         {
109                 // nothing - got frozen by the game type rules themselves
110         }
111 }
112
113 void freezetag_Unfreeze(entity attacker)
114 {
115         self.freezetag_frozen = 0;
116         self.freezetag_revive_progress = 0;
117         self.health = autocvar_g_balance_health_start;
118
119         // remove the ice block
120         entity ice;
121         for(ice = world; (ice = find(ice, classname, "freezetag_ice")); ) if(ice.owner == self)
122         {
123                 remove(ice);
124                 break;
125         }
126
127         // remove waypoint
128         if(self.waypointsprite_attached)
129                 WaypointSprite_Kill(self.waypointsprite_attached);
130 }
131
132 MUTATOR_HOOKFUNCTION(freezetag_RemovePlayer)
133 {
134         if(self.freezetag_frozen == 0)
135         {
136                 if(self.team == COLOR_TEAM1)
137                         --redalive;
138                 else if(self.team == COLOR_TEAM2)
139                         --bluealive;
140                 else if(self.team == COLOR_TEAM3)
141                         --yellowalive;
142                 else if(self.team == COLOR_TEAM4)
143                         --pinkalive;
144                 --totalalive;
145         }
146
147         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)
148                 freezetag_CheckWinner();
149
150         freezetag_Unfreeze(world);
151
152         return 1;
153 }
154
155 MUTATOR_HOOKFUNCTION(freezetag_PlayerDies)
156 {
157         if(self.freezetag_frozen == 0)
158         {
159                 if(self.team == COLOR_TEAM1)
160                         --redalive;
161                 else if(self.team == COLOR_TEAM2)
162                         --bluealive;
163                 else if(self.team == COLOR_TEAM3)
164                         --yellowalive;
165                 else if(self.team == COLOR_TEAM4)
166                         --pinkalive;
167                 --totalalive;
168
169         freezetag_Freeze(frag_attacker);
170         }
171
172     if(frag_attacker.classname == STR_PLAYER)
173         centerprint(frag_attacker, strcat("^2You froze ^7", frag_target.netname, ".\n"));
174
175         if(frag_attacker == frag_target || frag_attacker == world)
176         {
177         if(frag_target.classname == STR_PLAYER)
178             centerprint(frag_target, "^1You froze yourself.\n");
179                 bprint("^7", frag_target.netname, "^1 froze himself.\n");
180         }
181         else
182         {
183         if(frag_target.classname == STR_PLAYER)
184             centerprint(frag_target, strcat("^1You were frozen by ^7", frag_attacker.netname, ".\n"));
185                 bprint("^7", frag_target.netname, "^1 was frozen by ^7", frag_attacker.netname, ".\n");
186         }
187
188         frag_target.health = 1; // "respawn" the player :P
189
190         freezetag_CheckWinner();
191
192         return 1;
193 }
194
195 MUTATOR_HOOKFUNCTION(freezetag_PlayerSpawn)
196 {
197     freezetag_Unfreeze(world); // start by making sure that all ice blocks are removed
198
199         if(total_players == 1 && time > game_starttime) // only one player active on server, start a new match immediately
200         if(!next_round && warmup && (time < warmup - autocvar_g_freezetag_warmup || time > warmup)) // not awaiting next round
201         {
202                 next_round = time;
203                 return 1;
204         }
205         if(warmup && time > warmup) // spawn too late, freeze player
206         {
207                 centerprint(self, "^1You spawned after the round started, you'll spawn as frozen.\n");
208                 freezetag_Freeze(world);
209         }
210
211         return 1;
212 }
213
214 MUTATOR_HOOKFUNCTION(freezetag_GiveFragsForKill)
215 {
216         frag_score = 0; // no frags counted in Freeze Tag
217         return 1;
218 }
219
220 MUTATOR_HOOKFUNCTION(freezetag_PlayerPreThink)
221 {
222         float n;
223         vector revive_extra_size;
224
225         revive_extra_size = '1 1 1' * autocvar_g_freezetag_revive_extra_size;
226
227         entity o;
228         o = world;
229         n = 0;
230         FOR_EACH_PLAYER(other) if(self != other)
231         {
232                 if(other.freezetag_frozen == 0)
233                 {
234                         if(other.team == self.team)
235                         {
236                                 if(boxesoverlap(self.absmin - revive_extra_size, self.absmax + revive_extra_size, other.absmin, other.absmax))
237                                 {
238                                         if(!o)
239                                                 o = other;
240                                         ++n;
241                                 }
242                         }
243                 }
244         }
245
246         if(n && self.freezetag_frozen) // OK, there is at least one teammate reviving us
247         {
248                 self.freezetag_revive_progress = bound(0, self.freezetag_revive_progress + frametime * autocvar_g_freezetag_revive_speed, 1);
249                 self.health = max(1, self.freezetag_revive_progress * autocvar_g_balance_health_start);
250
251                 if(self.freezetag_revive_progress >= 1)
252                 {
253                         freezetag_Unfreeze(self);
254
255                         // EVERY team mate nearby gets a point (even if multiple!)
256                         FOR_EACH_PLAYER(other) if(self != other)
257                         {
258                                 if(other.freezetag_frozen == 0)
259                                 {
260                                         if(other.team == self.team)
261                                         {
262                                                 if(boxesoverlap(self.absmin - revive_extra_size, self.absmax + revive_extra_size, other.absmin, other.absmax))
263                                                 {
264                                                         PlayerScore_Add(other, SP_FREEZETAG_REVIVALS, +1);
265                                                         PlayerScore_Add(other, SP_SCORE, +1);
266                                                 }
267                                         }
268                                 }
269                         }
270
271                         if(n > 1)
272                                 centerprint(self, strcat("^5You were revived by ^7", o.netname, "^5 et al.\n"));
273                         else
274                                 centerprint(self, strcat("^5You were revived by ^7", o.netname, "^5.\n"));
275                         centerprint(o, strcat("^5You revived ^7", self.netname, "^5.\n"));
276                         if(n > 1)
277                                 bprint("^7", o.netname, "^5 et al revived ^7", self.netname, "^5.\n");
278                         else
279                                 bprint("^7", o.netname, "^5 revived ^7", self.netname, "^5.\n");
280                 }
281
282                 // now find EVERY teammate within reviving radius, set their revive_progress values correct
283                 FOR_EACH_PLAYER(other) if(self != other)
284                 {
285                         if(other.freezetag_frozen == 0)
286                         {
287                                 if(other.team == self.team)
288                                 {
289                                         if(boxesoverlap(self.absmin - revive_extra_size, self.absmax + revive_extra_size, other.absmin, other.absmax))
290                                                 other.freezetag_revive_progress = self.freezetag_revive_progress;
291                                 }
292                         }
293                 }
294         }
295         else if(!n && self.freezetag_frozen) // only if no teammate is nearby will we reset
296         {
297                 self.freezetag_revive_progress = bound(0, self.freezetag_revive_progress - frametime * autocvar_g_freezetag_revive_clearspeed, 1);
298                 self.health = max(1, self.freezetag_revive_progress * autocvar_g_balance_health_start);
299         }
300         else if(!n)
301         {
302                 self.freezetag_revive_progress = 0; // thawing nobody
303         }
304
305         return 1;
306 }
307
308 MUTATOR_HOOKFUNCTION(freezetag_PlayerPhysics)
309 {
310         if(self.freezetag_frozen)
311         {
312                 self.movement = '0 0 0';
313                 self.disableclientprediction = 1;
314         }
315         return 1;
316 }
317
318 MUTATOR_HOOKFUNCTION(freezetag_PlayerDamage_Calculate)
319 {
320     if(g_freezetag)
321     {
322         if(frag_target.freezetag_frozen == 1 && frag_deathtype != DEATH_HURTTRIGGER)
323         {
324             frag_damage = 0;
325             frag_force = frag_force * autocvar_g_freezetag_frozen_force;
326         }
327     }
328     return 1;
329 }
330
331 MUTATOR_HOOKFUNCTION(freezetag_ForbidThrowCurrentWeapon)
332 {
333         if (self.freezetag_frozen)
334                 return 1;
335         return 0;
336 }
337
338 MUTATOR_DEFINITION(gamemode_freezetag)
339 {
340         MUTATOR_HOOK(MakePlayerObserver, freezetag_RemovePlayer, CBC_ORDER_ANY);
341         MUTATOR_HOOK(ClientDisconnect, freezetag_RemovePlayer, CBC_ORDER_ANY);
342         MUTATOR_HOOK(PlayerDies, freezetag_PlayerDies, CBC_ORDER_ANY);
343         MUTATOR_HOOK(PlayerSpawn, freezetag_PlayerSpawn, CBC_ORDER_ANY);
344         MUTATOR_HOOK(GiveFragsForKill, freezetag_GiveFragsForKill, CBC_ORDER_FIRST);
345         MUTATOR_HOOK(PlayerPreThink, freezetag_PlayerPreThink, CBC_ORDER_FIRST);
346         MUTATOR_HOOK(PlayerPhysics, freezetag_PlayerPhysics, CBC_ORDER_FIRST);
347         MUTATOR_HOOK(PlayerDamage_Calculate, freezetag_PlayerDamage_Calculate, CBC_ORDER_ANY);
348         MUTATOR_HOOK(ForbidThrowCurrentWeapon, freezetag_ForbidThrowCurrentWeapon, CBC_ORDER_FIRST); //first, last or any? dunno.
349
350         MUTATOR_ONADD
351         {
352                 if(time > 1) // game loads at time 1
353                         error("This is a game type and it cannot be added at runtime.");
354                 g_freezetag = 1;
355                 freezetag_Initialize();
356         }
357
358         MUTATOR_ONREMOVE
359         {
360                 g_freezetag = 0;
361                 error("This is a game type and it cannot be removed at runtime.");
362         }
363
364         return 0;
365 }