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