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