Merge branch 'master' into terencehill/music_player
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_buffs.qc
1 float buffs_BuffModel_Customize()
2 {
3         entity player, myowner;
4         float same_team;
5
6         player = WaypointSprite_getviewentity(other);
7         myowner = self.owner;
8         same_team = (SAME_TEAM(player, myowner) || SAME_TEAM(player, myowner));
9
10         if(myowner.alpha <= 0.5 && !same_team && myowner.alpha != 0)
11                 return FALSE;
12
13         if(player == myowner || (IS_SPEC(other) && other.enemy == myowner))
14         {
15                 // somewhat hide the model, but keep the glow
16                 self.effects = 0;
17                 self.alpha = -1;
18         }
19         else
20         {
21                 self.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
22                 self.alpha = 1;
23         }
24         return TRUE;
25 }
26
27 // buff item
28 float buff_Waypoint_visible_for_player(entity plr)
29 {
30     if(!self.owner.buff_active && !self.owner.buff_activetime)
31         return FALSE;
32
33         if(plr.buffs)
34         {
35                 if(plr.cvar_cl_buffs_autoreplace)
36                 {
37                         if(plr.buffs == self.owner.buffs)
38                                 return FALSE;
39                 }
40                 else
41                         return FALSE;
42         }
43
44     return WaypointSprite_visible_for_player(plr);
45 }
46
47 void buff_Waypoint_Spawn(entity e)
48 {
49     WaypointSprite_Spawn(Buff_Sprite(e.buffs), 0, autocvar_g_buffs_waypoint_distance, e, '0 0 1' * e.maxs_z, world, e.team, e, buff_waypoint, TRUE, RADARICON_POWERUP, e.glowmod);
50     WaypointSprite_UpdateTeamRadar(e.buff_waypoint, RADARICON_POWERUP, e.glowmod);
51     e.buff_waypoint.waypointsprite_visible_for_player = buff_Waypoint_visible_for_player;
52 }
53
54 void buff_SetCooldown(float cd)
55 {
56         cd = max(0, cd);
57
58         if(!self.buff_waypoint)
59                 buff_Waypoint_Spawn(self);
60
61         WaypointSprite_UpdateBuildFinished(self.buff_waypoint, time + cd);
62         self.buff_activetime = cd;
63         self.buff_active = !cd;
64 }
65
66 void buff_Respawn(entity ent)
67 {
68         if(gameover) { return; }
69         
70         vector oldbufforigin = ent.origin;
71         
72         if(!MoveToRandomMapLocation(ent, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, ((autocvar_g_buffs_random_location_attempts > 0) ? autocvar_g_buffs_random_location_attempts : 10), 1024, 256))
73         {
74                 entity spot = SelectSpawnPoint(TRUE);
75                 setorigin(ent, ((spot.origin + '0 0 200') + (randomvec() * 300)));
76                 ent.angles = spot.angles;
77         }
78         
79         tracebox(ent.origin, ent.mins * 1.5, self.maxs * 1.5, ent.origin, MOVE_NOMONSTERS, ent);
80         
81         setorigin(ent, trace_endpos); // attempt to unstick
82         
83         ent.movetype = MOVETYPE_TOSS;
84         
85         makevectors(ent.angles);
86         ent.velocity = '0 0 200';
87         ent.angles = '0 0 0';
88         if(autocvar_g_buffs_random_lifetime > 0)
89                 ent.lifetime = time + autocvar_g_buffs_random_lifetime;
90
91         pointparticles(particleeffectnum("electro_combo"), oldbufforigin + ((ent.mins + ent.maxs) * 0.5), '0 0 0', 1);
92         pointparticles(particleeffectnum("electro_combo"), CENTER_OR_VIEWOFS(ent), '0 0 0', 1);
93         
94         WaypointSprite_Ping(ent.buff_waypoint);
95         
96         sound(ent, CH_TRIGGER, "keepaway/respawn.wav", VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
97 }
98
99 void buff_Touch()
100 {
101         if(gameover) { return; }
102
103         if(ITEM_TOUCH_NEEDKILL())
104         {
105                 buff_Respawn(self);
106                 return;
107         }
108
109         if((self.team && DIFF_TEAM(other, self))
110         || (other.frozen)
111         || (other.vehicle)
112         || (!IS_PLAYER(other))
113         || (!self.buff_active)
114         )
115         {
116                 // can't touch this
117                 return;
118         }
119
120         if(other.buffs)
121         {
122                 if(other.cvar_cl_buffs_autoreplace && other.buffs != self.buffs)
123                 {
124                         //Send_Notification(NOTIF_ONE, other, MSG_MULTI, ITEM_BUFF_DROP, other.buffs);
125                         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_ITEM_BUFF_LOST, other.netname, other.buffs);
126
127                         other.buffs = 0;
128                         //sound(other, CH_TRIGGER, "relics/relic_effect.wav", VOL_BASE, ATTN_NORM);
129                 }
130                 else { return; } // do nothing
131         }
132                 
133         self.owner = other;
134         self.buff_active = FALSE;
135         self.lifetime = 0;
136         
137         Send_Notification(NOTIF_ONE, other, MSG_MULTI, ITEM_BUFF_GOT, self.buffs);
138         Send_Notification(NOTIF_ALL_EXCEPT, other, MSG_INFO, INFO_ITEM_BUFF, other.netname, self.buffs);
139
140         pointparticles(particleeffectnum("item_pickup"), CENTER_OR_VIEWOFS(self), '0 0 0', 1);
141         sound(other, CH_TRIGGER, "misc/shield_respawn.wav", VOL_BASE, ATTN_NORM);
142         other.buffs |= (self.buffs);
143 }
144
145 float buff_Available(float buffid)
146 {
147         if(buffid == BUFF_AMMO && ((start_items & IT_UNLIMITED_WEAPON_AMMO) || (start_items & IT_UNLIMITED_AMMO) || (cvar("g_melee_only"))))
148                 return FALSE;
149
150         if(buffid == BUFF_VAMPIRE && cvar("g_vampire"))
151                 return FALSE;
152
153         if(!cvar(strcat("g_buffs_", Buff_Name(buffid))))
154                 return FALSE;
155
156         return TRUE;
157 }
158
159 void buff_NewType(entity ent, float cb)
160 {
161         entity e;
162         RandomSelection_Init();
163         for(e = Buff_Type_first; e; e = e.enemy)
164         if(buff_Available(e.items))
165         {
166                 RandomSelection_Add(world, e.items, string_null, 1, 1 / e.count); // if it's already been chosen, give it a lower priority
167                 e.count += 1;
168         }
169         ent.buffs = RandomSelection_chosen_float;
170 }
171
172 void buff_Think()
173 {
174         if(self.buffs != self.oldbuffs)
175         {
176                 self.color = Buff_Color(self.buffs);
177                 self.glowmod = ((self.team) ? Team_ColorRGB(self.team) + '0.1 0.1 0.1' : self.color);
178                 self.skin = Buff_Skin(self.buffs);
179                 
180                 setmodel(self, "models/relics/relic.md3");
181
182                 if(self.buff_waypoint)
183                 {
184                         //WaypointSprite_Disown(self.buff_waypoint, 1);
185                         WaypointSprite_Kill(self.buff_waypoint);
186                         buff_Waypoint_Spawn(self);
187                         if(self.buff_activetime)
188                                 WaypointSprite_UpdateBuildFinished(self.buff_waypoint, time + self.buff_activetime - frametime);
189                 }
190
191                 self.oldbuffs = self.buffs;
192         }
193
194         if(!gameover)
195         if((round_handler_IsActive() && !round_handler_IsRoundStarted()) || time >= game_starttime)
196         if(!self.buff_activetime_updated)
197         {
198                 buff_SetCooldown(self.buff_activetime);
199                 self.buff_activetime_updated = TRUE;
200     }
201
202         if(!self.buff_active && !self.buff_activetime)
203         if(!self.owner || self.owner.frozen || self.owner.deadflag != DEAD_NO || !self.owner.iscreature || !(self.owner.buffs & self.buffs))
204         {
205                 buff_SetCooldown(autocvar_g_buffs_cooldown_respawn + frametime);
206                 self.owner = world;
207                 if(autocvar_g_buffs_randomize)
208                         buff_NewType(self, self.buffs);
209                         
210                 if(autocvar_g_buffs_random_location || (self.spawnflags & 1))
211                         buff_Respawn(self);
212         }
213         
214         if(self.buff_activetime)
215         if(!gameover)
216         if((round_handler_IsActive() && !round_handler_IsRoundStarted()) || time >= game_starttime)
217         {
218                 self.buff_activetime = max(0, self.buff_activetime - frametime);
219
220                 if(!self.buff_activetime)
221                 {
222                         self.buff_active = TRUE;
223                         sound(self, CH_TRIGGER, "misc/strength_respawn.wav", VOL_BASE, ATTN_NORM);
224                         pointparticles(particleeffectnum("item_respawn"), CENTER_OR_VIEWOFS(self), '0 0 0', 1);
225                 }
226         }
227
228         if(!self.buff_active)
229         {
230                 self.alpha = 0.3;
231                 self.effects &= ~(EF_FULLBRIGHT);
232                 self.pflags = 0;
233         }
234         else
235         {
236                 self.alpha = 1;
237                 self.effects |= EF_FULLBRIGHT;
238                 self.light_lev = 220 + 36 * sin(time);
239                 self.pflags = PFLAGS_FULLDYNAMIC;
240
241                 if(self.team && !self.buff_waypoint)
242                         buff_Waypoint_Spawn(self);
243                         
244                 if(self.lifetime)
245                 if(time >= self.lifetime)
246                         buff_Respawn(self);
247         }
248     
249         self.nextthink = time;
250         //self.angles_y = time * 110.1;
251 }
252
253 void buff_Waypoint_Reset()
254 {
255         WaypointSprite_Kill(self.buff_waypoint);
256
257         if(self.buff_activetime) { buff_Waypoint_Spawn(self); }
258 }
259
260 void buff_Reset()
261 {
262         if(autocvar_g_buffs_randomize)
263                 buff_NewType(self, self.buffs);
264         self.owner = world;
265         buff_SetCooldown(autocvar_g_buffs_cooldown_activate);
266         buff_Waypoint_Reset();
267         self.buff_activetime_updated = FALSE;
268         
269         if(autocvar_g_buffs_random_location || (self.spawnflags & 1))
270                 buff_Respawn(self);
271 }
272
273 void buff_Init(entity ent)
274 {
275         if(!cvar("g_buffs")) { remove(self); return; }
276         
277         if(!teamplay && self.team) { self.team = 0; }
278
279         entity oldself = self;
280         self = ent;
281         if(!self.buffs || buff_Available(self.buffs))
282                 buff_NewType(self, 0);
283         
284         self.classname = "item_buff";
285         self.solid = SOLID_TRIGGER;
286         self.flags = FL_ITEM;
287         self.think = buff_Think;
288         self.touch = buff_Touch;
289         self.reset = buff_Reset;
290         self.nextthink = time + 0.1;
291         self.gravity = 1;
292         self.movetype = MOVETYPE_TOSS;
293         self.scale = 1;
294         self.skin = Buff_Skin(self.buffs);
295         self.effects = EF_FULLBRIGHT | EF_STARDUST | EF_NOSHADOW;
296         self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
297         //self.gravity = 100;
298         self.color = Buff_Color(self.buffs);
299         self.glowmod = ((self.team) ? Team_ColorRGB(self.team) + '0.1 0.1 0.1' : self.color);
300         buff_SetCooldown(autocvar_g_buffs_cooldown_activate + game_starttime);
301         self.buff_active = !self.buff_activetime;
302         self.pflags = PFLAGS_FULLDYNAMIC;
303         
304         if(self.noalign)
305                 self.movetype = MOVETYPE_NONE; // reset by random location
306
307         setmodel(self, "models/relics/relic.md3");
308         setsize(self, BUFF_MIN, BUFF_MAX);
309         
310         if(cvar("g_buffs_random_location") || (self.spawnflags & 1))
311                 buff_Respawn(self);
312         
313         self = oldself;
314 }
315
316 void buff_Init_Compat(entity ent, float replacement)
317 {
318         if(ent.spawnflags & 2)
319                 ent.team = NUM_TEAM_1;
320         else if(ent.spawnflags & 4)
321                 ent.team = NUM_TEAM_2;
322
323         ent.buffs = replacement;
324
325         buff_Init(ent);
326 }
327
328 void buff_SpawnReplacement(entity ent, entity old)
329 {
330         setorigin(ent, old.origin);
331         ent.angles = old.angles;
332         ent.noalign = old.noalign;
333         
334         buff_Init(ent);
335 }
336
337 // mutator hooks
338 MUTATOR_HOOKFUNCTION(buffs_PlayerDamage_SplitHealthArmor)
339 {
340         if(frag_deathtype == DEATH_BUFF_VENGEANCE) { return FALSE; } // oh no you don't
341
342         if(frag_target.buffs & BUFF_RESISTANCE)
343         {
344                 vector v = healtharmor_applydamage(50, autocvar_g_buffs_resistance_blockpercent, frag_deathtype, frag_damage);
345                 damage_take = v_x;
346                 damage_save = v_y;
347         }
348
349         return FALSE;
350 }
351
352 void buff_Vengeance_DelayedDamage()
353 {
354         if(self.enemy)
355                 Damage(self.enemy, self.owner, self.owner, self.dmg, DEATH_BUFF_VENGEANCE, self.enemy.origin, '0 0 0');
356         
357         remove(self);
358         return;
359 }
360
361 MUTATOR_HOOKFUNCTION(buffs_PlayerDamage_Calculate)
362 {
363         if(frag_deathtype == DEATH_BUFF_VENGEANCE) { return FALSE; } // oh no you don't
364
365         if(frag_target.buffs & BUFF_SPEED)
366         if(frag_target != frag_attacker)
367                 frag_damage *= autocvar_g_buffs_speed_damage_take;
368
369         if(frag_target.buffs & BUFF_MEDIC)
370         if((frag_target.health - frag_damage) <= 0)
371         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
372         if(frag_attacker)
373         if(random() <= autocvar_g_buffs_medic_survive_chance)
374         if(frag_target.health - autocvar_g_buffs_medic_survive_health > 0) // not if the final result would be less than 0, medic must get health
375                 frag_damage = frag_target.health - autocvar_g_buffs_medic_survive_health;
376                 
377         if(frag_target.buffs & BUFF_VENGEANCE)
378         if(frag_attacker)
379         if(frag_attacker != frag_target)
380         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
381         {
382                 entity dmgent = spawn();
383
384                 dmgent.dmg = frag_damage * autocvar_g_buffs_vengeance_damage_multiplier;
385                 dmgent.enemy = frag_attacker;
386                 dmgent.owner = frag_target;
387                 dmgent.think = buff_Vengeance_DelayedDamage;
388                 dmgent.nextthink = time + 0.1;
389         }
390
391         if(frag_target.buffs & BUFF_BASH)
392         if(frag_attacker != frag_target)
393         if(vlen(frag_force))
394                 frag_force = '0 0 0';
395         
396         if(frag_attacker.buffs & BUFF_BASH)
397         if(vlen(frag_force))
398         if(frag_attacker == frag_target)
399                 frag_force *= autocvar_g_buffs_bash_force_self;
400         else
401                 frag_force *= autocvar_g_buffs_bash_force;
402         
403         if(frag_attacker.buffs & BUFF_DISABILITY)
404         if(frag_target != frag_attacker)
405                 frag_target.buff_disability_time = time + autocvar_g_buffs_disability_time;
406
407         if(frag_attacker.buffs & BUFF_MEDIC)
408         if(SAME_TEAM(frag_attacker, frag_target))
409         if(frag_attacker != frag_target)
410         {
411                 frag_target.health = min(g_pickup_healthmega_max, frag_target.health + frag_damage);
412                 frag_damage = 0;
413         }
414
415         // this... is ridiculous (TODO: fix!)
416         if(frag_attacker.buffs & BUFF_VAMPIRE)
417         if(!frag_target.vehicle)
418         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
419         if(frag_target.deadflag == DEAD_NO)
420         if(IS_PLAYER(frag_target) || (frag_target.flags & FL_MONSTER))
421         if(frag_attacker != frag_target)
422         if(!frag_target.frozen)
423         if(frag_target.takedamage)
424         if(DIFF_TEAM(frag_attacker, frag_target))
425                 frag_attacker.health = bound(0, frag_attacker.health + bound(0, frag_damage * autocvar_g_buffs_vampire_damage_steal, frag_target.health), g_pickup_healthsmall_max);
426
427         return FALSE;
428 }
429
430 MUTATOR_HOOKFUNCTION(buffs_PlayerSpawn)
431 {
432         self.buffs = 0;
433         // reset timers here to prevent them continuing after re-spawn
434         self.buff_disability_time = 0;
435         self.buff_disability_effect_time = 0;
436         return FALSE;
437 }
438
439 MUTATOR_HOOKFUNCTION(buffs_PlayerPhysics)
440 {
441         if(self.buffs & BUFF_SPEED)
442         {
443                 self.stat_sv_maxspeed *= autocvar_g_buffs_speed_speed;
444                 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_speed_speed;
445         }
446         
447         if(time < self.buff_disability_time)
448         {
449                 self.stat_sv_maxspeed *= autocvar_g_buffs_disability_speed;
450                 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_disability_speed;
451         }
452
453         return FALSE;
454 }
455
456 MUTATOR_HOOKFUNCTION(buffs_PlayerJump)
457 {
458         if(self.buffs & BUFF_JUMP)
459                 player_jumpheight = autocvar_g_buffs_jump_height;
460         self.stat_jumpheight = player_jumpheight;
461
462         return FALSE;
463 }
464
465 MUTATOR_HOOKFUNCTION(buffs_MonsterMove)
466 {
467         if(time < self.buff_disability_time)
468         {
469                 monster_speed_walk *= autocvar_g_buffs_disability_speed;
470                 monster_speed_run *= autocvar_g_buffs_disability_speed;
471         }
472
473         return FALSE;
474 }
475
476 MUTATOR_HOOKFUNCTION(buffs_PlayerDies)
477 {
478         if(self.buffs)
479         {
480                 Send_Notification(NOTIF_ALL_EXCEPT, self, MSG_INFO, INFO_ITEM_BUFF_LOST, self.netname, self.buffs);
481                 self.buffs = 0;
482                 
483                 if(self.buff_model)
484                 {
485                         remove(self.buff_model);
486                         self.buff_model = world;
487                 }
488         }
489         return FALSE;
490 }
491
492 MUTATOR_HOOKFUNCTION(buffs_PlayerUseKey)
493 {
494         if(MUTATOR_RETURNVALUE || gameover) { return FALSE; }
495         if(self.buffs)
496         {
497                 Send_Notification(NOTIF_ONE, self, MSG_MULTI, ITEM_BUFF_DROP, self.buffs);
498                 Send_Notification(NOTIF_ALL_EXCEPT, self, MSG_INFO, INFO_ITEM_BUFF_LOST, self.netname, self.buffs);
499
500                 self.buffs = 0;
501                 sound(self, CH_TRIGGER, "relics/relic_effect.wav", VOL_BASE, ATTN_NORM);
502                 return TRUE;
503         }
504         return FALSE;
505 }
506
507 MUTATOR_HOOKFUNCTION(buffs_RemovePlayer)
508 {
509         if(self.buff_model)
510         {
511                 remove(self.buff_model);
512                 self.buff_model = world;
513         }
514         
515         // also reset timers here to prevent them continuing after spectating
516         self.buff_disability_time = 0;
517         self.buff_disability_effect_time = 0;
518
519         return FALSE;
520 }
521
522 MUTATOR_HOOKFUNCTION(buffs_CustomizeWaypoint)
523 {
524         entity e = WaypointSprite_getviewentity(other);
525
526         // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
527         // but only apply this to real players, not to spectators
528         if((self.owner.flags & FL_CLIENT) && (self.owner.buffs & BUFF_INVISIBLE) && (e == other))
529         if(DIFF_TEAM(self.owner, e))
530                 return TRUE;
531
532         return FALSE;
533 }
534
535 MUTATOR_HOOKFUNCTION(buffs_OnEntityPreSpawn)
536 {
537         if(autocvar_g_buffs_replace_powerups)
538         switch(self.classname)
539         {
540                 case "item_strength":
541                 case "item_invincible":
542                 {
543                         entity e = spawn();
544                         buff_SpawnReplacement(e, self);
545                         return TRUE;
546                 }
547         }
548         return FALSE;
549 }
550
551 MUTATOR_HOOKFUNCTION(buffs_WeaponRate)
552 {
553         if(self.buffs & BUFF_SPEED)
554                 weapon_rate *= autocvar_g_buffs_speed_rate;
555                 
556         if(time < self.buff_disability_time)
557                 weapon_rate *= autocvar_g_buffs_disability_rate;
558         
559         return FALSE;
560 }
561
562 MUTATOR_HOOKFUNCTION(buffs_PlayerThink)
563 {
564         if(gameover || self.deadflag != DEAD_NO) { return FALSE; }
565         
566         if(time < self.buff_disability_time)
567         if(time >= self.buff_disability_effect_time)
568         {
569                 pointparticles(particleeffectnum("smoking"), self.origin + ((self.mins + self.maxs) * 0.5), '0 0 0', 1);
570                 self.buff_disability_effect_time = time + 0.5;
571         }
572         
573         if(self.frozen)
574         {
575                 if(self.buffs)
576                 {
577                         Send_Notification(NOTIF_ALL_EXCEPT, self, MSG_INFO, INFO_ITEM_BUFF_LOST, self.netname, self.buffs);
578                         self.buffs = 0;
579                 }
580         }
581                 
582         if((self.buffs & BUFF_INVISIBLE) && (self.oldbuffs & BUFF_INVISIBLE))
583         if(self.alpha != autocvar_g_buffs_invisible_alpha)
584                 self.alpha = autocvar_g_buffs_invisible_alpha;
585
586         if(self.buffs != self.oldbuffs)
587         {
588                 if(self.oldbuffs & BUFF_AMMO)
589                 {
590                         if(self.buff_ammo_prev_infitems)
591                                 self.items |= IT_UNLIMITED_WEAPON_AMMO;
592                         else
593                                 self.items &= ~IT_UNLIMITED_WEAPON_AMMO;
594                 }
595                 else if(self.buffs & BUFF_AMMO)
596                 {
597                         self.buff_ammo_prev_infitems = (self.items & IT_UNLIMITED_WEAPON_AMMO);
598                         self.items |= IT_UNLIMITED_WEAPON_AMMO;
599                         if(!self.ammo_shells) { self.ammo_shells = 20; }
600                         if(!self.ammo_cells) { self.ammo_cells = 20; }
601                         if(!self.ammo_rockets) { self.ammo_rockets = 20; }
602                         if(!self.ammo_nails) { self.ammo_nails = 20; }
603                         if(!self.ammo_fuel) { self.ammo_fuel = 20; }
604                 }
605                 
606                 if(self.oldbuffs & BUFF_INVISIBLE)
607                 {
608                         if(time < self.strength_finished && g_instagib)
609                                 self.alpha = autocvar_g_instagib_invis_alpha;
610                         else
611                                 self.alpha = self.buff_invisible_prev_alpha;
612                 }
613                 else if(self.buffs & BUFF_INVISIBLE)
614                 {
615                         if(time < self.strength_finished && g_instagib)
616                                 self.buff_invisible_prev_alpha = default_player_alpha;
617                         else
618                                 self.buff_invisible_prev_alpha = self.alpha;
619                         self.alpha = autocvar_g_buffs_invisible_alpha;
620                 }
621                 
622                 if(self.oldbuffs & BUFF_FLIGHT)
623                         self.gravity = self.buff_flight_prev_gravity;
624                 else if(self.buffs & BUFF_FLIGHT)
625                 {
626                         self.buff_flight_prev_gravity = self.gravity;
627                         self.gravity = autocvar_g_buffs_flight_gravity;
628                 }
629
630                 self.oldbuffs = self.buffs;
631                 if(self.buffs)
632                 {
633                         if(!self.buff_model)
634                         {
635                                 self.buff_model = spawn();
636                                 setmodel(self.buff_model, "models/relics/relic.md3");
637                                 setsize(self.buff_model, '0 0 -40', '0 0 40');
638                                 setattachment(self.buff_model, self, "");
639                                 setorigin(self.buff_model, '0 0 1' * (self.buff_model.maxs_z * 1));
640                                 self.buff_model.owner = self;
641                                 self.buff_model.scale = 0.7;
642                                 self.buff_model.pflags = PFLAGS_FULLDYNAMIC;
643                                 self.buff_model.light_lev = 200;
644                                 self.buff_model.customizeentityforclient = buffs_BuffModel_Customize;
645                         }
646                         self.buff_model.color = Buff_Color(self.buffs);
647                         self.buff_model.glowmod = ((self.buff_model.team) ? Team_ColorRGB(self.buff_model.team) + '0.1 0.1 0.1' : self.buff_model.color);
648                         self.buff_model.skin = Buff_Skin(self.buffs);
649                         
650                         self.effects |= EF_NOSHADOW;
651                 }
652                 else
653                 {
654                         remove(self.buff_model);
655                         self.buff_model = world;
656                         
657                         self.effects &= ~(EF_NOSHADOW);
658                 }
659         }
660
661         if(self.buff_model)
662         {
663                 self.buff_model.effects = self.effects;
664                 self.buff_model.effects |= EF_LOWPRECISION;
665                 self.buff_model.effects = self.buff_model.effects & EFMASK_CHEAP; // eat performance
666                 
667                 self.buff_model.alpha = self.alpha;
668         }
669
670         return FALSE;
671 }
672
673 MUTATOR_HOOKFUNCTION(buffs_SpectateCopy)
674 {
675         self.buffs = other.buffs;
676         return FALSE;
677 }
678
679 MUTATOR_HOOKFUNCTION(buffs_VehicleEnter)
680 {
681         vh_vehicle.buffs = vh_player.buffs;
682         vh_player.buffs = 0;
683         return FALSE;
684 }
685
686 MUTATOR_HOOKFUNCTION(buffs_VehicleExit)
687 {
688         vh_player.buffs = vh_vehicle.buffs;
689         vh_vehicle.buffs = 0;
690         return FALSE;
691 }
692
693 MUTATOR_HOOKFUNCTION(buffs_PlayerRegen)
694 {
695         if(self.buffs & BUFF_MEDIC)
696         {
697                 regen_mod_rot = autocvar_g_buffs_medic_rot;
698                 regen_mod_limit = regen_mod_max = autocvar_g_buffs_medic_max;
699                 regen_mod_regen = autocvar_g_buffs_medic_regen;
700         }
701         
702         if(self.buffs & BUFF_SPEED)
703                 regen_mod_regen = autocvar_g_buffs_speed_regen;
704
705         return FALSE;
706 }
707
708 MUTATOR_HOOKFUNCTION(buffs_GetCvars)
709 {
710         GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_buffs_autoreplace, "cl_buffs_autoreplace");
711         return FALSE;
712 }
713
714 MUTATOR_HOOKFUNCTION(buffs_BuildMutatorsString)
715 {
716         ret_string = strcat(ret_string, ":Buffs");
717         return FALSE;
718 }
719
720 MUTATOR_HOOKFUNCTION(buffs_BuildMutatorsPrettyString)
721 {
722         ret_string = strcat(ret_string, ", Buffs");
723         return FALSE;
724 }
725
726 void buffs_DelayedInit()
727 {
728         if(autocvar_g_buffs_spawn_count > 0)
729         if(find(world, classname, "item_buff") == world)
730         {
731                 float i;
732                 for(i = 0; i < autocvar_g_buffs_spawn_count; ++i)
733                 {
734                         entity e = spawn();
735                         e.spawnflags |= 1; // always randomize
736                         e.velocity = randomvec() * 250; // this gets reset anyway if random location works
737                         buff_Init(e);
738                 }
739         }
740 }
741
742 void buffs_Initialize()
743 {
744         precache_model("models/relics/relic.md3");
745         precache_sound("misc/strength_respawn.wav");
746         precache_sound("misc/shield_respawn.wav");
747         precache_sound("relics/relic_effect.wav");
748         precache_sound("weapons/rocket_impact.wav");
749         precache_sound("keepaway/respawn.wav");
750
751         addstat(STAT_BUFFS, AS_INT, buffs);
752         addstat(STAT_MOVEVARS_JUMPVELOCITY, AS_FLOAT, stat_jumpheight);
753         
754         InitializeEntity(world, buffs_DelayedInit, INITPRIO_FINDTARGET);
755 }
756
757 MUTATOR_DEFINITION(mutator_buffs)
758 {
759         MUTATOR_HOOK(PlayerDamage_SplitHealthArmor, buffs_PlayerDamage_SplitHealthArmor, CBC_ORDER_ANY);
760         MUTATOR_HOOK(PlayerDamage_Calculate, buffs_PlayerDamage_Calculate, CBC_ORDER_ANY);
761         MUTATOR_HOOK(PlayerSpawn, buffs_PlayerSpawn, CBC_ORDER_ANY);
762         MUTATOR_HOOK(PlayerPhysics, buffs_PlayerPhysics, CBC_ORDER_ANY);
763         MUTATOR_HOOK(PlayerJump, buffs_PlayerJump, CBC_ORDER_ANY);
764         MUTATOR_HOOK(MonsterMove, buffs_MonsterMove, CBC_ORDER_ANY);
765         MUTATOR_HOOK(SpectateCopy, buffs_SpectateCopy, CBC_ORDER_ANY);
766         MUTATOR_HOOK(VehicleEnter, buffs_VehicleEnter, CBC_ORDER_ANY);
767         MUTATOR_HOOK(VehicleExit, buffs_VehicleExit, CBC_ORDER_ANY);
768         MUTATOR_HOOK(PlayerRegen, buffs_PlayerRegen, CBC_ORDER_ANY);
769         MUTATOR_HOOK(PlayerDies, buffs_PlayerDies, CBC_ORDER_ANY);
770         MUTATOR_HOOK(PlayerUseKey, buffs_PlayerUseKey, CBC_ORDER_ANY);
771         MUTATOR_HOOK(MakePlayerObserver, buffs_RemovePlayer, CBC_ORDER_ANY);
772         MUTATOR_HOOK(ClientDisconnect, buffs_RemovePlayer, CBC_ORDER_ANY);
773         MUTATOR_HOOK(OnEntityPreSpawn, buffs_OnEntityPreSpawn, CBC_ORDER_ANY);
774         MUTATOR_HOOK(CustomizeWaypoint, buffs_CustomizeWaypoint, CBC_ORDER_ANY);
775         MUTATOR_HOOK(WeaponRateFactor, buffs_WeaponRate, CBC_ORDER_ANY);
776         MUTATOR_HOOK(PlayerPreThink, buffs_PlayerThink, CBC_ORDER_ANY);
777         MUTATOR_HOOK(GetCvars, buffs_GetCvars, CBC_ORDER_ANY);
778         MUTATOR_HOOK(BuildMutatorsString, buffs_BuildMutatorsString, CBC_ORDER_ANY);
779         MUTATOR_HOOK(BuildMutatorsPrettyString, buffs_BuildMutatorsPrettyString, CBC_ORDER_ANY);
780
781         MUTATOR_ONADD
782         {
783                 buffs_Initialize();
784         }
785
786         return FALSE;
787 }