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