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