]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/buffs/sv_buffs.qc
Rate more small healths/armors in groups
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / buffs / sv_buffs.qc
1 #include "sv_buffs.qh"
2
3 #include <common/triggers/target/music.qh>
4 #include <common/gamemodes/_mod.qh>
5
6 .float buff_time = _STAT(BUFF_TIME);
7 void buffs_DelayedInit(entity this);
8
9 AUTOCVAR(g_buffs, int, -1, "Enable buffs, -1: enabled but no auto location or replacing powerups, 1: enabled and can replace them");
10
11 REGISTER_MUTATOR(buffs, autocvar_g_buffs)
12 {
13         MUTATOR_ONADD
14         {
15                 if(autocvar_g_buffs > 0)
16                         InitializeEntity(NULL, buffs_DelayedInit, INITPRIO_FINDTARGET);
17         }
18 }
19
20 bool buffs_BuffModel_Customize(entity this, entity client)
21 {
22         entity player, myowner;
23         bool same_team;
24
25         player = WaypointSprite_getviewentity(client);
26         myowner = this.owner;
27         same_team = (SAME_TEAM(player, myowner) || SAME_TEAM(player, myowner));
28
29         if(myowner.alpha <= 0.5 && !same_team && myowner.alpha != 0)
30                 return false;
31
32         if(MUTATOR_CALLHOOK(BuffModel_Customize, this, player))
33                 return false;
34
35         if(player == myowner || (IS_SPEC(client) && client.enemy == myowner))
36         {
37                 // somewhat hide the model, but keep the glow
38                 this.effects = 0;
39                 this.alpha = -1;
40         }
41         else
42         {
43                 this.effects = EF_FULLBRIGHT | EF_LOWPRECISION;
44                 this.alpha = 1;
45         }
46         return true;
47 }
48
49 void buffs_BuffModel_Spawn(entity player)
50 {
51         player.buff_model = spawn();
52         setmodel(player.buff_model, MDL_BUFF);
53         setsize(player.buff_model, '0 0 -40', '0 0 40');
54         setattachment(player.buff_model, player, "");
55         setorigin(player.buff_model, '0 0 1' * (player.buff_model.maxs.z * 1));
56         player.buff_model.owner = player;
57         player.buff_model.scale = 0.7;
58         player.buff_model.pflags = PFLAGS_FULLDYNAMIC;
59         player.buff_model.light_lev = 200;
60         setcefc(player.buff_model, buffs_BuffModel_Customize);
61 }
62
63 vector buff_GlowColor(entity buff)
64 {
65         //if(buff.team) { return Team_ColorRGB(buff.team); }
66         return buff.m_color;
67 }
68
69 void buff_Effect(entity player, string eff)
70 {
71         if(!autocvar_g_buffs_effects) { return; }
72
73         if(time >= player.buff_effect_delay)
74         {
75                 Send_Effect_(eff, player.origin + ((player.mins + player.maxs) * 0.5), '0 0 0', 1);
76                 player.buff_effect_delay = time + 0.05; // prevent spam
77         }
78 }
79
80 // buff item
81 bool buff_Waypoint_visible_for_player(entity this, entity player, entity view)
82 {
83         if(!this.owner.buff_active && !this.owner.buff_activetime)
84                 return false;
85
86         if (view.buffs)
87         {
88                 return view.cvar_cl_buffs_autoreplace == false || view.buffs != this.owner.buffs;
89         }
90
91         return WaypointSprite_visible_for_player(this, player, view);
92 }
93
94 void buff_Waypoint_Spawn(entity e)
95 {
96         entity buff = buff_FirstFromFlags(e.buffs);
97         entity wp = WaypointSprite_Spawn(WP_Buff, 0, autocvar_g_buffs_waypoint_distance, e, '0 0 1' * e.maxs.z, NULL, e.team, e, buff_waypoint, true, RADARICON_Buff);
98         wp.wp_extra = buff.m_id;
99         WaypointSprite_UpdateTeamRadar(e.buff_waypoint, RADARICON_Buff, e.glowmod);
100         e.buff_waypoint.waypointsprite_visible_for_player = buff_Waypoint_visible_for_player;
101 }
102
103 void buff_SetCooldown(entity this, float cd)
104 {
105         cd = max(0, cd);
106
107         if(!this.buff_waypoint)
108                 buff_Waypoint_Spawn(this);
109
110         WaypointSprite_UpdateBuildFinished(this.buff_waypoint, time + cd);
111         this.buff_activetime = cd;
112         this.buff_active = !cd;
113 }
114
115 void buff_Respawn(entity this)
116 {
117         if(game_stopped) return;
118
119         vector oldbufforigin = this.origin;
120         this.velocity = '0 0 200';
121
122         if(!MoveToRandomMapLocation(this, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY,
123                 ((autocvar_g_buffs_random_location_attempts > 0) ? autocvar_g_buffs_random_location_attempts : 10), 1024, 256))
124         {
125                 entity spot = SelectSpawnPoint(this, true);
126                 setorigin(this, spot.origin);
127                 this.velocity = ((randomvec() * 100) + '0 0 200');
128                 this.angles = spot.angles;
129         }
130
131         tracebox(this.origin, this.mins * 1.5, this.maxs * 1.5, this.origin, MOVE_NOMONSTERS, this);
132
133         setorigin(this, trace_endpos); // attempt to unstick
134
135         set_movetype(this, MOVETYPE_TOSS);
136
137         makevectors(this.angles);
138         this.angles = '0 0 0';
139         if(autocvar_g_buffs_random_lifetime > 0)
140                 this.lifetime = time + autocvar_g_buffs_random_lifetime;
141
142         Send_Effect(EFFECT_ELECTRO_COMBO, oldbufforigin + ((this.mins + this.maxs) * 0.5), '0 0 0', 1);
143         Send_Effect(EFFECT_ELECTRO_COMBO, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
144
145         WaypointSprite_Ping(this.buff_waypoint);
146
147         sound(this, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
148 }
149
150 void buff_Touch(entity this, entity toucher)
151 {
152         if(game_stopped) return;
153
154         if(ITEM_TOUCH_NEEDKILL())
155         {
156                 buff_Respawn(this);
157                 return;
158         }
159
160         if((this.team && DIFF_TEAM(toucher, this))
161         || (STAT(FROZEN, toucher))
162         || (toucher.vehicle)
163         || (!this.buff_active)
164         )
165         {
166                 // can't touch this
167                 return;
168         }
169
170         if(MUTATOR_CALLHOOK(BuffTouch, this, toucher))
171                 return;
172         toucher = M_ARGV(1, entity);
173
174         if(!IS_PLAYER(toucher))
175                 return; // incase mutator changed toucher
176
177         if (toucher.buffs)
178         {
179                 if (toucher.cvar_cl_buffs_autoreplace && toucher.buffs != this.buffs)
180                 {
181                         int buffid = buff_FirstFromFlags(toucher.buffs).m_id;
182                         //Send_Notification(NOTIF_ONE, toucher, MSG_MULTI, ITEM_BUFF_DROP, toucher.buffs);
183                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ITEM_BUFF_LOST, toucher.netname, buffid);
184
185                         toucher.buffs = 0;
186                         //sound(toucher, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
187                 }
188                 else { return; } // do nothing
189         }
190
191         this.owner = toucher;
192         this.buff_active = false;
193         this.lifetime = 0;
194         int buffid = buff_FirstFromFlags(this.buffs).m_id;
195         Send_Notification(NOTIF_ONE, toucher, MSG_MULTI, ITEM_BUFF_GOT, buffid);
196         Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_INFO, INFO_ITEM_BUFF, toucher.netname, buffid);
197
198         Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
199         sound(toucher, CH_TRIGGER, SND_SHIELD_RESPAWN, VOL_BASE, ATTN_NORM);
200         toucher.buffs |= (this.buffs);
201 }
202
203 float buff_Available(entity buff)
204 {
205         if (buff == BUFF_Null)
206                 return false;
207         if (buff == BUFF_AMMO && ((start_items & IT_UNLIMITED_WEAPON_AMMO) || (start_items & IT_UNLIMITED_AMMO) || (cvar("g_melee_only"))))
208                 return false;
209         if (buff == BUFF_VAMPIRE && cvar("g_vampire"))
210                 return false;
211         return cvar(strcat("g_buffs_", buff.m_name));
212 }
213
214 .int buff_seencount;
215
216 void buff_NewType(entity ent, float cb)
217 {
218         RandomSelection_Init();
219         FOREACH(Buffs, buff_Available(it), LAMBDA(
220                 it.buff_seencount += 1;
221                 // if it's already been chosen, give it a lower priority
222                 RandomSelection_AddFloat(it.m_itemid, 1, max(0.2, 1 / it.buff_seencount));
223         ));
224         ent.buffs = RandomSelection_chosen_float;
225 }
226
227 void buff_Think(entity this)
228 {
229         if(this.buffs != this.oldbuffs)
230         {
231                 entity buff = buff_FirstFromFlags(this.buffs);
232                 this.color = buff.m_color;
233                 this.glowmod = buff_GlowColor(buff);
234                 this.skin = buff.m_skin;
235
236                 setmodel(this, MDL_BUFF);
237                 setsize(this, BUFF_MIN, BUFF_MAX);
238
239                 if(this.buff_waypoint)
240                 {
241                         //WaypointSprite_Disown(this.buff_waypoint, 1);
242                         WaypointSprite_Kill(this.buff_waypoint);
243                         buff_Waypoint_Spawn(this);
244                         if(this.buff_activetime)
245                                 WaypointSprite_UpdateBuildFinished(this.buff_waypoint, time + this.buff_activetime - frametime);
246                 }
247
248                 this.oldbuffs = this.buffs;
249         }
250
251         if(!game_stopped)
252         if((round_handler_IsActive() && !round_handler_IsRoundStarted()) || time >= game_starttime)
253         if(!this.buff_activetime_updated)
254         {
255                 buff_SetCooldown(this, this.buff_activetime);
256                 this.buff_activetime_updated = true;
257         }
258
259         if(!this.buff_active && !this.buff_activetime)
260         if(!this.owner || STAT(FROZEN, this.owner) || IS_DEAD(this.owner) || !this.owner.iscreature || !(this.owner.buffs & this.buffs))
261         {
262                 buff_SetCooldown(this, autocvar_g_buffs_cooldown_respawn + frametime);
263                 this.owner = NULL;
264                 if(autocvar_g_buffs_randomize)
265                         buff_NewType(this, this.buffs);
266
267                 if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
268                         buff_Respawn(this);
269         }
270
271         if(this.buff_activetime)
272         if(!game_stopped)
273         if((round_handler_IsActive() && !round_handler_IsRoundStarted()) || time >= game_starttime)
274         {
275                 this.buff_activetime = max(0, this.buff_activetime - frametime);
276
277                 if(!this.buff_activetime)
278                 {
279                         this.buff_active = true;
280                         sound(this, CH_TRIGGER, SND_STRENGTH_RESPAWN, VOL_BASE, ATTN_NORM);
281                         Send_Effect(EFFECT_ITEM_RESPAWN, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
282                 }
283         }
284
285         if(this.buff_active)
286         {
287                 if(this.team && !this.buff_waypoint)
288                         buff_Waypoint_Spawn(this);
289
290                 if(this.lifetime)
291                 if(time >= this.lifetime)
292                         buff_Respawn(this);
293         }
294
295         this.nextthink = time;
296         //this.angles_y = time * 110.1;
297 }
298
299 void buff_Waypoint_Reset(entity this)
300 {
301         WaypointSprite_Kill(this.buff_waypoint);
302
303         if(this.buff_activetime) { buff_Waypoint_Spawn(this); }
304 }
305
306 void buff_Reset(entity this)
307 {
308         if(autocvar_g_buffs_randomize)
309                 buff_NewType(this, this.buffs);
310         this.owner = NULL;
311         buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate);
312         buff_Waypoint_Reset(this);
313         this.buff_activetime_updated = false;
314
315         if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
316                 buff_Respawn(this);
317 }
318
319 bool buff_Customize(entity this, entity client)
320 {
321         entity player = WaypointSprite_getviewentity(client);
322         if(!this.buff_active || (this.team && DIFF_TEAM(player, this)))
323         {
324                 this.alpha = 0.3;
325                 if(this.effects & EF_FULLBRIGHT) { this.effects &= ~(EF_FULLBRIGHT); }
326                 this.pflags = 0;
327         }
328         else
329         {
330                 this.alpha = 1;
331                 if(!(this.effects & EF_FULLBRIGHT)) { this.effects |= EF_FULLBRIGHT; }
332                 this.light_lev = 220 + 36 * sin(time);
333                 this.pflags = PFLAGS_FULLDYNAMIC;
334         }
335         return true;
336 }
337
338 void buff_Init(entity this)
339 {
340         if(!cvar("g_buffs")) { delete(this); return; }
341
342         if(!teamplay && this.team) { this.team = 0; }
343
344         entity buff = buff_FirstFromFlags(this.buffs);
345
346         if(!this.buffs || buff_Available(buff))
347                 buff_NewType(this, 0);
348
349         this.classname = "item_buff";
350         this.solid = SOLID_TRIGGER;
351         this.flags = FL_ITEM;
352         this.bot_pickup = true;
353         this.bot_pickupevalfunc = generic_pickupevalfunc;
354         this.bot_pickupbasevalue = 1000;
355         IL_PUSH(g_items, this);
356         setthink(this, buff_Think);
357         settouch(this, buff_Touch);
358         this.reset = buff_Reset;
359         this.nextthink = time + 0.1;
360         this.gravity = 1;
361         set_movetype(this, MOVETYPE_TOSS);
362         this.scale = 1;
363         this.skin = buff.m_skin;
364         this.effects = EF_FULLBRIGHT | EF_STARDUST | EF_NOSHADOW;
365         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
366         setcefc(this, buff_Customize);
367         //this.gravity = 100;
368         this.color = buff.m_color;
369         this.glowmod = buff_GlowColor(this);
370         buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate + game_starttime);
371         this.buff_active = !this.buff_activetime;
372         this.pflags = PFLAGS_FULLDYNAMIC;
373
374         if(this.spawnflags & 1)
375                 this.noalign = true;
376
377         if(this.noalign)
378                 set_movetype(this, MOVETYPE_NONE); // reset by random location
379
380         setmodel(this, MDL_BUFF);
381         setsize(this, BUFF_MIN, BUFF_MAX);
382
383         if(cvar("g_buffs_random_location") || (this.spawnflags & 64))
384                 buff_Respawn(this);
385 }
386
387 void buff_Init_Compat(entity ent, entity replacement)
388 {
389         if (ent.spawnflags & 2)
390                 ent.team = NUM_TEAM_1;
391         else if (ent.spawnflags & 4)
392                 ent.team = NUM_TEAM_2;
393
394         ent.buffs = replacement.m_itemid;
395
396         buff_Init(ent);
397 }
398
399 void buff_SpawnReplacement(entity ent, entity old)
400 {
401         setorigin(ent, old.origin);
402         ent.angles = old.angles;
403         ent.noalign = (old.noalign || (old.spawnflags & 1));
404
405         buff_Init(ent);
406 }
407
408 void buff_Vengeance_DelayedDamage(entity this)
409 {
410         if(this.enemy)
411                 Damage(this.enemy, this.owner, this.owner, this.dmg, DEATH_BUFF.m_id, this.enemy.origin, '0 0 0');
412
413         delete(this);
414         return;
415 }
416
417 // note: only really useful in teamplay
418 void buff_Medic_Heal(entity this)
419 {
420         FOREACH_CLIENT(IS_PLAYER(it) && it != this && vdist(it.origin - this.origin, <=, autocvar_g_buffs_medic_heal_range),
421         {
422                 if(SAME_TEAM(it, this))
423                 if(it.health < autocvar_g_balance_health_regenstable)
424                 {
425                         Send_Effect(EFFECT_HEALING, it.origin, '0 0 0', 1);
426                         it.health = bound(0, it.health + autocvar_g_buffs_medic_heal_amount, autocvar_g_balance_health_regenstable);
427                 }
428         });
429 }
430
431 float buff_Inferno_CalculateTime(float damg, float offset_x, float offset_y, float intersect_x, float intersect_y, float base)
432 {
433         return offset_y + (intersect_y - offset_y) * logn(((damg - offset_x) * ((base - 1) / intersect_x)) + 1, base);
434 }
435
436 // mutator hooks
437 MUTATOR_HOOKFUNCTION(buffs, PlayerDamage_SplitHealthArmor)
438 {
439         entity frag_target = M_ARGV(2, entity);
440         float frag_deathtype = M_ARGV(6, float);
441         float frag_damage = M_ARGV(7, float);
442
443         if(frag_deathtype == DEATH_BUFF.m_id) { return; }
444
445         if(frag_target.buffs & BUFF_RESISTANCE.m_itemid)
446         {
447                 vector v = healtharmor_applydamage(50, autocvar_g_buffs_resistance_blockpercent, frag_deathtype, frag_damage);
448                 M_ARGV(4, float) = v.x; // take
449                 M_ARGV(5, float) = v.y; // save
450         }
451 }
452
453 MUTATOR_HOOKFUNCTION(buffs, Damage_Calculate)
454 {
455         entity frag_attacker = M_ARGV(1, entity);
456         entity frag_target = M_ARGV(2, entity);
457         float frag_deathtype = M_ARGV(3, float);
458         float frag_damage = M_ARGV(4, float);
459         vector frag_force = M_ARGV(6, vector);
460
461         if(frag_deathtype == DEATH_BUFF.m_id) { return; }
462
463         if(frag_target.buffs & BUFF_SPEED.m_itemid)
464         if(frag_target != frag_attacker)
465                 frag_damage *= autocvar_g_buffs_speed_damage_take;
466
467         if(frag_target.buffs & BUFF_MEDIC.m_itemid)
468         if((frag_target.health - frag_damage) <= 0)
469         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
470         if(frag_attacker)
471         if(random() <= autocvar_g_buffs_medic_survive_chance)
472                 frag_damage = max(5, frag_target.health - autocvar_g_buffs_medic_survive_health);
473
474         if(frag_target.buffs & BUFF_JUMP.m_itemid)
475         if(frag_deathtype == DEATH_FALL.m_id)
476                 frag_damage = 0;
477
478         if(frag_target.buffs & BUFF_VENGEANCE.m_itemid)
479         if(frag_attacker)
480         if(frag_attacker != frag_target)
481         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
482         {
483                 entity dmgent = spawn();
484
485                 dmgent.dmg = frag_damage * autocvar_g_buffs_vengeance_damage_multiplier;
486                 dmgent.enemy = frag_attacker;
487                 dmgent.owner = frag_target;
488                 setthink(dmgent, buff_Vengeance_DelayedDamage);
489                 dmgent.nextthink = time + 0.1;
490         }
491
492         if(frag_target.buffs & BUFF_BASH.m_itemid)
493         if(frag_attacker != frag_target)
494                 frag_force = '0 0 0';
495
496         if(frag_attacker.buffs & BUFF_BASH.m_itemid)
497         if(frag_force)
498         if(frag_attacker == frag_target)
499                 frag_force *= autocvar_g_buffs_bash_force_self;
500         else
501                 frag_force *= autocvar_g_buffs_bash_force;
502
503         if(frag_attacker.buffs & BUFF_DISABILITY.m_itemid)
504         if(frag_target != frag_attacker)
505                 frag_target.buff_disability_time = time + autocvar_g_buffs_disability_slowtime;
506
507         if(frag_target.buffs & BUFF_INFERNO.m_itemid)
508         {
509                 if(frag_deathtype == DEATH_FIRE.m_id)
510                         frag_damage = 0;
511                 if(frag_deathtype == DEATH_LAVA.m_id)
512                         frag_damage *= 0.5; // TODO: cvarize?
513         }
514
515         if(frag_attacker.buffs & BUFF_LUCK.m_itemid)
516         if(frag_attacker != frag_target)
517         if(autocvar_g_buffs_luck_damagemultiplier > 0)
518         if(random() <= autocvar_g_buffs_luck_chance)
519                 frag_damage *= autocvar_g_buffs_luck_damagemultiplier;
520
521         if(frag_attacker.buffs & BUFF_INFERNO.m_itemid)
522         if(frag_target != frag_attacker) {
523                 float btime = buff_Inferno_CalculateTime(
524                         frag_damage,
525                         0,
526                         autocvar_g_buffs_inferno_burntime_min_time,
527                         autocvar_g_buffs_inferno_burntime_target_damage,
528                         autocvar_g_buffs_inferno_burntime_target_time,
529                         autocvar_g_buffs_inferno_burntime_factor
530                 );
531                 Fire_AddDamage(frag_target, frag_attacker, (frag_damage * autocvar_g_buffs_inferno_damagemultiplier), btime, DEATH_BUFF.m_id);
532         }
533
534         // this... is ridiculous (TODO: fix!)
535         if(frag_attacker.buffs & BUFF_VAMPIRE.m_itemid)
536         if(!frag_target.vehicle)
537         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
538         if(!IS_DEAD(frag_target))
539         if(IS_PLAYER(frag_target) || IS_MONSTER(frag_target))
540         if(frag_attacker != frag_target)
541         if(!STAT(FROZEN, frag_target))
542         if(frag_target.takedamage)
543         if(DIFF_TEAM(frag_attacker, frag_target))
544         {
545                 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);
546                 if(frag_target.armorvalue)
547                         frag_attacker.armorvalue = bound(0, frag_attacker.armorvalue + bound(0, frag_damage * autocvar_g_buffs_vampire_damage_steal, frag_target.armorvalue), g_pickup_armorsmall_max);
548         }
549
550         M_ARGV(4, float) = frag_damage;
551         M_ARGV(6, vector) = frag_force;
552 }
553
554 MUTATOR_HOOKFUNCTION(buffs, PlayerSpawn)
555 {
556         entity player = M_ARGV(0, entity);
557
558         player.buffs = 0;
559         player.buff_time = 0;
560         // reset timers here to prevent them continuing after re-spawn
561         player.buff_disability_time = 0;
562         player.buff_disability_effect_time = 0;
563 }
564
565 .float stat_sv_maxspeed;
566 .float stat_sv_airspeedlimit_nonqw;
567 .float stat_sv_jumpvelocity;
568
569 MUTATOR_HOOKFUNCTION(buffs, PlayerPhysics)
570 {
571         entity player = M_ARGV(0, entity);
572
573         if(player.buffs & BUFF_SPEED.m_itemid)
574         {
575                 player.stat_sv_maxspeed *= autocvar_g_buffs_speed_speed;
576                 player.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_speed_speed;
577         }
578
579         if(time < player.buff_disability_time)
580         {
581                 player.stat_sv_maxspeed *= autocvar_g_buffs_disability_speed;
582                 player.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_disability_speed;
583         }
584
585         if(player.buffs & BUFF_JUMP.m_itemid)
586         {
587                 // automatically reset, no need to worry
588                 player.stat_sv_jumpvelocity = autocvar_g_buffs_jump_height;
589         }
590 }
591
592 MUTATOR_HOOKFUNCTION(buffs, PlayerJump)
593 {
594         entity player = M_ARGV(0, entity);
595
596         if(player.buffs & BUFF_JUMP.m_itemid)
597                 M_ARGV(1, float) = autocvar_g_buffs_jump_height;
598 }
599
600 MUTATOR_HOOKFUNCTION(buffs, MonsterMove)
601 {
602         entity mon = M_ARGV(0, entity);
603
604         if(time < mon.buff_disability_time)
605         {
606                 M_ARGV(1, float) *= autocvar_g_buffs_disability_speed; // run speed
607                 M_ARGV(2, float) *= autocvar_g_buffs_disability_speed; // walk speed
608         }
609 }
610
611 MUTATOR_HOOKFUNCTION(buffs, PlayerDies)
612 {
613         entity frag_target = M_ARGV(2, entity);
614
615         if(frag_target.buffs)
616         {
617                 int buffid = buff_FirstFromFlags(frag_target.buffs).m_id;
618                 Send_Notification(NOTIF_ALL_EXCEPT, frag_target, MSG_INFO, INFO_ITEM_BUFF_LOST, frag_target.netname, buffid);
619                 frag_target.buffs = 0;
620
621                 if(frag_target.buff_model)
622                 {
623                         delete(frag_target.buff_model);
624                         frag_target.buff_model = NULL;
625                 }
626         }
627 }
628
629 MUTATOR_HOOKFUNCTION(buffs, PlayerUseKey, CBC_ORDER_FIRST)
630 {
631         if(MUTATOR_RETURNVALUE || game_stopped) return;
632
633         entity player = M_ARGV(0, entity);
634
635         if(player.buffs)
636         {
637                 int buffid = buff_FirstFromFlags(player.buffs).m_id;
638                 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid);
639                 Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
640
641                 player.buffs = 0;
642                 player.buff_time = 0; // already notified
643                 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
644                 return true;
645         }
646 }
647
648 MUTATOR_HOOKFUNCTION(buffs, ForbidThrowCurrentWeapon)
649 {
650         if(MUTATOR_RETURNVALUE || game_stopped) return;
651         entity player = M_ARGV(0, entity);
652
653         if(player.buffs & BUFF_SWAPPER.m_itemid)
654         {
655                 float best_distance = autocvar_g_buffs_swapper_range;
656                 entity closest = NULL;
657                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
658                         if(!IS_DEAD(it) && !STAT(FROZEN, it) && !it.vehicle)
659                         if(DIFF_TEAM(it, player))
660                         {
661                                 float test = vlen2(player.origin - it.origin);
662                                 if(test <= best_distance * best_distance)
663                                 {
664                                         best_distance = sqrt(test);
665                                         closest = it;
666                                 }
667                         }
668                 ));
669
670                 if(closest)
671                 {
672                         vector my_org, my_vel, my_ang, their_org, their_vel, their_ang;
673
674                         my_org = player.origin;
675                         my_vel = player.velocity;
676                         my_ang = player.angles;
677                         their_org = closest.origin;
678                         their_vel = closest.velocity;
679                         their_ang = closest.angles;
680
681                         Drop_Special_Items(closest);
682
683                         MUTATOR_CALLHOOK(PortalTeleport, player); // initiate flag dropper
684
685                         setorigin(player, their_org);
686                         setorigin(closest, my_org);
687
688                         closest.velocity = my_vel;
689                         closest.angles = my_ang;
690                         closest.fixangle = true;
691                         closest.oldorigin = my_org;
692                         closest.oldvelocity = my_vel;
693                         player.velocity = their_vel;
694                         player.angles = their_ang;
695                         player.fixangle = true;
696                         player.oldorigin = their_org;
697                         player.oldvelocity = their_vel;
698
699                         // set pusher so player gets the kill if they fall into void
700                         closest.pusher = player;
701                         closest.pushltime = time + autocvar_g_maxpushtime;
702                         closest.istypefrag = PHYS_INPUT_BUTTON_CHAT(closest);
703
704                         Send_Effect(EFFECT_ELECTRO_COMBO, their_org, '0 0 0', 1);
705                         Send_Effect(EFFECT_ELECTRO_COMBO, my_org, '0 0 0', 1);
706
707                         sound(player, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
708                         sound(closest, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
709
710                         // TODO: add a counter to handle how many times one can teleport, and a delay to prevent spam
711                         player.buffs = 0;
712                         return true;
713                 }
714         }
715 }
716
717 bool buffs_RemovePlayer(entity player)
718 {
719         if(player.buff_model)
720         {
721                 delete(player.buff_model);
722                 player.buff_model = NULL;
723         }
724
725         // also reset timers here to prevent them continuing after spectating
726         player.buff_disability_time = 0;
727         player.buff_disability_effect_time = 0;
728
729         return false;
730 }
731 MUTATOR_HOOKFUNCTION(buffs, MakePlayerObserver) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
732 MUTATOR_HOOKFUNCTION(buffs, ClientDisconnect) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
733
734 MUTATOR_HOOKFUNCTION(buffs, CustomizeWaypoint)
735 {
736         entity wp = M_ARGV(0, entity);
737         entity player = M_ARGV(1, entity);
738
739         entity e = WaypointSprite_getviewentity(player);
740
741         // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
742         // but only apply this to real players, not to spectators
743         if((wp.owner.flags & FL_CLIENT) && (wp.owner.buffs & BUFF_INVISIBLE.m_itemid) && (e == player))
744         if(DIFF_TEAM(wp.owner, e))
745                 return true;
746 }
747
748 MUTATOR_HOOKFUNCTION(buffs, OnEntityPreSpawn, CBC_ORDER_LAST)
749 {
750         if(autocvar_g_buffs < 0)
751                 return; // no auto replacing of entities in this mode
752
753         entity ent = M_ARGV(0, entity);
754
755         if(autocvar_g_buffs_replace_powerups)
756         switch(ent.classname)
757         {
758                 case "item_strength":
759                 case "item_invincible":
760                 {
761                         entity e = spawn();
762                         buff_SpawnReplacement(e, ent);
763                         return true;
764                 }
765         }
766 }
767
768 MUTATOR_HOOKFUNCTION(buffs, WeaponRateFactor)
769 {
770         entity player = M_ARGV(1, entity);
771
772         if(player.buffs & BUFF_SPEED.m_itemid)
773                 M_ARGV(0, float) *= autocvar_g_buffs_speed_rate;
774
775         if(time < player.buff_disability_time)
776                 M_ARGV(0, float) *= autocvar_g_buffs_disability_rate;
777 }
778
779 MUTATOR_HOOKFUNCTION(buffs, WeaponSpeedFactor)
780 {
781         entity player = M_ARGV(1, entity);
782
783         if(player.buffs & BUFF_SPEED.m_itemid)
784                 M_ARGV(0, float) *= autocvar_g_buffs_speed_weaponspeed;
785
786         if(time < player.buff_disability_time)
787                 M_ARGV(0, float) *= autocvar_g_buffs_disability_weaponspeed;
788 }
789
790 .bool buff_flight_crouchheld;
791
792 MUTATOR_HOOKFUNCTION(buffs, PlayerPreThink)
793 {
794         entity player = M_ARGV(0, entity);
795
796         if(game_stopped || IS_DEAD(player)) return;
797
798         if(player.buffs & BUFF_FLIGHT.m_itemid)
799         {
800                 if(!PHYS_INPUT_BUTTON_CROUCH(player))
801                         player.buff_flight_crouchheld = false;
802                 else if(!player.buff_flight_crouchheld)
803                 {
804                         player.buff_flight_crouchheld = true;
805                         player.gravity *= -1;
806                 }
807         }
808
809         if(time < player.buff_disability_time)
810         if(time >= player.buff_disability_effect_time)
811         {
812                 Send_Effect(EFFECT_SMOKING, player.origin + ((player.mins + player.maxs) * 0.5), '0 0 0', 1);
813                 player.buff_disability_effect_time = time + 0.5;
814         }
815
816         // handle buff lost status
817         // 1: notify everyone else
818         // 2: notify carrier as well
819         int buff_lost = 0;
820
821         if(player.buff_time && player.buffs)
822         if(time >= player.buff_time)
823         {
824                 player.buff_time = 0;
825                 buff_lost = 2;
826         }
827
828         if(STAT(FROZEN, player)) { buff_lost = 1; }
829
830         if(buff_lost)
831         {
832                 if(player.buffs)
833                 {
834                         int buffid = buff_FirstFromFlags(player.buffs).m_id;
835                         if(buff_lost == 2)
836                         {
837                                 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid); // TODO: special timeout message?
838                                 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
839                         }
840                         else
841                                 Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
842                         player.buffs = 0;
843                 }
844         }
845
846         if(player.buffs & BUFF_MAGNET.m_itemid)
847         {
848                 vector pickup_size;
849                 IL_EACH(g_items, it.itemdef,
850                 {
851                         if(it.buffs)
852                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_buff;
853                         else
854                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_item;
855
856                         if(boxesoverlap(player.absmin - pickup_size, player.absmax + pickup_size, it.absmin, it.absmax))
857                         {
858                                 if(gettouch(it))
859                                         gettouch(it)(it, player);
860                         }
861                 });
862         }
863
864         if(player.buffs & BUFF_AMMO.m_itemid)
865         {
866                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
867                 {
868                         .entity weaponentity = weaponentities[slot];
869                         if(player.(weaponentity).clip_size)
870                                 player.(weaponentity).clip_load = player.(weaponentity).(weapon_load[player.(weaponentity).m_switchweapon.m_id]) = player.(weaponentity).clip_size;
871                 }
872         }
873
874         if((player.buffs & BUFF_INVISIBLE.m_itemid) && (player.oldbuffs & BUFF_INVISIBLE.m_itemid))
875         if(player.alpha != autocvar_g_buffs_invisible_alpha)
876                 player.alpha = autocvar_g_buffs_invisible_alpha; // powerups reset alpha, so we must enforce this (TODO)
877
878         if(player.buffs & BUFF_MEDIC.m_itemid)
879         if(time >= player.buff_medic_healtime)
880         {
881                 buff_Medic_Heal(player);
882                 player.buff_medic_healtime = time + autocvar_g_buffs_medic_heal_delay;
883         }
884
885 #define BUFF_ONADD(b) if ( (player.buffs & (b).m_itemid) && !(player.oldbuffs & (b).m_itemid))
886 #define BUFF_ONREM(b) if (!(player.buffs & (b).m_itemid) &&  (player.oldbuffs & (b).m_itemid))
887
888         if(player.buffs != player.oldbuffs)
889         {
890                 entity buff = buff_FirstFromFlags(player.buffs);
891                 float bufftime = buff != BUFF_Null ? buff.m_time(buff) : 0;
892                 player.buff_time = (bufftime) ? time + bufftime : 0;
893
894                 BUFF_ONADD(BUFF_AMMO)
895                 {
896                         player.buff_ammo_prev_infitems = (player.items & IT_UNLIMITED_WEAPON_AMMO);
897                         player.items |= IT_UNLIMITED_WEAPON_AMMO;
898
899                         if(player.buffs & BUFF_AMMO.m_itemid)
900                         {
901                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
902                                 {
903                                         .entity weaponentity = weaponentities[slot];
904                                         if(player.(weaponentity).clip_load)
905                                                 player.(weaponentity).buff_ammo_prev_clipload = player.(weaponentity).clip_load;
906                                         if(player.(weaponentity).clip_size)
907                                                 player.(weaponentity).clip_load = player.(weaponentity).(weapon_load[player.(weaponentity).m_switchweapon.m_id]) = player.(weaponentity).clip_size;
908                                 }
909                         }
910                 }
911
912                 BUFF_ONREM(BUFF_AMMO)
913                 {
914                         if(player.buff_ammo_prev_infitems)
915                                 player.items |= IT_UNLIMITED_WEAPON_AMMO;
916                         else
917                                 player.items &= ~IT_UNLIMITED_WEAPON_AMMO;
918
919                         if(player.buffs & BUFF_AMMO.m_itemid)
920                         {
921                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
922                                 {
923                                         .entity weaponentity = weaponentities[slot];
924                                         if(player.(weaponentity).buff_ammo_prev_clipload)
925                                                 player.(weaponentity).clip_load = player.(weaponentity).buff_ammo_prev_clipload;
926                                 }
927                         }
928                 }
929
930                 BUFF_ONADD(BUFF_INVISIBLE)
931                 {
932                         if(time < player.strength_finished && g_instagib)
933                                 player.alpha = autocvar_g_instagib_invis_alpha;
934                         else
935                                 player.alpha = player.buff_invisible_prev_alpha;
936                         player.alpha = autocvar_g_buffs_invisible_alpha;
937                 }
938
939                 BUFF_ONREM(BUFF_INVISIBLE)
940                         player.alpha = player.buff_invisible_prev_alpha;
941
942                 BUFF_ONADD(BUFF_FLIGHT)
943                 {
944                         player.buff_flight_oldgravity = player.gravity;
945                         if(!player.gravity)
946                                 player.gravity = 1;
947                 }
948
949                 BUFF_ONREM(BUFF_FLIGHT)
950                         player.gravity = ((player.trigger_gravity_check) ? player.trigger_gravity_check.enemy.gravity : player.buff_flight_oldgravity);
951
952                 player.oldbuffs = player.buffs;
953                 if(player.buffs)
954                 {
955                         if(!player.buff_model)
956                                 buffs_BuffModel_Spawn(player);
957
958                         player.buff_model.color = buff.m_color;
959                         player.buff_model.glowmod = buff_GlowColor(player.buff_model);
960                         player.buff_model.skin = buff.m_skin;
961
962                         player.effects |= EF_NOSHADOW;
963                 }
964                 else
965                 {
966                         delete(player.buff_model);
967                         player.buff_model = NULL;
968
969                         player.effects &= ~(EF_NOSHADOW);
970                 }
971         }
972
973         if(player.buff_model)
974         {
975                 player.buff_model.effects = player.effects;
976                 player.buff_model.effects |= EF_LOWPRECISION;
977                 player.buff_model.effects = player.buff_model.effects & EFMASK_CHEAP; // eat performance
978
979                 player.buff_model.alpha = player.alpha;
980         }
981
982 #undef BUFF_ONADD
983 #undef BUFF_ONREM
984 }
985
986 MUTATOR_HOOKFUNCTION(buffs, SpectateCopy)
987 {
988         entity spectatee = M_ARGV(0, entity);
989         entity client = M_ARGV(1, entity);
990
991         client.buffs = spectatee.buffs;
992 }
993
994 MUTATOR_HOOKFUNCTION(buffs, VehicleEnter)
995 {
996         entity player = M_ARGV(0, entity);
997         entity veh = M_ARGV(1, entity);
998
999         veh.buffs = player.buffs;
1000         player.buffs = 0;
1001         veh.buff_time = max(0, player.buff_time - time);
1002         player.buff_time = 0;
1003 }
1004
1005 MUTATOR_HOOKFUNCTION(buffs, VehicleExit)
1006 {
1007         entity player = M_ARGV(0, entity);
1008         entity veh = M_ARGV(1, entity);
1009
1010         player.buffs = player.oldbuffs = veh.buffs;
1011         veh.buffs = 0;
1012         player.buff_time = time + veh.buff_time;
1013         veh.buff_time = 0;
1014 }
1015
1016 MUTATOR_HOOKFUNCTION(buffs, PlayerRegen)
1017 {
1018         entity player = M_ARGV(0, entity);
1019
1020         if(player.buffs & BUFF_MEDIC.m_itemid)
1021         {
1022                 M_ARGV(2, float) = autocvar_g_buffs_medic_rot; // rot_mod
1023                 M_ARGV(4, float) = M_ARGV(1, float) = autocvar_g_buffs_medic_max; // limit_mod = max_mod
1024                 M_ARGV(2, float) = autocvar_g_buffs_medic_regen; // regen_mod
1025         }
1026
1027         if(player.buffs & BUFF_SPEED.m_itemid)
1028                 M_ARGV(2, float) = autocvar_g_buffs_speed_regen; // regen_mod
1029 }
1030
1031 REPLICATE(cvar_cl_buffs_autoreplace, bool, "cl_buffs_autoreplace");
1032
1033 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsString)
1034 {
1035         if(autocvar_g_buffs > 0) // only report as a mutator if they're enabled
1036                 M_ARGV(0, string) = strcat(M_ARGV(0, string), ":Buffs");
1037 }
1038
1039 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsPrettyString)
1040 {
1041         if(autocvar_g_buffs > 0)
1042                 M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Buffs");
1043 }
1044
1045 void buffs_DelayedInit(entity this)
1046 {
1047         if(autocvar_g_buffs_spawn_count > 0)
1048         if(find(NULL, classname, "item_buff") == NULL)
1049         {
1050                 float i;
1051                 for(i = 0; i < autocvar_g_buffs_spawn_count; ++i)
1052                 {
1053                         entity e = spawn();
1054                         e.spawnflags |= 64; // always randomize
1055                         e.velocity = randomvec() * 250; // this gets reset anyway if random location works
1056                         buff_Init(e);
1057                 }
1058         }
1059 }