]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/buffs/sv_buffs.qc
Merge branch 't0uYK8Ne/target_init' into 'master'
[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 CS(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.buff_active)
161                 return;
162
163         if(MUTATOR_CALLHOOK(BuffTouch, this, toucher))
164                 return;
165         toucher = M_ARGV(1, entity);
166
167         if(!IS_PLAYER(toucher))
168                 return; // incase mutator changed toucher
169
170         if((this.team && DIFF_TEAM(toucher, this))
171         || (STAT(FROZEN, toucher))
172         || (toucher.vehicle)
173         || (time < PS(toucher).buff_shield)
174         )
175         {
176                 // can't touch this
177                 return;
178         }
179
180         if (toucher.buffs)
181         {
182                 if (CS(toucher).cvar_cl_buffs_autoreplace && toucher.buffs != this.buffs)
183                 {
184                         int buffid = buff_FirstFromFlags(toucher.buffs).m_id;
185                         //Send_Notification(NOTIF_ONE, toucher, MSG_MULTI, ITEM_BUFF_DROP, toucher.buffs);
186                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ITEM_BUFF_LOST, toucher.netname, buffid);
187
188                         toucher.buffs = 0;
189                         //sound(toucher, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
190                 }
191                 else { return; } // do nothing
192         }
193
194         this.owner = toucher;
195         this.buff_active = false;
196         this.lifetime = 0;
197         int buffid = buff_FirstFromFlags(this.buffs).m_id;
198         Send_Notification(NOTIF_ONE, toucher, MSG_MULTI, ITEM_BUFF_GOT, buffid);
199         Send_Notification(NOTIF_ALL_EXCEPT, toucher, MSG_INFO, INFO_ITEM_BUFF, toucher.netname, buffid);
200
201         Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
202         sound(toucher, CH_TRIGGER, SND_SHIELD_RESPAWN, VOL_BASE, ATTN_NORM);
203         toucher.buffs |= (this.buffs);
204 }
205
206 float buff_Available(entity buff)
207 {
208         if (buff == BUFF_Null)
209                 return false;
210         if (buff == BUFF_AMMO && ((start_items & IT_UNLIMITED_WEAPON_AMMO) || (start_items & IT_UNLIMITED_AMMO) || (cvar("g_melee_only"))))
211                 return false;
212         if (buff == BUFF_VAMPIRE && cvar("g_vampire"))
213                 return false;
214         return cvar(strcat("g_buffs_", buff.m_name));
215 }
216
217 .int buff_seencount;
218
219 void buff_NewType(entity ent)
220 {
221         RandomSelection_Init();
222         FOREACH(Buffs, buff_Available(it),
223         {
224                 // if it's already been chosen, give it a lower priority
225                 RandomSelection_AddEnt(it, max(0.2, 1 / it.buff_seencount), 1);
226         });
227         entity newbuff = RandomSelection_chosen_ent;
228         newbuff.buff_seencount += 1; // lower chances of seeing this buff again soon
229         ent.buffs = newbuff.m_itemid;
230 }
231
232 void buff_Think(entity this)
233 {
234         if(this.buffs != this.oldbuffs)
235         {
236                 entity buff = buff_FirstFromFlags(this.buffs);
237                 this.color = buff.m_color;
238                 this.glowmod = buff_GlowColor(buff);
239                 this.skin = buff.m_skin;
240
241                 setmodel(this, MDL_BUFF);
242                 setsize(this, BUFF_MIN, BUFF_MAX);
243
244                 if(this.buff_waypoint)
245                 {
246                         //WaypointSprite_Disown(this.buff_waypoint, 1);
247                         WaypointSprite_Kill(this.buff_waypoint);
248                         buff_Waypoint_Spawn(this);
249                         if(this.buff_activetime)
250                                 WaypointSprite_UpdateBuildFinished(this.buff_waypoint, time + this.buff_activetime - frametime);
251                 }
252
253                 this.oldbuffs = this.buffs;
254         }
255
256         if(!game_stopped)
257         if((round_handler_IsActive() && !round_handler_IsRoundStarted()) || time >= game_starttime)
258         if(!this.buff_activetime_updated)
259         {
260                 buff_SetCooldown(this, this.buff_activetime);
261                 this.buff_activetime_updated = true;
262         }
263
264         if(!this.buff_active && !this.buff_activetime)
265         if(!this.owner || STAT(FROZEN, this.owner) || IS_DEAD(this.owner) || !this.owner.iscreature || this.owner.vehicle || !(this.owner.buffs & this.buffs) || this.pickup_anyway > 0 || (this.pickup_anyway >= 0 && autocvar_g_buffs_pickup_anyway))
266         {
267                 buff_SetCooldown(this, autocvar_g_buffs_cooldown_respawn + frametime);
268                 this.owner = NULL;
269                 if(autocvar_g_buffs_randomize)
270                         buff_NewType(this);
271
272                 if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
273                         buff_Respawn(this);
274         }
275
276         if(this.buff_activetime)
277         if(!game_stopped)
278         if((round_handler_IsActive() && !round_handler_IsRoundStarted()) || time >= game_starttime)
279         {
280                 this.buff_activetime = max(0, this.buff_activetime - frametime);
281
282                 if(!this.buff_activetime)
283                 {
284                         this.buff_active = true;
285                         sound(this, CH_TRIGGER, SND_STRENGTH_RESPAWN, VOL_BASE, ATTN_NORM);
286                         Send_Effect(EFFECT_ITEM_RESPAWN, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
287                 }
288         }
289
290         if(this.buff_active)
291         {
292                 if(this.team && !this.buff_waypoint)
293                         buff_Waypoint_Spawn(this);
294
295                 if(this.lifetime)
296                 if(time >= this.lifetime)
297                         buff_Respawn(this);
298         }
299
300         this.nextthink = time;
301         //this.angles_y = time * 110.1;
302 }
303
304 void buff_Waypoint_Reset(entity this)
305 {
306         WaypointSprite_Kill(this.buff_waypoint);
307
308         if(this.buff_activetime) { buff_Waypoint_Spawn(this); }
309 }
310
311 void buff_Reset(entity this)
312 {
313         if(autocvar_g_buffs_randomize)
314                 buff_NewType(this);
315         this.owner = NULL;
316         buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate);
317         buff_Waypoint_Reset(this);
318         this.buff_activetime_updated = false;
319
320         if(autocvar_g_buffs_random_location || (this.spawnflags & 64))
321                 buff_Respawn(this);
322 }
323
324 bool buff_Customize(entity this, entity client)
325 {
326         entity player = WaypointSprite_getviewentity(client);
327         if(!this.buff_active || (this.team && DIFF_TEAM(player, this)))
328         {
329                 this.alpha = 0.3;
330                 if(this.effects & EF_FULLBRIGHT) { this.effects &= ~(EF_FULLBRIGHT); }
331                 this.pflags = 0;
332         }
333         else
334         {
335                 this.alpha = 1;
336                 if(!(this.effects & EF_FULLBRIGHT)) { this.effects |= EF_FULLBRIGHT; }
337                 this.light_lev = 220 + 36 * sin(time);
338                 this.pflags = PFLAGS_FULLDYNAMIC;
339         }
340         return true;
341 }
342
343 void buff_Init(entity this)
344 {
345         if(!cvar("g_buffs")) { delete(this); return; }
346
347         if(!teamplay && this.team) { this.team = 0; }
348
349         entity buff = buff_FirstFromFlags(this.buffs);
350
351         if(!this.buffs || !buff_Available(buff))
352                 buff_NewType(this);
353
354         this.classname = "item_buff";
355         this.solid = SOLID_TRIGGER;
356         this.flags = FL_ITEM;
357         this.bot_pickup = true;
358         this.bot_pickupevalfunc = generic_pickupevalfunc;
359         this.bot_pickupbasevalue = 1000;
360         IL_PUSH(g_items, this);
361         setthink(this, buff_Think);
362         settouch(this, buff_Touch);
363         this.reset = buff_Reset;
364         this.nextthink = time + 0.1;
365         this.gravity = 1;
366         set_movetype(this, MOVETYPE_TOSS);
367         this.scale = 1;
368         this.skin = buff.m_skin;
369         this.effects = EF_FULLBRIGHT | EF_STARDUST | EF_NOSHADOW;
370         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
371         setcefc(this, buff_Customize);
372         //this.gravity = 100;
373         this.color = buff.m_color;
374         this.glowmod = buff_GlowColor(this);
375         buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate + max(0, game_starttime - time));
376         this.buff_active = !this.buff_activetime;
377         this.pflags = PFLAGS_FULLDYNAMIC;
378
379         if(this.spawnflags & 1)
380                 this.noalign = true;
381
382         if(this.noalign)
383                 set_movetype(this, MOVETYPE_NONE); // reset by random location
384
385         setmodel(this, MDL_BUFF);
386         setsize(this, BUFF_MIN, BUFF_MAX);
387
388         if(cvar("g_buffs_random_location") || (this.spawnflags & 64))
389                 buff_Respawn(this);
390 }
391
392 void buff_Init_Compat(entity ent, entity replacement)
393 {
394         if (ent.spawnflags & 2)
395                 ent.team = NUM_TEAM_1;
396         else if (ent.spawnflags & 4)
397                 ent.team = NUM_TEAM_2;
398
399         ent.buffs = replacement.m_itemid;
400
401         buff_Init(ent);
402 }
403
404 void buff_SpawnReplacement(entity ent, entity old)
405 {
406         setorigin(ent, old.origin);
407         ent.angles = old.angles;
408         ent.noalign = Item_ShouldKeepPosition(old);
409
410         buff_Init(ent);
411 }
412
413 void buff_Vengeance_DelayedDamage(entity this)
414 {
415         if(this.enemy)
416                 Damage(this.enemy, this.owner, this.owner, this.dmg, DEATH_BUFF.m_id, DMG_NOWEP, this.enemy.origin, '0 0 0');
417
418         delete(this);
419         return;
420 }
421
422 // note: only really useful in teamplay
423 void buff_Medic_Heal(entity this)
424 {
425         FOREACH_CLIENT(IS_PLAYER(it) && it != this && vdist(it.origin - this.origin, <=, autocvar_g_buffs_medic_heal_range),
426         {
427                 if (!SAME_TEAM(it, this))
428                 {
429                         continue;
430                 }
431                 float hp = GetResourceAmount(it, RESOURCE_HEALTH);
432                 if(hp >= autocvar_g_balance_health_regenstable)
433                 {
434                         continue;
435                 }
436                 Send_Effect(EFFECT_HEALING, it.origin, '0 0 0', 1);
437                 SetResourceAmount(it, RESOURCE_HEALTH, bound(0, hp + autocvar_g_buffs_medic_heal_amount, autocvar_g_balance_health_regenstable));
438         });
439 }
440
441 float buff_Inferno_CalculateTime(float damg, float offset_x, float offset_y, float intersect_x, float intersect_y, float base)
442 {
443         return offset_y + (intersect_y - offset_y) * logn(((damg - offset_x) * ((base - 1) / intersect_x)) + 1, base);
444 }
445
446 // mutator hooks
447 MUTATOR_HOOKFUNCTION(buffs, Damage_Calculate)
448 {
449         entity frag_attacker = M_ARGV(1, entity);
450         entity frag_target = M_ARGV(2, entity);
451         float frag_deathtype = M_ARGV(3, float);
452         float frag_damage = M_ARGV(4, float);
453         vector frag_force = M_ARGV(6, vector);
454
455         if(frag_deathtype == DEATH_BUFF.m_id) { return; }
456
457         if(frag_target.buffs & BUFF_RESISTANCE.m_itemid)
458         {
459                 float reduced = frag_damage * autocvar_g_buffs_resistance_blockpercent;
460                 frag_damage = bound(0, frag_damage - reduced, frag_damage);
461         }
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((GetResourceAmount(frag_target, RESOURCE_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, GetResourceAmount(frag_target, RESOURCE_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                 float amount = bound(0, frag_damage * autocvar_g_buffs_vampire_damage_steal,
546                         GetResourceAmount(frag_target, RESOURCE_HEALTH));
547                 GiveResourceWithLimit(frag_attacker, RESOURCE_HEALTH, amount, g_pickup_healthsmall_max);
548                 if (frag_target.armorvalue)
549                 {
550                         amount = bound(0, frag_damage * autocvar_g_buffs_vampire_damage_steal,
551                                 GetResourceAmount(frag_target, RESOURCE_ARMOR));
552                         GiveResourceWithLimit(frag_attacker, RESOURCE_ARMOR, amount, g_pickup_armorsmall_max);
553                 }
554         }
555
556         M_ARGV(4, float) = frag_damage;
557         M_ARGV(6, vector) = frag_force;
558 }
559
560 MUTATOR_HOOKFUNCTION(buffs, PlayerSpawn)
561 {
562         entity player = M_ARGV(0, entity);
563
564         player.buffs = 0;
565         player.buff_time = 0;
566         PS(player).buff_shield = time + 0.5; // prevent picking up buffs immediately
567         // reset timers here to prevent them continuing after re-spawn
568         player.buff_disability_time = 0;
569         player.buff_disability_effect_time = 0;
570 }
571
572 MUTATOR_HOOKFUNCTION(buffs, PlayerPhysics_UpdateStats)
573 {
574         entity player = M_ARGV(0, entity);
575         // these automatically reset, no need to worry
576
577         if(player.buffs & BUFF_SPEED.m_itemid)
578                 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_buffs_speed_speed;
579
580         if(time < player.buff_disability_time)
581                 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_buffs_disability_speed;
582 }
583
584 MUTATOR_HOOKFUNCTION(buffs, PlayerPhysics)
585 {
586         entity player = M_ARGV(0, entity);
587         // these automatically reset, no need to worry
588
589         if(player.buffs & BUFF_JUMP.m_itemid)
590                 STAT(MOVEVARS_JUMPVELOCITY, player) = autocvar_g_buffs_jump_height;
591 }
592
593 MUTATOR_HOOKFUNCTION(buffs, MonsterMove)
594 {
595         entity mon = M_ARGV(0, entity);
596
597         if(time < mon.buff_disability_time)
598         {
599                 M_ARGV(1, float) *= autocvar_g_buffs_disability_speed; // run speed
600                 M_ARGV(2, float) *= autocvar_g_buffs_disability_speed; // walk speed
601         }
602 }
603
604 MUTATOR_HOOKFUNCTION(buffs, PlayerDies)
605 {
606         entity frag_target = M_ARGV(2, entity);
607
608         if(frag_target.buffs)
609         {
610                 int buffid = buff_FirstFromFlags(frag_target.buffs).m_id;
611                 Send_Notification(NOTIF_ALL_EXCEPT, frag_target, MSG_INFO, INFO_ITEM_BUFF_LOST, frag_target.netname, buffid);
612                 frag_target.buffs = 0;
613
614                 if(frag_target.buff_model)
615                 {
616                         delete(frag_target.buff_model);
617                         frag_target.buff_model = NULL;
618                 }
619         }
620 }
621
622 MUTATOR_HOOKFUNCTION(buffs, PlayerUseKey, CBC_ORDER_FIRST)
623 {
624         if(MUTATOR_RETURNVALUE || game_stopped || !autocvar_g_buffs_drop) return;
625
626         entity player = M_ARGV(0, entity);
627
628         if(player.buffs)
629         {
630                 int buffid = buff_FirstFromFlags(player.buffs).m_id;
631                 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid);
632                 Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
633
634                 player.buffs = 0;
635                 PS(player).buff_shield = time + max(0, autocvar_g_buffs_pickup_delay);
636                 //player.buff_time = 0; // already notified
637                 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
638                 return true;
639         }
640 }
641
642 MUTATOR_HOOKFUNCTION(buffs, ForbidThrowCurrentWeapon)
643 {
644         if(MUTATOR_RETURNVALUE || game_stopped) return;
645         entity player = M_ARGV(0, entity);
646
647         if(player.buffs & BUFF_SWAPPER.m_itemid)
648         {
649                 float best_distance = autocvar_g_buffs_swapper_range;
650                 entity closest = NULL;
651                 FOREACH_CLIENT(IS_PLAYER(it), {
652                         if(!IS_DEAD(it) && !STAT(FROZEN, it) && !it.vehicle)
653                         if(DIFF_TEAM(it, player))
654                         {
655                                 float test = vlen2(player.origin - it.origin);
656                                 if(test <= best_distance * best_distance)
657                                 {
658                                         best_distance = sqrt(test);
659                                         closest = it;
660                                 }
661                         }
662                 });
663
664                 if(closest)
665                 {
666                         vector my_org, my_vel, my_ang, their_org, their_vel, their_ang;
667
668                         my_org = player.origin;
669                         my_vel = player.velocity;
670                         my_ang = player.angles;
671                         their_org = closest.origin;
672                         their_vel = closest.velocity;
673                         their_ang = closest.angles;
674
675                         Drop_Special_Items(closest);
676
677                         MUTATOR_CALLHOOK(PortalTeleport, player); // initiate flag dropper
678
679                         setorigin(player, their_org);
680                         setorigin(closest, my_org);
681
682                         closest.velocity = my_vel;
683                         closest.angles = my_ang;
684                         closest.fixangle = true;
685                         closest.oldorigin = my_org;
686                         closest.oldvelocity = my_vel;
687                         player.velocity = their_vel;
688                         player.angles = their_ang;
689                         player.fixangle = true;
690                         player.oldorigin = their_org;
691                         player.oldvelocity = their_vel;
692
693                         // set pusher so player gets the kill if they fall into void
694                         closest.pusher = player;
695                         closest.pushltime = time + autocvar_g_maxpushtime;
696                         closest.istypefrag = PHYS_INPUT_BUTTON_CHAT(closest);
697
698                         Send_Effect(EFFECT_ELECTRO_COMBO, their_org, '0 0 0', 1);
699                         Send_Effect(EFFECT_ELECTRO_COMBO, my_org, '0 0 0', 1);
700
701                         sound(player, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
702                         sound(closest, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
703
704                         // TODO: add a counter to handle how many times one can teleport, and a delay to prevent spam
705                         player.buffs = 0;
706                         return true;
707                 }
708         }
709 }
710
711 bool buffs_RemovePlayer(entity player)
712 {
713         if(player.buff_model)
714         {
715                 delete(player.buff_model);
716                 player.buff_model = NULL;
717         }
718
719         // also reset timers here to prevent them continuing after spectating
720         player.buff_disability_time = 0;
721         player.buff_disability_effect_time = 0;
722
723         return false;
724 }
725 MUTATOR_HOOKFUNCTION(buffs, MakePlayerObserver) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
726 MUTATOR_HOOKFUNCTION(buffs, ClientDisconnect) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
727
728 MUTATOR_HOOKFUNCTION(buffs, CustomizeWaypoint)
729 {
730         entity wp = M_ARGV(0, entity);
731         entity player = M_ARGV(1, entity);
732
733         entity e = WaypointSprite_getviewentity(player);
734
735         // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
736         // but only apply this to real players, not to spectators
737         if((wp.owner.flags & FL_CLIENT) && (wp.owner.buffs & BUFF_INVISIBLE.m_itemid) && (e == player))
738         if(DIFF_TEAM(wp.owner, e))
739                 return true;
740 }
741
742 MUTATOR_HOOKFUNCTION(buffs, FilterItem)
743 {
744         if(autocvar_g_buffs < 0)
745                 return false; // no auto replacing of entities in this mode
746
747         entity item = M_ARGV(0, entity);
748
749         if(autocvar_g_buffs_replace_powerups)
750         {
751                 switch(item.classname)
752                 {
753                         case "item_strength":
754                         case "item_shield":
755                         {
756                                 entity e = spawn();
757                                 buff_SpawnReplacement(e, item);
758                                 return true;
759                         }
760                 }
761         }
762
763         return false;
764 }
765
766 MUTATOR_HOOKFUNCTION(buffs, WeaponRateFactor)
767 {
768         entity player = M_ARGV(1, entity);
769
770         if(player.buffs & BUFF_SPEED.m_itemid)
771                 M_ARGV(0, float) *= autocvar_g_buffs_speed_rate;
772
773         if(time < player.buff_disability_time)
774                 M_ARGV(0, float) *= autocvar_g_buffs_disability_rate;
775 }
776
777 MUTATOR_HOOKFUNCTION(buffs, WeaponSpeedFactor)
778 {
779         entity player = M_ARGV(1, entity);
780
781         if(player.buffs & BUFF_SPEED.m_itemid)
782                 M_ARGV(0, float) *= autocvar_g_buffs_speed_weaponspeed;
783
784         if(time < player.buff_disability_time)
785                 M_ARGV(0, float) *= autocvar_g_buffs_disability_weaponspeed;
786 }
787
788 .bool buff_flight_crouchheld;
789
790 MUTATOR_HOOKFUNCTION(buffs, PlayerPreThink)
791 {
792         entity player = M_ARGV(0, entity);
793
794         if(game_stopped || IS_DEAD(player) || frametime || !IS_PLAYER(player)) return;
795
796         if(player.buffs & BUFF_FLIGHT.m_itemid)
797         {
798                 if(!PHYS_INPUT_BUTTON_CROUCH(player))
799                         player.buff_flight_crouchheld = false;
800                 else if(!player.buff_flight_crouchheld)
801                 {
802                         player.buff_flight_crouchheld = true;
803                         player.gravity *= -1;
804                 }
805         }
806
807         if(time < player.buff_disability_time)
808         if(time >= player.buff_disability_effect_time)
809         {
810                 Send_Effect(EFFECT_SMOKING, player.origin + ((player.mins + player.maxs) * 0.5), '0 0 0', 1);
811                 player.buff_disability_effect_time = time + 0.5;
812         }
813
814         // handle buff lost status
815         // 1: notify everyone else
816         // 2: notify carrier as well
817         int buff_lost = 0;
818
819         if(player.buff_time && player.buffs)
820         if(time >= player.buff_time)
821         {
822                 player.buff_time = 0;
823                 buff_lost = 2;
824         }
825
826         if(STAT(FROZEN, player)) { buff_lost = 1; }
827
828         if(buff_lost)
829         {
830                 if(player.buffs)
831                 {
832                         int buffid = buff_FirstFromFlags(player.buffs).m_id;
833                         if(buff_lost == 2)
834                         {
835                                 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid); // TODO: special timeout message?
836                                 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
837                         }
838                         else
839                                 Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
840                         player.buffs = 0;
841                         PS(player).buff_shield = time + max(0, autocvar_g_buffs_pickup_delay); // always put in a delay, even if small
842                 }
843         }
844
845         if(player.buffs & BUFF_MAGNET.m_itemid)
846         {
847                 vector pickup_size;
848                 IL_EACH(g_items, it.itemdef,
849                 {
850                         if(it.buffs)
851                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_buff;
852                         else
853                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_item;
854
855                         if(boxesoverlap(player.absmin - pickup_size, player.absmax + pickup_size, it.absmin, it.absmax))
856                         {
857                                 if(gettouch(it))
858                                         gettouch(it)(it, player);
859                         }
860                 });
861         }
862
863         if(player.buffs & BUFF_AMMO.m_itemid)
864         {
865                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
866                 {
867                         .entity weaponentity = weaponentities[slot];
868                         if(player.(weaponentity).clip_size)
869                                 player.(weaponentity).clip_load = player.(weaponentity).(weapon_load[player.(weaponentity).m_switchweapon.m_id]) = player.(weaponentity).clip_size;
870                 }
871         }
872
873         if((player.buffs & BUFF_INVISIBLE.m_itemid) && (player.oldbuffs & BUFF_INVISIBLE.m_itemid))
874                 player.alpha = ((autocvar_g_buffs_invisible_alpha) ? autocvar_g_buffs_invisible_alpha : -1); // powerups reset alpha, so we must enforce this (TODO)
875
876         if(player.buffs & BUFF_MEDIC.m_itemid)
877         if(time >= player.buff_medic_healtime)
878         {
879                 buff_Medic_Heal(player);
880                 player.buff_medic_healtime = time + autocvar_g_buffs_medic_heal_delay;
881         }
882
883 #define BUFF_ONADD(b) if ( (player.buffs & (b).m_itemid) && !(player.oldbuffs & (b).m_itemid))
884 #define BUFF_ONREM(b) if (!(player.buffs & (b).m_itemid) &&  (player.oldbuffs & (b).m_itemid))
885
886         if(player.buffs != player.oldbuffs)
887         {
888                 entity buff = buff_FirstFromFlags(player.buffs);
889                 float bufftime = buff != BUFF_Null ? buff.m_time(buff) : 0;
890                 player.buff_time = (bufftime) ? time + bufftime : 0;
891
892                 BUFF_ONADD(BUFF_AMMO)
893                 {
894                         player.buff_ammo_prev_infitems = (player.items & IT_UNLIMITED_WEAPON_AMMO);
895                         player.items |= IT_UNLIMITED_WEAPON_AMMO;
896
897                         if(player.buffs & BUFF_AMMO.m_itemid)
898                         {
899                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
900                                 {
901                                         .entity weaponentity = weaponentities[slot];
902                                         if(player.(weaponentity).clip_load)
903                                                 player.(weaponentity).buff_ammo_prev_clipload = player.(weaponentity).clip_load;
904                                         if(player.(weaponentity).clip_size)
905                                                 player.(weaponentity).clip_load = player.(weaponentity).(weapon_load[player.(weaponentity).m_switchweapon.m_id]) = player.(weaponentity).clip_size;
906                                 }
907                         }
908                 }
909
910                 BUFF_ONREM(BUFF_AMMO)
911                 {
912                         if(player.buff_ammo_prev_infitems)
913                                 player.items |= IT_UNLIMITED_WEAPON_AMMO;
914                         else
915                                 player.items &= ~IT_UNLIMITED_WEAPON_AMMO;
916
917                         if(player.buffs & BUFF_AMMO.m_itemid)
918                         {
919                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
920                                 {
921                                         .entity weaponentity = weaponentities[slot];
922                                         if(player.(weaponentity).buff_ammo_prev_clipload)
923                                                 player.(weaponentity).clip_load = player.(weaponentity).buff_ammo_prev_clipload;
924                                 }
925                         }
926                 }
927
928                 BUFF_ONADD(BUFF_INVISIBLE)
929                 {
930                         if(time < player.strength_finished && g_instagib)
931                                 player.buff_invisible_prev_alpha = default_player_alpha; // we don't want to save the powerup's alpha, as player may lose the powerup while holding the buff
932                         else
933                                 player.buff_invisible_prev_alpha = player.alpha;
934                         player.alpha = autocvar_g_buffs_invisible_alpha;
935                 }
936
937                 BUFF_ONREM(BUFF_INVISIBLE)
938                 {
939                         if(time < player.strength_finished && g_instagib)
940                                 player.alpha = autocvar_g_instagib_invis_alpha;
941                         else
942                                 player.alpha = player.buff_invisible_prev_alpha;
943                 }
944
945                 BUFF_ONADD(BUFF_FLIGHT)
946                 {
947                         player.buff_flight_oldgravity = player.gravity;
948                         if(!player.gravity)
949                                 player.gravity = 1;
950                 }
951
952                 BUFF_ONREM(BUFF_FLIGHT)
953                         player.gravity = ((player.trigger_gravity_check) ? player.trigger_gravity_check.enemy.gravity : player.buff_flight_oldgravity);
954
955                 player.oldbuffs = player.buffs;
956                 if(player.buffs)
957                 {
958                         if(!player.buff_model)
959                                 buffs_BuffModel_Spawn(player);
960
961                         player.buff_model.color = buff.m_color;
962                         player.buff_model.glowmod = buff_GlowColor(player.buff_model);
963                         player.buff_model.skin = buff.m_skin;
964
965                         player.effects |= EF_NOSHADOW;
966                 }
967                 else
968                 {
969                         if(player.buff_model)
970                                 delete(player.buff_model);
971                         player.buff_model = NULL;
972
973                         player.effects &= ~(EF_NOSHADOW);
974                 }
975         }
976
977         if(player.buff_model)
978         {
979                 player.buff_model.effects = player.effects;
980                 player.buff_model.effects |= EF_LOWPRECISION;
981                 player.buff_model.effects = player.buff_model.effects & EFMASK_CHEAP; // eat performance
982
983                 player.buff_model.alpha = player.alpha;
984         }
985
986 #undef BUFF_ONADD
987 #undef BUFF_ONREM
988 }
989
990 MUTATOR_HOOKFUNCTION(buffs, SpectateCopy)
991 {
992         entity spectatee = M_ARGV(0, entity);
993         entity client = M_ARGV(1, entity);
994
995         client.buffs = spectatee.buffs;
996         client.buff_time = spectatee.buff_time;
997 }
998
999 MUTATOR_HOOKFUNCTION(buffs, PlayerRegen)
1000 {
1001         entity player = M_ARGV(0, entity);
1002
1003         if(player.buffs & BUFF_MEDIC.m_itemid)
1004         {
1005                 M_ARGV(2, float) = autocvar_g_buffs_medic_rot; // rot_mod
1006                 M_ARGV(4, float) = M_ARGV(1, float) = autocvar_g_buffs_medic_max; // limit_mod = max_mod
1007                 M_ARGV(2, float) = autocvar_g_buffs_medic_regen; // regen_mod
1008         }
1009
1010         if(player.buffs & BUFF_SPEED.m_itemid)
1011                 M_ARGV(2, float) = autocvar_g_buffs_speed_regen; // regen_mod
1012 }
1013
1014 REPLICATE(cvar_cl_buffs_autoreplace, bool, "cl_buffs_autoreplace");
1015
1016 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsString)
1017 {
1018         if(autocvar_g_buffs > 0) // only report as a mutator if they're enabled
1019                 M_ARGV(0, string) = strcat(M_ARGV(0, string), ":Buffs");
1020 }
1021
1022 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsPrettyString)
1023 {
1024         if(autocvar_g_buffs > 0)
1025                 M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Buffs");
1026 }
1027
1028 void buffs_DelayedInit(entity this)
1029 {
1030         if(autocvar_g_buffs_spawn_count > 0)
1031         if(find(NULL, classname, "item_buff") == NULL)
1032         {
1033                 float i;
1034                 for(i = 0; i < autocvar_g_buffs_spawn_count; ++i)
1035                 {
1036                         entity e = spawn();
1037                         e.spawnflags |= 64; // always randomize
1038                         e.velocity = randomvec() * 250; // this gets reset anyway if random location works
1039                         buff_Init(e);
1040                 }
1041         }
1042 }