Rename a few parameters and locals named x, y, z
[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_AddFloat(it.m_itemid, 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         this.bot_pickup = true;
349         this.bot_pickupevalfunc = commodity_pickupevalfunc;
350         this.bot_pickupbasevalue = 1000;
351         IL_PUSH(g_items, this);
352         setthink(this, buff_Think);
353         settouch(this, buff_Touch);
354         this.reset = buff_Reset;
355         this.nextthink = time + 0.1;
356         this.gravity = 1;
357         set_movetype(this, MOVETYPE_TOSS);
358         this.scale = 1;
359         this.skin = buff.m_skin;
360         this.effects = EF_FULLBRIGHT | EF_STARDUST | EF_NOSHADOW;
361         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY;
362         setcefc(this, buff_Customize);
363         //this.gravity = 100;
364         this.color = buff.m_color;
365         this.glowmod = buff_GlowColor(this);
366         buff_SetCooldown(this, autocvar_g_buffs_cooldown_activate + game_starttime);
367         this.buff_active = !this.buff_activetime;
368         this.pflags = PFLAGS_FULLDYNAMIC;
369
370         if(this.spawnflags & 1)
371                 this.noalign = true;
372
373         if(this.noalign)
374                 set_movetype(this, MOVETYPE_NONE); // reset by random location
375
376         setmodel(this, MDL_BUFF);
377         setsize(this, BUFF_MIN, BUFF_MAX);
378
379         if(cvar("g_buffs_random_location") || (this.spawnflags & 64))
380                 buff_Respawn(this);
381 }
382
383 void buff_Init_Compat(entity ent, entity replacement)
384 {
385         if (ent.spawnflags & 2)
386                 ent.team = NUM_TEAM_1;
387         else if (ent.spawnflags & 4)
388                 ent.team = NUM_TEAM_2;
389
390         ent.buffs = replacement.m_itemid;
391
392         buff_Init(ent);
393 }
394
395 void buff_SpawnReplacement(entity ent, entity old)
396 {
397         setorigin(ent, old.origin);
398         ent.angles = old.angles;
399         ent.noalign = (old.noalign || (old.spawnflags & 1));
400
401         buff_Init(ent);
402 }
403
404 void buff_Vengeance_DelayedDamage(entity this)
405 {
406         if(this.enemy)
407                 Damage(this.enemy, this.owner, this.owner, this.dmg, DEATH_BUFF.m_id, this.enemy.origin, '0 0 0');
408
409         delete(this);
410         return;
411 }
412
413 // note: only really useful in teamplay
414 void buff_Medic_Heal(entity this)
415 {
416         FOREACH_CLIENT(IS_PLAYER(it) && it != this && vdist(it.origin - this.origin, <=, autocvar_g_buffs_medic_heal_range),
417         {
418                 if(SAME_TEAM(it, this))
419                 if(it.health < autocvar_g_balance_health_regenstable)
420                 {
421                         Send_Effect(EFFECT_HEALING, it.origin, '0 0 0', 1);
422                         it.health = bound(0, it.health + autocvar_g_buffs_medic_heal_amount, autocvar_g_balance_health_regenstable);
423                 }
424         });
425 }
426
427 float buff_Inferno_CalculateTime(float damg, float offset_x, float offset_y, float intersect_x, float intersect_y, float base)
428 {
429         return offset_y + (intersect_y - offset_y) * logn(((damg - offset_x) * ((base - 1) / intersect_x)) + 1, base);
430 }
431
432 // mutator hooks
433 MUTATOR_HOOKFUNCTION(buffs, PlayerDamage_SplitHealthArmor)
434 {
435         entity frag_target = M_ARGV(2, entity);
436         float frag_deathtype = M_ARGV(6, float);
437         float frag_damage = M_ARGV(7, float);
438
439         if(frag_deathtype == DEATH_BUFF.m_id) { return; }
440
441         if(frag_target.buffs & BUFF_RESISTANCE.m_itemid)
442         {
443                 vector v = healtharmor_applydamage(50, autocvar_g_buffs_resistance_blockpercent, frag_deathtype, frag_damage);
444                 M_ARGV(4, float) = v.x; // take
445                 M_ARGV(5, float) = v.y; // save
446         }
447 }
448
449 MUTATOR_HOOKFUNCTION(buffs, PlayerDamage_Calculate)
450 {
451         entity frag_attacker = M_ARGV(1, entity);
452         entity frag_target = M_ARGV(2, entity);
453         float frag_deathtype = M_ARGV(3, float);
454         float frag_damage = M_ARGV(4, float);
455         vector frag_force = M_ARGV(6, vector);
456
457         if(frag_deathtype == DEATH_BUFF.m_id) { return; }
458
459         if(frag_target.buffs & BUFF_SPEED.m_itemid)
460         if(frag_target != frag_attacker)
461                 frag_damage *= autocvar_g_buffs_speed_damage_take;
462
463         if(frag_target.buffs & BUFF_MEDIC.m_itemid)
464         if((frag_target.health - frag_damage) <= 0)
465         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
466         if(frag_attacker)
467         if(random() <= autocvar_g_buffs_medic_survive_chance)
468                 frag_damage = max(5, frag_target.health - autocvar_g_buffs_medic_survive_health);
469
470         if(frag_target.buffs & BUFF_JUMP.m_itemid)
471         if(frag_deathtype == DEATH_FALL.m_id)
472                 frag_damage = 0;
473
474         if(frag_target.buffs & BUFF_VENGEANCE.m_itemid)
475         if(frag_attacker)
476         if(frag_attacker != frag_target)
477         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
478         {
479                 entity dmgent = spawn();
480
481                 dmgent.dmg = frag_damage * autocvar_g_buffs_vengeance_damage_multiplier;
482                 dmgent.enemy = frag_attacker;
483                 dmgent.owner = frag_target;
484                 setthink(dmgent, buff_Vengeance_DelayedDamage);
485                 dmgent.nextthink = time + 0.1;
486         }
487
488         if(frag_target.buffs & BUFF_BASH.m_itemid)
489         if(frag_attacker != frag_target)
490                 frag_force = '0 0 0';
491
492         if(frag_attacker.buffs & BUFF_BASH.m_itemid)
493         if(frag_force)
494         if(frag_attacker == frag_target)
495                 frag_force *= autocvar_g_buffs_bash_force_self;
496         else
497                 frag_force *= autocvar_g_buffs_bash_force;
498
499         if(frag_attacker.buffs & BUFF_DISABILITY.m_itemid)
500         if(frag_target != frag_attacker)
501                 frag_target.buff_disability_time = time + autocvar_g_buffs_disability_slowtime;
502
503         if(frag_target.buffs & BUFF_INFERNO.m_itemid)
504         {
505                 if(frag_deathtype == DEATH_FIRE.m_id)
506                         frag_damage = 0;
507                 if(frag_deathtype == DEATH_LAVA.m_id)
508                         frag_damage *= 0.5; // TODO: cvarize?
509         }
510
511         if(frag_attacker.buffs & BUFF_LUCK.m_itemid)
512         if(frag_attacker != frag_target)
513         if(autocvar_g_buffs_luck_damagemultiplier > 0)
514         if(random() <= autocvar_g_buffs_luck_chance)
515                 frag_damage *= autocvar_g_buffs_luck_damagemultiplier;
516
517         if(frag_attacker.buffs & BUFF_INFERNO.m_itemid)
518         if(frag_target != frag_attacker) {
519                 float btime = buff_Inferno_CalculateTime(
520                         frag_damage,
521                         0,
522                         autocvar_g_buffs_inferno_burntime_min_time,
523                         autocvar_g_buffs_inferno_burntime_target_damage,
524                         autocvar_g_buffs_inferno_burntime_target_time,
525                         autocvar_g_buffs_inferno_burntime_factor
526                 );
527                 Fire_AddDamage(frag_target, frag_attacker, (frag_damage * autocvar_g_buffs_inferno_damagemultiplier), btime, DEATH_BUFF.m_id);
528         }
529
530         // this... is ridiculous (TODO: fix!)
531         if(frag_attacker.buffs & BUFF_VAMPIRE.m_itemid)
532         if(!frag_target.vehicle)
533         if(!ITEM_DAMAGE_NEEDKILL(frag_deathtype))
534         if(!IS_DEAD(frag_target))
535         if(IS_PLAYER(frag_target) || IS_MONSTER(frag_target))
536         if(frag_attacker != frag_target)
537         if(!STAT(FROZEN, frag_target))
538         if(frag_target.takedamage)
539         if(DIFF_TEAM(frag_attacker, frag_target))
540         {
541                 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);
542                 if(frag_target.armorvalue)
543                         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);
544         }
545
546         M_ARGV(4, float) = frag_damage;
547         M_ARGV(6, vector) = frag_force;
548 }
549
550 MUTATOR_HOOKFUNCTION(buffs, PlayerSpawn)
551 {
552         entity player = M_ARGV(0, entity);
553
554         player.buffs = 0;
555         player.buff_time = 0;
556         // reset timers here to prevent them continuing after re-spawn
557         player.buff_disability_time = 0;
558         player.buff_disability_effect_time = 0;
559 }
560
561 .float stat_sv_maxspeed;
562 .float stat_sv_airspeedlimit_nonqw;
563 .float stat_sv_jumpvelocity;
564
565 MUTATOR_HOOKFUNCTION(buffs, PlayerPhysics)
566 {
567         entity player = M_ARGV(0, entity);
568
569         if(player.buffs & BUFF_SPEED.m_itemid)
570         {
571                 player.stat_sv_maxspeed *= autocvar_g_buffs_speed_speed;
572                 player.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_speed_speed;
573         }
574
575         if(time < player.buff_disability_time)
576         {
577                 player.stat_sv_maxspeed *= autocvar_g_buffs_disability_speed;
578                 player.stat_sv_airspeedlimit_nonqw *= autocvar_g_buffs_disability_speed;
579         }
580
581         if(player.buffs & BUFF_JUMP.m_itemid)
582         {
583                 // automatically reset, no need to worry
584                 player.stat_sv_jumpvelocity = autocvar_g_buffs_jump_height;
585         }
586 }
587
588 MUTATOR_HOOKFUNCTION(buffs, PlayerJump)
589 {
590         entity player = M_ARGV(0, entity);
591
592         if(player.buffs & BUFF_JUMP.m_itemid)
593                 M_ARGV(1, float) = autocvar_g_buffs_jump_height;
594 }
595
596 MUTATOR_HOOKFUNCTION(buffs, MonsterMove)
597 {
598         entity mon = M_ARGV(0, entity);
599
600         if(time < mon.buff_disability_time)
601         {
602                 M_ARGV(1, float) *= autocvar_g_buffs_disability_speed; // run speed
603                 M_ARGV(2, float) *= autocvar_g_buffs_disability_speed; // walk speed
604         }
605 }
606
607 MUTATOR_HOOKFUNCTION(buffs, PlayerDies)
608 {
609         entity frag_target = M_ARGV(2, entity);
610
611         if(frag_target.buffs)
612         {
613                 int buffid = buff_FirstFromFlags(frag_target.buffs).m_id;
614                 Send_Notification(NOTIF_ALL_EXCEPT, frag_target, MSG_INFO, INFO_ITEM_BUFF_LOST, frag_target.netname, buffid);
615                 frag_target.buffs = 0;
616
617                 if(frag_target.buff_model)
618                 {
619                         delete(frag_target.buff_model);
620                         frag_target.buff_model = NULL;
621                 }
622         }
623 }
624
625 MUTATOR_HOOKFUNCTION(buffs, PlayerUseKey, CBC_ORDER_FIRST)
626 {
627         if(MUTATOR_RETURNVALUE || gameover) { return; }
628
629         entity player = M_ARGV(0, entity);
630
631         if(player.buffs)
632         {
633                 int buffid = buff_FirstFromFlags(player.buffs).m_id;
634                 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid);
635                 Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
636
637                 player.buffs = 0;
638                 player.buff_time = 0; // already notified
639                 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
640                 return true;
641         }
642 }
643
644 MUTATOR_HOOKFUNCTION(buffs, ForbidThrowCurrentWeapon)
645 {
646         if(MUTATOR_RETURNVALUE || gameover) { return; }
647         entity player = M_ARGV(0, entity);
648
649         if(player.buffs & BUFF_SWAPPER.m_itemid)
650         {
651                 float best_distance = autocvar_g_buffs_swapper_range;
652                 entity closest = NULL;
653                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
654                         if(!IS_DEAD(it) && !STAT(FROZEN, it) && !it.vehicle)
655                         if(DIFF_TEAM(it, player))
656                         {
657                                 float test = vlen2(player.origin - it.origin);
658                                 if(test <= best_distance * best_distance)
659                                 {
660                                         best_distance = sqrt(test);
661                                         closest = it;
662                                 }
663                         }
664                 ));
665
666                 if(closest)
667                 {
668                         vector my_org, my_vel, my_ang, their_org, their_vel, their_ang;
669
670                         my_org = player.origin;
671                         my_vel = player.velocity;
672                         my_ang = player.angles;
673                         their_org = closest.origin;
674                         their_vel = closest.velocity;
675                         their_ang = closest.angles;
676
677                         Drop_Special_Items(closest);
678
679                         MUTATOR_CALLHOOK(PortalTeleport, player); // initiate flag dropper
680
681                         setorigin(player, their_org);
682                         setorigin(closest, my_org);
683
684                         closest.velocity = my_vel;
685                         closest.angles = my_ang;
686                         closest.fixangle = true;
687                         closest.oldorigin = my_org;
688                         closest.oldvelocity = my_vel;
689                         player.velocity = their_vel;
690                         player.angles = their_ang;
691                         player.fixangle = true;
692                         player.oldorigin = their_org;
693                         player.oldvelocity = their_vel;
694
695                         // set pusher so player gets the kill if they fall into void
696                         closest.pusher = player;
697                         closest.pushltime = time + autocvar_g_maxpushtime;
698                         closest.istypefrag = PHYS_INPUT_BUTTON_CHAT(closest);
699
700                         Send_Effect(EFFECT_ELECTRO_COMBO, their_org, '0 0 0', 1);
701                         Send_Effect(EFFECT_ELECTRO_COMBO, my_org, '0 0 0', 1);
702
703                         sound(player, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
704                         sound(closest, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NORM);
705
706                         // TODO: add a counter to handle how many times one can teleport, and a delay to prevent spam
707                         player.buffs = 0;
708                         return true;
709                 }
710         }
711 }
712
713 bool buffs_RemovePlayer(entity player)
714 {
715         if(player.buff_model)
716         {
717                 delete(player.buff_model);
718                 player.buff_model = NULL;
719         }
720
721         // also reset timers here to prevent them continuing after spectating
722         player.buff_disability_time = 0;
723         player.buff_disability_effect_time = 0;
724
725         return false;
726 }
727 MUTATOR_HOOKFUNCTION(buffs, MakePlayerObserver) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
728 MUTATOR_HOOKFUNCTION(buffs, ClientDisconnect) { entity player = M_ARGV(0, entity); return buffs_RemovePlayer(player); }
729
730 MUTATOR_HOOKFUNCTION(buffs, CustomizeWaypoint)
731 {
732         entity wp = M_ARGV(0, entity);
733         entity player = M_ARGV(1, entity);
734
735         entity e = WaypointSprite_getviewentity(player);
736
737         // if you have the invisibility powerup, sprites ALWAYS are restricted to your team
738         // but only apply this to real players, not to spectators
739         if((wp.owner.flags & FL_CLIENT) && (wp.owner.buffs & BUFF_INVISIBLE.m_itemid) && (e == player))
740         if(DIFF_TEAM(wp.owner, e))
741                 return true;
742 }
743
744 MUTATOR_HOOKFUNCTION(buffs, OnEntityPreSpawn, CBC_ORDER_LAST)
745 {
746         entity ent = M_ARGV(0, entity);
747
748         if(autocvar_g_buffs_replace_powerups)
749         switch(ent.classname)
750         {
751                 case "item_strength":
752                 case "item_invincible":
753                 {
754                         entity e = spawn();
755                         buff_SpawnReplacement(e, ent);
756                         return true;
757                 }
758         }
759 }
760
761 MUTATOR_HOOKFUNCTION(buffs, WeaponRateFactor)
762 {
763         entity player = M_ARGV(1, entity);
764
765         if(player.buffs & BUFF_SPEED.m_itemid)
766                 M_ARGV(0, float) *= autocvar_g_buffs_speed_rate;
767
768         if(time < player.buff_disability_time)
769                 M_ARGV(0, float) *= autocvar_g_buffs_disability_rate;
770 }
771
772 MUTATOR_HOOKFUNCTION(buffs, WeaponSpeedFactor)
773 {
774         entity player = M_ARGV(1, entity);
775
776         if(player.buffs & BUFF_SPEED.m_itemid)
777                 M_ARGV(0, float) *= autocvar_g_buffs_speed_weaponspeed;
778
779         if(time < player.buff_disability_time)
780                 M_ARGV(0, float) *= autocvar_g_buffs_disability_weaponspeed;
781 }
782
783 MUTATOR_HOOKFUNCTION(buffs, PlayerPreThink)
784 {
785         entity player = M_ARGV(0, entity);
786
787         if(gameover || IS_DEAD(player)) { return; }
788
789         if(time < player.buff_disability_time)
790         if(time >= player.buff_disability_effect_time)
791         {
792                 Send_Effect(EFFECT_SMOKING, player.origin + ((player.mins + player.maxs) * 0.5), '0 0 0', 1);
793                 player.buff_disability_effect_time = time + 0.5;
794         }
795
796         // handle buff lost status
797         // 1: notify everyone else
798         // 2: notify carrier as well
799         int buff_lost = 0;
800
801         if(player.buff_time && player.buffs)
802         if(time >= player.buff_time)
803         {
804                 player.buff_time = 0;
805                 buff_lost = 2;
806         }
807
808         if(STAT(FROZEN, player)) { buff_lost = 1; }
809
810         if(buff_lost)
811         {
812                 if(player.buffs)
813                 {
814                         int buffid = buff_FirstFromFlags(player.buffs).m_id;
815                         if(buff_lost == 2)
816                         {
817                                 Send_Notification(NOTIF_ONE, player, MSG_MULTI, ITEM_BUFF_DROP, buffid); // TODO: special timeout message?
818                                 sound(player, CH_TRIGGER, SND_BUFF_LOST, VOL_BASE, ATTN_NORM);
819                         }
820                         else
821                                 Send_Notification(NOTIF_ALL_EXCEPT, player, MSG_INFO, INFO_ITEM_BUFF_LOST, player.netname, buffid);
822                         player.buffs = 0;
823                 }
824         }
825
826         if(player.buffs & BUFF_MAGNET.m_itemid)
827         {
828                 vector pickup_size;
829                 IL_EACH(g_items, it.itemdef,
830                 {
831                         if(it.buffs)
832                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_buff;
833                         else
834                                 pickup_size = '1 1 1' * autocvar_g_buffs_magnet_range_item;
835
836                         if(boxesoverlap(player.absmin - pickup_size, player.absmax + pickup_size, it.absmin, it.absmax))
837                         {
838                                 if(gettouch(it))
839                                         gettouch(it)(it, player);
840                         }
841                 });
842         }
843
844         if(player.buffs & BUFF_AMMO.m_itemid)
845         if(player.clip_size)
846                 player.clip_load = player.(weapon_load[PS(player).m_switchweapon.m_id]) = player.clip_size;
847
848         if((player.buffs & BUFF_INVISIBLE.m_itemid) && (player.oldbuffs & BUFF_INVISIBLE.m_itemid))
849         if(player.alpha != autocvar_g_buffs_invisible_alpha)
850                 player.alpha = autocvar_g_buffs_invisible_alpha; // powerups reset alpha, so we must enforce this (TODO)
851
852         if(player.buffs & BUFF_MEDIC.m_itemid)
853         if(time >= player.buff_medic_healtime)
854         {
855                 buff_Medic_Heal(player);
856                 player.buff_medic_healtime = time + autocvar_g_buffs_medic_heal_delay;
857         }
858
859 #define BUFF_ONADD(b) if ( (player.buffs & (b).m_itemid) && !(player.oldbuffs & (b).m_itemid))
860 #define BUFF_ONREM(b) if (!(player.buffs & (b).m_itemid) &&  (player.oldbuffs & (b).m_itemid))
861
862         if(player.buffs != player.oldbuffs)
863         {
864                 entity buff = buff_FirstFromFlags(player.buffs);
865                 float bufftime = buff != BUFF_Null ? buff.m_time(buff) : 0;
866                 player.buff_time = (bufftime) ? time + bufftime : 0;
867
868                 BUFF_ONADD(BUFF_AMMO)
869                 {
870                         player.buff_ammo_prev_infitems = (player.items & IT_UNLIMITED_WEAPON_AMMO);
871                         player.items |= IT_UNLIMITED_WEAPON_AMMO;
872
873                         if(player.clip_load)
874                                 player.buff_ammo_prev_clipload = player.clip_load;
875                         player.clip_load = player.(weapon_load[PS(player).m_switchweapon.m_id]) = player.clip_size;
876                 }
877
878                 BUFF_ONREM(BUFF_AMMO)
879                 {
880                         if(player.buff_ammo_prev_infitems)
881                                 player.items |= IT_UNLIMITED_WEAPON_AMMO;
882                         else
883                                 player.items &= ~IT_UNLIMITED_WEAPON_AMMO;
884
885                         if(player.buff_ammo_prev_clipload)
886                                 player.clip_load = player.buff_ammo_prev_clipload;
887                 }
888
889                 BUFF_ONADD(BUFF_INVISIBLE)
890                 {
891                         if(time < player.strength_finished && g_instagib)
892                                 player.alpha = autocvar_g_instagib_invis_alpha;
893                         else
894                                 player.alpha = player.buff_invisible_prev_alpha;
895                         player.alpha = autocvar_g_buffs_invisible_alpha;
896                 }
897
898                 BUFF_ONREM(BUFF_INVISIBLE)
899                         player.alpha = player.buff_invisible_prev_alpha;
900
901                 player.oldbuffs = player.buffs;
902                 if(player.buffs)
903                 {
904                         if(!player.buff_model)
905                                 buffs_BuffModel_Spawn(player);
906
907                         player.buff_model.color = buff.m_color;
908                         player.buff_model.glowmod = buff_GlowColor(player.buff_model);
909                         player.buff_model.skin = buff.m_skin;
910
911                         player.effects |= EF_NOSHADOW;
912                 }
913                 else
914                 {
915                         delete(player.buff_model);
916                         player.buff_model = NULL;
917
918                         player.effects &= ~(EF_NOSHADOW);
919                 }
920         }
921
922         if(player.buff_model)
923         {
924                 player.buff_model.effects = player.effects;
925                 player.buff_model.effects |= EF_LOWPRECISION;
926                 player.buff_model.effects = player.buff_model.effects & EFMASK_CHEAP; // eat performance
927
928                 player.buff_model.alpha = player.alpha;
929         }
930
931 #undef BUFF_ONADD
932 #undef BUFF_ONREM
933 }
934
935 MUTATOR_HOOKFUNCTION(buffs, SpectateCopy)
936 {
937         entity spectatee = M_ARGV(0, entity);
938         entity client = M_ARGV(1, entity);
939
940         client.buffs = spectatee.buffs;
941 }
942
943 MUTATOR_HOOKFUNCTION(buffs, VehicleEnter)
944 {
945         entity player = M_ARGV(0, entity);
946         entity veh = M_ARGV(1, entity);
947
948         veh.buffs = player.buffs;
949         player.buffs = 0;
950         veh.buff_time = max(0, player.buff_time - time);
951         player.buff_time = 0;
952 }
953
954 MUTATOR_HOOKFUNCTION(buffs, VehicleExit)
955 {
956         entity player = M_ARGV(0, entity);
957         entity veh = M_ARGV(1, entity);
958
959         player.buffs = player.oldbuffs = veh.buffs;
960         veh.buffs = 0;
961         player.buff_time = time + veh.buff_time;
962         veh.buff_time = 0;
963 }
964
965 MUTATOR_HOOKFUNCTION(buffs, PlayerRegen)
966 {
967         entity player = M_ARGV(0, entity);
968
969         if(player.buffs & BUFF_MEDIC.m_itemid)
970         {
971                 M_ARGV(2, float) = autocvar_g_buffs_medic_rot; // rot_mod
972                 M_ARGV(4, float) = M_ARGV(1, float) = autocvar_g_buffs_medic_max; // limit_mod = max_mod
973                 M_ARGV(2, float) = autocvar_g_buffs_medic_regen; // regen_mod
974         }
975
976         if(player.buffs & BUFF_SPEED.m_itemid)
977                 M_ARGV(2, float) = autocvar_g_buffs_speed_regen; // regen_mod
978 }
979
980 REPLICATE(cvar_cl_buffs_autoreplace, bool, "cl_buffs_autoreplace");
981
982 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsString)
983 {
984         M_ARGV(0, string) = strcat(M_ARGV(0, string), ":Buffs");
985 }
986
987 MUTATOR_HOOKFUNCTION(buffs, BuildMutatorsPrettyString)
988 {
989         M_ARGV(0, string) = strcat(M_ARGV(0, string), ", Buffs");
990 }
991
992 void buffs_DelayedInit(entity this)
993 {
994         if(autocvar_g_buffs_spawn_count > 0)
995         if(find(NULL, classname, "item_buff") == NULL)
996         {
997                 float i;
998                 for(i = 0; i < autocvar_g_buffs_spawn_count; ++i)
999                 {
1000                         entity e = spawn();
1001                         e.spawnflags |= 64; // always randomize
1002                         e.velocity = randomvec() * 250; // this gets reset anyway if random location works
1003                         buff_Init(e);
1004                 }
1005         }
1006 }