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